[obj2yaml] - Fix BB after r373315.
[llvm-complete.git] / unittests / Support / YAMLIOTest.cpp
blob0c9df11703172231c62ed0aa30a67aa609f2f91f
1 //===- unittest/Support/YAMLIOTest.cpp ------------------------------------===//
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/ADT/BitmaskEnum.h"
10 #include "llvm/ADT/StringMap.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/Support/Casting.h"
14 #include "llvm/Support/Endian.h"
15 #include "llvm/Support/Format.h"
16 #include "llvm/Support/YAMLTraits.h"
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
20 using llvm::yaml::Hex16;
21 using llvm::yaml::Hex32;
22 using llvm::yaml::Hex64;
23 using llvm::yaml::Hex8;
24 using llvm::yaml::Input;
25 using llvm::yaml::IO;
26 using llvm::yaml::isNumeric;
27 using llvm::yaml::MappingNormalization;
28 using llvm::yaml::MappingTraits;
29 using llvm::yaml::Output;
30 using llvm::yaml::ScalarTraits;
31 using ::testing::StartsWith;
36 static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
41 //===----------------------------------------------------------------------===//
42 // Test MappingTraits
43 //===----------------------------------------------------------------------===//
45 struct FooBar {
46 int foo;
47 int bar;
49 typedef std::vector<FooBar> FooBarSequence;
51 LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar)
53 struct FooBarContainer {
54 FooBarSequence fbs;
57 namespace llvm {
58 namespace yaml {
59 template <>
60 struct MappingTraits<FooBar> {
61 static void mapping(IO &io, FooBar& fb) {
62 io.mapRequired("foo", fb.foo);
63 io.mapRequired("bar", fb.bar);
67 template <> struct MappingTraits<FooBarContainer> {
68 static void mapping(IO &io, FooBarContainer &fb) {
69 io.mapRequired("fbs", fb.fbs);
77 // Test the reading of a yaml mapping
79 TEST(YAMLIO, TestMapRead) {
80 FooBar doc;
82 Input yin("---\nfoo: 3\nbar: 5\n...\n");
83 yin >> doc;
85 EXPECT_FALSE(yin.error());
86 EXPECT_EQ(doc.foo, 3);
87 EXPECT_EQ(doc.bar, 5);
91 Input yin("{foo: 3, bar: 5}");
92 yin >> doc;
94 EXPECT_FALSE(yin.error());
95 EXPECT_EQ(doc.foo, 3);
96 EXPECT_EQ(doc.bar, 5);
100 TEST(YAMLIO, TestMalformedMapRead) {
101 FooBar doc;
102 Input yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages);
103 yin >> doc;
104 EXPECT_TRUE(!!yin.error());
108 // Test the reading of a yaml sequence of mappings
110 TEST(YAMLIO, TestSequenceMapRead) {
111 FooBarSequence seq;
112 Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
113 yin >> seq;
115 EXPECT_FALSE(yin.error());
116 EXPECT_EQ(seq.size(), 2UL);
117 FooBar& map1 = seq[0];
118 FooBar& map2 = seq[1];
119 EXPECT_EQ(map1.foo, 3);
120 EXPECT_EQ(map1.bar, 5);
121 EXPECT_EQ(map2.foo, 7);
122 EXPECT_EQ(map2.bar, 9);
126 // Test the reading of a map containing a yaml sequence of mappings
128 TEST(YAMLIO, TestContainerSequenceMapRead) {
130 FooBarContainer cont;
131 Input yin2("---\nfbs:\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
132 yin2 >> cont;
134 EXPECT_FALSE(yin2.error());
135 EXPECT_EQ(cont.fbs.size(), 2UL);
136 EXPECT_EQ(cont.fbs[0].foo, 3);
137 EXPECT_EQ(cont.fbs[0].bar, 5);
138 EXPECT_EQ(cont.fbs[1].foo, 7);
139 EXPECT_EQ(cont.fbs[1].bar, 9);
143 FooBarContainer cont;
144 Input yin("---\nfbs:\n...\n");
145 yin >> cont;
146 // Okay: Empty node represents an empty array.
147 EXPECT_FALSE(yin.error());
148 EXPECT_EQ(cont.fbs.size(), 0UL);
152 FooBarContainer cont;
153 Input yin("---\nfbs: !!null null\n...\n");
154 yin >> cont;
155 // Okay: null represents an empty array.
156 EXPECT_FALSE(yin.error());
157 EXPECT_EQ(cont.fbs.size(), 0UL);
161 FooBarContainer cont;
162 Input yin("---\nfbs: ~\n...\n");
163 yin >> cont;
164 // Okay: null represents an empty array.
165 EXPECT_FALSE(yin.error());
166 EXPECT_EQ(cont.fbs.size(), 0UL);
170 FooBarContainer cont;
171 Input yin("---\nfbs: null\n...\n");
172 yin >> cont;
173 // Okay: null represents an empty array.
174 EXPECT_FALSE(yin.error());
175 EXPECT_EQ(cont.fbs.size(), 0UL);
180 // Test the reading of a map containing a malformed yaml sequence
182 TEST(YAMLIO, TestMalformedContainerSequenceMapRead) {
184 FooBarContainer cont;
185 Input yin("---\nfbs:\n foo: 3\n bar: 5\n...\n", nullptr,
186 suppressErrorMessages);
187 yin >> cont;
188 // Error: fbs is not a sequence.
189 EXPECT_TRUE(!!yin.error());
190 EXPECT_EQ(cont.fbs.size(), 0UL);
194 FooBarContainer cont;
195 Input yin("---\nfbs: 'scalar'\n...\n", nullptr, suppressErrorMessages);
196 yin >> cont;
197 // This should be an error.
198 EXPECT_TRUE(!!yin.error());
199 EXPECT_EQ(cont.fbs.size(), 0UL);
204 // Test writing then reading back a sequence of mappings
206 TEST(YAMLIO, TestSequenceMapWriteAndRead) {
207 std::string intermediate;
209 FooBar entry1;
210 entry1.foo = 10;
211 entry1.bar = -3;
212 FooBar entry2;
213 entry2.foo = 257;
214 entry2.bar = 0;
215 FooBarSequence seq;
216 seq.push_back(entry1);
217 seq.push_back(entry2);
219 llvm::raw_string_ostream ostr(intermediate);
220 Output yout(ostr);
221 yout << seq;
225 Input yin(intermediate);
226 FooBarSequence seq2;
227 yin >> seq2;
229 EXPECT_FALSE(yin.error());
230 EXPECT_EQ(seq2.size(), 2UL);
231 FooBar& map1 = seq2[0];
232 FooBar& map2 = seq2[1];
233 EXPECT_EQ(map1.foo, 10);
234 EXPECT_EQ(map1.bar, -3);
235 EXPECT_EQ(map2.foo, 257);
236 EXPECT_EQ(map2.bar, 0);
241 // Test YAML filename handling.
243 static void testErrorFilename(const llvm::SMDiagnostic &Error, void *) {
244 EXPECT_EQ(Error.getFilename(), "foo.yaml");
247 TEST(YAMLIO, TestGivenFilename) {
248 auto Buffer = llvm::MemoryBuffer::getMemBuffer("{ x: 42 }", "foo.yaml");
249 Input yin(*Buffer, nullptr, testErrorFilename);
250 FooBar Value;
251 yin >> Value;
253 EXPECT_TRUE(!!yin.error());
256 struct WithStringField {
257 std::string str1;
258 std::string str2;
259 std::string str3;
262 namespace llvm {
263 namespace yaml {
264 template <> struct MappingTraits<WithStringField> {
265 static void mapping(IO &io, WithStringField &fb) {
266 io.mapRequired("str1", fb.str1);
267 io.mapRequired("str2", fb.str2);
268 io.mapRequired("str3", fb.str3);
271 } // namespace yaml
272 } // namespace llvm
274 TEST(YAMLIO, MultilineStrings) {
275 WithStringField Original;
276 Original.str1 = "a multiline string\nfoobarbaz";
277 Original.str2 = "another one\rfoobarbaz";
278 Original.str3 = "a one-line string";
280 std::string Serialized;
282 llvm::raw_string_ostream OS(Serialized);
283 Output YOut(OS);
284 YOut << Original;
286 auto Expected = "---\n"
287 "str1: 'a multiline string\n"
288 "foobarbaz'\n"
289 "str2: 'another one\r"
290 "foobarbaz'\n"
291 "str3: a one-line string\n"
292 "...\n";
293 ASSERT_EQ(Serialized, Expected);
295 // Also check it parses back without the errors.
296 WithStringField Deserialized;
298 Input YIn(Serialized);
299 YIn >> Deserialized;
300 ASSERT_FALSE(YIn.error())
301 << "Parsing error occurred during deserialization. Serialized string:\n"
302 << Serialized;
304 EXPECT_EQ(Original.str1, Deserialized.str1);
305 EXPECT_EQ(Original.str2, Deserialized.str2);
306 EXPECT_EQ(Original.str3, Deserialized.str3);
309 TEST(YAMLIO, NoQuotesForTab) {
310 WithStringField WithTab;
311 WithTab.str1 = "aba\tcaba";
312 std::string Serialized;
314 llvm::raw_string_ostream OS(Serialized);
315 Output YOut(OS);
316 YOut << WithTab;
318 auto ExpectedPrefix = "---\n"
319 "str1: aba\tcaba\n";
320 EXPECT_THAT(Serialized, StartsWith(ExpectedPrefix));
323 //===----------------------------------------------------------------------===//
324 // Test built-in types
325 //===----------------------------------------------------------------------===//
327 struct BuiltInTypes {
328 llvm::StringRef str;
329 std::string stdstr;
330 uint64_t u64;
331 uint32_t u32;
332 uint16_t u16;
333 uint8_t u8;
334 bool b;
335 int64_t s64;
336 int32_t s32;
337 int16_t s16;
338 int8_t s8;
339 float f;
340 double d;
341 Hex8 h8;
342 Hex16 h16;
343 Hex32 h32;
344 Hex64 h64;
347 namespace llvm {
348 namespace yaml {
349 template <>
350 struct MappingTraits<BuiltInTypes> {
351 static void mapping(IO &io, BuiltInTypes& bt) {
352 io.mapRequired("str", bt.str);
353 io.mapRequired("stdstr", bt.stdstr);
354 io.mapRequired("u64", bt.u64);
355 io.mapRequired("u32", bt.u32);
356 io.mapRequired("u16", bt.u16);
357 io.mapRequired("u8", bt.u8);
358 io.mapRequired("b", bt.b);
359 io.mapRequired("s64", bt.s64);
360 io.mapRequired("s32", bt.s32);
361 io.mapRequired("s16", bt.s16);
362 io.mapRequired("s8", bt.s8);
363 io.mapRequired("f", bt.f);
364 io.mapRequired("d", bt.d);
365 io.mapRequired("h8", bt.h8);
366 io.mapRequired("h16", bt.h16);
367 io.mapRequired("h32", bt.h32);
368 io.mapRequired("h64", bt.h64);
376 // Test the reading of all built-in scalar conversions
378 TEST(YAMLIO, TestReadBuiltInTypes) {
379 BuiltInTypes map;
380 Input yin("---\n"
381 "str: hello there\n"
382 "stdstr: hello where?\n"
383 "u64: 5000000000\n"
384 "u32: 4000000000\n"
385 "u16: 65000\n"
386 "u8: 255\n"
387 "b: false\n"
388 "s64: -5000000000\n"
389 "s32: -2000000000\n"
390 "s16: -32000\n"
391 "s8: -127\n"
392 "f: 137.125\n"
393 "d: -2.8625\n"
394 "h8: 0xFF\n"
395 "h16: 0x8765\n"
396 "h32: 0xFEDCBA98\n"
397 "h64: 0xFEDCBA9876543210\n"
398 "...\n");
399 yin >> map;
401 EXPECT_FALSE(yin.error());
402 EXPECT_TRUE(map.str.equals("hello there"));
403 EXPECT_TRUE(map.stdstr == "hello where?");
404 EXPECT_EQ(map.u64, 5000000000ULL);
405 EXPECT_EQ(map.u32, 4000000000U);
406 EXPECT_EQ(map.u16, 65000);
407 EXPECT_EQ(map.u8, 255);
408 EXPECT_EQ(map.b, false);
409 EXPECT_EQ(map.s64, -5000000000LL);
410 EXPECT_EQ(map.s32, -2000000000L);
411 EXPECT_EQ(map.s16, -32000);
412 EXPECT_EQ(map.s8, -127);
413 EXPECT_EQ(map.f, 137.125);
414 EXPECT_EQ(map.d, -2.8625);
415 EXPECT_EQ(map.h8, Hex8(255));
416 EXPECT_EQ(map.h16, Hex16(0x8765));
417 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
418 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
423 // Test writing then reading back all built-in scalar types
425 TEST(YAMLIO, TestReadWriteBuiltInTypes) {
426 std::string intermediate;
428 BuiltInTypes map;
429 map.str = "one two";
430 map.stdstr = "three four";
431 map.u64 = 6000000000ULL;
432 map.u32 = 3000000000U;
433 map.u16 = 50000;
434 map.u8 = 254;
435 map.b = true;
436 map.s64 = -6000000000LL;
437 map.s32 = -2000000000;
438 map.s16 = -32000;
439 map.s8 = -128;
440 map.f = 3.25;
441 map.d = -2.8625;
442 map.h8 = 254;
443 map.h16 = 50000;
444 map.h32 = 3000000000U;
445 map.h64 = 6000000000LL;
447 llvm::raw_string_ostream ostr(intermediate);
448 Output yout(ostr);
449 yout << map;
453 Input yin(intermediate);
454 BuiltInTypes map;
455 yin >> map;
457 EXPECT_FALSE(yin.error());
458 EXPECT_TRUE(map.str.equals("one two"));
459 EXPECT_TRUE(map.stdstr == "three four");
460 EXPECT_EQ(map.u64, 6000000000ULL);
461 EXPECT_EQ(map.u32, 3000000000U);
462 EXPECT_EQ(map.u16, 50000);
463 EXPECT_EQ(map.u8, 254);
464 EXPECT_EQ(map.b, true);
465 EXPECT_EQ(map.s64, -6000000000LL);
466 EXPECT_EQ(map.s32, -2000000000L);
467 EXPECT_EQ(map.s16, -32000);
468 EXPECT_EQ(map.s8, -128);
469 EXPECT_EQ(map.f, 3.25);
470 EXPECT_EQ(map.d, -2.8625);
471 EXPECT_EQ(map.h8, Hex8(254));
472 EXPECT_EQ(map.h16, Hex16(50000));
473 EXPECT_EQ(map.h32, Hex32(3000000000U));
474 EXPECT_EQ(map.h64, Hex64(6000000000LL));
478 //===----------------------------------------------------------------------===//
479 // Test endian-aware types
480 //===----------------------------------------------------------------------===//
482 struct EndianTypes {
483 typedef llvm::support::detail::packed_endian_specific_integral<
484 float, llvm::support::little, llvm::support::unaligned>
485 ulittle_float;
486 typedef llvm::support::detail::packed_endian_specific_integral<
487 double, llvm::support::little, llvm::support::unaligned>
488 ulittle_double;
490 llvm::support::ulittle64_t u64;
491 llvm::support::ulittle32_t u32;
492 llvm::support::ulittle16_t u16;
493 llvm::support::little64_t s64;
494 llvm::support::little32_t s32;
495 llvm::support::little16_t s16;
496 ulittle_float f;
497 ulittle_double d;
500 namespace llvm {
501 namespace yaml {
502 template <> struct MappingTraits<EndianTypes> {
503 static void mapping(IO &io, EndianTypes &et) {
504 io.mapRequired("u64", et.u64);
505 io.mapRequired("u32", et.u32);
506 io.mapRequired("u16", et.u16);
507 io.mapRequired("s64", et.s64);
508 io.mapRequired("s32", et.s32);
509 io.mapRequired("s16", et.s16);
510 io.mapRequired("f", et.f);
511 io.mapRequired("d", et.d);
518 // Test the reading of all endian scalar conversions
520 TEST(YAMLIO, TestReadEndianTypes) {
521 EndianTypes map;
522 Input yin("---\n"
523 "u64: 5000000000\n"
524 "u32: 4000000000\n"
525 "u16: 65000\n"
526 "s64: -5000000000\n"
527 "s32: -2000000000\n"
528 "s16: -32000\n"
529 "f: 3.25\n"
530 "d: -2.8625\n"
531 "...\n");
532 yin >> map;
534 EXPECT_FALSE(yin.error());
535 EXPECT_EQ(map.u64, 5000000000ULL);
536 EXPECT_EQ(map.u32, 4000000000U);
537 EXPECT_EQ(map.u16, 65000);
538 EXPECT_EQ(map.s64, -5000000000LL);
539 EXPECT_EQ(map.s32, -2000000000L);
540 EXPECT_EQ(map.s16, -32000);
541 EXPECT_EQ(map.f, 3.25f);
542 EXPECT_EQ(map.d, -2.8625);
546 // Test writing then reading back all endian-aware scalar types
548 TEST(YAMLIO, TestReadWriteEndianTypes) {
549 std::string intermediate;
551 EndianTypes map;
552 map.u64 = 6000000000ULL;
553 map.u32 = 3000000000U;
554 map.u16 = 50000;
555 map.s64 = -6000000000LL;
556 map.s32 = -2000000000;
557 map.s16 = -32000;
558 map.f = 3.25f;
559 map.d = -2.8625;
561 llvm::raw_string_ostream ostr(intermediate);
562 Output yout(ostr);
563 yout << map;
567 Input yin(intermediate);
568 EndianTypes map;
569 yin >> map;
571 EXPECT_FALSE(yin.error());
572 EXPECT_EQ(map.u64, 6000000000ULL);
573 EXPECT_EQ(map.u32, 3000000000U);
574 EXPECT_EQ(map.u16, 50000);
575 EXPECT_EQ(map.s64, -6000000000LL);
576 EXPECT_EQ(map.s32, -2000000000L);
577 EXPECT_EQ(map.s16, -32000);
578 EXPECT_EQ(map.f, 3.25f);
579 EXPECT_EQ(map.d, -2.8625);
583 enum class Enum : uint16_t { One, Two };
584 enum class BitsetEnum : uint16_t {
585 ZeroOne = 0x01,
586 OneZero = 0x10,
587 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue*/ OneZero),
589 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
590 struct EndianEnums {
591 llvm::support::little_t<Enum> LittleEnum;
592 llvm::support::big_t<Enum> BigEnum;
593 llvm::support::little_t<BitsetEnum> LittleBitset;
594 llvm::support::big_t<BitsetEnum> BigBitset;
596 namespace llvm {
597 namespace yaml {
598 template <> struct ScalarEnumerationTraits<Enum> {
599 static void enumeration(IO &io, Enum &E) {
600 io.enumCase(E, "One", Enum::One);
601 io.enumCase(E, "Two", Enum::Two);
605 template <> struct ScalarBitSetTraits<BitsetEnum> {
606 static void bitset(IO &io, BitsetEnum &E) {
607 io.bitSetCase(E, "ZeroOne", BitsetEnum::ZeroOne);
608 io.bitSetCase(E, "OneZero", BitsetEnum::OneZero);
612 template <> struct MappingTraits<EndianEnums> {
613 static void mapping(IO &io, EndianEnums &EE) {
614 io.mapRequired("LittleEnum", EE.LittleEnum);
615 io.mapRequired("BigEnum", EE.BigEnum);
616 io.mapRequired("LittleBitset", EE.LittleBitset);
617 io.mapRequired("BigBitset", EE.BigBitset);
620 } // namespace yaml
621 } // namespace llvm
623 TEST(YAMLIO, TestReadEndianEnums) {
624 EndianEnums map;
625 Input yin("---\n"
626 "LittleEnum: One\n"
627 "BigEnum: Two\n"
628 "LittleBitset: [ ZeroOne ]\n"
629 "BigBitset: [ ZeroOne, OneZero ]\n"
630 "...\n");
631 yin >> map;
633 EXPECT_FALSE(yin.error());
634 EXPECT_EQ(Enum::One, map.LittleEnum);
635 EXPECT_EQ(Enum::Two, map.BigEnum);
636 EXPECT_EQ(BitsetEnum::ZeroOne, map.LittleBitset);
637 EXPECT_EQ(BitsetEnum::ZeroOne | BitsetEnum::OneZero, map.BigBitset);
640 TEST(YAMLIO, TestReadWriteEndianEnums) {
641 std::string intermediate;
643 EndianEnums map;
644 map.LittleEnum = Enum::Two;
645 map.BigEnum = Enum::One;
646 map.LittleBitset = BitsetEnum::OneZero | BitsetEnum::ZeroOne;
647 map.BigBitset = BitsetEnum::OneZero;
649 llvm::raw_string_ostream ostr(intermediate);
650 Output yout(ostr);
651 yout << map;
655 Input yin(intermediate);
656 EndianEnums map;
657 yin >> map;
659 EXPECT_FALSE(yin.error());
660 EXPECT_EQ(Enum::Two, map.LittleEnum);
661 EXPECT_EQ(Enum::One, map.BigEnum);
662 EXPECT_EQ(BitsetEnum::OneZero | BitsetEnum::ZeroOne, map.LittleBitset);
663 EXPECT_EQ(BitsetEnum::OneZero, map.BigBitset);
667 struct StringTypes {
668 llvm::StringRef str1;
669 llvm::StringRef str2;
670 llvm::StringRef str3;
671 llvm::StringRef str4;
672 llvm::StringRef str5;
673 llvm::StringRef str6;
674 llvm::StringRef str7;
675 llvm::StringRef str8;
676 llvm::StringRef str9;
677 llvm::StringRef str10;
678 llvm::StringRef str11;
679 std::string stdstr1;
680 std::string stdstr2;
681 std::string stdstr3;
682 std::string stdstr4;
683 std::string stdstr5;
684 std::string stdstr6;
685 std::string stdstr7;
686 std::string stdstr8;
687 std::string stdstr9;
688 std::string stdstr10;
689 std::string stdstr11;
690 std::string stdstr12;
691 std::string stdstr13;
694 namespace llvm {
695 namespace yaml {
696 template <>
697 struct MappingTraits<StringTypes> {
698 static void mapping(IO &io, StringTypes& st) {
699 io.mapRequired("str1", st.str1);
700 io.mapRequired("str2", st.str2);
701 io.mapRequired("str3", st.str3);
702 io.mapRequired("str4", st.str4);
703 io.mapRequired("str5", st.str5);
704 io.mapRequired("str6", st.str6);
705 io.mapRequired("str7", st.str7);
706 io.mapRequired("str8", st.str8);
707 io.mapRequired("str9", st.str9);
708 io.mapRequired("str10", st.str10);
709 io.mapRequired("str11", st.str11);
710 io.mapRequired("stdstr1", st.stdstr1);
711 io.mapRequired("stdstr2", st.stdstr2);
712 io.mapRequired("stdstr3", st.stdstr3);
713 io.mapRequired("stdstr4", st.stdstr4);
714 io.mapRequired("stdstr5", st.stdstr5);
715 io.mapRequired("stdstr6", st.stdstr6);
716 io.mapRequired("stdstr7", st.stdstr7);
717 io.mapRequired("stdstr8", st.stdstr8);
718 io.mapRequired("stdstr9", st.stdstr9);
719 io.mapRequired("stdstr10", st.stdstr10);
720 io.mapRequired("stdstr11", st.stdstr11);
721 io.mapRequired("stdstr12", st.stdstr12);
722 io.mapRequired("stdstr13", st.stdstr13);
728 TEST(YAMLIO, TestReadWriteStringTypes) {
729 std::string intermediate;
731 StringTypes map;
732 map.str1 = "'aaa";
733 map.str2 = "\"bbb";
734 map.str3 = "`ccc";
735 map.str4 = "@ddd";
736 map.str5 = "";
737 map.str6 = "0000000004000000";
738 map.str7 = "true";
739 map.str8 = "FALSE";
740 map.str9 = "~";
741 map.str10 = "0.2e20";
742 map.str11 = "0x30";
743 map.stdstr1 = "'eee";
744 map.stdstr2 = "\"fff";
745 map.stdstr3 = "`ggg";
746 map.stdstr4 = "@hhh";
747 map.stdstr5 = "";
748 map.stdstr6 = "0000000004000000";
749 map.stdstr7 = "true";
750 map.stdstr8 = "FALSE";
751 map.stdstr9 = "~";
752 map.stdstr10 = "0.2e20";
753 map.stdstr11 = "0x30";
754 map.stdstr12 = "- match";
755 map.stdstr13.assign("\0a\0b\0", 5);
757 llvm::raw_string_ostream ostr(intermediate);
758 Output yout(ostr);
759 yout << map;
762 llvm::StringRef flowOut(intermediate);
763 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
764 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
765 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
766 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
767 EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
768 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0000000004000000'\n"));
769 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'true'\n"));
770 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'FALSE'\n"));
771 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'~'\n"));
772 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0.2e20'\n"));
773 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0x30'\n"));
774 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'- match'\n"));
775 EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
776 EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
777 EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
778 EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
779 EXPECT_NE(std::string::npos, flowOut.find("''\n"));
780 EXPECT_NE(std::string::npos, flowOut.find("'0000000004000000'\n"));
781 EXPECT_NE(std::string::npos, flowOut.find("\"\\0a\\0b\\0\""));
784 Input yin(intermediate);
785 StringTypes map;
786 yin >> map;
788 EXPECT_FALSE(yin.error());
789 EXPECT_TRUE(map.str1.equals("'aaa"));
790 EXPECT_TRUE(map.str2.equals("\"bbb"));
791 EXPECT_TRUE(map.str3.equals("`ccc"));
792 EXPECT_TRUE(map.str4.equals("@ddd"));
793 EXPECT_TRUE(map.str5.equals(""));
794 EXPECT_TRUE(map.str6.equals("0000000004000000"));
795 EXPECT_TRUE(map.stdstr1 == "'eee");
796 EXPECT_TRUE(map.stdstr2 == "\"fff");
797 EXPECT_TRUE(map.stdstr3 == "`ggg");
798 EXPECT_TRUE(map.stdstr4 == "@hhh");
799 EXPECT_TRUE(map.stdstr5 == "");
800 EXPECT_TRUE(map.stdstr6 == "0000000004000000");
801 EXPECT_EQ(std::string("\0a\0b\0", 5), map.stdstr13);
805 //===----------------------------------------------------------------------===//
806 // Test ScalarEnumerationTraits
807 //===----------------------------------------------------------------------===//
809 enum Colors {
810 cRed,
811 cBlue,
812 cGreen,
813 cYellow
816 struct ColorMap {
817 Colors c1;
818 Colors c2;
819 Colors c3;
820 Colors c4;
821 Colors c5;
822 Colors c6;
825 namespace llvm {
826 namespace yaml {
827 template <>
828 struct ScalarEnumerationTraits<Colors> {
829 static void enumeration(IO &io, Colors &value) {
830 io.enumCase(value, "red", cRed);
831 io.enumCase(value, "blue", cBlue);
832 io.enumCase(value, "green", cGreen);
833 io.enumCase(value, "yellow",cYellow);
836 template <>
837 struct MappingTraits<ColorMap> {
838 static void mapping(IO &io, ColorMap& c) {
839 io.mapRequired("c1", c.c1);
840 io.mapRequired("c2", c.c2);
841 io.mapRequired("c3", c.c3);
842 io.mapOptional("c4", c.c4, cBlue); // supplies default
843 io.mapOptional("c5", c.c5, cYellow); // supplies default
844 io.mapOptional("c6", c.c6, cRed); // supplies default
852 // Test reading enumerated scalars
854 TEST(YAMLIO, TestEnumRead) {
855 ColorMap map;
856 Input yin("---\n"
857 "c1: blue\n"
858 "c2: red\n"
859 "c3: green\n"
860 "c5: yellow\n"
861 "...\n");
862 yin >> map;
864 EXPECT_FALSE(yin.error());
865 EXPECT_EQ(cBlue, map.c1);
866 EXPECT_EQ(cRed, map.c2);
867 EXPECT_EQ(cGreen, map.c3);
868 EXPECT_EQ(cBlue, map.c4); // tests default
869 EXPECT_EQ(cYellow,map.c5); // tests overridden
870 EXPECT_EQ(cRed, map.c6); // tests default
875 //===----------------------------------------------------------------------===//
876 // Test ScalarBitSetTraits
877 //===----------------------------------------------------------------------===//
879 enum MyFlags {
880 flagNone = 0,
881 flagBig = 1 << 0,
882 flagFlat = 1 << 1,
883 flagRound = 1 << 2,
884 flagPointy = 1 << 3
886 inline MyFlags operator|(MyFlags a, MyFlags b) {
887 return static_cast<MyFlags>(
888 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
891 struct FlagsMap {
892 MyFlags f1;
893 MyFlags f2;
894 MyFlags f3;
895 MyFlags f4;
899 namespace llvm {
900 namespace yaml {
901 template <>
902 struct ScalarBitSetTraits<MyFlags> {
903 static void bitset(IO &io, MyFlags &value) {
904 io.bitSetCase(value, "big", flagBig);
905 io.bitSetCase(value, "flat", flagFlat);
906 io.bitSetCase(value, "round", flagRound);
907 io.bitSetCase(value, "pointy",flagPointy);
910 template <>
911 struct MappingTraits<FlagsMap> {
912 static void mapping(IO &io, FlagsMap& c) {
913 io.mapRequired("f1", c.f1);
914 io.mapRequired("f2", c.f2);
915 io.mapRequired("f3", c.f3);
916 io.mapOptional("f4", c.f4, flagRound);
924 // Test reading flow sequence representing bit-mask values
926 TEST(YAMLIO, TestFlagsRead) {
927 FlagsMap map;
928 Input yin("---\n"
929 "f1: [ big ]\n"
930 "f2: [ round, flat ]\n"
931 "f3: []\n"
932 "...\n");
933 yin >> map;
935 EXPECT_FALSE(yin.error());
936 EXPECT_EQ(flagBig, map.f1);
937 EXPECT_EQ(flagRound|flagFlat, map.f2);
938 EXPECT_EQ(flagNone, map.f3); // check empty set
939 EXPECT_EQ(flagRound, map.f4); // check optional key
944 // Test writing then reading back bit-mask values
946 TEST(YAMLIO, TestReadWriteFlags) {
947 std::string intermediate;
949 FlagsMap map;
950 map.f1 = flagBig;
951 map.f2 = flagRound | flagFlat;
952 map.f3 = flagNone;
953 map.f4 = flagNone;
955 llvm::raw_string_ostream ostr(intermediate);
956 Output yout(ostr);
957 yout << map;
961 Input yin(intermediate);
962 FlagsMap map2;
963 yin >> map2;
965 EXPECT_FALSE(yin.error());
966 EXPECT_EQ(flagBig, map2.f1);
967 EXPECT_EQ(flagRound|flagFlat, map2.f2);
968 EXPECT_EQ(flagNone, map2.f3);
969 //EXPECT_EQ(flagRound, map2.f4); // check optional key
975 //===----------------------------------------------------------------------===//
976 // Test ScalarTraits
977 //===----------------------------------------------------------------------===//
979 struct MyCustomType {
980 int length;
981 int width;
984 struct MyCustomTypeMap {
985 MyCustomType f1;
986 MyCustomType f2;
987 int f3;
991 namespace llvm {
992 namespace yaml {
993 template <>
994 struct MappingTraits<MyCustomTypeMap> {
995 static void mapping(IO &io, MyCustomTypeMap& s) {
996 io.mapRequired("f1", s.f1);
997 io.mapRequired("f2", s.f2);
998 io.mapRequired("f3", s.f3);
1001 // MyCustomType is formatted as a yaml scalar. A value of
1002 // {length=3, width=4} would be represented in yaml as "3 by 4".
1003 template<>
1004 struct ScalarTraits<MyCustomType> {
1005 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
1006 out << llvm::format("%d by %d", value.length, value.width);
1008 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
1009 size_t byStart = scalar.find("by");
1010 if ( byStart != StringRef::npos ) {
1011 StringRef lenStr = scalar.slice(0, byStart);
1012 lenStr = lenStr.rtrim();
1013 if ( lenStr.getAsInteger(0, value.length) ) {
1014 return "malformed length";
1016 StringRef widthStr = scalar.drop_front(byStart+2);
1017 widthStr = widthStr.ltrim();
1018 if ( widthStr.getAsInteger(0, value.width) ) {
1019 return "malformed width";
1021 return StringRef();
1023 else {
1024 return "malformed by";
1027 static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
1034 // Test writing then reading back custom values
1036 TEST(YAMLIO, TestReadWriteMyCustomType) {
1037 std::string intermediate;
1039 MyCustomTypeMap map;
1040 map.f1.length = 1;
1041 map.f1.width = 4;
1042 map.f2.length = 100;
1043 map.f2.width = 400;
1044 map.f3 = 10;
1046 llvm::raw_string_ostream ostr(intermediate);
1047 Output yout(ostr);
1048 yout << map;
1052 Input yin(intermediate);
1053 MyCustomTypeMap map2;
1054 yin >> map2;
1056 EXPECT_FALSE(yin.error());
1057 EXPECT_EQ(1, map2.f1.length);
1058 EXPECT_EQ(4, map2.f1.width);
1059 EXPECT_EQ(100, map2.f2.length);
1060 EXPECT_EQ(400, map2.f2.width);
1061 EXPECT_EQ(10, map2.f3);
1066 //===----------------------------------------------------------------------===//
1067 // Test BlockScalarTraits
1068 //===----------------------------------------------------------------------===//
1070 struct MultilineStringType {
1071 std::string str;
1074 struct MultilineStringTypeMap {
1075 MultilineStringType name;
1076 MultilineStringType description;
1077 MultilineStringType ingredients;
1078 MultilineStringType recipes;
1079 MultilineStringType warningLabels;
1080 MultilineStringType documentation;
1081 int price;
1084 namespace llvm {
1085 namespace yaml {
1086 template <>
1087 struct MappingTraits<MultilineStringTypeMap> {
1088 static void mapping(IO &io, MultilineStringTypeMap& s) {
1089 io.mapRequired("name", s.name);
1090 io.mapRequired("description", s.description);
1091 io.mapRequired("ingredients", s.ingredients);
1092 io.mapRequired("recipes", s.recipes);
1093 io.mapRequired("warningLabels", s.warningLabels);
1094 io.mapRequired("documentation", s.documentation);
1095 io.mapRequired("price", s.price);
1099 // MultilineStringType is formatted as a yaml block literal scalar. A value of
1100 // "Hello\nWorld" would be represented in yaml as
1101 // |
1102 // Hello
1103 // World
1104 template <>
1105 struct BlockScalarTraits<MultilineStringType> {
1106 static void output(const MultilineStringType &value, void *ctxt,
1107 llvm::raw_ostream &out) {
1108 out << value.str;
1110 static StringRef input(StringRef scalar, void *ctxt,
1111 MultilineStringType &value) {
1112 value.str = scalar.str();
1113 return StringRef();
1119 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MultilineStringType)
1122 // Test writing then reading back custom values
1124 TEST(YAMLIO, TestReadWriteMultilineStringType) {
1125 std::string intermediate;
1127 MultilineStringTypeMap map;
1128 map.name.str = "An Item";
1129 map.description.str = "Hello\nWorld";
1130 map.ingredients.str = "SubItem 1\nSub Item 2\n\nSub Item 3\n";
1131 map.recipes.str = "\n\nTest 1\n\n\n";
1132 map.warningLabels.str = "";
1133 map.documentation.str = "\n\n";
1134 map.price = 350;
1136 llvm::raw_string_ostream ostr(intermediate);
1137 Output yout(ostr);
1138 yout << map;
1141 Input yin(intermediate);
1142 MultilineStringTypeMap map2;
1143 yin >> map2;
1145 EXPECT_FALSE(yin.error());
1146 EXPECT_EQ(map2.name.str, "An Item\n");
1147 EXPECT_EQ(map2.description.str, "Hello\nWorld\n");
1148 EXPECT_EQ(map2.ingredients.str, "SubItem 1\nSub Item 2\n\nSub Item 3\n");
1149 EXPECT_EQ(map2.recipes.str, "\n\nTest 1\n");
1150 EXPECT_TRUE(map2.warningLabels.str.empty());
1151 EXPECT_TRUE(map2.documentation.str.empty());
1152 EXPECT_EQ(map2.price, 350);
1157 // Test writing then reading back custom values
1159 TEST(YAMLIO, TestReadWriteBlockScalarDocuments) {
1160 std::string intermediate;
1162 std::vector<MultilineStringType> documents;
1163 MultilineStringType doc;
1164 doc.str = "Hello\nWorld";
1165 documents.push_back(doc);
1167 llvm::raw_string_ostream ostr(intermediate);
1168 Output yout(ostr);
1169 yout << documents;
1171 // Verify that the block scalar header was written out on the same line
1172 // as the document marker.
1173 EXPECT_NE(llvm::StringRef::npos, llvm::StringRef(ostr.str()).find("--- |"));
1176 Input yin(intermediate);
1177 std::vector<MultilineStringType> documents2;
1178 yin >> documents2;
1180 EXPECT_FALSE(yin.error());
1181 EXPECT_EQ(documents2.size(), size_t(1));
1182 EXPECT_EQ(documents2[0].str, "Hello\nWorld\n");
1186 TEST(YAMLIO, TestReadWriteBlockScalarValue) {
1187 std::string intermediate;
1189 MultilineStringType doc;
1190 doc.str = "Just a block\nscalar doc";
1192 llvm::raw_string_ostream ostr(intermediate);
1193 Output yout(ostr);
1194 yout << doc;
1197 Input yin(intermediate);
1198 MultilineStringType doc;
1199 yin >> doc;
1201 EXPECT_FALSE(yin.error());
1202 EXPECT_EQ(doc.str, "Just a block\nscalar doc\n");
1206 //===----------------------------------------------------------------------===//
1207 // Test flow sequences
1208 //===----------------------------------------------------------------------===//
1210 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
1211 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
1212 LLVM_YAML_STRONG_TYPEDEF(llvm::StringRef, MyString)
1213 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyString)
1215 namespace llvm {
1216 namespace yaml {
1217 template<>
1218 struct ScalarTraits<MyNumber> {
1219 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
1220 out << value;
1223 static StringRef input(StringRef scalar, void *, MyNumber &value) {
1224 long long n;
1225 if ( getAsSignedInteger(scalar, 0, n) )
1226 return "invalid number";
1227 value = n;
1228 return StringRef();
1231 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1234 template <> struct ScalarTraits<MyString> {
1235 using Impl = ScalarTraits<StringRef>;
1236 static void output(const MyString &V, void *Ctx, raw_ostream &OS) {
1237 Impl::output(V, Ctx, OS);
1239 static StringRef input(StringRef S, void *Ctx, MyString &V) {
1240 return Impl::input(S, Ctx, V.value);
1242 static QuotingType mustQuote(StringRef S) {
1243 return Impl::mustQuote(S);
1249 struct NameAndNumbers {
1250 llvm::StringRef name;
1251 std::vector<MyString> strings;
1252 std::vector<MyNumber> single;
1253 std::vector<MyNumber> numbers;
1256 namespace llvm {
1257 namespace yaml {
1258 template <>
1259 struct MappingTraits<NameAndNumbers> {
1260 static void mapping(IO &io, NameAndNumbers& nn) {
1261 io.mapRequired("name", nn.name);
1262 io.mapRequired("strings", nn.strings);
1263 io.mapRequired("single", nn.single);
1264 io.mapRequired("numbers", nn.numbers);
1270 typedef std::vector<MyNumber> MyNumberFlowSequence;
1272 LLVM_YAML_IS_SEQUENCE_VECTOR(MyNumberFlowSequence)
1274 struct NameAndNumbersFlow {
1275 llvm::StringRef name;
1276 std::vector<MyNumberFlowSequence> sequenceOfNumbers;
1279 namespace llvm {
1280 namespace yaml {
1281 template <>
1282 struct MappingTraits<NameAndNumbersFlow> {
1283 static void mapping(IO &io, NameAndNumbersFlow& nn) {
1284 io.mapRequired("name", nn.name);
1285 io.mapRequired("sequenceOfNumbers", nn.sequenceOfNumbers);
1292 // Test writing then reading back custom values
1294 TEST(YAMLIO, TestReadWriteMyFlowSequence) {
1295 std::string intermediate;
1297 NameAndNumbers map;
1298 map.name = "hello";
1299 map.strings.push_back(llvm::StringRef("one"));
1300 map.strings.push_back(llvm::StringRef("two"));
1301 map.single.push_back(1);
1302 map.numbers.push_back(10);
1303 map.numbers.push_back(-30);
1304 map.numbers.push_back(1024);
1306 llvm::raw_string_ostream ostr(intermediate);
1307 Output yout(ostr);
1308 yout << map;
1310 // Verify sequences were written in flow style
1311 ostr.flush();
1312 llvm::StringRef flowOut(intermediate);
1313 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
1314 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
1318 Input yin(intermediate);
1319 NameAndNumbers map2;
1320 yin >> map2;
1322 EXPECT_FALSE(yin.error());
1323 EXPECT_TRUE(map2.name.equals("hello"));
1324 EXPECT_EQ(map2.strings.size(), 2UL);
1325 EXPECT_TRUE(map2.strings[0].value.equals("one"));
1326 EXPECT_TRUE(map2.strings[1].value.equals("two"));
1327 EXPECT_EQ(map2.single.size(), 1UL);
1328 EXPECT_EQ(1, map2.single[0]);
1329 EXPECT_EQ(map2.numbers.size(), 3UL);
1330 EXPECT_EQ(10, map2.numbers[0]);
1331 EXPECT_EQ(-30, map2.numbers[1]);
1332 EXPECT_EQ(1024, map2.numbers[2]);
1338 // Test writing then reading back a sequence of flow sequences.
1340 TEST(YAMLIO, TestReadWriteSequenceOfMyFlowSequence) {
1341 std::string intermediate;
1343 NameAndNumbersFlow map;
1344 map.name = "hello";
1345 MyNumberFlowSequence single = { 0 };
1346 MyNumberFlowSequence numbers = { 12, 1, -512 };
1347 map.sequenceOfNumbers.push_back(single);
1348 map.sequenceOfNumbers.push_back(numbers);
1349 map.sequenceOfNumbers.push_back(MyNumberFlowSequence());
1351 llvm::raw_string_ostream ostr(intermediate);
1352 Output yout(ostr);
1353 yout << map;
1355 // Verify sequences were written in flow style
1356 // and that the parent sequence used '-'.
1357 ostr.flush();
1358 llvm::StringRef flowOut(intermediate);
1359 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 0 ]"));
1360 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 12, 1, -512 ]"));
1361 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ ]"));
1365 Input yin(intermediate);
1366 NameAndNumbersFlow map2;
1367 yin >> map2;
1369 EXPECT_FALSE(yin.error());
1370 EXPECT_TRUE(map2.name.equals("hello"));
1371 EXPECT_EQ(map2.sequenceOfNumbers.size(), 3UL);
1372 EXPECT_EQ(map2.sequenceOfNumbers[0].size(), 1UL);
1373 EXPECT_EQ(0, map2.sequenceOfNumbers[0][0]);
1374 EXPECT_EQ(map2.sequenceOfNumbers[1].size(), 3UL);
1375 EXPECT_EQ(12, map2.sequenceOfNumbers[1][0]);
1376 EXPECT_EQ(1, map2.sequenceOfNumbers[1][1]);
1377 EXPECT_EQ(-512, map2.sequenceOfNumbers[1][2]);
1378 EXPECT_TRUE(map2.sequenceOfNumbers[2].empty());
1382 //===----------------------------------------------------------------------===//
1383 // Test normalizing/denormalizing
1384 //===----------------------------------------------------------------------===//
1386 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
1388 typedef std::vector<TotalSeconds> SecondsSequence;
1390 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
1393 namespace llvm {
1394 namespace yaml {
1395 template <>
1396 struct MappingTraits<TotalSeconds> {
1398 class NormalizedSeconds {
1399 public:
1400 NormalizedSeconds(IO &io)
1401 : hours(0), minutes(0), seconds(0) {
1403 NormalizedSeconds(IO &, TotalSeconds &secs)
1404 : hours(secs/3600),
1405 minutes((secs - (hours*3600))/60),
1406 seconds(secs % 60) {
1408 TotalSeconds denormalize(IO &) {
1409 return TotalSeconds(hours*3600 + minutes*60 + seconds);
1412 uint32_t hours;
1413 uint8_t minutes;
1414 uint8_t seconds;
1417 static void mapping(IO &io, TotalSeconds &secs) {
1418 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
1420 io.mapOptional("hours", keys->hours, 0);
1421 io.mapOptional("minutes", keys->minutes, 0);
1422 io.mapRequired("seconds", keys->seconds);
1430 // Test the reading of a yaml sequence of mappings
1432 TEST(YAMLIO, TestReadMySecondsSequence) {
1433 SecondsSequence seq;
1434 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
1435 yin >> seq;
1437 EXPECT_FALSE(yin.error());
1438 EXPECT_EQ(seq.size(), 2UL);
1439 EXPECT_EQ(seq[0], 3605U);
1440 EXPECT_EQ(seq[1], 59U);
1445 // Test writing then reading back custom values
1447 TEST(YAMLIO, TestReadWriteMySecondsSequence) {
1448 std::string intermediate;
1450 SecondsSequence seq;
1451 seq.push_back(4000);
1452 seq.push_back(500);
1453 seq.push_back(59);
1455 llvm::raw_string_ostream ostr(intermediate);
1456 Output yout(ostr);
1457 yout << seq;
1460 Input yin(intermediate);
1461 SecondsSequence seq2;
1462 yin >> seq2;
1464 EXPECT_FALSE(yin.error());
1465 EXPECT_EQ(seq2.size(), 3UL);
1466 EXPECT_EQ(seq2[0], 4000U);
1467 EXPECT_EQ(seq2[1], 500U);
1468 EXPECT_EQ(seq2[2], 59U);
1473 //===----------------------------------------------------------------------===//
1474 // Test dynamic typing
1475 //===----------------------------------------------------------------------===//
1477 enum AFlags {
1483 enum BFlags {
1489 enum Kind {
1490 kindA,
1491 kindB
1494 struct KindAndFlags {
1495 KindAndFlags() : kind(kindA), flags(0) { }
1496 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
1497 Kind kind;
1498 uint32_t flags;
1501 typedef std::vector<KindAndFlags> KindAndFlagsSequence;
1503 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
1505 namespace llvm {
1506 namespace yaml {
1507 template <>
1508 struct ScalarEnumerationTraits<AFlags> {
1509 static void enumeration(IO &io, AFlags &value) {
1510 io.enumCase(value, "a1", a1);
1511 io.enumCase(value, "a2", a2);
1512 io.enumCase(value, "a3", a3);
1515 template <>
1516 struct ScalarEnumerationTraits<BFlags> {
1517 static void enumeration(IO &io, BFlags &value) {
1518 io.enumCase(value, "b1", b1);
1519 io.enumCase(value, "b2", b2);
1520 io.enumCase(value, "b3", b3);
1523 template <>
1524 struct ScalarEnumerationTraits<Kind> {
1525 static void enumeration(IO &io, Kind &value) {
1526 io.enumCase(value, "A", kindA);
1527 io.enumCase(value, "B", kindB);
1530 template <>
1531 struct MappingTraits<KindAndFlags> {
1532 static void mapping(IO &io, KindAndFlags& kf) {
1533 io.mapRequired("kind", kf.kind);
1534 // Type of "flags" field varies depending on "kind" field.
1535 // Use memcpy here to avoid breaking strict aliasing rules.
1536 if (kf.kind == kindA) {
1537 AFlags aflags = static_cast<AFlags>(kf.flags);
1538 io.mapRequired("flags", aflags);
1539 kf.flags = aflags;
1540 } else {
1541 BFlags bflags = static_cast<BFlags>(kf.flags);
1542 io.mapRequired("flags", bflags);
1543 kf.flags = bflags;
1552 // Test the reading of a yaml sequence dynamic types
1554 TEST(YAMLIO, TestReadKindAndFlagsSequence) {
1555 KindAndFlagsSequence seq;
1556 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
1557 yin >> seq;
1559 EXPECT_FALSE(yin.error());
1560 EXPECT_EQ(seq.size(), 2UL);
1561 EXPECT_EQ(seq[0].kind, kindA);
1562 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
1563 EXPECT_EQ(seq[1].kind, kindB);
1564 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
1568 // Test writing then reading back dynamic types
1570 TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
1571 std::string intermediate;
1573 KindAndFlagsSequence seq;
1574 seq.push_back(KindAndFlags(kindA,a1));
1575 seq.push_back(KindAndFlags(kindB,b1));
1576 seq.push_back(KindAndFlags(kindA,a2));
1577 seq.push_back(KindAndFlags(kindB,b2));
1578 seq.push_back(KindAndFlags(kindA,a3));
1580 llvm::raw_string_ostream ostr(intermediate);
1581 Output yout(ostr);
1582 yout << seq;
1585 Input yin(intermediate);
1586 KindAndFlagsSequence seq2;
1587 yin >> seq2;
1589 EXPECT_FALSE(yin.error());
1590 EXPECT_EQ(seq2.size(), 5UL);
1591 EXPECT_EQ(seq2[0].kind, kindA);
1592 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
1593 EXPECT_EQ(seq2[1].kind, kindB);
1594 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
1595 EXPECT_EQ(seq2[2].kind, kindA);
1596 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
1597 EXPECT_EQ(seq2[3].kind, kindB);
1598 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
1599 EXPECT_EQ(seq2[4].kind, kindA);
1600 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
1605 //===----------------------------------------------------------------------===//
1606 // Test document list
1607 //===----------------------------------------------------------------------===//
1609 struct FooBarMap {
1610 int foo;
1611 int bar;
1613 typedef std::vector<FooBarMap> FooBarMapDocumentList;
1615 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
1618 namespace llvm {
1619 namespace yaml {
1620 template <>
1621 struct MappingTraits<FooBarMap> {
1622 static void mapping(IO &io, FooBarMap& fb) {
1623 io.mapRequired("foo", fb.foo);
1624 io.mapRequired("bar", fb.bar);
1632 // Test the reading of a yaml mapping
1634 TEST(YAMLIO, TestDocRead) {
1635 FooBarMap doc;
1636 Input yin("---\nfoo: 3\nbar: 5\n...\n");
1637 yin >> doc;
1639 EXPECT_FALSE(yin.error());
1640 EXPECT_EQ(doc.foo, 3);
1641 EXPECT_EQ(doc.bar,5);
1647 // Test writing then reading back a sequence of mappings
1649 TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
1650 std::string intermediate;
1652 FooBarMap doc1;
1653 doc1.foo = 10;
1654 doc1.bar = -3;
1655 FooBarMap doc2;
1656 doc2.foo = 257;
1657 doc2.bar = 0;
1658 std::vector<FooBarMap> docList;
1659 docList.push_back(doc1);
1660 docList.push_back(doc2);
1662 llvm::raw_string_ostream ostr(intermediate);
1663 Output yout(ostr);
1664 yout << docList;
1669 Input yin(intermediate);
1670 std::vector<FooBarMap> docList2;
1671 yin >> docList2;
1673 EXPECT_FALSE(yin.error());
1674 EXPECT_EQ(docList2.size(), 2UL);
1675 FooBarMap& map1 = docList2[0];
1676 FooBarMap& map2 = docList2[1];
1677 EXPECT_EQ(map1.foo, 10);
1678 EXPECT_EQ(map1.bar, -3);
1679 EXPECT_EQ(map2.foo, 257);
1680 EXPECT_EQ(map2.bar, 0);
1684 //===----------------------------------------------------------------------===//
1685 // Test document tags
1686 //===----------------------------------------------------------------------===//
1688 struct MyDouble {
1689 MyDouble() : value(0.0) { }
1690 MyDouble(double x) : value(x) { }
1691 double value;
1694 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble)
1697 namespace llvm {
1698 namespace yaml {
1699 template <>
1700 struct MappingTraits<MyDouble> {
1701 static void mapping(IO &io, MyDouble &d) {
1702 if (io.mapTag("!decimal", true)) {
1703 mappingDecimal(io, d);
1704 } else if (io.mapTag("!fraction")) {
1705 mappingFraction(io, d);
1708 static void mappingDecimal(IO &io, MyDouble &d) {
1709 io.mapRequired("value", d.value);
1711 static void mappingFraction(IO &io, MyDouble &d) {
1712 double num, denom;
1713 io.mapRequired("numerator", num);
1714 io.mapRequired("denominator", denom);
1715 // convert fraction to double
1716 d.value = num/denom;
1724 // Test the reading of two different tagged yaml documents.
1726 TEST(YAMLIO, TestTaggedDocuments) {
1727 std::vector<MyDouble> docList;
1728 Input yin("--- !decimal\nvalue: 3.0\n"
1729 "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
1730 yin >> docList;
1731 EXPECT_FALSE(yin.error());
1732 EXPECT_EQ(docList.size(), 2UL);
1733 EXPECT_EQ(docList[0].value, 3.0);
1734 EXPECT_EQ(docList[1].value, 4.5);
1740 // Test writing then reading back tagged documents
1742 TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
1743 std::string intermediate;
1745 MyDouble a(10.25);
1746 MyDouble b(-3.75);
1747 std::vector<MyDouble> docList;
1748 docList.push_back(a);
1749 docList.push_back(b);
1751 llvm::raw_string_ostream ostr(intermediate);
1752 Output yout(ostr);
1753 yout << docList;
1757 Input yin(intermediate);
1758 std::vector<MyDouble> docList2;
1759 yin >> docList2;
1761 EXPECT_FALSE(yin.error());
1762 EXPECT_EQ(docList2.size(), 2UL);
1763 EXPECT_EQ(docList2[0].value, 10.25);
1764 EXPECT_EQ(docList2[1].value, -3.75);
1769 //===----------------------------------------------------------------------===//
1770 // Test mapping validation
1771 //===----------------------------------------------------------------------===//
1773 struct MyValidation {
1774 double value;
1777 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation)
1779 namespace llvm {
1780 namespace yaml {
1781 template <>
1782 struct MappingTraits<MyValidation> {
1783 static void mapping(IO &io, MyValidation &d) {
1784 io.mapRequired("value", d.value);
1786 static StringRef validate(IO &io, MyValidation &d) {
1787 if (d.value < 0)
1788 return "negative value";
1789 return StringRef();
1797 // Test that validate() is called and complains about the negative value.
1799 TEST(YAMLIO, TestValidatingInput) {
1800 std::vector<MyValidation> docList;
1801 Input yin("--- \nvalue: 3.0\n"
1802 "--- \nvalue: -1.0\n...\n",
1803 nullptr, suppressErrorMessages);
1804 yin >> docList;
1805 EXPECT_TRUE(!!yin.error());
1808 //===----------------------------------------------------------------------===//
1809 // Test flow mapping
1810 //===----------------------------------------------------------------------===//
1812 struct FlowFooBar {
1813 int foo;
1814 int bar;
1816 FlowFooBar() : foo(0), bar(0) {}
1817 FlowFooBar(int foo, int bar) : foo(foo), bar(bar) {}
1820 typedef std::vector<FlowFooBar> FlowFooBarSequence;
1822 LLVM_YAML_IS_SEQUENCE_VECTOR(FlowFooBar)
1824 struct FlowFooBarDoc {
1825 FlowFooBar attribute;
1826 FlowFooBarSequence seq;
1829 namespace llvm {
1830 namespace yaml {
1831 template <>
1832 struct MappingTraits<FlowFooBar> {
1833 static void mapping(IO &io, FlowFooBar &fb) {
1834 io.mapRequired("foo", fb.foo);
1835 io.mapRequired("bar", fb.bar);
1838 static const bool flow = true;
1841 template <>
1842 struct MappingTraits<FlowFooBarDoc> {
1843 static void mapping(IO &io, FlowFooBarDoc &fb) {
1844 io.mapRequired("attribute", fb.attribute);
1845 io.mapRequired("seq", fb.seq);
1852 // Test writing then reading back custom mappings
1854 TEST(YAMLIO, TestReadWriteMyFlowMapping) {
1855 std::string intermediate;
1857 FlowFooBarDoc doc;
1858 doc.attribute = FlowFooBar(42, 907);
1859 doc.seq.push_back(FlowFooBar(1, 2));
1860 doc.seq.push_back(FlowFooBar(0, 0));
1861 doc.seq.push_back(FlowFooBar(-1, 1024));
1863 llvm::raw_string_ostream ostr(intermediate);
1864 Output yout(ostr);
1865 yout << doc;
1867 // Verify that mappings were written in flow style
1868 ostr.flush();
1869 llvm::StringRef flowOut(intermediate);
1870 EXPECT_NE(llvm::StringRef::npos, flowOut.find("{ foo: 42, bar: 907 }"));
1871 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 1, bar: 2 }"));
1872 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 0, bar: 0 }"));
1873 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: -1, bar: 1024 }"));
1877 Input yin(intermediate);
1878 FlowFooBarDoc doc2;
1879 yin >> doc2;
1881 EXPECT_FALSE(yin.error());
1882 EXPECT_EQ(doc2.attribute.foo, 42);
1883 EXPECT_EQ(doc2.attribute.bar, 907);
1884 EXPECT_EQ(doc2.seq.size(), 3UL);
1885 EXPECT_EQ(doc2.seq[0].foo, 1);
1886 EXPECT_EQ(doc2.seq[0].bar, 2);
1887 EXPECT_EQ(doc2.seq[1].foo, 0);
1888 EXPECT_EQ(doc2.seq[1].bar, 0);
1889 EXPECT_EQ(doc2.seq[2].foo, -1);
1890 EXPECT_EQ(doc2.seq[2].bar, 1024);
1894 //===----------------------------------------------------------------------===//
1895 // Test error handling
1896 //===----------------------------------------------------------------------===//
1899 // Test error handling of unknown enumerated scalar
1901 TEST(YAMLIO, TestColorsReadError) {
1902 ColorMap map;
1903 Input yin("---\n"
1904 "c1: blue\n"
1905 "c2: purple\n"
1906 "c3: green\n"
1907 "...\n",
1908 /*Ctxt=*/nullptr,
1909 suppressErrorMessages);
1910 yin >> map;
1911 EXPECT_TRUE(!!yin.error());
1916 // Test error handling of flow sequence with unknown value
1918 TEST(YAMLIO, TestFlagsReadError) {
1919 FlagsMap map;
1920 Input yin("---\n"
1921 "f1: [ big ]\n"
1922 "f2: [ round, hollow ]\n"
1923 "f3: []\n"
1924 "...\n",
1925 /*Ctxt=*/nullptr,
1926 suppressErrorMessages);
1927 yin >> map;
1929 EXPECT_TRUE(!!yin.error());
1934 // Test error handling reading built-in uint8_t type
1936 TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1937 std::vector<uint8_t> seq;
1938 Input yin("---\n"
1939 "- 255\n"
1940 "- 0\n"
1941 "- 257\n"
1942 "...\n",
1943 /*Ctxt=*/nullptr,
1944 suppressErrorMessages);
1945 yin >> seq;
1947 EXPECT_TRUE(!!yin.error());
1952 // Test error handling reading built-in uint16_t type
1954 TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1955 std::vector<uint16_t> seq;
1956 Input yin("---\n"
1957 "- 65535\n"
1958 "- 0\n"
1959 "- 66000\n"
1960 "...\n",
1961 /*Ctxt=*/nullptr,
1962 suppressErrorMessages);
1963 yin >> seq;
1965 EXPECT_TRUE(!!yin.error());
1970 // Test error handling reading built-in uint32_t type
1972 TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1973 std::vector<uint32_t> seq;
1974 Input yin("---\n"
1975 "- 4000000000\n"
1976 "- 0\n"
1977 "- 5000000000\n"
1978 "...\n",
1979 /*Ctxt=*/nullptr,
1980 suppressErrorMessages);
1981 yin >> seq;
1983 EXPECT_TRUE(!!yin.error());
1988 // Test error handling reading built-in uint64_t type
1990 TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1991 std::vector<uint64_t> seq;
1992 Input yin("---\n"
1993 "- 18446744073709551615\n"
1994 "- 0\n"
1995 "- 19446744073709551615\n"
1996 "...\n",
1997 /*Ctxt=*/nullptr,
1998 suppressErrorMessages);
1999 yin >> seq;
2001 EXPECT_TRUE(!!yin.error());
2006 // Test error handling reading built-in int8_t type
2008 TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
2009 std::vector<int8_t> seq;
2010 Input yin("---\n"
2011 "- -128\n"
2012 "- 0\n"
2013 "- 127\n"
2014 "- 128\n"
2015 "...\n",
2016 /*Ctxt=*/nullptr,
2017 suppressErrorMessages);
2018 yin >> seq;
2020 EXPECT_TRUE(!!yin.error());
2024 // Test error handling reading built-in int8_t type
2026 TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
2027 std::vector<int8_t> seq;
2028 Input yin("---\n"
2029 "- -128\n"
2030 "- 0\n"
2031 "- 127\n"
2032 "- -129\n"
2033 "...\n",
2034 /*Ctxt=*/nullptr,
2035 suppressErrorMessages);
2036 yin >> seq;
2038 EXPECT_TRUE(!!yin.error());
2043 // Test error handling reading built-in int16_t type
2045 TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
2046 std::vector<int16_t> seq;
2047 Input yin("---\n"
2048 "- 32767\n"
2049 "- 0\n"
2050 "- -32768\n"
2051 "- -32769\n"
2052 "...\n",
2053 /*Ctxt=*/nullptr,
2054 suppressErrorMessages);
2055 yin >> seq;
2057 EXPECT_TRUE(!!yin.error());
2062 // Test error handling reading built-in int16_t type
2064 TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
2065 std::vector<int16_t> seq;
2066 Input yin("---\n"
2067 "- 32767\n"
2068 "- 0\n"
2069 "- -32768\n"
2070 "- 32768\n"
2071 "...\n",
2072 /*Ctxt=*/nullptr,
2073 suppressErrorMessages);
2074 yin >> seq;
2076 EXPECT_TRUE(!!yin.error());
2081 // Test error handling reading built-in int32_t type
2083 TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
2084 std::vector<int32_t> seq;
2085 Input yin("---\n"
2086 "- 2147483647\n"
2087 "- 0\n"
2088 "- -2147483648\n"
2089 "- -2147483649\n"
2090 "...\n",
2091 /*Ctxt=*/nullptr,
2092 suppressErrorMessages);
2093 yin >> seq;
2095 EXPECT_TRUE(!!yin.error());
2099 // Test error handling reading built-in int32_t type
2101 TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
2102 std::vector<int32_t> seq;
2103 Input yin("---\n"
2104 "- 2147483647\n"
2105 "- 0\n"
2106 "- -2147483648\n"
2107 "- 2147483649\n"
2108 "...\n",
2109 /*Ctxt=*/nullptr,
2110 suppressErrorMessages);
2111 yin >> seq;
2113 EXPECT_TRUE(!!yin.error());
2118 // Test error handling reading built-in int64_t type
2120 TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
2121 std::vector<int64_t> seq;
2122 Input yin("---\n"
2123 "- -9223372036854775808\n"
2124 "- 0\n"
2125 "- 9223372036854775807\n"
2126 "- -9223372036854775809\n"
2127 "...\n",
2128 /*Ctxt=*/nullptr,
2129 suppressErrorMessages);
2130 yin >> seq;
2132 EXPECT_TRUE(!!yin.error());
2136 // Test error handling reading built-in int64_t type
2138 TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
2139 std::vector<int64_t> seq;
2140 Input yin("---\n"
2141 "- -9223372036854775808\n"
2142 "- 0\n"
2143 "- 9223372036854775807\n"
2144 "- 9223372036854775809\n"
2145 "...\n",
2146 /*Ctxt=*/nullptr,
2147 suppressErrorMessages);
2148 yin >> seq;
2150 EXPECT_TRUE(!!yin.error());
2154 // Test error handling reading built-in float type
2156 TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
2157 std::vector<float> seq;
2158 Input yin("---\n"
2159 "- 0.0\n"
2160 "- 1000.1\n"
2161 "- -123.456\n"
2162 "- 1.2.3\n"
2163 "...\n",
2164 /*Ctxt=*/nullptr,
2165 suppressErrorMessages);
2166 yin >> seq;
2168 EXPECT_TRUE(!!yin.error());
2172 // Test error handling reading built-in float type
2174 TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
2175 std::vector<double> seq;
2176 Input yin("---\n"
2177 "- 0.0\n"
2178 "- 1000.1\n"
2179 "- -123.456\n"
2180 "- 1.2.3\n"
2181 "...\n",
2182 /*Ctxt=*/nullptr,
2183 suppressErrorMessages);
2184 yin >> seq;
2186 EXPECT_TRUE(!!yin.error());
2190 // Test error handling reading built-in Hex8 type
2192 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
2193 TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
2194 std::vector<Hex8> seq;
2195 Input yin("---\n"
2196 "- 0x12\n"
2197 "- 0xFE\n"
2198 "- 0x123\n"
2199 "...\n",
2200 /*Ctxt=*/nullptr,
2201 suppressErrorMessages);
2202 yin >> seq;
2204 EXPECT_TRUE(!!yin.error());
2209 // Test error handling reading built-in Hex16 type
2211 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
2212 TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
2213 std::vector<Hex16> seq;
2214 Input yin("---\n"
2215 "- 0x0012\n"
2216 "- 0xFEFF\n"
2217 "- 0x12345\n"
2218 "...\n",
2219 /*Ctxt=*/nullptr,
2220 suppressErrorMessages);
2221 yin >> seq;
2223 EXPECT_TRUE(!!yin.error());
2227 // Test error handling reading built-in Hex32 type
2229 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
2230 TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
2231 std::vector<Hex32> seq;
2232 Input yin("---\n"
2233 "- 0x0012\n"
2234 "- 0xFEFF0000\n"
2235 "- 0x1234556789\n"
2236 "...\n",
2237 /*Ctxt=*/nullptr,
2238 suppressErrorMessages);
2239 yin >> seq;
2241 EXPECT_TRUE(!!yin.error());
2245 // Test error handling reading built-in Hex64 type
2247 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
2248 TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
2249 std::vector<Hex64> seq;
2250 Input yin("---\n"
2251 "- 0x0012\n"
2252 "- 0xFFEEDDCCBBAA9988\n"
2253 "- 0x12345567890ABCDEF0\n"
2254 "...\n",
2255 /*Ctxt=*/nullptr,
2256 suppressErrorMessages);
2257 yin >> seq;
2259 EXPECT_TRUE(!!yin.error());
2262 TEST(YAMLIO, TestMalformedMapFailsGracefully) {
2263 FooBar doc;
2265 // We pass the suppressErrorMessages handler to handle the error
2266 // message generated in the constructor of Input.
2267 Input yin("{foo:3, bar: 5}", /*Ctxt=*/nullptr, suppressErrorMessages);
2268 yin >> doc;
2269 EXPECT_TRUE(!!yin.error());
2273 Input yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/nullptr, suppressErrorMessages);
2274 yin >> doc;
2275 EXPECT_TRUE(!!yin.error());
2279 struct OptionalTest {
2280 std::vector<int> Numbers;
2283 struct OptionalTestSeq {
2284 std::vector<OptionalTest> Tests;
2287 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
2288 namespace llvm {
2289 namespace yaml {
2290 template <>
2291 struct MappingTraits<OptionalTest> {
2292 static void mapping(IO& IO, OptionalTest &OT) {
2293 IO.mapOptional("Numbers", OT.Numbers);
2297 template <>
2298 struct MappingTraits<OptionalTestSeq> {
2299 static void mapping(IO &IO, OptionalTestSeq &OTS) {
2300 IO.mapOptional("Tests", OTS.Tests);
2306 TEST(YAMLIO, SequenceElideTest) {
2307 // Test that writing out a purely optional structure with its fields set to
2308 // default followed by other data is properly read back in.
2309 OptionalTestSeq Seq;
2310 OptionalTest One, Two, Three, Four;
2311 int N[] = {1, 2, 3};
2312 Three.Numbers.assign(N, N + 3);
2313 Seq.Tests.push_back(One);
2314 Seq.Tests.push_back(Two);
2315 Seq.Tests.push_back(Three);
2316 Seq.Tests.push_back(Four);
2318 std::string intermediate;
2320 llvm::raw_string_ostream ostr(intermediate);
2321 Output yout(ostr);
2322 yout << Seq;
2325 Input yin(intermediate);
2326 OptionalTestSeq Seq2;
2327 yin >> Seq2;
2329 EXPECT_FALSE(yin.error());
2331 EXPECT_EQ(4UL, Seq2.Tests.size());
2333 EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
2334 EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
2336 EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
2337 EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
2338 EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
2340 EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());
2343 TEST(YAMLIO, TestEmptyStringFailsForMapWithRequiredFields) {
2344 FooBar doc;
2345 Input yin("");
2346 yin >> doc;
2347 EXPECT_TRUE(!!yin.error());
2350 TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) {
2351 OptionalTest doc;
2352 Input yin("");
2353 yin >> doc;
2354 EXPECT_FALSE(yin.error());
2357 TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {
2358 std::vector<uint8_t> seq;
2359 Input yin("", /*Ctxt=*/nullptr, suppressErrorMessages);
2360 yin >> seq;
2362 EXPECT_FALSE(yin.error());
2363 EXPECT_TRUE(seq.empty());
2366 struct FlowMap {
2367 llvm::StringRef str1, str2, str3;
2368 FlowMap(llvm::StringRef str1, llvm::StringRef str2, llvm::StringRef str3)
2369 : str1(str1), str2(str2), str3(str3) {}
2372 struct FlowSeq {
2373 llvm::StringRef str;
2374 FlowSeq(llvm::StringRef S) : str(S) {}
2375 FlowSeq() = default;
2378 namespace llvm {
2379 namespace yaml {
2380 template <>
2381 struct MappingTraits<FlowMap> {
2382 static void mapping(IO &io, FlowMap &fm) {
2383 io.mapRequired("str1", fm.str1);
2384 io.mapRequired("str2", fm.str2);
2385 io.mapRequired("str3", fm.str3);
2388 static const bool flow = true;
2391 template <>
2392 struct ScalarTraits<FlowSeq> {
2393 static void output(const FlowSeq &value, void*, llvm::raw_ostream &out) {
2394 out << value.str;
2396 static StringRef input(StringRef scalar, void*, FlowSeq &value) {
2397 value.str = scalar;
2398 return "";
2401 static QuotingType mustQuote(StringRef S) { return QuotingType::None; }
2406 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FlowSeq)
2408 TEST(YAMLIO, TestWrapFlow) {
2409 std::string out;
2410 llvm::raw_string_ostream ostr(out);
2411 FlowMap Map("This is str1", "This is str2", "This is str3");
2412 std::vector<FlowSeq> Seq;
2413 Seq.emplace_back("This is str1");
2414 Seq.emplace_back("This is str2");
2415 Seq.emplace_back("This is str3");
2418 // 20 is just bellow the total length of the first mapping field.
2419 // We should wreap at every element.
2420 Output yout(ostr, nullptr, 15);
2422 yout << Map;
2423 ostr.flush();
2424 EXPECT_EQ(out,
2425 "---\n"
2426 "{ str1: This is str1, \n"
2427 " str2: This is str2, \n"
2428 " str3: This is str3 }\n"
2429 "...\n");
2430 out.clear();
2432 yout << Seq;
2433 ostr.flush();
2434 EXPECT_EQ(out,
2435 "---\n"
2436 "[ This is str1, \n"
2437 " This is str2, \n"
2438 " This is str3 ]\n"
2439 "...\n");
2440 out.clear();
2443 // 25 will allow the second field to be output on the first line.
2444 Output yout(ostr, nullptr, 25);
2446 yout << Map;
2447 ostr.flush();
2448 EXPECT_EQ(out,
2449 "---\n"
2450 "{ str1: This is str1, str2: This is str2, \n"
2451 " str3: This is str3 }\n"
2452 "...\n");
2453 out.clear();
2455 yout << Seq;
2456 ostr.flush();
2457 EXPECT_EQ(out,
2458 "---\n"
2459 "[ This is str1, This is str2, \n"
2460 " This is str3 ]\n"
2461 "...\n");
2462 out.clear();
2465 // 0 means no wrapping.
2466 Output yout(ostr, nullptr, 0);
2468 yout << Map;
2469 ostr.flush();
2470 EXPECT_EQ(out,
2471 "---\n"
2472 "{ str1: This is str1, str2: This is str2, str3: This is str3 }\n"
2473 "...\n");
2474 out.clear();
2476 yout << Seq;
2477 ostr.flush();
2478 EXPECT_EQ(out,
2479 "---\n"
2480 "[ This is str1, This is str2, This is str3 ]\n"
2481 "...\n");
2482 out.clear();
2486 struct MappingContext {
2487 int A = 0;
2489 struct SimpleMap {
2490 int B = 0;
2491 int C = 0;
2494 struct NestedMap {
2495 NestedMap(MappingContext &Context) : Context(Context) {}
2496 SimpleMap Simple;
2497 MappingContext &Context;
2500 namespace llvm {
2501 namespace yaml {
2502 template <> struct MappingContextTraits<SimpleMap, MappingContext> {
2503 static void mapping(IO &io, SimpleMap &sm, MappingContext &Context) {
2504 io.mapRequired("B", sm.B);
2505 io.mapRequired("C", sm.C);
2506 ++Context.A;
2507 io.mapRequired("Context", Context.A);
2511 template <> struct MappingTraits<NestedMap> {
2512 static void mapping(IO &io, NestedMap &nm) {
2513 io.mapRequired("Simple", nm.Simple, nm.Context);
2519 TEST(YAMLIO, TestMapWithContext) {
2520 MappingContext Context;
2521 NestedMap Nested(Context);
2522 std::string out;
2523 llvm::raw_string_ostream ostr(out);
2525 Output yout(ostr, nullptr, 15);
2527 yout << Nested;
2528 ostr.flush();
2529 EXPECT_EQ(1, Context.A);
2530 EXPECT_EQ("---\n"
2531 "Simple:\n"
2532 " B: 0\n"
2533 " C: 0\n"
2534 " Context: 1\n"
2535 "...\n",
2536 out);
2538 out.clear();
2540 Nested.Simple.B = 2;
2541 Nested.Simple.C = 3;
2542 yout << Nested;
2543 ostr.flush();
2544 EXPECT_EQ(2, Context.A);
2545 EXPECT_EQ("---\n"
2546 "Simple:\n"
2547 " B: 2\n"
2548 " C: 3\n"
2549 " Context: 2\n"
2550 "...\n",
2551 out);
2552 out.clear();
2555 LLVM_YAML_IS_STRING_MAP(int)
2557 TEST(YAMLIO, TestCustomMapping) {
2558 std::map<std::string, int> x;
2560 std::string out;
2561 llvm::raw_string_ostream ostr(out);
2562 Output xout(ostr, nullptr, 0);
2564 xout << x;
2565 ostr.flush();
2566 EXPECT_EQ("---\n"
2567 "{}\n"
2568 "...\n",
2569 out);
2571 x["foo"] = 1;
2572 x["bar"] = 2;
2574 out.clear();
2575 xout << x;
2576 ostr.flush();
2577 EXPECT_EQ("---\n"
2578 "bar: 2\n"
2579 "foo: 1\n"
2580 "...\n",
2581 out);
2583 Input yin(out);
2584 std::map<std::string, int> y;
2585 yin >> y;
2586 EXPECT_EQ(2ul, y.size());
2587 EXPECT_EQ(1, y["foo"]);
2588 EXPECT_EQ(2, y["bar"]);
2591 LLVM_YAML_IS_STRING_MAP(FooBar)
2593 TEST(YAMLIO, TestCustomMappingStruct) {
2594 std::map<std::string, FooBar> x;
2595 x["foo"].foo = 1;
2596 x["foo"].bar = 2;
2597 x["bar"].foo = 3;
2598 x["bar"].bar = 4;
2600 std::string out;
2601 llvm::raw_string_ostream ostr(out);
2602 Output xout(ostr, nullptr, 0);
2604 xout << x;
2605 ostr.flush();
2606 EXPECT_EQ("---\n"
2607 "bar:\n"
2608 " foo: 3\n"
2609 " bar: 4\n"
2610 "foo:\n"
2611 " foo: 1\n"
2612 " bar: 2\n"
2613 "...\n",
2614 out);
2616 Input yin(out);
2617 std::map<std::string, FooBar> y;
2618 yin >> y;
2619 EXPECT_EQ(2ul, y.size());
2620 EXPECT_EQ(1, y["foo"].foo);
2621 EXPECT_EQ(2, y["foo"].bar);
2622 EXPECT_EQ(3, y["bar"].foo);
2623 EXPECT_EQ(4, y["bar"].bar);
2626 struct FooBarMapMap {
2627 std::map<std::string, FooBar> fbm;
2630 namespace llvm {
2631 namespace yaml {
2632 template <> struct MappingTraits<FooBarMapMap> {
2633 static void mapping(IO &io, FooBarMapMap &x) {
2634 io.mapRequired("fbm", x.fbm);
2640 TEST(YAMLIO, TestEmptyMapWrite) {
2641 FooBarMapMap cont;
2642 std::string str;
2643 llvm::raw_string_ostream OS(str);
2644 Output yout(OS);
2645 yout << cont;
2646 EXPECT_EQ(OS.str(), "---\nfbm: {}\n...\n");
2649 TEST(YAMLIO, TestEmptySequenceWrite) {
2650 FooBarContainer cont;
2651 std::string str;
2652 llvm::raw_string_ostream OS(str);
2653 Output yout(OS);
2654 yout << cont;
2655 EXPECT_EQ(OS.str(), "---\nfbs: []\n...\n");
2658 static void TestEscaped(llvm::StringRef Input, llvm::StringRef Expected) {
2659 std::string out;
2660 llvm::raw_string_ostream ostr(out);
2661 Output xout(ostr, nullptr, 0);
2663 llvm::yaml::EmptyContext Ctx;
2664 yamlize(xout, Input, true, Ctx);
2666 ostr.flush();
2668 // Make a separate StringRef so we get nice byte-by-byte output.
2669 llvm::StringRef Got(out);
2670 EXPECT_EQ(Expected, Got);
2673 TEST(YAMLIO, TestEscaped) {
2674 // Single quote
2675 TestEscaped("@abc@", "'@abc@'");
2676 // No quote
2677 TestEscaped("abc", "abc");
2678 // Forward slash quoted
2679 TestEscaped("abc/", "'abc/'");
2680 // Double quote non-printable
2681 TestEscaped("\01@abc@", "\"\\x01@abc@\"");
2682 // Double quote inside single quote
2683 TestEscaped("abc\"fdf", "'abc\"fdf'");
2684 // Double quote inside double quote
2685 TestEscaped("\01bc\"fdf", "\"\\x01bc\\\"fdf\"");
2686 // Single quote inside single quote
2687 TestEscaped("abc'fdf", "'abc''fdf'");
2688 // UTF8
2689 TestEscaped("/*параметр*/", "\"/*параметр*/\"");
2690 // UTF8 with single quote inside double quote
2691 TestEscaped("parameter 'параметр' is unused",
2692 "\"parameter 'параметр' is unused\"");
2694 // String with embedded non-printable multibyte UTF-8 sequence (U+200B
2695 // zero-width space). The thing to test here is that we emit a
2696 // unicode-scalar level escape like \uNNNN (at the YAML level), and don't
2697 // just pass the UTF-8 byte sequence through as with quoted printables.
2699 const unsigned char foobar[10] = {'f', 'o', 'o',
2700 0xE2, 0x80, 0x8B, // UTF-8 of U+200B
2701 'b', 'a', 'r',
2702 0x0};
2703 TestEscaped((char const *)foobar, "\"foo\\u200Bbar\"");
2707 TEST(YAMLIO, Numeric) {
2708 EXPECT_TRUE(isNumeric(".inf"));
2709 EXPECT_TRUE(isNumeric(".INF"));
2710 EXPECT_TRUE(isNumeric(".Inf"));
2711 EXPECT_TRUE(isNumeric("-.inf"));
2712 EXPECT_TRUE(isNumeric("+.inf"));
2714 EXPECT_TRUE(isNumeric(".nan"));
2715 EXPECT_TRUE(isNumeric(".NaN"));
2716 EXPECT_TRUE(isNumeric(".NAN"));
2718 EXPECT_TRUE(isNumeric("0"));
2719 EXPECT_TRUE(isNumeric("0."));
2720 EXPECT_TRUE(isNumeric("0.0"));
2721 EXPECT_TRUE(isNumeric("-0.0"));
2722 EXPECT_TRUE(isNumeric("+0.0"));
2724 EXPECT_TRUE(isNumeric("12345"));
2725 EXPECT_TRUE(isNumeric("012345"));
2726 EXPECT_TRUE(isNumeric("+12.0"));
2727 EXPECT_TRUE(isNumeric(".5"));
2728 EXPECT_TRUE(isNumeric("+.5"));
2729 EXPECT_TRUE(isNumeric("-1.0"));
2731 EXPECT_TRUE(isNumeric("2.3e4"));
2732 EXPECT_TRUE(isNumeric("-2E+05"));
2733 EXPECT_TRUE(isNumeric("+12e03"));
2734 EXPECT_TRUE(isNumeric("6.8523015e+5"));
2736 EXPECT_TRUE(isNumeric("1.e+1"));
2737 EXPECT_TRUE(isNumeric(".0e+1"));
2739 EXPECT_TRUE(isNumeric("0x2aF3"));
2740 EXPECT_TRUE(isNumeric("0o01234567"));
2742 EXPECT_FALSE(isNumeric("not a number"));
2743 EXPECT_FALSE(isNumeric("."));
2744 EXPECT_FALSE(isNumeric(".e+1"));
2745 EXPECT_FALSE(isNumeric(".1e"));
2746 EXPECT_FALSE(isNumeric(".1e+"));
2747 EXPECT_FALSE(isNumeric(".1e++1"));
2749 EXPECT_FALSE(isNumeric("ABCD"));
2750 EXPECT_FALSE(isNumeric("+0x2AF3"));
2751 EXPECT_FALSE(isNumeric("-0x2AF3"));
2752 EXPECT_FALSE(isNumeric("0x2AF3Z"));
2753 EXPECT_FALSE(isNumeric("0o012345678"));
2754 EXPECT_FALSE(isNumeric("0xZ"));
2755 EXPECT_FALSE(isNumeric("-0o012345678"));
2756 EXPECT_FALSE(isNumeric("000003A8229434B839616A25C16B0291F77A438B"));
2758 EXPECT_FALSE(isNumeric(""));
2759 EXPECT_FALSE(isNumeric("."));
2760 EXPECT_FALSE(isNumeric(".e+1"));
2761 EXPECT_FALSE(isNumeric(".e+"));
2762 EXPECT_FALSE(isNumeric(".e"));
2763 EXPECT_FALSE(isNumeric("e1"));
2765 // Deprecated formats: as for YAML 1.2 specification, the following are not
2766 // valid numbers anymore:
2768 // * Sexagecimal numbers
2769 // * Decimal numbers with comma s the delimiter
2770 // * "inf", "nan" without '.' prefix
2771 EXPECT_FALSE(isNumeric("3:25:45"));
2772 EXPECT_FALSE(isNumeric("+12,345"));
2773 EXPECT_FALSE(isNumeric("-inf"));
2774 EXPECT_FALSE(isNumeric("1,230.15"));
2777 //===----------------------------------------------------------------------===//
2778 // Test PolymorphicTraits and TaggedScalarTraits
2779 //===----------------------------------------------------------------------===//
2781 struct Poly {
2782 enum NodeKind {
2783 NK_Scalar,
2784 NK_Seq,
2785 NK_Map,
2786 } Kind;
2788 Poly(NodeKind Kind) : Kind(Kind) {}
2790 virtual ~Poly() = default;
2792 NodeKind getKind() const { return Kind; }
2795 struct Scalar : Poly {
2796 enum ScalarKind {
2797 SK_Unknown,
2798 SK_Double,
2799 SK_Bool,
2800 } SKind;
2802 union {
2803 double DoubleValue;
2804 bool BoolValue;
2807 Scalar() : Poly(NK_Scalar), SKind(SK_Unknown) {}
2808 Scalar(double DoubleValue)
2809 : Poly(NK_Scalar), SKind(SK_Double), DoubleValue(DoubleValue) {}
2810 Scalar(bool BoolValue)
2811 : Poly(NK_Scalar), SKind(SK_Bool), BoolValue(BoolValue) {}
2813 static bool classof(const Poly *N) { return N->getKind() == NK_Scalar; }
2816 struct Seq : Poly, std::vector<std::unique_ptr<Poly>> {
2817 Seq() : Poly(NK_Seq) {}
2819 static bool classof(const Poly *N) { return N->getKind() == NK_Seq; }
2822 struct Map : Poly, llvm::StringMap<std::unique_ptr<Poly>> {
2823 Map() : Poly(NK_Map) {}
2825 static bool classof(const Poly *N) { return N->getKind() == NK_Map; }
2828 namespace llvm {
2829 namespace yaml {
2831 template <> struct PolymorphicTraits<std::unique_ptr<Poly>> {
2832 static NodeKind getKind(const std::unique_ptr<Poly> &N) {
2833 if (isa<Scalar>(*N))
2834 return NodeKind::Scalar;
2835 if (isa<Seq>(*N))
2836 return NodeKind::Sequence;
2837 if (isa<Map>(*N))
2838 return NodeKind::Map;
2839 llvm_unreachable("unsupported node type");
2842 static Scalar &getAsScalar(std::unique_ptr<Poly> &N) {
2843 if (!N || !isa<Scalar>(*N))
2844 N = std::make_unique<Scalar>();
2845 return *cast<Scalar>(N.get());
2848 static Seq &getAsSequence(std::unique_ptr<Poly> &N) {
2849 if (!N || !isa<Seq>(*N))
2850 N = std::make_unique<Seq>();
2851 return *cast<Seq>(N.get());
2854 static Map &getAsMap(std::unique_ptr<Poly> &N) {
2855 if (!N || !isa<Map>(*N))
2856 N = std::make_unique<Map>();
2857 return *cast<Map>(N.get());
2861 template <> struct TaggedScalarTraits<Scalar> {
2862 static void output(const Scalar &S, void *Ctxt, raw_ostream &ScalarOS,
2863 raw_ostream &TagOS) {
2864 switch (S.SKind) {
2865 case Scalar::SK_Unknown:
2866 report_fatal_error("output unknown scalar");
2867 break;
2868 case Scalar::SK_Double:
2869 TagOS << "!double";
2870 ScalarTraits<double>::output(S.DoubleValue, Ctxt, ScalarOS);
2871 break;
2872 case Scalar::SK_Bool:
2873 TagOS << "!bool";
2874 ScalarTraits<bool>::output(S.BoolValue, Ctxt, ScalarOS);
2875 break;
2879 static StringRef input(StringRef ScalarStr, StringRef Tag, void *Ctxt,
2880 Scalar &S) {
2881 S.SKind = StringSwitch<Scalar::ScalarKind>(Tag)
2882 .Case("!double", Scalar::SK_Double)
2883 .Case("!bool", Scalar::SK_Bool)
2884 .Default(Scalar::SK_Unknown);
2885 switch (S.SKind) {
2886 case Scalar::SK_Unknown:
2887 return StringRef("unknown scalar tag");
2888 case Scalar::SK_Double:
2889 return ScalarTraits<double>::input(ScalarStr, Ctxt, S.DoubleValue);
2890 case Scalar::SK_Bool:
2891 return ScalarTraits<bool>::input(ScalarStr, Ctxt, S.BoolValue);
2893 llvm_unreachable("unknown scalar kind");
2896 static QuotingType mustQuote(const Scalar &S, StringRef Str) {
2897 switch (S.SKind) {
2898 case Scalar::SK_Unknown:
2899 report_fatal_error("quote unknown scalar");
2900 case Scalar::SK_Double:
2901 return ScalarTraits<double>::mustQuote(Str);
2902 case Scalar::SK_Bool:
2903 return ScalarTraits<bool>::mustQuote(Str);
2905 llvm_unreachable("unknown scalar kind");
2909 template <> struct CustomMappingTraits<Map> {
2910 static void inputOne(IO &IO, StringRef Key, Map &M) {
2911 IO.mapRequired(Key.str().c_str(), M[Key]);
2914 static void output(IO &IO, Map &M) {
2915 for (auto &N : M)
2916 IO.mapRequired(N.getKey().str().c_str(), N.getValue());
2920 template <> struct SequenceTraits<Seq> {
2921 static size_t size(IO &IO, Seq &A) { return A.size(); }
2923 static std::unique_ptr<Poly> &element(IO &IO, Seq &A, size_t Index) {
2924 if (Index >= A.size())
2925 A.resize(Index + 1);
2926 return A[Index];
2930 } // namespace yaml
2931 } // namespace llvm
2933 TEST(YAMLIO, TestReadWritePolymorphicScalar) {
2934 std::string intermediate;
2935 std::unique_ptr<Poly> node = std::make_unique<Scalar>(true);
2937 llvm::raw_string_ostream ostr(intermediate);
2938 Output yout(ostr);
2939 #ifdef GTEST_HAS_DEATH_TEST
2940 #ifndef NDEBUG
2941 EXPECT_DEATH(yout << node, "plain scalar documents are not supported");
2942 #endif
2943 #endif
2946 TEST(YAMLIO, TestReadWritePolymorphicSeq) {
2947 std::string intermediate;
2949 auto seq = std::make_unique<Seq>();
2950 seq->push_back(std::make_unique<Scalar>(true));
2951 seq->push_back(std::make_unique<Scalar>(1.0));
2952 auto node = llvm::unique_dyn_cast<Poly>(seq);
2954 llvm::raw_string_ostream ostr(intermediate);
2955 Output yout(ostr);
2956 yout << node;
2959 Input yin(intermediate);
2960 std::unique_ptr<Poly> node;
2961 yin >> node;
2963 EXPECT_FALSE(yin.error());
2964 auto seq = llvm::dyn_cast<Seq>(node.get());
2965 ASSERT_TRUE(seq);
2966 ASSERT_EQ(seq->size(), 2u);
2967 auto first = llvm::dyn_cast<Scalar>((*seq)[0].get());
2968 ASSERT_TRUE(first);
2969 EXPECT_EQ(first->SKind, Scalar::SK_Bool);
2970 EXPECT_TRUE(first->BoolValue);
2971 auto second = llvm::dyn_cast<Scalar>((*seq)[1].get());
2972 ASSERT_TRUE(second);
2973 EXPECT_EQ(second->SKind, Scalar::SK_Double);
2974 EXPECT_EQ(second->DoubleValue, 1.0);
2978 TEST(YAMLIO, TestReadWritePolymorphicMap) {
2979 std::string intermediate;
2981 auto map = std::make_unique<Map>();
2982 (*map)["foo"] = std::make_unique<Scalar>(false);
2983 (*map)["bar"] = std::make_unique<Scalar>(2.0);
2984 std::unique_ptr<Poly> node = llvm::unique_dyn_cast<Poly>(map);
2986 llvm::raw_string_ostream ostr(intermediate);
2987 Output yout(ostr);
2988 yout << node;
2991 Input yin(intermediate);
2992 std::unique_ptr<Poly> node;
2993 yin >> node;
2995 EXPECT_FALSE(yin.error());
2996 auto map = llvm::dyn_cast<Map>(node.get());
2997 ASSERT_TRUE(map);
2998 auto foo = llvm::dyn_cast<Scalar>((*map)["foo"].get());
2999 ASSERT_TRUE(foo);
3000 EXPECT_EQ(foo->SKind, Scalar::SK_Bool);
3001 EXPECT_FALSE(foo->BoolValue);
3002 auto bar = llvm::dyn_cast<Scalar>((*map)["bar"].get());
3003 ASSERT_TRUE(bar);
3004 EXPECT_EQ(bar->SKind, Scalar::SK_Double);
3005 EXPECT_EQ(bar->DoubleValue, 2.0);