abseil-cpp/absl/base/internal/atomic_hook_test.cc
Go to the documentation of this file.
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 
25 
26 int value = 0;
27 void TestHook(int x) { value = x; }
28 
29 TEST(AtomicHookTest, NoDefaultFunction) {
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 
56 TEST(AtomicHookTest, WithDefaultFunction) {
57  // Set the default value to TestHook at compile-time.
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;
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
90  EXPECT_THAT(override_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
ABSL_CONST_INIT
#define ABSL_CONST_INIT
Definition: abseil-cpp/absl/base/attributes.h:716
EXPECT_THAT
#define EXPECT_THAT(value, matcher)
ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
#define ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
Definition: abseil-cpp/absl/base/internal/atomic_hook.h:49
absl::base_internal::AtomicHook
Definition: abseil-cpp/absl/base/internal/atomic_hook.h:43
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
xds_interop_client.int
int
Definition: xds_interop_client.py:113
TEST
#define TEST(name, init_size,...)
Definition: arena_test.cc:75
testing::Eq
internal::EqMatcher< T > Eq(T x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8561
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
value
const char * value
Definition: hpack_parser_table.cc:165
absl::atomic_hook_internal::func
absl::base_internal::AtomicHook< VoidF > func
Definition: bloaty/third_party/abseil-cpp/absl/base/internal/atomic_hook_test_helper.h:25
absl::atomic_hook_internal::default_func_calls
ABSL_CONST_INIT int default_func_calls
Definition: abseil-cpp/absl/base/internal/atomic_hook_test_helper.cc:26
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:45