1 // Copyright 2012 The Chromium 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 <set> 6 7 #include "cc/base/scoped_ptr_vector.h" 8 #include "testing/gmock/include/gmock/gmock.h" 9 10 namespace cc { 11 namespace { 12 13 class Data { 14 public: 15 static scoped_ptr<Data> Create(int i) { return make_scoped_ptr(new Data(i)); } 16 int data() const { return data_; } 17 private: 18 explicit Data(int i) : data_(i) {} 19 int data_; 20 }; 21 22 class IsOddPredicate { 23 public: 24 bool operator()(const Data* data) { return (data->data() % 2) == 1; } 25 }; 26 27 TEST(ScopedPtrVectorTest, PushBack) { 28 ScopedPtrVector<Data> v; 29 30 // Insert 5 things into the vector. 31 v.push_back(Data::Create(1)); 32 v.push_back(Data::Create(2)); 33 v.push_back(Data::Create(3)); 34 v.push_back(Data::Create(4)); 35 v.push_back(Data::Create(5)); 36 37 EXPECT_EQ(5u, v.size()); 38 EXPECT_EQ(1, v[0]->data()); 39 EXPECT_EQ(2, v[1]->data()); 40 EXPECT_EQ(3, v[2]->data()); 41 EXPECT_EQ(4, v[3]->data()); 42 EXPECT_EQ(5, v[4]->data()); 43 } 44 45 TEST(ScopedPtrVectorTest, InsertAndTake) { 46 // Insert 3 things into each vector. 47 ScopedPtrVector<Data> v; 48 v.push_back(Data::Create(1)); 49 v.push_back(Data::Create(2)); 50 v.push_back(Data::Create(6)); 51 52 ScopedPtrVector<Data> v2; 53 v2.push_back(Data::Create(3)); 54 v2.push_back(Data::Create(4)); 55 v2.push_back(Data::Create(5)); 56 57 ScopedPtrVector<Data>::iterator it = v.begin(); 58 ++it; 59 ++it; 60 EXPECT_EQ(6, (*it)->data()); 61 62 v.insert_and_take(it, v2); 63 64 EXPECT_EQ(6u, v.size()); 65 EXPECT_EQ(1, v[0]->data()); 66 EXPECT_EQ(2, v[1]->data()); 67 EXPECT_EQ(3, v[2]->data()); 68 EXPECT_EQ(4, v[3]->data()); 69 EXPECT_EQ(5, v[4]->data()); 70 EXPECT_EQ(6, v[5]->data()); 71 72 EXPECT_EQ(3u, v2.size()); 73 EXPECT_EQ(NULL, v2[0]); 74 EXPECT_EQ(NULL, v2[1]); 75 EXPECT_EQ(NULL, v2[2]); 76 } 77 78 TEST(ScopedPtrVectorTest, Partition) { 79 ScopedPtrVector<Data> v; 80 v.push_back(Data::Create(1)); 81 v.push_back(Data::Create(2)); 82 v.push_back(Data::Create(3)); 83 v.push_back(Data::Create(4)); 84 v.push_back(Data::Create(5)); 85 86 ScopedPtrVector<Data>::iterator it = v.partition(IsOddPredicate()); 87 std::set<int> odd_numbers; 88 for (ScopedPtrVector<Data>::iterator second_it = v.begin(); 89 second_it != it; 90 ++second_it) { 91 EXPECT_EQ(1, (*second_it)->data() % 2); 92 odd_numbers.insert((*second_it)->data()); 93 } 94 EXPECT_EQ(3u, odd_numbers.size()); 95 96 std::set<int> even_numbers; 97 for (; it != v.end(); ++it) { 98 EXPECT_EQ(0, (*it)->data() % 2); 99 even_numbers.insert((*it)->data()); 100 } 101 EXPECT_EQ(2u, even_numbers.size()); 102 } 103 104 } // namespace 105 } // namespace cc 106