]> arthur.barton.de Git - bup.git/blob - test/int/test_vint.py
Remove Client __del__ in favor of context management
[bup.git] / test / int / test_vint.py
1
2 from __future__ import absolute_import
3 from io import BytesIO
4 from itertools import combinations_with_replacement
5
6 from wvpytest import *
7
8 from bup import vint
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 def test_vuint():
18         for x in (0, 1, 42, 128, 10**16, 10**100):
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 def test_vint():
31     values = (0, 1, 42, 64, 10**16, 10**100)
32     for x in values:
33         WVPASSEQ(encode_and_decode_vint(x), x)
34     for x in [-x for x in values]:
35         WVPASSEQ(encode_and_decode_vint(x), x)
36     WVEXCEPT(EOFError, vint.read_vint, BytesIO())
37     WVEXCEPT(EOFError, vint.read_vint, BytesIO(b"\x80\x80"))
38
39
40 def encode_and_decode_bvec(x):
41     f = BytesIO()
42     vint.write_bvec(f, x)
43     return vint.read_bvec(BytesIO(f.getvalue()))
44
45
46 def test_bvec():
47     values = (b'', b'x', b'foo', b'\0', b'\0foo', b'foo\0bar\0')
48     for x in values:
49         WVPASSEQ(encode_and_decode_bvec(x), x)
50     WVEXCEPT(EOFError, vint.read_bvec, BytesIO())
51     outf = BytesIO()
52     for x in (b'foo', b'bar', b'baz', b'bax'):
53         vint.write_bvec(outf, x)
54     inf = BytesIO(outf.getvalue())
55     WVPASSEQ(vint.read_bvec(inf), b'foo')
56     WVPASSEQ(vint.read_bvec(inf), b'bar')
57     vint.skip_bvec(inf)
58     WVPASSEQ(vint.read_bvec(inf), b'bax')
59
60
61 def pack_and_unpack(types, *values):
62     data = vint.pack(types, *values)
63     return vint.unpack(types, data)
64
65
66 def test_pack_and_unpack():
67     candidates = (('s', b''),
68                   ('s', b'x'),
69                   ('s', b'foo'),
70                   ('s', b'foo' * 10),
71                   ('v', -10**100),
72                   ('v', -1),
73                   ('v', 0),
74                   ('v', 1),
75                   ('v', -10**100),
76                   ('V', 0),
77                   ('V', 1),
78                   ('V', 10**100))
79     WVPASSEQ(pack_and_unpack(''), [])
80     for f, v in candidates:
81         WVPASSEQ(pack_and_unpack(f, v), [v])
82     for (f1, v1), (f2, v2) in combinations_with_replacement(candidates, r=2):
83         WVPASSEQ(pack_and_unpack(f1 + f2, v1, v2), [v1, v2])
84     WVEXCEPT(Exception, vint.pack, 's')
85     WVEXCEPT(Exception, vint.pack, 's', 'foo', 'bar')
86     WVEXCEPT(Exception, vint.pack, 'x', 1)
87     WVEXCEPT(Exception, vint.unpack, 's', '')
88     WVEXCEPT(Exception, vint.unpack, 'x', '')