hash_policy_traits_test.cc
Go to the documentation of this file.
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 
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 namespace container_internal {
26 namespace {
27 
28 using ::testing::MockFunction;
29 using ::testing::Return;
30 using ::testing::ReturnRef;
31 
32 using Alloc = std::allocator<int>;
33 using Slot = int;
34 
35 struct PolicyWithoutOptionalOps {
36  using slot_type = Slot;
37  using key_type = Slot;
38  using init_type = Slot;
39 
40  static std::function<void(void*, Slot*, Slot)> construct;
41  static std::function<void(void*, Slot*)> destroy;
42 
43  static std::function<Slot&(Slot*)> element;
44  static int apply(int v) { return apply_impl(v); }
45  static std::function<int(int)> apply_impl;
46  static std::function<Slot&(Slot*)> value;
47 };
48 
49 std::function<void(void*, Slot*, Slot)> PolicyWithoutOptionalOps::construct;
50 std::function<void(void*, Slot*)> PolicyWithoutOptionalOps::destroy;
51 
52 std::function<Slot&(Slot*)> PolicyWithoutOptionalOps::element;
53 std::function<int(int)> PolicyWithoutOptionalOps::apply_impl;
54 std::function<Slot&(Slot*)> PolicyWithoutOptionalOps::value;
55 
56 struct PolicyWithOptionalOps : PolicyWithoutOptionalOps {
57  static std::function<void(void*, Slot*, Slot*)> transfer;
58 };
59 
60 std::function<void(void*, Slot*, Slot*)> PolicyWithOptionalOps::transfer;
61 
62 struct Test : ::testing::Test {
63  Test() {
64  PolicyWithoutOptionalOps::construct = [&](void* a1, Slot* a2, Slot a3) {
65  construct.Call(a1, a2, std::move(a3));
66  };
67  PolicyWithoutOptionalOps::destroy = [&](void* a1, Slot* a2) {
68  destroy.Call(a1, a2);
69  };
70 
71  PolicyWithoutOptionalOps::element = [&](Slot* a1) -> Slot& {
72  return element.Call(a1);
73  };
74  PolicyWithoutOptionalOps::apply_impl = [&](int a1) -> int {
75  return apply.Call(a1);
76  };
77  PolicyWithoutOptionalOps::value = [&](Slot* a1) -> Slot& {
78  return value.Call(a1);
79  };
80 
81  PolicyWithOptionalOps::transfer = [&](void* a1, Slot* a2, Slot* a3) {
82  return transfer.Call(a1, a2, a3);
83  };
84  }
85 
86  std::allocator<int> alloc;
87  int a = 53;
88 
89  MockFunction<void(void*, Slot*, Slot)> construct;
90  MockFunction<void(void*, Slot*)> destroy;
91 
92  MockFunction<Slot&(Slot*)> element;
93  MockFunction<int(int)> apply;
94  MockFunction<Slot&(Slot*)> value;
95 
96  MockFunction<void(void*, Slot*, Slot*)> transfer;
97 };
98 
99 TEST_F(Test, construct) {
100  EXPECT_CALL(construct, Call(&alloc, &a, 53));
102 }
103 
104 TEST_F(Test, destroy) {
105  EXPECT_CALL(destroy, Call(&alloc, &a));
107 }
108 
109 TEST_F(Test, element) {
110  int b = 0;
111  EXPECT_CALL(element, Call(&a)).WillOnce(ReturnRef(b));
113 }
114 
115 TEST_F(Test, apply) {
116  EXPECT_CALL(apply, Call(42)).WillOnce(Return(1337));
118 }
119 
120 TEST_F(Test, value) {
121  int b = 0;
122  EXPECT_CALL(value, Call(&a)).WillOnce(ReturnRef(b));
124 }
125 
126 TEST_F(Test, without_transfer) {
127  int b = 42;
128  EXPECT_CALL(element, Call(&b)).WillOnce(::testing::ReturnRef(b));
129  EXPECT_CALL(construct, Call(&alloc, &a, b));
130  EXPECT_CALL(destroy, Call(&alloc, &b));
132 }
133 
134 TEST_F(Test, with_transfer) {
135  int b = 42;
136  EXPECT_CALL(transfer, Call(&alloc, &a, &b));
138 }
139 
140 } // namespace
141 } // namespace container_internal
142 } // namespace absl
int v
Definition: variant_test.cc:81
static std::function< Slot &(Slot *)> element
static std::function< int(int)> apply_impl
static std::function< Slot &(Slot *)> value
static void construct(Alloc *alloc, slot_type *slot, Args &&... args)
T::first_type a1
static void transfer(Alloc *alloc, slot_type *new_slot, slot_type *old_slot)
Definition: algorithm.h:29
static std::function< void(void *, Slot *)> destroy
static auto value(T *elem) -> decltype(P::value(elem))
TEST_F(GraphCyclesTest, NoCycle)
static std::function< void(void *, Slot *, Slot *)> transfer
static std::function< void(void *, Slot *, Slot)> construct
static auto apply(F &&f, Ts &&... ts) -> decltype(P::apply(std::forward< F >(f), std::forward< Ts >(ts)...))
auto apply(Functor &&functor, Tuple &&t) -> decltype(utility_internal::apply_helper(absl::forward< Functor >(functor), absl::forward< Tuple >(t), absl::make_index_sequence< std::tuple_size< typename std::remove_reference< Tuple >::type >::value >
Definition: utility.h:287
T::first_type a2
uint64_t b
Definition: layout_test.cc:50
std::allocator< int > alloc
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: utility.h:219
static void destroy(Alloc *alloc, slot_type *slot)
static auto element(slot_type *slot) -> decltype(P::element(slot))


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:18