summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDavid Turner <dturner@twopensource.com>2016-05-17 15:40:46 -0400
committerDavid Turner <dturner@twosigma.com>2016-08-10 14:19:06 -0400
commitaeb5ee5ab50a062aac02ca084b02582430669808 (patch)
tree55b875ce84341eceb06724f070eacf47dfafc447 /tests
parent26a8617d96ba96875304d56a7ce698a85e559c43 (diff)
downloadlibgit2-aeb5ee5ab50a062aac02ca084b02582430669808.tar.gz
varint: Add varint encoding/decoding
This code is ported from git.git Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: David Turner <dturner@twopensource.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/core/encoding.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/core/encoding.c b/tests/core/encoding.c
new file mode 100644
index 000000000..7d91720f4
--- /dev/null
+++ b/tests/core/encoding.c
@@ -0,0 +1,39 @@
+#include "clar_libgit2.h"
+#include "varint.h"
+
+void test_core_encoding__decode(void)
+{
+ const unsigned char *buf = (unsigned char *)"AB";
+ size_t size;
+
+ cl_assert(git_decode_varint(buf, &size) == 65);
+ cl_assert(size == 1);
+
+ buf = (unsigned char *)"\xfe\xdc\xbaXY";
+ cl_assert(git_decode_varint(buf, &size) == 267869656);
+ cl_assert(size == 4);
+
+ buf = (unsigned char *)"\xaa\xaa\xfe\xdc\xbaXY";
+ cl_assert(git_decode_varint(buf, &size) == 1489279344088ULL);
+ cl_assert(size == 6);
+
+ buf = (unsigned char *)"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xfe\xdc\xbaXY";
+ cl_assert(git_decode_varint(buf, &size) == 0);
+ cl_assert(size == 0);
+
+}
+
+void test_core_encoding__encode(void)
+{
+ unsigned char buf[100];
+ cl_assert(git_encode_varint(buf, 100, 65) == 1);
+ cl_assert(buf[0] == 'A');
+
+ cl_assert(git_encode_varint(buf, 100, 267869656) == 4);
+ cl_assert(!memcmp(buf, "\xfe\xdc\xbaX", 4));
+
+ cl_assert(git_encode_varint(buf, 100, 1489279344088ULL) == 6);
+ cl_assert(!memcmp(buf, "\xaa\xaa\xfe\xdc\xbaX", 6));
+
+ cl_assert(git_encode_varint(buf, 1, 1489279344088ULL) == -1);
+}