Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: client/libs/logdog/tests/varint_test.py

Issue 1961603002: Add LogDog Python client library. (Closed) Base URL: https://github.com/luci/luci-py@master
Patch Set: Remove "client." from package name, make pylint happy. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « client/libs/logdog/tests/streamname_test.py ('k') | client/libs/logdog/varint.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright 2016 The LUCI Authors. All rights reserved.
3 # Use of this source code is governed by the Apache v2.0 license that can be
4 # found in the LICENSE file.
5
6 import itertools
7 import os
8 import sys
9 import unittest
10 import StringIO
11
12 ROOT_DIR = os.path.dirname(os.path.abspath(os.path.join(
13 __file__, os.pardir, os.pardir, os.pardir)))
14 sys.path.insert(0, ROOT_DIR)
15
16 from libs.logdog import varint
17
18
19 class VarintTestCase(unittest.TestCase):
20
21 def testVarintEncodingRaw(self):
22 for base, exp in (
23 (0, b'\x00'),
24 (1, b'\x01'),
25 (0x7F, b'\x7f'),
26 (0x80, b'\x80\x01'),
27 (0x81, b'\x81\x01'),
28 (0x18080, b'\x80\x81\x06'),
29 ):
30 sio = StringIO.StringIO()
31 count = varint.write_uvarint(sio, base)
32 act = sio.getvalue()
33
34 self.assertEqual(act, exp,
35 "Encoding for %d (%r) doesn't match expected (%r)" % (base, act, exp))
36 self.assertEqual(count, len(act),
37 "Length of %d (%d) doesn't match encoded length (%d)" % (
38 base, len(act), count))
39
40 def testVarintEncodeDecode(self):
41 seed = (b'\x00', b'\x01', b'\x55', b'\x7F', b'\x80', b'\x81', b'\xff')
42 for perm in itertools.permutations(seed):
43 perm = ''.join(perm).encode('hex')
44
45 while len(perm) > 0:
46 exp = int(perm.encode('hex'), 16)
47
48 sio = StringIO.StringIO()
49 count = varint.write_uvarint(sio, exp)
50 sio.seek(0)
51 act, count = varint.read_uvarint(sio)
52
53 self.assertEqual(act, exp,
54 "Decoded %r (%d) doesn't match expected (%d)" % (
55 sio.getvalue().encode('hex'), act, exp))
56 self.assertEqual(count, len(sio.getvalue()),
57 "Decoded length (%d) doesn't match expected (%d)" % (
58 count, len(sio.getvalue())))
59
60 if perm == 0:
61 break
62 perm = perm[1:]
63
64
65 if __name__ == '__main__':
66 unittest.main()
OLDNEW
« no previous file with comments | « client/libs/logdog/tests/streamname_test.py ('k') | client/libs/logdog/varint.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698