1 from test import test_support 2 import unittest 3 4 import xdrlib 5 6 class XDRTest(unittest.TestCase): 7 8 def test_xdr(self): 9 p = xdrlib.Packer() 10 11 s = 'hello world' 12 a = ['what', 'is', 'hapnin', 'doctor'] 13 14 p.pack_int(42) 15 p.pack_int(-17) 16 p.pack_uint(9) 17 p.pack_bool(True) 18 p.pack_bool(False) 19 p.pack_uhyper(45L) 20 p.pack_float(1.9) 21 p.pack_double(1.9) 22 p.pack_string(s) 23 p.pack_list(range(5), p.pack_uint) 24 p.pack_array(a, p.pack_string) 25 26 # now verify 27 data = p.get_buffer() 28 up = xdrlib.Unpacker(data) 29 30 self.assertEqual(up.get_position(), 0) 31 32 self.assertEqual(up.unpack_int(), 42) 33 self.assertEqual(up.unpack_int(), -17) 34 self.assertEqual(up.unpack_uint(), 9) 35 self.assertTrue(up.unpack_bool() is True) 36 37 # remember position 38 pos = up.get_position() 39 self.assertTrue(up.unpack_bool() is False) 40 41 # rewind and unpack again 42 up.set_position(pos) 43 self.assertTrue(up.unpack_bool() is False) 44 45 self.assertEqual(up.unpack_uhyper(), 45L) 46 self.assertAlmostEqual(up.unpack_float(), 1.9) 47 self.assertAlmostEqual(up.unpack_double(), 1.9) 48 self.assertEqual(up.unpack_string(), s) 49 self.assertEqual(up.unpack_list(up.unpack_uint), range(5)) 50 self.assertEqual(up.unpack_array(up.unpack_string), a) 51 up.done() 52 self.assertRaises(EOFError, up.unpack_uint) 53 54 def test_main(): 55 test_support.run_unittest(XDRTest) 56 57 if __name__ == "__main__": 58 test_main() 59