atomic_hook_test.cc
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
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   // Test the default DummyFunction.
00030   EXPECT_TRUE(hook.Load() == nullptr);
00031   EXPECT_EQ(value, 0);
00032   hook(1);
00033   EXPECT_EQ(value, 0);
00034 
00035   // Test a stored hook.
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   // Calling Store() with the same hook should not crash.
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   // Set the default value to TestHook at compile-time.
00052   ABSL_CONST_INIT static absl::base_internal::AtomicHook<void (*)(int)> hook(
00053       TestHook);
00054   value = 0;
00055 
00056   // Test the default value is TestHook.
00057   EXPECT_TRUE(hook.Load() == TestHook);
00058   EXPECT_EQ(value, 0);
00059   hook(1);
00060   EXPECT_EQ(value, 1);
00061 
00062   // Calling Store() with the same hook should not crash.
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 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14