3
3
#include < gtest/gtest.h>
4
4
#include < random>
5
5
6
- #define DEF_TEST_FUNC1 (bytes_ ) \
7
- kvarint_buf64_t buf64; \
8
- uint64_t in = 1 ; \
9
- in <<= (7 * ((bytes_)-1 )); \
10
- in += 123 ; \
11
- kvarint_encode64 (in, &buf64); \
12
- ASSERT_EQ (buf64.len, (bytes_)); \
13
- \
14
- uint64_t out64; \
15
- size_t len = 0 ; \
16
- ASSERT_EQ (KVARINT_OK, kvarint_decode64(buf64.buf, buf64.len, &len, &out64)); \
17
- \
18
- ASSERT_EQ (in, out64)
6
+ #include " endian_api.h"
7
+
8
+ extern void kvarint_encode64be (uint64_t num, kvarint_buf64_t *buf);
9
+ extern kvarint_errcode_en kvarint_decode64be (
10
+ void const *buf,
11
+ size_t len,
12
+ size_t *out_len,
13
+ uint64_t *out
14
+ );
15
+
16
+ void print_bits (uint64_t n)
17
+ {
18
+ for (size_t i = 0 ; i < sizeof (n) * 8 ; ++i) {
19
+ std::cout << ((n & (1 << (sizeof (n) * 8 - 1 - i))) > 0 ? 1 : 0 );
20
+ }
21
+ std::cout << ' \n ' ;
22
+ }
23
+
24
+ #define DEF_TEST_FUNC1 (bytes_ ) \
25
+ kvarint_buf64_t buf64; \
26
+ uint64_t in = 1 ; \
27
+ \
28
+ in <<= (7 * ((bytes_)-1 )); \
29
+ in += 123 ; \
30
+ kvarint_encode64 (in, &buf64); \
31
+ ASSERT_EQ (buf64.len, (bytes_)); \
32
+ \
33
+ uint64_t out64; \
34
+ size_t len = 0 ; \
35
+ ASSERT_EQ (KVARINT_OK, kvarint_decode64(buf64.buf, buf64.len, &len, &out64)); \
36
+ ASSERT_EQ (in, out64); \
37
+ in = kvarint_ToNetworkByteOrder64(in); \
38
+ kvarint_encode64be (in, &buf64); \
39
+ ASSERT_EQ (buf64.len, (bytes_)); \
40
+ ASSERT_EQ (KVARINT_OK, kvarint_decode64be(buf64.buf, buf64.len, &len, &out64)); \
41
+ print_bits (in); \
42
+ print_bits (out64); \
43
+ ASSERT_EQ (in, out64) << "decode be error"; \
44
+ ASSERT_EQ (len, buf64.len) << "decode be len error";
19
45
20
46
TEST (kvarint, encode_and_decode1)
21
47
{
22
- size_t len = 0 ;
48
+ size_t len = 0 ;
23
49
kvarint_buf32_t buf32;
24
50
kvarint_encode32 (123 , &buf32);
25
51
ASSERT_EQ (buf32.len , 1 );
@@ -42,12 +68,12 @@ TEST(kvarint, encode_and_decode10) { DEF_TEST_FUNC1(10); }
42
68
43
69
TEST (kvarint, encode_and_decode_large_data)
44
70
{
45
- kvarint_buf64_t buf;
46
- uint64_t out;
47
- uint64_t N = 1000000 ;
71
+ kvarint_buf64_t buf;
72
+ uint64_t out;
73
+ uint64_t N = 1000000 ;
48
74
std::uniform_int_distribution<uint64_t > uid (0 , (uint64_t )-1 );
49
- std::default_random_engine dre (time (NULL ));
50
- size_t len = 0 ;
75
+ std::default_random_engine dre (time (NULL ));
76
+ size_t len = 0 ;
51
77
for (uint64_t i = 0 ; i < N; ++i) {
52
78
auto r = uid (dre);
53
79
kvarint_encode64 (r, &buf);
@@ -58,16 +84,77 @@ TEST(kvarint, encode_and_decode_large_data)
58
84
59
85
TEST (kvarint, encode_and_decode_large_data_sign)
60
86
{
61
- kvarint_buf64_t buf;
62
- int64_t out;
63
- int64_t N = 1000000 ;
87
+ kvarint_buf64_t buf;
88
+ int64_t out;
89
+ int64_t N = 1000000 ;
64
90
std::uniform_int_distribution<int64_t > uid (-INT64_MAX, INT64_MAX);
65
- std::default_random_engine dre (time (NULL ));
66
- size_t len;
67
- for (int64_t i = -N ; i < 0 ; ++i) {
91
+ std::default_random_engine dre (time (NULL ));
92
+ size_t len;
93
+ for (int64_t i = 0 ; i < N ; ++i) {
68
94
auto r = uid (dre);
69
95
kvarint_encode64s (r, &buf);
70
96
kvarint_decode64s (buf.buf , buf.len , &len, &out);
71
97
ASSERT_EQ (r, out) << " i = " << i;
72
98
}
73
99
}
100
+
101
+ TEST (kvarint, signed_example)
102
+ {
103
+ kvarint_buf64_t buf;
104
+ int64_t out;
105
+ size_t len;
106
+ kvarint_encode64s (1 , &buf);
107
+ kvarint_decode64s (buf.buf , buf.len , &len, &out);
108
+
109
+ ASSERT_EQ (1 , out) << out;
110
+ }
111
+
112
+ void kvarint_encode64be (uint64_t num, kvarint_buf64_t *buf)
113
+ {
114
+ // int idx = sizeof(buf->buf) - 1;
115
+ int idx = 0 ;
116
+ printf (" Encode num = %lu\n " , num);
117
+ printf (" Encode num(le) = %lu\n " , kvarint_ToHostByteOrder64 (num));
118
+
119
+ while (kvarint_ToHostByteOrder64 (num) > 127 ) {
120
+ buf->buf [idx++] = uint8_t (0x80 ) | ((uint8_t )(num >> 56 ) & uint8_t (0x7f ));
121
+ printf (" Encode buf[%d] = 0x%x\n " , idx - 1 , (uint8_t )buf->buf [idx - 1 ]);
122
+ printf (
123
+ " Correct Encode = 0x%x\n " ,
124
+ uint8_t (0x80 ) | ((uint8_t )(kvarint_ToHostByteOrder64 (num)) & uint8_t (0x7f ))
125
+ );
126
+ num = kvarint_ToNetworkByteOrder64 (kvarint_ToHostByteOrder64 (num) >> 7 );
127
+ printf (" num = %lu\n " , num);
128
+ printf (" num(le) = %lu\n " , kvarint_ToHostByteOrder64 (num));
129
+ }
130
+ buf->buf [idx] = uint8_t (num >> 56 );
131
+ printf (" last encode = 0x%x\n " , buf->buf [idx]);
132
+ printf (" Correct last encode(le) = %lu\n " , kvarint_ToHostByteOrder64 (num));
133
+ /* buf->buf[sizeof(buf->buf) - 1] &= uint8_t(0x7f); */
134
+ /* memmove(buf->buf, &buf->buf[idx], buf->len); */
135
+ /* buf->len = sizeof(buf->buf) - idx; */
136
+ buf->len = idx + 1 ;
137
+ }
138
+
139
+ kvarint_errcode_en kvarint_decode64be (void const *buf, size_t len, size_t *out_len, uint64_t *out)
140
+ {
141
+ assert (out);
142
+ assert (out_len);
143
+
144
+ int idx = 0 ;
145
+ uint8_t const *buf8 = (uint8_t const *)buf;
146
+ int shift = 0 ;
147
+ *out = 0 ;
148
+ while (idx < len) {
149
+ *out |= ((uint64_t )(buf8[idx] & uint8_t (0x7f ))) << shift;
150
+ shift += 7 ;
151
+ if (!(buf8[idx] & uint8_t (0x80 ))) {
152
+ *out_len = idx + 1 ;
153
+ *out = kvarint_ToNetworkByteOrder64 (*out);
154
+ return KVARINT_OK;
155
+ }
156
+ if (shift > 63 ) return KVARINT_DECODE_BUF_INVALID;
157
+ ++idx;
158
+ }
159
+ return KVARINT_DECODE_BUF_SHORT;
160
+ }
0 commit comments