no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / mfbt / tests / TestScopeExit.cpp
blob1c5eef68c2caf02f7fe54a75c7706a0930d0579d
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/. */
7 #include "mozilla/Assertions.h"
8 #include "mozilla/ScopeExit.h"
10 using mozilla::MakeScopeExit;
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)
21 static bool Test() {
22 int a = 1;
23 int b = 1;
24 int c = 1;
27 a++;
28 auto guardA = MakeScopeExit([&] { a--; });
30 b++;
31 auto guardB = MakeScopeExit([&] { b--; });
33 guardB.release();
35 c++;
36 auto guardC = MakeScopeExit([&] { c--; });
38 { auto guardC_ = std::move(guardC); }
40 CHECK(c == 1);
43 CHECK(a == 1);
44 CHECK(b == 2);
45 CHECK(c == 1);
47 return true;
50 int main() {
51 if (!Test()) {
52 return 1;
54 return 0;