Bug 1645429 - Add tests for ReverseIterator and fix implementation of ReverseIterator::operator-. r=froydnj

Depends on D79488

Differential Revision: https://phabricator.services.mozilla.com/D79957
This commit is contained in:
Simon Giesecke 2020-06-17 13:54:12 +00:00
Родитель 7ea8ee451e
Коммит 901af53e36
3 изменённых файлов: 106 добавлений и 1 удалений

Просмотреть файл

@ -47,7 +47,7 @@ class ReverseIterator {
/* Difference operator */
difference_type operator-(const ReverseIterator& aOther) const {
return -(aOther.mCurrent - mCurrent);
return aOther.mCurrent - mCurrent;
}
/* Increments and decrements operators */

Просмотреть файл

@ -0,0 +1,104 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "mozilla/ReverseIterator.h"
using namespace mozilla;
TEST(ReverseIterator, Const_RangeBasedFor)
{
const std::vector<int> in = {1, 2, 3, 4};
const auto reversedRange =
detail::IteratorRange<ReverseIterator<std::vector<int>::const_iterator>>{
in.end(), in.begin()};
const std::vector<int> expected = {4, 3, 2, 1};
std::vector<int> out;
for (auto i : reversedRange) {
out.emplace_back(i);
}
EXPECT_EQ(expected, out);
}
TEST(ReverseIterator, NonConst_RangeBasedFor)
{
std::vector<int> in = {1, 2, 3, 4};
auto reversedRange =
detail::IteratorRange<ReverseIterator<std::vector<int>::iterator>>{
in.end(), in.begin()};
const std::vector<int> expected = {-1, -2, -3, -4};
for (auto& i : reversedRange) {
i = -i;
}
EXPECT_EQ(expected, in);
}
TEST(ReverseIterator, Difference)
{
const std::vector<int> in = {1, 2, 3, 4};
using reverse_iterator = ReverseIterator<std::vector<int>::const_iterator>;
reverse_iterator rbegin = reverse_iterator{in.end()},
rend = reverse_iterator{in.begin()};
EXPECT_EQ(4, rend - rbegin);
EXPECT_EQ(0, rend - rend);
EXPECT_EQ(0, rbegin - rbegin);
--rend;
EXPECT_EQ(3, rend - rbegin);
++rbegin;
EXPECT_EQ(2, rend - rbegin);
rend--;
EXPECT_EQ(1, rend - rbegin);
rbegin++;
EXPECT_EQ(0, rend - rbegin);
}
TEST(ReverseIterator, Comparison)
{
const std::vector<int> in = {1, 2, 3, 4};
using reverse_iterator = ReverseIterator<std::vector<int>::const_iterator>;
reverse_iterator rbegin = reverse_iterator{in.end()},
rend = reverse_iterator{in.begin()};
EXPECT_TRUE(rbegin < rend);
EXPECT_FALSE(rend < rbegin);
EXPECT_FALSE(rend < rend);
EXPECT_FALSE(rbegin < rbegin);
EXPECT_TRUE(rend > rbegin);
EXPECT_FALSE(rbegin > rend);
EXPECT_FALSE(rend > rend);
EXPECT_FALSE(rbegin > rbegin);
EXPECT_TRUE(rbegin <= rend);
EXPECT_FALSE(rend <= rbegin);
EXPECT_TRUE(rend <= rend);
EXPECT_TRUE(rbegin <= rbegin);
EXPECT_TRUE(rend >= rbegin);
EXPECT_FALSE(rbegin >= rend);
EXPECT_TRUE(rend >= rend);
EXPECT_TRUE(rbegin >= rbegin);
EXPECT_FALSE(rend == rbegin);
EXPECT_FALSE(rbegin == rend);
EXPECT_TRUE(rend == rend);
EXPECT_TRUE(rbegin == rbegin);
EXPECT_TRUE(rend != rbegin);
EXPECT_TRUE(rbegin != rend);
EXPECT_FALSE(rend != rend);
EXPECT_FALSE(rbegin != rbegin);
}

Просмотреть файл

@ -7,6 +7,7 @@
UNIFIED_SOURCES += [
'TestBuffer.cpp',
'TestLinkedList.cpp',
'TestReverseIterator.cpp',
'TestSpan.cpp',
]