]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tvint.py
283e179f51af2d56b65e40e26e578833279f068d
[bup.git] / lib / bup / t / tvint.py
1 from io import BytesIO
2
3 from wvtest import *
4
5 from bup import vint
6 from buptest import no_lingering_errors
7
8
9 def encode_and_decode_vuint(x):
10     f = BytesIO()
11     vint.write_vuint(f, x)
12     return vint.read_vuint(BytesIO(f.getvalue()))
13
14
15 @wvtest
16 def test_vuint():
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())
22
23
24 def encode_and_decode_vint(x):
25     f = BytesIO()
26     vint.write_vint(f, x)
27     return vint.read_vint(BytesIO(f.getvalue()))
28
29
30 @wvtest
31 def test_vint():
32     with no_lingering_errors():
33         values = (0, 1, 42, 64, 10**16)
34         for x in values:
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())
39
40
41 def encode_and_decode_bvec(x):
42     f = BytesIO()
43     vint.write_bvec(f, x)
44     return vint.read_bvec(BytesIO(f.getvalue()))
45
46
47 @wvtest
48 def test_bvec():
49     with no_lingering_errors():
50         values = ('', 'x', 'foo', '\0', '\0foo', 'foo\0bar\0')
51         for x in values:
52             WVPASSEQ(encode_and_decode_bvec(x), x)
53         WVEXCEPT(EOFError, vint.read_bvec, BytesIO())
54         outf = 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')
60         vint.skip_bvec(inf)
61         WVPASSEQ(vint.read_bvec(inf), 'bax')
62
63
64 def pack_and_unpack(types, *values):
65     data = vint.pack(types, *values)
66     return vint.unpack(types, data)
67
68
69 @wvtest
70 def test_pack_and_unpack():
71     with no_lingering_errors():
72         tests = [('', []),
73                  ('s', ['foo']),
74                  ('ss', ['foo', 'bar']),
75                  ('sV', ['foo', 0]),
76                  ('sv', ['foo', -1]),
77                  ('V', [0]),
78                  ('Vs', [0, 'foo']),
79                  ('VV', [0, 1]),
80                  ('Vv', [0, -1]),
81                  ('v', [0]),
82                  ('vs', [0, 'foo']),
83                  ('vV', [0, 1]),
84                  ('vv', [0, -1])]
85         for test in tests:
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', '')