[C++] Update to address comparator failure in big endian (#7681)
* update unit test and generated file to test is extra endianswap can help resolve issue * remove EndianScalar wrapper from Get method * remove endianscalar wrapper * update * update * use Array instead * clang format * address error * clang * update * manually generate * Move Nim to completed language * Add swift link * address comments * update unit test * address comment * address comment * regenerate file * use auto instead of size_t * use uint32_t instead * update * format * delete extra whitespace Co-authored-by: Wen Sun <sunwen@google.com> Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
parent
11394575bc
commit
b5ebd3fd78
|
@ -2270,14 +2270,17 @@ class CppGenerator : public BaseGenerator {
|
|||
code_ +=
|
||||
" int KeyCompareWithValue(const {{INPUT_TYPE}} *_{{FIELD_NAME}}"
|
||||
") const {";
|
||||
code_ += " for (auto i = 0; i < {{FIELD_NAME}}()->size(); i++) {";
|
||||
code_ += " const auto {{FIELD_NAME}}_l = {{FIELD_NAME}}_[i];";
|
||||
code_ += " const auto {{FIELD_NAME}}_r = _{{FIELD_NAME}}->Get(i);";
|
||||
code_ += " if({{FIELD_NAME}}_l != {{FIELD_NAME}}_r) ";
|
||||
code_ +=
|
||||
" return static_cast<int>({{FIELD_NAME}}_l > "
|
||||
"{{FIELD_NAME}}_r)"
|
||||
" - static_cast<int>({{FIELD_NAME}}_l < {{FIELD_NAME}}_r);";
|
||||
" const {{INPUT_TYPE}} *curr_{{FIELD_NAME}} = {{FIELD_NAME}}();";
|
||||
code_ +=
|
||||
" for (flatbuffers::uoffset_t i = 0; i < "
|
||||
"curr_{{FIELD_NAME}}->size(); i++) {";
|
||||
code_ += " const auto lhs = curr_{{FIELD_NAME}}->Get(i);";
|
||||
code_ += " const auto rhs = _{{FIELD_NAME}}->Get(i);";
|
||||
code_ += " if(lhs != rhs)";
|
||||
code_ +=
|
||||
" return static_cast<int>(lhs > rhs)"
|
||||
" - static_cast<int>(lhs < rhs);";
|
||||
code_ += " }";
|
||||
code_ += " return 0;";
|
||||
}
|
||||
|
|
|
@ -68,11 +68,12 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(1) Baz FLATBUFFERS_FINAL_CLASS {
|
|||
return KeyCompareWithValue(o->a()) < 0;
|
||||
}
|
||||
int KeyCompareWithValue(const flatbuffers::Array<uint8_t, 4> *_a) const {
|
||||
for (auto i = 0; i < a()->size(); i++) {
|
||||
const auto a_l = a_[i];
|
||||
const auto a_r = _a->Get(i);
|
||||
if(a_l != a_r)
|
||||
return static_cast<int>(a_l > a_r) - static_cast<int>(a_l < a_r);
|
||||
const flatbuffers::Array<uint8_t, 4> *curr_a = a();
|
||||
for (flatbuffers::uoffset_t i = 0; i < curr_a->size(); i++) {
|
||||
const auto lhs = curr_a->Get(i);
|
||||
const auto rhs = _a->Get(i);
|
||||
if(lhs != rhs)
|
||||
return static_cast<int>(lhs > rhs) - static_cast<int>(lhs < rhs);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -140,11 +141,12 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Bar FLATBUFFERS_FINAL_CLASS {
|
|||
return KeyCompareWithValue(o->a()) < 0;
|
||||
}
|
||||
int KeyCompareWithValue(const flatbuffers::Array<float, 3> *_a) const {
|
||||
for (auto i = 0; i < a()->size(); i++) {
|
||||
const auto a_l = a_[i];
|
||||
const auto a_r = _a->Get(i);
|
||||
if(a_l != a_r)
|
||||
return static_cast<int>(a_l > a_r) - static_cast<int>(a_l < a_r);
|
||||
const flatbuffers::Array<float, 3> *curr_a = a();
|
||||
for (flatbuffers::uoffset_t i = 0; i < curr_a->size(); i++) {
|
||||
const auto lhs = curr_a->Get(i);
|
||||
const auto rhs = _a->Get(i);
|
||||
if(lhs != rhs)
|
||||
return static_cast<int>(lhs > rhs) - static_cast<int>(lhs < rhs);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -24,7 +24,9 @@ void FixedSizedScalarKeyInStructTest() {
|
|||
bazs.push_back(Baz(flatbuffers::make_span(test_array3), 2));
|
||||
bazs.push_back(Baz(flatbuffers::make_span(test_array4), 3));
|
||||
auto baz_vec = fbb.CreateVectorOfSortedStructs(&bazs);
|
||||
|
||||
auto test_string = fbb.CreateString("TEST");
|
||||
|
||||
float test_float_array1[3] = { 1.5, 2.5, 0 };
|
||||
float test_float_array2[3] = { 7.5, 2.5, 0 };
|
||||
float test_float_array3[3] = { 1.5, 2.5, -1 };
|
||||
|
@ -36,6 +38,7 @@ void FixedSizedScalarKeyInStructTest() {
|
|||
bars.push_back(Bar(flatbuffers::make_span(test_float_array4), 1));
|
||||
auto bar_vec = fbb.CreateVectorOfSortedStructs(&bars);
|
||||
|
||||
|
||||
auto t = CreateFooTable(fbb, 1, 2, test_string, baz_vec, bar_vec);
|
||||
fbb.Finish(t);
|
||||
|
||||
|
@ -45,26 +48,34 @@ void FixedSizedScalarKeyInStructTest() {
|
|||
auto sorted_baz_vec = foo_table->d();
|
||||
TEST_EQ(sorted_baz_vec->Get(0)->b(), 1);
|
||||
TEST_EQ(sorted_baz_vec->Get(3)->b(), 4);
|
||||
|
||||
uint8_t test_array[4];
|
||||
auto* key_array = &flatbuffers::CastToArray(test_array);
|
||||
key_array->CopyFromSpan(flatbuffers::make_span(test_array1));
|
||||
|
||||
|
||||
TEST_NOTNULL(
|
||||
sorted_baz_vec->LookupByKey(&flatbuffers::CastToArray(test_array1)));
|
||||
sorted_baz_vec->LookupByKey(key_array));
|
||||
TEST_EQ(
|
||||
sorted_baz_vec->LookupByKey(&flatbuffers::CastToArray(test_array1))->b(),
|
||||
sorted_baz_vec->LookupByKey(key_array)->b(),
|
||||
4);
|
||||
uint8_t array_int[4] = { 7, 2, 3, 0 };
|
||||
TEST_EQ(sorted_baz_vec->LookupByKey(&flatbuffers::CastToArray(array_int)),
|
||||
key_array->CopyFromSpan(flatbuffers::make_span(array_int));
|
||||
TEST_EQ(sorted_baz_vec->LookupByKey(key_array),
|
||||
static_cast<const Baz *>(nullptr));
|
||||
|
||||
auto sorted_bar_vec = foo_table->e();
|
||||
TEST_EQ(sorted_bar_vec->Get(0)->b(), 1);
|
||||
TEST_EQ(sorted_bar_vec->Get(3)->b(), 4);
|
||||
TEST_NOTNULL(sorted_bar_vec->LookupByKey(
|
||||
&flatbuffers::CastToArray(test_float_array1)));
|
||||
TEST_EQ(
|
||||
sorted_bar_vec->LookupByKey(&flatbuffers::CastToArray(test_float_array1))
|
||||
->b(),
|
||||
3);
|
||||
|
||||
float test_float_array[3];
|
||||
auto* key_float_array = &flatbuffers::CastToArray(test_float_array);
|
||||
key_float_array->CopyFromSpan(flatbuffers::make_span(test_float_array1));
|
||||
TEST_NOTNULL(sorted_bar_vec->LookupByKey(key_float_array));
|
||||
TEST_EQ(sorted_bar_vec->LookupByKey(key_float_array)->b(), 3);
|
||||
float array_float[3] = { -1, -2, -3 };
|
||||
TEST_EQ(sorted_bar_vec->LookupByKey(&flatbuffers::CastToArray(array_float)),
|
||||
key_float_array->CopyFromSpan(flatbuffers::make_span(array_float));
|
||||
TEST_EQ(sorted_bar_vec->LookupByKey(key_float_array),
|
||||
static_cast<const Bar *>(nullptr));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue