tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestScopeExit.cpp (1308B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "mozilla/Assertions.h"
      8 #include "mozilla/ScopeExit.h"
      9 
     10 using mozilla::MakeScopeExit;
     11 
     12 #define CHECK(c)                                       \
     13  do {                                                 \
     14    bool cond = !!(c);                                 \
     15    MOZ_RELEASE_ASSERT(cond, "Failed assertion: " #c); \
     16    if (!cond) {                                       \
     17      return false;                                    \
     18    }                                                  \
     19  } while (false)
     20 
     21 static bool Test() {
     22  int a = 1;
     23  int b = 1;
     24  int c = 1;
     25 
     26  {
     27    a++;
     28    auto guardA = MakeScopeExit([&] { a--; });
     29 
     30    b++;
     31    auto guardB = MakeScopeExit([&] { b--; });
     32 
     33    guardB.release();
     34 
     35    c++;
     36    auto guardC = MakeScopeExit([&] { c--; });
     37 
     38    {
     39      auto guardC_ = std::move(guardC);
     40    }
     41 
     42    CHECK(c == 1);
     43  }
     44 
     45  CHECK(a == 1);
     46  CHECK(b == 2);
     47  CHECK(c == 1);
     48 
     49  return true;
     50 }
     51 
     52 int main() {
     53  if (!Test()) {
     54    return 1;
     55  }
     56  return 0;
     57 }