Fix bug with comparisons in selectable_int.py
authorMichael Nolan <mtnolan2640@gmail.com>
Thu, 7 May 2020 15:17:48 +0000 (11:17 -0400)
committerMichael Nolan <mtnolan2640@gmail.com>
Thu, 7 May 2020 15:41:42 +0000 (11:41 -0400)
src/soc/decoder/selectable_int.py

index 1227335e45256aadd2021a9d0475fcbdf86e4ba7..b6414f90d9483d5a4ecdbe02b965aae3ca9ffb5b 100644 (file)
@@ -235,7 +235,7 @@ class SelectableInt:
             assert other.bits == self.bits
             other = other.value
         if isinstance(other, int):
-            return other >= self.value
+            return onebit(self.value >= other.value)
         assert False
 
     def __le__(self, other):
@@ -246,7 +246,7 @@ class SelectableInt:
             assert other.bits == self.bits
             other = other.value
         if isinstance(other, int):
-            return onebit(other <= self.value)
+            return onebit(self.value <= other)
         assert False
 
     def __gt__(self, other):
@@ -257,7 +257,7 @@ class SelectableInt:
             assert other.bits == self.bits
             other = other.value
         if isinstance(other, int):
-            return onebit(other > self.value)
+            return onebit(self.value > other)
         assert False
 
     def __lt__(self, other):
@@ -268,7 +268,7 @@ class SelectableInt:
             assert other.bits == self.bits
             other = other.value
         if isinstance(other, int):
-            return onebit(other < self.value)
+            return onebit(self.value < other)
         assert False
 
     def __eq__(self, other):
@@ -413,5 +413,21 @@ class SelectableIntTestCase(unittest.TestCase):
             b = eval(repr(a))
             self.assertEqual(a, b)
 
+    def test_cmp(self):
+        a = SelectableInt(10, bits=8)
+        b = SelectableInt(5, bits=8)
+        self.assertTrue(a > b)
+        self.assertFalse(a < b)
+        self.assertTrue(a != b)
+        self.assertFalse(a == b)
+
+    def test_unsigned(self):
+        a = SelectableInt(0x80, bits=8)
+        b = SelectableInt(0x7f, bits=8)
+        self.assertTrue(a > b)
+        self.assertFalse(a < b)
+        self.assertTrue(a != b)
+        self.assertFalse(a == b)
+
 if __name__ == "__main__":
     unittest.main()