|
1 | 1 | import unittest
|
2 | 2 |
|
3 |
| -from ..periph import PeripheralInfo |
| 3 | +from ..periph import * |
4 | 4 | from ..memory import MemoryMap
|
5 | 5 | from .. import event
|
6 | 6 |
|
7 | 7 |
|
| 8 | +class ConstantBoolTestCase(unittest.TestCase): |
| 9 | + def test_init(self): |
| 10 | + a = ConstantBool(True) |
| 11 | + b = ConstantBool(False) |
| 12 | + self.assertTrue(a.value) |
| 13 | + self.assertFalse(b.value) |
| 14 | + |
| 15 | + def test_value_wrong(self): |
| 16 | + with self.assertRaisesRegex(TypeError, r"Value must be a bool, not 'foo'"): |
| 17 | + ConstantBool("foo") |
| 18 | + |
| 19 | + def test_repr(self): |
| 20 | + self.assertEqual(repr(ConstantBool(True)), "ConstantBool(True)") |
| 21 | + |
| 22 | + |
| 23 | +class ConstantIntTestCase(unittest.TestCase): |
| 24 | + def test_init(self): |
| 25 | + c = ConstantInt(5, width=8, signed=True) |
| 26 | + self.assertEqual(c.value, 5) |
| 27 | + self.assertEqual(c.width, 8) |
| 28 | + self.assertEqual(c.signed, True) |
| 29 | + |
| 30 | + def test_init_default(self): |
| 31 | + c = ConstantInt(5) |
| 32 | + self.assertEqual(c.value, 5) |
| 33 | + self.assertEqual(c.width, 3) |
| 34 | + self.assertEqual(c.signed, False) |
| 35 | + |
| 36 | + def test_value_wrong(self): |
| 37 | + with self.assertRaisesRegex(TypeError, r"Value must be an integer, not 'foo'"): |
| 38 | + ConstantInt("foo") |
| 39 | + |
| 40 | + def test_width_wrong(self): |
| 41 | + with self.assertRaisesRegex(TypeError, r"Width must be an integer, not 'foo'"): |
| 42 | + ConstantInt(5, width="foo") |
| 43 | + |
| 44 | + def test_width_overflow(self): |
| 45 | + with self.assertRaisesRegex(ValueError, |
| 46 | + r"Width must be greater than or equal to the number of bits needed to represent 5"): |
| 47 | + ConstantInt(5, width=1) |
| 48 | + |
| 49 | + def test_signed_wrong(self): |
| 50 | + with self.assertRaisesRegex(TypeError, r"Signedness must be a bool, not 'foo'"): |
| 51 | + ConstantInt(5, signed="foo") |
| 52 | + |
| 53 | + def test_repr(self): |
| 54 | + self.assertEqual( |
| 55 | + repr(ConstantInt(-5, width=8, signed=True)), |
| 56 | + "ConstantInt(-5, width=8, signed=True)" |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +class ConstantMapTestCase(unittest.TestCase): |
| 61 | + def test_init(self): |
| 62 | + constant_map = ConstantMap(A=5, B=True, C=ConstantBool(False)) |
| 63 | + self.assertEqual( |
| 64 | + repr(constant_map), "ConstantMap([" |
| 65 | + "('A', ConstantInt(5, width=3, signed=False)), " |
| 66 | + "('B', ConstantBool(True)), " |
| 67 | + "('C', ConstantBool(False))])", |
| 68 | + ) |
| 69 | + |
| 70 | + def test_init_wrong_value(self): |
| 71 | + with self.assertRaisesRegex(TypeError, |
| 72 | + r"Constant value must be an instance of ConstantValue, not \('foo', 'bar'\)"): |
| 73 | + ConstantMap(A=("foo", "bar")) |
| 74 | + |
| 75 | + def test_getitem(self): |
| 76 | + a = ConstantInt(1) |
| 77 | + b = ConstantBool(False) |
| 78 | + constant_map = ConstantMap(A=a, B=b) |
| 79 | + self.assertIs(constant_map["A"], a) |
| 80 | + self.assertIs(constant_map["B"], b) |
| 81 | + |
| 82 | + def test_iter(self): |
| 83 | + a = ConstantInt(1) |
| 84 | + b = ConstantBool(False) |
| 85 | + constant_map = ConstantMap(B=b, A=a) |
| 86 | + self.assertEqual(list(constant_map.items()), [ |
| 87 | + ("B", b), |
| 88 | + ("A", a), |
| 89 | + ]) |
| 90 | + |
| 91 | + def test_len(self): |
| 92 | + a = ConstantInt(1) |
| 93 | + b = ConstantBool(False) |
| 94 | + constant_map = ConstantMap(B=b, A=a) |
| 95 | + self.assertEqual(len(constant_map), 2) |
| 96 | + |
| 97 | + |
8 | 98 | class PeripheralInfoTestCase(unittest.TestCase):
|
9 | 99 | def test_memory_map(self):
|
10 | 100 | memory_map = MemoryMap(addr_width=1, data_width=8)
|
@@ -48,3 +138,27 @@ def test_irq_wrong(self):
|
48 | 138 | with self.assertRaisesRegex(TypeError,
|
49 | 139 | r"IRQ line must be an instance of event.Source, not 'foo'"):
|
50 | 140 | info = PeripheralInfo(memory_map=memory_map, irq="foo")
|
| 141 | + |
| 142 | + def test_constant_map(self): |
| 143 | + constant_map = ConstantMap() |
| 144 | + memory_map = MemoryMap(addr_width=1, data_width=8) |
| 145 | + info = PeripheralInfo(memory_map=memory_map, constant_map=constant_map) |
| 146 | + self.assertIs(info.constant_map, constant_map) |
| 147 | + |
| 148 | + def test_constant_map_none(self): |
| 149 | + memory_map = MemoryMap(addr_width=1, data_width=8) |
| 150 | + info = PeripheralInfo(memory_map=memory_map, constant_map=None) |
| 151 | + self.assertIsInstance(info.constant_map, ConstantMap) |
| 152 | + self.assertEqual(info.constant_map, {}) |
| 153 | + |
| 154 | + def test_constant_map_default(self): |
| 155 | + memory_map = MemoryMap(addr_width=1, data_width=8) |
| 156 | + info = PeripheralInfo(memory_map=memory_map) |
| 157 | + self.assertIsInstance(info.constant_map, ConstantMap) |
| 158 | + self.assertEqual(info.constant_map, {}) |
| 159 | + |
| 160 | + def test_constant_map_wrong(self): |
| 161 | + memory_map = MemoryMap(addr_width=1, data_width=8) |
| 162 | + with self.assertRaisesRegex(TypeError, |
| 163 | + r"Constant map must be an instance of ConstantMap, not 'foo'"): |
| 164 | + info = PeripheralInfo(memory_map=memory_map, constant_map="foo") |
0 commit comments