xref: /aosp_15_r20/system/libbase/no_destructor_test.cpp (revision 8f0ba417480079999ba552f1087ae592091b9d02)
1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker  *
4*8f0ba417SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker  *
8*8f0ba417SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker  *
10*8f0ba417SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker  * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker  */
16*8f0ba417SAndroid Build Coastguard Worker 
17*8f0ba417SAndroid Build Coastguard Worker #include "android-base/no_destructor.h"
18*8f0ba417SAndroid Build Coastguard Worker 
19*8f0ba417SAndroid Build Coastguard Worker #include <gtest/gtest.h>
20*8f0ba417SAndroid Build Coastguard Worker 
21*8f0ba417SAndroid Build Coastguard Worker struct __attribute__((packed)) Bomb {
BombBomb22*8f0ba417SAndroid Build Coastguard Worker   Bomb() : magic_(123) {}
23*8f0ba417SAndroid Build Coastguard Worker 
~BombBomb24*8f0ba417SAndroid Build Coastguard Worker   ~Bomb() { exit(42); }
25*8f0ba417SAndroid Build Coastguard Worker 
getBomb26*8f0ba417SAndroid Build Coastguard Worker   int get() const { return magic_; }
27*8f0ba417SAndroid Build Coastguard Worker 
28*8f0ba417SAndroid Build Coastguard Worker  private:
29*8f0ba417SAndroid Build Coastguard Worker   [[maybe_unused]] char padding_;
30*8f0ba417SAndroid Build Coastguard Worker   int magic_;
31*8f0ba417SAndroid Build Coastguard Worker };
32*8f0ba417SAndroid Build Coastguard Worker 
TEST(no_destructor,bomb)33*8f0ba417SAndroid Build Coastguard Worker TEST(no_destructor, bomb) {
34*8f0ba417SAndroid Build Coastguard Worker   ASSERT_EXIT(({
35*8f0ba417SAndroid Build Coastguard Worker                 {
36*8f0ba417SAndroid Build Coastguard Worker                   Bomb b;
37*8f0ba417SAndroid Build Coastguard Worker                   if (b.get() != 123) exit(1);
38*8f0ba417SAndroid Build Coastguard Worker                 }
39*8f0ba417SAndroid Build Coastguard Worker 
40*8f0ba417SAndroid Build Coastguard Worker                 exit(0);
41*8f0ba417SAndroid Build Coastguard Worker               }),
42*8f0ba417SAndroid Build Coastguard Worker               ::testing::ExitedWithCode(42), "");
43*8f0ba417SAndroid Build Coastguard Worker }
44*8f0ba417SAndroid Build Coastguard Worker 
TEST(no_destructor,defused)45*8f0ba417SAndroid Build Coastguard Worker TEST(no_destructor, defused) {
46*8f0ba417SAndroid Build Coastguard Worker   ASSERT_EXIT(({
47*8f0ba417SAndroid Build Coastguard Worker                 {
48*8f0ba417SAndroid Build Coastguard Worker                   android::base::NoDestructor<Bomb> b;
49*8f0ba417SAndroid Build Coastguard Worker                   if (b->get() != 123) exit(1);
50*8f0ba417SAndroid Build Coastguard Worker                 }
51*8f0ba417SAndroid Build Coastguard Worker 
52*8f0ba417SAndroid Build Coastguard Worker                 exit(0);
53*8f0ba417SAndroid Build Coastguard Worker               }),
54*8f0ba417SAndroid Build Coastguard Worker               ::testing::ExitedWithCode(0), "");
55*8f0ba417SAndroid Build Coastguard Worker }
56*8f0ba417SAndroid Build Coastguard Worker 
TEST(no_destructor,operators)57*8f0ba417SAndroid Build Coastguard Worker TEST(no_destructor, operators) {
58*8f0ba417SAndroid Build Coastguard Worker   android::base::NoDestructor<Bomb> b;
59*8f0ba417SAndroid Build Coastguard Worker   const android::base::NoDestructor<Bomb>& c = b;
60*8f0ba417SAndroid Build Coastguard Worker   ASSERT_EQ(123, b.get()->get());
61*8f0ba417SAndroid Build Coastguard Worker   ASSERT_EQ(123, b->get());
62*8f0ba417SAndroid Build Coastguard Worker   ASSERT_EQ(123, (*b).get());
63*8f0ba417SAndroid Build Coastguard Worker   ASSERT_EQ(123, c.get()->get());
64*8f0ba417SAndroid Build Coastguard Worker   ASSERT_EQ(123, c->get());
65*8f0ba417SAndroid Build Coastguard Worker   ASSERT_EQ(123, (*c).get());
66*8f0ba417SAndroid Build Coastguard Worker }
67