1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of TokuDB
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     TokuDBis is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     TokuDB is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with TokuDB.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ======= */
23 
24 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <assert.h>
30 #include <tokudb_vlq.h>
31 
32 namespace tokudb {
33     template size_t vlq_encode_ui(uint32_t n, void *p, size_t s);
34     template size_t vlq_decode_ui(uint32_t *np, void *p, size_t s);
35     template size_t vlq_encode_ui(uint64_t n, void *p, size_t s);
36     template size_t vlq_decode_ui(uint64_t *np, void *p, size_t s);
37 };
38 
test_vlq_uint32_error(void)39 static void test_vlq_uint32_error(void) {
40     uint32_t n;
41     unsigned char b[5];
42     size_t out_s, in_s;
43 
44     out_s = tokudb::vlq_encode_ui<uint32_t>(128, b, 0);
45     assert(out_s == 0);
46     out_s = tokudb::vlq_encode_ui<uint32_t>(128, b, 1);
47     assert(out_s == 0);
48     out_s = tokudb::vlq_encode_ui<uint32_t>(128, b, 2);
49     assert(out_s == 2);
50     in_s = tokudb::vlq_decode_ui<uint32_t>(&n, b, 0);
51     assert(in_s == 0);
52     in_s = tokudb::vlq_decode_ui<uint32_t>(&n, b, 1);
53     assert(in_s == 0);
54     in_s = tokudb::vlq_decode_ui<uint32_t>(&n, b, 2);
55     assert(in_s == 2);
56     assert(n == 128);
57 }
58 
test_80000000(void)59 static void test_80000000(void) {
60     uint64_t n;
61     unsigned char b[10];
62     size_t out_s, in_s;
63     uint64_t v = 0x80000000;
64     out_s = tokudb::vlq_encode_ui<uint64_t>(v, b, sizeof b);
65     assert(out_s == 5);
66     in_s = tokudb::vlq_decode_ui<uint64_t>(&n, b, out_s);
67     assert(in_s == 5);
68     assert(n == v);
69 }
70 
test_100000000(void)71 static void test_100000000(void) {
72     uint64_t n;
73     unsigned char b[10];
74     size_t out_s, in_s;
75     uint64_t v = 0x100000000;
76     out_s = tokudb::vlq_encode_ui<uint64_t>(v, b, sizeof b);
77     assert(out_s == 5);
78     in_s = tokudb::vlq_decode_ui<uint64_t>(&n, b, out_s);
79     assert(in_s == 5);
80     assert(n == v);
81 }
82 
main(void)83 int main(void) {
84     test_vlq_uint32_error();
85     test_80000000();
86     test_100000000();
87     return 0;
88 }
89