def test_lt(self): self.assertTrue(self.bin_neg_32768 < self.bin_1) self.assertTrue(self.bin_0 < self.bin_1) self.assertFalse(self.bin_0 < self.bin_0) self.assertFalse(self.bin_1 < self.bin_0) self.assertFalse(self.bin_1 < self.bin_1) self.assertIsNotNone(self.bin_1 < self.bin_1) self.assertIsNotNone(self.bin_1 < self.bin_neg_1) self.assertIsNotNone(self.bin_neg_1 < self.bin_neg_1) self.assertIsNotNone(self.bin_neg_1 < self.bin_neg_3) self.assertIsNotNone(self.bin_5 < self.bin_neg_3) self.assertIsNotNone(self.bin_5 < self.bin_1) self.assertFalse(self.bin_1 < self.bin_neg_1) self.assertFalse(self.bin_neg_1 < self.bin_neg_1) self.assertFalse(self.bin_neg_1 < self.bin_neg_3) self.assertFalse(self.bin_5 < self.bin_neg_3) self.assertFalse(self.bin_5 < self.bin_1) self.assertFalse(self.bin_1 < self.bin_neg_5) self.assertTrue(self.bin_neg_5 < self.bin_1) self.assertTrue(self.bin_neg_32768 < self.bin_neg_1) self.assertTrue(self.bin_neg_5 < self.bin_neg_3) self.assertFalse(self.bin_neg_5 < self.bin_neg_5) self.assertTrue(self.bin_neg_32768 < self.bin_32767) self.assertFalse(self.bin_32767 < self.bin_neg_32768) self.assertFalse(self.bin_neg_32767 < self.bin_neg_32767) self.assertTrue(self.bin_neg_32767 < self.bin_32767) self.assertTrue(self.bin_1 < self.bin_32767) self.assertEqual(Binary(), self.bin_0) # make sure original instance is intact self.assertEqual(Binary('1011'), self.bin_neg_5) self.assertEqual(Binary('0101'), self.bin_5) text = inspect.getsource(Binary.__lt__) self.assertTrue('int' not in text)
def test_add(self): self.assertEqual( self.bin_1, self.bin_7 + self.bin_neg_7 + self.bin_5 + self.bin_neg_3 + self.bin_neg_1) self.assertEqual(Binary('1001'), self.bin_neg_7) self.assertEqual(Binary('0111'), self.bin_7) self.assertEqual(self.bin_0, self.bin_0 + self.bin_0) self.assertEqual(self.bin_0, self.bin_1 + self.bin_neg_1) self.assertEqual(Binary(), self.bin_0) # make sure original instance is intact self.assertEqual(self.bin_neg_1, self.bin_0 + self.bin_neg_1) self.assertEqual(self.bin_neg_2, self.bin_neg_1 + self.bin_neg_1) with self.assertRaises(RuntimeError): self.bin_neg_32768 + self.bin_neg_1 with self.assertRaises(RuntimeError): self.bin_32767 + self.bin_1 self.assertEqual(self.bin_neg_32768, self.bin_0 + self.bin_neg_32768) self.assertEqual(self.bin_32767, self.bin_0 + self.bin_32767) self.assertEqual(self.bin_neg_32767, self.bin_1 + self.bin_neg_32768) self.assertEqual(self.bin_32766, self.bin_neg_1 + self.bin_32767) text = inspect.getsource(Binary.__add__) i = text.find('int') while i >= 0: self.assertTrue(text[i + 3] == ')') i = text.find('int', i + 1)
def test_eq(self): self.assertEqual(Binary("0101"), self.bin_5) self.assertEqual(Binary("0101"), self.bin_5) self.assertEqual(Binary("1011"), self.bin_neg_5) self.assertEqual(Binary("1011"), self.bin_neg_5) text = inspect.getsource(Binary.__eq__) self.assertTrue('int' not in text)
def test_repr(self): self.assertEqual("0000000000000000", repr(self.bin_0)) self.assertEqual(Binary(), self.bin_0) # make sure original instance is intact self.assertEqual("0000000000000001", repr(self.bin_1)) self.assertEqual(Binary('01'), self.bin_1) self.assertEqual("1111111111111011", repr(self.bin_neg_5)) self.assertEqual(Binary('1011'), self.bin_neg_5) self.assertEqual("1111111111111001", repr(self.bin_neg_7))
def binfld(self, event, index): txt = self.calculator.binflds[index].get() self.data.bins[index] = Binary(txt) self.calculator.binflds[index].delete(0, "end") self.calculator.binflds[index].insert('end', repr(self.data.bins[index])) self.restore_flds(index)
def test_sub(self): self.assertEqual(self.bin_0, self.bin_7 - self.bin_7) self.assertEqual(self.bin_2, self.bin_neg_3 - self.bin_neg_5) self.assertEqual(self.bin_neg_7, self.bin_neg_5 - self.bin_2) self.assertEqual(self.bin_neg_1, self.bin_5 - self.bin_3 - self.bin_3) self.assertEqual(self.bin_neg_3, self.bin_neg_5 - self.bin_neg_1 - self.bin_neg_1) self.assertEqual(Binary(), self.bin_0) # make sure original instance is intact self.assertEqual(Binary('1001'), self.bin_neg_7) self.assertEqual(Binary('0111'), self.bin_7) with self.assertRaises(RuntimeError): self.bin_neg_32768 - self.bin_1 with self.assertRaises(RuntimeError): self.bin_32767 - self.bin_neg_1 text = inspect.getsource(Binary.__sub__) self.assertTrue('int' not in text)
def test_int(self): self.assertEqual(0, int(self.bin_0)) self.assertEqual(1, int(self.bin_1)) self.assertEqual(2, int(self.bin_2)) self.assertEqual(3, int(self.bin_3)) self.assertEqual(5, int(self.bin_5)) self.assertEqual(7, int(self.bin_7)) self.assertEqual(32767, int(self.bin_32767)) self.assertEqual(-1, int(self.bin_neg_1)) self.assertEqual(-3, int(self.bin_neg_3)) self.assertEqual(-5, int(self.bin_neg_5)) self.assertEqual(-7, int(self.bin_neg_7)) self.assertEqual(-32767, int(self.bin_neg_32767)) self.assertEqual(-32768, int(self.bin_neg_32768)) self.assertEqual(Binary(), self.bin_0) # make sure original instance is intact self.assertEqual(Binary('1001'), self.bin_neg_7) self.assertEqual(Binary('0111'), self.bin_7)
def test_neg(self): self.assertEqual(self.bin_0, -self.bin_0) self.assertTrue(self.bin_0 is not -self.bin_0) self.assertFalse(self.bin_0 is -self.bin_0) self.assertEqual(self.bin_1, -self.bin_neg_1) self.assertEqual(self.bin_neg_1, -self.bin_1) self.assertEqual(self.bin_7, -self.bin_neg_7) self.assertEqual(self.bin_neg_7, -self.bin_7) self.assertEqual(Binary(), self.bin_0) # make sure original instance is intact self.assertEqual(Binary('1001'), self.bin_neg_7) self.assertEqual(Binary('0111'), self.bin_7) with self.assertRaises(RuntimeError): -self.bin_neg_32768 with self.assertRaises(RuntimeError): -self.bin_32767 + self.bin_neg_1 + self.bin_neg_1 text = inspect.getsource(Binary.__neg__) self.assertTrue('int' not in text)
def test_abs(self): self.assertEqual(Binary("0101"), abs(self.bin_5)) self.assertEqual(Binary("0101"), abs(self.bin_neg_5)) self.assertEqual(Binary("0"), abs(self.bin_0)) self.assertEqual(Binary(), self.bin_0) # make sure original instance is intact self.assertEqual(Binary('1011'), self.bin_neg_5) self.assertEqual(Binary('0101'), self.bin_5) self.assertTrue(self.bin_0 is not abs(self.bin_0)) self.assertTrue(self.bin_0 is self.bin_0) self.assertTrue(self.bin_5 is not abs(self.bin_5)) self.assertFalse(self.bin_0 is abs(self.bin_0)) self.assertFalse(self.bin_5 is abs(self.bin_5)) text = inspect.getsource(Binary.__abs__) self.assertTrue('int' not in text)
def setUp(self): self.bin_0 = Binary("") self.bin_1 = Binary("01") self.bin_neg_1 = Binary("1") self.bin_neg_2 = Binary("10") self.bin_neg_3 = Binary("101") self.bin_2 = Binary("010") self.bin_3 = Binary("011") self.bin_5 = Binary("0101") self.bin_neg_5 = Binary("1011") self.bin_7 = Binary("0000000111") self.bin_neg_7 = Binary("1111111001") self.bin_neg_32768 = Binary("1000000000000000") self.bin_32767 = Binary("0111111111111111") self.bin_neg_32767 = Binary("1000000000000001") self.bin_32766 = Binary("0111111111111110")
def __init__(self): self.bins = [Binary(), Binary(), Binary()]