[SimplifyCFG] Replace unreachable switch lookup table holes with poison (#94990)
[llvm-project.git] / lldb / unittests / Utility / SubsystemRAIITest.cpp
blob1a23bfc716f797a0d16fcbcd7bad4e6865a8a188
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 //===----------------------------------------------------------------------===//
9 #include "gtest/gtest-spi.h"
10 #include "gtest/gtest.h"
12 #include "TestingSupport/SubsystemRAII.h"
14 using namespace lldb_private;
16 namespace {
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
27 struct TestSubsystem {
28 static SystemState state;
29 static void Initialize() {
30 assert(state == SystemState::Start);
31 state = SystemState::Initialized;
33 static void Terminate() {
34 assert(state == SystemState::Initialized);
35 state = SystemState::Terminated;
38 } // namespace
40 SystemState TestSubsystem::state = SystemState::Start;
42 TEST(SubsystemRAIITest, NormalSubsystem) {
43 // Tests that SubsystemRAII handles Initialize functions that return void.
44 EXPECT_EQ(SystemState::Start, TestSubsystem::state);
46 SubsystemRAII<TestSubsystem> subsystem;
47 EXPECT_EQ(SystemState::Initialized, TestSubsystem::state);
49 EXPECT_EQ(SystemState::Terminated, TestSubsystem::state);
52 static const char *SubsystemErrorString = "Initialize failed";
54 namespace {
55 struct TestSubsystemWithError {
56 static SystemState state;
57 static bool will_fail;
58 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();
66 static void Terminate() {
67 assert(state == SystemState::Initialized);
68 state = SystemState::Terminated;
70 /// Reset the subsystem to the default state for testing.
71 static void Reset() { state = SystemState::Start; }
73 } // namespace
75 SystemState TestSubsystemWithError::state = SystemState::Start;
76 bool TestSubsystemWithError::will_fail = false;
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);
84 TestSubsystemWithError::will_fail = false;
85 SubsystemRAII<TestSubsystemWithError> subsystem;
86 EXPECT_EQ(SystemState::Initialized, TestSubsystemWithError::state);
88 EXPECT_EQ(SystemState::Terminated, TestSubsystemWithError::state);
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);