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