def test_remaining(self): dg = Datagram() dg.add_uint16(25) dg.add_int64(-354843598374) dgi = dg.iterator() self.assertEqual(dgi.remaining(), 10) s = 'remaining unit test' dg.add_string32(s.encode('utf-8')) self.assertEqual(dgi.remaining(), 10 + 4 + len(s)) self.assertEqual(dgi.get_uint16(), 25) self.assertEqual(dgi.remaining(), 8 + 4 + len(s)) dgi.seek(dg.get_length()) self.assertEqual(dgi.remaining(), 0) dgi.seek(0) self.assertEqual(dgi.remaining(), 10 + 4 + len(s)) dgi.seek(14) self.assertEqual(dgi.remaining(), len(s)) dgi.seek(999) self.assertEqual(dgi.remaining(), 0)
def test_get(self): dg = Datagram() dg.add_uint16(25) dg.add_int64(-354843598374) dg.add_string32('datagram iterator test'.encode('utf-8')) dgi = dg.iterator() self.assertEqual(dgi.get_uint16(), 25) self.assertEqual(dgi.get_int64(), -354843598374) self.assertEqual(dgi.get_string32(), 'datagram iterator test')
def test_overwrite(self): dg = Datagram() dg.add_uint32(2828) pos = dg.tell() self.assertEqual(pos, 4) dg.add_uint16(24) dg.add_int64(-352793) dg.seek(pos) dg.add_uint16(5000) dg.seek(len(dg)) dg.add_string32(b'overwrite') dgi = dg.iterator() self.assertEqual(dgi.get_uint32(), 2828) self.assertEqual(dgi.get_uint16(), 5000) self.assertEqual(dgi.get_int64(), -352793) self.assertEqual(dgi.get_string32(), 'overwrite')