1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 #include "aemu/base/memory/NoDestructor.h"
5 #include <string>
6 #include <utility>
7 #include <gtest/gtest.h>
8 #include "aemu/base/Log.h"
9 
10 namespace android::base {
11 namespace {
12 struct CheckOnDestroy {
~CheckOnDestroyandroid::base::__anon594061e50111::CheckOnDestroy13   ~CheckOnDestroy() { dfatal("Destructor was called"); }
14 };
TEST(NoDestructorTest,SkipsDestructors)15 TEST(NoDestructorTest, SkipsDestructors) {
16   NoDestructor<CheckOnDestroy> destructor_should_not_run;
17 }
18 struct CopyOnly {
19   CopyOnly() = default;
20   CopyOnly(const CopyOnly&) = default;
21   CopyOnly& operator=(const CopyOnly&) = default;
22   CopyOnly(CopyOnly&&) = delete;
23   CopyOnly& operator=(CopyOnly&&) = delete;
24 };
25 struct MoveOnly {
26   MoveOnly() = default;
27   MoveOnly(const MoveOnly&) = delete;
28   MoveOnly& operator=(const MoveOnly&) = delete;
29   MoveOnly(MoveOnly&&) = default;
30   MoveOnly& operator=(MoveOnly&&) = default;
31 };
32 struct ForwardingTestStruct {
ForwardingTestStructandroid::base::__anon594061e50111::ForwardingTestStruct33   ForwardingTestStruct(const CopyOnly&, MoveOnly&&) {}
34 };
TEST(NoDestructorTest,ForwardsArguments)35 TEST(NoDestructorTest, ForwardsArguments) {
36   CopyOnly copy_only;
37   MoveOnly move_only;
38   static NoDestructor<ForwardingTestStruct> test_forwarding(
39       copy_only, std::move(move_only));
40 }
TEST(NoDestructorTest,Accessors)41 TEST(NoDestructorTest, Accessors) {
42   static NoDestructor<std::string> awesome("awesome");
43   EXPECT_EQ("awesome", *awesome);
44   EXPECT_EQ(0, awesome->compare("awesome"));
45   EXPECT_EQ(0, awesome.get()->compare("awesome"));
46 }
47 }  // namespace
48 }  // namespace base