Home | History | Annotate | Download | only in ADT
      1 //===- llvm/unittest/ADT/ScopeExit.cpp - Scope exit unit tests --*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "llvm/ADT/ScopeExit.h"
     11 #include "gtest/gtest.h"
     12 
     13 using namespace llvm;
     14 
     15 namespace {
     16 
     17 TEST(ScopeExitTest, Basic) {
     18   struct Callable {
     19     bool &Called;
     20     Callable(bool &Called) : Called(Called) {}
     21     Callable(Callable &&RHS) : Called(RHS.Called) {}
     22     void operator()() { Called = true; }
     23   };
     24   bool Called = false;
     25   {
     26     auto g = make_scope_exit(Callable(Called));
     27     EXPECT_FALSE(Called);
     28   }
     29   EXPECT_TRUE(Called);
     30 }
     31 
     32 TEST(ScopeExitTest, Release) {
     33   int Count = 0;
     34   auto Increment = [&] { ++Count; };
     35   {
     36     auto G = make_scope_exit(Increment);
     37     auto H = std::move(G);
     38     auto I = std::move(G);
     39     EXPECT_EQ(0, Count);
     40   }
     41   EXPECT_EQ(1, Count);
     42   {
     43     auto G = make_scope_exit(Increment);
     44     G.release();
     45   }
     46   EXPECT_EQ(1, Count);
     47 }
     48 
     49 } // end anonymous namespace
     50