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