Skip to content

Commit

Permalink
Merge pull request #129 from SpiNNakerManchester/tester
Browse files Browse the repository at this point in the history
Increase testing
  • Loading branch information
dkfellows authored Mar 25, 2021
2 parents 73d1a10 + 1a1f913 commit 7728491
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 13 deletions.
11 changes: 5 additions & 6 deletions spinn_utilities/default_ordered_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,18 @@ def __reduce__(self):
args = tuple()
else:
args = self.default_factory,
return type(self), args, None, None, self.items()
return type(self), args, None, None, iter(self.items())

def copy(self):
return self.__copy__()
return type(self)(self.default_factory, self.items())

def __copy__(self):
return type(self)(self.default_factory, self)
__copy__ = copy

def __deepcopy__(self, memo):
import copy
return type(self)(self.default_factory,
copy.deepcopy(self.items()))
copy.deepcopy(iter(self.items())))

def __repr__(self):
return 'OrderedDefaultDict(%s, %s)' % (
return 'DefaultOrderedDict(%s, %s)' % (
self.default_factory, OrderedDict.__repr__(self))
9 changes: 3 additions & 6 deletions spinn_utilities/make_tools/file_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,16 +424,13 @@ def _short_log(self, line_num):
front += TOKEN
if match.endswith("f"):
front += "%x"
elif match.endswith("F"):
front += "%x" + TOKEN + "%x"
else:
front += match
if match.endswith("f"):
back += ", float_to_int({})".format(parts[i + 1])
elif match.endswith("F"):
front += "%x" + TOKEN + "%x"
back += DOUBLE_HEX.format(parts[i + 1])
else:
back += ", {}".format(parts[i+1])
front += match
back += ", {}".format(parts[i + 1])
front += '", {}'.format(self._message_id)
back += ");"
return original, front + back
Expand Down
2 changes: 1 addition & 1 deletion spinn_utilities/ordered_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def peek(self, last=True):
if last:
return next(reversed(self))
else:
return next(self)
return next(iter(self))

def __len__(self):
return len(self._map)
Expand Down
7 changes: 7 additions & 0 deletions unittests/make_tools/mock_src/weird,file.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ static String woops = "log_info(";

log_info("test -three %f", -3.0f);

log_info("test double %F", -3.0d);

log_info("test slash // %f", 3/2);

log_info("this is ok");

//log_info("this is just a comment");
Expand Down Expand Up @@ -99,6 +103,9 @@ static String woops = "log_info(";
ds_regions->version >> VERSION_SHIFT,
ds_regions->version & VERSION_MASK);

log_inf("blah",
")",
"more");
/* comment */ log_info("comment before");

fluff fluff
Expand Down
5 changes: 5 additions & 0 deletions unittests/make_tools/test.dict
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Id,Preface,Original
1006,[INFO] (weird;file.c: 25): ,Test %u for alan); so there!
1007,[INFO] (weird;file.c: 29): ,\t back off = %u, time between spikes %u
1008,[DEBUG] (weird;file.c: 33): ,the neuron %d has been determined to not spike
A weird lone got in
Another, weird, line, got, in
And one, with, three parts
1009,[WARNING] (weird;file.c: 37): ,Inside a loop
1010,[INFO] (weird;file.c: 39): ,then a space
1011,[INFO] (weird;file.c: 42): ,then a newline simple
Expand All @@ -20,3 +23,5 @@ Id,Preface,Original
1018,[DEBUG] (bit_field.c: 71): ,%c
1019,[DEBUG] (bit_field.c: 75): ,\n
1020,[DEBUG] (bit_field.c: 111): ,%08x\n
1021,[INFO] (weird;file.c: 32):, a float %f
1022,[INFO] (weird;file.c: 34):, a double %F
35 changes: 35 additions & 0 deletions unittests/make_tools/test_file_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,38 @@ def test_convert(self):
assert("then a backslash comment on a middle line" in data)
assert("then a standard comment on a middle line" in data)
assert("comment before" in data)

def test_exceptions(self):
file_name = "weird,file.c"
src = os.path.join("mock_src", file_name)
dest = os.path.join("modified_src", file_name)
dict = dest + "dict"
convertor = FileConverter(src, dest, dict)
try:
convertor.split_by_comma_plus(None, 12)
assert False
except Exception as ex1:
assert "Unexpected line" in str(ex1)
try:
convertor._short_log(12)
assert False
except Exception as ex2:
assert "Unexpected line" in str(ex2)
try:
convertor._log_full = '"test %f", -3.0f, 12);'
convertor._short_log(12)
assert False
except Exception as ex2:
assert "Too many" in str(ex2)
try:
convertor._log_full = '"test %f %i", -3.0f);'
convertor._short_log(12)
assert False
except Exception as ex2:
assert "Too few" in str(ex2)
try:
convertor._log_full = '"test %1", -3.0f);'
convertor._short_log(12)
assert False
except Exception as ex2:
assert "Unexpected formatString" in str(ex2)
20 changes: 20 additions & 0 deletions unittests/make_tools/test_replacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ def test_tab(self):
" spikes 20"
assert (message == new)

def test_float(self):
replacer = Replacer(os.path.join(PATH, "test"))
new = replacer.replace("1021" + TOKEN + "3f800000")
message = "[INFO] (weird;file.c: 32): a float 1.0"
assert (message == new)

def test_double(self):
replacer = Replacer(os.path.join(PATH, "test"))
new = replacer.replace(
"1022" + TOKEN + "40379999" + TOKEN + "9999999a")
message = "[INFO] (weird;file.c: 34): a double 23.6"
assert (message == new)

def test_bad(self):
replacer = Replacer(os.path.join(PATH, "test"))
new = replacer.replace("1007" + TOKEN + "10")
# An exception so just output the input
message = "1007" + TOKEN + "10"
assert (message == new)

def near_equals(self, a, b):
diff = a - b
if diff == 0:
Expand Down
36 changes: 36 additions & 0 deletions unittests/matrix/test_double.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,39 @@ def test_singleSet_inverted():
double[1] = new_data
assert double["foo"][1] == "One"
assert double[1]["bar"] == "Two"


def test_errors():
matrix = DemoMatrix()
double = DoubleDict(xtype=str, ytype=int, matrix=matrix)
new_data = {"foo": "One", "bar": "Two"}
double[1] = new_data
try:
double[1.1]
assert False
except KeyError as ex:
assert "unexpected type" in str(ex)
try:
double["Bar"] = "Opps"
except ValueError as ex:
assert "Value must of type dict" in str(ex)
try:
double["Bar"] = [2, "Opps"]
assert False
except ValueError as ex:
assert "Value must of type dict" in str(ex)
try:
double["Bar"] = {2.1: "Opps"}
assert False
except ValueError as ex:
assert "All keys in the value" in str(ex)
try:
double[2] = {1: "Opps"}
assert False
except ValueError as ex:
assert "All keys in the value" in str(ex)
try:
double[2.1] = {1: "Opps"}
assert False
except KeyError as ex:
assert "unexpected type" in str(ex)
21 changes: 21 additions & 0 deletions unittests/test_conf_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ def test_new_section_validation(tmpdir, default_config):
conf_loader.load_config(CFGFILE, [CFGPATH], validation_cfg="blank.cfg")


def test_types(tmpdir, default_config):
with tmpdir.as_cwd():
f = tmpdir.join(CFGFILE)
default_config = (
default_config +
"[machine]\nmachineName=foo\nVersion=5\nsize=4.6\nb1=True\n"
"b2=true\nb3=False\nb4=False\nOops=None\n")
f.write(default_config)
config = conf_loader.load_config(
CFGFILE, [CFGPATH], validation_cfg="blank.cfg")
assert config.get_int("machine", "version") == 5
assert config.get_int("machine", "oops") is None
assert config.get_float("machine", "size") == 4.6
assert config.get_float("machine", "oops") is None
assert config.get_bool("machine", "b1")
assert config.get_bool("machine", "b2")
assert not config.get_bool("machine", "b3")
assert not config.get_bool("machine", "b4")
assert config.get_bool("machine", "oops") is None


def test_dead_section(tmpdir, default_config):
with tmpdir.as_cwd():
f = tmpdir.join(CFGFILE)
Expand Down
25 changes: 25 additions & 0 deletions unittests/test_default_ordered_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import copy
import pickle
import pytest
from spinn_utilities.default_ordered_dict import DefaultOrderedDict
from spinn_utilities.ordered_set import OrderedSet
Expand Down Expand Up @@ -53,3 +55,26 @@ def test_keys_in_order():
c = o["c"]
assert a == b == c
assert tuple(o) == ("a", "b", "c")


def test_callable():
try:
DefaultOrderedDict("Not callable")
assert False
except TypeError:
pass


def test_special_methods():
o = DefaultOrderedDict(list)
o["gamma"].append("bacon")
# test _-reduce
pickle.dumps(o)
# test copy
o2 = o.copy()
assert o2["gamma"] == ["bacon"]
o3 = copy.deepcopy(o)
assert o3["gamma"] == ["bacon"]
a = repr(o)
b = repr(o3)
assert a == b
22 changes: 22 additions & 0 deletions unittests/test_index_is_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (c) 2017-2018 The University of Manchester
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from spinn_utilities.index_is_value import IndexIsValue


def test_index_is_value():
a = IndexIsValue()
assert a[10] == 10
assert len(a) > 1000
32 changes: 32 additions & 0 deletions unittests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import logging
import tempfile
from spinn_utilities.log import (ConfiguredFilter, ConfiguredFormatter)
from spinn_utilities.log import FormatAdapter, LogLevelTooHighException


Expand Down Expand Up @@ -98,6 +99,37 @@ class Exn(Exception):
assert len(logger._repeat_log()) == 1


class MockConfig1(object):

def get(self, section, option):
return "debug"

def has_section(self, section):
return False


def test_weird_config1():
ConfiguredFormatter(MockConfig1())
ConfiguredFilter(MockConfig1())


class MockConfig2(object):

def get(self, section, option):
return "critical"

def has_section(self, section):
return True

def has_option(self, section, option):
return option == 'warning'


def test_weird_config2():
ConfiguredFormatter(MockConfig2())
ConfiguredFilter(MockConfig2())


def test_waning_file():
log = MockLog()
logger = FormatAdapter(log)
Expand Down
1 change: 1 addition & 0 deletions unittests/test_logger_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def test_error(self):
logger_utils.error_once(logger, "a log Error")
logger_utils.warn_once(logger, "another log error")
logger_utils.warn_once(logger, "a log warning")
logger_utils.error_once(logger, "a log Error")
log_checker.assert_logs_contains_once(
"ERROR", lc.records, "a log Error")
log_checker.assert_logs_error_not_contains(
Expand Down
15 changes: 15 additions & 0 deletions unittests/test_ordered_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,19 @@ def test_peek():
o.add(2)
o.add(3)
p1 = o.peek()
p2 = o.pop()
assert p1 == 3
assert p1 == p2
p3 = o.peek(last=False)
assert p3 == 1
p4 = o.pop(last=False)
assert p4 == p3


def test_reverse():
o = OrderedSet()
o.add(1)
o.add(2)
o.add(3)
a = list(reversed(o))
assert a == [3, 2, 1]

0 comments on commit 7728491

Please sign in to comment.