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<Slot&(Slot*)> element;
applyabsl::container_internal::__anon8600eb060111::PolicyWithoutOptionalOps42   static int apply(int v) { return apply_impl(v); }
43   static std::function<int(int)> apply_impl;
44   static std::function<Slot&(Slot*)> value;
45 };
46 
47 std::function<int(int)> PolicyWithoutOptionalOps::apply_impl;
48 std::function<Slot&(Slot*)> PolicyWithoutOptionalOps::value;
49 
50 struct Test : ::testing::Test {
Testabsl::container_internal::__anon8600eb060111::Test51   Test() {
52     PolicyWithoutOptionalOps::apply_impl = [&](int a1) -> int {
53       return apply.Call(a1);
54     };
55     PolicyWithoutOptionalOps::value = [&](Slot* a1) -> Slot& {
56       return value.Call(a1);
57     };
58   }
59 
60   std::allocator<int> alloc;
61   int a = 53;
62   MockFunction<int(int)> apply;
63   MockFunction<Slot&(Slot*)> value;
64 };
65 
TEST_F(Test,apply)66 TEST_F(Test, apply) {
67   EXPECT_CALL(apply, Call(42)).WillOnce(Return(1337));
68   EXPECT_EQ(1337, (hash_policy_traits<PolicyWithoutOptionalOps>::apply(42)));
69 }
70 
TEST_F(Test,value)71 TEST_F(Test, value) {
72   int b = 0;
73   EXPECT_CALL(value, Call(&a)).WillOnce(ReturnRef(b));
74   EXPECT_EQ(&b, &hash_policy_traits<PolicyWithoutOptionalOps>::value(&a));
75 }
76 
77 }  // namespace
78 }  // namespace container_internal
79 ABSL_NAMESPACE_END
80 }  // namespace absl
81