1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/container/internal/hash_policy_traits.h"
16 
17 #include <functional>
18 #include <memory>
19 #include <new>
20 
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 
24 namespace absl {
25 ABSL_NAMESPACE_BEGIN
26 namespace container_internal {
27 namespace {
28 
29 using ::testing::MockFunction;
30 using ::testing::Return;
31 using ::testing::ReturnRef;
32 
33 using Alloc = std::allocator<int>;
34 using Slot = int;
35 
36 struct PolicyWithoutOptionalOps {
37   using slot_type = Slot;
38   using key_type = Slot;
39   using init_type = Slot;
40 
41   static std::function<void(void*, Slot*, Slot)> construct;
42   static std::function<void(void*, Slot*)> destroy;
43 
44   static std::function<Slot&(Slot*)> element;
applyabsl::container_internal::__anon1afdb54b0111::PolicyWithoutOptionalOps45   static int apply(int v) { return apply_impl(v); }
46   static std::function<int(int)> apply_impl;
47   static std::function<Slot&(Slot*)> value;
48 };
49 
50 std::function<void(void*, Slot*, Slot)> PolicyWithoutOptionalOps::construct;
51 std::function<void(void*, Slot*)> PolicyWithoutOptionalOps::destroy;
52 
53 std::function<Slot&(Slot*)> PolicyWithoutOptionalOps::element;
54 std::function<int(int)> PolicyWithoutOptionalOps::apply_impl;
55 std::function<Slot&(Slot*)> PolicyWithoutOptionalOps::value;
56 
57 struct PolicyWithOptionalOps : PolicyWithoutOptionalOps {
58   static std::function<void(void*, Slot*, Slot*)> transfer;
59 };
60 
61 std::function<void(void*, Slot*, Slot*)> PolicyWithOptionalOps::transfer;
62 
63 struct Test : ::testing::Test {
Testabsl::container_internal::__anon1afdb54b0111::Test64   Test() {
65     PolicyWithoutOptionalOps::construct = [&](void* a1, Slot* a2, Slot a3) {
66       construct.Call(a1, a2, std::move(a3));
67     };
68     PolicyWithoutOptionalOps::destroy = [&](void* a1, Slot* a2) {
69       destroy.Call(a1, a2);
70     };
71 
72     PolicyWithoutOptionalOps::element = [&](Slot* a1) -> Slot& {
73       return element.Call(a1);
74     };
75     PolicyWithoutOptionalOps::apply_impl = [&](int a1) -> int {
76       return apply.Call(a1);
77     };
78     PolicyWithoutOptionalOps::value = [&](Slot* a1) -> Slot& {
79       return value.Call(a1);
80     };
81 
82     PolicyWithOptionalOps::transfer = [&](void* a1, Slot* a2, Slot* a3) {
83       return transfer.Call(a1, a2, a3);
84     };
85   }
86 
87   std::allocator<int> alloc;
88   int a = 53;
89 
90   MockFunction<void(void*, Slot*, Slot)> construct;
91   MockFunction<void(void*, Slot*)> destroy;
92 
93   MockFunction<Slot&(Slot*)> element;
94   MockFunction<int(int)> apply;
95   MockFunction<Slot&(Slot*)> value;
96 
97   MockFunction<void(void*, Slot*, Slot*)> transfer;
98 };
99 
TEST_F(Test,construct)100 TEST_F(Test, construct) {
101   EXPECT_CALL(construct, Call(&alloc, &a, 53));
102   hash_policy_traits<PolicyWithoutOptionalOps>::construct(&alloc, &a, 53);
103 }
104 
TEST_F(Test,destroy)105 TEST_F(Test, destroy) {
106   EXPECT_CALL(destroy, Call(&alloc, &a));
107   hash_policy_traits<PolicyWithoutOptionalOps>::destroy(&alloc, &a);
108 }
109 
TEST_F(Test,element)110 TEST_F(Test, element) {
111   int b = 0;
112   EXPECT_CALL(element, Call(&a)).WillOnce(ReturnRef(b));
113   EXPECT_EQ(&b, &hash_policy_traits<PolicyWithoutOptionalOps>::element(&a));
114 }
115 
TEST_F(Test,apply)116 TEST_F(Test, apply) {
117   EXPECT_CALL(apply, Call(42)).WillOnce(Return(1337));
118   EXPECT_EQ(1337, (hash_policy_traits<PolicyWithoutOptionalOps>::apply(42)));
119 }
120 
TEST_F(Test,value)121 TEST_F(Test, value) {
122   int b = 0;
123   EXPECT_CALL(value, Call(&a)).WillOnce(ReturnRef(b));
124   EXPECT_EQ(&b, &hash_policy_traits<PolicyWithoutOptionalOps>::value(&a));
125 }
126 
TEST_F(Test,without_transfer)127 TEST_F(Test, without_transfer) {
128   int b = 42;
129   EXPECT_CALL(element, Call(&b)).WillOnce(::testing::ReturnRef(b));
130   EXPECT_CALL(construct, Call(&alloc, &a, b));
131   EXPECT_CALL(destroy, Call(&alloc, &b));
132   hash_policy_traits<PolicyWithoutOptionalOps>::transfer(&alloc, &a, &b);
133 }
134 
TEST_F(Test,with_transfer)135 TEST_F(Test, with_transfer) {
136   int b = 42;
137   EXPECT_CALL(transfer, Call(&alloc, &a, &b));
138   hash_policy_traits<PolicyWithOptionalOps>::transfer(&alloc, &a, &b);
139 }
140 
141 }  // namespace
142 }  // namespace container_internal
143 ABSL_NAMESPACE_END
144 }  // namespace absl
145