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 {
154 struct MappingTraits<BuiltInTypes> {
155 static void mapping(IO &io, BuiltInTypes& bt) {
156 io.mapRequired("str", bt.str);
157 io.mapRequired("stdstr", bt.stdstr);
158 io.mapRequired("u64", bt.u64);
159 io.mapRequired("u32", bt.u32);
160 io.mapRequired("u16", bt.u16);
161 io.mapRequired("u8", bt.u8);
162 io.mapRequired("b", bt.b);
163 io.mapRequired("s64", bt.s64);
164 io.mapRequired("s32", bt.s32);
165 io.mapRequired("s16", bt.s16);
166 io.mapRequired("s8", bt.s8);
167 io.mapRequired("f", bt.f);
168 io.mapRequired("d", bt.d);
169 io.mapRequired("h8", bt.h8);
170 io.mapRequired("h16", bt.h16);
171 io.mapRequired("h32", bt.h32);
172 io.mapRequired("h64", bt.h64);
180 // Test the reading of all built-in scalar conversions
182 TEST(YAMLIO, TestReadBuiltInTypes) {
186 "stdstr: hello where?\n"
201 "h64: 0xFEDCBA9876543210\n"
205 EXPECT_FALSE(yin.error());
206 EXPECT_TRUE(map.str.equals("hello there"));
207 EXPECT_TRUE(map.stdstr == "hello where?");
208 EXPECT_EQ(map.u64, 5000000000ULL);
209 EXPECT_EQ(map.u32, 4000000000U);
210 EXPECT_EQ(map.u16, 65000);
211 EXPECT_EQ(map.u8, 255);
212 EXPECT_EQ(map.b, false);
213 EXPECT_EQ(map.s64, -5000000000LL);
214 EXPECT_EQ(map.s32, -2000000000L);
215 EXPECT_EQ(map.s16, -32000);
216 EXPECT_EQ(map.s8, -127);
217 EXPECT_EQ(map.f, 137.125);
218 EXPECT_EQ(map.d, -2.8625);
219 EXPECT_EQ(map.h8, Hex8(255));
220 EXPECT_EQ(map.h16, Hex16(0x8765));
221 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
222 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
227 // Test writing then reading back all built-in scalar types
229 TEST(YAMLIO, TestReadWriteBuiltInTypes) {
230 std::string intermediate;
234 map.stdstr = "three four";
235 map.u64 = 6000000000ULL;
236 map.u32 = 3000000000U;
240 map.s64 = -6000000000LL;
241 map.s32 = -2000000000;
248 map.h32 = 3000000000U;
249 map.h64 = 6000000000LL;
251 llvm::raw_string_ostream ostr(intermediate);
257 Input yin(intermediate);
261 EXPECT_FALSE(yin.error());
262 EXPECT_TRUE(map.str.equals("one two"));
263 EXPECT_TRUE(map.stdstr == "three four");
264 EXPECT_EQ(map.u64, 6000000000ULL);
265 EXPECT_EQ(map.u32, 3000000000U);
266 EXPECT_EQ(map.u16, 50000);
267 EXPECT_EQ(map.u8, 254);
268 EXPECT_EQ(map.b, true);
269 EXPECT_EQ(map.s64, -6000000000LL);
270 EXPECT_EQ(map.s32, -2000000000L);
271 EXPECT_EQ(map.s16, -32000);
272 EXPECT_EQ(map.s8, -128);
273 EXPECT_EQ(map.f, 3.25);
274 EXPECT_EQ(map.d, -2.8625);
275 EXPECT_EQ(map.h8, Hex8(254));
276 EXPECT_EQ(map.h16, Hex16(50000));
277 EXPECT_EQ(map.h32, Hex32(3000000000U));
278 EXPECT_EQ(map.h64, Hex64(6000000000LL));
283 llvm::StringRef str1;
284 llvm::StringRef str2;
285 llvm::StringRef str3;
286 llvm::StringRef str4;
287 llvm::StringRef str5;
298 struct MappingTraits<StringTypes> {
299 static void mapping(IO &io, StringTypes& st) {
300 io.mapRequired("str1", st.str1);
301 io.mapRequired("str2", st.str2);
302 io.mapRequired("str3", st.str3);
303 io.mapRequired("str4", st.str4);
304 io.mapRequired("str5", st.str5);
305 io.mapRequired("stdstr1", st.stdstr1);
306 io.mapRequired("stdstr2", st.stdstr2);
307 io.mapRequired("stdstr3", st.stdstr3);
308 io.mapRequired("stdstr4", st.stdstr4);
309 io.mapRequired("stdstr5", st.stdstr5);
315 TEST(YAMLIO, TestReadWriteStringTypes) {
316 std::string intermediate;
324 map.stdstr1 = "'eee";
325 map.stdstr2 = "\"fff";
326 map.stdstr3 = "`ggg";
327 map.stdstr4 = "@hhh";
330 llvm::raw_string_ostream ostr(intermediate);
335 llvm::StringRef flowOut(intermediate);
336 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
337 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
338 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
339 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
340 EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
341 EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
342 EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
343 EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
344 EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
345 EXPECT_NE(std::string::npos, flowOut.find("''\n"));
348 Input yin(intermediate);
352 EXPECT_FALSE(yin.error());
353 EXPECT_TRUE(map.str1.equals("'aaa"));
354 EXPECT_TRUE(map.str2.equals("\"bbb"));
355 EXPECT_TRUE(map.str3.equals("`ccc"));
356 EXPECT_TRUE(map.str4.equals("@ddd"));
357 EXPECT_TRUE(map.str5.equals(""));
358 EXPECT_TRUE(map.stdstr1 == "'eee");
359 EXPECT_TRUE(map.stdstr2 == "\"fff");
360 EXPECT_TRUE(map.stdstr3 == "`ggg");
361 EXPECT_TRUE(map.stdstr4 == "@hhh");
362 EXPECT_TRUE(map.stdstr5 == "");
366 //===----------------------------------------------------------------------===//
367 // Test ScalarEnumerationTraits
368 //===----------------------------------------------------------------------===//
389 struct ScalarEnumerationTraits<Colors> {
390 static void enumeration(IO &io, Colors &value) {
391 io.enumCase(value, "red", cRed);
392 io.enumCase(value, "blue", cBlue);
393 io.enumCase(value, "green", cGreen);
394 io.enumCase(value, "yellow",cYellow);
398 struct MappingTraits<ColorMap> {
399 static void mapping(IO &io, ColorMap& c) {
400 io.mapRequired("c1", c.c1);
401 io.mapRequired("c2", c.c2);
402 io.mapRequired("c3", c.c3);
403 io.mapOptional("c4", c.c4, cBlue); // supplies default
404 io.mapOptional("c5", c.c5, cYellow); // supplies default
405 io.mapOptional("c6", c.c6, cRed); // supplies default
413 // Test reading enumerated scalars
415 TEST(YAMLIO, TestEnumRead) {
425 EXPECT_FALSE(yin.error());
426 EXPECT_EQ(cBlue, map.c1);
427 EXPECT_EQ(cRed, map.c2);
428 EXPECT_EQ(cGreen, map.c3);
429 EXPECT_EQ(cBlue, map.c4); // tests default
430 EXPECT_EQ(cYellow,map.c5); // tests overridden
431 EXPECT_EQ(cRed, map.c6); // tests default
436 //===----------------------------------------------------------------------===//
437 // Test ScalarBitSetTraits
438 //===----------------------------------------------------------------------===//
447 inline MyFlags operator|(MyFlags a, MyFlags b) {
448 return static_cast<MyFlags>(
449 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
463 struct ScalarBitSetTraits<MyFlags> {
464 static void bitset(IO &io, MyFlags &value) {
465 io.bitSetCase(value, "big", flagBig);
466 io.bitSetCase(value, "flat", flagFlat);
467 io.bitSetCase(value, "round", flagRound);
468 io.bitSetCase(value, "pointy",flagPointy);
472 struct MappingTraits<FlagsMap> {
473 static void mapping(IO &io, FlagsMap& c) {
474 io.mapRequired("f1", c.f1);
475 io.mapRequired("f2", c.f2);
476 io.mapRequired("f3", c.f3);
477 io.mapOptional("f4", c.f4, MyFlags(flagRound));
485 // Test reading flow sequence representing bit-mask values
487 TEST(YAMLIO, TestFlagsRead) {
491 "f2: [ round, flat ]\n"
496 EXPECT_FALSE(yin.error());
497 EXPECT_EQ(flagBig, map.f1);
498 EXPECT_EQ(flagRound|flagFlat, map.f2);
499 EXPECT_EQ(flagNone, map.f3); // check empty set
500 EXPECT_EQ(flagRound, map.f4); // check optional key
505 // Test writing then reading back bit-mask values
507 TEST(YAMLIO, TestReadWriteFlags) {
508 std::string intermediate;
512 map.f2 = flagRound | flagFlat;
516 llvm::raw_string_ostream ostr(intermediate);
522 Input yin(intermediate);
526 EXPECT_FALSE(yin.error());
527 EXPECT_EQ(flagBig, map2.f1);
528 EXPECT_EQ(flagRound|flagFlat, map2.f2);
529 EXPECT_EQ(flagNone, map2.f3);
530 //EXPECT_EQ(flagRound, map2.f4); // check optional key
536 //===----------------------------------------------------------------------===//
538 //===----------------------------------------------------------------------===//
540 struct MyCustomType {
545 struct MyCustomTypeMap {
555 struct MappingTraits<MyCustomTypeMap> {
556 static void mapping(IO &io, MyCustomTypeMap& s) {
557 io.mapRequired("f1", s.f1);
558 io.mapRequired("f2", s.f2);
559 io.mapRequired("f3", s.f3);
562 // MyCustomType is formatted as a yaml scalar. A value of
563 // {length=3, width=4} would be represented in yaml as "3 by 4".
565 struct ScalarTraits<MyCustomType> {
566 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
567 out << llvm::format("%d by %d", value.length, value.width);
569 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
570 size_t byStart = scalar.find("by");
571 if ( byStart != StringRef::npos ) {
572 StringRef lenStr = scalar.slice(0, byStart);
573 lenStr = lenStr.rtrim();
574 if ( lenStr.getAsInteger(0, value.length) ) {
575 return "malformed length";
577 StringRef widthStr = scalar.drop_front(byStart+2);
578 widthStr = widthStr.ltrim();
579 if ( widthStr.getAsInteger(0, value.width) ) {
580 return "malformed width";
585 return "malformed by";
594 // Test writing then reading back custom values
596 TEST(YAMLIO, TestReadWriteMyCustomType) {
597 std::string intermediate;
606 llvm::raw_string_ostream ostr(intermediate);
612 Input yin(intermediate);
613 MyCustomTypeMap map2;
616 EXPECT_FALSE(yin.error());
617 EXPECT_EQ(1, map2.f1.length);
618 EXPECT_EQ(4, map2.f1.width);
619 EXPECT_EQ(100, map2.f2.length);
620 EXPECT_EQ(400, map2.f2.width);
621 EXPECT_EQ(10, map2.f3);
626 //===----------------------------------------------------------------------===//
627 // Test flow sequences
628 //===----------------------------------------------------------------------===//
630 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
631 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
632 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::StringRef)
637 struct ScalarTraits<MyNumber> {
638 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
642 static StringRef input(StringRef scalar, void *, MyNumber &value) {
644 if ( getAsSignedInteger(scalar, 0, n) )
645 return "invalid number";
653 struct NameAndNumbers {
654 llvm::StringRef name;
655 std::vector<llvm::StringRef> strings;
656 std::vector<MyNumber> single;
657 std::vector<MyNumber> numbers;
663 struct MappingTraits<NameAndNumbers> {
664 static void mapping(IO &io, NameAndNumbers& nn) {
665 io.mapRequired("name", nn.name);
666 io.mapRequired("strings", nn.strings);
667 io.mapRequired("single", nn.single);
668 io.mapRequired("numbers", nn.numbers);
676 // Test writing then reading back custom values
678 TEST(YAMLIO, TestReadWriteMyFlowSequence) {
679 std::string intermediate;
683 map.strings.push_back(llvm::StringRef("one"));
684 map.strings.push_back(llvm::StringRef("two"));
685 map.single.push_back(1);
686 map.numbers.push_back(10);
687 map.numbers.push_back(-30);
688 map.numbers.push_back(1024);
690 llvm::raw_string_ostream ostr(intermediate);
694 // Verify sequences were written in flow style
696 llvm::StringRef flowOut(intermediate);
697 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
698 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
702 Input yin(intermediate);
706 EXPECT_FALSE(yin.error());
707 EXPECT_TRUE(map2.name.equals("hello"));
708 EXPECT_EQ(map2.strings.size(), 2UL);
709 EXPECT_TRUE(map2.strings[0].equals("one"));
710 EXPECT_TRUE(map2.strings[1].equals("two"));
711 EXPECT_EQ(map2.single.size(), 1UL);
712 EXPECT_EQ(1, map2.single[0]);
713 EXPECT_EQ(map2.numbers.size(), 3UL);
714 EXPECT_EQ(10, map2.numbers[0]);
715 EXPECT_EQ(-30, map2.numbers[1]);
716 EXPECT_EQ(1024, map2.numbers[2]);
721 //===----------------------------------------------------------------------===//
722 // Test normalizing/denormalizing
723 //===----------------------------------------------------------------------===//
725 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
727 typedef std::vector<TotalSeconds> SecondsSequence;
729 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
735 struct MappingTraits<TotalSeconds> {
737 class NormalizedSeconds {
739 NormalizedSeconds(IO &io)
740 : hours(0), minutes(0), seconds(0) {
742 NormalizedSeconds(IO &, TotalSeconds &secs)
744 minutes((secs - (hours*3600))/60),
747 TotalSeconds denormalize(IO &) {
748 return TotalSeconds(hours*3600 + minutes*60 + seconds);
756 static void mapping(IO &io, TotalSeconds &secs) {
757 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
759 io.mapOptional("hours", keys->hours, (uint32_t)0);
760 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
761 io.mapRequired("seconds", keys->seconds);
769 // Test the reading of a yaml sequence of mappings
771 TEST(YAMLIO, TestReadMySecondsSequence) {
773 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
776 EXPECT_FALSE(yin.error());
777 EXPECT_EQ(seq.size(), 2UL);
778 EXPECT_EQ(seq[0], 3605U);
779 EXPECT_EQ(seq[1], 59U);
784 // Test writing then reading back custom values
786 TEST(YAMLIO, TestReadWriteMySecondsSequence) {
787 std::string intermediate;
794 llvm::raw_string_ostream ostr(intermediate);
799 Input yin(intermediate);
800 SecondsSequence seq2;
803 EXPECT_FALSE(yin.error());
804 EXPECT_EQ(seq2.size(), 3UL);
805 EXPECT_EQ(seq2[0], 4000U);
806 EXPECT_EQ(seq2[1], 500U);
807 EXPECT_EQ(seq2[2], 59U);
812 //===----------------------------------------------------------------------===//
813 // Test dynamic typing
814 //===----------------------------------------------------------------------===//
833 struct KindAndFlags {
834 KindAndFlags() : kind(kindA), flags(0) { }
835 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
840 typedef std::vector<KindAndFlags> KindAndFlagsSequence;
842 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
847 struct ScalarEnumerationTraits<AFlags> {
848 static void enumeration(IO &io, AFlags &value) {
849 io.enumCase(value, "a1", a1);
850 io.enumCase(value, "a2", a2);
851 io.enumCase(value, "a3", a3);
855 struct ScalarEnumerationTraits<BFlags> {
856 static void enumeration(IO &io, BFlags &value) {
857 io.enumCase(value, "b1", b1);
858 io.enumCase(value, "b2", b2);
859 io.enumCase(value, "b3", b3);
863 struct ScalarEnumerationTraits<Kind> {
864 static void enumeration(IO &io, Kind &value) {
865 io.enumCase(value, "A", kindA);
866 io.enumCase(value, "B", kindB);
870 struct MappingTraits<KindAndFlags> {
871 static void mapping(IO &io, KindAndFlags& kf) {
872 io.mapRequired("kind", kf.kind);
873 // Type of "flags" field varies depending on "kind" field.
874 // Use memcpy here to avoid breaking strict aliasing rules.
875 if (kf.kind == kindA) {
876 AFlags aflags = static_cast<AFlags>(kf.flags);
877 io.mapRequired("flags", aflags);
880 BFlags bflags = static_cast<BFlags>(kf.flags);
881 io.mapRequired("flags", bflags);
891 // Test the reading of a yaml sequence dynamic types
893 TEST(YAMLIO, TestReadKindAndFlagsSequence) {
894 KindAndFlagsSequence seq;
895 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
898 EXPECT_FALSE(yin.error());
899 EXPECT_EQ(seq.size(), 2UL);
900 EXPECT_EQ(seq[0].kind, kindA);
901 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
902 EXPECT_EQ(seq[1].kind, kindB);
903 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
907 // Test writing then reading back dynamic types
909 TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
910 std::string intermediate;
912 KindAndFlagsSequence seq;
913 seq.push_back(KindAndFlags(kindA,a1));
914 seq.push_back(KindAndFlags(kindB,b1));
915 seq.push_back(KindAndFlags(kindA,a2));
916 seq.push_back(KindAndFlags(kindB,b2));
917 seq.push_back(KindAndFlags(kindA,a3));
919 llvm::raw_string_ostream ostr(intermediate);
924 Input yin(intermediate);
925 KindAndFlagsSequence seq2;
928 EXPECT_FALSE(yin.error());
929 EXPECT_EQ(seq2.size(), 5UL);
930 EXPECT_EQ(seq2[0].kind, kindA);
931 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
932 EXPECT_EQ(seq2[1].kind, kindB);
933 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
934 EXPECT_EQ(seq2[2].kind, kindA);
935 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
936 EXPECT_EQ(seq2[3].kind, kindB);
937 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
938 EXPECT_EQ(seq2[4].kind, kindA);
939 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
944 //===----------------------------------------------------------------------===//
945 // Test document list
946 //===----------------------------------------------------------------------===//
952 typedef std::vector<FooBarMap> FooBarMapDocumentList;
954 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
960 struct MappingTraits<FooBarMap> {
961 static void mapping(IO &io, FooBarMap& fb) {
962 io.mapRequired("foo", fb.foo);
963 io.mapRequired("bar", fb.bar);
971 // Test the reading of a yaml mapping
973 TEST(YAMLIO, TestDocRead) {
975 Input yin("---\nfoo: 3\nbar: 5\n...\n");
978 EXPECT_FALSE(yin.error());
979 EXPECT_EQ(doc.foo, 3);
980 EXPECT_EQ(doc.bar,5);
986 // Test writing then reading back a sequence of mappings
988 TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
989 std::string intermediate;
997 std::vector<FooBarMap> docList;
998 docList.push_back(doc1);
999 docList.push_back(doc2);
1001 llvm::raw_string_ostream ostr(intermediate);
1008 Input yin(intermediate);
1009 std::vector<FooBarMap> docList2;
1012 EXPECT_FALSE(yin.error());
1013 EXPECT_EQ(docList2.size(), 2UL);
1014 FooBarMap& map1 = docList2[0];
1015 FooBarMap& map2 = docList2[1];
1016 EXPECT_EQ(map1.foo, 10);
1017 EXPECT_EQ(map1.bar, -3);
1018 EXPECT_EQ(map2.foo, 257);
1019 EXPECT_EQ(map2.bar, 0);
1024 //===----------------------------------------------------------------------===//
1025 // Test error handling
1026 //===----------------------------------------------------------------------===//
1030 static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
1035 // Test error handling of unknown enumerated scalar
1037 TEST(YAMLIO, TestColorsReadError) {
1044 yin.setDiagHandler(suppressErrorMessages);
1046 EXPECT_TRUE(yin.error());
1051 // Test error handling of flow sequence with unknown value
1053 TEST(YAMLIO, TestFlagsReadError) {
1057 "f2: [ round, hollow ]\n"
1060 yin.setDiagHandler(suppressErrorMessages);
1063 EXPECT_TRUE(yin.error());
1068 // Test error handling reading built-in uint8_t type
1070 LLVM_YAML_IS_SEQUENCE_VECTOR(uint8_t)
1071 TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1072 std::vector<uint8_t> seq;
1078 yin.setDiagHandler(suppressErrorMessages);
1081 EXPECT_TRUE(yin.error());
1086 // Test error handling reading built-in uint16_t type
1088 LLVM_YAML_IS_SEQUENCE_VECTOR(uint16_t)
1089 TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1090 std::vector<uint16_t> seq;
1096 yin.setDiagHandler(suppressErrorMessages);
1099 EXPECT_TRUE(yin.error());
1104 // Test error handling reading built-in uint32_t type
1106 LLVM_YAML_IS_SEQUENCE_VECTOR(uint32_t)
1107 TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1108 std::vector<uint32_t> seq;
1114 yin.setDiagHandler(suppressErrorMessages);
1117 EXPECT_TRUE(yin.error());
1122 // Test error handling reading built-in uint64_t type
1124 LLVM_YAML_IS_SEQUENCE_VECTOR(uint64_t)
1125 TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1126 std::vector<uint64_t> seq;
1128 "- 18446744073709551615\n"
1130 "- 19446744073709551615\n"
1132 yin.setDiagHandler(suppressErrorMessages);
1135 EXPECT_TRUE(yin.error());
1140 // Test error handling reading built-in int8_t type
1142 LLVM_YAML_IS_SEQUENCE_VECTOR(int8_t)
1143 TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1144 std::vector<int8_t> seq;
1151 yin.setDiagHandler(suppressErrorMessages);
1154 EXPECT_TRUE(yin.error());
1158 // Test error handling reading built-in int8_t type
1160 TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1161 std::vector<int8_t> seq;
1168 yin.setDiagHandler(suppressErrorMessages);
1171 EXPECT_TRUE(yin.error());
1176 // Test error handling reading built-in int16_t type
1178 LLVM_YAML_IS_SEQUENCE_VECTOR(int16_t)
1179 TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1180 std::vector<int16_t> seq;
1187 yin.setDiagHandler(suppressErrorMessages);
1190 EXPECT_TRUE(yin.error());
1195 // Test error handling reading built-in int16_t type
1197 TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1198 std::vector<int16_t> seq;
1205 yin.setDiagHandler(suppressErrorMessages);
1208 EXPECT_TRUE(yin.error());
1213 // Test error handling reading built-in int32_t type
1215 LLVM_YAML_IS_SEQUENCE_VECTOR(int32_t)
1216 TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1217 std::vector<int32_t> seq;
1224 yin.setDiagHandler(suppressErrorMessages);
1227 EXPECT_TRUE(yin.error());
1231 // Test error handling reading built-in int32_t type
1233 TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1234 std::vector<int32_t> seq;
1241 yin.setDiagHandler(suppressErrorMessages);
1244 EXPECT_TRUE(yin.error());
1249 // Test error handling reading built-in int64_t type
1251 LLVM_YAML_IS_SEQUENCE_VECTOR(int64_t)
1252 TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1253 std::vector<int64_t> seq;
1255 "- -9223372036854775808\n"
1257 "- 9223372036854775807\n"
1258 "- -9223372036854775809\n"
1260 yin.setDiagHandler(suppressErrorMessages);
1263 EXPECT_TRUE(yin.error());
1267 // Test error handling reading built-in int64_t type
1269 TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1270 std::vector<int64_t> seq;
1272 "- -9223372036854775808\n"
1274 "- 9223372036854775807\n"
1275 "- 9223372036854775809\n"
1277 yin.setDiagHandler(suppressErrorMessages);
1280 EXPECT_TRUE(yin.error());
1284 // Test error handling reading built-in float type
1286 LLVM_YAML_IS_SEQUENCE_VECTOR(float)
1287 TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1288 std::vector<float> seq;
1295 yin.setDiagHandler(suppressErrorMessages);
1298 EXPECT_TRUE(yin.error());
1302 // Test error handling reading built-in float type
1304 LLVM_YAML_IS_SEQUENCE_VECTOR(double)
1305 TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
1306 std::vector<double> seq;
1313 yin.setDiagHandler(suppressErrorMessages);
1316 EXPECT_TRUE(yin.error());
1320 // Test error handling reading built-in Hex8 type
1322 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
1323 TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
1324 std::vector<Hex8> seq;
1330 yin.setDiagHandler(suppressErrorMessages);
1333 EXPECT_TRUE(yin.error());
1338 // Test error handling reading built-in Hex16 type
1340 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
1341 TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
1342 std::vector<Hex16> seq;
1348 yin.setDiagHandler(suppressErrorMessages);
1351 EXPECT_TRUE(yin.error());
1355 // Test error handling reading built-in Hex32 type
1357 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
1358 TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
1359 std::vector<Hex32> seq;
1365 yin.setDiagHandler(suppressErrorMessages);
1368 EXPECT_TRUE(yin.error());
1372 // Test error handling reading built-in Hex64 type
1374 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
1375 TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
1376 std::vector<Hex64> seq;
1379 "- 0xFFEEDDCCBBAA9988\n"
1380 "- 0x12345567890ABCDEF0\n"
1382 yin.setDiagHandler(suppressErrorMessages);
1385 EXPECT_TRUE(yin.error());
1388 struct OptionalTest {
1389 std::vector<int> Numbers;
1392 struct OptionalTestSeq {
1393 std::vector<OptionalTest> Tests;
1396 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
1400 struct MappingTraits<OptionalTest> {
1401 static void mapping(IO& IO, OptionalTest &OT) {
1402 IO.mapOptional("Numbers", OT.Numbers);
1407 struct MappingTraits<OptionalTestSeq> {
1408 static void mapping(IO &IO, OptionalTestSeq &OTS) {
1409 IO.mapOptional("Tests", OTS.Tests);
1415 TEST(YAMLIO, SequenceElideTest) {
1416 // Test that writing out a purely optional structure with its fields set to
1417 // default followed by other data is properly read back in.
1418 OptionalTestSeq Seq;
1419 OptionalTest One, Two, Three, Four;
1420 int N[] = {1, 2, 3};
1421 Three.Numbers.assign(N, N + 3);
1422 Seq.Tests.push_back(One);
1423 Seq.Tests.push_back(Two);
1424 Seq.Tests.push_back(Three);
1425 Seq.Tests.push_back(Four);
1427 std::string intermediate;
1429 llvm::raw_string_ostream ostr(intermediate);
1434 Input yin(intermediate);
1435 OptionalTestSeq Seq2;
1438 EXPECT_FALSE(yin.error());
1440 EXPECT_EQ(4UL, Seq2.Tests.size());
1442 EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
1443 EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
1445 EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
1446 EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
1447 EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
1449 EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());