Added conversion operations that can be used inline in JSON.

e.g.: { myfield: cos(rad(180)) } is equivalent to writing { myfield: -1.0 }

Bug: 29338398
Change-Id: I6fc4ef1fd10bda3ba78cba464414dd071a2f50ca
Tested: on Linux.
This commit is contained in:
Wouter van Oortmerssen
2016-06-15 13:54:17 -07:00
parent afa276288c
commit d3ac0bc149
3 changed files with 52 additions and 9 deletions

View File

@@ -814,20 +814,29 @@ void ErrorTest() {
TestError("table X { Y:byte; } root_type X; { Y:1, Y:2 }", "more than once");
}
// Additional parser testing not covered elsewhere.
void ScientificTest() {
float TestValue(const char *json) {
flatbuffers::Parser parser;
// Simple schema.
TEST_EQ(parser.Parse("table X { Y:float; } root_type X;"), true);
// Test scientific notation numbers.
TEST_EQ(parser.Parse("{ Y:0.0314159e+2 }"), true);
TEST_EQ(parser.Parse(json), true);
auto root = flatbuffers::GetRoot<float>(parser.builder_.GetBufferPointer());
// root will point to the table, which is a 32bit vtable offset followed
// by a float:
TEST_EQ(sizeof(flatbuffers::soffset_t) == 4 && // Test assumes 32bit offsets
fabs(root[1] - 3.14159) < 0.001, true);
TEST_EQ(sizeof(flatbuffers::soffset_t), 4); // Test assumes 32bit offsets
return root[1];
}
bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; }
// Additional parser testing not covered elsewhere.
void ValueTest() {
// Test scientific notation numbers.
TEST_EQ(FloatCompare(TestValue("{ Y:0.0314159e+2 }"), 3.14159), true);
// Test conversion functions.
TEST_EQ(FloatCompare(TestValue("{ Y:cos(rad(180)) }"), -1), true);
}
void EnumStringsTest() {
@@ -963,7 +972,7 @@ int main(int /*argc*/, const char * /*argv*/[]) {
FuzzTest2();
ErrorTest();
ScientificTest();
ValueTest();
EnumStringsTest();
IntegerOutOfRangeTest();
UnicodeTest();