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
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 #include "absl/base/attributes.h"
20 #include "absl/base/internal/atomic_hook_test_helper.h"
21
22 namespace {
23
24 using ::testing::Eq;
25
26 int value = 0;
TestHook(int x)27 void TestHook(int x) { value = x; }
28
TEST(AtomicHookTest,NoDefaultFunction)29 TEST(AtomicHookTest, NoDefaultFunction) {
30 ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES static absl::base_internal::AtomicHook<
31 void (*)(int)>
32 hook;
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
TEST(AtomicHookTest,WithDefaultFunction)56 TEST(AtomicHookTest, WithDefaultFunction) {
57 // Set the default value to TestHook at compile-time.
58 ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES static absl::base_internal::AtomicHook<
59 void (*)(int)>
60 hook(TestHook);
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
77 ABSL_CONST_INIT int override_func_calls = 0;
OverrideFunc()78 void OverrideFunc() { override_func_calls++; }
79 static struct OverrideInstaller {
OverrideInstaller__anone4f192db0111::OverrideInstaller80 OverrideInstaller() { absl::atomic_hook_internal::func.Store(OverrideFunc); }
81 } override_installer;
82
TEST(AtomicHookTest,DynamicInitFromAnotherTU)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
97 } // namespace
98