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 
39 // test a slice of the number space where the slice is described by
40 // a start number and a stride through the space.
test_vlq_uint64(uint64_t start,uint64_t stride)41 static void test_vlq_uint64(uint64_t start, uint64_t stride) {
42     printf("%u\n", 0);
43     for (uint64_t v = 0 + start; v < (1<<7); v += stride) {
44         unsigned char b[10];
45         size_t out_s = tokudb::vlq_encode_ui<uint64_t>(v, b, sizeof b);
46         assert(out_s == 1);
47         uint64_t n;
48         size_t in_s = tokudb::vlq_decode_ui<uint64_t>(&n, b, out_s);
49         assert(in_s == 1);
50         assert(n == v);
51     }
52 
53     printf("%u\n", 1<<7);
54     for (uint64_t v = (1<<7) + start; v < (1<<14); v += stride) {
55         unsigned char b[10];
56         size_t out_s = tokudb::vlq_encode_ui<uint64_t>(v, b, sizeof b);
57         assert(out_s == 2);
58         uint64_t n;
59         size_t in_s = tokudb::vlq_decode_ui<uint64_t>(&n, b, out_s);
60         assert(in_s == 2);
61         assert(n == v);
62     }
63 
64     printf("%u\n", 1<<14);
65     for (uint64_t v = (1<<14) + start; v < (1<<21); v += stride) {
66         unsigned char b[10];
67         size_t out_s = tokudb::vlq_encode_ui<uint64_t>(v, b, sizeof b);
68         assert(out_s == 3);
69         uint64_t n;
70         size_t in_s = tokudb::vlq_decode_ui<uint64_t>(&n, b, out_s);
71         assert(in_s == 3);
72         assert(n == v);
73     }
74 
75     printf("%u\n", 1<<21);
76     for (uint64_t v = (1<<21) + start; v < (1<<28); v += stride) {
77         unsigned char b[10];
78         size_t out_s = tokudb::vlq_encode_ui<uint64_t>(v, b, sizeof b);
79         assert(out_s == 4);
80         uint64_t n;
81         size_t in_s = tokudb::vlq_decode_ui<uint64_t>(&n, b, out_s);
82         assert(in_s == 4);
83         assert(n == v);
84     }
85 
86     printf("%u\n", 1<<28);
87 #if USE_OPENMP
88 #pragma omp parallel num_threads(4)
89 #pragma omp for
90 #endif
91     for (uint64_t v = (1<<28) + start; v < (1ULL<<35); v += stride) {
92         unsigned char b[10];
93         size_t out_s = tokudb::vlq_encode_ui<uint64_t>(v, b, sizeof b);
94         assert(out_s == 5);
95         uint64_t n;
96         size_t in_s = tokudb::vlq_decode_ui<uint64_t>(&n, b, out_s);
97         assert(in_s == 5);
98         assert(n == v);
99     }
100 }
101 
main(int argc,char * argv[])102 int main(int argc, char *argv[]) {
103     uint64_t start = 0, stride = 1;
104     if (argc == 3) {
105         start = atoll(argv[1]);
106         stride = atoll(argv[2]);
107     }
108     test_vlq_uint64(start, stride);
109     return 0;
110 }
111