Fix reverse_iterator in Vector and tests (#5344)

Before this commit tests for iterators passed, even if the code inside
the loop has not been executed.
This commit is contained in:
mmoscicki2 2019-05-16 20:49:01 +02:00 committed by Wouter van Oortmerssen
parent 0bb3ce6935
commit 39bd667fd0
2 changed files with 26 additions and 16 deletions

View File

@ -201,14 +201,16 @@ template<typename T, typename IT> struct VectorIterator {
template<typename Iterator> struct VectorReverseIterator :
public std::reverse_iterator<Iterator> {
explicit VectorReverseIterator(Iterator iter) : iter_(iter) {}
explicit VectorReverseIterator(Iterator iter) :
std::reverse_iterator<Iterator>(iter) {}
typename Iterator::value_type operator*() const { return *(iter_ - 1); }
typename Iterator::value_type operator*() const {
return *(std::reverse_iterator<Iterator>::current);
}
typename Iterator::value_type operator->() const { return *(iter_ - 1); }
private:
Iterator iter_;
typename Iterator::value_type operator->() const {
return *(std::reverse_iterator<Iterator>::current);
}
};
struct String;
@ -269,11 +271,11 @@ template<typename T> class Vector {
iterator end() { return iterator(Data(), size()); }
const_iterator end() const { return const_iterator(Data(), size()); }
reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
reverse_iterator rbegin() { return reverse_iterator(end() - 1); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(end() - 1); }
reverse_iterator rend() { return reverse_iterator(end()); }
const_reverse_iterator rend() const { return const_reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin() - 1); }
const_reverse_iterator rend() const { return const_reverse_iterator(begin() - 1); }
const_iterator cbegin() const { return begin(); }

View File

@ -253,29 +253,37 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length,
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Check compatibilty of iterators with STL.
std::vector<unsigned char> inv_vec(inventory->begin(), inventory->end());
for (auto it = inventory->begin(); it != inventory->end(); ++it) {
int n = 0;
for (auto it = inventory->begin(); it != inventory->end(); ++it, ++n) {
auto indx = it - inventory->begin();
TEST_EQ(*it, inv_vec.at(indx)); // Use bounds-check.
TEST_EQ(*it, inv_data[indx]);
}
TEST_EQ(n, inv_vec.size());
for (auto it = inventory->cbegin(); it != inventory->cend(); ++it) {
n = 0;
for (auto it = inventory->cbegin(); it != inventory->cend(); ++it, ++n) {
auto indx = it - inventory->cbegin();
TEST_EQ(*it, inv_vec.at(indx)); // Use bounds-check.
TEST_EQ(*it, inv_data[indx]);
}
TEST_EQ(n, inv_vec.size());
for (auto it = inventory->rbegin(); it != inventory->rend(); ++it) {
auto indx = inventory->rend() - it;
n = 0;
for (auto it = inventory->rbegin(); it != inventory->rend(); ++it, ++n) {
auto indx = inventory->rend() - it - 1;
TEST_EQ(*it, inv_vec.at(indx)); // Use bounds-check.
TEST_EQ(*it, inv_data[indx]);
}
TEST_EQ(n, inv_vec.size());
for (auto it = inventory->crbegin(); it != inventory->crend(); ++it) {
auto indx = inventory->crend() - it;
n = 0;
for (auto it = inventory->crbegin(); it != inventory->crend(); ++it, ++n) {
auto indx = inventory->crend() - it - 1;
TEST_EQ(*it, inv_vec.at(indx)); // Use bounds-check.
TEST_EQ(*it, inv_data[indx]);
}
TEST_EQ(n, inv_vec.size());
TEST_EQ(monster->color(), Color_Blue);