1 //===-- SubsystemRAIITest.cpp ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "gtest/gtest-spi.h"
10 #include "gtest/gtest.h"
11
12 #include "TestingSupport/SubsystemRAII.h"
13
14 using namespace lldb_private;
15
16 namespace {
17
18 enum class SystemState {
19 /// Start state of the subsystem.
20 Start,
21 /// Initialize has been called but Terminate hasn't been called yet.
22 Initialized,
23 /// Terminate has been called.
24 Terminated
25 };
26
27 struct TestSubsystem {
28 static SystemState state;
Initialize__anonb3ca4bdd0111::TestSubsystem29 static void Initialize() {
30 assert(state == SystemState::Start);
31 state = SystemState::Initialized;
32 }
Terminate__anonb3ca4bdd0111::TestSubsystem33 static void Terminate() {
34 assert(state == SystemState::Initialized);
35 state = SystemState::Terminated;
36 }
37 };
38 } // namespace
39
40 SystemState TestSubsystem::state = SystemState::Start;
41
TEST(SubsystemRAIITest,NormalSubsystem)42 TEST(SubsystemRAIITest, NormalSubsystem) {
43 // Tests that SubsystemRAII handles Initialize functions that return void.
44 EXPECT_EQ(SystemState::Start, TestSubsystem::state);
45 {
46 SubsystemRAII<TestSubsystem> subsystem;
47 EXPECT_EQ(SystemState::Initialized, TestSubsystem::state);
48 }
49 EXPECT_EQ(SystemState::Terminated, TestSubsystem::state);
50 }
51
52 static const char *SubsystemErrorString = "Initialize failed";
53
54 namespace {
55 struct TestSubsystemWithError {
56 static SystemState state;
57 static bool will_fail;
Initialize__anonb3ca4bdd0211::TestSubsystemWithError58 static llvm::Error Initialize() {
59 assert(state == SystemState::Start);
60 state = SystemState::Initialized;
61 if (will_fail)
62 return llvm::make_error<llvm::StringError>(
63 SubsystemErrorString, llvm::inconvertibleErrorCode());
64 return llvm::Error::success();
65 }
Terminate__anonb3ca4bdd0211::TestSubsystemWithError66 static void Terminate() {
67 assert(state == SystemState::Initialized);
68 state = SystemState::Terminated;
69 }
70 /// Reset the subsystem to the default state for testing.
Reset__anonb3ca4bdd0211::TestSubsystemWithError71 static void Reset() { state = SystemState::Start; }
72 };
73 } // namespace
74
75 SystemState TestSubsystemWithError::state = SystemState::Start;
76 bool TestSubsystemWithError::will_fail = false;
77
TEST(SubsystemRAIITest,SubsystemWithErrorSuccess)78 TEST(SubsystemRAIITest, SubsystemWithErrorSuccess) {
79 // Tests that SubsystemRAII handles llvm::success() returned from
80 // Initialize.
81 TestSubsystemWithError::Reset();
82 EXPECT_EQ(SystemState::Start, TestSubsystemWithError::state);
83 {
84 TestSubsystemWithError::will_fail = false;
85 SubsystemRAII<TestSubsystemWithError> subsystem;
86 EXPECT_EQ(SystemState::Initialized, TestSubsystemWithError::state);
87 }
88 EXPECT_EQ(SystemState::Terminated, TestSubsystemWithError::state);
89 }
90
TEST(SubsystemRAIITest,SubsystemWithErrorFailure)91 TEST(SubsystemRAIITest, SubsystemWithErrorFailure) {
92 // Tests that SubsystemRAII handles any errors returned from
93 // Initialize.
94 TestSubsystemWithError::Reset();
95 EXPECT_EQ(SystemState::Start, TestSubsystemWithError::state);
96 TestSubsystemWithError::will_fail = true;
97 EXPECT_FATAL_FAILURE(SubsystemRAII<TestSubsystemWithError> subsystem,
98 SubsystemErrorString);
99 }
100