6 from buptest import no_lingering_errors
9 def encode_and_decode_vuint(x):
11 vint.write_vuint(f, x)
12 return vint.read_vuint(BytesIO(f.getvalue()))
17 with no_lingering_errors():
18 for x in (0, 1, 42, 128, 10**16):
19 WVPASSEQ(encode_and_decode_vuint(x), x)
20 WVEXCEPT(Exception, vint.write_vuint, BytesIO(), -1)
21 WVEXCEPT(EOFError, vint.read_vuint, BytesIO())
24 def encode_and_decode_vint(x):
27 return vint.read_vint(BytesIO(f.getvalue()))
32 with no_lingering_errors():
33 values = (0, 1, 42, 64, 10**16)
35 WVPASSEQ(encode_and_decode_vint(x), x)
36 for x in [-x for x in values]:
37 WVPASSEQ(encode_and_decode_vint(x), x)
38 WVEXCEPT(EOFError, vint.read_vint, BytesIO())
41 def encode_and_decode_bvec(x):
44 return vint.read_bvec(BytesIO(f.getvalue()))
49 with no_lingering_errors():
50 values = ('', 'x', 'foo', '\0', '\0foo', 'foo\0bar\0')
52 WVPASSEQ(encode_and_decode_bvec(x), x)
53 WVEXCEPT(EOFError, vint.read_bvec, BytesIO())
55 for x in ('foo', 'bar', 'baz', 'bax'):
56 vint.write_bvec(outf, x)
57 inf = BytesIO(outf.getvalue())
58 WVPASSEQ(vint.read_bvec(inf), 'foo')
59 WVPASSEQ(vint.read_bvec(inf), 'bar')
61 WVPASSEQ(vint.read_bvec(inf), 'bax')
64 def pack_and_unpack(types, *values):
65 data = vint.pack(types, *values)
66 return vint.unpack(types, data)
70 def test_pack_and_unpack():
71 with no_lingering_errors():
74 ('ss', ['foo', 'bar']),
86 (types, values) = test
87 WVPASSEQ(pack_and_unpack(types, *values), values)
88 WVEXCEPT(Exception, vint.pack, 's')
89 WVEXCEPT(Exception, vint.pack, 's', 'foo', 'bar')
90 WVEXCEPT(Exception, vint.pack, 'x', 1)
91 WVEXCEPT(Exception, vint.unpack, 's', '')
92 WVEXCEPT(Exception, vint.unpack, 'x', '')