[Alignment][NFC] Migrate Instructions to Align
[llvm-core.git] / unittests / BinaryFormat / MsgPackWriterTest.cpp
blob3a5ba9af08f6c8728b048b508c6b0dca5816ecc2
1 //===- MsgPackWriterTest.cpp ------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/BinaryFormat/MsgPackWriter.h"
10 #include "llvm/BinaryFormat/MsgPack.h"
11 #include "gtest/gtest.h"
13 using namespace llvm;
14 using namespace llvm::msgpack;
16 struct MsgPackWriter : testing::Test {
17 std::string Buffer;
18 llvm::raw_string_ostream OStream;
19 Writer MPWriter;
21 MsgPackWriter() : OStream(Buffer), MPWriter(OStream) {}
24 TEST_F(MsgPackWriter, TestWriteNil) {
25 MPWriter.writeNil();
26 EXPECT_EQ(OStream.str(), "\xc0");
29 TEST_F(MsgPackWriter, TestWriteBool) {
30 MPWriter.write(true);
31 MPWriter.write(false);
32 EXPECT_EQ(OStream.str(), "\xc3\xc2");
35 TEST_F(MsgPackWriter, TestWriteFixPositiveInt) {
36 // FixPositiveInt form bitpattern starts with 0, so max FixPositiveInt
37 // is 01111111 = 127
38 for (uint64_t u = 0; u <= 127; ++u) {
39 Buffer.clear();
40 MPWriter.write(u);
41 std::string Output = OStream.str();
42 EXPECT_EQ(Output.size(), 1u);
43 EXPECT_EQ(Output.data()[0], static_cast<uint8_t>(u));
47 TEST_F(MsgPackWriter, TestWriteUInt8Min) {
48 // See TestWriteFixPositiveInt for why 128 is the min non-fix Int8
49 uint64_t u = 128;
50 MPWriter.write(u);
51 EXPECT_EQ(OStream.str(), "\xcc\x80");
54 TEST_F(MsgPackWriter, TestWriteUInt8) {
55 uint64_t u = 221;
56 MPWriter.write(u);
57 EXPECT_EQ(OStream.str(), "\xcc\xdd");
60 TEST_F(MsgPackWriter, TestWriteUInt8Max) {
61 uint64_t u = UINT8_MAX;
62 MPWriter.write(u);
63 EXPECT_EQ(OStream.str(), "\xcc\xff");
66 TEST_F(MsgPackWriter, TestWriteUInt16Min) {
67 uint64_t u = static_cast<uint64_t>(UINT8_MAX) + 1;
68 MPWriter.write(u);
69 EXPECT_EQ(OStream.str(), std::string("\xcd\x01\x00", 3));
72 TEST_F(MsgPackWriter, TestWriteUInt16) {
73 uint64_t u = 43981;
74 MPWriter.write(u);
75 EXPECT_EQ(OStream.str(), "\xcd\xab\xcd");
78 TEST_F(MsgPackWriter, TestWriteUInt16Max) {
79 uint64_t u = UINT16_MAX;
80 MPWriter.write(u);
81 EXPECT_EQ(OStream.str(), "\xcd\xff\xff");
84 TEST_F(MsgPackWriter, TestWriteUInt32Min) {
85 uint64_t u = static_cast<uint64_t>(UINT16_MAX) + 1;
86 MPWriter.write(u);
87 EXPECT_EQ(OStream.str(), std::string("\xce\x00\x01\x00\x00", 5));
90 TEST_F(MsgPackWriter, TestWriteUInt32) {
91 uint64_t u = 2882400186;
92 MPWriter.write(u);
93 EXPECT_EQ(OStream.str(), "\xce\xab\xcd\xef\xba");
96 TEST_F(MsgPackWriter, TestWriteUInt32Max) {
97 uint64_t u = UINT32_MAX;
98 MPWriter.write(u);
99 EXPECT_EQ(OStream.str(), "\xce\xff\xff\xff\xff");
102 TEST_F(MsgPackWriter, TestWriteUInt64Min) {
103 uint64_t u = static_cast<uint64_t>(UINT32_MAX) + 1;
104 MPWriter.write(u);
105 EXPECT_EQ(OStream.str(),
106 std::string("\xcf\x00\x00\x00\x01\x00\x00\x00\x00", 9));
109 TEST_F(MsgPackWriter, TestWriteUInt64) {
110 uint64_t u = 0x010203040506074a;
111 MPWriter.write(u);
112 EXPECT_EQ(OStream.str(), "\xcf\x01\x02\x03\x04\x05\x06\x07\x4a");
115 TEST_F(MsgPackWriter, TestWriteUInt64Max) {
116 uint64_t u = UINT64_MAX;
117 MPWriter.write(u);
118 EXPECT_EQ(OStream.str(), "\xcf\xff\xff\xff\xff\xff\xff\xff\xff");
121 TEST_F(MsgPackWriter, TestWriteFixNegativeInt) {
122 // Positive values will be written in a UInt form, so max FixNegativeInt is -1
124 // FixNegativeInt form bitpattern starts with 111, so min FixNegativeInt
125 // is 11100000 = -32
126 for (int64_t i = -1; i >= -32; --i) {
127 Buffer.clear();
128 MPWriter.write(i);
129 std::string Output = OStream.str();
130 EXPECT_EQ(Output.size(), 1u);
131 EXPECT_EQ(static_cast<int8_t>(Output.data()[0]), static_cast<int8_t>(i));
135 TEST_F(MsgPackWriter, TestWriteInt8Max) {
136 // See TestWriteFixNegativeInt for why -33 is the max non-fix Int8
137 int64_t i = -33;
138 MPWriter.write(i);
139 EXPECT_EQ(OStream.str(), "\xd0\xdf");
142 TEST_F(MsgPackWriter, TestWriteInt8) {
143 int64_t i = -40;
144 MPWriter.write(i);
145 EXPECT_EQ(OStream.str(), "\xd0\xd8");
148 TEST_F(MsgPackWriter, TestWriteInt8Min) {
149 int64_t i = INT8_MIN;
150 MPWriter.write(i);
151 EXPECT_EQ(OStream.str(), "\xd0\x80");
154 TEST_F(MsgPackWriter, TestWriteInt16Max) {
155 int64_t i = static_cast<int64_t>(INT8_MIN) - 1;
156 MPWriter.write(i);
157 EXPECT_EQ(OStream.str(), "\xd1\xff\x7f");
160 TEST_F(MsgPackWriter, TestWriteInt16) {
161 int64_t i = -4369;
162 MPWriter.write(i);
163 EXPECT_EQ(OStream.str(), "\xd1\xee\xef");
166 TEST_F(MsgPackWriter, TestWriteInt16Min) {
167 int64_t i = INT16_MIN;
168 MPWriter.write(i);
169 EXPECT_EQ(OStream.str(), std::string("\xd1\x80\x00", 3));
172 TEST_F(MsgPackWriter, TestWriteInt32Max) {
173 int64_t i = static_cast<int64_t>(INT16_MIN) - 1;
174 MPWriter.write(i);
175 EXPECT_EQ(OStream.str(), "\xd2\xff\xff\x7f\xff");
178 TEST_F(MsgPackWriter, TestWriteInt32) {
179 int64_t i = -286331153;
180 MPWriter.write(i);
181 EXPECT_EQ(OStream.str(), "\xd2\xee\xee\xee\xef");
184 TEST_F(MsgPackWriter, TestWriteInt32Min) {
185 int64_t i = INT32_MIN;
186 MPWriter.write(i);
187 EXPECT_EQ(OStream.str(), std::string("\xd2\x80\x00\x00\x00", 5));
190 TEST_F(MsgPackWriter, TestWriteInt64Max) {
191 int64_t i = static_cast<int64_t>(INT32_MIN) - 1;
192 MPWriter.write(i);
193 EXPECT_EQ(OStream.str(), "\xd3\xff\xff\xff\xff\x7f\xff\xff\xff");
196 TEST_F(MsgPackWriter, TestWriteInt64) {
197 int64_t i = -1229782938247303441;
198 MPWriter.write(i);
199 EXPECT_EQ(OStream.str(), "\xd3\xee\xee\xee\xee\xee\xee\xee\xef");
202 TEST_F(MsgPackWriter, TestWriteInt64Min) {
203 int64_t i = INT64_MIN;
204 MPWriter.write(i);
205 EXPECT_EQ(OStream.str(),
206 std::string("\xd3\x80\x00\x00\x00\x00\x00\x00\x00", 9));
209 TEST_F(MsgPackWriter, TestWriteFloat32) {
210 float f = -3.6973142664068907e+28;
211 MPWriter.write(f);
212 EXPECT_EQ(OStream.str(), "\xca\xee\xee\xee\xef");
215 TEST_F(MsgPackWriter, TestWriteFloat64) {
216 double d = -2.2899894549927042e+226;
217 MPWriter.write(d);
218 EXPECT_EQ(OStream.str(), "\xcb\xee\xee\xee\xee\xee\xee\xee\xef");
221 TEST_F(MsgPackWriter, TestWriteFixStrMin) {
222 std::string s;
223 MPWriter.write(s);
224 EXPECT_EQ(OStream.str(), "\xa0");
227 TEST_F(MsgPackWriter, TestWriteFixStr) {
228 std::string s = "foo";
229 MPWriter.write(s);
230 EXPECT_EQ(OStream.str(), "\xa3"
231 "foo");
234 TEST_F(MsgPackWriter, TestWriteFixStrMax) {
235 // FixStr format's size is a 5 bit unsigned integer, so max is 11111 = 31
236 std::string s(31, 'a');
237 MPWriter.write(s);
238 EXPECT_EQ(OStream.str(), std::string("\xbf") + s);
241 TEST_F(MsgPackWriter, TestWriteStr8Min) {
242 // See TestWriteFixStrMax for why 32 is the min non-fix Str8
243 std::string s(32, 'a');
244 MPWriter.write(s);
245 EXPECT_EQ(OStream.str(), std::string("\xd9\x20") + s);
248 TEST_F(MsgPackWriter, TestWriteStr8) {
249 std::string s(33, 'a');
250 MPWriter.write(s);
251 EXPECT_EQ(OStream.str(), std::string("\xd9\x21") + s);
254 TEST_F(MsgPackWriter, TestWriteStr8Max) {
255 std::string s(UINT8_MAX, 'a');
256 MPWriter.write(s);
257 EXPECT_EQ(OStream.str(), std::string("\xd9\xff") + s);
260 TEST_F(MsgPackWriter, TestWriteStr16Min) {
261 std::string s(static_cast<uint64_t>(UINT8_MAX) + 1, 'a');
262 MPWriter.write(s);
263 EXPECT_EQ(OStream.str(), std::string("\xda\x01\x00", 3) + s);
266 TEST_F(MsgPackWriter, TestWriteStr16) {
267 std::string s(511, 'a');
268 MPWriter.write(s);
269 EXPECT_EQ(OStream.str(), std::string("\xda\x01\xff") + s);
272 TEST_F(MsgPackWriter, TestWriteStr16Max) {
273 std::string s(UINT16_MAX, 'a');
274 MPWriter.write(s);
275 EXPECT_EQ(OStream.str(), std::string("\xda\xff\xff") + s);
278 TEST_F(MsgPackWriter, TestWriteStr32Min) {
279 std::string s(static_cast<uint64_t>(UINT16_MAX) + 1, 'a');
280 MPWriter.write(s);
281 EXPECT_EQ(OStream.str(), std::string("\xdb\x00\x01\x00\x00", 5) + s);
284 TEST_F(MsgPackWriter, TestWriteStr32) {
285 std::string s(131071, 'a');
286 MPWriter.write(s);
287 EXPECT_EQ(OStream.str(), std::string("\xdb\x00\x01\xff\xff", 5) + s);
290 TEST_F(MsgPackWriter, TestWriteBin8Min) {
291 std::string s;
292 MPWriter.write(MemoryBufferRef(s, ""));
293 EXPECT_EQ(OStream.str(), std::string("\xc4\x00", 2) + s);
296 TEST_F(MsgPackWriter, TestWriteBin8) {
297 std::string s(5, 'a');
298 MPWriter.write(MemoryBufferRef(s, ""));
299 EXPECT_EQ(OStream.str(), std::string("\xc4\x05") + s);
302 TEST_F(MsgPackWriter, TestWriteBin8Max) {
303 std::string s(UINT8_MAX, 'a');
304 MPWriter.write(MemoryBufferRef(s, ""));
305 EXPECT_EQ(OStream.str(), std::string("\xc4\xff") + s);
308 TEST_F(MsgPackWriter, TestWriteBin16Min) {
309 std::string s(static_cast<uint64_t>(UINT8_MAX) + 1, 'a');
310 MPWriter.write(MemoryBufferRef(s, ""));
311 EXPECT_EQ(OStream.str(), std::string("\xc5\x01\x00", 3) + s);
314 TEST_F(MsgPackWriter, TestWriteBin16) {
315 std::string s(511, 'a');
316 MPWriter.write(MemoryBufferRef(s, ""));
317 EXPECT_EQ(OStream.str(), "\xc5\x01\xff" + s);
320 TEST_F(MsgPackWriter, TestWriteBin16Max) {
321 std::string s(UINT16_MAX, 'a');
322 MPWriter.write(MemoryBufferRef(s, ""));
323 EXPECT_EQ(OStream.str(), std::string("\xc5\xff\xff") + s);
326 TEST_F(MsgPackWriter, TestWriteBin32Min) {
327 std::string s(static_cast<uint64_t>(UINT16_MAX) + 1, 'a');
328 MPWriter.write(MemoryBufferRef(s, ""));
329 EXPECT_EQ(OStream.str(), std::string("\xc6\x00\x01\x00\x00", 5) + s);
332 TEST_F(MsgPackWriter, TestWriteBin32) {
333 std::string s(131071, 'a');
334 MPWriter.write(MemoryBufferRef(s, ""));
335 EXPECT_EQ(OStream.str(), std::string("\xc6\x00\x01\xff\xff", 5) + s);
338 TEST_F(MsgPackWriter, TestWriteFixArrayMin) {
339 MPWriter.writeArraySize(0);
340 EXPECT_EQ(OStream.str(), "\x90");
343 TEST_F(MsgPackWriter, TestWriteFixArray) {
344 MPWriter.writeArraySize(4);
345 EXPECT_EQ(OStream.str(), "\x94");
348 TEST_F(MsgPackWriter, TestWriteFixArrayMax) {
349 // FixArray format's size is a 4 bit unsigned integer, so max is 1111 = 15
350 MPWriter.writeArraySize(15);
351 EXPECT_EQ(OStream.str(), "\x9f");
354 TEST_F(MsgPackWriter, TestWriteArray16Min) {
355 // See TestWriteFixArrayMax for why 16 is the min non-fix Array16
356 MPWriter.writeArraySize(16);
357 EXPECT_EQ(OStream.str(), std::string("\xdc\x00\x10", 3));
360 TEST_F(MsgPackWriter, TestWriteArray16) {
361 MPWriter.writeArraySize(273);
362 EXPECT_EQ(OStream.str(), "\xdc\x01\x11");
365 TEST_F(MsgPackWriter, TestWriteArray16Max) {
366 MPWriter.writeArraySize(UINT16_MAX);
367 EXPECT_EQ(OStream.str(), "\xdc\xff\xff");
370 TEST_F(MsgPackWriter, TestWriteArray32Min) {
371 MPWriter.writeArraySize(static_cast<uint64_t>(UINT16_MAX) + 1);
372 EXPECT_EQ(OStream.str(), std::string("\xdd\x00\x01\x00\x00", 5));
375 TEST_F(MsgPackWriter, TestWriteArray32) {
376 MPWriter.writeArraySize(131071);
377 EXPECT_EQ(OStream.str(), std::string("\xdd\x00\x01\xff\xff", 5));
380 TEST_F(MsgPackWriter, TestWriteArray32Max) {
381 MPWriter.writeArraySize(UINT32_MAX);
382 EXPECT_EQ(OStream.str(), "\xdd\xff\xff\xff\xff");
385 TEST_F(MsgPackWriter, TestWriteFixMapMin) {
386 MPWriter.writeMapSize(0);
387 EXPECT_EQ(OStream.str(), "\x80");
390 TEST_F(MsgPackWriter, TestWriteFixMap) {
391 MPWriter.writeMapSize(4);
392 EXPECT_EQ(OStream.str(), "\x84");
395 TEST_F(MsgPackWriter, TestWriteFixMapMax) {
396 // FixMap format's size is a 4 bit unsigned integer, so max is 1111 = 15
397 MPWriter.writeMapSize(15);
398 EXPECT_EQ(OStream.str(), "\x8f");
401 TEST_F(MsgPackWriter, TestWriteMap16Min) {
402 // See TestWriteFixMapMax for why 16 is the min non-fix Map16
403 MPWriter.writeMapSize(16);
404 EXPECT_EQ(OStream.str(), std::string("\xde\x00\x10", 3));
407 TEST_F(MsgPackWriter, TestWriteMap16) {
408 MPWriter.writeMapSize(273);
409 EXPECT_EQ(OStream.str(), "\xde\x01\x11");
412 TEST_F(MsgPackWriter, TestWriteMap16Max) {
413 MPWriter.writeMapSize(UINT16_MAX);
414 EXPECT_EQ(OStream.str(), "\xde\xff\xff");
417 TEST_F(MsgPackWriter, TestWriteMap32Min) {
418 MPWriter.writeMapSize(static_cast<uint64_t>(UINT16_MAX) + 1);
419 EXPECT_EQ(OStream.str(), std::string("\xdf\x00\x01\x00\x00", 5));
422 TEST_F(MsgPackWriter, TestWriteMap32) {
423 MPWriter.writeMapSize(131071);
424 EXPECT_EQ(OStream.str(), std::string("\xdf\x00\x01\xff\xff", 5));
427 TEST_F(MsgPackWriter, TestWriteMap32Max) {
428 MPWriter.writeMapSize(UINT32_MAX);
429 EXPECT_EQ(OStream.str(), std::string("\xdf\xff\xff\xff\xff", 5));
432 // FixExt formats are only available for these specific lengths: 1, 2, 4, 8, 16
434 TEST_F(MsgPackWriter, TestWriteFixExt1) {
435 std::string s(1, 'a');
436 MPWriter.writeExt(0x01, MemoryBufferRef(s, ""));
437 EXPECT_EQ(OStream.str(), std::string("\xd4\x01") + s);
440 TEST_F(MsgPackWriter, TestWriteFixExt2) {
441 std::string s(2, 'a');
442 MPWriter.writeExt(0x01, MemoryBufferRef(s, ""));
443 EXPECT_EQ(OStream.str(), std::string("\xd5\x01") + s);
446 TEST_F(MsgPackWriter, TestWriteFixExt4) {
447 std::string s(4, 'a');
448 MPWriter.writeExt(0x01, MemoryBufferRef(s, ""));
449 EXPECT_EQ(OStream.str(), std::string("\xd6\x01") + s);
452 TEST_F(MsgPackWriter, TestWriteFixExt8) {
453 std::string s(8, 'a');
454 MPWriter.writeExt(0x01, MemoryBufferRef(s, ""));
455 EXPECT_EQ(OStream.str(), std::string("\xd7\x01") + s);
458 TEST_F(MsgPackWriter, TestWriteFixExt16) {
459 std::string s(16, 'a');
460 MPWriter.writeExt(0x01, MemoryBufferRef(s, ""));
461 EXPECT_EQ(OStream.str(), std::string("\xd8\x01") + s);
464 TEST_F(MsgPackWriter, TestWriteExt8Min) {
465 std::string s;
466 MPWriter.writeExt(0x01, MemoryBufferRef(s, ""));
467 EXPECT_EQ(OStream.str(), std::string("\xc7\x00\x01", 3) + s);
470 TEST_F(MsgPackWriter, TestWriteExt8) {
471 std::string s(0x2a, 'a');
472 MPWriter.writeExt(0x01, MemoryBufferRef(s, ""));
473 EXPECT_EQ(OStream.str(), std::string("\xc7\x2a\x01") + s);
476 TEST_F(MsgPackWriter, TestWriteExt8Max) {
477 std::string s(UINT8_MAX, 'a');
478 MPWriter.writeExt(0x01, MemoryBufferRef(s, ""));
479 EXPECT_EQ(OStream.str(), std::string("\xc7\xff\x01") + s);
482 TEST_F(MsgPackWriter, TestWriteExt16Min) {
483 std::string s(static_cast<uint16_t>(UINT8_MAX) + 1, 'a');
484 MPWriter.writeExt(0x02, MemoryBufferRef(s, ""));
485 EXPECT_EQ(OStream.str(), std::string("\xc8\x01\x00\x02", 4) + s);
488 TEST_F(MsgPackWriter, TestWriteExt16) {
489 std::string s(273, 'a');
490 MPWriter.writeExt(0x01, MemoryBufferRef(s, ""));
491 EXPECT_EQ(OStream.str(), std::string("\xc8\x01\x11\x01") + s);
494 TEST_F(MsgPackWriter, TestWriteExt16Max) {
495 std::string s(UINT16_MAX, 'a');
496 MPWriter.writeExt(0x01, MemoryBufferRef(s, ""));
497 EXPECT_EQ(OStream.str(), std::string("\xc8\xff\xff\x01") + s);
500 TEST_F(MsgPackWriter, TestWriteExt32Min) {
501 std::string s(static_cast<uint32_t>(UINT16_MAX) + 1, 'a');
502 MPWriter.writeExt(0x02, MemoryBufferRef(s, ""));
503 EXPECT_EQ(OStream.str(), std::string("\xc9\x00\x01\x00\x00\x02", 6) + s);
506 TEST_F(MsgPackWriter, TestWriteCompatibleNoStr8) {
507 Writer CompatWriter(OStream, true);
508 std::string s(32, 'a');
509 CompatWriter.write(s);
510 EXPECT_EQ(OStream.str(), std::string("\xda\x00\x20", 3) + s);
513 TEST_F(MsgPackWriter, TestWriteCompatibleNoBin) {
514 Writer CompatWriter(OStream, true);
515 std::string s;
517 #ifdef GTEST_HAS_DEATH_TEST
518 #ifndef NDEBUG
519 EXPECT_DEATH(CompatWriter.write(MemoryBufferRef(s, "")), "compatible mode");
520 #endif
521 #endif