Tim Shen | 64afe23 | 2016-08-10 17:52:09 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/ADT/ScopeExit.cpp - Scope exit unit tests --*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Tim Shen | 64afe23 | 2016-08-10 17:52:09 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/ADT/ScopeExit.h" |
| 10 | #include "gtest/gtest.h" |
| 11 | |
| 12 | using namespace llvm; |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | TEST(ScopeExitTest, Basic) { |
| 17 | struct Callable { |
| 18 | bool &Called; |
| 19 | Callable(bool &Called) : Called(Called) {} |
| 20 | Callable(Callable &&RHS) : Called(RHS.Called) {} |
| 21 | void operator()() { Called = true; } |
| 22 | }; |
| 23 | bool Called = false; |
| 24 | { |
| 25 | auto g = make_scope_exit(Callable(Called)); |
| 26 | EXPECT_FALSE(Called); |
| 27 | } |
| 28 | EXPECT_TRUE(Called); |
| 29 | } |
| 30 | |
Sam McCall | 7e6d025 | 2018-01-25 16:55:48 +0000 | [diff] [blame] | 31 | TEST(ScopeExitTest, Release) { |
| 32 | int Count = 0; |
| 33 | auto Increment = [&] { ++Count; }; |
| 34 | { |
| 35 | auto G = make_scope_exit(Increment); |
| 36 | auto H = std::move(G); |
| 37 | auto I = std::move(G); |
| 38 | EXPECT_EQ(0, Count); |
| 39 | } |
| 40 | EXPECT_EQ(1, Count); |
| 41 | { |
| 42 | auto G = make_scope_exit(Increment); |
| 43 | G.release(); |
| 44 | } |
| 45 | EXPECT_EQ(1, Count); |
| 46 | } |
| 47 | |
Tim Shen | 64afe23 | 2016-08-10 17:52:09 +0000 | [diff] [blame] | 48 | } // end anonymous namespace |