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())
43 def encode_and_decode_bvec(x):
46 return vint.read_bvec(BytesIO(f.getvalue()))
51 with no_lingering_errors():
52 values = ('', 'x', 'foo', '\0', '\0foo', 'foo\0bar\0')
54 WVPASSEQ(encode_and_decode_bvec(x), x)
55 WVEXCEPT(EOFError, vint.read_bvec, BytesIO())
57 for x in ('foo', 'bar', 'baz', 'bax'):
58 vint.write_bvec(outf, x)
59 inf = BytesIO(outf.getvalue())
60 WVPASSEQ(vint.read_bvec(inf), 'foo')
61 WVPASSEQ(vint.read_bvec(inf), 'bar')
63 WVPASSEQ(vint.read_bvec(inf), 'bax')
66 def pack_and_unpack(types, *values):
67 data = vint.pack(types, *values)
68 return vint.unpack(types, data)
72 def test_pack_and_unpack():
73 with no_lingering_errors():
76 ('ss', ['foo', 'bar']),
88 (types, values) = test
89 WVPASSEQ(pack_and_unpack(types, *values), values)
90 WVEXCEPT(Exception, vint.pack, 's')
91 WVEXCEPT(Exception, vint.pack, 's', 'foo', 'bar')
92 WVEXCEPT(Exception, vint.pack, 'x', 1)
93 WVEXCEPT(Exception, vint.unpack, 's', '')
94 WVEXCEPT(Exception, vint.unpack, 'x', '')