node_hash_policy_test.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/node_hash_policy.h"
  15. #include <memory>
  16. #include "gmock/gmock.h"
  17. #include "gtest/gtest.h"
  18. #include "absl/container/internal/hash_policy_traits.h"
  19. namespace absl {
  20. ABSL_NAMESPACE_BEGIN
  21. namespace container_internal {
  22. namespace {
  23. using ::testing::Pointee;
  24. struct Policy : node_hash_policy<int&, Policy> {
  25. using key_type = int;
  26. using init_type = int;
  27. template <class Alloc>
  28. static int* new_element(Alloc* alloc, int value) {
  29. return new int(value);
  30. }
  31. template <class Alloc>
  32. static void delete_element(Alloc* alloc, int* elem) {
  33. delete elem;
  34. }
  35. };
  36. using NodePolicy = hash_policy_traits<Policy>;
  37. struct NodeTest : ::testing::Test {
  38. std::allocator<int> alloc;
  39. int n = 53;
  40. int* a = &n;
  41. };
  42. TEST_F(NodeTest, ConstructDestroy) {
  43. NodePolicy::construct(&alloc, &a, 42);
  44. EXPECT_THAT(a, Pointee(42));
  45. NodePolicy::destroy(&alloc, &a);
  46. }
  47. TEST_F(NodeTest, transfer) {
  48. int s = 42;
  49. int* b = &s;
  50. NodePolicy::transfer(&alloc, &a, &b);
  51. EXPECT_EQ(&s, a);
  52. }
  53. } // namespace
  54. } // namespace container_internal
  55. ABSL_NAMESPACE_END
  56. } // namespace absl