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