1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/base/iterator.h"
6
7 #include <deque>
8
9 #include "test/unittests/test-utils.h"
10
11 namespace v8 {
12 namespace base {
13
TEST(IteratorTest,IteratorRangeEmpty)14 TEST(IteratorTest, IteratorRangeEmpty) {
15 base::iterator_range<char*> r;
16 EXPECT_EQ(r.begin(), r.end());
17 EXPECT_EQ(r.end(), r.cend());
18 EXPECT_EQ(r.begin(), r.cbegin());
19 EXPECT_TRUE(r.empty());
20 EXPECT_EQ(0, r.size());
21 }
22
TEST(IteratorTest,IteratorRangeArray)23 TEST(IteratorTest, IteratorRangeArray) {
24 int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
25 base::iterator_range<int*> r1(&array[0], &array[10]);
26 for (auto i : r1) {
27 EXPECT_EQ(array[i], i);
28 }
29 EXPECT_EQ(10, r1.size());
30 EXPECT_FALSE(r1.empty());
31 for (size_t i = 0; i < arraysize(array); ++i) {
32 EXPECT_EQ(r1[i], array[i]);
33 }
34 base::iterator_range<int*> r2(&array[0], &array[0]);
35 EXPECT_EQ(0, r2.size());
36 EXPECT_TRUE(r2.empty());
37 for (auto i : array) {
38 EXPECT_EQ(r2.end(), std::find(r2.begin(), r2.end(), i));
39 }
40 }
41
TEST(IteratorTest,IteratorRangeDeque)42 TEST(IteratorTest, IteratorRangeDeque) {
43 using C = std::deque<int>;
44 C c;
45 c.push_back(1);
46 c.push_back(2);
47 c.push_back(2);
48 base::iterator_range<typename C::iterator> r(c.begin(), c.end());
49 EXPECT_EQ(3, r.size());
50 EXPECT_FALSE(r.empty());
51 EXPECT_TRUE(c.begin() == r.begin());
52 EXPECT_TRUE(c.end() == r.end());
53 EXPECT_EQ(0, std::count(r.begin(), r.end(), 0));
54 EXPECT_EQ(1, std::count(r.begin(), r.end(), 1));
55 EXPECT_EQ(2, std::count(r.begin(), r.end(), 2));
56 }
57
TEST(IteratorTest,IteratorTypeDeduction)58 TEST(IteratorTest, IteratorTypeDeduction) {
59 base::iterator_range<char*> r;
60 auto r2 = make_iterator_range(r.begin(), r.end());
61 EXPECT_EQ(r2.begin(), r.begin());
62 EXPECT_EQ(r2.end(), r2.end());
63 auto I = r.begin(), E = r.end();
64 // Check that this compiles and does the correct thing even if the iterators
65 // are lvalues:
66 auto r3 = make_iterator_range(I, E);
67 EXPECT_TRUE((std::is_same<decltype(r2), decltype(r3)>::value));
68 EXPECT_EQ(r3.begin(), r.begin());
69 EXPECT_EQ(r3.end(), r2.end());
70 }
71 } // namespace base
72 } // namespace v8
73