1 //===- unittest/Support/YAMLIOTest.cpp ------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Support/Casting.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/YAMLTraits.h"
15 #include "gtest/gtest.h"
18 using llvm::yaml::Input;
19 using llvm::yaml::Output;
21 using llvm::yaml::MappingTraits;
22 using llvm::yaml::MappingNormalization;
23 using llvm::yaml::ScalarTraits;
24 using llvm::yaml::Hex8;
25 using llvm::yaml::Hex16;
26 using llvm::yaml::Hex32;
27 using llvm::yaml::Hex64;
30 //===----------------------------------------------------------------------===//
32 //===----------------------------------------------------------------------===//
38 typedef std::vector<FooBar> FooBarSequence;
40 LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar)
46 struct MappingTraits<FooBar> {
47 static void mapping(IO &io, FooBar& fb) {
48 io.mapRequired("foo", fb.foo);
49 io.mapRequired("bar", fb.bar);
57 // Test the reading of a yaml mapping
59 TEST(YAMLIO, TestMapRead) {
61 Input yin("---\nfoo: 3\nbar: 5\n...\n");
64 EXPECT_FALSE(yin.error());
65 EXPECT_EQ(doc.foo, 3);
71 // Test the reading of a yaml sequence of mappings
73 TEST(YAMLIO, TestSequenceMapRead) {
75 Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
78 EXPECT_FALSE(yin.error());
79 EXPECT_EQ(seq.size(), 2UL);
80 FooBar& map1 = seq[0];
81 FooBar& map2 = seq[1];
82 EXPECT_EQ(map1.foo, 3);
83 EXPECT_EQ(map1.bar, 5);
84 EXPECT_EQ(map2.foo, 7);
85 EXPECT_EQ(map2.bar, 9);
90 // Test writing then reading back a sequence of mappings
92 TEST(YAMLIO, TestSequenceMapWriteAndRead) {
93 std::string intermediate;
102 seq.push_back(entry1);
103 seq.push_back(entry2);
105 llvm::raw_string_ostream ostr(intermediate);
111 Input yin(intermediate);
115 EXPECT_FALSE(yin.error());
116 EXPECT_EQ(seq2.size(), 2UL);
117 FooBar& map1 = seq2[0];
118 FooBar& map2 = seq2[1];
119 EXPECT_EQ(map1.foo, 10);
120 EXPECT_EQ(map1.bar, -3);
121 EXPECT_EQ(map2.foo, 257);
122 EXPECT_EQ(map2.bar, 0);
127 //===----------------------------------------------------------------------===//
128 // Test built-in types
129 //===----------------------------------------------------------------------===//
131 struct BuiltInTypes {
153 struct MappingTraits<BuiltInTypes> {
154 static void mapping(IO &io, BuiltInTypes& bt) {
155 io.mapRequired("str", bt.str);
156 io.mapRequired("u64", bt.u64);
157 io.mapRequired("u32", bt.u32);
158 io.mapRequired("u16", bt.u16);
159 io.mapRequired("u8", bt.u8);
160 io.mapRequired("b", bt.b);
161 io.mapRequired("s64", bt.s64);
162 io.mapRequired("s32", bt.s32);
163 io.mapRequired("s16", bt.s16);
164 io.mapRequired("s8", bt.s8);
165 io.mapRequired("f", bt.f);
166 io.mapRequired("d", bt.d);
167 io.mapRequired("h8", bt.h8);
168 io.mapRequired("h16", bt.h16);
169 io.mapRequired("h32", bt.h32);
170 io.mapRequired("h64", bt.h64);
178 // Test the reading of all built-in scalar conversions
180 TEST(YAMLIO, TestReadBuiltInTypes) {
198 "h64: 0xFEDCBA9876543210\n"
202 EXPECT_FALSE(yin.error());
203 EXPECT_TRUE(map.str.equals("hello there"));
204 EXPECT_EQ(map.u64, 5000000000ULL);
205 EXPECT_EQ(map.u32, 4000000000U);
206 EXPECT_EQ(map.u16, 65000);
207 EXPECT_EQ(map.u8, 255);
208 EXPECT_EQ(map.b, false);
209 EXPECT_EQ(map.s64, -5000000000LL);
210 EXPECT_EQ(map.s32, -2000000000L);
211 EXPECT_EQ(map.s16, -32000);
212 EXPECT_EQ(map.s8, -127);
213 EXPECT_EQ(map.f, 137.125);
214 EXPECT_EQ(map.d, -2.8625);
215 EXPECT_EQ(map.h8, Hex8(255));
216 EXPECT_EQ(map.h16, Hex16(0x8765));
217 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
218 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
223 // Test writing then reading back all built-in scalar types
225 TEST(YAMLIO, TestReadWriteBuiltInTypes) {
226 std::string intermediate;
230 map.u64 = 6000000000ULL;
231 map.u32 = 3000000000U;
235 map.s64 = -6000000000LL;
236 map.s32 = -2000000000;
243 map.h32 = 3000000000U;
244 map.h64 = 6000000000LL;
246 llvm::raw_string_ostream ostr(intermediate);
252 Input yin(intermediate);
256 EXPECT_FALSE(yin.error());
257 EXPECT_TRUE(map.str.equals("one two"));
258 EXPECT_EQ(map.u64, 6000000000ULL);
259 EXPECT_EQ(map.u32, 3000000000U);
260 EXPECT_EQ(map.u16, 50000);
261 EXPECT_EQ(map.u8, 254);
262 EXPECT_EQ(map.b, true);
263 EXPECT_EQ(map.s64, -6000000000LL);
264 EXPECT_EQ(map.s32, -2000000000L);
265 EXPECT_EQ(map.s16, -32000);
266 EXPECT_EQ(map.s8, -128);
267 EXPECT_EQ(map.f, 3.25);
268 EXPECT_EQ(map.d, -2.8625);
269 EXPECT_EQ(map.h8, Hex8(254));
270 EXPECT_EQ(map.h16, Hex16(50000));
271 EXPECT_EQ(map.h32, Hex32(3000000000U));
272 EXPECT_EQ(map.h64, Hex64(6000000000LL));
277 llvm::StringRef str1;
278 llvm::StringRef str2;
279 llvm::StringRef str3;
280 llvm::StringRef str4;
281 llvm::StringRef str5;
287 struct MappingTraits<StringTypes> {
288 static void mapping(IO &io, StringTypes& st) {
289 io.mapRequired("str1", st.str1);
290 io.mapRequired("str2", st.str2);
291 io.mapRequired("str3", st.str3);
292 io.mapRequired("str4", st.str4);
293 io.mapRequired("str5", st.str5);
299 TEST(YAMLIO, TestReadWriteStringTypes) {
300 std::string intermediate;
309 llvm::raw_string_ostream ostr(intermediate);
314 llvm::StringRef flowOut(intermediate);
315 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
316 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
317 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
318 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
319 EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
322 Input yin(intermediate);
326 EXPECT_FALSE(yin.error());
327 EXPECT_TRUE(map.str1.equals("'aaa"));
328 EXPECT_TRUE(map.str2.equals("\"bbb"));
329 EXPECT_TRUE(map.str3.equals("`ccc"));
330 EXPECT_TRUE(map.str4.equals("@ddd"));
331 EXPECT_TRUE(map.str5.equals(""));
335 //===----------------------------------------------------------------------===//
336 // Test ScalarEnumerationTraits
337 //===----------------------------------------------------------------------===//
358 struct ScalarEnumerationTraits<Colors> {
359 static void enumeration(IO &io, Colors &value) {
360 io.enumCase(value, "red", cRed);
361 io.enumCase(value, "blue", cBlue);
362 io.enumCase(value, "green", cGreen);
363 io.enumCase(value, "yellow",cYellow);
367 struct MappingTraits<ColorMap> {
368 static void mapping(IO &io, ColorMap& c) {
369 io.mapRequired("c1", c.c1);
370 io.mapRequired("c2", c.c2);
371 io.mapRequired("c3", c.c3);
372 io.mapOptional("c4", c.c4, cBlue); // supplies default
373 io.mapOptional("c5", c.c5, cYellow); // supplies default
374 io.mapOptional("c6", c.c6, cRed); // supplies default
382 // Test reading enumerated scalars
384 TEST(YAMLIO, TestEnumRead) {
394 EXPECT_FALSE(yin.error());
395 EXPECT_EQ(cBlue, map.c1);
396 EXPECT_EQ(cRed, map.c2);
397 EXPECT_EQ(cGreen, map.c3);
398 EXPECT_EQ(cBlue, map.c4); // tests default
399 EXPECT_EQ(cYellow,map.c5); // tests overridden
400 EXPECT_EQ(cRed, map.c6); // tests default
405 //===----------------------------------------------------------------------===//
406 // Test ScalarBitSetTraits
407 //===----------------------------------------------------------------------===//
416 inline MyFlags operator|(MyFlags a, MyFlags b) {
417 return static_cast<MyFlags>(
418 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
432 struct ScalarBitSetTraits<MyFlags> {
433 static void bitset(IO &io, MyFlags &value) {
434 io.bitSetCase(value, "big", flagBig);
435 io.bitSetCase(value, "flat", flagFlat);
436 io.bitSetCase(value, "round", flagRound);
437 io.bitSetCase(value, "pointy",flagPointy);
441 struct MappingTraits<FlagsMap> {
442 static void mapping(IO &io, FlagsMap& c) {
443 io.mapRequired("f1", c.f1);
444 io.mapRequired("f2", c.f2);
445 io.mapRequired("f3", c.f3);
446 io.mapOptional("f4", c.f4, MyFlags(flagRound));
454 // Test reading flow sequence representing bit-mask values
456 TEST(YAMLIO, TestFlagsRead) {
460 "f2: [ round, flat ]\n"
465 EXPECT_FALSE(yin.error());
466 EXPECT_EQ(flagBig, map.f1);
467 EXPECT_EQ(flagRound|flagFlat, map.f2);
468 EXPECT_EQ(flagNone, map.f3); // check empty set
469 EXPECT_EQ(flagRound, map.f4); // check optional key
474 // Test writing then reading back bit-mask values
476 TEST(YAMLIO, TestReadWriteFlags) {
477 std::string intermediate;
481 map.f2 = flagRound | flagFlat;
485 llvm::raw_string_ostream ostr(intermediate);
491 Input yin(intermediate);
495 EXPECT_FALSE(yin.error());
496 EXPECT_EQ(flagBig, map2.f1);
497 EXPECT_EQ(flagRound|flagFlat, map2.f2);
498 EXPECT_EQ(flagNone, map2.f3);
499 //EXPECT_EQ(flagRound, map2.f4); // check optional key
505 //===----------------------------------------------------------------------===//
507 //===----------------------------------------------------------------------===//
509 struct MyCustomType {
514 struct MyCustomTypeMap {
524 struct MappingTraits<MyCustomTypeMap> {
525 static void mapping(IO &io, MyCustomTypeMap& s) {
526 io.mapRequired("f1", s.f1);
527 io.mapRequired("f2", s.f2);
528 io.mapRequired("f3", s.f3);
531 // MyCustomType is formatted as a yaml scalar. A value of
532 // {length=3, width=4} would be represented in yaml as "3 by 4".
534 struct ScalarTraits<MyCustomType> {
535 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
536 out << llvm::format("%d by %d", value.length, value.width);
538 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
539 size_t byStart = scalar.find("by");
540 if ( byStart != StringRef::npos ) {
541 StringRef lenStr = scalar.slice(0, byStart);
542 lenStr = lenStr.rtrim();
543 if ( lenStr.getAsInteger(0, value.length) ) {
544 return "malformed length";
546 StringRef widthStr = scalar.drop_front(byStart+2);
547 widthStr = widthStr.ltrim();
548 if ( widthStr.getAsInteger(0, value.width) ) {
549 return "malformed width";
554 return "malformed by";
563 // Test writing then reading back custom values
565 TEST(YAMLIO, TestReadWriteMyCustomType) {
566 std::string intermediate;
575 llvm::raw_string_ostream ostr(intermediate);
581 Input yin(intermediate);
582 MyCustomTypeMap map2;
585 EXPECT_FALSE(yin.error());
586 EXPECT_EQ(1, map2.f1.length);
587 EXPECT_EQ(4, map2.f1.width);
588 EXPECT_EQ(100, map2.f2.length);
589 EXPECT_EQ(400, map2.f2.width);
590 EXPECT_EQ(10, map2.f3);
595 //===----------------------------------------------------------------------===//
596 // Test flow sequences
597 //===----------------------------------------------------------------------===//
599 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
600 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
601 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::StringRef)
606 struct ScalarTraits<MyNumber> {
607 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
611 static StringRef input(StringRef scalar, void *, MyNumber &value) {
613 if ( getAsSignedInteger(scalar, 0, n) )
614 return "invalid number";
622 struct NameAndNumbers {
623 llvm::StringRef name;
624 std::vector<llvm::StringRef> strings;
625 std::vector<MyNumber> single;
626 std::vector<MyNumber> numbers;
632 struct MappingTraits<NameAndNumbers> {
633 static void mapping(IO &io, NameAndNumbers& nn) {
634 io.mapRequired("name", nn.name);
635 io.mapRequired("strings", nn.strings);
636 io.mapRequired("single", nn.single);
637 io.mapRequired("numbers", nn.numbers);
645 // Test writing then reading back custom values
647 TEST(YAMLIO, TestReadWriteMyFlowSequence) {
648 std::string intermediate;
652 map.strings.push_back(llvm::StringRef("one"));
653 map.strings.push_back(llvm::StringRef("two"));
654 map.single.push_back(1);
655 map.numbers.push_back(10);
656 map.numbers.push_back(-30);
657 map.numbers.push_back(1024);
659 llvm::raw_string_ostream ostr(intermediate);
663 // Verify sequences were written in flow style
665 llvm::StringRef flowOut(intermediate);
666 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
667 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
671 Input yin(intermediate);
675 EXPECT_FALSE(yin.error());
676 EXPECT_TRUE(map2.name.equals("hello"));
677 EXPECT_EQ(map2.strings.size(), 2UL);
678 EXPECT_TRUE(map2.strings[0].equals("one"));
679 EXPECT_TRUE(map2.strings[1].equals("two"));
680 EXPECT_EQ(map2.single.size(), 1UL);
681 EXPECT_EQ(1, map2.single[0]);
682 EXPECT_EQ(map2.numbers.size(), 3UL);
683 EXPECT_EQ(10, map2.numbers[0]);
684 EXPECT_EQ(-30, map2.numbers[1]);
685 EXPECT_EQ(1024, map2.numbers[2]);
690 //===----------------------------------------------------------------------===//
691 // Test normalizing/denormalizing
692 //===----------------------------------------------------------------------===//
694 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
696 typedef std::vector<TotalSeconds> SecondsSequence;
698 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
704 struct MappingTraits<TotalSeconds> {
706 class NormalizedSeconds {
708 NormalizedSeconds(IO &io)
709 : hours(0), minutes(0), seconds(0) {
711 NormalizedSeconds(IO &, TotalSeconds &secs)
713 minutes((secs - (hours*3600))/60),
716 TotalSeconds denormalize(IO &) {
717 return TotalSeconds(hours*3600 + minutes*60 + seconds);
725 static void mapping(IO &io, TotalSeconds &secs) {
726 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
728 io.mapOptional("hours", keys->hours, (uint32_t)0);
729 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
730 io.mapRequired("seconds", keys->seconds);
738 // Test the reading of a yaml sequence of mappings
740 TEST(YAMLIO, TestReadMySecondsSequence) {
742 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
745 EXPECT_FALSE(yin.error());
746 EXPECT_EQ(seq.size(), 2UL);
747 EXPECT_EQ(seq[0], 3605U);
748 EXPECT_EQ(seq[1], 59U);
753 // Test writing then reading back custom values
755 TEST(YAMLIO, TestReadWriteMySecondsSequence) {
756 std::string intermediate;
763 llvm::raw_string_ostream ostr(intermediate);
768 Input yin(intermediate);
769 SecondsSequence seq2;
772 EXPECT_FALSE(yin.error());
773 EXPECT_EQ(seq2.size(), 3UL);
774 EXPECT_EQ(seq2[0], 4000U);
775 EXPECT_EQ(seq2[1], 500U);
776 EXPECT_EQ(seq2[2], 59U);
781 //===----------------------------------------------------------------------===//
782 // Test dynamic typing
783 //===----------------------------------------------------------------------===//
802 struct KindAndFlags {
803 KindAndFlags() : kind(kindA), flags(0) { }
804 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
809 typedef std::vector<KindAndFlags> KindAndFlagsSequence;
811 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
816 struct ScalarEnumerationTraits<AFlags> {
817 static void enumeration(IO &io, AFlags &value) {
818 io.enumCase(value, "a1", a1);
819 io.enumCase(value, "a2", a2);
820 io.enumCase(value, "a3", a3);
824 struct ScalarEnumerationTraits<BFlags> {
825 static void enumeration(IO &io, BFlags &value) {
826 io.enumCase(value, "b1", b1);
827 io.enumCase(value, "b2", b2);
828 io.enumCase(value, "b3", b3);
832 struct ScalarEnumerationTraits<Kind> {
833 static void enumeration(IO &io, Kind &value) {
834 io.enumCase(value, "A", kindA);
835 io.enumCase(value, "B", kindB);
839 struct MappingTraits<KindAndFlags> {
840 static void mapping(IO &io, KindAndFlags& kf) {
841 io.mapRequired("kind", kf.kind);
842 // Type of "flags" field varies depending on "kind" field.
843 // Use memcpy here to avoid breaking strict aliasing rules.
844 if (kf.kind == kindA) {
845 AFlags aflags = static_cast<AFlags>(kf.flags);
846 io.mapRequired("flags", aflags);
849 BFlags bflags = static_cast<BFlags>(kf.flags);
850 io.mapRequired("flags", bflags);
860 // Test the reading of a yaml sequence dynamic types
862 TEST(YAMLIO, TestReadKindAndFlagsSequence) {
863 KindAndFlagsSequence seq;
864 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
867 EXPECT_FALSE(yin.error());
868 EXPECT_EQ(seq.size(), 2UL);
869 EXPECT_EQ(seq[0].kind, kindA);
870 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
871 EXPECT_EQ(seq[1].kind, kindB);
872 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
876 // Test writing then reading back dynamic types
878 TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
879 std::string intermediate;
881 KindAndFlagsSequence seq;
882 seq.push_back(KindAndFlags(kindA,a1));
883 seq.push_back(KindAndFlags(kindB,b1));
884 seq.push_back(KindAndFlags(kindA,a2));
885 seq.push_back(KindAndFlags(kindB,b2));
886 seq.push_back(KindAndFlags(kindA,a3));
888 llvm::raw_string_ostream ostr(intermediate);
893 Input yin(intermediate);
894 KindAndFlagsSequence seq2;
897 EXPECT_FALSE(yin.error());
898 EXPECT_EQ(seq2.size(), 5UL);
899 EXPECT_EQ(seq2[0].kind, kindA);
900 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
901 EXPECT_EQ(seq2[1].kind, kindB);
902 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
903 EXPECT_EQ(seq2[2].kind, kindA);
904 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
905 EXPECT_EQ(seq2[3].kind, kindB);
906 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
907 EXPECT_EQ(seq2[4].kind, kindA);
908 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
913 //===----------------------------------------------------------------------===//
914 // Test document list
915 //===----------------------------------------------------------------------===//
921 typedef std::vector<FooBarMap> FooBarMapDocumentList;
923 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
929 struct MappingTraits<FooBarMap> {
930 static void mapping(IO &io, FooBarMap& fb) {
931 io.mapRequired("foo", fb.foo);
932 io.mapRequired("bar", fb.bar);
940 // Test the reading of a yaml mapping
942 TEST(YAMLIO, TestDocRead) {
944 Input yin("---\nfoo: 3\nbar: 5\n...\n");
947 EXPECT_FALSE(yin.error());
948 EXPECT_EQ(doc.foo, 3);
949 EXPECT_EQ(doc.bar,5);
955 // Test writing then reading back a sequence of mappings
957 TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
958 std::string intermediate;
966 std::vector<FooBarMap> docList;
967 docList.push_back(doc1);
968 docList.push_back(doc2);
970 llvm::raw_string_ostream ostr(intermediate);
977 Input yin(intermediate);
978 std::vector<FooBarMap> docList2;
981 EXPECT_FALSE(yin.error());
982 EXPECT_EQ(docList2.size(), 2UL);
983 FooBarMap& map1 = docList2[0];
984 FooBarMap& map2 = docList2[1];
985 EXPECT_EQ(map1.foo, 10);
986 EXPECT_EQ(map1.bar, -3);
987 EXPECT_EQ(map2.foo, 257);
988 EXPECT_EQ(map2.bar, 0);
993 //===----------------------------------------------------------------------===//
994 // Test error handling
995 //===----------------------------------------------------------------------===//
999 static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
1004 // Test error handling of unknown enumerated scalar
1006 TEST(YAMLIO, TestColorsReadError) {
1013 yin.setDiagHandler(suppressErrorMessages);
1015 EXPECT_TRUE(yin.error());
1020 // Test error handling of flow sequence with unknown value
1022 TEST(YAMLIO, TestFlagsReadError) {
1026 "f2: [ round, hollow ]\n"
1029 yin.setDiagHandler(suppressErrorMessages);
1032 EXPECT_TRUE(yin.error());
1037 // Test error handling reading built-in uint8_t type
1039 LLVM_YAML_IS_SEQUENCE_VECTOR(uint8_t)
1040 TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1041 std::vector<uint8_t> seq;
1047 yin.setDiagHandler(suppressErrorMessages);
1050 EXPECT_TRUE(yin.error());
1055 // Test error handling reading built-in uint16_t type
1057 LLVM_YAML_IS_SEQUENCE_VECTOR(uint16_t)
1058 TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1059 std::vector<uint16_t> seq;
1065 yin.setDiagHandler(suppressErrorMessages);
1068 EXPECT_TRUE(yin.error());
1073 // Test error handling reading built-in uint32_t type
1075 LLVM_YAML_IS_SEQUENCE_VECTOR(uint32_t)
1076 TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1077 std::vector<uint32_t> seq;
1083 yin.setDiagHandler(suppressErrorMessages);
1086 EXPECT_TRUE(yin.error());
1091 // Test error handling reading built-in uint64_t type
1093 LLVM_YAML_IS_SEQUENCE_VECTOR(uint64_t)
1094 TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1095 std::vector<uint64_t> seq;
1097 "- 18446744073709551615\n"
1099 "- 19446744073709551615\n"
1101 yin.setDiagHandler(suppressErrorMessages);
1104 EXPECT_TRUE(yin.error());
1109 // Test error handling reading built-in int8_t type
1111 LLVM_YAML_IS_SEQUENCE_VECTOR(int8_t)
1112 TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1113 std::vector<int8_t> seq;
1120 yin.setDiagHandler(suppressErrorMessages);
1123 EXPECT_TRUE(yin.error());
1127 // Test error handling reading built-in int8_t type
1129 TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1130 std::vector<int8_t> seq;
1137 yin.setDiagHandler(suppressErrorMessages);
1140 EXPECT_TRUE(yin.error());
1145 // Test error handling reading built-in int16_t type
1147 LLVM_YAML_IS_SEQUENCE_VECTOR(int16_t)
1148 TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1149 std::vector<int16_t> seq;
1156 yin.setDiagHandler(suppressErrorMessages);
1159 EXPECT_TRUE(yin.error());
1164 // Test error handling reading built-in int16_t type
1166 TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1167 std::vector<int16_t> seq;
1174 yin.setDiagHandler(suppressErrorMessages);
1177 EXPECT_TRUE(yin.error());
1182 // Test error handling reading built-in int32_t type
1184 LLVM_YAML_IS_SEQUENCE_VECTOR(int32_t)
1185 TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1186 std::vector<int32_t> seq;
1193 yin.setDiagHandler(suppressErrorMessages);
1196 EXPECT_TRUE(yin.error());
1200 // Test error handling reading built-in int32_t type
1202 TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1203 std::vector<int32_t> seq;
1210 yin.setDiagHandler(suppressErrorMessages);
1213 EXPECT_TRUE(yin.error());
1218 // Test error handling reading built-in int64_t type
1220 LLVM_YAML_IS_SEQUENCE_VECTOR(int64_t)
1221 TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1222 std::vector<int64_t> seq;
1224 "- -9223372036854775808\n"
1226 "- 9223372036854775807\n"
1227 "- -9223372036854775809\n"
1229 yin.setDiagHandler(suppressErrorMessages);
1232 EXPECT_TRUE(yin.error());
1236 // Test error handling reading built-in int64_t type
1238 TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1239 std::vector<int64_t> seq;
1241 "- -9223372036854775808\n"
1243 "- 9223372036854775807\n"
1244 "- 9223372036854775809\n"
1246 yin.setDiagHandler(suppressErrorMessages);
1249 EXPECT_TRUE(yin.error());
1253 // Test error handling reading built-in float type
1255 LLVM_YAML_IS_SEQUENCE_VECTOR(float)
1256 TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1257 std::vector<float> seq;
1264 yin.setDiagHandler(suppressErrorMessages);
1267 EXPECT_TRUE(yin.error());
1271 // Test error handling reading built-in float type
1273 LLVM_YAML_IS_SEQUENCE_VECTOR(double)
1274 TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
1275 std::vector<double> seq;
1282 yin.setDiagHandler(suppressErrorMessages);
1285 EXPECT_TRUE(yin.error());
1289 // Test error handling reading built-in Hex8 type
1291 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
1292 TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
1293 std::vector<Hex8> seq;
1299 yin.setDiagHandler(suppressErrorMessages);
1302 EXPECT_TRUE(yin.error());
1307 // Test error handling reading built-in Hex16 type
1309 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
1310 TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
1311 std::vector<Hex16> seq;
1317 yin.setDiagHandler(suppressErrorMessages);
1320 EXPECT_TRUE(yin.error());
1324 // Test error handling reading built-in Hex32 type
1326 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
1327 TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
1328 std::vector<Hex32> seq;
1334 yin.setDiagHandler(suppressErrorMessages);
1337 EXPECT_TRUE(yin.error());
1341 // Test error handling reading built-in Hex64 type
1343 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
1344 TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
1345 std::vector<Hex64> seq;
1348 "- 0xFFEEDDCCBBAA9988\n"
1349 "- 0x12345567890ABCDEF0\n"
1351 yin.setDiagHandler(suppressErrorMessages);
1354 EXPECT_TRUE(yin.error());
1357 struct OptionalTest {
1358 std::vector<int> Numbers;
1361 struct OptionalTestSeq {
1362 std::vector<OptionalTest> Tests;
1365 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
1369 struct MappingTraits<OptionalTest> {
1370 static void mapping(IO& IO, OptionalTest &OT) {
1371 IO.mapOptional("Numbers", OT.Numbers);
1376 struct MappingTraits<OptionalTestSeq> {
1377 static void mapping(IO &IO, OptionalTestSeq &OTS) {
1378 IO.mapOptional("Tests", OTS.Tests);
1384 TEST(YAMLIO, SequenceElideTest) {
1385 // Test that writing out a purely optional structure with its fields set to
1386 // default followed by other data is properly read back in.
1387 OptionalTestSeq Seq;
1388 OptionalTest One, Two, Three, Four;
1389 int N[] = {1, 2, 3};
1390 Three.Numbers.assign(N, N + 3);
1391 Seq.Tests.push_back(One);
1392 Seq.Tests.push_back(Two);
1393 Seq.Tests.push_back(Three);
1394 Seq.Tests.push_back(Four);
1396 std::string intermediate;
1398 llvm::raw_string_ostream ostr(intermediate);
1403 Input yin(intermediate);
1404 OptionalTestSeq Seq2;
1407 EXPECT_FALSE(yin.error());
1409 EXPECT_EQ(4UL, Seq2.Tests.size());
1411 EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
1412 EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
1414 EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
1415 EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
1416 EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
1418 EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());