xref: /aosp_15_r20/external/libchrome/base/no_destructor_unittest.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2018 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/no_destructor.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include <string>
8*635a8641SAndroid Build Coastguard Worker #include <utility>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
11*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
12*635a8641SAndroid Build Coastguard Worker #include "testing/gtest/include/gtest/gtest.h"
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker namespace base {
15*635a8641SAndroid Build Coastguard Worker 
16*635a8641SAndroid Build Coastguard Worker namespace {
17*635a8641SAndroid Build Coastguard Worker 
18*635a8641SAndroid Build Coastguard Worker struct CheckOnDestroy {
~CheckOnDestroybase::__anon6b6f6cb30111::CheckOnDestroy19*635a8641SAndroid Build Coastguard Worker   ~CheckOnDestroy() { CHECK(false); }
20*635a8641SAndroid Build Coastguard Worker };
21*635a8641SAndroid Build Coastguard Worker 
TEST(NoDestructorTest,SkipsDestructors)22*635a8641SAndroid Build Coastguard Worker TEST(NoDestructorTest, SkipsDestructors) {
23*635a8641SAndroid Build Coastguard Worker   NoDestructor<CheckOnDestroy> destructor_should_not_run;
24*635a8641SAndroid Build Coastguard Worker }
25*635a8641SAndroid Build Coastguard Worker 
26*635a8641SAndroid Build Coastguard Worker struct CopyOnly {
27*635a8641SAndroid Build Coastguard Worker   CopyOnly() = default;
28*635a8641SAndroid Build Coastguard Worker 
29*635a8641SAndroid Build Coastguard Worker   CopyOnly(const CopyOnly&) = default;
30*635a8641SAndroid Build Coastguard Worker   CopyOnly& operator=(const CopyOnly&) = default;
31*635a8641SAndroid Build Coastguard Worker 
32*635a8641SAndroid Build Coastguard Worker   CopyOnly(CopyOnly&&) = delete;
33*635a8641SAndroid Build Coastguard Worker   CopyOnly& operator=(CopyOnly&&) = delete;
34*635a8641SAndroid Build Coastguard Worker };
35*635a8641SAndroid Build Coastguard Worker 
36*635a8641SAndroid Build Coastguard Worker struct MoveOnly {
37*635a8641SAndroid Build Coastguard Worker   MoveOnly() = default;
38*635a8641SAndroid Build Coastguard Worker 
39*635a8641SAndroid Build Coastguard Worker   MoveOnly(const MoveOnly&) = delete;
40*635a8641SAndroid Build Coastguard Worker   MoveOnly& operator=(const MoveOnly&) = delete;
41*635a8641SAndroid Build Coastguard Worker 
42*635a8641SAndroid Build Coastguard Worker   MoveOnly(MoveOnly&&) = default;
43*635a8641SAndroid Build Coastguard Worker   MoveOnly& operator=(MoveOnly&&) = default;
44*635a8641SAndroid Build Coastguard Worker };
45*635a8641SAndroid Build Coastguard Worker 
46*635a8641SAndroid Build Coastguard Worker struct ForwardingTestStruct {
ForwardingTestStructbase::__anon6b6f6cb30111::ForwardingTestStruct47*635a8641SAndroid Build Coastguard Worker   ForwardingTestStruct(const CopyOnly&, MoveOnly&&) {}
48*635a8641SAndroid Build Coastguard Worker };
49*635a8641SAndroid Build Coastguard Worker 
TEST(NoDestructorTest,ForwardsArguments)50*635a8641SAndroid Build Coastguard Worker TEST(NoDestructorTest, ForwardsArguments) {
51*635a8641SAndroid Build Coastguard Worker   CopyOnly copy_only;
52*635a8641SAndroid Build Coastguard Worker   MoveOnly move_only;
53*635a8641SAndroid Build Coastguard Worker 
54*635a8641SAndroid Build Coastguard Worker   static NoDestructor<ForwardingTestStruct> test_forwarding(
55*635a8641SAndroid Build Coastguard Worker       copy_only, std::move(move_only));
56*635a8641SAndroid Build Coastguard Worker }
57*635a8641SAndroid Build Coastguard Worker 
TEST(NoDestructorTest,Accessors)58*635a8641SAndroid Build Coastguard Worker TEST(NoDestructorTest, Accessors) {
59*635a8641SAndroid Build Coastguard Worker   static NoDestructor<std::string> awesome("awesome");
60*635a8641SAndroid Build Coastguard Worker 
61*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ("awesome", *awesome);
62*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(0, awesome->compare("awesome"));
63*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(0, awesome.get()->compare("awesome"));
64*635a8641SAndroid Build Coastguard Worker }
65*635a8641SAndroid Build Coastguard Worker 
66*635a8641SAndroid Build Coastguard Worker // Passing initializer list to a NoDestructor like in this test
67*635a8641SAndroid Build Coastguard Worker // is ambiguous in GCC.
68*635a8641SAndroid Build Coastguard Worker // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84849
69*635a8641SAndroid Build Coastguard Worker #if !defined(COMPILER_GCC) && !defined(__clang__)
TEST(NoDestructorTest,InitializerList)70*635a8641SAndroid Build Coastguard Worker TEST(NoDestructorTest, InitializerList) {
71*635a8641SAndroid Build Coastguard Worker   static NoDestructor<std::vector<std::string>> vector({"a", "b", "c"});
72*635a8641SAndroid Build Coastguard Worker }
73*635a8641SAndroid Build Coastguard Worker #endif
74*635a8641SAndroid Build Coastguard Worker }  // namespace
75*635a8641SAndroid Build Coastguard Worker 
76*635a8641SAndroid Build Coastguard Worker }  // namespace base
77