blob: e577a8fd930bfbf16d62c3810b2338a06f225935 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// 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 Schuhb4691e92020-12-31 12:37:18 -080017#include "gmock/gmock.h"
Austin Schuh36244a12019-09-21 17:52:38 -070018#include "gtest/gtest.h"
19#include "absl/base/attributes.h"
Austin Schuhb4691e92020-12-31 12:37:18 -080020#include "absl/base/internal/atomic_hook_test_helper.h"
Austin Schuh36244a12019-09-21 17:52:38 -070021
22namespace {
23
Austin Schuhb4691e92020-12-31 12:37:18 -080024using ::testing::Eq;
25
Austin Schuh36244a12019-09-21 17:52:38 -070026int value = 0;
27void TestHook(int x) { value = x; }
28
29TEST(AtomicHookTest, NoDefaultFunction) {
Austin Schuhb4691e92020-12-31 12:37:18 -080030 ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES static absl::base_internal::AtomicHook<
31 void (*)(int)>
32 hook;
Austin Schuh36244a12019-09-21 17:52:38 -070033 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
56TEST(AtomicHookTest, WithDefaultFunction) {
57 // Set the default value to TestHook at compile-time.
Austin Schuhb4691e92020-12-31 12:37:18 -080058 ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES static absl::base_internal::AtomicHook<
59 void (*)(int)>
60 hook(TestHook);
Austin Schuh36244a12019-09-21 17:52:38 -070061 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 Schuhb4691e92020-12-31 12:37:18 -080077ABSL_CONST_INIT int override_func_calls = 0;
78void OverrideFunc() { override_func_calls++; }
79static struct OverrideInstaller {
80 OverrideInstaller() { absl::atomic_hook_internal::func.Store(OverrideFunc); }
81} override_installer;
82
83TEST(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 Schuh36244a12019-09-21 17:52:38 -070097} // namespace