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