2 from __future__ import absolute_import
8 from buptest import no_lingering_errors
11 def encode_and_decode_vuint(x):
13 vint.write_vuint(f, x)
14 return vint.read_vuint(BytesIO(f.getvalue()))
19 with no_lingering_errors():
20 for x in (0, 1, 42, 128, 10**16):
21 WVPASSEQ(encode_and_decode_vuint(x), x)
22 WVEXCEPT(Exception, vint.write_vuint, BytesIO(), -1)
23 WVEXCEPT(EOFError, vint.read_vuint, BytesIO())
26 def encode_and_decode_vint(x):
29 return vint.read_vint(BytesIO(f.getvalue()))
34 with no_lingering_errors():
35 values = (0, 1, 42, 64, 10**16)
37 WVPASSEQ(encode_and_decode_vint(x), x)
38 for x in [-x for x in values]:
39 WVPASSEQ(encode_and_decode_vint(x), x)
40 WVEXCEPT(EOFError, vint.read_vint, BytesIO())
41 WVEXCEPT(EOFError, vint.read_vint, BytesIO(b"\x80\x80"))
44 def encode_and_decode_bvec(x):
47 return vint.read_bvec(BytesIO(f.getvalue()))
52 with no_lingering_errors():
53 values = (b'', b'x', b'foo', b'\0', b'\0foo', b'foo\0bar\0')
55 WVPASSEQ(encode_and_decode_bvec(x), x)
56 WVEXCEPT(EOFError, vint.read_bvec, BytesIO())
58 for x in (b'foo', b'bar', b'baz', b'bax'):
59 vint.write_bvec(outf, x)
60 inf = BytesIO(outf.getvalue())
61 WVPASSEQ(vint.read_bvec(inf), b'foo')
62 WVPASSEQ(vint.read_bvec(inf), b'bar')
64 WVPASSEQ(vint.read_bvec(inf), b'bax')
67 def pack_and_unpack(types, *values):
68 data = vint.pack(types, *values)
69 return vint.unpack(types, data)
73 def test_pack_and_unpack():
74 with no_lingering_errors():
77 ('ss', [b'foo', b'bar']),
89 (types, values) = test
90 WVPASSEQ(pack_and_unpack(types, *values), values)
91 WVEXCEPT(Exception, vint.pack, 's')
92 WVEXCEPT(Exception, vint.pack, 's', 'foo', 'bar')
93 WVEXCEPT(Exception, vint.pack, 'x', 1)
94 WVEXCEPT(Exception, vint.unpack, 's', '')
95 WVEXCEPT(Exception, vint.unpack, 'x', '')