Skip to content

Commit

Permalink
Fix for #66 (#73)
Browse files Browse the repository at this point in the history
* Fix for #66

STL iterator expects reference type to be const based on if it is a
const iterator or not.

* fixing test logic
  • Loading branch information
thirtytwobits authored Oct 23, 2023
1 parent 910aed3 commit 382839e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
56 changes: 56 additions & 0 deletions cetlvast/suites/unittest/test_variable_length_array_compat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,3 +618,59 @@ TYPED_TEST(VLATestsCompatAnyType, TestInitFromString)
ASSERT_EQ('s', subject[0]);
ASSERT_EQ('.', subject[subject.size() - 1]);
}

TYPED_TEST(VLATestsCompatAnyType, TestMakeReverseIterator)
{
std::allocator<bool> allocator{};
using SubjectType = typename TestFixture::template TestSubjectType<bool, std::allocator<bool>>;
SubjectType subject{allocator};
subject.push_back(true);
subject.push_back(false);
subject.push_back(false);
auto rbegin = std::make_reverse_iterator(subject.end());
auto rend = std::make_reverse_iterator(subject.begin());
auto c = 0;
for(auto i = rbegin; i < rend; ++i)
{
if (c == 0)
{
ASSERT_FALSE(*i);
}
else if (c == 1)
{
ASSERT_FALSE(*i);
}
else {
ASSERT_TRUE(*i);
}
++c;
}
}

TYPED_TEST(VLATestsCompatAnyType, TestMakeReverseConstIterator)
{
std::allocator<bool> allocator{};
using SubjectType = typename TestFixture::template TestSubjectType<bool, std::allocator<bool>>;
SubjectType subject{allocator};
subject.push_back(true);
subject.push_back(false);
subject.push_back(false);
auto rbegin = std::make_reverse_iterator(subject.cend());
auto rend = std::make_reverse_iterator(subject.cbegin());
auto c = 0;
for(auto i = rbegin; i < rend; ++i)
{
if (c == 0)
{
ASSERT_FALSE(*i);
}
else if (c == 1)
{
ASSERT_FALSE(*i);
}
else {
ASSERT_TRUE(*i);
}
++c;
}
}
11 changes: 5 additions & 6 deletions include/cetl/variable_length_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,6 @@ class VariableLengthArray<bool, Allocator> : protected VariableLengthArrayBase<u
{
return !((*this) == rhs);
}

void flip()
{
set(array_, index_, !test(array_, index_));
Expand Down Expand Up @@ -1674,9 +1673,9 @@ class VariableLengthArray<bool, Allocator> : protected VariableLengthArrayBase<u
using iterator_category = std::random_access_iterator_tag;
using value_type = typename A::value_type;
using difference_type = typename A::difference_type;
using reference = typename A::reference;
using const_reference = typename A::const_reference;
using pointer = void;
using reference =
std::conditional_t<std::is_const<A>::value, typename A::const_reference, typename A::reference>;
using pointer = void;

IteratorImpl() noexcept = default;

Expand Down Expand Up @@ -1734,7 +1733,7 @@ class VariableLengthArray<bool, Allocator> : protected VariableLengthArrayBase<u
{
return this->operator[](0);
}
const_reference operator*() const
reference operator*() const
{
return this->operator[](0);
}
Expand All @@ -1747,7 +1746,7 @@ class VariableLengthArray<bool, Allocator> : protected VariableLengthArrayBase<u
{
return array_->operator[](static_cast<size_type>(static_cast<difference_type>(index_) + n));
}
const_reference operator[](const difference_type n) const
reference operator[](const difference_type n) const
{
return array_->operator[](static_cast<size_type>(static_cast<difference_type>(index_) + n));
}
Expand Down

0 comments on commit 382839e

Please sign in to comment.