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