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/node_slot_policy.h"
16
17 #include <memory>
18
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include "absl/base/config.h"
22 #include "absl/container/internal/hash_policy_traits.h"
23
24 namespace absl {
25 ABSL_NAMESPACE_BEGIN
26 namespace container_internal {
27 namespace {
28
29 using ::testing::Pointee;
30
31 struct Policy : node_slot_policy<int&, Policy> {
32 using key_type = int;
33 using init_type = int;
34
35 template <class Alloc>
new_elementabsl::container_internal::__anona710432e0111::Policy36 static int* new_element(Alloc* alloc, int value) {
37 return new int(value);
38 }
39
40 template <class Alloc>
delete_elementabsl::container_internal::__anona710432e0111::Policy41 static void delete_element(Alloc* alloc, int* elem) {
42 delete elem;
43 }
44 };
45
46 using NodePolicy = hash_policy_traits<Policy>;
47
48 struct NodeTest : ::testing::Test {
49 std::allocator<int> alloc;
50 int n = 53;
51 int* a = &n;
52 };
53
TEST_F(NodeTest,ConstructDestroy)54 TEST_F(NodeTest, ConstructDestroy) {
55 NodePolicy::construct(&alloc, &a, 42);
56 EXPECT_THAT(a, Pointee(42));
57 NodePolicy::destroy(&alloc, &a);
58 }
59
TEST_F(NodeTest,transfer)60 TEST_F(NodeTest, transfer) {
61 int s = 42;
62 int* b = &s;
63 NodePolicy::transfer(&alloc, &a, &b);
64 EXPECT_EQ(&s, a);
65 EXPECT_TRUE(NodePolicy::transfer_uses_memcpy());
66 }
67
68 } // namespace
69 } // namespace container_internal
70 ABSL_NAMESPACE_END
71 } // namespace absl
72