blob: 4e2efe233a70854974a72aff90783fa33bba343e [file] [log] [blame]
Alexei Frolov82d3cb32019-11-27 14:38:39 -08001// Copyright 2019 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
Wyatt Hepler8c493bb2019-12-02 14:49:21 -08004// use this file except in compliance with the License. You may obtain a copy of
5// the License at
Alexei Frolov82d3cb32019-11-27 14:38:39 -08006//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
Wyatt Hepler8c493bb2019-12-02 14:49:21 -080012// License for the specific language governing permissions and limitations under
13// the License.
Alexei Frolov82d3cb32019-11-27 14:38:39 -080014
15#include "pw_varint/varint.h"
16
17#include <cinttypes>
18#include <cstdint>
19#include <cstring>
20#include <limits>
21
22#include "gtest/gtest.h"
23
24namespace pw::varint {
25namespace {
26
Alexei Frolov8ecefe92020-01-13 10:40:08 -080027extern "C" {
28
29// Functions defined in varint_test.c which call the varint API from C.
30size_t pw_VarintCallEncode(uint64_t integer, void* output, size_t output_size);
31size_t pw_VarintCallZigZagEncode(int64_t integer,
32 void* output,
33 size_t output_size);
34size_t pw_VarintCallDecode(void* input, size_t input_size, uint64_t* output);
35size_t pw_VarintCallZigZagDecode(void* input,
36 size_t input_size,
37 int64_t* output);
38
39} // extern "C"
40
Alexei Frolov82d3cb32019-11-27 14:38:39 -080041class Varint : public ::testing::Test {
42 protected:
Alexei Frolov311c6202020-01-06 11:16:20 -080043 Varint()
44 : buffer_{std::byte{'a'},
45 std::byte{'b'},
46 std::byte{'c'},
47 std::byte{'d'},
48 std::byte{'e'},
49 std::byte{'f'},
50 std::byte{'g'},
51 std::byte{'h'},
52 std::byte{'i'},
53 std::byte{'j'}} {}
54 std::byte buffer_[10];
Alexei Frolov82d3cb32019-11-27 14:38:39 -080055};
56
57TEST_F(Varint, EncodeSizeUnsigned32_SmallSingleByte) {
Alexei Frolov65ffd222020-01-06 14:23:02 -080058 ASSERT_EQ(1u, Encode(UINT32_C(0), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -080059 EXPECT_EQ(std::byte{0}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -080060 ASSERT_EQ(1u, Encode(UINT32_C(1), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -080061 EXPECT_EQ(std::byte{1}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -080062 ASSERT_EQ(1u, Encode(UINT32_C(2), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -080063 EXPECT_EQ(std::byte{2}, buffer_[0]);
Alexei Frolov82d3cb32019-11-27 14:38:39 -080064}
65
Alexei Frolov8ecefe92020-01-13 10:40:08 -080066TEST_F(Varint, EncodeSizeUnsigned32_SmallSingleByte_C) {
67 ASSERT_EQ(1u, pw_VarintCallEncode(UINT32_C(0), buffer_, sizeof(buffer_)));
68 EXPECT_EQ(std::byte{0}, buffer_[0]);
69 ASSERT_EQ(1u, pw_VarintCallEncode(UINT32_C(1), buffer_, sizeof(buffer_)));
70 EXPECT_EQ(std::byte{1}, buffer_[0]);
71 ASSERT_EQ(1u, pw_VarintCallEncode(UINT32_C(2), buffer_, sizeof(buffer_)));
72 EXPECT_EQ(std::byte{2}, buffer_[0]);
73}
74
Alexei Frolov82d3cb32019-11-27 14:38:39 -080075TEST_F(Varint, EncodeSizeUnsigned32_LargeSingleByte) {
Alexei Frolov65ffd222020-01-06 14:23:02 -080076 ASSERT_EQ(1u, Encode(UINT32_C(63), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -080077 EXPECT_EQ(std::byte{63}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -080078 ASSERT_EQ(1u, Encode(UINT32_C(64), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -080079 EXPECT_EQ(std::byte{64}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -080080 ASSERT_EQ(1u, Encode(UINT32_C(126), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -080081 EXPECT_EQ(std::byte{126}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -080082 ASSERT_EQ(1u, Encode(UINT32_C(127), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -080083 EXPECT_EQ(std::byte{127}, buffer_[0]);
Alexei Frolov82d3cb32019-11-27 14:38:39 -080084}
85
Alexei Frolov8ecefe92020-01-13 10:40:08 -080086TEST_F(Varint, EncodeSizeUnsigned32_LargeSingleByte_C) {
87 ASSERT_EQ(1u, pw_VarintCallEncode(UINT32_C(63), buffer_, sizeof(buffer_)));
88 EXPECT_EQ(std::byte{63}, buffer_[0]);
89 ASSERT_EQ(1u, pw_VarintCallEncode(UINT32_C(64), buffer_, sizeof(buffer_)));
90 EXPECT_EQ(std::byte{64}, buffer_[0]);
91 ASSERT_EQ(1u, pw_VarintCallEncode(UINT32_C(126), buffer_, sizeof(buffer_)));
92 EXPECT_EQ(std::byte{126}, buffer_[0]);
93 ASSERT_EQ(1u, pw_VarintCallEncode(UINT32_C(127), buffer_, sizeof(buffer_)));
94 EXPECT_EQ(std::byte{127}, buffer_[0]);
95}
96
Alexei Frolov82d3cb32019-11-27 14:38:39 -080097TEST_F(Varint, EncodeSizeUnsigned32_MultiByte) {
Alexei Frolov65ffd222020-01-06 14:23:02 -080098 ASSERT_EQ(2u, Encode(UINT32_C(128), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -080099 EXPECT_EQ(std::memcmp("\x80\x01", buffer_, 2), 0);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800100 ASSERT_EQ(2u, Encode(UINT32_C(129), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800101 EXPECT_EQ(std::memcmp("\x81\x01", buffer_, 2), 0);
102
Alexei Frolov65ffd222020-01-06 14:23:02 -0800103 ASSERT_EQ(5u, Encode(std::numeric_limits<uint32_t>::max() - 1, buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800104 EXPECT_EQ(std::memcmp("\xfe\xff\xff\xff\x0f", buffer_, 5), 0);
105
Alexei Frolov65ffd222020-01-06 14:23:02 -0800106 ASSERT_EQ(5u, Encode(std::numeric_limits<uint32_t>::max(), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800107 EXPECT_EQ(std::memcmp("\xff\xff\xff\xff\x0f", buffer_, 5), 0);
108}
109
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800110TEST_F(Varint, EncodeSizeUnsigned32_MultiByte_C) {
111 ASSERT_EQ(2u, pw_VarintCallEncode(UINT32_C(128), buffer_, sizeof(buffer_)));
112 EXPECT_EQ(std::memcmp("\x80\x01", buffer_, 2), 0);
113 ASSERT_EQ(2u, pw_VarintCallEncode(UINT32_C(129), buffer_, sizeof(buffer_)));
114 EXPECT_EQ(std::memcmp("\x81\x01", buffer_, 2), 0);
115
116 ASSERT_EQ(
117 5u,
118 pw_VarintCallEncode(
119 std::numeric_limits<uint32_t>::max() - 1, buffer_, sizeof(buffer_)));
120 EXPECT_EQ(std::memcmp("\xfe\xff\xff\xff\x0f", buffer_, 5), 0);
121
122 ASSERT_EQ(
123 5u,
124 pw_VarintCallEncode(
125 std::numeric_limits<uint32_t>::max(), buffer_, sizeof(buffer_)));
126 EXPECT_EQ(std::memcmp("\xff\xff\xff\xff\x0f", buffer_, 5), 0);
127}
128
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800129TEST_F(Varint, EncodeSizeSigned32_SmallSingleByte) {
Alexei Frolov65ffd222020-01-06 14:23:02 -0800130 ASSERT_EQ(1u, Encode(INT32_C(0), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800131 EXPECT_EQ(std::byte{0}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800132 ASSERT_EQ(1u, Encode(INT32_C(-1), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800133 EXPECT_EQ(std::byte{1}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800134 ASSERT_EQ(1u, Encode(INT32_C(1), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800135 EXPECT_EQ(std::byte{2}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800136 ASSERT_EQ(1u, Encode(INT32_C(-2), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800137 EXPECT_EQ(std::byte{3}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800138 ASSERT_EQ(1u, Encode(INT32_C(2), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800139 EXPECT_EQ(std::byte{4}, buffer_[0]);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800140}
141
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800142TEST_F(Varint, EncodeSizeSigned32_SmallSingleByte_C) {
143 ASSERT_EQ(1u,
144 pw_VarintCallZigZagEncode(INT32_C(0), buffer_, sizeof(buffer_)));
145 EXPECT_EQ(std::byte{0}, buffer_[0]);
146 ASSERT_EQ(1u,
147 pw_VarintCallZigZagEncode(INT32_C(-1), buffer_, sizeof(buffer_)));
148 EXPECT_EQ(std::byte{1}, buffer_[0]);
149 ASSERT_EQ(1u,
150 pw_VarintCallZigZagEncode(INT32_C(1), buffer_, sizeof(buffer_)));
151 EXPECT_EQ(std::byte{2}, buffer_[0]);
152 ASSERT_EQ(1u,
153 pw_VarintCallZigZagEncode(INT32_C(-2), buffer_, sizeof(buffer_)));
154 EXPECT_EQ(std::byte{3}, buffer_[0]);
155 ASSERT_EQ(1u,
156 pw_VarintCallZigZagEncode(INT32_C(2), buffer_, sizeof(buffer_)));
157 EXPECT_EQ(std::byte{4}, buffer_[0]);
158}
159
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800160TEST_F(Varint, EncodeSizeSigned32_LargeSingleByte) {
Alexei Frolov65ffd222020-01-06 14:23:02 -0800161 ASSERT_EQ(1u, Encode(INT32_C(-63), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800162 EXPECT_EQ(std::byte{125}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800163 ASSERT_EQ(1u, Encode(INT32_C(63), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800164 EXPECT_EQ(std::byte{126}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800165 ASSERT_EQ(1u, Encode(INT32_C(-64), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800166 EXPECT_EQ(std::byte{127}, buffer_[0]);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800167}
168
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800169TEST_F(Varint, EncodeSizeSigned32_LargeSingleByte_C) {
170 ASSERT_EQ(1u,
171 pw_VarintCallZigZagEncode(INT32_C(-63), buffer_, sizeof(buffer_)));
172 EXPECT_EQ(std::byte{125}, buffer_[0]);
173 ASSERT_EQ(1u,
174 pw_VarintCallZigZagEncode(INT32_C(63), buffer_, sizeof(buffer_)));
175 EXPECT_EQ(std::byte{126}, buffer_[0]);
176 ASSERT_EQ(1u,
177 pw_VarintCallZigZagEncode(INT32_C(-64), buffer_, sizeof(buffer_)));
178 EXPECT_EQ(std::byte{127}, buffer_[0]);
179}
180
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800181TEST_F(Varint, EncodeSizeSigned32_MultiByte) {
Alexei Frolov65ffd222020-01-06 14:23:02 -0800182 ASSERT_EQ(2u, Encode(INT32_C(64), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800183 EXPECT_EQ(std::memcmp("\x80\x01", buffer_, 2), 0);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800184 ASSERT_EQ(2u, Encode(INT32_C(-65), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800185 EXPECT_EQ(std::memcmp("\x81\x01", buffer_, 2), 0);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800186 ASSERT_EQ(2u, Encode(INT32_C(65), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800187 EXPECT_EQ(std::memcmp("\x82\x01", buffer_, 2), 0);
188
Alexei Frolov65ffd222020-01-06 14:23:02 -0800189 ASSERT_EQ(5u, Encode(std::numeric_limits<int32_t>::min(), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800190 EXPECT_EQ(std::memcmp("\xff\xff\xff\xff\x0f", buffer_, 5), 0);
191
Alexei Frolov65ffd222020-01-06 14:23:02 -0800192 ASSERT_EQ(5u, Encode(std::numeric_limits<int32_t>::max(), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800193 EXPECT_EQ(std::memcmp("\xfe\xff\xff\xff\x0f", buffer_, 5), 0);
194}
195
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800196TEST_F(Varint, EncodeSizeSigned32_MultiByte_C) {
197 ASSERT_EQ(2u,
198 pw_VarintCallZigZagEncode(INT32_C(64), buffer_, sizeof(buffer_)));
199 EXPECT_EQ(std::memcmp("\x80\x01", buffer_, 2), 0);
200 ASSERT_EQ(2u,
201 pw_VarintCallZigZagEncode(INT32_C(-65), buffer_, sizeof(buffer_)));
202 EXPECT_EQ(std::memcmp("\x81\x01", buffer_, 2), 0);
203 ASSERT_EQ(2u,
204 pw_VarintCallZigZagEncode(INT32_C(65), buffer_, sizeof(buffer_)));
205 EXPECT_EQ(std::memcmp("\x82\x01", buffer_, 2), 0);
206
207 ASSERT_EQ(5u,
208 pw_VarintCallZigZagEncode(
209 std::numeric_limits<int32_t>::min(), buffer_, sizeof(buffer_)));
210 EXPECT_EQ(std::memcmp("\xff\xff\xff\xff\x0f", buffer_, 5), 0);
211
212 ASSERT_EQ(5u,
213 pw_VarintCallZigZagEncode(
214 std::numeric_limits<int32_t>::max(), buffer_, sizeof(buffer_)));
215 EXPECT_EQ(std::memcmp("\xfe\xff\xff\xff\x0f", buffer_, 5), 0);
216}
217
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800218TEST_F(Varint, EncodeSizeUnsigned64_SmallSingleByte) {
Alexei Frolov65ffd222020-01-06 14:23:02 -0800219 ASSERT_EQ(1u, Encode(UINT64_C(0), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800220 EXPECT_EQ(std::byte{0}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800221 ASSERT_EQ(1u, Encode(UINT64_C(1), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800222 EXPECT_EQ(std::byte{1}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800223 ASSERT_EQ(1u, Encode(UINT64_C(2), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800224 EXPECT_EQ(std::byte{2}, buffer_[0]);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800225}
226
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800227TEST_F(Varint, EncodeSizeUnsigned64_SmallSingleByte_C) {
228 ASSERT_EQ(1u, pw_VarintCallEncode(UINT64_C(0), buffer_, sizeof(buffer_)));
229 EXPECT_EQ(std::byte{0}, buffer_[0]);
230 ASSERT_EQ(1u, pw_VarintCallEncode(UINT64_C(1), buffer_, sizeof(buffer_)));
231 EXPECT_EQ(std::byte{1}, buffer_[0]);
232 ASSERT_EQ(1u, pw_VarintCallEncode(UINT64_C(2), buffer_, sizeof(buffer_)));
233 EXPECT_EQ(std::byte{2}, buffer_[0]);
234}
235
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800236TEST_F(Varint, EncodeSizeUnsigned64_LargeSingleByte) {
Alexei Frolov65ffd222020-01-06 14:23:02 -0800237 ASSERT_EQ(1u, Encode(UINT64_C(63), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800238 EXPECT_EQ(std::byte{63}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800239 ASSERT_EQ(1u, Encode(UINT64_C(64), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800240 EXPECT_EQ(std::byte{64}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800241 ASSERT_EQ(1u, Encode(UINT64_C(126), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800242 EXPECT_EQ(std::byte{126}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800243 ASSERT_EQ(1u, Encode(UINT64_C(127), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800244 EXPECT_EQ(std::byte{127}, buffer_[0]);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800245}
246
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800247TEST_F(Varint, EncodeSizeUnsigned64_LargeSingleByte_C) {
248 ASSERT_EQ(1u, pw_VarintCallEncode(UINT64_C(63), buffer_, sizeof(buffer_)));
249 EXPECT_EQ(std::byte{63}, buffer_[0]);
250 ASSERT_EQ(1u, pw_VarintCallEncode(UINT64_C(64), buffer_, sizeof(buffer_)));
251 EXPECT_EQ(std::byte{64}, buffer_[0]);
252 ASSERT_EQ(1u, pw_VarintCallEncode(UINT64_C(126), buffer_, sizeof(buffer_)));
253 EXPECT_EQ(std::byte{126}, buffer_[0]);
254 ASSERT_EQ(1u, pw_VarintCallEncode(UINT64_C(127), buffer_, sizeof(buffer_)));
255 EXPECT_EQ(std::byte{127}, buffer_[0]);
256}
257
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800258TEST_F(Varint, EncodeSizeUnsigned64_MultiByte) {
Alexei Frolov65ffd222020-01-06 14:23:02 -0800259 ASSERT_EQ(2u, Encode(UINT64_C(128), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800260 EXPECT_EQ(std::memcmp("\x80\x01", buffer_, 2), 0);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800261 ASSERT_EQ(2u, Encode(UINT64_C(129), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800262 EXPECT_EQ(std::memcmp("\x81\x01", buffer_, 2), 0);
263
Alexei Frolov65ffd222020-01-06 14:23:02 -0800264 ASSERT_EQ(5u, Encode(std::numeric_limits<uint32_t>::max() - 1, buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800265 EXPECT_EQ(std::memcmp("\xfe\xff\xff\xff\x0f", buffer_, 5), 0);
266
Alexei Frolov65ffd222020-01-06 14:23:02 -0800267 ASSERT_EQ(5u, Encode(std::numeric_limits<uint32_t>::max(), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800268 EXPECT_EQ(std::memcmp("\xff\xff\xff\xff\x0f", buffer_, 5), 0);
269
Alexei Frolov65ffd222020-01-06 14:23:02 -0800270 ASSERT_EQ(10u, Encode(std::numeric_limits<uint64_t>::max() - 1, buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800271 EXPECT_EQ(
272 std::memcmp("\xfe\xff\xff\xff\xff\xff\xff\xff\xff\x01", buffer_, 10), 0);
273
Alexei Frolov65ffd222020-01-06 14:23:02 -0800274 ASSERT_EQ(10u, Encode(std::numeric_limits<uint64_t>::max(), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800275 EXPECT_EQ(
276 std::memcmp("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01", buffer_, 10), 0);
277}
278
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800279TEST_F(Varint, EncodeSizeUnsigned64_MultiByte_C) {
280 ASSERT_EQ(2u, pw_VarintCallEncode(UINT64_C(128), buffer_, sizeof(buffer_)));
281 EXPECT_EQ(std::memcmp("\x80\x01", buffer_, 2), 0);
282 ASSERT_EQ(2u, pw_VarintCallEncode(UINT64_C(129), buffer_, sizeof(buffer_)));
283 EXPECT_EQ(std::memcmp("\x81\x01", buffer_, 2), 0);
284
285 ASSERT_EQ(
286 5u,
287 pw_VarintCallEncode(
288 std::numeric_limits<uint32_t>::max() - 1, buffer_, sizeof(buffer_)));
289 EXPECT_EQ(std::memcmp("\xfe\xff\xff\xff\x0f", buffer_, 5), 0);
290
291 ASSERT_EQ(
292 5u,
293 pw_VarintCallEncode(
294 std::numeric_limits<uint32_t>::max(), buffer_, sizeof(buffer_)));
295 EXPECT_EQ(std::memcmp("\xff\xff\xff\xff\x0f", buffer_, 5), 0);
296
297 ASSERT_EQ(
298 10u,
299 pw_VarintCallEncode(
300 std::numeric_limits<uint64_t>::max() - 1, buffer_, sizeof(buffer_)));
301 EXPECT_EQ(
302 std::memcmp("\xfe\xff\xff\xff\xff\xff\xff\xff\xff\x01", buffer_, 10), 0);
303
304 ASSERT_EQ(
305 10u,
306 pw_VarintCallEncode(
307 std::numeric_limits<uint64_t>::max(), buffer_, sizeof(buffer_)));
308 EXPECT_EQ(
309 std::memcmp("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01", buffer_, 10), 0);
310}
311
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800312TEST_F(Varint, EncodeSizeSigned64_SmallSingleByte) {
Alexei Frolov65ffd222020-01-06 14:23:02 -0800313 ASSERT_EQ(1u, Encode(INT64_C(0), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800314 EXPECT_EQ(std::byte{0}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800315 ASSERT_EQ(1u, Encode(INT64_C(-1), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800316 EXPECT_EQ(std::byte{1}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800317 ASSERT_EQ(1u, Encode(INT64_C(1), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800318 EXPECT_EQ(std::byte{2}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800319 ASSERT_EQ(1u, Encode(INT64_C(-2), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800320 EXPECT_EQ(std::byte{3}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800321 ASSERT_EQ(1u, Encode(INT64_C(2), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800322 EXPECT_EQ(std::byte{4}, buffer_[0]);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800323}
324
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800325TEST_F(Varint, EncodeSizeSigned64_SmallSingleByte_C) {
326 ASSERT_EQ(1u,
327 pw_VarintCallZigZagEncode(INT64_C(0), buffer_, sizeof(buffer_)));
328 EXPECT_EQ(std::byte{0}, buffer_[0]);
329 ASSERT_EQ(1u,
330 pw_VarintCallZigZagEncode(INT64_C(-1), buffer_, sizeof(buffer_)));
331 EXPECT_EQ(std::byte{1}, buffer_[0]);
332 ASSERT_EQ(1u,
333 pw_VarintCallZigZagEncode(INT64_C(1), buffer_, sizeof(buffer_)));
334 EXPECT_EQ(std::byte{2}, buffer_[0]);
335 ASSERT_EQ(1u,
336 pw_VarintCallZigZagEncode(INT64_C(-2), buffer_, sizeof(buffer_)));
337 EXPECT_EQ(std::byte{3}, buffer_[0]);
338 ASSERT_EQ(1u,
339 pw_VarintCallZigZagEncode(INT64_C(2), buffer_, sizeof(buffer_)));
340 EXPECT_EQ(std::byte{4}, buffer_[0]);
341}
342
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800343TEST_F(Varint, EncodeSizeSigned64_LargeSingleByte) {
Alexei Frolov65ffd222020-01-06 14:23:02 -0800344 ASSERT_EQ(1u, Encode(INT64_C(-63), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800345 EXPECT_EQ(std::byte{125}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800346 ASSERT_EQ(1u, Encode(INT64_C(63), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800347 EXPECT_EQ(std::byte{126}, buffer_[0]);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800348 ASSERT_EQ(1u, Encode(INT64_C(-64), buffer_));
Alexei Frolov311c6202020-01-06 11:16:20 -0800349 EXPECT_EQ(std::byte{127}, buffer_[0]);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800350}
351
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800352TEST_F(Varint, EncodeSizeSigned64_LargeSingleByte_C) {
353 ASSERT_EQ(1u,
354 pw_VarintCallZigZagEncode(INT64_C(-63), buffer_, sizeof(buffer_)));
355 EXPECT_EQ(std::byte{125}, buffer_[0]);
356 ASSERT_EQ(1u,
357 pw_VarintCallZigZagEncode(INT64_C(63), buffer_, sizeof(buffer_)));
358 EXPECT_EQ(std::byte{126}, buffer_[0]);
359 ASSERT_EQ(1u,
360 pw_VarintCallZigZagEncode(INT64_C(-64), buffer_, sizeof(buffer_)));
361 EXPECT_EQ(std::byte{127}, buffer_[0]);
362}
363
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800364TEST_F(Varint, EncodeSizeSigned64_MultiByte) {
Alexei Frolov65ffd222020-01-06 14:23:02 -0800365 ASSERT_EQ(2u, Encode(INT64_C(64), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800366 EXPECT_EQ(std::memcmp("\x80\x01", buffer_, 2), 0);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800367 ASSERT_EQ(2u, Encode(INT64_C(-65), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800368 EXPECT_EQ(std::memcmp("\x81\x01", buffer_, 2), 0);
Alexei Frolov65ffd222020-01-06 14:23:02 -0800369 ASSERT_EQ(2u, Encode(INT64_C(65), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800370 EXPECT_EQ(std::memcmp("\x82\x01", buffer_, 2), 0);
371
Alexei Frolov65ffd222020-01-06 14:23:02 -0800372 ASSERT_EQ(5u,
373 Encode(static_cast<int64_t>(std::numeric_limits<int32_t>::min()),
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800374 buffer_));
375 EXPECT_EQ(std::memcmp("\xff\xff\xff\xff\x0f", buffer_, 5), 0);
376
Alexei Frolov65ffd222020-01-06 14:23:02 -0800377 ASSERT_EQ(5u,
378 Encode(static_cast<int64_t>(std::numeric_limits<int32_t>::max()),
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800379 buffer_));
380 EXPECT_EQ(std::memcmp("\xfe\xff\xff\xff\x0f", buffer_, 5), 0);
381
Alexei Frolov65ffd222020-01-06 14:23:02 -0800382 ASSERT_EQ(10u, Encode(std::numeric_limits<int64_t>::min(), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800383 EXPECT_EQ(
384 std::memcmp("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01", buffer_, 10), 0);
385
Alexei Frolov65ffd222020-01-06 14:23:02 -0800386 ASSERT_EQ(10u, Encode(std::numeric_limits<int64_t>::max(), buffer_));
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800387 EXPECT_EQ(
388 std::memcmp("\xfe\xff\xff\xff\xff\xff\xff\xff\xff\x01", buffer_, 10), 0);
389}
390
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800391TEST_F(Varint, EncodeSizeSigned64_MultiByte_C) {
392 ASSERT_EQ(2u,
393 pw_VarintCallZigZagEncode(INT64_C(64), buffer_, sizeof(buffer_)));
394 EXPECT_EQ(std::memcmp("\x80\x01", buffer_, 2), 0);
395 ASSERT_EQ(2u,
396 pw_VarintCallZigZagEncode(INT64_C(-65), buffer_, sizeof(buffer_)));
397 EXPECT_EQ(std::memcmp("\x81\x01", buffer_, 2), 0);
398 ASSERT_EQ(2u,
399 pw_VarintCallZigZagEncode(INT64_C(65), buffer_, sizeof(buffer_)));
400 EXPECT_EQ(std::memcmp("\x82\x01", buffer_, 2), 0);
401
402 ASSERT_EQ(5u,
403 pw_VarintCallZigZagEncode(
404 static_cast<int64_t>(std::numeric_limits<int32_t>::min()),
405 buffer_,
406 sizeof(buffer_)));
407 EXPECT_EQ(std::memcmp("\xff\xff\xff\xff\x0f", buffer_, 5), 0);
408
409 ASSERT_EQ(5u,
410 pw_VarintCallZigZagEncode(
411 static_cast<int64_t>(std::numeric_limits<int32_t>::max()),
412 buffer_,
413 sizeof(buffer_)));
414 EXPECT_EQ(std::memcmp("\xfe\xff\xff\xff\x0f", buffer_, 5), 0);
415
416 ASSERT_EQ(10u,
417 pw_VarintCallZigZagEncode(
418 std::numeric_limits<int64_t>::min(), buffer_, sizeof(buffer_)));
419 EXPECT_EQ(
420 std::memcmp("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01", buffer_, 10), 0);
421
422 ASSERT_EQ(10u,
423 pw_VarintCallZigZagEncode(
424 std::numeric_limits<int64_t>::max(), buffer_, sizeof(buffer_)));
425 EXPECT_EQ(
426 std::memcmp("\xfe\xff\xff\xff\xff\xff\xff\xff\xff\x01", buffer_, 10), 0);
427}
428
Wyatt Hepler4681e152020-02-13 13:19:46 -0800429// How much to increment by for each iteration of the exhaustive encode/decode
430// tests. Set the increment to 1 to test every number (this is slow).
431constexpr int kIncrement = 100'000'009;
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800432
Wyatt Hepler4681e152020-02-13 13:19:46 -0800433TEST_F(Varint, EncodeDecodeSigned32) {
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800434 int32_t i = std::numeric_limits<int32_t>::min();
435 while (true) {
Alexei Frolov65ffd222020-01-06 14:23:02 -0800436 size_t encoded = Encode(i, buffer_);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800437
438 int64_t result;
Alexei Frolov65ffd222020-01-06 14:23:02 -0800439 size_t decoded = Decode(buffer_, &result);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800440
441 EXPECT_EQ(encoded, decoded);
442 ASSERT_EQ(i, result);
443
444 if (i > std::numeric_limits<int32_t>::max() - kIncrement) {
445 break;
446 }
447
448 i += kIncrement;
449 }
450}
451
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800452TEST_F(Varint, EncodeDecodeSigned32_C) {
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800453 int32_t i = std::numeric_limits<int32_t>::min();
454 while (true) {
455 size_t encoded = pw_VarintCallZigZagEncode(i, buffer_, sizeof(buffer_));
456
457 int64_t result;
458 size_t decoded =
459 pw_VarintCallZigZagDecode(buffer_, sizeof(buffer_), &result);
460
461 EXPECT_EQ(encoded, decoded);
462 ASSERT_EQ(i, result);
463
464 if (i > std::numeric_limits<int32_t>::max() - kIncrement) {
465 break;
466 }
467
468 i += kIncrement;
469 }
470}
471
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800472TEST_F(Varint, EncodeDecodeUnsigned32) {
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800473 uint32_t i = 0;
474 while (true) {
Alexei Frolov65ffd222020-01-06 14:23:02 -0800475 size_t encoded = Encode(i, buffer_);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800476
477 uint64_t result;
Alexei Frolov65ffd222020-01-06 14:23:02 -0800478 size_t decoded = Decode(buffer_, &result);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800479
480 EXPECT_EQ(encoded, decoded);
481 ASSERT_EQ(i, result);
482
483 if (i > std::numeric_limits<uint32_t>::max() - kIncrement) {
484 break;
485 }
486
487 i += kIncrement;
488 }
489}
490
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800491TEST_F(Varint, EncodeDecodeUnsigned32_C) {
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800492 uint32_t i = 0;
493 while (true) {
494 size_t encoded = pw_VarintCallEncode(i, buffer_, sizeof(buffer_));
495
496 uint64_t result;
497 size_t decoded = pw_VarintCallDecode(buffer_, sizeof(buffer_), &result);
498
499 EXPECT_EQ(encoded, decoded);
500 ASSERT_EQ(i, result);
501
502 if (i > std::numeric_limits<uint32_t>::max() - kIncrement) {
503 break;
504 }
505
506 i += kIncrement;
507 }
508}
509
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800510template <size_t kStringSize>
511auto MakeBuffer(const char (&data)[kStringSize]) {
512 constexpr size_t kSizeBytes = kStringSize - 1;
513 static_assert(kSizeBytes <= 10, "Varint arrays never need be larger than 10");
514
Alexei Frolov311c6202020-01-06 11:16:20 -0800515 std::array<std::byte, kSizeBytes> array;
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800516 std::memcpy(array.data(), data, kSizeBytes);
517 return array;
518}
519
520TEST(VarintDecode, DecodeSigned64_SingleByte) {
521 int64_t value = -1234;
522
Alexei Frolov65ffd222020-01-06 14:23:02 -0800523 EXPECT_EQ(Decode(MakeBuffer("\x00"), &value), 1u);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800524 EXPECT_EQ(value, 0);
525
Alexei Frolov65ffd222020-01-06 14:23:02 -0800526 EXPECT_EQ(Decode(MakeBuffer("\x01"), &value), 1u);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800527 EXPECT_EQ(value, -1);
528
Alexei Frolov65ffd222020-01-06 14:23:02 -0800529 EXPECT_EQ(Decode(MakeBuffer("\x02"), &value), 1u);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800530 EXPECT_EQ(value, 1);
531
Alexei Frolov65ffd222020-01-06 14:23:02 -0800532 EXPECT_EQ(Decode(MakeBuffer("\x03"), &value), 1u);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800533 EXPECT_EQ(value, -2);
534
Alexei Frolov65ffd222020-01-06 14:23:02 -0800535 EXPECT_EQ(Decode(MakeBuffer("\x04"), &value), 1u);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800536 EXPECT_EQ(value, 2);
537
Alexei Frolov65ffd222020-01-06 14:23:02 -0800538 EXPECT_EQ(Decode(MakeBuffer("\x04"), &value), 1u);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800539 EXPECT_EQ(value, 2);
540}
541
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800542TEST(VarintDecode, DecodeSigned64_SingleByte_C) {
543 int64_t value = -1234;
544
545 auto buffer = MakeBuffer("\x00");
546 EXPECT_EQ(pw_VarintCallZigZagDecode(buffer.data(), buffer.size(), &value),
547 1u);
548 EXPECT_EQ(value, 0);
549
550 buffer = MakeBuffer("\x01");
551 EXPECT_EQ(pw_VarintCallZigZagDecode(buffer.data(), buffer.size(), &value),
552 1u);
553 EXPECT_EQ(value, -1);
554
555 buffer = MakeBuffer("\x02");
556 EXPECT_EQ(pw_VarintCallZigZagDecode(buffer.data(), buffer.size(), &value),
557 1u);
558 EXPECT_EQ(value, 1);
559
560 buffer = MakeBuffer("\x03");
561 EXPECT_EQ(pw_VarintCallZigZagDecode(buffer.data(), buffer.size(), &value),
562 1u);
563 EXPECT_EQ(value, -2);
564
565 buffer = MakeBuffer("\x04");
566 EXPECT_EQ(pw_VarintCallZigZagDecode(buffer.data(), buffer.size(), &value),
567 1u);
568 EXPECT_EQ(value, 2);
569
570 buffer = MakeBuffer("\x04");
571 EXPECT_EQ(pw_VarintCallZigZagDecode(buffer.data(), buffer.size(), &value),
572 1u);
573 EXPECT_EQ(value, 2);
574}
575
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800576TEST(VarintDecode, DecodeSigned64_MultiByte) {
577 int64_t value = -1234;
578
Alexei Frolov65ffd222020-01-06 14:23:02 -0800579 EXPECT_EQ(Decode(MakeBuffer("\x80\x01"), &value), 2u);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800580 EXPECT_EQ(value, 64);
581
Alexei Frolov65ffd222020-01-06 14:23:02 -0800582 EXPECT_EQ(Decode(MakeBuffer("\x81\x01"), &value), 2u);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800583 EXPECT_EQ(value, -65);
584
Alexei Frolov65ffd222020-01-06 14:23:02 -0800585 EXPECT_EQ(Decode(MakeBuffer("\x82\x01"), &value), 2u);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800586 EXPECT_EQ(value, 65);
587
Alexei Frolov65ffd222020-01-06 14:23:02 -0800588 EXPECT_EQ(Decode(MakeBuffer("\xff\xff\xff\xff\x0f"), &value), 5u);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800589 EXPECT_EQ(value, std::numeric_limits<int32_t>::min());
590
Alexei Frolov65ffd222020-01-06 14:23:02 -0800591 EXPECT_EQ(Decode(MakeBuffer("\xfe\xff\xff\xff\x0f"), &value), 5u);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800592 EXPECT_EQ(value, std::numeric_limits<int32_t>::max());
593
Alexei Frolov65ffd222020-01-06 14:23:02 -0800594 EXPECT_EQ(
595 Decode(MakeBuffer("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01"), &value),
596 10u);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800597 EXPECT_EQ(value, std::numeric_limits<int64_t>::min());
598
Alexei Frolov65ffd222020-01-06 14:23:02 -0800599 EXPECT_EQ(
600 Decode(MakeBuffer("\xfe\xff\xff\xff\xff\xff\xff\xff\xff\x01"), &value),
601 10u);
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800602 EXPECT_EQ(value, std::numeric_limits<int64_t>::max());
603}
604
Alexei Frolov8ecefe92020-01-13 10:40:08 -0800605TEST(VarintDecode, DecodeSigned64_MultiByte_C) {
606 int64_t value = -1234;
607
608 auto buffer2 = MakeBuffer("\x80\x01");
609 EXPECT_EQ(pw_VarintCallZigZagDecode(buffer2.data(), buffer2.size(), &value),
610 2u);
611 EXPECT_EQ(value, 64);
612
613 buffer2 = MakeBuffer("\x81\x01");
614 EXPECT_EQ(pw_VarintCallZigZagDecode(buffer2.data(), buffer2.size(), &value),
615 2u);
616 EXPECT_EQ(value, -65);
617
618 buffer2 = MakeBuffer("\x82\x01");
619 EXPECT_EQ(pw_VarintCallZigZagDecode(buffer2.data(), buffer2.size(), &value),
620 2u);
621 EXPECT_EQ(value, 65);
622
623 auto buffer4 = MakeBuffer("\xff\xff\xff\xff\x0f");
624 EXPECT_EQ(pw_VarintCallZigZagDecode(buffer4.data(), buffer4.size(), &value),
625 5u);
626 EXPECT_EQ(value, std::numeric_limits<int32_t>::min());
627
628 buffer4 = MakeBuffer("\xfe\xff\xff\xff\x0f");
629 EXPECT_EQ(pw_VarintCallZigZagDecode(buffer4.data(), buffer4.size(), &value),
630 5u);
631 EXPECT_EQ(value, std::numeric_limits<int32_t>::max());
632
633 auto buffer8 = MakeBuffer("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01");
634 EXPECT_EQ(pw_VarintCallZigZagDecode(buffer8.data(), buffer8.size(), &value),
635 10u);
636 EXPECT_EQ(value, std::numeric_limits<int64_t>::min());
637
638 buffer8 = MakeBuffer("\xfe\xff\xff\xff\xff\xff\xff\xff\xff\x01");
639 EXPECT_EQ(pw_VarintCallZigZagDecode(buffer8.data(), buffer8.size(), &value),
640 10u);
641 EXPECT_EQ(value, std::numeric_limits<int64_t>::max());
642}
643
Alexei Frolov5d63df92020-01-06 16:11:15 -0800644TEST(Varint, ZigZagEncode_Int8) {
645 EXPECT_EQ(ZigZagEncode(int8_t(0)), uint8_t(0));
646 EXPECT_EQ(ZigZagEncode(int8_t(-1)), uint8_t(1));
647 EXPECT_EQ(ZigZagEncode(int8_t(1)), uint8_t(2));
648 EXPECT_EQ(ZigZagEncode(int8_t(-2)), uint8_t(3));
649 EXPECT_EQ(ZigZagEncode(int8_t(2)), uint8_t(4));
650 EXPECT_EQ(ZigZagEncode(int8_t(-33)), uint8_t(65));
651 EXPECT_EQ(ZigZagEncode(int8_t(33)), uint8_t(66));
652 EXPECT_EQ(ZigZagEncode(std::numeric_limits<int8_t>::min()),
653 std::numeric_limits<uint8_t>::max());
654 EXPECT_EQ(ZigZagEncode(std::numeric_limits<int8_t>::max()),
655 std::numeric_limits<uint8_t>::max() - 1u);
656}
657
658TEST(Varint, ZigZagEncode_Int16) {
659 EXPECT_EQ(ZigZagEncode(int16_t(0)), uint16_t(0));
660 EXPECT_EQ(ZigZagEncode(int16_t(-1)), uint16_t(1));
661 EXPECT_EQ(ZigZagEncode(int16_t(1)), uint16_t(2));
662 EXPECT_EQ(ZigZagEncode(int16_t(-2)), uint16_t(3));
663 EXPECT_EQ(ZigZagEncode(int16_t(2)), uint16_t(4));
664 EXPECT_EQ(ZigZagEncode(int16_t(-3333)), uint16_t(6665));
665 EXPECT_EQ(ZigZagEncode(int16_t(3333)), uint16_t(6666));
666 EXPECT_EQ(ZigZagEncode(std::numeric_limits<int16_t>::min()),
667 std::numeric_limits<uint16_t>::max());
668 EXPECT_EQ(ZigZagEncode(std::numeric_limits<int16_t>::max()),
669 std::numeric_limits<uint16_t>::max() - 1u);
670}
671
672TEST(Varint, ZigZagEncode_Int32) {
673 EXPECT_EQ(ZigZagEncode(int32_t(0)), uint32_t(0));
674 EXPECT_EQ(ZigZagEncode(int32_t(-1)), uint32_t(1));
675 EXPECT_EQ(ZigZagEncode(int32_t(1)), uint32_t(2));
676 EXPECT_EQ(ZigZagEncode(int32_t(-2)), uint32_t(3));
677 EXPECT_EQ(ZigZagEncode(int32_t(2)), uint32_t(4));
678 EXPECT_EQ(ZigZagEncode(int32_t(-128)), uint32_t(255));
679 EXPECT_EQ(ZigZagEncode(int32_t(128)), uint32_t(256));
680 EXPECT_EQ(ZigZagEncode(int32_t(-333333)), uint32_t(666665));
681 EXPECT_EQ(ZigZagEncode(int32_t(333333)), uint32_t(666666));
682 EXPECT_EQ(ZigZagEncode(std::numeric_limits<int32_t>::min()),
683 std::numeric_limits<uint32_t>::max());
684 EXPECT_EQ(ZigZagEncode(std::numeric_limits<int32_t>::max()),
685 std::numeric_limits<uint32_t>::max() - 1u);
686}
687
688TEST(Varint, ZigZagEncode_Int64) {
689 EXPECT_EQ(ZigZagEncode(int64_t(0)), uint64_t(0));
690 EXPECT_EQ(ZigZagEncode(int64_t(-1)), uint64_t(1));
691 EXPECT_EQ(ZigZagEncode(int64_t(1)), uint64_t(2));
692 EXPECT_EQ(ZigZagEncode(int64_t(-2)), uint64_t(3));
693 EXPECT_EQ(ZigZagEncode(int64_t(2)), uint64_t(4));
694 EXPECT_EQ(ZigZagEncode(int64_t(-3333333333)), uint64_t(6666666665));
695 EXPECT_EQ(ZigZagEncode(int64_t(3333333333)), uint64_t(6666666666));
696 EXPECT_EQ(ZigZagEncode(std::numeric_limits<int64_t>::min()),
697 std::numeric_limits<uint64_t>::max());
698 EXPECT_EQ(ZigZagEncode(std::numeric_limits<int64_t>::max()),
699 std::numeric_limits<uint64_t>::max() - 1u);
700}
701
702TEST(Varint, ZigZagDecode_Int8) {
703 EXPECT_EQ(ZigZagDecode(uint8_t(0)), int8_t(0));
704 EXPECT_EQ(ZigZagDecode(uint8_t(1)), int8_t(-1));
705 EXPECT_EQ(ZigZagDecode(uint8_t(2)), int8_t(1));
706 EXPECT_EQ(ZigZagDecode(uint8_t(3)), int8_t(-2));
707 EXPECT_EQ(ZigZagDecode(uint8_t(4)), int8_t(2));
708 EXPECT_EQ(ZigZagDecode(uint8_t(65)), int8_t(-33));
709 EXPECT_EQ(ZigZagDecode(uint8_t(66)), int8_t(33));
710 EXPECT_EQ(ZigZagDecode(std::numeric_limits<uint8_t>::max()),
711 std::numeric_limits<int8_t>::min());
712 EXPECT_EQ(ZigZagDecode(std::numeric_limits<uint8_t>::max() - 1u),
713 std::numeric_limits<int8_t>::max());
714}
715
716TEST(Varint, ZigZagDecode_Int16) {
717 EXPECT_EQ(ZigZagDecode(uint16_t(0)), int16_t(0));
718 EXPECT_EQ(ZigZagDecode(uint16_t(1)), int16_t(-1));
719 EXPECT_EQ(ZigZagDecode(uint16_t(2)), int16_t(1));
720 EXPECT_EQ(ZigZagDecode(uint16_t(3)), int16_t(-2));
721 EXPECT_EQ(ZigZagDecode(uint16_t(4)), int16_t(2));
722 EXPECT_EQ(ZigZagDecode(uint16_t(6665)), int16_t(-3333));
723 EXPECT_EQ(ZigZagDecode(uint16_t(6666)), int16_t(3333));
724 EXPECT_EQ(ZigZagDecode(std::numeric_limits<uint16_t>::max()),
725 std::numeric_limits<int16_t>::min());
726 EXPECT_EQ(ZigZagDecode(std::numeric_limits<uint16_t>::max() - 1u),
727 std::numeric_limits<int16_t>::max());
728}
729
730TEST(Varint, ZigZagDecode_Int32) {
731 EXPECT_EQ(ZigZagDecode(uint32_t(0)), int32_t(0));
732 EXPECT_EQ(ZigZagDecode(uint32_t(1)), int32_t(-1));
733 EXPECT_EQ(ZigZagDecode(uint32_t(2)), int32_t(1));
734 EXPECT_EQ(ZigZagDecode(uint32_t(3)), int32_t(-2));
735 EXPECT_EQ(ZigZagDecode(uint32_t(4)), int32_t(2));
736 EXPECT_EQ(ZigZagDecode(uint32_t(255)), int32_t(-128));
737 EXPECT_EQ(ZigZagDecode(uint32_t(256)), int32_t(128));
738 EXPECT_EQ(ZigZagDecode(uint32_t(666665)), int32_t(-333333));
739 EXPECT_EQ(ZigZagDecode(uint32_t(666666)), int32_t(333333));
740 EXPECT_EQ(ZigZagDecode(std::numeric_limits<uint32_t>::max()),
741 std::numeric_limits<int32_t>::min());
742 EXPECT_EQ(ZigZagDecode(std::numeric_limits<uint32_t>::max() - 1u),
743 std::numeric_limits<int32_t>::max());
744}
745
746TEST(Varint, ZigZagDecode_Int64) {
747 EXPECT_EQ(ZigZagDecode(uint64_t(0)), int64_t(0));
748 EXPECT_EQ(ZigZagDecode(uint64_t(1)), int64_t(-1));
749 EXPECT_EQ(ZigZagDecode(uint64_t(2)), int64_t(1));
750 EXPECT_EQ(ZigZagDecode(uint64_t(3)), int64_t(-2));
751 EXPECT_EQ(ZigZagDecode(uint64_t(4)), int64_t(2));
752 EXPECT_EQ(ZigZagDecode(uint64_t(6666666665)), int64_t(-3333333333));
753 EXPECT_EQ(ZigZagDecode(uint64_t(6666666666)), int64_t(3333333333));
754 EXPECT_EQ(ZigZagDecode(std::numeric_limits<uint64_t>::max()),
755 std::numeric_limits<int64_t>::min());
756 EXPECT_EQ(ZigZagDecode(std::numeric_limits<uint64_t>::max() - 1llu),
757 std::numeric_limits<int64_t>::max());
758}
759
760TEST(Varint, ZigZagEncodeDecode) {
761 EXPECT_EQ(ZigZagDecode(ZigZagEncode(0)), 0);
762 EXPECT_EQ(ZigZagDecode(ZigZagEncode(1)), 1);
763 EXPECT_EQ(ZigZagDecode(ZigZagEncode(-1)), -1);
764 EXPECT_EQ(ZigZagDecode(ZigZagEncode(8675309)), 8675309);
765 EXPECT_EQ(ZigZagDecode(ZigZagEncode(std::numeric_limits<int8_t>::min())),
766 std::numeric_limits<int8_t>::min());
767 EXPECT_EQ(ZigZagDecode(ZigZagEncode(std::numeric_limits<int8_t>::max())),
768 std::numeric_limits<int8_t>::max());
769 EXPECT_EQ(ZigZagDecode(ZigZagEncode(std::numeric_limits<int16_t>::min())),
770 std::numeric_limits<int16_t>::min());
771 EXPECT_EQ(ZigZagDecode(ZigZagEncode(std::numeric_limits<int16_t>::max())),
772 std::numeric_limits<int16_t>::max());
773 EXPECT_EQ(ZigZagDecode(ZigZagEncode(std::numeric_limits<int32_t>::min())),
774 std::numeric_limits<int32_t>::min());
775 EXPECT_EQ(ZigZagDecode(ZigZagEncode(std::numeric_limits<int32_t>::max())),
776 std::numeric_limits<int32_t>::max());
777 EXPECT_EQ(ZigZagDecode(ZigZagEncode(std::numeric_limits<int64_t>::min())),
778 std::numeric_limits<int64_t>::min());
779 EXPECT_EQ(ZigZagDecode(ZigZagEncode(std::numeric_limits<int64_t>::max())),
780 std::numeric_limits<int64_t>::max());
781}
782
Alexei Frolov82d3cb32019-11-27 14:38:39 -0800783} // namespace
784} // namespace pw::varint