1 // Copyright 2017 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 "base/containers/unique_ptr_adapters.h"
6
7 #include <memory>
8 #include <vector>
9
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace base {
13 namespace {
14
15 class Foo {
16 public:
Foo()17 Foo() { instance_count++; }
~Foo()18 ~Foo() { instance_count--; }
19 static int instance_count;
20 };
21
22 int Foo::instance_count = 0;
23
TEST(UniquePtrComparatorTest,Basic)24 TEST(UniquePtrComparatorTest, Basic) {
25 std::set<std::unique_ptr<Foo>, UniquePtrComparator> set;
26 Foo* foo1 = new Foo();
27 Foo* foo2 = new Foo();
28 Foo* foo3 = new Foo();
29 EXPECT_EQ(3, Foo::instance_count);
30
31 set.emplace(foo1);
32 set.emplace(foo2);
33
34 auto it1 = set.find(foo1);
35 EXPECT_TRUE(it1 != set.end());
36 EXPECT_EQ(foo1, it1->get());
37
38 {
39 auto it2 = set.find(foo2);
40 EXPECT_TRUE(it2 != set.end());
41 EXPECT_EQ(foo2, it2->get());
42 }
43
44 EXPECT_TRUE(set.find(foo3) == set.end());
45
46 set.erase(it1);
47 EXPECT_EQ(2, Foo::instance_count);
48
49 EXPECT_TRUE(set.find(foo1) == set.end());
50
51 {
52 auto it2 = set.find(foo2);
53 EXPECT_TRUE(it2 != set.end());
54 EXPECT_EQ(foo2, it2->get());
55 }
56
57 set.clear();
58 EXPECT_EQ(1, Foo::instance_count);
59
60 EXPECT_TRUE(set.find(foo1) == set.end());
61 EXPECT_TRUE(set.find(foo2) == set.end());
62 EXPECT_TRUE(set.find(foo3) == set.end());
63
64 delete foo3;
65 EXPECT_EQ(0, Foo::instance_count);
66 }
67
TEST(UniquePtrMatcherTest,Basic)68 TEST(UniquePtrMatcherTest, Basic) {
69 std::vector<std::unique_ptr<Foo>> v;
70 auto foo_ptr1 = std::make_unique<Foo>();
71 Foo* foo1 = foo_ptr1.get();
72 v.push_back(std::move(foo_ptr1));
73 auto foo_ptr2 = std::make_unique<Foo>();
74 Foo* foo2 = foo_ptr2.get();
75 v.push_back(std::move(foo_ptr2));
76
77 {
78 auto iter = std::find_if(v.begin(), v.end(), UniquePtrMatcher<Foo>(foo1));
79 ASSERT_TRUE(iter != v.end());
80 EXPECT_EQ(foo1, iter->get());
81 }
82
83 {
84 auto iter = std::find_if(v.begin(), v.end(), UniquePtrMatcher<Foo>(foo2));
85 ASSERT_TRUE(iter != v.end());
86 EXPECT_EQ(foo2, iter->get());
87 }
88
89 {
90 auto iter = std::find_if(v.begin(), v.end(), MatchesUniquePtr(foo2));
91 ASSERT_TRUE(iter != v.end());
92 EXPECT_EQ(foo2, iter->get());
93 }
94 }
95
96 class TestDeleter {
97 public:
operator ()(Foo * foo)98 void operator()(Foo* foo) { delete foo; }
99 };
100
TEST(UniquePtrMatcherTest,Deleter)101 TEST(UniquePtrMatcherTest, Deleter) {
102 using UniqueFoo = std::unique_ptr<Foo, TestDeleter>;
103 std::vector<UniqueFoo> v;
104 UniqueFoo foo_ptr1(new Foo);
105 Foo* foo1 = foo_ptr1.get();
106 v.push_back(std::move(foo_ptr1));
107 UniqueFoo foo_ptr2(new Foo);
108 Foo* foo2 = foo_ptr2.get();
109 v.push_back(std::move(foo_ptr2));
110
111 {
112 auto iter = std::find_if(v.begin(), v.end(),
113 UniquePtrMatcher<Foo, TestDeleter>(foo1));
114 ASSERT_TRUE(iter != v.end());
115 EXPECT_EQ(foo1, iter->get());
116 }
117
118 {
119 auto iter = std::find_if(v.begin(), v.end(),
120 UniquePtrMatcher<Foo, TestDeleter>(foo2));
121 ASSERT_TRUE(iter != v.end());
122 EXPECT_EQ(foo2, iter->get());
123 }
124
125 {
126 auto iter = std::find_if(v.begin(), v.end(),
127 MatchesUniquePtr<Foo, TestDeleter>(foo2));
128 ASSERT_TRUE(iter != v.end());
129 EXPECT_EQ(foo2, iter->get());
130 }
131 }
132
133 } // namespace
134 } // namespace base
135