Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/base/internal/atomic_hook.h"
00016
00017 #include "gtest/gtest.h"
00018 #include "absl/base/attributes.h"
00019
00020 namespace {
00021
00022 int value = 0;
00023 void TestHook(int x) { value = x; }
00024
00025 TEST(AtomicHookTest, NoDefaultFunction) {
00026 ABSL_CONST_INIT static absl::base_internal::AtomicHook<void(*)(int)> hook;
00027 value = 0;
00028
00029
00030 EXPECT_TRUE(hook.Load() == nullptr);
00031 EXPECT_EQ(value, 0);
00032 hook(1);
00033 EXPECT_EQ(value, 0);
00034
00035
00036 hook.Store(TestHook);
00037 EXPECT_TRUE(hook.Load() == TestHook);
00038 EXPECT_EQ(value, 0);
00039 hook(1);
00040 EXPECT_EQ(value, 1);
00041
00042
00043 hook.Store(TestHook);
00044 EXPECT_TRUE(hook.Load() == TestHook);
00045 EXPECT_EQ(value, 1);
00046 hook(2);
00047 EXPECT_EQ(value, 2);
00048 }
00049
00050 TEST(AtomicHookTest, WithDefaultFunction) {
00051
00052 ABSL_CONST_INIT static absl::base_internal::AtomicHook<void (*)(int)> hook(
00053 TestHook);
00054 value = 0;
00055
00056
00057 EXPECT_TRUE(hook.Load() == TestHook);
00058 EXPECT_EQ(value, 0);
00059 hook(1);
00060 EXPECT_EQ(value, 1);
00061
00062
00063 hook.Store(TestHook);
00064 EXPECT_TRUE(hook.Load() == TestHook);
00065 EXPECT_EQ(value, 1);
00066 hook(2);
00067 EXPECT_EQ(value, 2);
00068 }
00069
00070 }