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.
5 #include "src/base/iterator.h"
9 #include "test/unittests/test-utils.h"
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());
24 TEST(IteratorTest, IteratorRangeArray) {
25 int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
26 base::iterator_range<int*> r1(&array[0], &array[10]);
28 EXPECT_EQ(array[i], i);
30 EXPECT_EQ(10, r1.size());
31 EXPECT_FALSE(r1.empty());
32 for (size_t i = 0; i < arraysize(array); ++i) {
33 EXPECT_EQ(r1[i], array[i]);
35 base::iterator_range<int*> r2(&array[0], &array[0]);
36 EXPECT_EQ(0, r2.size());
37 EXPECT_TRUE(r2.empty());
38 for (auto i : array) {
39 EXPECT_EQ(r2.end(), std::find(r2.begin(), r2.end(), i));
44 TEST(IteratorTest, IteratorRangeDeque) {
45 typedef std::deque<unsigned> C;
50 base::iterator_range<typename C::iterator> r(c.begin(), c.end());
51 EXPECT_EQ(3, r.size());
52 EXPECT_FALSE(r.empty());
53 EXPECT_TRUE(c.begin() == r.begin());
54 EXPECT_TRUE(c.end() == r.end());
55 EXPECT_EQ(0, std::count(r.begin(), r.end(), 0));
56 EXPECT_EQ(1, std::count(r.begin(), r.end(), 1));
57 EXPECT_EQ(2, std::count(r.begin(), r.end(), 2));