...
1#include "optional_scalars_test.h"
2
3#include <string>
4#include <vector>
5
6#include "flatbuffers/idl.h"
7#include "optional_scalars_generated.h"
8#include "test_assert.h"
9
10namespace flatbuffers {
11namespace tests {
12
13void OptionalScalarsTest() {
14 // Simple schemas and a "has optional scalar" sentinal.
15 std::vector<std::string> schemas;
16 schemas.push_back("table Monster { mana : int; }");
17 schemas.push_back("table Monster { mana : int = 42; }");
18 schemas.push_back("table Monster { mana : int = null; }");
19 schemas.push_back("table Monster { mana : long; }");
20 schemas.push_back("table Monster { mana : long = 42; }");
21 schemas.push_back("table Monster { mana : long = null; }");
22 schemas.push_back("table Monster { mana : float; }");
23 schemas.push_back("table Monster { mana : float = 42; }");
24 schemas.push_back("table Monster { mana : float = null; }");
25 schemas.push_back("table Monster { mana : double; }");
26 schemas.push_back("table Monster { mana : double = 42; }");
27 schemas.push_back("table Monster { mana : double = null; }");
28 schemas.push_back("table Monster { mana : bool; }");
29 schemas.push_back("table Monster { mana : bool = 42; }");
30 schemas.push_back("table Monster { mana : bool = null; }");
31 schemas.push_back(
32 "enum Enum: int {A=0, B=1} "
33 "table Monster { mana : Enum; }");
34 schemas.push_back(
35 "enum Enum: int {A=0, B=1} "
36 "table Monster { mana : Enum = B; }");
37 schemas.push_back(
38 "enum Enum: int {A=0, B=1} "
39 "table Monster { mana : Enum = null; }");
40
41 // Check the FieldDef is correctly set.
42 for (auto schema = schemas.begin(); schema < schemas.end(); schema++) {
43 const bool has_null = schema->find("null") != std::string::npos;
44 flatbuffers::Parser parser;
45 TEST_ASSERT(parser.Parse(schema->c_str()));
46 const auto *mana = parser.structs_.Lookup("Monster")->fields.Lookup("mana");
47 TEST_EQ(mana->IsOptional(), has_null);
48 }
49
50 // Test if nullable scalars are allowed for each language.
51 for (unsigned lang = 1; lang < flatbuffers::IDLOptions::kMAX; lang <<= 1) {
52 flatbuffers::IDLOptions opts;
53 opts.lang_to_generate = lang;
54 if (false == flatbuffers::Parser::SupportsOptionalScalars(opts)) {
55 continue;
56 }
57 for (auto schema = schemas.begin(); schema < schemas.end(); schema++) {
58 flatbuffers::Parser parser(opts);
59 auto done = parser.Parse(schema->c_str());
60 TEST_EQ_STR(parser.error_.c_str(), "");
61 TEST_ASSERT(done);
62 }
63 }
64
65 // test C++ nullable
66 flatbuffers::FlatBufferBuilder fbb;
67 FinishScalarStuffBuffer(
68 fbb, optional_scalars::CreateScalarStuff(fbb, 1, static_cast<int8_t>(2)));
69 auto opts = optional_scalars::GetMutableScalarStuff(fbb.GetBufferPointer());
70 TEST_ASSERT(!opts->maybe_bool());
71 TEST_ASSERT(!opts->maybe_f32().has_value());
72 TEST_ASSERT(opts->maybe_i8().has_value());
73 TEST_EQ(opts->maybe_i8().value(), 2);
74 TEST_ASSERT(opts->mutate_maybe_i8(3));
75 TEST_ASSERT(opts->maybe_i8().has_value());
76 TEST_EQ(opts->maybe_i8().value(), 3);
77 TEST_ASSERT(!opts->mutate_maybe_i16(-10));
78
79 optional_scalars::ScalarStuffT obj;
80 TEST_ASSERT(!obj.maybe_bool);
81 TEST_ASSERT(!obj.maybe_f32.has_value());
82 opts->UnPackTo(&obj);
83 TEST_ASSERT(!obj.maybe_bool);
84 TEST_ASSERT(!obj.maybe_f32.has_value());
85 TEST_ASSERT(obj.maybe_i8.has_value() && obj.maybe_i8.value() == 3);
86 TEST_ASSERT(obj.maybe_i8 && *obj.maybe_i8 == 3);
87 obj.maybe_i32 = -1;
88 obj.maybe_enum = optional_scalars::OptionalByte_Two;
89
90 fbb.Clear();
91 FinishScalarStuffBuffer(fbb, optional_scalars::ScalarStuff::Pack(fbb, &obj));
92 opts = optional_scalars::GetMutableScalarStuff(fbb.GetBufferPointer());
93 TEST_ASSERT(opts->maybe_i8().has_value());
94 TEST_EQ(opts->maybe_i8().value(), 3);
95 TEST_ASSERT(opts->maybe_i32().has_value());
96 TEST_EQ(opts->maybe_i32().value(), -1);
97 TEST_EQ(opts->maybe_enum().value(), optional_scalars::OptionalByte_Two);
98 TEST_ASSERT(opts->maybe_i32() == flatbuffers::Optional<int64_t>(-1));
99}
100
101} // namespace tests
102} // namespace flatbuffers
View as plain text