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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: client/libs/logdog/tests/varint_test.py
diff --git a/client/libs/logdog/tests/varint_test.py b/client/libs/logdog/tests/varint_test.py
new file mode 100755
index 0000000000000000000000000000000000000000..0e80dddc48e31d60b17405277fdd02a8fea39c5d
--- /dev/null
+++ b/client/libs/logdog/tests/varint_test.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+# Copyright 2016 The LUCI Authors. All rights reserved.
+# Use of this source code is governed by the Apache v2.0 license that can be
+# found in the LICENSE file.
+
+import itertools
+import os
+import sys
+import unittest
+import StringIO
+
+ROOT_DIR = os.path.dirname(os.path.abspath(os.path.join(
+ __file__, os.pardir, os.pardir, os.pardir)))
+sys.path.insert(0, ROOT_DIR)
+
+from libs.logdog import varint
+
+
+class VarintTestCase(unittest.TestCase):
+
+ def testVarintEncodingRaw(self):
+ for base, exp in (
+ (0, b'\x00'),
+ (1, b'\x01'),
+ (0x7F, b'\x7f'),
+ (0x80, b'\x80\x01'),
+ (0x81, b'\x81\x01'),
+ (0x18080, b'\x80\x81\x06'),
+ ):
+ sio = StringIO.StringIO()
+ count = varint.write_uvarint(sio, base)
+ act = sio.getvalue()
+
+ self.assertEqual(act, exp,
+ "Encoding for %d (%r) doesn't match expected (%r)" % (base, act, exp))
+ self.assertEqual(count, len(act),
+ "Length of %d (%d) doesn't match encoded length (%d)" % (
+ base, len(act), count))
+
+ def testVarintEncodeDecode(self):
+ seed = (b'\x00', b'\x01', b'\x55', b'\x7F', b'\x80', b'\x81', b'\xff')
+ for perm in itertools.permutations(seed):
+ perm = ''.join(perm).encode('hex')
+
+ while len(perm) > 0:
+ exp = int(perm.encode('hex'), 16)
+
+ sio = StringIO.StringIO()
+ count = varint.write_uvarint(sio, exp)
+ sio.seek(0)
+ act, count = varint.read_uvarint(sio)
+
+ self.assertEqual(act, exp,
+ "Decoded %r (%d) doesn't match expected (%d)" % (
+ sio.getvalue().encode('hex'), act, exp))
+ self.assertEqual(count, len(sio.getvalue()),
+ "Decoded length (%d) doesn't match expected (%d)" % (
+ count, len(sio.getvalue())))
+
+ if perm == 0:
+ break
+ perm = perm[1:]
+
+
+if __name__ == '__main__':
+ unittest.main()
« 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