atomic_hook_test.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/base/internal/atomic_hook.h"
  15. #include "gmock/gmock.h"
  16. #include "gtest/gtest.h"
  17. #include "absl/base/attributes.h"
  18. #include "absl/base/internal/atomic_hook_test_helper.h"
  19. namespace {
  20. using ::testing::Eq;
  21. int value = 0;
  22. void TestHook(int x) { value = x; }
  23. TEST(AtomicHookTest, NoDefaultFunction) {
  24. ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES static absl::base_internal::AtomicHook<
  25. void (*)(int)>
  26. hook;
  27. value = 0;
  28. // Test the default DummyFunction.
  29. EXPECT_TRUE(hook.Load() == nullptr);
  30. EXPECT_EQ(value, 0);
  31. hook(1);
  32. EXPECT_EQ(value, 0);
  33. // Test a stored hook.
  34. hook.Store(TestHook);
  35. EXPECT_TRUE(hook.Load() == TestHook);
  36. EXPECT_EQ(value, 0);
  37. hook(1);
  38. EXPECT_EQ(value, 1);
  39. // Calling Store() with the same hook should not crash.
  40. hook.Store(TestHook);
  41. EXPECT_TRUE(hook.Load() == TestHook);
  42. EXPECT_EQ(value, 1);
  43. hook(2);
  44. EXPECT_EQ(value, 2);
  45. }
  46. TEST(AtomicHookTest, WithDefaultFunction) {
  47. // Set the default value to TestHook at compile-time.
  48. ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES static absl::base_internal::AtomicHook<
  49. void (*)(int)>
  50. hook(TestHook);
  51. value = 0;
  52. // Test the default value is TestHook.
  53. EXPECT_TRUE(hook.Load() == TestHook);
  54. EXPECT_EQ(value, 0);
  55. hook(1);
  56. EXPECT_EQ(value, 1);
  57. // Calling Store() with the same hook should not crash.
  58. hook.Store(TestHook);
  59. EXPECT_TRUE(hook.Load() == TestHook);
  60. EXPECT_EQ(value, 1);
  61. hook(2);
  62. EXPECT_EQ(value, 2);
  63. }
  64. ABSL_CONST_INIT int override_func_calls = 0;
  65. void OverrideFunc() { override_func_calls++; }
  66. static struct OverrideInstaller {
  67. OverrideInstaller() { absl::atomic_hook_internal::func.Store(OverrideFunc); }
  68. } override_installer;
  69. TEST(AtomicHookTest, DynamicInitFromAnotherTU) {
  70. // MSVC 14.2 doesn't do constexpr static init correctly; in particular it
  71. // tends to sequence static init (i.e. defaults) of `AtomicHook` objects
  72. // after their dynamic init (i.e. overrides), overwriting whatever value was
  73. // written during dynamic init. This regression test validates the fix.
  74. // https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html
  75. EXPECT_THAT(absl::atomic_hook_internal::default_func_calls, Eq(0));
  76. EXPECT_THAT(override_func_calls, Eq(0));
  77. absl::atomic_hook_internal::func();
  78. EXPECT_THAT(absl::atomic_hook_internal::default_func_calls, Eq(0));
  79. EXPECT_THAT(override_func_calls, Eq(1));
  80. EXPECT_THAT(absl::atomic_hook_internal::func.Load(), Eq(OverrideFunc));
  81. }
  82. } // namespace