100 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "gtest/gtest-spi.h"10#include "gtest/gtest.h"11 12#include "TestingSupport/SubsystemRAII.h"13 14using namespace lldb_private;15 16namespace {17 18enum 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 Terminated25};26 27struct TestSubsystem {28 static SystemState state;29 static void Initialize() {30 assert(state == SystemState::Start);31 state = SystemState::Initialized;32 }33 static void Terminate() {34 assert(state == SystemState::Initialized);35 state = SystemState::Terminated;36 }37};38} // namespace39 40SystemState TestSubsystem::state = SystemState::Start;41 42TEST(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 52static const char *SubsystemErrorString = "Initialize failed";53 54namespace {55struct 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();65 }66 static void Terminate() {67 assert(state == SystemState::Initialized);68 state = SystemState::Terminated;69 }70 /// Reset the subsystem to the default state for testing.71 static void Reset() { state = SystemState::Start; }72};73} // namespace74 75SystemState TestSubsystemWithError::state = SystemState::Start;76bool TestSubsystemWithError::will_fail = false;77 78TEST(SubsystemRAIITest, SubsystemWithErrorSuccess) {79 // Tests that SubsystemRAII handles llvm::success() returned from80 // 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 91TEST(SubsystemRAIITest, SubsystemWithErrorFailure) {92 // Tests that SubsystemRAII handles any errors returned from93 // 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