tizen 2.3 release
[framework/web/wearable/wrt-commons.git] / tests / unused / test_caller.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_address.cpp
18  * @author      Tomasz Swierczek (t.swierczek@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of caller tests
21  */
22
23 #include <dpl/test_runner.h>
24 #include <dpl/serialization.h>
25 #include <dpl/caller.h>
26
27 RUNNER_TEST_GROUP_INIT(DPL)
28
29 // test stream class
30 class BinaryStream : public DPL::IStream
31 {
32   public:
33     virtual void Read(size_t num, void * bytes)
34     {
35         for (unsigned i = 0; i < num; ++i) {
36             ((unsigned char*)bytes)[i] = data[i + readPosition];
37         }
38         readPosition += num;
39     }
40     virtual void Write(size_t num, const void * bytes)
41     {
42         for (unsigned i = 0; i < num; ++i) {
43             data.push_back(((unsigned char*)bytes)[i]);
44         }
45     }
46     BinaryStream()
47     {
48         readPosition = 0;
49     }
50     virtual ~BinaryStream(){}
51
52   private:
53     std::vector<unsigned char> data;
54     unsigned readPosition;
55 };
56
57 static int return_func(int a, bool b)
58 {
59     if (b) {
60         return a;
61     } else {
62         return 0;
63     }
64 }
65
66 static int called = 0;
67
68 static void void_func(int a)
69 {
70     called = a;
71 }
72
73 static struct VoidDelegate
74 {
75     void operator()(int a)
76     {
77         called = a;
78     }
79 } voidDelegate;
80
81 static struct ReturnDelegate
82 {
83     int operator()(int a)
84     {
85         return a;
86     }
87 } returnDelegate;
88
89 RUNNER_TEST(Caller_function_void)
90 {
91     int a = 23;
92     BinaryStream stream;
93     DPL::Serialization::Serialize(stream, a);
94     called = 0;
95     DPL::Caller::Call(stream, void_func);
96     RUNNER_ASSERT(called == a);
97 }
98
99 RUNNER_TEST(Caller_function_return)
100 {
101     int a = 23;
102     bool b = true;
103     BinaryStream stream;
104     DPL::Serialization::Serialize(stream, a);
105     DPL::Serialization::Serialize(stream, b);
106     int result = DPL::Caller::Call(stream, return_func);
107     RUNNER_ASSERT(result == a);
108 }
109
110 RUNNER_TEST(Caller_delegate_void)
111 {
112     int a = 23;
113     BinaryStream stream;
114     called = 0;
115     DPL::Serialization::Serialize(stream, a);
116     DPL::Caller::CallDelegate(stream, voidDelegate);
117     RUNNER_ASSERT(called == a);
118 }
119
120 RUNNER_TEST(Caller_delegate_return)
121 {
122     int a = 23;
123     BinaryStream stream;
124     called = 0;
125     DPL::Serialization::Serialize(stream, a);
126     int result = 0;
127     DPL::Caller::CallDelegate(stream, returnDelegate, result);
128     RUNNER_ASSERT(result == a);
129 }