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