tizen 2.3.1 release
[framework/web/wearable/wrt-commons.git] / tests / core / test_bind.cpp
1 /*
2  * Copyright (c) 2013 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_bind.cpp
18  * @author      Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of test bind
21  */
22 #include <functional>
23
24 #include <dpl/test/test_runner.h>
25 #include <dpl/bind.h>
26
27 namespace {
28 const int MAGIC_INT = 42;
29 const double MAGIC_DOUBLE = 42.42;
30
31 struct BindTestBase {
32     BindTestBase() : called{false} {}
33
34     bool called;
35 };
36
37 struct BindNoArguments : BindTestBase {
38     void foo() { called = true; }
39 };
40
41 struct BindWithArguments : BindTestBase {
42     void foo(int, double) { called = true; }
43 };
44
45 struct BindWithArgumentsAndResult : BindTestBase {
46     int foo(int, double) { called = true; return MAGIC_INT; }
47 };
48 }
49
50 RUNNER_TEST_GROUP_INIT(DPL)
51
52 /*
53 Name: Bind_NoArguments
54 Description: tests binding method without arguments
55 Expected: bound method called
56 */
57 RUNNER_TEST(Bind_NoArguments)
58 {
59     BindNoArguments test;
60
61     std::function<void()> delegate = DPL::Bind(&BindNoArguments::foo, &test);
62     delegate();
63
64     RUNNER_ASSERT(test.called);
65 }
66
67 /*
68 Name: Bind_WithArguments
69 Description: tests binding method with arguments
70 Expected: bound method called
71 */
72 RUNNER_TEST(Bind_WithArguments)
73 {
74     BindWithArguments test;
75
76     std::function<void(int, double)> delegate =
77         DPL::Bind(&BindWithArguments::foo, &test);
78     delegate(MAGIC_INT, MAGIC_DOUBLE);
79
80     RUNNER_ASSERT(test.called);
81 }
82
83 /*
84 Name: Bind_WithArgumentsAndResult
85 Description: tests binding method with arguments and result
86 Expected: bound method called
87 */
88 RUNNER_TEST(Bind_WithArgumentsAndResult)
89 {
90     BindWithArgumentsAndResult test;
91
92     std::function<int(int, double)> delegate =
93         DPL::Bind(&BindWithArgumentsAndResult::foo, &test);
94     int result = delegate(MAGIC_INT, MAGIC_DOUBLE);
95
96     RUNNER_ASSERT(test.called);
97     RUNNER_ASSERT(MAGIC_INT == result);
98 }