indexed_iterator_test.cxx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "iterator/indexed_iterator.hpp"
  2. #include <vector>
  3. #include <gmock/gmock.h>
  4. using idx_iterator = iterator::indexed_iterator<std::vector<int>::iterator>;
  5. // TODO: This ought to be implemented as a compiles-test
  6. TEST(IndexedIteratorTest, CanCastCompatibleIterators) {
  7. std::vector<int> v{1, 2, 3, 4, 5};
  8. idx_iterator eai(v.begin());
  9. ::iterator::indexed_iterator<std::vector<int>::const_iterator>{eai};
  10. }
  11. TEST(IndexedIteratorTest, CanLieAboutIndex) {
  12. std::vector<int> vec{5, 3, 2, 8, 9, 11, 2, 4};
  13. idx_iterator it(vec.begin(), 3);
  14. EXPECT_THAT(it->first, 3);
  15. }
  16. TEST(IndexedIteratorTest, FakeIndexDoesntEffectEqualityCheck) {
  17. std::vector<int> vec{5, 3, 2, 8, 9, 11, 2, 4};
  18. EXPECT_THAT(idx_iterator(vec.begin()), idx_iterator(vec.begin(), 3));
  19. }
  20. TEST(IndexedIteratorTest, TreatsVectorIteratorAsMapIdxToValue) {
  21. std::vector<int> vec{5, 3, 2, 8, 9, 11, 2, 4};
  22. std::vector<std::pair<int, int>> const expected{
  23. {0, 5}, {1, 3}, {2, 2}, {3, 8}, {4, 9}, {5, 11}, {6, 2}, {7, 4}};
  24. std::vector<std::pair<int, int>> const result(idx_iterator(vec.begin()),
  25. idx_iterator(vec.end()));
  26. EXPECT_THAT(result, expected);
  27. }
  28. TEST(IndexedIteratorTest, CanMutatePointedToData) {
  29. std::vector<int> vec{5, 3, 2, 8, 9, 11, 2, 4};
  30. idx_iterator(vec.begin() + 4, 4)->second = -1;
  31. EXPECT_THAT(vec[4], -1);
  32. }