tizen 2.3.1 release
[framework/web/wearable/wrt-commons.git] / tests / core / test_scope_guard.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        test_scope_guard.cpp
18  * @author      Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of test scope guard
21  */
22 #include <dpl/test/test_runner.h>
23 #include <dpl/scope_guard.h>
24
25 namespace {
26 bool g_guardCalled = false;
27
28 void regularFunction()
29 {
30     g_guardCalled = true;
31 }
32
33 struct Functor
34 {
35     explicit Functor(bool& guardCalled)
36         : m_guardCalled(guardCalled)
37     {}
38
39     void operator()()
40     {
41         m_guardCalled = true;
42     }
43
44     bool& m_guardCalled;
45 };
46 }
47
48 RUNNER_TEST_GROUP_INIT(DPL)
49
50 /*
51 Name: ScopeGuard_MakeScopeGuard_RegularFunction
52 Description: tests scope guard, created explicitly, with regular function
53 Expected: guard should be called
54 */
55 RUNNER_TEST(ScopeGuard_MakeScopeGuard_RegularFunction)
56 {
57     g_guardCalled = false;
58     {
59         auto guard = DPL::MakeScopeGuard(regularFunction);
60     }
61     RUNNER_ASSERT(g_guardCalled);
62 }
63
64 /*
65 Name: ScopeGuard_MakeScopeGuard_Functor
66 Description: tests scope guard, created explicitly, with a functor
67 Expected: guard should be called
68 */
69 RUNNER_TEST(ScopeGuard_MakeScopeGuard_Functor)
70 {
71     g_guardCalled = false;
72     {
73         auto guard = DPL::MakeScopeGuard(Functor(g_guardCalled));
74     }
75     RUNNER_ASSERT(g_guardCalled);
76 }
77
78 /*
79 Name: ScopeGuard_MakeScopeGuard_Lambda
80 Description: tests scope guard, created explicitly, with a lambda
81 Expected: guard should be called
82 */
83 RUNNER_TEST(ScopeGuard_MakeScopeGuard_Lambda)
84 {
85     g_guardCalled = false;
86     {
87         auto guard = DPL::MakeScopeGuard(
88           [&g_guardCalled](){ g_guardCalled = true; });
89     }
90     RUNNER_ASSERT(g_guardCalled);
91 }
92
93 /*
94 Name: ScopeGuard_Macro
95 Description: tests scope guard, created implicitly, with a lambda
96 Expected: guard should be called
97 */
98 RUNNER_TEST(ScopeGuard_Macro)
99 {
100     g_guardCalled = false;
101     {
102         DPL_SCOPE_EXIT(&g_guardCalled)
103         {
104             g_guardCalled = true;
105         };
106     }
107     RUNNER_ASSERT(g_guardCalled);
108 }
109
110 /*
111 Name: ScopeGuard_Release
112 Description: tests scope guard releasing API
113 Expected: guard should not be called
114 */
115 RUNNER_TEST(ScopeGuard_Release)
116 {
117     g_guardCalled = false;
118     {
119         auto guard = DPL::MakeScopeGuard(
120                 [&g_guardCalled](){ g_guardCalled = true; });
121         guard.Release();
122     }
123     RUNNER_ASSERT(!g_guardCalled);
124 }