1 //===- llvm/unittest/Support/LEB128Test.cpp - LEB128 function tests -------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/LEB128.h"
10 #include "llvm/Support/DataTypes.h"
11 #include "llvm/Support/raw_ostream.h"
12 #include "gtest/gtest.h"
18 TEST(LEB128Test
, EncodeSLEB128
) {
19 #define EXPECT_SLEB128_EQ(EXPECTED, VALUE, PAD) \
21 std::string Expected(EXPECTED, sizeof(EXPECTED) - 1); \
23 /* encodeSLEB128(uint64_t, raw_ostream &, unsigned) */ \
24 std::string Actual1; \
25 raw_string_ostream Stream(Actual1); \
26 encodeSLEB128(VALUE, Stream, PAD); \
28 EXPECT_EQ(Expected, Actual1); \
30 /* encodeSLEB128(uint64_t, uint8_t *, unsigned) */ \
32 unsigned Size = encodeSLEB128(VALUE, Buffer, PAD); \
33 std::string Actual2(reinterpret_cast<const char *>(Buffer), Size); \
34 EXPECT_EQ(Expected, Actual2); \
38 EXPECT_SLEB128_EQ("\x00", 0, 0);
39 EXPECT_SLEB128_EQ("\x01", 1, 0);
40 EXPECT_SLEB128_EQ("\x7f", -1, 0);
41 EXPECT_SLEB128_EQ("\x3f", 63, 0);
42 EXPECT_SLEB128_EQ("\x41", -63, 0);
43 EXPECT_SLEB128_EQ("\x40", -64, 0);
44 EXPECT_SLEB128_EQ("\xbf\x7f", -65, 0);
45 EXPECT_SLEB128_EQ("\xc0\x00", 64, 0);
47 // Encode SLEB128 with some extra padding bytes
48 EXPECT_SLEB128_EQ("\x80\x00", 0, 2);
49 EXPECT_SLEB128_EQ("\x80\x80\x00", 0, 3);
50 EXPECT_SLEB128_EQ("\xff\x80\x00", 0x7f, 3);
51 EXPECT_SLEB128_EQ("\xff\x80\x80\x00", 0x7f, 4);
52 EXPECT_SLEB128_EQ("\x80\x81\x00", 0x80, 3);
53 EXPECT_SLEB128_EQ("\x80\x81\x80\x00", 0x80, 4);
54 EXPECT_SLEB128_EQ("\xc0\x7f", -0x40, 2);
56 EXPECT_SLEB128_EQ("\xc0\xff\x7f", -0x40, 3);
57 EXPECT_SLEB128_EQ("\x80\xff\x7f", -0x80, 3);
58 EXPECT_SLEB128_EQ("\x80\xff\xff\x7f", -0x80, 4);
60 #undef EXPECT_SLEB128_EQ
63 TEST(LEB128Test
, EncodeULEB128
) {
64 #define EXPECT_ULEB128_EQ(EXPECTED, VALUE, PAD) \
66 std::string Expected(EXPECTED, sizeof(EXPECTED) - 1); \
68 /* encodeULEB128(uint64_t, raw_ostream &, unsigned) */ \
69 std::string Actual1; \
70 raw_string_ostream Stream(Actual1); \
71 encodeULEB128(VALUE, Stream, PAD); \
73 EXPECT_EQ(Expected, Actual1); \
75 /* encodeULEB128(uint64_t, uint8_t *, unsigned) */ \
77 unsigned Size = encodeULEB128(VALUE, Buffer, PAD); \
78 std::string Actual2(reinterpret_cast<const char *>(Buffer), Size); \
79 EXPECT_EQ(Expected, Actual2); \
83 EXPECT_ULEB128_EQ("\x00", 0, 0);
84 EXPECT_ULEB128_EQ("\x01", 1, 0);
85 EXPECT_ULEB128_EQ("\x3f", 63, 0);
86 EXPECT_ULEB128_EQ("\x40", 64, 0);
87 EXPECT_ULEB128_EQ("\x7f", 0x7f, 0);
88 EXPECT_ULEB128_EQ("\x80\x01", 0x80, 0);
89 EXPECT_ULEB128_EQ("\x81\x01", 0x81, 0);
90 EXPECT_ULEB128_EQ("\x90\x01", 0x90, 0);
91 EXPECT_ULEB128_EQ("\xff\x01", 0xff, 0);
92 EXPECT_ULEB128_EQ("\x80\x02", 0x100, 0);
93 EXPECT_ULEB128_EQ("\x81\x02", 0x101, 0);
95 // Encode ULEB128 with some extra padding bytes
96 EXPECT_ULEB128_EQ("\x80\x00", 0, 2);
97 EXPECT_ULEB128_EQ("\x80\x80\x00", 0, 3);
98 EXPECT_ULEB128_EQ("\xff\x00", 0x7f, 2);
99 EXPECT_ULEB128_EQ("\xff\x80\x00", 0x7f, 3);
100 EXPECT_ULEB128_EQ("\x80\x81\x00", 0x80, 3);
101 EXPECT_ULEB128_EQ("\x80\x81\x80\x00", 0x80, 4);
103 #undef EXPECT_ULEB128_EQ
106 TEST(LEB128Test
, DecodeULEB128
) {
107 #define EXPECT_DECODE_ULEB128_EQ(EXPECTED, VALUE) \
109 unsigned ActualSize = 0; \
110 uint64_t Actual = decodeULEB128(reinterpret_cast<const uint8_t *>(VALUE), \
112 EXPECT_EQ(sizeof(VALUE) - 1, ActualSize); \
113 EXPECT_EQ(EXPECTED, Actual); \
117 EXPECT_EQ(0u, decodeULEB128(nullptr, nullptr, nullptr));
120 EXPECT_DECODE_ULEB128_EQ(0u, "\x00");
121 EXPECT_DECODE_ULEB128_EQ(1u, "\x01");
122 EXPECT_DECODE_ULEB128_EQ(63u, "\x3f");
123 EXPECT_DECODE_ULEB128_EQ(64u, "\x40");
124 EXPECT_DECODE_ULEB128_EQ(0x7fu
, "\x7f");
125 EXPECT_DECODE_ULEB128_EQ(0x80u
, "\x80\x01");
126 EXPECT_DECODE_ULEB128_EQ(0x81u
, "\x81\x01");
127 EXPECT_DECODE_ULEB128_EQ(0x90u
, "\x90\x01");
128 EXPECT_DECODE_ULEB128_EQ(0xffu
, "\xff\x01");
129 EXPECT_DECODE_ULEB128_EQ(0x100u
, "\x80\x02");
130 EXPECT_DECODE_ULEB128_EQ(0x101u
, "\x81\x02");
131 EXPECT_DECODE_ULEB128_EQ(4294975616ULL, "\x80\xc1\x80\x80\x10");
133 // Decode ULEB128 with extra padding bytes
134 EXPECT_DECODE_ULEB128_EQ(0u, "\x80\x00");
135 EXPECT_DECODE_ULEB128_EQ(0u, "\x80\x80\x00");
136 EXPECT_DECODE_ULEB128_EQ(0x7fu
, "\xff\x00");
137 EXPECT_DECODE_ULEB128_EQ(0x7fu
, "\xff\x80\x00");
138 EXPECT_DECODE_ULEB128_EQ(0x80u
, "\x80\x81\x00");
139 EXPECT_DECODE_ULEB128_EQ(0x80u
, "\x80\x81\x80\x00");
140 EXPECT_DECODE_ULEB128_EQ(0x80u
, "\x80\x81\x80\x80\x80\x80\x80\x80\x80\x00");
141 EXPECT_DECODE_ULEB128_EQ(0x80000000'00000000ul
,
142 "\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01");
144 #undef EXPECT_DECODE_ULEB128_EQ
147 TEST(LEB128Test
, DecodeInvalidULEB128
) {
148 #define EXPECT_INVALID_ULEB128(VALUE, ERROR_OFFSET) \
150 const char *DefaultValue = VALUE; \
151 const uint8_t *Value = reinterpret_cast<const uint8_t *>(DefaultValue); \
152 const char *Error = nullptr; \
153 unsigned ErrorOffset = 0; \
155 decodeULEB128(Value, &ErrorOffset, Value + strlen(VALUE), &Error); \
156 EXPECT_NE(Error, nullptr); \
157 EXPECT_EQ(0ul, Actual); \
158 EXPECT_EQ(ERROR_OFFSET, ErrorOffset); \
159 Value = reinterpret_cast<const uint8_t *>(DefaultValue); \
161 Actual = decodeULEB128AndInc(Value, Value + strlen(VALUE), &Error); \
162 EXPECT_NE(Error, nullptr); \
163 EXPECT_EQ(0ul, Actual); \
164 EXPECT_EQ(ERROR_OFFSET, \
165 Value - reinterpret_cast<const uint8_t *>(DefaultValue)); \
169 EXPECT_INVALID_ULEB128("", 0u);
170 EXPECT_INVALID_ULEB128("\x80", 1u);
172 // Does not fit in 64 bits.
173 EXPECT_INVALID_ULEB128("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x02", 9u);
174 EXPECT_INVALID_ULEB128("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x02", 10u);
176 #undef EXPECT_INVALID_ULEB128
179 TEST(LEB128Test
, DecodeSLEB128
) {
180 #define EXPECT_DECODE_SLEB128_EQ(EXPECTED, VALUE) \
182 unsigned ActualSize = 0; \
183 int64_t Actual = decodeSLEB128(reinterpret_cast<const uint8_t *>(VALUE), \
185 EXPECT_EQ(sizeof(VALUE) - 1, ActualSize); \
186 EXPECT_EQ(EXPECTED, Actual); \
190 EXPECT_EQ(0, decodeSLEB128(nullptr, nullptr, nullptr));
193 EXPECT_DECODE_SLEB128_EQ(0L, "\x00");
194 EXPECT_DECODE_SLEB128_EQ(1L, "\x01");
195 EXPECT_DECODE_SLEB128_EQ(63L, "\x3f");
196 EXPECT_DECODE_SLEB128_EQ(-64L, "\x40");
197 EXPECT_DECODE_SLEB128_EQ(-63L, "\x41");
198 EXPECT_DECODE_SLEB128_EQ(-1L, "\x7f");
199 EXPECT_DECODE_SLEB128_EQ(128L, "\x80\x01");
200 EXPECT_DECODE_SLEB128_EQ(129L, "\x81\x01");
201 EXPECT_DECODE_SLEB128_EQ(-129L, "\xff\x7e");
202 EXPECT_DECODE_SLEB128_EQ(-128L, "\x80\x7f");
203 EXPECT_DECODE_SLEB128_EQ(-127L, "\x81\x7f");
204 EXPECT_DECODE_SLEB128_EQ(64L, "\xc0\x00");
205 EXPECT_DECODE_SLEB128_EQ(-12345L, "\xc7\x9f\x7f");
207 // Decode unnormalized SLEB128 with extra padding bytes.
208 EXPECT_DECODE_SLEB128_EQ(0L, "\x80\x00");
209 EXPECT_DECODE_SLEB128_EQ(0L, "\x80\x80\x00");
210 EXPECT_DECODE_SLEB128_EQ(0x7fL
, "\xff\x00");
211 EXPECT_DECODE_SLEB128_EQ(0x7fL
, "\xff\x80\x00");
212 EXPECT_DECODE_SLEB128_EQ(0x80L
, "\x80\x81\x00");
213 EXPECT_DECODE_SLEB128_EQ(0x80L
, "\x80\x81\x80\x00");
214 EXPECT_DECODE_SLEB128_EQ(0x80L
, "\x80\x81\x80\x80\x80\x80\x80\x80\x80\x00");
215 EXPECT_DECODE_SLEB128_EQ(-2L, "\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
216 EXPECT_DECODE_SLEB128_EQ(INT64_MIN
,
217 "\x80\x80\x80\x80\x80\x80\x80\x80\x80\x7F");
218 EXPECT_DECODE_SLEB128_EQ(INT64_MAX
,
219 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00");
221 #undef EXPECT_DECODE_SLEB128_EQ
224 TEST(LEB128Test
, DecodeInvalidSLEB128
) {
225 #define EXPECT_INVALID_SLEB128(VALUE, ERROR_OFFSET) \
227 const char *DefaultValue = VALUE; \
228 const uint8_t *Value = reinterpret_cast<const uint8_t *>(DefaultValue); \
229 const char *Error = nullptr; \
230 unsigned ErrorOffset = 0; \
232 decodeSLEB128(Value, &ErrorOffset, Value + strlen(VALUE), &Error); \
233 EXPECT_NE(Error, nullptr); \
234 EXPECT_EQ(0ul, Actual); \
235 EXPECT_EQ(ERROR_OFFSET, ErrorOffset); \
236 Value = reinterpret_cast<const uint8_t *>(DefaultValue); \
238 Actual = decodeSLEB128AndInc(Value, Value + strlen(VALUE), &Error); \
239 EXPECT_NE(Error, nullptr); \
240 EXPECT_EQ(0ul, Actual); \
241 EXPECT_EQ(ERROR_OFFSET, \
242 Value - reinterpret_cast<const uint8_t *>(DefaultValue)); \
246 EXPECT_INVALID_SLEB128("", 0u);
247 EXPECT_INVALID_SLEB128("\x80", 1u);
249 // Does not fit in 64 bits.
250 EXPECT_INVALID_SLEB128("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01", 9u);
251 EXPECT_INVALID_SLEB128("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x7E", 9u);
252 EXPECT_INVALID_SLEB128("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x02", 10u);
253 EXPECT_INVALID_SLEB128("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7E", 9u);
254 EXPECT_INVALID_SLEB128("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01", 9u);
255 EXPECT_INVALID_SLEB128("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7E", 10u);
256 EXPECT_INVALID_SLEB128("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00", 10u);
258 #undef EXPECT_INVALID_SLEB128
261 TEST(LEB128Test
, DecodeAndInc
) {
262 #define EXPECT_LEB128(FUN, VALUE, SIZE) \
264 const char *DefaultValue = VALUE; \
265 const uint8_t *V = reinterpret_cast<const uint8_t *>(DefaultValue), \
267 auto Expected = FUN(P), Actual = FUN##AndInc(P, P + strlen(VALUE)); \
268 EXPECT_EQ(Actual, Expected); \
269 EXPECT_EQ(P - V, SIZE); \
271 EXPECT_LEB128(decodeULEB128
, "\x7f", 1);
272 EXPECT_LEB128(decodeULEB128
, "\x80\x01", 2);
273 EXPECT_LEB128(decodeSLEB128
, "\x7f", 1);
274 EXPECT_LEB128(decodeSLEB128
, "\x80\x01", 2);
277 #define EXPECT_LEB128(FUN, VALUE, SIZE) \
279 const uint8_t *V = reinterpret_cast<const uint8_t *>(VALUE), *P = V; \
280 auto Expected = FUN(P), Actual = FUN##AndIncUnsafe(P); \
281 EXPECT_EQ(Actual, Expected); \
282 EXPECT_EQ(P - V, SIZE); \
284 EXPECT_LEB128(decodeULEB128
, "\x7f", 1);
285 EXPECT_LEB128(decodeULEB128
, "\x80\x01", 2);
289 TEST(LEB128Test
, SLEB128Size
) {
290 // Positive Value Testing Plan:
291 // (1) 128 ^ n - 1 ........ need (n+1) bytes
292 // (2) 128 ^ n ............ need (n+1) bytes
293 // (3) 128 ^ n * 63 ....... need (n+1) bytes
294 // (4) 128 ^ n * 64 - 1 ... need (n+1) bytes
295 // (5) 128 ^ n * 64 ....... need (n+2) bytes
297 EXPECT_EQ(1u, getSLEB128Size(0x0LL
));
298 EXPECT_EQ(1u, getSLEB128Size(0x1LL
));
299 EXPECT_EQ(1u, getSLEB128Size(0x3fLL
));
300 EXPECT_EQ(1u, getSLEB128Size(0x3fLL
));
301 EXPECT_EQ(2u, getSLEB128Size(0x40LL
));
303 EXPECT_EQ(2u, getSLEB128Size(0x7fLL
));
304 EXPECT_EQ(2u, getSLEB128Size(0x80LL
));
305 EXPECT_EQ(2u, getSLEB128Size(0x1f80LL
));
306 EXPECT_EQ(2u, getSLEB128Size(0x1fffLL
));
307 EXPECT_EQ(3u, getSLEB128Size(0x2000LL
));
309 EXPECT_EQ(3u, getSLEB128Size(0x3fffLL
));
310 EXPECT_EQ(3u, getSLEB128Size(0x4000LL
));
311 EXPECT_EQ(3u, getSLEB128Size(0xfc000LL
));
312 EXPECT_EQ(3u, getSLEB128Size(0xfffffLL
));
313 EXPECT_EQ(4u, getSLEB128Size(0x100000LL
));
315 EXPECT_EQ(4u, getSLEB128Size(0x1fffffLL
));
316 EXPECT_EQ(4u, getSLEB128Size(0x200000LL
));
317 EXPECT_EQ(4u, getSLEB128Size(0x7e00000LL
));
318 EXPECT_EQ(4u, getSLEB128Size(0x7ffffffLL
));
319 EXPECT_EQ(5u, getSLEB128Size(0x8000000LL
));
321 EXPECT_EQ(5u, getSLEB128Size(0xfffffffLL
));
322 EXPECT_EQ(5u, getSLEB128Size(0x10000000LL
));
323 EXPECT_EQ(5u, getSLEB128Size(0x3f0000000LL
));
324 EXPECT_EQ(5u, getSLEB128Size(0x3ffffffffLL
));
325 EXPECT_EQ(6u, getSLEB128Size(0x400000000LL
));
327 EXPECT_EQ(6u, getSLEB128Size(0x7ffffffffLL
));
328 EXPECT_EQ(6u, getSLEB128Size(0x800000000LL
));
329 EXPECT_EQ(6u, getSLEB128Size(0x1f800000000LL
));
330 EXPECT_EQ(6u, getSLEB128Size(0x1ffffffffffLL
));
331 EXPECT_EQ(7u, getSLEB128Size(0x20000000000LL
));
333 EXPECT_EQ(7u, getSLEB128Size(0x3ffffffffffLL
));
334 EXPECT_EQ(7u, getSLEB128Size(0x40000000000LL
));
335 EXPECT_EQ(7u, getSLEB128Size(0xfc0000000000LL
));
336 EXPECT_EQ(7u, getSLEB128Size(0xffffffffffffLL
));
337 EXPECT_EQ(8u, getSLEB128Size(0x1000000000000LL
));
339 EXPECT_EQ(8u, getSLEB128Size(0x1ffffffffffffLL
));
340 EXPECT_EQ(8u, getSLEB128Size(0x2000000000000LL
));
341 EXPECT_EQ(8u, getSLEB128Size(0x7e000000000000LL
));
342 EXPECT_EQ(8u, getSLEB128Size(0x7fffffffffffffLL
));
343 EXPECT_EQ(9u, getSLEB128Size(0x80000000000000LL
));
345 EXPECT_EQ(9u, getSLEB128Size(0xffffffffffffffLL
));
346 EXPECT_EQ(9u, getSLEB128Size(0x100000000000000LL
));
347 EXPECT_EQ(9u, getSLEB128Size(0x3f00000000000000LL
));
348 EXPECT_EQ(9u, getSLEB128Size(0x3fffffffffffffffLL
));
349 EXPECT_EQ(10u, getSLEB128Size(0x4000000000000000LL
));
351 EXPECT_EQ(10u, getSLEB128Size(0x7fffffffffffffffLL
));
352 EXPECT_EQ(10u, getSLEB128Size(INT64_MAX
));
354 // Negative Value Testing Plan:
355 // (1) - 128 ^ n - 1 ........ need (n+1) bytes
356 // (2) - 128 ^ n ............ need (n+1) bytes
357 // (3) - 128 ^ n * 63 ....... need (n+1) bytes
358 // (4) - 128 ^ n * 64 ....... need (n+1) bytes (different from positive one)
359 // (5) - 128 ^ n * 65 - 1 ... need (n+2) bytes (if n > 0)
360 // (6) - 128 ^ n * 65 ....... need (n+2) bytes
362 EXPECT_EQ(1u, getSLEB128Size(0x0LL
));
363 EXPECT_EQ(1u, getSLEB128Size(-0x1LL
));
364 EXPECT_EQ(1u, getSLEB128Size(-0x3fLL
));
365 EXPECT_EQ(1u, getSLEB128Size(-0x40LL
));
366 EXPECT_EQ(1u, getSLEB128Size(-0x40LL
)); // special case
367 EXPECT_EQ(2u, getSLEB128Size(-0x41LL
));
369 EXPECT_EQ(2u, getSLEB128Size(-0x7fLL
));
370 EXPECT_EQ(2u, getSLEB128Size(-0x80LL
));
371 EXPECT_EQ(2u, getSLEB128Size(-0x1f80LL
));
372 EXPECT_EQ(2u, getSLEB128Size(-0x2000LL
));
373 EXPECT_EQ(3u, getSLEB128Size(-0x207fLL
));
374 EXPECT_EQ(3u, getSLEB128Size(-0x2080LL
));
376 EXPECT_EQ(3u, getSLEB128Size(-0x3fffLL
));
377 EXPECT_EQ(3u, getSLEB128Size(-0x4000LL
));
378 EXPECT_EQ(3u, getSLEB128Size(-0xfc000LL
));
379 EXPECT_EQ(3u, getSLEB128Size(-0x100000LL
));
380 EXPECT_EQ(4u, getSLEB128Size(-0x103fffLL
));
381 EXPECT_EQ(4u, getSLEB128Size(-0x104000LL
));
383 EXPECT_EQ(4u, getSLEB128Size(-0x1fffffLL
));
384 EXPECT_EQ(4u, getSLEB128Size(-0x200000LL
));
385 EXPECT_EQ(4u, getSLEB128Size(-0x7e00000LL
));
386 EXPECT_EQ(4u, getSLEB128Size(-0x8000000LL
));
387 EXPECT_EQ(5u, getSLEB128Size(-0x81fffffLL
));
388 EXPECT_EQ(5u, getSLEB128Size(-0x8200000LL
));
390 EXPECT_EQ(5u, getSLEB128Size(-0xfffffffLL
));
391 EXPECT_EQ(5u, getSLEB128Size(-0x10000000LL
));
392 EXPECT_EQ(5u, getSLEB128Size(-0x3f0000000LL
));
393 EXPECT_EQ(5u, getSLEB128Size(-0x400000000LL
));
394 EXPECT_EQ(6u, getSLEB128Size(-0x40fffffffLL
));
395 EXPECT_EQ(6u, getSLEB128Size(-0x410000000LL
));
397 EXPECT_EQ(6u, getSLEB128Size(-0x7ffffffffLL
));
398 EXPECT_EQ(6u, getSLEB128Size(-0x800000000LL
));
399 EXPECT_EQ(6u, getSLEB128Size(-0x1f800000000LL
));
400 EXPECT_EQ(6u, getSLEB128Size(-0x20000000000LL
));
401 EXPECT_EQ(7u, getSLEB128Size(-0x207ffffffffLL
));
402 EXPECT_EQ(7u, getSLEB128Size(-0x20800000000LL
));
404 EXPECT_EQ(7u, getSLEB128Size(-0x3ffffffffffLL
));
405 EXPECT_EQ(7u, getSLEB128Size(-0x40000000000LL
));
406 EXPECT_EQ(7u, getSLEB128Size(-0xfc0000000000LL
));
407 EXPECT_EQ(7u, getSLEB128Size(-0x1000000000000LL
));
408 EXPECT_EQ(8u, getSLEB128Size(-0x103ffffffffffLL
));
409 EXPECT_EQ(8u, getSLEB128Size(-0x1040000000000LL
));
411 EXPECT_EQ(8u, getSLEB128Size(-0x1ffffffffffffLL
));
412 EXPECT_EQ(8u, getSLEB128Size(-0x2000000000000LL
));
413 EXPECT_EQ(8u, getSLEB128Size(-0x7e000000000000LL
));
414 EXPECT_EQ(8u, getSLEB128Size(-0x80000000000000LL
));
415 EXPECT_EQ(9u, getSLEB128Size(-0x81ffffffffffffLL
));
416 EXPECT_EQ(9u, getSLEB128Size(-0x82000000000000LL
));
418 EXPECT_EQ(9u, getSLEB128Size(-0xffffffffffffffLL
));
419 EXPECT_EQ(9u, getSLEB128Size(-0x100000000000000LL
));
420 EXPECT_EQ(9u, getSLEB128Size(-0x3f00000000000000LL
));
421 EXPECT_EQ(9u, getSLEB128Size(-0x4000000000000000LL
));
422 EXPECT_EQ(10u, getSLEB128Size(-0x40ffffffffffffffLL
));
423 EXPECT_EQ(10u, getSLEB128Size(-0x4100000000000000LL
));
425 EXPECT_EQ(10u, getSLEB128Size(-0x7fffffffffffffffLL
));
426 EXPECT_EQ(10u, getSLEB128Size(-0x7fffffffffffffffLL
- 1));
427 EXPECT_EQ(10u, getSLEB128Size(INT64_MIN
));
430 TEST(LEB128Test
, ULEB128Size
) {
432 // (1) 128 ^ n ............ need (n+1) bytes
433 // (2) 128 ^ n * 64 ....... need (n+1) bytes
434 // (3) 128 ^ (n+1) - 1 .... need (n+1) bytes
436 EXPECT_EQ(1u, getULEB128Size(0)); // special case
438 EXPECT_EQ(1u, getULEB128Size(0x1ULL
));
439 EXPECT_EQ(1u, getULEB128Size(0x40ULL
));
440 EXPECT_EQ(1u, getULEB128Size(0x7fULL
));
442 EXPECT_EQ(2u, getULEB128Size(0x80ULL
));
443 EXPECT_EQ(2u, getULEB128Size(0x2000ULL
));
444 EXPECT_EQ(2u, getULEB128Size(0x3fffULL
));
446 EXPECT_EQ(3u, getULEB128Size(0x4000ULL
));
447 EXPECT_EQ(3u, getULEB128Size(0x100000ULL
));
448 EXPECT_EQ(3u, getULEB128Size(0x1fffffULL
));
450 EXPECT_EQ(4u, getULEB128Size(0x200000ULL
));
451 EXPECT_EQ(4u, getULEB128Size(0x8000000ULL
));
452 EXPECT_EQ(4u, getULEB128Size(0xfffffffULL
));
454 EXPECT_EQ(5u, getULEB128Size(0x10000000ULL
));
455 EXPECT_EQ(5u, getULEB128Size(0x400000000ULL
));
456 EXPECT_EQ(5u, getULEB128Size(0x7ffffffffULL
));
458 EXPECT_EQ(6u, getULEB128Size(0x800000000ULL
));
459 EXPECT_EQ(6u, getULEB128Size(0x20000000000ULL
));
460 EXPECT_EQ(6u, getULEB128Size(0x3ffffffffffULL
));
462 EXPECT_EQ(7u, getULEB128Size(0x40000000000ULL
));
463 EXPECT_EQ(7u, getULEB128Size(0x1000000000000ULL
));
464 EXPECT_EQ(7u, getULEB128Size(0x1ffffffffffffULL
));
466 EXPECT_EQ(8u, getULEB128Size(0x2000000000000ULL
));
467 EXPECT_EQ(8u, getULEB128Size(0x80000000000000ULL
));
468 EXPECT_EQ(8u, getULEB128Size(0xffffffffffffffULL
));
470 EXPECT_EQ(9u, getULEB128Size(0x100000000000000ULL
));
471 EXPECT_EQ(9u, getULEB128Size(0x4000000000000000ULL
));
472 EXPECT_EQ(9u, getULEB128Size(0x7fffffffffffffffULL
));
474 EXPECT_EQ(10u, getULEB128Size(0x8000000000000000ULL
));
476 EXPECT_EQ(10u, getULEB128Size(UINT64_MAX
));
479 } // anonymous namespace