keys = (keys,)
# Map: 2 -> "0010"; "0010" -> "0010"
new_keys = ()
+ key_mask = (1 << len(self.test)) - 1
for key in keys:
if isinstance(key, str):
key = "".join(key.split()) # remove whitespace
elif isinstance(key, int):
- key = format(key, "b").rjust(len(self.test), "0")
+ key = format(key & key_mask, "b").rjust(len(self.test), "0")
elif isinstance(key, Enum):
- key = format(key.value, "b").rjust(len(self.test), "0")
+ key = format(key.value & key_mask, "b").rjust(len(self.test), "0")
else:
raise TypeError("Object {!r} cannot be used as a switch key"
.format(key))
def test_initial(self):
i = Initial()
self.assertEqual(i.shape(), unsigned(1))
+
+
+class SwitchTestCase(FHDLTestCase):
+ def test_default_case(self):
+ s = Switch(Const(0), {None: []})
+ self.assertEqual(s.cases, {(): []})
+
+ def test_int_case(self):
+ s = Switch(Const(0, 8), {10: []})
+ self.assertEqual(s.cases, {("00001010",): []})
+
+ def test_int_neg_case(self):
+ s = Switch(Const(0, 8), {-10: []})
+ self.assertEqual(s.cases, {("11110110",): []})
+
+ def test_enum_case(self):
+ s = Switch(Const(0, UnsignedEnum), {UnsignedEnum.FOO: []})
+ self.assertEqual(s.cases, {("01",): []})
+
+ def test_str_case(self):
+ s = Switch(Const(0, 8), {"0000 11\t01": []})
+ self.assertEqual(s.cases, {("00001101",): []})
+
+ def test_two_cases(self):
+ s = Switch(Const(0, 8), {("00001111", 123): []})
+ self.assertEqual(s.cases, {("00001111", "01111011"): []})