Line data Source code
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 :
14 15418 : TEST(IteratorTest, IteratorRangeEmpty) {
15 : base::iterator_range<char*> r;
16 2 : EXPECT_EQ(r.begin(), r.end());
17 2 : EXPECT_EQ(r.end(), r.cend());
18 2 : EXPECT_EQ(r.begin(), r.cbegin());
19 : EXPECT_TRUE(r.empty());
20 2 : EXPECT_EQ(0, r.size());
21 1 : }
22 :
23 :
24 15418 : TEST(IteratorTest, IteratorRangeArray) {
25 1 : int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
26 : base::iterator_range<int*> r1(&array[0], &array[10]);
27 21 : for (auto i : r1) {
28 20 : EXPECT_EQ(array[i], i);
29 : }
30 2 : EXPECT_EQ(10, r1.size());
31 : EXPECT_FALSE(r1.empty());
32 21 : for (size_t i = 0; i < arraysize(array); ++i) {
33 20 : EXPECT_EQ(r1[i], array[i]);
34 : }
35 : base::iterator_range<int*> r2(&array[0], &array[0]);
36 2 : EXPECT_EQ(0, r2.size());
37 : EXPECT_TRUE(r2.empty());
38 21 : for (auto i : array) {
39 20 : EXPECT_EQ(r2.end(), std::find(r2.begin(), r2.end(), i));
40 : }
41 1 : }
42 :
43 :
44 15418 : TEST(IteratorTest, IteratorRangeDeque) {
45 : typedef std::deque<int> C;
46 : C c;
47 2 : c.push_back(1);
48 2 : c.push_back(2);
49 2 : c.push_back(2);
50 : base::iterator_range<typename C::iterator> r(c.begin(), c.end());
51 2 : EXPECT_EQ(3, r.size());
52 2 : EXPECT_FALSE(r.empty());
53 1 : EXPECT_TRUE(c.begin() == r.begin());
54 1 : EXPECT_TRUE(c.end() == r.end());
55 2 : EXPECT_EQ(0, std::count(r.begin(), r.end(), 0));
56 2 : EXPECT_EQ(1, std::count(r.begin(), r.end(), 1));
57 2 : EXPECT_EQ(2, std::count(r.begin(), r.end(), 2));
58 1 : }
59 :
60 : } // namespace base
61 9249 : } // namespace v8
|