mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-22 08:48:29 +00:00
Made sure tests.cpp is testing the new vector iterator functionality.
Also fixes a potential big-endian bug, and makes iterators work correctly with pointer types. Change-Id: Ib7f88fe9e6053d1a9afa7895fba0695627c158b1 Tested: on Windows and Linux
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <cstddef>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
@@ -163,15 +164,14 @@ template<typename T> struct IndirectHelper<Offset<T>> {
|
|||||||
static const size_t element_stride = sizeof(uoffset_t);
|
static const size_t element_stride = sizeof(uoffset_t);
|
||||||
static return_type Read(const uint8_t *p, uoffset_t i) {
|
static return_type Read(const uint8_t *p, uoffset_t i) {
|
||||||
p += i * sizeof(uoffset_t);
|
p += i * sizeof(uoffset_t);
|
||||||
return EndianScalar(reinterpret_cast<return_type>(
|
return reinterpret_cast<return_type>(p + ReadScalar<uoffset_t>(p));
|
||||||
p + ReadScalar<uoffset_t>(p)));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
template<typename T> struct IndirectHelper<const T *> {
|
template<typename T> struct IndirectHelper<const T *> {
|
||||||
typedef const T &return_type;
|
typedef const T *return_type;
|
||||||
static const size_t element_stride = sizeof(T);
|
static const size_t element_stride = sizeof(T);
|
||||||
static return_type Read(const uint8_t *p, uoffset_t i) {
|
static return_type Read(const uint8_t *p, uoffset_t i) {
|
||||||
return *reinterpret_cast<const T *>(p + i * sizeof(T));
|
return reinterpret_cast<const T *>(p + i * sizeof(T));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -213,12 +213,16 @@ public:
|
|||||||
return data_ != other.data_;
|
return data_ != other.data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ptrdiff_t operator-(const VectorIterator& other) const {
|
||||||
|
return (data_ - other.data_) / IndirectHelper<T>::element_stride;
|
||||||
|
}
|
||||||
|
|
||||||
typename super_type::value_type operator *() const {
|
typename super_type::value_type operator *() const {
|
||||||
return IndirectHelper<T>::Read(data_, 0);
|
return IndirectHelper<T>::Read(data_, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
typename super_type::pointer operator->() const {
|
typename super_type::value_type operator->() const {
|
||||||
return &IndirectHelper<T>::Read(data_, 0);
|
return IndirectHelper<T>::Read(data_, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
VectorIterator &operator++() {
|
VectorIterator &operator++() {
|
||||||
|
|||||||
@@ -135,8 +135,8 @@ void AccessFlatBufferTest(const std::string &flatbuf) {
|
|||||||
auto inventory = monster->inventory();
|
auto inventory = monster->inventory();
|
||||||
TEST_NOTNULL(inventory);
|
TEST_NOTNULL(inventory);
|
||||||
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||||
for (flatbuffers::uoffset_t i = 0; i < inventory->Length(); i++)
|
for (auto it = inventory->begin(); it != inventory->end(); ++it)
|
||||||
TEST_EQ(inventory->Get(i), inv_data[i]);
|
TEST_EQ(*it, inv_data[it - inventory->begin()]);
|
||||||
|
|
||||||
// Example of accessing a union:
|
// Example of accessing a union:
|
||||||
TEST_EQ(monster->test_type(), Any_Monster); // First make sure which it is.
|
TEST_EQ(monster->test_type(), Any_Monster); // First make sure which it is.
|
||||||
@@ -153,7 +153,8 @@ void AccessFlatBufferTest(const std::string &flatbuf) {
|
|||||||
// Example of accessing a vector of tables:
|
// Example of accessing a vector of tables:
|
||||||
auto vecoftables = monster->testarrayoftables();
|
auto vecoftables = monster->testarrayoftables();
|
||||||
TEST_EQ(vecoftables->Length(), 1U);
|
TEST_EQ(vecoftables->Length(), 1U);
|
||||||
TEST_EQ(vecoftables->Get(0)->hp(), 20);
|
for (auto it = vecoftables->begin(); it != vecoftables->end(); ++it)
|
||||||
|
TEST_EQ(it->hp(), 20);
|
||||||
|
|
||||||
// Since Flatbuffers uses explicit mechanisms to override the default
|
// Since Flatbuffers uses explicit mechanisms to override the default
|
||||||
// compiler alignment, double check that the compiler indeed obeys them:
|
// compiler alignment, double check that the compiler indeed obeys them:
|
||||||
@@ -163,12 +164,16 @@ void AccessFlatBufferTest(const std::string &flatbuf) {
|
|||||||
|
|
||||||
auto tests = monster->test4();
|
auto tests = monster->test4();
|
||||||
TEST_NOTNULL(tests);
|
TEST_NOTNULL(tests);
|
||||||
auto &test_0 = tests->Get(0);
|
auto test_0 = tests->Get(0);
|
||||||
auto &test_1 = tests->Get(1);
|
auto test_1 = tests->Get(1);
|
||||||
TEST_EQ(test_0.a(), 10);
|
TEST_EQ(test_0->a(), 10);
|
||||||
TEST_EQ(test_0.b(), 20);
|
TEST_EQ(test_0->b(), 20);
|
||||||
TEST_EQ(test_1.a(), 30);
|
TEST_EQ(test_1->a(), 30);
|
||||||
TEST_EQ(test_1.b(), 40);
|
TEST_EQ(test_1->b(), 40);
|
||||||
|
for (auto it = tests->begin(); it != tests->end(); ++it) {
|
||||||
|
TEST_EQ(it->a() == 10 || it->a() == 30, true); // Just testing iterators.
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// example of parsing text straight into a buffer, and generating
|
// example of parsing text straight into a buffer, and generating
|
||||||
|
|||||||
Reference in New Issue
Block a user