hash_policy_traits_test.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include "absl/container/internal/hash_policy_traits.h"
  15. #include <functional>
  16. #include <memory>
  17. #include <new>
  18. #include "gmock/gmock.h"
  19. #include "gtest/gtest.h"
  20. namespace absl {
  21. ABSL_NAMESPACE_BEGIN
  22. namespace container_internal {
  23. namespace {
  24. using ::testing::MockFunction;
  25. using ::testing::Return;
  26. using ::testing::ReturnRef;
  27. using Alloc = std::allocator<int>;
  28. using Slot = int;
  29. struct PolicyWithoutOptionalOps {
  30. using slot_type = Slot;
  31. using key_type = Slot;
  32. using init_type = Slot;
  33. static std::function<void(void*, Slot*, Slot)> construct;
  34. static std::function<void(void*, Slot*)> destroy;
  35. static std::function<Slot&(Slot*)> element;
  36. static int apply(int v) { return apply_impl(v); }
  37. static std::function<int(int)> apply_impl;
  38. static std::function<Slot&(Slot*)> value;
  39. };
  40. std::function<void(void*, Slot*, Slot)> PolicyWithoutOptionalOps::construct;
  41. std::function<void(void*, Slot*)> PolicyWithoutOptionalOps::destroy;
  42. std::function<Slot&(Slot*)> PolicyWithoutOptionalOps::element;
  43. std::function<int(int)> PolicyWithoutOptionalOps::apply_impl;
  44. std::function<Slot&(Slot*)> PolicyWithoutOptionalOps::value;
  45. struct PolicyWithOptionalOps : PolicyWithoutOptionalOps {
  46. static std::function<void(void*, Slot*, Slot*)> transfer;
  47. };
  48. std::function<void(void*, Slot*, Slot*)> PolicyWithOptionalOps::transfer;
  49. struct Test : ::testing::Test {
  50. Test() {
  51. PolicyWithoutOptionalOps::construct = [&](void* a1, Slot* a2, Slot a3) {
  52. construct.Call(a1, a2, std::move(a3));
  53. };
  54. PolicyWithoutOptionalOps::destroy = [&](void* a1, Slot* a2) {
  55. destroy.Call(a1, a2);
  56. };
  57. PolicyWithoutOptionalOps::element = [&](Slot* a1) -> Slot& {
  58. return element.Call(a1);
  59. };
  60. PolicyWithoutOptionalOps::apply_impl = [&](int a1) -> int {
  61. return apply.Call(a1);
  62. };
  63. PolicyWithoutOptionalOps::value = [&](Slot* a1) -> Slot& {
  64. return value.Call(a1);
  65. };
  66. PolicyWithOptionalOps::transfer = [&](void* a1, Slot* a2, Slot* a3) {
  67. return transfer.Call(a1, a2, a3);
  68. };
  69. }
  70. std::allocator<int> alloc;
  71. int a = 53;
  72. MockFunction<void(void*, Slot*, Slot)> construct;
  73. MockFunction<void(void*, Slot*)> destroy;
  74. MockFunction<Slot&(Slot*)> element;
  75. MockFunction<int(int)> apply;
  76. MockFunction<Slot&(Slot*)> value;
  77. MockFunction<void(void*, Slot*, Slot*)> transfer;
  78. };
  79. TEST_F(Test, construct) {
  80. EXPECT_CALL(construct, Call(&alloc, &a, 53));
  81. hash_policy_traits<PolicyWithoutOptionalOps>::construct(&alloc, &a, 53);
  82. }
  83. TEST_F(Test, destroy) {
  84. EXPECT_CALL(destroy, Call(&alloc, &a));
  85. hash_policy_traits<PolicyWithoutOptionalOps>::destroy(&alloc, &a);
  86. }
  87. TEST_F(Test, element) {
  88. int b = 0;
  89. EXPECT_CALL(element, Call(&a)).WillOnce(ReturnRef(b));
  90. EXPECT_EQ(&b, &hash_policy_traits<PolicyWithoutOptionalOps>::element(&a));
  91. }
  92. TEST_F(Test, apply) {
  93. EXPECT_CALL(apply, Call(42)).WillOnce(Return(1337));
  94. EXPECT_EQ(1337, (hash_policy_traits<PolicyWithoutOptionalOps>::apply(42)));
  95. }
  96. TEST_F(Test, value) {
  97. int b = 0;
  98. EXPECT_CALL(value, Call(&a)).WillOnce(ReturnRef(b));
  99. EXPECT_EQ(&b, &hash_policy_traits<PolicyWithoutOptionalOps>::value(&a));
  100. }
  101. TEST_F(Test, without_transfer) {
  102. int b = 42;
  103. EXPECT_CALL(element, Call(&b)).WillOnce(::testing::ReturnRef(b));
  104. EXPECT_CALL(construct, Call(&alloc, &a, b));
  105. EXPECT_CALL(destroy, Call(&alloc, &b));
  106. hash_policy_traits<PolicyWithoutOptionalOps>::transfer(&alloc, &a, &b);
  107. }
  108. TEST_F(Test, with_transfer) {
  109. int b = 42;
  110. EXPECT_CALL(transfer, Call(&alloc, &a, &b));
  111. hash_policy_traits<PolicyWithOptionalOps>::transfer(&alloc, &a, &b);
  112. }
  113. } // namespace
  114. } // namespace container_internal
  115. ABSL_NAMESPACE_END
  116. } // namespace absl