Skip to content

Commit 6b41793

Browse files
authored
Merge pull request RustPython#2736 from fanninpm/test-xdrlib
Add test_xdrlib from CPython 3.8
2 parents 6a94c90 + 5f9d59a commit 6b41793

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

Lib/test/test_xdrlib.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import unittest
2+
3+
import xdrlib
4+
5+
class XDRTest(unittest.TestCase):
6+
7+
def test_xdr(self):
8+
p = xdrlib.Packer()
9+
10+
s = b'hello world'
11+
a = [b'what', b'is', b'hapnin', b'doctor']
12+
13+
p.pack_int(42)
14+
p.pack_int(-17)
15+
p.pack_uint(9)
16+
p.pack_bool(True)
17+
p.pack_bool(False)
18+
p.pack_uhyper(45)
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)
24+
25+
# now verify
26+
data = p.get_buffer()
27+
up = xdrlib.Unpacker(data)
28+
29+
self.assertEqual(up.get_position(), 0)
30+
31+
self.assertEqual(up.unpack_int(), 42)
32+
self.assertEqual(up.unpack_int(), -17)
33+
self.assertEqual(up.unpack_uint(), 9)
34+
self.assertTrue(up.unpack_bool() is True)
35+
36+
# remember position
37+
pos = up.get_position()
38+
self.assertTrue(up.unpack_bool() is False)
39+
40+
# rewind and unpack again
41+
up.set_position(pos)
42+
self.assertTrue(up.unpack_bool() is False)
43+
44+
self.assertEqual(up.unpack_uhyper(), 45)
45+
self.assertAlmostEqual(up.unpack_float(), 1.9)
46+
self.assertAlmostEqual(up.unpack_double(), 1.9)
47+
self.assertEqual(up.unpack_string(), s)
48+
self.assertEqual(up.unpack_list(up.unpack_uint), list(range(5)))
49+
self.assertEqual(up.unpack_array(up.unpack_string), a)
50+
up.done()
51+
self.assertRaises(EOFError, up.unpack_uint)
52+
53+
class ConversionErrorTest(unittest.TestCase):
54+
55+
def setUp(self):
56+
self.packer = xdrlib.Packer()
57+
58+
def assertRaisesConversion(self, *args):
59+
self.assertRaises(xdrlib.ConversionError, *args)
60+
61+
def test_pack_int(self):
62+
self.assertRaisesConversion(self.packer.pack_int, 'string')
63+
64+
def test_pack_uint(self):
65+
self.assertRaisesConversion(self.packer.pack_uint, 'string')
66+
67+
# TODO: RUSTPYTHON
68+
@unittest.expectedFailure
69+
def test_float(self):
70+
self.assertRaisesConversion(self.packer.pack_float, 'string')
71+
72+
# TODO: RUSTPYTHON
73+
@unittest.expectedFailure
74+
def test_double(self):
75+
self.assertRaisesConversion(self.packer.pack_double, 'string')
76+
77+
def test_uhyper(self):
78+
self.assertRaisesConversion(self.packer.pack_uhyper, 'string')
79+
80+
if __name__ == "__main__":
81+
unittest.main()

0 commit comments

Comments
 (0)