def test_deserialize(self): u1 = UIntBase(2) self.assertEqual(hash(u1), 0) # deserialize from stream now. hash should equal hash of b'ab', # because size was set to 2. u1.Deserialize(BinaryReader(BytesIO(b'abc'))) self.assertEqual(hash(u1), 25185)
def test_initialization(self): u0 = UIntBase(0) self.assertEqual(hash(u0), 0) u1 = UIntBase(10) self.assertEqual(hash(u1), 0) u2 = UIntBase(3, bytearray(b'abc')) self.assertEqual(hash(u2), 6513249) u3 = UIntBase(3, b'abc') self.assertEqual(hash(u3), 6513249)
def test_eq(self): u1 = UIntBase(3, b'abc') # Should equal self.assertEqual(u1, u1) self.assertEqual(u1, UIntBase(3, b'abc')) self.assertEqual(u1, UIntBase(3, bytearray(b'abc'))) # Should not equal self.assertNotEqual(u1, None) self.assertNotEqual(u1, 123) self.assertNotEqual(u1, 'abc') self.assertNotEqual(u1, UIntBase(3, b'abd'))
def test_dunder_methods(self): u1 = UInt160(b'12345678901234567890') u1b = UIntBase(20, b'12345678901234567890') u_larger = UInt160(b'12345678901234567891') u_smaller = UInt160(b'12345678901234567880') self.assertTrue(u1 < u_larger) self.assertTrue(u1 <= u_larger) self.assertTrue(u1 <= u1b) self.assertTrue(u1 == u1b) self.assertTrue(u1b == u1) self.assertTrue(u1 >= u1b) self.assertTrue(u1 >= u_smaller) self.assertTrue(u1 > u_smaller)
def test_serialize(self): data = b'abc' stream = BytesIO() u1 = UIntBase(3, bytearray(data)) u1.Serialize(BinaryWriter(stream)) self.assertEqual(stream.getvalue(), data) stream = BytesIO() u1 = UIntBase(3, data) u1.Serialize(BinaryWriter(stream)) self.assertEqual(stream.getvalue(), data)
def test_compareto_invalid_datatype(self): u1 = UIntBase(20, b'12345678901234567890') with self.assertRaises(Exception): self.assertEqual(u1.CompareTo('asd'), 0) with self.assertRaises(Exception): self.assertEqual(u1.CompareTo(b'asd'), 0) with self.assertRaises(Exception): self.assertEqual(u1.CompareTo(123), 0) # Cannot compare uints with different lengths with self.assertRaises(Exception): a = UInt256(b'12345678901234567890123456789012') b = UIntBase(20, b'12345678901234567890') a.CompareTo(b)
def test_compareto_valid(self): u1 = UInt160(b'12345678901234567890') # Same value should return 0 u2 = UIntBase(20, b'12345678901234567890') self.assertEqual(u1.CompareTo(u2), 0) # Higher digit in 'other' should return -1 u2 = UIntBase(20, b'12345678901234567891') self.assertEqual(u1.CompareTo(u2), -1) # Lower digit in 'other' should return 1 u2 = UIntBase(20, b'12345678901234567980') self.assertEqual(u1.CompareTo(u2), 1) # CompareTo across different UIntBase subclasses data = b'12345678901234567890' self.assertEqual(UInt160(data).CompareTo(UIntBase(len(data), data)), 0) self.assertEqual(UIntBase(len(data), data).CompareTo(UInt160(data)), 0) data = b'12345678901234567890123456789012' self.assertEqual(UInt256(data).CompareTo(UIntBase(len(data), data)), 0) self.assertEqual(UIntBase(len(data), data).CompareTo(UInt256(data)), 0)
def test_tobytes(self): u1 = UIntBase(3, b'abc') self.assertEqual(u1.ToBytes(), b'636261')
def test_to0xstring(self): u1 = UIntBase(3, b'abc') self.assertEqual(u1.To0xString(), '0x636261')
def test_tostring2(self): u1 = UIntBase(3, b'abc') self.assertEqual(u1.ToString2(), '616263')
def test_str(self): u1 = UIntBase(3, b'abc') self.assertEqual(str(u1), '636261')
def test_size(self): u1 = UIntBase(3, bytearray(b'abc')) self.assertEqual(u1.Size, 3)
def test_initialization_with_invalid_datatype(self): with self.assertRaises(Exception): UIntBase(3, 'abc')
def test_initialization_with_invalid_datalen(self): with self.assertRaises(Exception): UIntBase(3, bytearray(b'abcd'))
def test_initialization_with_bytearray(self): u1 = UIntBase(3, bytearray(b'abc')) self.assertEqual(hash(u1), 6513249)
def test_toarray(self): data = b'abc' u1 = UIntBase(3, data) self.assertEqual(u1.ToArray(), data)
def test_hash_code(self): x = UIntBase(num_bytes=4, data=bytearray.fromhex('DEADBEEF')) self.assertEqual(x.GetHashCode(), 4022250974) x = UIntBase(num_bytes=2, data=bytearray.fromhex('1122')) self.assertEqual(x.GetHashCode(), 8721)