summaryrefslogtreecommitdiffstats
path: root/mfbt/tests/gtest/TestReverseIterator.cpp
blob: a1ba019aa14a6043600b494fb6e789350b9ef672 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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>>{
          ReverseIterator{in.end()}, ReverseIterator{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>>{
          ReverseIterator{in.end()}, ReverseIterator{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);
}