| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196 |
- //===- unittest/Support/YAMLIOTest.cpp ------------------------------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is distributed under the University of Illinois Open Source
- // License. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- #include "llvm/ADT/SmallString.h"
- #include "llvm/ADT/Twine.h"
- #include "llvm/Support/Casting.h"
- #include "llvm/Support/Format.h"
- #include "llvm/Support/YAMLTraits.h"
- #include "gtest/gtest.h"
- using llvm::yaml::Input;
- using llvm::yaml::Output;
- using llvm::yaml::IO;
- using llvm::yaml::MappingTraits;
- using llvm::yaml::MappingNormalization;
- using llvm::yaml::ScalarTraits;
- using llvm::yaml::Hex8;
- using llvm::yaml::Hex16;
- using llvm::yaml::Hex32;
- using llvm::yaml::Hex64;
- static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
- }
- //===----------------------------------------------------------------------===//
- // Test MappingTraits
- //===----------------------------------------------------------------------===//
- struct FooBar {
- int foo;
- int bar;
- };
- typedef std::vector<FooBar> FooBarSequence;
- LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar)
- struct FooBarContainer {
- FooBarSequence fbs;
- };
- namespace llvm {
- namespace yaml {
- template <>
- struct MappingTraits<FooBar> {
- static void mapping(IO &io, FooBar& fb) {
- io.mapRequired("foo", fb.foo);
- io.mapRequired("bar", fb.bar);
- }
- };
- template <> struct MappingTraits<FooBarContainer> {
- static void mapping(IO &io, FooBarContainer &fb) {
- io.mapRequired("fbs", fb.fbs);
- }
- };
- }
- }
- //
- // Test the reading of a yaml mapping
- //
- TEST(YAMLIO, TestMapRead) {
- FooBar doc;
- {
- Input yin("---\nfoo: 3\nbar: 5\n...\n");
- yin >> doc;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(doc.foo, 3);
- EXPECT_EQ(doc.bar, 5);
- }
- {
- Input yin("{foo: 3, bar: 5}");
- yin >> doc;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(doc.foo, 3);
- EXPECT_EQ(doc.bar, 5);
- }
- }
- TEST(YAMLIO, TestMalformedMapRead) {
- FooBar doc;
- Input yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages);
- yin >> doc;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test the reading of a yaml sequence of mappings
- //
- TEST(YAMLIO, TestSequenceMapRead) {
- FooBarSequence seq;
- Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
- yin >> seq;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(seq.size(), 2UL);
- FooBar& map1 = seq[0];
- FooBar& map2 = seq[1];
- EXPECT_EQ(map1.foo, 3);
- EXPECT_EQ(map1.bar, 5);
- EXPECT_EQ(map2.foo, 7);
- EXPECT_EQ(map2.bar, 9);
- }
- //
- // Test the reading of a map containing a yaml sequence of mappings
- //
- TEST(YAMLIO, TestContainerSequenceMapRead) {
- {
- FooBarContainer cont;
- Input yin2("---\nfbs:\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
- yin2 >> cont;
- EXPECT_FALSE(yin2.error());
- EXPECT_EQ(cont.fbs.size(), 2UL);
- EXPECT_EQ(cont.fbs[0].foo, 3);
- EXPECT_EQ(cont.fbs[0].bar, 5);
- EXPECT_EQ(cont.fbs[1].foo, 7);
- EXPECT_EQ(cont.fbs[1].bar, 9);
- }
- {
- FooBarContainer cont;
- Input yin("---\nfbs:\n...\n");
- yin >> cont;
- // Okay: Empty node represents an empty array.
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(cont.fbs.size(), 0UL);
- }
- {
- FooBarContainer cont;
- Input yin("---\nfbs: !!null null\n...\n");
- yin >> cont;
- // Okay: null represents an empty array.
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(cont.fbs.size(), 0UL);
- }
- {
- FooBarContainer cont;
- Input yin("---\nfbs: ~\n...\n");
- yin >> cont;
- // Okay: null represents an empty array.
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(cont.fbs.size(), 0UL);
- }
- {
- FooBarContainer cont;
- Input yin("---\nfbs: null\n...\n");
- yin >> cont;
- // Okay: null represents an empty array.
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(cont.fbs.size(), 0UL);
- }
- }
- //
- // Test the reading of a map containing a malformed yaml sequence
- //
- TEST(YAMLIO, TestMalformedContainerSequenceMapRead) {
- {
- FooBarContainer cont;
- Input yin("---\nfbs:\n foo: 3\n bar: 5\n...\n", nullptr,
- suppressErrorMessages);
- yin >> cont;
- // Error: fbs is not a sequence.
- EXPECT_TRUE(!!yin.error());
- EXPECT_EQ(cont.fbs.size(), 0UL);
- }
- {
- FooBarContainer cont;
- Input yin("---\nfbs: 'scalar'\n...\n", nullptr, suppressErrorMessages);
- yin >> cont;
- // This should be an error.
- EXPECT_TRUE(!!yin.error());
- EXPECT_EQ(cont.fbs.size(), 0UL);
- }
- }
- //
- // Test writing then reading back a sequence of mappings
- //
- TEST(YAMLIO, TestSequenceMapWriteAndRead) {
- std::string intermediate;
- {
- FooBar entry1;
- entry1.foo = 10;
- entry1.bar = -3;
- FooBar entry2;
- entry2.foo = 257;
- entry2.bar = 0;
- FooBarSequence seq;
- seq.push_back(entry1);
- seq.push_back(entry2);
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << seq;
- }
- {
- Input yin(intermediate);
- FooBarSequence seq2;
- yin >> seq2;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(seq2.size(), 2UL);
- FooBar& map1 = seq2[0];
- FooBar& map2 = seq2[1];
- EXPECT_EQ(map1.foo, 10);
- EXPECT_EQ(map1.bar, -3);
- EXPECT_EQ(map2.foo, 257);
- EXPECT_EQ(map2.bar, 0);
- }
- }
- //===----------------------------------------------------------------------===//
- // Test built-in types
- //===----------------------------------------------------------------------===//
- struct BuiltInTypes {
- llvm::StringRef str;
- std::string stdstr;
- uint64_t u64;
- uint32_t u32;
- uint16_t u16;
- uint8_t u8;
- bool b;
- int64_t s64;
- int32_t s32;
- int16_t s16;
- int8_t s8;
- float f;
- double d;
- Hex8 h8;
- Hex16 h16;
- Hex32 h32;
- Hex64 h64;
- };
- namespace llvm {
- namespace yaml {
- template <>
- struct MappingTraits<BuiltInTypes> {
- static void mapping(IO &io, BuiltInTypes& bt) {
- io.mapRequired("str", bt.str);
- io.mapRequired("stdstr", bt.stdstr);
- io.mapRequired("u64", bt.u64);
- io.mapRequired("u32", bt.u32);
- io.mapRequired("u16", bt.u16);
- io.mapRequired("u8", bt.u8);
- io.mapRequired("b", bt.b);
- io.mapRequired("s64", bt.s64);
- io.mapRequired("s32", bt.s32);
- io.mapRequired("s16", bt.s16);
- io.mapRequired("s8", bt.s8);
- io.mapRequired("f", bt.f);
- io.mapRequired("d", bt.d);
- io.mapRequired("h8", bt.h8);
- io.mapRequired("h16", bt.h16);
- io.mapRequired("h32", bt.h32);
- io.mapRequired("h64", bt.h64);
- }
- };
- }
- }
- //
- // Test the reading of all built-in scalar conversions
- //
- TEST(YAMLIO, TestReadBuiltInTypes) {
- BuiltInTypes map;
- Input yin("---\n"
- "str: hello there\n"
- "stdstr: hello where?\n"
- "u64: 5000000000\n"
- "u32: 4000000000\n"
- "u16: 65000\n"
- "u8: 255\n"
- "b: false\n"
- "s64: -5000000000\n"
- "s32: -2000000000\n"
- "s16: -32000\n"
- "s8: -127\n"
- "f: 137.125\n"
- "d: -2.8625\n"
- "h8: 0xFF\n"
- "h16: 0x8765\n"
- "h32: 0xFEDCBA98\n"
- "h64: 0xFEDCBA9876543210\n"
- "...\n");
- yin >> map;
- EXPECT_FALSE(yin.error());
- EXPECT_TRUE(map.str.equals("hello there"));
- EXPECT_TRUE(map.stdstr == "hello where?");
- EXPECT_EQ(map.u64, 5000000000ULL);
- EXPECT_EQ(map.u32, 4000000000U);
- EXPECT_EQ(map.u16, 65000);
- EXPECT_EQ(map.u8, 255);
- EXPECT_EQ(map.b, false);
- EXPECT_EQ(map.s64, -5000000000LL);
- EXPECT_EQ(map.s32, -2000000000L);
- EXPECT_EQ(map.s16, -32000);
- EXPECT_EQ(map.s8, -127);
- EXPECT_EQ(map.f, 137.125);
- EXPECT_EQ(map.d, -2.8625);
- EXPECT_EQ(map.h8, Hex8(255));
- EXPECT_EQ(map.h16, Hex16(0x8765));
- EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
- EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
- }
- //
- // Test writing then reading back all built-in scalar types
- //
- TEST(YAMLIO, TestReadWriteBuiltInTypes) {
- std::string intermediate;
- {
- BuiltInTypes map;
- map.str = "one two";
- map.stdstr = "three four";
- map.u64 = 6000000000ULL;
- map.u32 = 3000000000U;
- map.u16 = 50000;
- map.u8 = 254;
- map.b = true;
- map.s64 = -6000000000LL;
- map.s32 = -2000000000;
- map.s16 = -32000;
- map.s8 = -128;
- map.f = 3.25;
- map.d = -2.8625;
- map.h8 = 254;
- map.h16 = 50000;
- map.h32 = 3000000000U;
- map.h64 = 6000000000LL;
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << map;
- }
- {
- Input yin(intermediate);
- BuiltInTypes map;
- yin >> map;
- EXPECT_FALSE(yin.error());
- EXPECT_TRUE(map.str.equals("one two"));
- EXPECT_TRUE(map.stdstr == "three four");
- EXPECT_EQ(map.u64, 6000000000ULL);
- EXPECT_EQ(map.u32, 3000000000U);
- EXPECT_EQ(map.u16, 50000);
- EXPECT_EQ(map.u8, 254);
- EXPECT_EQ(map.b, true);
- EXPECT_EQ(map.s64, -6000000000LL);
- EXPECT_EQ(map.s32, -2000000000L);
- EXPECT_EQ(map.s16, -32000);
- EXPECT_EQ(map.s8, -128);
- EXPECT_EQ(map.f, 3.25);
- EXPECT_EQ(map.d, -2.8625);
- EXPECT_EQ(map.h8, Hex8(254));
- EXPECT_EQ(map.h16, Hex16(50000));
- EXPECT_EQ(map.h32, Hex32(3000000000U));
- EXPECT_EQ(map.h64, Hex64(6000000000LL));
- }
- }
- struct StringTypes {
- llvm::StringRef str1;
- llvm::StringRef str2;
- llvm::StringRef str3;
- llvm::StringRef str4;
- llvm::StringRef str5;
- llvm::StringRef str6;
- llvm::StringRef str7;
- llvm::StringRef str8;
- llvm::StringRef str9;
- llvm::StringRef str10;
- llvm::StringRef str11;
- std::string stdstr1;
- std::string stdstr2;
- std::string stdstr3;
- std::string stdstr4;
- std::string stdstr5;
- std::string stdstr6;
- std::string stdstr7;
- std::string stdstr8;
- std::string stdstr9;
- std::string stdstr10;
- std::string stdstr11;
- };
- namespace llvm {
- namespace yaml {
- template <>
- struct MappingTraits<StringTypes> {
- static void mapping(IO &io, StringTypes& st) {
- io.mapRequired("str1", st.str1);
- io.mapRequired("str2", st.str2);
- io.mapRequired("str3", st.str3);
- io.mapRequired("str4", st.str4);
- io.mapRequired("str5", st.str5);
- io.mapRequired("str6", st.str6);
- io.mapRequired("str7", st.str7);
- io.mapRequired("str8", st.str8);
- io.mapRequired("str9", st.str9);
- io.mapRequired("str10", st.str10);
- io.mapRequired("str11", st.str11);
- io.mapRequired("stdstr1", st.stdstr1);
- io.mapRequired("stdstr2", st.stdstr2);
- io.mapRequired("stdstr3", st.stdstr3);
- io.mapRequired("stdstr4", st.stdstr4);
- io.mapRequired("stdstr5", st.stdstr5);
- io.mapRequired("stdstr6", st.stdstr6);
- io.mapRequired("stdstr7", st.stdstr7);
- io.mapRequired("stdstr8", st.stdstr8);
- io.mapRequired("stdstr9", st.stdstr9);
- io.mapRequired("stdstr10", st.stdstr10);
- io.mapRequired("stdstr11", st.stdstr11);
- }
- };
- }
- }
- TEST(YAMLIO, TestReadWriteStringTypes) {
- std::string intermediate;
- {
- StringTypes map;
- map.str1 = "'aaa";
- map.str2 = "\"bbb";
- map.str3 = "`ccc";
- map.str4 = "@ddd";
- map.str5 = "";
- map.str6 = "0000000004000000";
- map.str7 = "true";
- map.str8 = "FALSE";
- map.str9 = "~";
- map.str10 = "0.2e20";
- map.str11 = "0x30";
- map.stdstr1 = "'eee";
- map.stdstr2 = "\"fff";
- map.stdstr3 = "`ggg";
- map.stdstr4 = "@hhh";
- map.stdstr5 = "";
- map.stdstr6 = "0000000004000000";
- map.stdstr7 = "true";
- map.stdstr8 = "FALSE";
- map.stdstr9 = "~";
- map.stdstr10 = "0.2e20";
- map.stdstr11 = "0x30";
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << map;
- }
- llvm::StringRef flowOut(intermediate);
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0000000004000000'\n"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("'true'\n"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("'FALSE'\n"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("'~'\n"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0.2e20'\n"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0x30'\n"));
- EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
- EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
- EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
- EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
- EXPECT_NE(std::string::npos, flowOut.find("''\n"));
- EXPECT_NE(std::string::npos, flowOut.find("'0000000004000000'\n"));
- {
- Input yin(intermediate);
- StringTypes map;
- yin >> map;
- EXPECT_FALSE(yin.error());
- EXPECT_TRUE(map.str1.equals("'aaa"));
- EXPECT_TRUE(map.str2.equals("\"bbb"));
- EXPECT_TRUE(map.str3.equals("`ccc"));
- EXPECT_TRUE(map.str4.equals("@ddd"));
- EXPECT_TRUE(map.str5.equals(""));
- EXPECT_TRUE(map.str6.equals("0000000004000000"));
- EXPECT_TRUE(map.stdstr1 == "'eee");
- EXPECT_TRUE(map.stdstr2 == "\"fff");
- EXPECT_TRUE(map.stdstr3 == "`ggg");
- EXPECT_TRUE(map.stdstr4 == "@hhh");
- EXPECT_TRUE(map.stdstr5 == "");
- EXPECT_TRUE(map.stdstr6 == "0000000004000000");
- }
- }
- //===----------------------------------------------------------------------===//
- // Test ScalarEnumerationTraits
- //===----------------------------------------------------------------------===//
- enum Colors {
- cRed,
- cBlue,
- cGreen,
- cYellow
- };
- struct ColorMap {
- Colors c1;
- Colors c2;
- Colors c3;
- Colors c4;
- Colors c5;
- Colors c6;
- };
- namespace llvm {
- namespace yaml {
- template <>
- struct ScalarEnumerationTraits<Colors> {
- static void enumeration(IO &io, Colors &value) {
- io.enumCase(value, "red", cRed);
- io.enumCase(value, "blue", cBlue);
- io.enumCase(value, "green", cGreen);
- io.enumCase(value, "yellow",cYellow);
- }
- };
- template <>
- struct MappingTraits<ColorMap> {
- static void mapping(IO &io, ColorMap& c) {
- io.mapRequired("c1", c.c1);
- io.mapRequired("c2", c.c2);
- io.mapRequired("c3", c.c3);
- io.mapOptional("c4", c.c4, cBlue); // supplies default
- io.mapOptional("c5", c.c5, cYellow); // supplies default
- io.mapOptional("c6", c.c6, cRed); // supplies default
- }
- };
- }
- }
- //
- // Test reading enumerated scalars
- //
- TEST(YAMLIO, TestEnumRead) {
- ColorMap map;
- Input yin("---\n"
- "c1: blue\n"
- "c2: red\n"
- "c3: green\n"
- "c5: yellow\n"
- "...\n");
- yin >> map;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(cBlue, map.c1);
- EXPECT_EQ(cRed, map.c2);
- EXPECT_EQ(cGreen, map.c3);
- EXPECT_EQ(cBlue, map.c4); // tests default
- EXPECT_EQ(cYellow,map.c5); // tests overridden
- EXPECT_EQ(cRed, map.c6); // tests default
- }
- //===----------------------------------------------------------------------===//
- // Test ScalarBitSetTraits
- //===----------------------------------------------------------------------===//
- enum MyFlags {
- flagNone = 0,
- flagBig = 1 << 0,
- flagFlat = 1 << 1,
- flagRound = 1 << 2,
- flagPointy = 1 << 3
- };
- inline MyFlags operator|(MyFlags a, MyFlags b) {
- return static_cast<MyFlags>(
- static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
- }
- struct FlagsMap {
- MyFlags f1;
- MyFlags f2;
- MyFlags f3;
- MyFlags f4;
- };
- namespace llvm {
- namespace yaml {
- template <>
- struct ScalarBitSetTraits<MyFlags> {
- static void bitset(IO &io, MyFlags &value) {
- io.bitSetCase(value, "big", flagBig);
- io.bitSetCase(value, "flat", flagFlat);
- io.bitSetCase(value, "round", flagRound);
- io.bitSetCase(value, "pointy",flagPointy);
- }
- };
- template <>
- struct MappingTraits<FlagsMap> {
- static void mapping(IO &io, FlagsMap& c) {
- io.mapRequired("f1", c.f1);
- io.mapRequired("f2", c.f2);
- io.mapRequired("f3", c.f3);
- io.mapOptional("f4", c.f4, MyFlags(flagRound));
- }
- };
- }
- }
- //
- // Test reading flow sequence representing bit-mask values
- //
- TEST(YAMLIO, TestFlagsRead) {
- FlagsMap map;
- Input yin("---\n"
- "f1: [ big ]\n"
- "f2: [ round, flat ]\n"
- "f3: []\n"
- "...\n");
- yin >> map;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(flagBig, map.f1);
- EXPECT_EQ(flagRound|flagFlat, map.f2);
- EXPECT_EQ(flagNone, map.f3); // check empty set
- EXPECT_EQ(flagRound, map.f4); // check optional key
- }
- //
- // Test writing then reading back bit-mask values
- //
- TEST(YAMLIO, TestReadWriteFlags) {
- std::string intermediate;
- {
- FlagsMap map;
- map.f1 = flagBig;
- map.f2 = flagRound | flagFlat;
- map.f3 = flagNone;
- map.f4 = flagNone;
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << map;
- }
- {
- Input yin(intermediate);
- FlagsMap map2;
- yin >> map2;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(flagBig, map2.f1);
- EXPECT_EQ(flagRound|flagFlat, map2.f2);
- EXPECT_EQ(flagNone, map2.f3);
- //EXPECT_EQ(flagRound, map2.f4); // check optional key
- }
- }
- //===----------------------------------------------------------------------===//
- // Test ScalarTraits
- //===----------------------------------------------------------------------===//
- struct MyCustomType {
- int length;
- int width;
- };
- struct MyCustomTypeMap {
- MyCustomType f1;
- MyCustomType f2;
- int f3;
- };
- namespace llvm {
- namespace yaml {
- template <>
- struct MappingTraits<MyCustomTypeMap> {
- static void mapping(IO &io, MyCustomTypeMap& s) {
- io.mapRequired("f1", s.f1);
- io.mapRequired("f2", s.f2);
- io.mapRequired("f3", s.f3);
- }
- };
- // MyCustomType is formatted as a yaml scalar. A value of
- // {length=3, width=4} would be represented in yaml as "3 by 4".
- template<>
- struct ScalarTraits<MyCustomType> {
- static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
- out << llvm::format("%d by %d", value.length, value.width);
- }
- static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
- size_t byStart = scalar.find("by");
- if ( byStart != StringRef::npos ) {
- StringRef lenStr = scalar.slice(0, byStart);
- lenStr = lenStr.rtrim();
- if ( lenStr.getAsInteger(0, value.length) ) {
- return "malformed length";
- }
- StringRef widthStr = scalar.drop_front(byStart+2);
- widthStr = widthStr.ltrim();
- if ( widthStr.getAsInteger(0, value.width) ) {
- return "malformed width";
- }
- return StringRef();
- }
- else {
- return "malformed by";
- }
- }
- static bool mustQuote(StringRef) { return true; }
- };
- }
- }
- //
- // Test writing then reading back custom values
- //
- TEST(YAMLIO, TestReadWriteMyCustomType) {
- std::string intermediate;
- {
- MyCustomTypeMap map;
- map.f1.length = 1;
- map.f1.width = 4;
- map.f2.length = 100;
- map.f2.width = 400;
- map.f3 = 10;
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << map;
- }
- {
- Input yin(intermediate);
- MyCustomTypeMap map2;
- yin >> map2;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(1, map2.f1.length);
- EXPECT_EQ(4, map2.f1.width);
- EXPECT_EQ(100, map2.f2.length);
- EXPECT_EQ(400, map2.f2.width);
- EXPECT_EQ(10, map2.f3);
- }
- }
- //===----------------------------------------------------------------------===//
- // Test BlockScalarTraits
- //===----------------------------------------------------------------------===//
- struct MultilineStringType {
- std::string str;
- };
- struct MultilineStringTypeMap {
- MultilineStringType name;
- MultilineStringType description;
- MultilineStringType ingredients;
- MultilineStringType recipes;
- MultilineStringType warningLabels;
- MultilineStringType documentation;
- int price;
- };
- namespace llvm {
- namespace yaml {
- template <>
- struct MappingTraits<MultilineStringTypeMap> {
- static void mapping(IO &io, MultilineStringTypeMap& s) {
- io.mapRequired("name", s.name);
- io.mapRequired("description", s.description);
- io.mapRequired("ingredients", s.ingredients);
- io.mapRequired("recipes", s.recipes);
- io.mapRequired("warningLabels", s.warningLabels);
- io.mapRequired("documentation", s.documentation);
- io.mapRequired("price", s.price);
- }
- };
- // MultilineStringType is formatted as a yaml block literal scalar. A value of
- // "Hello\nWorld" would be represented in yaml as
- // |
- // Hello
- // World
- template <>
- struct BlockScalarTraits<MultilineStringType> {
- static void output(const MultilineStringType &value, void *ctxt,
- llvm::raw_ostream &out) {
- out << value.str;
- }
- static StringRef input(StringRef scalar, void *ctxt,
- MultilineStringType &value) {
- value.str = scalar.str();
- return StringRef();
- }
- };
- }
- }
- LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MultilineStringType)
- //
- // Test writing then reading back custom values
- //
- TEST(YAMLIO, TestReadWriteMultilineStringType) {
- std::string intermediate;
- {
- MultilineStringTypeMap map;
- map.name.str = "An Item";
- map.description.str = "Hello\nWorld";
- map.ingredients.str = "SubItem 1\nSub Item 2\n\nSub Item 3\n";
- map.recipes.str = "\n\nTest 1\n\n\n";
- map.warningLabels.str = "";
- map.documentation.str = "\n\n";
- map.price = 350;
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << map;
- }
- {
- Input yin(intermediate);
- MultilineStringTypeMap map2;
- yin >> map2;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(map2.name.str, "An Item\n");
- EXPECT_EQ(map2.description.str, "Hello\nWorld\n");
- EXPECT_EQ(map2.ingredients.str, "SubItem 1\nSub Item 2\n\nSub Item 3\n");
- EXPECT_EQ(map2.recipes.str, "\n\nTest 1\n");
- EXPECT_TRUE(map2.warningLabels.str.empty());
- EXPECT_TRUE(map2.documentation.str.empty());
- EXPECT_EQ(map2.price, 350);
- }
- }
- //
- // Test writing then reading back custom values
- //
- TEST(YAMLIO, TestReadWriteBlockScalarDocuments) {
- std::string intermediate;
- {
- std::vector<MultilineStringType> documents;
- MultilineStringType doc;
- doc.str = "Hello\nWorld";
- documents.push_back(doc);
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << documents;
- // Verify that the block scalar header was written out on the same line
- // as the document marker.
- EXPECT_NE(llvm::StringRef::npos, llvm::StringRef(ostr.str()).find("--- |"));
- }
- {
- Input yin(intermediate);
- std::vector<MultilineStringType> documents2;
- yin >> documents2;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(documents2.size(), size_t(1));
- EXPECT_EQ(documents2[0].str, "Hello\nWorld\n");
- }
- }
- TEST(YAMLIO, TestReadWriteBlockScalarValue) {
- std::string intermediate;
- {
- MultilineStringType doc;
- doc.str = "Just a block\nscalar doc";
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << doc;
- }
- {
- Input yin(intermediate);
- MultilineStringType doc;
- yin >> doc;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(doc.str, "Just a block\nscalar doc\n");
- }
- }
- //===----------------------------------------------------------------------===//
- // Test flow sequences
- //===----------------------------------------------------------------------===//
- LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
- LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
- LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::StringRef)
- namespace llvm {
- namespace yaml {
- template<>
- struct ScalarTraits<MyNumber> {
- static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
- out << value;
- }
- static StringRef input(StringRef scalar, void *, MyNumber &value) {
- long long n;
- if ( getAsSignedInteger(scalar, 0, n) )
- return "invalid number";
- value = n;
- return StringRef();
- }
- static bool mustQuote(StringRef) { return false; }
- };
- }
- }
- struct NameAndNumbers {
- llvm::StringRef name;
- std::vector<llvm::StringRef> strings;
- std::vector<MyNumber> single;
- std::vector<MyNumber> numbers;
- };
- namespace llvm {
- namespace yaml {
- template <>
- struct MappingTraits<NameAndNumbers> {
- static void mapping(IO &io, NameAndNumbers& nn) {
- io.mapRequired("name", nn.name);
- io.mapRequired("strings", nn.strings);
- io.mapRequired("single", nn.single);
- io.mapRequired("numbers", nn.numbers);
- }
- };
- }
- }
- typedef std::vector<MyNumber> MyNumberFlowSequence;
- LLVM_YAML_IS_SEQUENCE_VECTOR(MyNumberFlowSequence)
- struct NameAndNumbersFlow {
- llvm::StringRef name;
- std::vector<MyNumberFlowSequence> sequenceOfNumbers;
- };
- namespace llvm {
- namespace yaml {
- template <>
- struct MappingTraits<NameAndNumbersFlow> {
- static void mapping(IO &io, NameAndNumbersFlow& nn) {
- io.mapRequired("name", nn.name);
- io.mapRequired("sequenceOfNumbers", nn.sequenceOfNumbers);
- }
- };
- }
- }
- //
- // Test writing then reading back custom values
- //
- TEST(YAMLIO, TestReadWriteMyFlowSequence) {
- std::string intermediate;
- {
- NameAndNumbers map;
- map.name = "hello";
- map.strings.push_back(llvm::StringRef("one"));
- map.strings.push_back(llvm::StringRef("two"));
- map.single.push_back(1);
- map.numbers.push_back(10);
- map.numbers.push_back(-30);
- map.numbers.push_back(1024);
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << map;
- // Verify sequences were written in flow style
- ostr.flush();
- llvm::StringRef flowOut(intermediate);
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
- }
- {
- Input yin(intermediate);
- NameAndNumbers map2;
- yin >> map2;
- EXPECT_FALSE(yin.error());
- EXPECT_TRUE(map2.name.equals("hello"));
- EXPECT_EQ(map2.strings.size(), 2UL);
- EXPECT_TRUE(map2.strings[0].equals("one"));
- EXPECT_TRUE(map2.strings[1].equals("two"));
- EXPECT_EQ(map2.single.size(), 1UL);
- EXPECT_EQ(1, map2.single[0]);
- EXPECT_EQ(map2.numbers.size(), 3UL);
- EXPECT_EQ(10, map2.numbers[0]);
- EXPECT_EQ(-30, map2.numbers[1]);
- EXPECT_EQ(1024, map2.numbers[2]);
- }
- }
- //
- // Test writing then reading back a sequence of flow sequences.
- //
- TEST(YAMLIO, TestReadWriteSequenceOfMyFlowSequence) {
- std::string intermediate;
- {
- NameAndNumbersFlow map;
- map.name = "hello";
- MyNumberFlowSequence single = { 0 };
- MyNumberFlowSequence numbers = { 12, 1, -512 };
- map.sequenceOfNumbers.push_back(single);
- map.sequenceOfNumbers.push_back(numbers);
- map.sequenceOfNumbers.push_back(MyNumberFlowSequence());
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << map;
- // Verify sequences were written in flow style
- // and that the parent sequence used '-'.
- ostr.flush();
- llvm::StringRef flowOut(intermediate);
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 0 ]"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 12, 1, -512 ]"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ ]"));
- }
- {
- Input yin(intermediate);
- NameAndNumbersFlow map2;
- yin >> map2;
- EXPECT_FALSE(yin.error());
- EXPECT_TRUE(map2.name.equals("hello"));
- EXPECT_EQ(map2.sequenceOfNumbers.size(), 3UL);
- EXPECT_EQ(map2.sequenceOfNumbers[0].size(), 1UL);
- EXPECT_EQ(0, map2.sequenceOfNumbers[0][0]);
- EXPECT_EQ(map2.sequenceOfNumbers[1].size(), 3UL);
- EXPECT_EQ(12, map2.sequenceOfNumbers[1][0]);
- EXPECT_EQ(1, map2.sequenceOfNumbers[1][1]);
- EXPECT_EQ(-512, map2.sequenceOfNumbers[1][2]);
- EXPECT_TRUE(map2.sequenceOfNumbers[2].empty());
- }
- }
- //===----------------------------------------------------------------------===//
- // Test normalizing/denormalizing
- //===----------------------------------------------------------------------===//
- LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
- typedef std::vector<TotalSeconds> SecondsSequence;
- LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
- namespace llvm {
- namespace yaml {
- template <>
- struct MappingTraits<TotalSeconds> {
- class NormalizedSeconds {
- public:
- NormalizedSeconds(IO &io)
- : hours(0), minutes(0), seconds(0) {
- }
- NormalizedSeconds(IO &, TotalSeconds &secs)
- : hours(secs/3600),
- minutes((secs - (hours*3600))/60),
- seconds(secs % 60) {
- }
- TotalSeconds denormalize(IO &) {
- return TotalSeconds(hours*3600 + minutes*60 + seconds);
- }
- uint32_t hours;
- uint8_t minutes;
- uint8_t seconds;
- };
- static void mapping(IO &io, TotalSeconds &secs) {
- MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
- io.mapOptional("hours", keys->hours, (uint32_t)0);
- io.mapOptional("minutes", keys->minutes, (uint8_t)0);
- io.mapRequired("seconds", keys->seconds);
- }
- };
- }
- }
- //
- // Test the reading of a yaml sequence of mappings
- //
- TEST(YAMLIO, TestReadMySecondsSequence) {
- SecondsSequence seq;
- Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
- yin >> seq;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(seq.size(), 2UL);
- EXPECT_EQ(seq[0], 3605U);
- EXPECT_EQ(seq[1], 59U);
- }
- //
- // Test writing then reading back custom values
- //
- TEST(YAMLIO, TestReadWriteMySecondsSequence) {
- std::string intermediate;
- {
- SecondsSequence seq;
- seq.push_back(4000);
- seq.push_back(500);
- seq.push_back(59);
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << seq;
- }
- {
- Input yin(intermediate);
- SecondsSequence seq2;
- yin >> seq2;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(seq2.size(), 3UL);
- EXPECT_EQ(seq2[0], 4000U);
- EXPECT_EQ(seq2[1], 500U);
- EXPECT_EQ(seq2[2], 59U);
- }
- }
- //===----------------------------------------------------------------------===//
- // Test dynamic typing
- //===----------------------------------------------------------------------===//
- enum AFlags {
- a1,
- a2,
- a3
- };
- enum BFlags {
- b1,
- b2,
- b3
- };
- enum Kind {
- kindA,
- kindB
- };
- struct KindAndFlags {
- KindAndFlags() : kind(kindA), flags(0) { }
- KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
- Kind kind;
- uint32_t flags;
- };
- typedef std::vector<KindAndFlags> KindAndFlagsSequence;
- LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
- namespace llvm {
- namespace yaml {
- template <>
- struct ScalarEnumerationTraits<AFlags> {
- static void enumeration(IO &io, AFlags &value) {
- io.enumCase(value, "a1", a1);
- io.enumCase(value, "a2", a2);
- io.enumCase(value, "a3", a3);
- }
- };
- template <>
- struct ScalarEnumerationTraits<BFlags> {
- static void enumeration(IO &io, BFlags &value) {
- io.enumCase(value, "b1", b1);
- io.enumCase(value, "b2", b2);
- io.enumCase(value, "b3", b3);
- }
- };
- template <>
- struct ScalarEnumerationTraits<Kind> {
- static void enumeration(IO &io, Kind &value) {
- io.enumCase(value, "A", kindA);
- io.enumCase(value, "B", kindB);
- }
- };
- template <>
- struct MappingTraits<KindAndFlags> {
- static void mapping(IO &io, KindAndFlags& kf) {
- io.mapRequired("kind", kf.kind);
- // Type of "flags" field varies depending on "kind" field.
- // Use memcpy here to avoid breaking strict aliasing rules.
- if (kf.kind == kindA) {
- AFlags aflags = static_cast<AFlags>(kf.flags);
- io.mapRequired("flags", aflags);
- kf.flags = aflags;
- } else {
- BFlags bflags = static_cast<BFlags>(kf.flags);
- io.mapRequired("flags", bflags);
- kf.flags = bflags;
- }
- }
- };
- }
- }
- //
- // Test the reading of a yaml sequence dynamic types
- //
- TEST(YAMLIO, TestReadKindAndFlagsSequence) {
- KindAndFlagsSequence seq;
- Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
- yin >> seq;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(seq.size(), 2UL);
- EXPECT_EQ(seq[0].kind, kindA);
- EXPECT_EQ(seq[0].flags, (uint32_t)a2);
- EXPECT_EQ(seq[1].kind, kindB);
- EXPECT_EQ(seq[1].flags, (uint32_t)b1);
- }
- //
- // Test writing then reading back dynamic types
- //
- TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
- std::string intermediate;
- {
- KindAndFlagsSequence seq;
- seq.push_back(KindAndFlags(kindA,a1));
- seq.push_back(KindAndFlags(kindB,b1));
- seq.push_back(KindAndFlags(kindA,a2));
- seq.push_back(KindAndFlags(kindB,b2));
- seq.push_back(KindAndFlags(kindA,a3));
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << seq;
- }
- {
- Input yin(intermediate);
- KindAndFlagsSequence seq2;
- yin >> seq2;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(seq2.size(), 5UL);
- EXPECT_EQ(seq2[0].kind, kindA);
- EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
- EXPECT_EQ(seq2[1].kind, kindB);
- EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
- EXPECT_EQ(seq2[2].kind, kindA);
- EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
- EXPECT_EQ(seq2[3].kind, kindB);
- EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
- EXPECT_EQ(seq2[4].kind, kindA);
- EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
- }
- }
- //===----------------------------------------------------------------------===//
- // Test document list
- //===----------------------------------------------------------------------===//
- struct FooBarMap {
- int foo;
- int bar;
- };
- typedef std::vector<FooBarMap> FooBarMapDocumentList;
- LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
- namespace llvm {
- namespace yaml {
- template <>
- struct MappingTraits<FooBarMap> {
- static void mapping(IO &io, FooBarMap& fb) {
- io.mapRequired("foo", fb.foo);
- io.mapRequired("bar", fb.bar);
- }
- };
- }
- }
- //
- // Test the reading of a yaml mapping
- //
- TEST(YAMLIO, TestDocRead) {
- FooBarMap doc;
- Input yin("---\nfoo: 3\nbar: 5\n...\n");
- yin >> doc;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(doc.foo, 3);
- EXPECT_EQ(doc.bar,5);
- }
- //
- // Test writing then reading back a sequence of mappings
- //
- TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
- std::string intermediate;
- {
- FooBarMap doc1;
- doc1.foo = 10;
- doc1.bar = -3;
- FooBarMap doc2;
- doc2.foo = 257;
- doc2.bar = 0;
- std::vector<FooBarMap> docList;
- docList.push_back(doc1);
- docList.push_back(doc2);
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << docList;
- }
- {
- Input yin(intermediate);
- std::vector<FooBarMap> docList2;
- yin >> docList2;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(docList2.size(), 2UL);
- FooBarMap& map1 = docList2[0];
- FooBarMap& map2 = docList2[1];
- EXPECT_EQ(map1.foo, 10);
- EXPECT_EQ(map1.bar, -3);
- EXPECT_EQ(map2.foo, 257);
- EXPECT_EQ(map2.bar, 0);
- }
- }
- //===----------------------------------------------------------------------===//
- // Test document tags
- //===----------------------------------------------------------------------===//
- struct MyDouble {
- MyDouble() : value(0.0) { }
- MyDouble(double x) : value(x) { }
- double value;
- };
- LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble)
- namespace llvm {
- namespace yaml {
- template <>
- struct MappingTraits<MyDouble> {
- static void mapping(IO &io, MyDouble &d) {
- if (io.mapTag("!decimal", true)) {
- mappingDecimal(io, d);
- } else if (io.mapTag("!fraction")) {
- mappingFraction(io, d);
- }
- }
- static void mappingDecimal(IO &io, MyDouble &d) {
- io.mapRequired("value", d.value);
- }
- static void mappingFraction(IO &io, MyDouble &d) {
- double num, denom;
- io.mapRequired("numerator", num);
- io.mapRequired("denominator", denom);
- // convert fraction to double
- d.value = num/denom;
- }
- };
- }
- }
- //
- // Test the reading of two different tagged yaml documents.
- //
- TEST(YAMLIO, TestTaggedDocuments) {
- std::vector<MyDouble> docList;
- Input yin("--- !decimal\nvalue: 3.0\n"
- "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
- yin >> docList;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(docList.size(), 2UL);
- EXPECT_EQ(docList[0].value, 3.0);
- EXPECT_EQ(docList[1].value, 4.5);
- }
- //
- // Test writing then reading back tagged documents
- //
- TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
- std::string intermediate;
- {
- MyDouble a(10.25);
- MyDouble b(-3.75);
- std::vector<MyDouble> docList;
- docList.push_back(a);
- docList.push_back(b);
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << docList;
- }
- {
- Input yin(intermediate);
- std::vector<MyDouble> docList2;
- yin >> docList2;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(docList2.size(), 2UL);
- EXPECT_EQ(docList2[0].value, 10.25);
- EXPECT_EQ(docList2[1].value, -3.75);
- }
- }
- //===----------------------------------------------------------------------===//
- // Test mapping validation
- //===----------------------------------------------------------------------===//
- struct MyValidation {
- double value;
- };
- LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation)
- namespace llvm {
- namespace yaml {
- template <>
- struct MappingTraits<MyValidation> {
- static void mapping(IO &io, MyValidation &d) {
- io.mapRequired("value", d.value);
- }
- static StringRef validate(IO &io, MyValidation &d) {
- if (d.value < 0)
- return "negative value";
- return StringRef();
- }
- };
- }
- }
- //
- // Test that validate() is called and complains about the negative value.
- //
- TEST(YAMLIO, TestValidatingInput) {
- std::vector<MyValidation> docList;
- Input yin("--- \nvalue: 3.0\n"
- "--- \nvalue: -1.0\n...\n",
- nullptr, suppressErrorMessages);
- yin >> docList;
- EXPECT_TRUE(!!yin.error());
- }
- //===----------------------------------------------------------------------===//
- // Test flow mapping
- //===----------------------------------------------------------------------===//
- struct FlowFooBar {
- int foo;
- int bar;
- FlowFooBar() : foo(0), bar(0) {}
- FlowFooBar(int foo, int bar) : foo(foo), bar(bar) {}
- };
- typedef std::vector<FlowFooBar> FlowFooBarSequence;
- LLVM_YAML_IS_SEQUENCE_VECTOR(FlowFooBar)
- struct FlowFooBarDoc {
- FlowFooBar attribute;
- FlowFooBarSequence seq;
- };
- namespace llvm {
- namespace yaml {
- template <>
- struct MappingTraits<FlowFooBar> {
- static void mapping(IO &io, FlowFooBar &fb) {
- io.mapRequired("foo", fb.foo);
- io.mapRequired("bar", fb.bar);
- }
- static const bool flow = true;
- };
- template <>
- struct MappingTraits<FlowFooBarDoc> {
- static void mapping(IO &io, FlowFooBarDoc &fb) {
- io.mapRequired("attribute", fb.attribute);
- io.mapRequired("seq", fb.seq);
- }
- };
- }
- }
- //
- // Test writing then reading back custom mappings
- //
- TEST(YAMLIO, TestReadWriteMyFlowMapping) {
- std::string intermediate;
- {
- FlowFooBarDoc doc;
- doc.attribute = FlowFooBar(42, 907);
- doc.seq.push_back(FlowFooBar(1, 2));
- doc.seq.push_back(FlowFooBar(0, 0));
- doc.seq.push_back(FlowFooBar(-1, 1024));
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << doc;
- // Verify that mappings were written in flow style
- ostr.flush();
- llvm::StringRef flowOut(intermediate);
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("{ foo: 42, bar: 907 }"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 1, bar: 2 }"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 0, bar: 0 }"));
- EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: -1, bar: 1024 }"));
- }
- {
- Input yin(intermediate);
- FlowFooBarDoc doc2;
- yin >> doc2;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(doc2.attribute.foo, 42);
- EXPECT_EQ(doc2.attribute.bar, 907);
- EXPECT_EQ(doc2.seq.size(), 3UL);
- EXPECT_EQ(doc2.seq[0].foo, 1);
- EXPECT_EQ(doc2.seq[0].bar, 2);
- EXPECT_EQ(doc2.seq[1].foo, 0);
- EXPECT_EQ(doc2.seq[1].bar, 0);
- EXPECT_EQ(doc2.seq[2].foo, -1);
- EXPECT_EQ(doc2.seq[2].bar, 1024);
- }
- }
- //===----------------------------------------------------------------------===//
- // Test error handling
- //===----------------------------------------------------------------------===//
- //
- // Test error handling of unknown enumerated scalar
- //
- TEST(YAMLIO, TestColorsReadError) {
- ColorMap map;
- Input yin("---\n"
- "c1: blue\n"
- "c2: purple\n"
- "c3: green\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> map;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling of flow sequence with unknown value
- //
- TEST(YAMLIO, TestFlagsReadError) {
- FlagsMap map;
- Input yin("---\n"
- "f1: [ big ]\n"
- "f2: [ round, hollow ]\n"
- "f3: []\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> map;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in uint8_t type
- //
- LLVM_YAML_IS_SEQUENCE_VECTOR(uint8_t)
- TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
- std::vector<uint8_t> seq;
- Input yin("---\n"
- "- 255\n"
- "- 0\n"
- "- 257\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in uint16_t type
- //
- LLVM_YAML_IS_SEQUENCE_VECTOR(uint16_t)
- TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
- std::vector<uint16_t> seq;
- Input yin("---\n"
- "- 65535\n"
- "- 0\n"
- "- 66000\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in uint32_t type
- //
- LLVM_YAML_IS_SEQUENCE_VECTOR(uint32_t)
- TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
- std::vector<uint32_t> seq;
- Input yin("---\n"
- "- 4000000000\n"
- "- 0\n"
- "- 5000000000\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in uint64_t type
- //
- LLVM_YAML_IS_SEQUENCE_VECTOR(uint64_t)
- TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
- std::vector<uint64_t> seq;
- Input yin("---\n"
- "- 18446744073709551615\n"
- "- 0\n"
- "- 19446744073709551615\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in int8_t type
- //
- LLVM_YAML_IS_SEQUENCE_VECTOR(int8_t)
- TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
- std::vector<int8_t> seq;
- Input yin("---\n"
- "- -128\n"
- "- 0\n"
- "- 127\n"
- "- 128\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in int8_t type
- //
- TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
- std::vector<int8_t> seq;
- Input yin("---\n"
- "- -128\n"
- "- 0\n"
- "- 127\n"
- "- -129\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in int16_t type
- //
- LLVM_YAML_IS_SEQUENCE_VECTOR(int16_t)
- TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
- std::vector<int16_t> seq;
- Input yin("---\n"
- "- 32767\n"
- "- 0\n"
- "- -32768\n"
- "- -32769\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in int16_t type
- //
- TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
- std::vector<int16_t> seq;
- Input yin("---\n"
- "- 32767\n"
- "- 0\n"
- "- -32768\n"
- "- 32768\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in int32_t type
- //
- LLVM_YAML_IS_SEQUENCE_VECTOR(int32_t)
- TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
- std::vector<int32_t> seq;
- Input yin("---\n"
- "- 2147483647\n"
- "- 0\n"
- "- -2147483648\n"
- "- -2147483649\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in int32_t type
- //
- TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
- std::vector<int32_t> seq;
- Input yin("---\n"
- "- 2147483647\n"
- "- 0\n"
- "- -2147483648\n"
- "- 2147483649\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in int64_t type
- //
- LLVM_YAML_IS_SEQUENCE_VECTOR(int64_t)
- TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
- std::vector<int64_t> seq;
- Input yin("---\n"
- "- -9223372036854775808\n"
- "- 0\n"
- "- 9223372036854775807\n"
- "- -9223372036854775809\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in int64_t type
- //
- TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
- std::vector<int64_t> seq;
- Input yin("---\n"
- "- -9223372036854775808\n"
- "- 0\n"
- "- 9223372036854775807\n"
- "- 9223372036854775809\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in float type
- //
- LLVM_YAML_IS_SEQUENCE_VECTOR(float)
- TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
- std::vector<float> seq;
- Input yin("---\n"
- "- 0.0\n"
- "- 1000.1\n"
- "- -123.456\n"
- "- 1.2.3\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in float type
- //
- LLVM_YAML_IS_SEQUENCE_VECTOR(double)
- TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
- std::vector<double> seq;
- Input yin("---\n"
- "- 0.0\n"
- "- 1000.1\n"
- "- -123.456\n"
- "- 1.2.3\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in Hex8 type
- //
- LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
- TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
- std::vector<Hex8> seq;
- Input yin("---\n"
- "- 0x12\n"
- "- 0xFE\n"
- "- 0x123\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in Hex16 type
- //
- LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
- TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
- std::vector<Hex16> seq;
- Input yin("---\n"
- "- 0x0012\n"
- "- 0xFEFF\n"
- "- 0x12345\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in Hex32 type
- //
- LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
- TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
- std::vector<Hex32> seq;
- Input yin("---\n"
- "- 0x0012\n"
- "- 0xFEFF0000\n"
- "- 0x1234556789\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- //
- // Test error handling reading built-in Hex64 type
- //
- LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
- TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
- std::vector<Hex64> seq;
- Input yin("---\n"
- "- 0x0012\n"
- "- 0xFFEEDDCCBBAA9988\n"
- "- 0x12345567890ABCDEF0\n"
- "...\n",
- /*Ctxt=*/nullptr,
- suppressErrorMessages);
- yin >> seq;
- EXPECT_TRUE(!!yin.error());
- }
- TEST(YAMLIO, TestMalformedMapFailsGracefully) {
- FooBar doc;
- {
- // We pass the suppressErrorMessages handler to handle the error
- // message generated in the constructor of Input.
- Input yin("{foo:3, bar: 5}", /*Ctxt=*/nullptr, suppressErrorMessages);
- yin >> doc;
- EXPECT_TRUE(!!yin.error());
- }
- {
- Input yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/nullptr, suppressErrorMessages);
- yin >> doc;
- EXPECT_TRUE(!!yin.error());
- }
- }
- struct OptionalTest {
- std::vector<int> Numbers;
- };
- struct OptionalTestSeq {
- std::vector<OptionalTest> Tests;
- };
- LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
- namespace llvm {
- namespace yaml {
- template <>
- struct MappingTraits<OptionalTest> {
- static void mapping(IO& IO, OptionalTest &OT) {
- IO.mapOptional("Numbers", OT.Numbers);
- }
- };
- template <>
- struct MappingTraits<OptionalTestSeq> {
- static void mapping(IO &IO, OptionalTestSeq &OTS) {
- IO.mapOptional("Tests", OTS.Tests);
- }
- };
- }
- }
- TEST(YAMLIO, SequenceElideTest) {
- // Test that writing out a purely optional structure with its fields set to
- // default followed by other data is properly read back in.
- OptionalTestSeq Seq;
- OptionalTest One, Two, Three, Four;
- int N[] = {1, 2, 3};
- Three.Numbers.assign(N, N + 3);
- Seq.Tests.push_back(One);
- Seq.Tests.push_back(Two);
- Seq.Tests.push_back(Three);
- Seq.Tests.push_back(Four);
- std::string intermediate;
- {
- llvm::raw_string_ostream ostr(intermediate);
- Output yout(ostr);
- yout << Seq;
- }
- Input yin(intermediate);
- OptionalTestSeq Seq2;
- yin >> Seq2;
- EXPECT_FALSE(yin.error());
- EXPECT_EQ(4UL, Seq2.Tests.size());
- EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
- EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
- EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
- EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
- EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
- EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());
- }
- TEST(YAMLIO, TestEmptyStringFailsForMapWithRequiredFields) {
- FooBar doc;
- Input yin("");
- yin >> doc;
- EXPECT_TRUE(!!yin.error());
- }
- TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) {
- OptionalTest doc;
- Input yin("");
- yin >> doc;
- EXPECT_FALSE(yin.error());
- }
- TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {
- std::vector<uint8_t> seq;
- Input yin("", /*Ctxt=*/nullptr, suppressErrorMessages);
- yin >> seq;
- EXPECT_FALSE(yin.error());
- EXPECT_TRUE(seq.empty());
- }
- struct FlowMap {
- llvm::StringRef str1, str2, str3;
- FlowMap(llvm::StringRef str1, llvm::StringRef str2, llvm::StringRef str3)
- : str1(str1), str2(str2), str3(str3) {}
- };
- struct FlowSeq {
- llvm::StringRef str;
- FlowSeq(llvm::StringRef S) : str(S) {}
- FlowSeq() = default;
- };
- namespace llvm {
- namespace yaml {
- template <>
- struct MappingTraits<FlowMap> {
- static void mapping(IO &io, FlowMap &fm) {
- io.mapRequired("str1", fm.str1);
- io.mapRequired("str2", fm.str2);
- io.mapRequired("str3", fm.str3);
- }
- static const bool flow = true;
- };
- template <>
- struct ScalarTraits<FlowSeq> {
- static void output(const FlowSeq &value, void*, llvm::raw_ostream &out) {
- out << value.str;
- }
- static StringRef input(StringRef scalar, void*, FlowSeq &value) {
- value.str = scalar;
- return "";
- }
- static bool mustQuote(StringRef S) { return false; }
- };
- }
- }
- LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FlowSeq)
- TEST(YAMLIO, TestWrapFlow) {
- std::string out;
- llvm::raw_string_ostream ostr(out);
- FlowMap Map("This is str1", "This is str2", "This is str3");
- std::vector<FlowSeq> Seq;
- Seq.emplace_back("This is str1");
- Seq.emplace_back("This is str2");
- Seq.emplace_back("This is str3");
- {
- // 20 is just bellow the total length of the first mapping field.
- // We should wreap at every element.
- Output yout(ostr, nullptr, 15);
- yout << Map;
- ostr.flush();
- EXPECT_EQ(out,
- "---\n"
- "{ str1: This is str1, \n"
- " str2: This is str2, \n"
- " str3: This is str3 }\n"
- "...\n");
- out.clear();
- yout << Seq;
- ostr.flush();
- EXPECT_EQ(out,
- "---\n"
- "[ This is str1, \n"
- " This is str2, \n"
- " This is str3 ]\n"
- "...\n");
- out.clear();
- }
- {
- // 25 will allow the second field to be output on the first line.
- Output yout(ostr, nullptr, 25);
- yout << Map;
- ostr.flush();
- EXPECT_EQ(out,
- "---\n"
- "{ str1: This is str1, str2: This is str2, \n"
- " str3: This is str3 }\n"
- "...\n");
- out.clear();
- yout << Seq;
- ostr.flush();
- EXPECT_EQ(out,
- "---\n"
- "[ This is str1, This is str2, \n"
- " This is str3 ]\n"
- "...\n");
- out.clear();
- }
- {
- // 0 means no wrapping.
- Output yout(ostr, nullptr, 0);
- yout << Map;
- ostr.flush();
- EXPECT_EQ(out,
- "---\n"
- "{ str1: This is str1, str2: This is str2, str3: This is str3 }\n"
- "...\n");
- out.clear();
- yout << Seq;
- ostr.flush();
- EXPECT_EQ(out,
- "---\n"
- "[ This is str1, This is str2, This is str3 ]\n"
- "...\n");
- out.clear();
- }
- }
|