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 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 23 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]); 27 for (auto i : r1) { 28 EXPECT_EQ(array[i], i); 29 } 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]); 34 } 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)); 40 } 41 } 42 43 44 TEST(IteratorTest, IteratorRangeDeque) { 45 typedef std::deque<unsigned> C; 46 C c; 47 c.push_back(1); 48 c.push_back(2); 49 c.push_back(2); 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)); 58 } 59 60 } // namespace base 61 } // namespace v8 62