| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed by the Apache v2.0 license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import unittest |
| 6 import itertools |
| 7 import StringIO |
| 8 |
| 9 from client.libs.logdog import varint |
| 10 |
| 11 |
| 12 class VarintTestCase(unittest.TestCase): |
| 13 |
| 14 def testVarintEncodingRaw(self): |
| 15 for base, exp in ( |
| 16 (0, b'\x00'), |
| 17 (1, b'\x01'), |
| 18 (0x7F, b'\x7f'), |
| 19 (0x80, b'\x80\x01'), |
| 20 (0x81, b'\x81\x01'), |
| 21 (0x18080, b'\x80\x81\x06'), |
| 22 ): |
| 23 sio = StringIO.StringIO() |
| 24 count = varint.write_uvarint(sio, base) |
| 25 act = sio.getvalue() |
| 26 |
| 27 self.assertEqual(act, exp, |
| 28 "Encoding for %d (%r) doesn't match expected (%r)" % (base, act, exp)) |
| 29 self.assertEqual(count, len(act), |
| 30 "Length of %d (%d) doesn't match encoded length (%d)" % ( |
| 31 base, len(act), count)) |
| 32 |
| 33 def testVarintEncodeDecode(self): |
| 34 seed = (b'\x00', b'\x01', b'\x55', b'\x7F', b'\x80', b'\x81', b'\xff') |
| 35 for perm in itertools.permutations(seed): |
| 36 perm = ''.join(perm).encode('hex') |
| 37 |
| 38 while len(perm) > 0: |
| 39 exp = int(perm.encode('hex'), 16) |
| 40 |
| 41 sio = StringIO.StringIO() |
| 42 count = varint.write_uvarint(sio, exp) |
| 43 sio.seek(0) |
| 44 act, count = varint.read_uvarint(sio) |
| 45 |
| 46 self.assertEqual(act, exp, |
| 47 "Decoded %r (%d) doesn't match expected (%d)" % ( |
| 48 sio.getvalue().encode('hex'), act, exp)) |
| 49 self.assertEqual(count, len(sio.getvalue()), |
| 50 "Decoded length (%d) doesn't match expected (%d)" % ( |
| 51 count, len(sio.getvalue()))) |
| 52 |
| 53 if perm == 0: |
| 54 break |
| 55 perm = perm[1:] |
| 56 |
| 57 |
| 58 if __name__ == '__main__': |
| 59 unittest.main() |
| OLD | NEW |