Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 1 | #include "aos/json_to_flatbuffer.h" |
| 2 | #include "aos/json_to_flatbuffer_generated.h" |
| 3 | #include "aos/util/file.h" |
| 4 | #include "flatbuffers/reflection.h" |
| 5 | #include "gtest/gtest.h" |
| 6 | |
| 7 | namespace aos { |
| 8 | namespace testing { |
| 9 | |
| 10 | class FlatbufferIntrospectionTest : public ::testing::Test { |
| 11 | public: |
| 12 | FlatbufferIntrospectionTest() |
| 13 | : schema_data_( |
| 14 | util::ReadFileToStringOrDie("aos/json_to_flatbuffer.bfbs")) { |
| 15 | schema_ = reflection::GetSchema(schema_data_.data()); |
| 16 | } |
| 17 | |
| 18 | protected: |
| 19 | FlatbufferString<reflection::Schema> schema_data_; |
| 20 | const reflection::Schema *schema_; |
| 21 | }; |
| 22 | |
| 23 | TEST_F(FlatbufferIntrospectionTest, IntegerTest) { |
| 24 | flatbuffers::FlatBufferBuilder builder; |
| 25 | ConfigurationBuilder config_builder(builder); |
| 26 | |
| 27 | config_builder.add_foo_byte(-5); |
| 28 | config_builder.add_foo_ubyte(5); |
| 29 | config_builder.add_foo_bool(true); |
| 30 | |
| 31 | config_builder.add_foo_short(-10); |
| 32 | config_builder.add_foo_ushort(10); |
| 33 | |
| 34 | config_builder.add_foo_int(-20); |
| 35 | config_builder.add_foo_uint(20); |
| 36 | |
| 37 | config_builder.add_foo_long(-100); |
| 38 | config_builder.add_foo_ulong(100); |
| 39 | |
| 40 | builder.Finish(config_builder.Finish()); |
| 41 | |
| 42 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer()); |
| 43 | |
| 44 | EXPECT_EQ(out, |
| 45 | "{\"foo_bool\": true, \"foo_byte\": -5, \"foo_int\": -20, " |
| 46 | "\"foo_long\": -100, \"foo_short\": -10, \"foo_ubyte\": 5, " |
| 47 | "\"foo_uint\": 20, \"foo_ulong\": 100, \"foo_ushort\": 10}"); |
| 48 | } |
| 49 | |
| 50 | TEST_F(FlatbufferIntrospectionTest, FloatTest) { |
| 51 | flatbuffers::FlatBufferBuilder builder; |
| 52 | ConfigurationBuilder config_builder(builder); |
| 53 | |
| 54 | config_builder.add_foo_float(1.0 / 3.0); |
| 55 | config_builder.add_foo_double(5.0 / 9.0); |
| 56 | |
| 57 | builder.Finish(config_builder.Finish()); |
| 58 | |
| 59 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer()); |
| 60 | |
| 61 | EXPECT_EQ(out, |
| 62 | "{\"foo_double\": 0.555555555555556, \"foo_float\": 0.333333}"); |
| 63 | } |
| 64 | |
Brian Silverman | a6aa504 | 2020-04-28 16:42:38 -0700 | [diff] [blame] | 65 | TEST_F(FlatbufferIntrospectionTest, NanFloatTest) { |
| 66 | flatbuffers::FlatBufferBuilder builder; |
| 67 | ConfigurationBuilder config_builder(builder); |
| 68 | |
| 69 | config_builder.add_foo_float(std::numeric_limits<float>::quiet_NaN()); |
| 70 | config_builder.add_foo_double(std::numeric_limits<double>::quiet_NaN()); |
| 71 | |
| 72 | builder.Finish(config_builder.Finish()); |
| 73 | |
| 74 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer()); |
| 75 | |
| 76 | EXPECT_EQ(out, "{\"foo_double\": null, \"foo_float\": null}"); |
| 77 | } |
| 78 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 79 | TEST_F(FlatbufferIntrospectionTest, VectorScalarTest) { |
| 80 | flatbuffers::FlatBufferBuilder builder; |
| 81 | |
| 82 | // Flatbuffers don't like creating vectors simultaneously with table, so do |
| 83 | // first. |
| 84 | auto foo_bytes = builder.CreateVector<int8_t>({-3, -2, -1, 0, 1, 2, 3}); |
| 85 | auto foo_ubytes = builder.CreateVector<uint8_t>({0, 1, 2, 3, 4, 5, 6}); |
| 86 | auto foo_bools = builder.CreateVector<uint8_t>({true, false, true, false}); |
| 87 | |
| 88 | auto foo_shorts = |
| 89 | builder.CreateVector<int16_t>({-30, -20, -10, 0, 10, 20, 30}); |
| 90 | auto foo_ushorts = |
| 91 | builder.CreateVector<uint16_t>({0, 10, 20, 30, 40, 50, 60}); |
| 92 | |
| 93 | auto foo_ints = |
| 94 | builder.CreateVector<int32_t>({-300, -200, -100, 0, 100, 200, 300}); |
| 95 | auto foo_uints = |
| 96 | builder.CreateVector<uint32_t>({0, 100, 200, 300, 400, 500, 600}); |
| 97 | |
| 98 | auto foo_longs = |
| 99 | builder.CreateVector<int64_t>({-3000, -2000, -1000, 0, 1000, 2000, 3000}); |
| 100 | auto foo_ulongs = |
| 101 | builder.CreateVector<uint64_t>({0, 1000, 2000, 3000, 4000, 5000, 6000}); |
| 102 | |
| 103 | auto foo_floats = |
| 104 | builder.CreateVector<float>({0.0, 1.0 / 9.0, 2.0 / 9.0, 3.0 / 9.0}); |
| 105 | auto foo_doubles = |
| 106 | builder.CreateVector<double>({0, 1.0 / 9.0, 2.0 / 9.0, 3.0 / 9.0}); |
| 107 | |
| 108 | ConfigurationBuilder config_builder(builder); |
| 109 | config_builder.add_vector_foo_byte(foo_bytes); |
| 110 | config_builder.add_vector_foo_ubyte(foo_ubytes); |
| 111 | config_builder.add_vector_foo_bool(foo_bools); |
| 112 | |
| 113 | config_builder.add_vector_foo_short(foo_shorts); |
| 114 | config_builder.add_vector_foo_ushort(foo_ushorts); |
| 115 | |
| 116 | config_builder.add_vector_foo_int(foo_ints); |
| 117 | config_builder.add_vector_foo_uint(foo_uints); |
| 118 | |
| 119 | config_builder.add_vector_foo_long(foo_longs); |
| 120 | config_builder.add_vector_foo_ulong(foo_ulongs); |
| 121 | |
| 122 | config_builder.add_vector_foo_float(foo_floats); |
| 123 | config_builder.add_vector_foo_double(foo_doubles); |
| 124 | |
| 125 | builder.Finish(config_builder.Finish()); |
| 126 | |
| 127 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer()); |
| 128 | |
| 129 | EXPECT_EQ( |
| 130 | out, |
| 131 | "{\"vector_foo_bool\": [true, false, true, false], \"vector_foo_byte\": " |
| 132 | "[-3, -2, -1, 0, 1, 2, 3], \"vector_foo_double\": [0, 0.111111111111111, " |
| 133 | "0.222222222222222, 0.333333333333333], \"vector_foo_float\": [0, " |
| 134 | "0.111111, 0.222222, 0.333333], \"vector_foo_int\": [-300, -200, -100, " |
| 135 | "0, 100, 200, 300], \"vector_foo_long\": [-3000, -2000, -1000, 0, 1000, " |
| 136 | "2000, 3000], \"vector_foo_short\": [-30, -20, -10, 0, 10, 20, 30], " |
| 137 | "\"vector_foo_ubyte\": [0, 1, 2, 3, 4, 5, 6], \"vector_foo_uint\": [0, " |
| 138 | "100, 200, 300, 400, 500, 600], \"vector_foo_ulong\": [0, 1000, 2000, " |
| 139 | "3000, 4000, 5000, 6000], \"vector_foo_ushort\": [0, 10, 20, 30, 40, 50, " |
| 140 | "60]}"); |
| 141 | } |
| 142 | |
| 143 | TEST_F(FlatbufferIntrospectionTest, StringTest) { |
| 144 | flatbuffers::FlatBufferBuilder builder; |
| 145 | |
| 146 | auto foo_string = builder.CreateString("I <3 FlatBuffers!"); |
| 147 | |
| 148 | ConfigurationBuilder config_builder(builder); |
| 149 | config_builder.add_foo_string(foo_string); |
| 150 | |
| 151 | builder.Finish(config_builder.Finish()); |
| 152 | |
| 153 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer()); |
| 154 | |
| 155 | EXPECT_EQ(out, "{\"foo_string\": \"I <3 FlatBuffers!\"}"); |
| 156 | } |
| 157 | |
| 158 | TEST_F(FlatbufferIntrospectionTest, EnumTest) { |
| 159 | flatbuffers::FlatBufferBuilder builder; |
| 160 | |
| 161 | ConfigurationBuilder config_builder(builder); |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 162 | config_builder.add_foo_enum(BaseType::UShort); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 163 | |
| 164 | builder.Finish(config_builder.Finish()); |
| 165 | |
| 166 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer()); |
| 167 | |
| 168 | EXPECT_EQ(out, "{\"foo_enum\": \"UShort\"}"); |
| 169 | } |
| 170 | |
Brian Silverman | a6aa504 | 2020-04-28 16:42:38 -0700 | [diff] [blame] | 171 | TEST_F(FlatbufferIntrospectionTest, EnumWithUnknownValueTest) { |
| 172 | flatbuffers::FlatBufferBuilder builder; |
| 173 | |
| 174 | ConfigurationBuilder config_builder(builder); |
| 175 | // 123 is not part of the enum. We expect it to be represented by null in |
| 176 | // the json. |
| 177 | config_builder.fbb_.AddElement<uint8_t>(Configuration::VT_FOO_ENUM, 123, 0); |
| 178 | |
| 179 | builder.Finish(config_builder.Finish()); |
| 180 | |
| 181 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer()); |
| 182 | |
| 183 | EXPECT_EQ(out, "{\"foo_enum\": 123}"); |
| 184 | } |
| 185 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 186 | TEST_F(FlatbufferIntrospectionTest, VectorStringTest) { |
| 187 | flatbuffers::FlatBufferBuilder builder; |
| 188 | |
| 189 | std::vector<std::vector<std::string>> words{ |
| 190 | {"abc", "acb"}, {"bac", "bca"}, {"cab", "cba"}}; |
| 191 | std::vector<flatbuffers::Offset< |
| 192 | flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>>> |
| 193 | strings; |
| 194 | |
| 195 | for (const auto &v : words) { |
| 196 | strings.push_back(builder.CreateVectorOfStrings(v)); |
| 197 | } |
| 198 | |
| 199 | std::vector<flatbuffers::Offset<VectorOfStrings>> sub_vectors; |
| 200 | |
| 201 | for (const auto &v : strings) { |
| 202 | VectorOfStringsBuilder v_builder(builder); |
| 203 | v_builder.add_str(v); |
| 204 | sub_vectors.push_back(v_builder.Finish()); |
| 205 | } |
| 206 | |
| 207 | auto foo_vov = builder.CreateVector(sub_vectors); |
| 208 | |
| 209 | VectorOfVectorOfStringBuilder vov_builder(builder); |
| 210 | vov_builder.add_v(foo_vov); |
| 211 | auto vov = vov_builder.Finish(); |
| 212 | |
| 213 | ConfigurationBuilder config_builder(builder); |
| 214 | config_builder.add_vector_foo_string(strings[0]); |
| 215 | config_builder.add_vov(vov); |
| 216 | |
| 217 | builder.Finish(config_builder.Finish()); |
| 218 | |
| 219 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer()); |
| 220 | |
| 221 | EXPECT_EQ(out, |
| 222 | "{\"vector_foo_string\": [\"abc\", \"acb\"], \"vov\": {\"v\": " |
| 223 | "[{\"str\": [\"abc\", \"acb\"]}, {\"str\": [\"bac\", \"bca\"]}, " |
| 224 | "{\"str\": [\"cab\", \"cba\"]}]}}"); |
| 225 | } |
| 226 | |
| 227 | TEST_F(FlatbufferIntrospectionTest, TableTest) { |
| 228 | flatbuffers::FlatBufferBuilder builder; |
| 229 | |
| 230 | auto foo_string2 = builder.CreateString("Nested Config String"); |
| 231 | auto foo_bytes2 = builder.CreateVector<int8_t>({6, 7, 8, 9, 10}); |
| 232 | |
| 233 | ConfigurationBuilder config_builder2(builder); |
| 234 | config_builder2.add_foo_byte(10); |
| 235 | config_builder2.add_foo_string(foo_string2); |
| 236 | config_builder2.add_vector_foo_byte(foo_bytes2); |
| 237 | |
| 238 | flatbuffers::Offset<Configuration> config_2 = config_builder2.Finish(); |
| 239 | |
| 240 | auto foo_string = builder.CreateString("Root Config String"); |
| 241 | auto foo_bytes = builder.CreateVector<int8_t>({0, 1, 2, 3, 4, 5}); |
| 242 | |
| 243 | ConfigurationBuilder config_builder(builder); |
| 244 | config_builder.add_nested_config(config_2); |
| 245 | config_builder.add_foo_byte(5); |
| 246 | config_builder.add_foo_string(foo_string); |
| 247 | config_builder.add_vector_foo_byte(foo_bytes); |
| 248 | |
| 249 | builder.Finish(config_builder.Finish()); |
| 250 | |
| 251 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer()); |
| 252 | |
| 253 | EXPECT_EQ(out, |
| 254 | "{\"foo_byte\": 5, \"foo_string\": \"Root Config String\", " |
| 255 | "\"nested_config\": {\"foo_byte\": 10, \"foo_string\": \"Nested " |
| 256 | "Config String\", \"vector_foo_byte\": [6, 7, 8, 9, 10]}, " |
| 257 | "\"vector_foo_byte\": [0, 1, 2, 3, 4, 5]}"); |
| 258 | } |
| 259 | |
| 260 | TEST_F(FlatbufferIntrospectionTest, StructTest) { |
| 261 | flatbuffers::FlatBufferBuilder builder; |
| 262 | |
| 263 | FooStructNested foo_struct2(10); |
| 264 | |
| 265 | FooStruct foo_struct(5, foo_struct2); |
| 266 | |
| 267 | ConfigurationBuilder config_builder(builder); |
| 268 | config_builder.add_foo_struct(&foo_struct); |
| 269 | |
| 270 | builder.Finish(config_builder.Finish()); |
| 271 | |
| 272 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer()); |
| 273 | |
| 274 | EXPECT_EQ(out, |
| 275 | "{\"foo_struct\": {\"foo_byte\": 5, \"nested_struct\": " |
| 276 | "{\"foo_byte\": 10}}}"); |
| 277 | } |
| 278 | |
| 279 | TEST_F(FlatbufferIntrospectionTest, VectorStructTest) { |
| 280 | flatbuffers::FlatBufferBuilder builder; |
| 281 | |
| 282 | FooStructNested foo_struct2(1); |
| 283 | |
| 284 | auto structs = builder.CreateVectorOfStructs( |
| 285 | std::vector<FooStruct>({{5, foo_struct2}, {10, foo_struct2}})); |
| 286 | |
| 287 | ConfigurationBuilder config_builder(builder); |
| 288 | config_builder.add_vector_foo_struct(structs); |
| 289 | |
| 290 | builder.Finish(config_builder.Finish()); |
| 291 | |
| 292 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer()); |
| 293 | |
| 294 | EXPECT_EQ(out, |
| 295 | "{\"vector_foo_struct\": [{\"foo_byte\": 5, \"nested_struct\": " |
| 296 | "{\"foo_byte\": 1}}, {\"foo_byte\": 10, \"nested_struct\": " |
| 297 | "{\"foo_byte\": 1}}]}"); |
| 298 | } |
| 299 | |
| 300 | TEST_F(FlatbufferIntrospectionTest, VectorEnumTest) { |
| 301 | flatbuffers::FlatBufferBuilder builder; |
| 302 | |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 303 | auto enums = builder.CreateVector<BaseType>( |
| 304 | {BaseType::UShort, BaseType::Obj, BaseType::UInt}); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 305 | |
| 306 | ConfigurationBuilder config_builder(builder); |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 307 | config_builder.add_vector_foo_enum( |
| 308 | flatbuffers::Offset<flatbuffers::Vector<int8_t>>(enums.o)); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 309 | |
| 310 | builder.Finish(config_builder.Finish()); |
| 311 | |
| 312 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer()); |
| 313 | |
| 314 | EXPECT_EQ(out, "{\"vector_foo_enum\": [\"UShort\", \"Obj\", \"UInt\"]}"); |
| 315 | } |
| 316 | |
| 317 | TEST_F(FlatbufferIntrospectionTest, StructEnumTest) { |
| 318 | flatbuffers::FlatBufferBuilder builder; |
| 319 | |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 320 | StructEnum foo_struct(BaseType::UShort); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 321 | |
| 322 | ConfigurationBuilder config_builder(builder); |
| 323 | config_builder.add_foo_struct_enum(&foo_struct); |
| 324 | |
| 325 | builder.Finish(config_builder.Finish()); |
| 326 | |
| 327 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer()); |
| 328 | |
| 329 | EXPECT_EQ(out, "{\"foo_struct_enum\": {\"foo_enum\": \"UShort\"}}"); |
| 330 | } |
| 331 | |
| 332 | TEST_F(FlatbufferIntrospectionTest, StringEscapeTest) { |
| 333 | flatbuffers::FlatBufferBuilder builder; |
| 334 | |
| 335 | auto foo_string = builder.CreateString("\"\\\b\f\n\r\t"); |
| 336 | |
| 337 | ConfigurationBuilder config_builder(builder); |
| 338 | config_builder.add_foo_string(foo_string); |
| 339 | |
| 340 | builder.Finish(config_builder.Finish()); |
| 341 | |
| 342 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer()); |
| 343 | EXPECT_EQ(out, "{\"foo_string\": \"\\\"\\\\\\b\\f\\n\\r\\t\"}"); |
| 344 | } |
| 345 | |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 346 | TEST_F(FlatbufferIntrospectionTest, TrimmedVector) { |
| 347 | flatbuffers::FlatBufferBuilder builder; |
| 348 | |
| 349 | std::vector<int32_t> contents; |
| 350 | for (int i = 0; i < 101; ++i) { |
| 351 | contents.push_back(i); |
| 352 | } |
| 353 | const auto contents_offset = builder.CreateVector(contents); |
| 354 | |
| 355 | ConfigurationBuilder config_builder(builder); |
| 356 | config_builder.add_vector_foo_int(contents_offset); |
| 357 | |
| 358 | builder.Finish(config_builder.Finish()); |
| 359 | |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 360 | std::string out = |
| 361 | FlatbufferToJson(schema_, builder.GetBufferPointer(), false, 100); |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 362 | EXPECT_EQ(out, "{\"vector_foo_int\": [ ... 101 elements ... ]}"); |
| 363 | } |
| 364 | |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 365 | TEST_F(FlatbufferIntrospectionTest, MultilineTest) { |
| 366 | flatbuffers::FlatBufferBuilder builder; |
| 367 | ConfigurationBuilder config_builder(builder); |
| 368 | |
| 369 | config_builder.add_foo_bool(true); |
| 370 | config_builder.add_foo_int(-20); |
| 371 | |
| 372 | builder.Finish(config_builder.Finish()); |
| 373 | |
| 374 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer(), true); |
| 375 | |
| 376 | EXPECT_EQ(out, |
| 377 | "{\n" |
| 378 | " \"foo_bool\": true,\n" |
| 379 | " \"foo_int\": -20\n" |
| 380 | "}"); |
| 381 | } |
| 382 | |
| 383 | TEST_F(FlatbufferIntrospectionTest, MultilineStructTest) { |
| 384 | flatbuffers::FlatBufferBuilder builder; |
| 385 | ConfigurationBuilder config_builder(builder); |
| 386 | |
| 387 | FooStructNested foo_struct2(10); |
| 388 | FooStruct foo_struct(5, foo_struct2); |
| 389 | |
| 390 | config_builder.add_foo_struct(&foo_struct); |
| 391 | |
| 392 | builder.Finish(config_builder.Finish()); |
| 393 | |
| 394 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer(), true); |
| 395 | |
| 396 | EXPECT_EQ(out, |
| 397 | "{\n" |
| 398 | " \"foo_struct\": {\n" |
| 399 | " \"foo_byte\": 5,\n" |
| 400 | " \"nested_struct\": {\"foo_byte\": 10}\n" |
| 401 | " }\n" |
| 402 | "}"); |
| 403 | } |
| 404 | |
| 405 | TEST_F(FlatbufferIntrospectionTest, MultilineVectorStructTest) { |
| 406 | flatbuffers::FlatBufferBuilder builder; |
| 407 | |
| 408 | FooStructNested foo_struct2(1); |
| 409 | |
| 410 | auto structs = builder.CreateVectorOfStructs( |
| 411 | std::vector<FooStruct>({{5, foo_struct2}, {10, foo_struct2}})); |
| 412 | |
| 413 | ConfigurationBuilder config_builder(builder); |
| 414 | config_builder.add_vector_foo_struct(structs); |
| 415 | |
| 416 | builder.Finish(config_builder.Finish()); |
| 417 | |
| 418 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer(), true); |
| 419 | |
| 420 | EXPECT_EQ(out, |
| 421 | "{\n" |
| 422 | " \"vector_foo_struct\": [\n" |
| 423 | " {\n" |
| 424 | " \"foo_byte\": 5,\n" |
| 425 | " \"nested_struct\": {\"foo_byte\": 1}\n" |
| 426 | " },\n" |
| 427 | " {\n" |
| 428 | " \"foo_byte\": 10,\n" |
| 429 | " \"nested_struct\": {\"foo_byte\": 1}\n" |
| 430 | " }\n" |
| 431 | " ]\n" |
| 432 | "}"); |
| 433 | } |
| 434 | |
| 435 | TEST_F(FlatbufferIntrospectionTest, MultilineVectorScalarTest) { |
| 436 | flatbuffers::FlatBufferBuilder builder; |
| 437 | |
| 438 | // Flatbuffers don't like creating vectors simultaneously with table, so do |
| 439 | // first. |
| 440 | auto foo_ints = |
| 441 | builder.CreateVector<int32_t>({-300, -200, -100, 0, 100, 200, 300}); |
| 442 | |
| 443 | auto foo_floats = |
| 444 | builder.CreateVector<float>({0.0, 1.0 / 9.0, 2.0 / 9.0, 3.0 / 9.0}); |
| 445 | auto foo_doubles = |
| 446 | builder.CreateVector<double>({0, 1.0 / 9.0, 2.0 / 9.0, 3.0 / 9.0}); |
| 447 | |
| 448 | ConfigurationBuilder config_builder(builder); |
| 449 | |
| 450 | config_builder.add_vector_foo_int(foo_ints); |
| 451 | config_builder.add_vector_foo_float(foo_floats); |
| 452 | config_builder.add_vector_foo_double(foo_doubles); |
| 453 | |
| 454 | builder.Finish(config_builder.Finish()); |
| 455 | |
| 456 | std::string out = FlatbufferToJson(schema_, builder.GetBufferPointer(), true); |
| 457 | |
| 458 | EXPECT_EQ(out, |
| 459 | "{\n" |
| 460 | " \"vector_foo_double\": [0, 0.111111111111111, " |
| 461 | "0.222222222222222, 0.333333333333333],\n" |
| 462 | " \"vector_foo_float\": [0, 0.111111, 0.222222, 0.333333],\n" |
| 463 | " \"vector_foo_int\": [-300, -200, -100, 0, 100, 200, 300]\n" |
| 464 | "}"); |
| 465 | } |
| 466 | |
Austin Schuh | 7f99e47 | 2020-06-17 20:38:17 -0700 | [diff] [blame] | 467 | // Tests that a nullptr buffer prints nullptr. |
| 468 | TEST_F(FlatbufferIntrospectionTest, NullptrData) { |
| 469 | EXPECT_EQ("null", FlatbufferToJson(schema_, nullptr)); |
| 470 | } |
| 471 | |
| 472 | // Tests that a null schema gets caught. |
| 473 | TEST(FlatbufferIntrospectionDeathTest, NullSchema) { |
| 474 | EXPECT_DEATH( |
| 475 | { |
| 476 | FlatbufferToJson(static_cast<const reflection::Schema *>(nullptr), |
| 477 | nullptr); |
| 478 | }, |
| 479 | "Need to provide a schema"); |
| 480 | } |
| 481 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 482 | } // namespace testing |
| 483 | } // namespace aos |