00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/container/internal/hash_policy_traits.h"
00016
00017 #include <functional>
00018 #include <memory>
00019 #include <new>
00020
00021 #include "gmock/gmock.h"
00022 #include "gtest/gtest.h"
00023
00024 namespace absl {
00025 namespace container_internal {
00026 namespace {
00027
00028 using ::testing::MockFunction;
00029 using ::testing::Return;
00030 using ::testing::ReturnRef;
00031
00032 using Alloc = std::allocator<int>;
00033 using Slot = int;
00034
00035 struct PolicyWithoutOptionalOps {
00036 using slot_type = Slot;
00037 using key_type = Slot;
00038 using init_type = Slot;
00039
00040 static std::function<void(void*, Slot*, Slot)> construct;
00041 static std::function<void(void*, Slot*)> destroy;
00042
00043 static std::function<Slot&(Slot*)> element;
00044 static int apply(int v) { return apply_impl(v); }
00045 static std::function<int(int)> apply_impl;
00046 static std::function<Slot&(Slot*)> value;
00047 };
00048
00049 std::function<void(void*, Slot*, Slot)> PolicyWithoutOptionalOps::construct;
00050 std::function<void(void*, Slot*)> PolicyWithoutOptionalOps::destroy;
00051
00052 std::function<Slot&(Slot*)> PolicyWithoutOptionalOps::element;
00053 std::function<int(int)> PolicyWithoutOptionalOps::apply_impl;
00054 std::function<Slot&(Slot*)> PolicyWithoutOptionalOps::value;
00055
00056 struct PolicyWithOptionalOps : PolicyWithoutOptionalOps {
00057 static std::function<void(void*, Slot*, Slot*)> transfer;
00058 };
00059
00060 std::function<void(void*, Slot*, Slot*)> PolicyWithOptionalOps::transfer;
00061
00062 struct Test : ::testing::Test {
00063 Test() {
00064 PolicyWithoutOptionalOps::construct = [&](void* a1, Slot* a2, Slot a3) {
00065 construct.Call(a1, a2, std::move(a3));
00066 };
00067 PolicyWithoutOptionalOps::destroy = [&](void* a1, Slot* a2) {
00068 destroy.Call(a1, a2);
00069 };
00070
00071 PolicyWithoutOptionalOps::element = [&](Slot* a1) -> Slot& {
00072 return element.Call(a1);
00073 };
00074 PolicyWithoutOptionalOps::apply_impl = [&](int a1) -> int {
00075 return apply.Call(a1);
00076 };
00077 PolicyWithoutOptionalOps::value = [&](Slot* a1) -> Slot& {
00078 return value.Call(a1);
00079 };
00080
00081 PolicyWithOptionalOps::transfer = [&](void* a1, Slot* a2, Slot* a3) {
00082 return transfer.Call(a1, a2, a3);
00083 };
00084 }
00085
00086 std::allocator<int> alloc;
00087 int a = 53;
00088
00089 MockFunction<void(void*, Slot*, Slot)> construct;
00090 MockFunction<void(void*, Slot*)> destroy;
00091
00092 MockFunction<Slot&(Slot*)> element;
00093 MockFunction<int(int)> apply;
00094 MockFunction<Slot&(Slot*)> value;
00095
00096 MockFunction<void(void*, Slot*, Slot*)> transfer;
00097 };
00098
00099 TEST_F(Test, construct) {
00100 EXPECT_CALL(construct, Call(&alloc, &a, 53));
00101 hash_policy_traits<PolicyWithoutOptionalOps>::construct(&alloc, &a, 53);
00102 }
00103
00104 TEST_F(Test, destroy) {
00105 EXPECT_CALL(destroy, Call(&alloc, &a));
00106 hash_policy_traits<PolicyWithoutOptionalOps>::destroy(&alloc, &a);
00107 }
00108
00109 TEST_F(Test, element) {
00110 int b = 0;
00111 EXPECT_CALL(element, Call(&a)).WillOnce(ReturnRef(b));
00112 EXPECT_EQ(&b, &hash_policy_traits<PolicyWithoutOptionalOps>::element(&a));
00113 }
00114
00115 TEST_F(Test, apply) {
00116 EXPECT_CALL(apply, Call(42)).WillOnce(Return(1337));
00117 EXPECT_EQ(1337, (hash_policy_traits<PolicyWithoutOptionalOps>::apply(42)));
00118 }
00119
00120 TEST_F(Test, value) {
00121 int b = 0;
00122 EXPECT_CALL(value, Call(&a)).WillOnce(ReturnRef(b));
00123 EXPECT_EQ(&b, &hash_policy_traits<PolicyWithoutOptionalOps>::value(&a));
00124 }
00125
00126 TEST_F(Test, without_transfer) {
00127 int b = 42;
00128 EXPECT_CALL(element, Call(&b)).WillOnce(::testing::ReturnRef(b));
00129 EXPECT_CALL(construct, Call(&alloc, &a, b));
00130 EXPECT_CALL(destroy, Call(&alloc, &b));
00131 hash_policy_traits<PolicyWithoutOptionalOps>::transfer(&alloc, &a, &b);
00132 }
00133
00134 TEST_F(Test, with_transfer) {
00135 int b = 42;
00136 EXPECT_CALL(transfer, Call(&alloc, &a, &b));
00137 hash_policy_traits<PolicyWithOptionalOps>::transfer(&alloc, &a, &b);
00138 }
00139
00140 }
00141 }
00142 }