1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <comphelper/flagguard.hxx>
11 #include <unotest/bootstrapfixturebase.hxx>
13 CPPUNIT_TEST_FIXTURE(CppUnit::TestFixture
, testScopeGuard
)
15 // Test that comphelper::ScopeGuard executes its parameter on destruction
17 // initial value "true", out-of-scope ScopeGuard function executes and changes the value to "false"
20 comphelper::ScopeGuard
aGuard([&bFlag
] { bFlag
= false; });
21 CPPUNIT_ASSERT(bFlag
);
23 CPPUNIT_ASSERT(!bFlag
);
26 CPPUNIT_TEST_FIXTURE(CppUnit::TestFixture
, testFlagGuard
)
28 // Test that comphelper::FlagGuard properly sets and resets the flag
30 // initial value "false", change to "true", out-of-scope change to "false"
33 comphelper::FlagGuard
aGuard(bFlag
);
34 CPPUNIT_ASSERT(bFlag
);
36 // comphelper::FlagGuard must reset flag to false on destruction unconditionally
37 CPPUNIT_ASSERT(!bFlag
);
39 // initial value "true", retain the value at "true", out-of-scope change to "false"
42 comphelper::FlagGuard
aGuard(bFlag
);
43 CPPUNIT_ASSERT(bFlag
);
45 // comphelper::FlagGuard must reset flag to false on destruction unconditionally
46 CPPUNIT_ASSERT(!bFlag
);
49 CPPUNIT_TEST_FIXTURE(CppUnit::TestFixture
, testFlagRestorationGuard
)
51 // Test that comphelper::FlagRestorationGuard properly sets and resets the flag
53 // initial value "true", change to "false", out-of-scope change to "true"
57 comphelper::FlagRestorationGuard
aGuard(bFlag
, false);
58 CPPUNIT_ASSERT(!bFlag
);
60 // comphelper::FlagRestorationGuard must reset flag to initial state on destruction
61 CPPUNIT_ASSERT(bFlag
);
64 CPPUNIT_TEST_FIXTURE(CppUnit::TestFixture
, testValueRestorationGuard
)
66 // Test that comphelper::ValueRestorationGuard properly sets and resets the (int) value
70 // set value and restore after scope ends
72 CPPUNIT_ASSERT_EQUAL(199, value
);
73 comphelper::ValueRestorationGuard
aGuard(value
, 100);
74 CPPUNIT_ASSERT_EQUAL(100, value
);
76 CPPUNIT_ASSERT_EQUAL(199, value
);
78 // set value, manually setto another value and restore after scope ends
80 CPPUNIT_ASSERT_EQUAL(199, value
);
81 comphelper::ValueRestorationGuard
aGuard(value
, 100);
82 CPPUNIT_ASSERT_EQUAL(100, value
);
85 CPPUNIT_ASSERT_EQUAL(199, value
);
88 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */