| 1975 | } |
| 1976 | |
| 1977 | void UnionVectorTest() { |
| 1978 | // load FlatBuffer fbs schema. |
| 1979 | // TODO: load a JSON file with such a vector when JSON support is ready. |
| 1980 | std::string schemafile; |
| 1981 | TEST_EQ(flatbuffers::LoadFile( |
| 1982 | (test_data_path + "union_vector/union_vector.fbs").c_str(), false, |
| 1983 | &schemafile), |
| 1984 | true); |
| 1985 | |
| 1986 | // parse schema. |
| 1987 | flatbuffers::IDLOptions idl_opts; |
| 1988 | idl_opts.lang_to_generate |= flatbuffers::IDLOptions::kCpp; |
| 1989 | flatbuffers::Parser parser(idl_opts); |
| 1990 | TEST_EQ(parser.Parse(schemafile.c_str()), true); |
| 1991 | |
| 1992 | flatbuffers::FlatBufferBuilder fbb; |
| 1993 | |
| 1994 | // union types. |
| 1995 | std::vector<uint8_t> types; |
| 1996 | types.push_back(static_cast<uint8_t>(Character_Belle)); |
| 1997 | types.push_back(static_cast<uint8_t>(Character_MuLan)); |
| 1998 | types.push_back(static_cast<uint8_t>(Character_BookFan)); |
| 1999 | types.push_back(static_cast<uint8_t>(Character_Other)); |
| 2000 | types.push_back(static_cast<uint8_t>(Character_Unused)); |
| 2001 | |
| 2002 | // union values. |
| 2003 | std::vector<flatbuffers::Offset<void>> characters; |
| 2004 | characters.push_back(fbb.CreateStruct(BookReader(/*books_read=*/7)).Union()); |
| 2005 | characters.push_back(CreateAttacker(fbb, /*sword_attack_damage=*/5).Union()); |
| 2006 | characters.push_back(fbb.CreateStruct(BookReader(/*books_read=*/2)).Union()); |
| 2007 | characters.push_back(fbb.CreateString("Other").Union()); |
| 2008 | characters.push_back(fbb.CreateString("Unused").Union()); |
| 2009 | |
| 2010 | // create Movie. |
| 2011 | const auto movie_offset = |
| 2012 | CreateMovie(fbb, Character_Rapunzel, |
| 2013 | fbb.CreateStruct(Rapunzel(/*hair_length=*/6)).Union(), |
| 2014 | fbb.CreateVector(types), fbb.CreateVector(characters)); |
| 2015 | FinishMovieBuffer(fbb, movie_offset); |
| 2016 | auto buf = fbb.GetBufferPointer(); |
| 2017 | |
| 2018 | flatbuffers::Verifier verifier(buf, fbb.GetSize()); |
| 2019 | TEST_EQ(VerifyMovieBuffer(verifier), true); |
| 2020 | |
| 2021 | auto flat_movie = GetMovie(buf); |
| 2022 | |
| 2023 | auto TestMovie = [](const Movie *movie) { |
| 2024 | TEST_EQ(movie->main_character_type() == Character_Rapunzel, true); |
| 2025 | |
| 2026 | auto cts = movie->characters_type(); |
| 2027 | TEST_EQ(movie->characters_type()->size(), 5); |
| 2028 | TEST_EQ(cts->GetEnum<Character>(0) == Character_Belle, true); |
| 2029 | TEST_EQ(cts->GetEnum<Character>(1) == Character_MuLan, true); |
| 2030 | TEST_EQ(cts->GetEnum<Character>(2) == Character_BookFan, true); |
| 2031 | TEST_EQ(cts->GetEnum<Character>(3) == Character_Other, true); |
| 2032 | TEST_EQ(cts->GetEnum<Character>(4) == Character_Unused, true); |
| 2033 | |
| 2034 | auto rapunzel = movie->main_character_as_Rapunzel(); |
no test coverage detected