1 //===- llvm/unittest/Support/LEB128Test.cpp - LEB128 function tests -------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Support/LEB128.h"
11 #include "llvm/Support/DataTypes.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include "gtest/gtest.h"
14 #include <string>
15 using namespace llvm;
16
17 namespace {
18
TEST(LEB128Test,EncodeSLEB128)19 TEST(LEB128Test, EncodeSLEB128) {
20 #define EXPECT_SLEB128_EQ(EXPECTED, VALUE, PAD) \
21 do { \
22 std::string Expected(EXPECTED, sizeof(EXPECTED) - 1); \
23 \
24 /* encodeSLEB128(uint64_t, raw_ostream &, unsigned) */ \
25 std::string Actual1; \
26 raw_string_ostream Stream(Actual1); \
27 encodeSLEB128(VALUE, Stream, PAD); \
28 Stream.flush(); \
29 EXPECT_EQ(Expected, Actual1); \
30 \
31 /* encodeSLEB128(uint64_t, uint8_t *, unsigned) */ \
32 uint8_t Buffer[32]; \
33 unsigned Size = encodeSLEB128(VALUE, Buffer, PAD); \
34 std::string Actual2(reinterpret_cast<const char *>(Buffer), Size); \
35 EXPECT_EQ(Expected, Actual2); \
36 } while (0)
37
38 // Encode SLEB128
39 EXPECT_SLEB128_EQ("\x00", 0, 0);
40 EXPECT_SLEB128_EQ("\x01", 1, 0);
41 EXPECT_SLEB128_EQ("\x7f", -1, 0);
42 EXPECT_SLEB128_EQ("\x3f", 63, 0);
43 EXPECT_SLEB128_EQ("\x41", -63, 0);
44 EXPECT_SLEB128_EQ("\x40", -64, 0);
45 EXPECT_SLEB128_EQ("\xbf\x7f", -65, 0);
46 EXPECT_SLEB128_EQ("\xc0\x00", 64, 0);
47
48 // Encode SLEB128 with some extra padding bytes
49 EXPECT_SLEB128_EQ("\x80\x00", 0, 2);
50 EXPECT_SLEB128_EQ("\x80\x80\x00", 0, 3);
51 EXPECT_SLEB128_EQ("\xff\x80\x00", 0x7f, 3);
52 EXPECT_SLEB128_EQ("\xff\x80\x80\x00", 0x7f, 4);
53 EXPECT_SLEB128_EQ("\x80\x81\x00", 0x80, 3);
54 EXPECT_SLEB128_EQ("\x80\x81\x80\x00", 0x80, 4);
55 EXPECT_SLEB128_EQ("\xc0\x7f", -0x40, 2);
56
57 EXPECT_SLEB128_EQ("\xc0\xff\x7f", -0x40, 3);
58 EXPECT_SLEB128_EQ("\x80\xff\x7f", -0x80, 3);
59 EXPECT_SLEB128_EQ("\x80\xff\xff\x7f", -0x80, 4);
60
61 #undef EXPECT_SLEB128_EQ
62 }
63
TEST(LEB128Test,EncodeULEB128)64 TEST(LEB128Test, EncodeULEB128) {
65 #define EXPECT_ULEB128_EQ(EXPECTED, VALUE, PAD) \
66 do { \
67 std::string Expected(EXPECTED, sizeof(EXPECTED) - 1); \
68 \
69 /* encodeULEB128(uint64_t, raw_ostream &, unsigned) */ \
70 std::string Actual1; \
71 raw_string_ostream Stream(Actual1); \
72 encodeULEB128(VALUE, Stream, PAD); \
73 Stream.flush(); \
74 EXPECT_EQ(Expected, Actual1); \
75 \
76 /* encodeULEB128(uint64_t, uint8_t *, unsigned) */ \
77 uint8_t Buffer[32]; \
78 unsigned Size = encodeULEB128(VALUE, Buffer, PAD); \
79 std::string Actual2(reinterpret_cast<const char *>(Buffer), Size); \
80 EXPECT_EQ(Expected, Actual2); \
81 } while (0)
82
83 // Encode ULEB128
84 EXPECT_ULEB128_EQ("\x00", 0, 0);
85 EXPECT_ULEB128_EQ("\x01", 1, 0);
86 EXPECT_ULEB128_EQ("\x3f", 63, 0);
87 EXPECT_ULEB128_EQ("\x40", 64, 0);
88 EXPECT_ULEB128_EQ("\x7f", 0x7f, 0);
89 EXPECT_ULEB128_EQ("\x80\x01", 0x80, 0);
90 EXPECT_ULEB128_EQ("\x81\x01", 0x81, 0);
91 EXPECT_ULEB128_EQ("\x90\x01", 0x90, 0);
92 EXPECT_ULEB128_EQ("\xff\x01", 0xff, 0);
93 EXPECT_ULEB128_EQ("\x80\x02", 0x100, 0);
94 EXPECT_ULEB128_EQ("\x81\x02", 0x101, 0);
95
96 // Encode ULEB128 with some extra padding bytes
97 EXPECT_ULEB128_EQ("\x80\x00", 0, 2);
98 EXPECT_ULEB128_EQ("\x80\x80\x00", 0, 3);
99 EXPECT_ULEB128_EQ("\xff\x00", 0x7f, 2);
100 EXPECT_ULEB128_EQ("\xff\x80\x00", 0x7f, 3);
101 EXPECT_ULEB128_EQ("\x80\x81\x00", 0x80, 3);
102 EXPECT_ULEB128_EQ("\x80\x81\x80\x00", 0x80, 4);
103
104 #undef EXPECT_ULEB128_EQ
105 }
106
TEST(LEB128Test,DecodeULEB128)107 TEST(LEB128Test, DecodeULEB128) {
108 #define EXPECT_DECODE_ULEB128_EQ(EXPECTED, VALUE) \
109 do { \
110 unsigned ActualSize = 0; \
111 uint64_t Actual = decodeULEB128(reinterpret_cast<const uint8_t *>(VALUE), \
112 &ActualSize); \
113 EXPECT_EQ(sizeof(VALUE) - 1, ActualSize); \
114 EXPECT_EQ(EXPECTED, Actual); \
115 } while (0)
116
117 // Decode ULEB128
118 EXPECT_DECODE_ULEB128_EQ(0u, "\x00");
119 EXPECT_DECODE_ULEB128_EQ(1u, "\x01");
120 EXPECT_DECODE_ULEB128_EQ(63u, "\x3f");
121 EXPECT_DECODE_ULEB128_EQ(64u, "\x40");
122 EXPECT_DECODE_ULEB128_EQ(0x7fu, "\x7f");
123 EXPECT_DECODE_ULEB128_EQ(0x80u, "\x80\x01");
124 EXPECT_DECODE_ULEB128_EQ(0x81u, "\x81\x01");
125 EXPECT_DECODE_ULEB128_EQ(0x90u, "\x90\x01");
126 EXPECT_DECODE_ULEB128_EQ(0xffu, "\xff\x01");
127 EXPECT_DECODE_ULEB128_EQ(0x100u, "\x80\x02");
128 EXPECT_DECODE_ULEB128_EQ(0x101u, "\x81\x02");
129 EXPECT_DECODE_ULEB128_EQ(4294975616ULL, "\x80\xc1\x80\x80\x10");
130
131 // Decode ULEB128 with extra padding bytes
132 EXPECT_DECODE_ULEB128_EQ(0u, "\x80\x00");
133 EXPECT_DECODE_ULEB128_EQ(0u, "\x80\x80\x00");
134 EXPECT_DECODE_ULEB128_EQ(0x7fu, "\xff\x00");
135 EXPECT_DECODE_ULEB128_EQ(0x7fu, "\xff\x80\x00");
136 EXPECT_DECODE_ULEB128_EQ(0x80u, "\x80\x81\x00");
137 EXPECT_DECODE_ULEB128_EQ(0x80u, "\x80\x81\x80\x00");
138
139 #undef EXPECT_DECODE_ULEB128_EQ
140 }
141
TEST(LEB128Test,DecodeSLEB128)142 TEST(LEB128Test, DecodeSLEB128) {
143 #define EXPECT_DECODE_SLEB128_EQ(EXPECTED, VALUE) \
144 do { \
145 unsigned ActualSize = 0; \
146 int64_t Actual = decodeSLEB128(reinterpret_cast<const uint8_t *>(VALUE), \
147 &ActualSize); \
148 EXPECT_EQ(sizeof(VALUE) - 1, ActualSize); \
149 EXPECT_EQ(EXPECTED, Actual); \
150 } while (0)
151
152 // Decode SLEB128
153 EXPECT_DECODE_SLEB128_EQ(0L, "\x00");
154 EXPECT_DECODE_SLEB128_EQ(1L, "\x01");
155 EXPECT_DECODE_SLEB128_EQ(63L, "\x3f");
156 EXPECT_DECODE_SLEB128_EQ(-64L, "\x40");
157 EXPECT_DECODE_SLEB128_EQ(-63L, "\x41");
158 EXPECT_DECODE_SLEB128_EQ(-1L, "\x7f");
159 EXPECT_DECODE_SLEB128_EQ(128L, "\x80\x01");
160 EXPECT_DECODE_SLEB128_EQ(129L, "\x81\x01");
161 EXPECT_DECODE_SLEB128_EQ(-129L, "\xff\x7e");
162 EXPECT_DECODE_SLEB128_EQ(-128L, "\x80\x7f");
163 EXPECT_DECODE_SLEB128_EQ(-127L, "\x81\x7f");
164 EXPECT_DECODE_SLEB128_EQ(64L, "\xc0\x00");
165 EXPECT_DECODE_SLEB128_EQ(-12345L, "\xc7\x9f\x7f");
166
167 // Decode unnormalized SLEB128 with extra padding bytes.
168 EXPECT_DECODE_SLEB128_EQ(0L, "\x80\x00");
169 EXPECT_DECODE_SLEB128_EQ(0L, "\x80\x80\x00");
170 EXPECT_DECODE_SLEB128_EQ(0x7fL, "\xff\x00");
171 EXPECT_DECODE_SLEB128_EQ(0x7fL, "\xff\x80\x00");
172 EXPECT_DECODE_SLEB128_EQ(0x80L, "\x80\x81\x00");
173 EXPECT_DECODE_SLEB128_EQ(0x80L, "\x80\x81\x80\x00");
174
175 #undef EXPECT_DECODE_SLEB128_EQ
176 }
177
TEST(LEB128Test,SLEB128Size)178 TEST(LEB128Test, SLEB128Size) {
179 // Positive Value Testing Plan:
180 // (1) 128 ^ n - 1 ........ need (n+1) bytes
181 // (2) 128 ^ n ............ need (n+1) bytes
182 // (3) 128 ^ n * 63 ....... need (n+1) bytes
183 // (4) 128 ^ n * 64 - 1 ... need (n+1) bytes
184 // (5) 128 ^ n * 64 ....... need (n+2) bytes
185
186 EXPECT_EQ(1u, getSLEB128Size(0x0LL));
187 EXPECT_EQ(1u, getSLEB128Size(0x1LL));
188 EXPECT_EQ(1u, getSLEB128Size(0x3fLL));
189 EXPECT_EQ(1u, getSLEB128Size(0x3fLL));
190 EXPECT_EQ(2u, getSLEB128Size(0x40LL));
191
192 EXPECT_EQ(2u, getSLEB128Size(0x7fLL));
193 EXPECT_EQ(2u, getSLEB128Size(0x80LL));
194 EXPECT_EQ(2u, getSLEB128Size(0x1f80LL));
195 EXPECT_EQ(2u, getSLEB128Size(0x1fffLL));
196 EXPECT_EQ(3u, getSLEB128Size(0x2000LL));
197
198 EXPECT_EQ(3u, getSLEB128Size(0x3fffLL));
199 EXPECT_EQ(3u, getSLEB128Size(0x4000LL));
200 EXPECT_EQ(3u, getSLEB128Size(0xfc000LL));
201 EXPECT_EQ(3u, getSLEB128Size(0xfffffLL));
202 EXPECT_EQ(4u, getSLEB128Size(0x100000LL));
203
204 EXPECT_EQ(4u, getSLEB128Size(0x1fffffLL));
205 EXPECT_EQ(4u, getSLEB128Size(0x200000LL));
206 EXPECT_EQ(4u, getSLEB128Size(0x7e00000LL));
207 EXPECT_EQ(4u, getSLEB128Size(0x7ffffffLL));
208 EXPECT_EQ(5u, getSLEB128Size(0x8000000LL));
209
210 EXPECT_EQ(5u, getSLEB128Size(0xfffffffLL));
211 EXPECT_EQ(5u, getSLEB128Size(0x10000000LL));
212 EXPECT_EQ(5u, getSLEB128Size(0x3f0000000LL));
213 EXPECT_EQ(5u, getSLEB128Size(0x3ffffffffLL));
214 EXPECT_EQ(6u, getSLEB128Size(0x400000000LL));
215
216 EXPECT_EQ(6u, getSLEB128Size(0x7ffffffffLL));
217 EXPECT_EQ(6u, getSLEB128Size(0x800000000LL));
218 EXPECT_EQ(6u, getSLEB128Size(0x1f800000000LL));
219 EXPECT_EQ(6u, getSLEB128Size(0x1ffffffffffLL));
220 EXPECT_EQ(7u, getSLEB128Size(0x20000000000LL));
221
222 EXPECT_EQ(7u, getSLEB128Size(0x3ffffffffffLL));
223 EXPECT_EQ(7u, getSLEB128Size(0x40000000000LL));
224 EXPECT_EQ(7u, getSLEB128Size(0xfc0000000000LL));
225 EXPECT_EQ(7u, getSLEB128Size(0xffffffffffffLL));
226 EXPECT_EQ(8u, getSLEB128Size(0x1000000000000LL));
227
228 EXPECT_EQ(8u, getSLEB128Size(0x1ffffffffffffLL));
229 EXPECT_EQ(8u, getSLEB128Size(0x2000000000000LL));
230 EXPECT_EQ(8u, getSLEB128Size(0x7e000000000000LL));
231 EXPECT_EQ(8u, getSLEB128Size(0x7fffffffffffffLL));
232 EXPECT_EQ(9u, getSLEB128Size(0x80000000000000LL));
233
234 EXPECT_EQ(9u, getSLEB128Size(0xffffffffffffffLL));
235 EXPECT_EQ(9u, getSLEB128Size(0x100000000000000LL));
236 EXPECT_EQ(9u, getSLEB128Size(0x3f00000000000000LL));
237 EXPECT_EQ(9u, getSLEB128Size(0x3fffffffffffffffLL));
238 EXPECT_EQ(10u, getSLEB128Size(0x4000000000000000LL));
239
240 EXPECT_EQ(10u, getSLEB128Size(0x7fffffffffffffffLL));
241 EXPECT_EQ(10u, getSLEB128Size(INT64_MAX));
242
243 // Negative Value Testing Plan:
244 // (1) - 128 ^ n - 1 ........ need (n+1) bytes
245 // (2) - 128 ^ n ............ need (n+1) bytes
246 // (3) - 128 ^ n * 63 ....... need (n+1) bytes
247 // (4) - 128 ^ n * 64 ....... need (n+1) bytes (different from positive one)
248 // (5) - 128 ^ n * 65 - 1 ... need (n+2) bytes (if n > 0)
249 // (6) - 128 ^ n * 65 ....... need (n+2) bytes
250
251 EXPECT_EQ(1u, getSLEB128Size(0x0LL));
252 EXPECT_EQ(1u, getSLEB128Size(-0x1LL));
253 EXPECT_EQ(1u, getSLEB128Size(-0x3fLL));
254 EXPECT_EQ(1u, getSLEB128Size(-0x40LL));
255 EXPECT_EQ(1u, getSLEB128Size(-0x40LL)); // special case
256 EXPECT_EQ(2u, getSLEB128Size(-0x41LL));
257
258 EXPECT_EQ(2u, getSLEB128Size(-0x7fLL));
259 EXPECT_EQ(2u, getSLEB128Size(-0x80LL));
260 EXPECT_EQ(2u, getSLEB128Size(-0x1f80LL));
261 EXPECT_EQ(2u, getSLEB128Size(-0x2000LL));
262 EXPECT_EQ(3u, getSLEB128Size(-0x207fLL));
263 EXPECT_EQ(3u, getSLEB128Size(-0x2080LL));
264
265 EXPECT_EQ(3u, getSLEB128Size(-0x3fffLL));
266 EXPECT_EQ(3u, getSLEB128Size(-0x4000LL));
267 EXPECT_EQ(3u, getSLEB128Size(-0xfc000LL));
268 EXPECT_EQ(3u, getSLEB128Size(-0x100000LL));
269 EXPECT_EQ(4u, getSLEB128Size(-0x103fffLL));
270 EXPECT_EQ(4u, getSLEB128Size(-0x104000LL));
271
272 EXPECT_EQ(4u, getSLEB128Size(-0x1fffffLL));
273 EXPECT_EQ(4u, getSLEB128Size(-0x200000LL));
274 EXPECT_EQ(4u, getSLEB128Size(-0x7e00000LL));
275 EXPECT_EQ(4u, getSLEB128Size(-0x8000000LL));
276 EXPECT_EQ(5u, getSLEB128Size(-0x81fffffLL));
277 EXPECT_EQ(5u, getSLEB128Size(-0x8200000LL));
278
279 EXPECT_EQ(5u, getSLEB128Size(-0xfffffffLL));
280 EXPECT_EQ(5u, getSLEB128Size(-0x10000000LL));
281 EXPECT_EQ(5u, getSLEB128Size(-0x3f0000000LL));
282 EXPECT_EQ(5u, getSLEB128Size(-0x400000000LL));
283 EXPECT_EQ(6u, getSLEB128Size(-0x40fffffffLL));
284 EXPECT_EQ(6u, getSLEB128Size(-0x410000000LL));
285
286 EXPECT_EQ(6u, getSLEB128Size(-0x7ffffffffLL));
287 EXPECT_EQ(6u, getSLEB128Size(-0x800000000LL));
288 EXPECT_EQ(6u, getSLEB128Size(-0x1f800000000LL));
289 EXPECT_EQ(6u, getSLEB128Size(-0x20000000000LL));
290 EXPECT_EQ(7u, getSLEB128Size(-0x207ffffffffLL));
291 EXPECT_EQ(7u, getSLEB128Size(-0x20800000000LL));
292
293 EXPECT_EQ(7u, getSLEB128Size(-0x3ffffffffffLL));
294 EXPECT_EQ(7u, getSLEB128Size(-0x40000000000LL));
295 EXPECT_EQ(7u, getSLEB128Size(-0xfc0000000000LL));
296 EXPECT_EQ(7u, getSLEB128Size(-0x1000000000000LL));
297 EXPECT_EQ(8u, getSLEB128Size(-0x103ffffffffffLL));
298 EXPECT_EQ(8u, getSLEB128Size(-0x1040000000000LL));
299
300 EXPECT_EQ(8u, getSLEB128Size(-0x1ffffffffffffLL));
301 EXPECT_EQ(8u, getSLEB128Size(-0x2000000000000LL));
302 EXPECT_EQ(8u, getSLEB128Size(-0x7e000000000000LL));
303 EXPECT_EQ(8u, getSLEB128Size(-0x80000000000000LL));
304 EXPECT_EQ(9u, getSLEB128Size(-0x81ffffffffffffLL));
305 EXPECT_EQ(9u, getSLEB128Size(-0x82000000000000LL));
306
307 EXPECT_EQ(9u, getSLEB128Size(-0xffffffffffffffLL));
308 EXPECT_EQ(9u, getSLEB128Size(-0x100000000000000LL));
309 EXPECT_EQ(9u, getSLEB128Size(-0x3f00000000000000LL));
310 EXPECT_EQ(9u, getSLEB128Size(-0x4000000000000000LL));
311 EXPECT_EQ(10u, getSLEB128Size(-0x40ffffffffffffffLL));
312 EXPECT_EQ(10u, getSLEB128Size(-0x4100000000000000LL));
313
314 EXPECT_EQ(10u, getSLEB128Size(-0x7fffffffffffffffLL));
315 EXPECT_EQ(10u, getSLEB128Size(-0x8000000000000000LL));
316 EXPECT_EQ(10u, getSLEB128Size(INT64_MIN));
317 }
318
TEST(LEB128Test,ULEB128Size)319 TEST(LEB128Test, ULEB128Size) {
320 // Testing Plan:
321 // (1) 128 ^ n ............ need (n+1) bytes
322 // (2) 128 ^ n * 64 ....... need (n+1) bytes
323 // (3) 128 ^ (n+1) - 1 .... need (n+1) bytes
324
325 EXPECT_EQ(1u, getULEB128Size(0)); // special case
326
327 EXPECT_EQ(1u, getULEB128Size(0x1ULL));
328 EXPECT_EQ(1u, getULEB128Size(0x40ULL));
329 EXPECT_EQ(1u, getULEB128Size(0x7fULL));
330
331 EXPECT_EQ(2u, getULEB128Size(0x80ULL));
332 EXPECT_EQ(2u, getULEB128Size(0x2000ULL));
333 EXPECT_EQ(2u, getULEB128Size(0x3fffULL));
334
335 EXPECT_EQ(3u, getULEB128Size(0x4000ULL));
336 EXPECT_EQ(3u, getULEB128Size(0x100000ULL));
337 EXPECT_EQ(3u, getULEB128Size(0x1fffffULL));
338
339 EXPECT_EQ(4u, getULEB128Size(0x200000ULL));
340 EXPECT_EQ(4u, getULEB128Size(0x8000000ULL));
341 EXPECT_EQ(4u, getULEB128Size(0xfffffffULL));
342
343 EXPECT_EQ(5u, getULEB128Size(0x10000000ULL));
344 EXPECT_EQ(5u, getULEB128Size(0x400000000ULL));
345 EXPECT_EQ(5u, getULEB128Size(0x7ffffffffULL));
346
347 EXPECT_EQ(6u, getULEB128Size(0x800000000ULL));
348 EXPECT_EQ(6u, getULEB128Size(0x20000000000ULL));
349 EXPECT_EQ(6u, getULEB128Size(0x3ffffffffffULL));
350
351 EXPECT_EQ(7u, getULEB128Size(0x40000000000ULL));
352 EXPECT_EQ(7u, getULEB128Size(0x1000000000000ULL));
353 EXPECT_EQ(7u, getULEB128Size(0x1ffffffffffffULL));
354
355 EXPECT_EQ(8u, getULEB128Size(0x2000000000000ULL));
356 EXPECT_EQ(8u, getULEB128Size(0x80000000000000ULL));
357 EXPECT_EQ(8u, getULEB128Size(0xffffffffffffffULL));
358
359 EXPECT_EQ(9u, getULEB128Size(0x100000000000000ULL));
360 EXPECT_EQ(9u, getULEB128Size(0x4000000000000000ULL));
361 EXPECT_EQ(9u, getULEB128Size(0x7fffffffffffffffULL));
362
363 EXPECT_EQ(10u, getULEB128Size(0x8000000000000000ULL));
364
365 EXPECT_EQ(10u, getULEB128Size(UINT64_MAX));
366 }
367
368 } // anonymous namespace
369