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