Tizen 2.0 Release
[framework/web/wrt-commons.git] / tests / dpl / 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   public:
32     virtual void Read(size_t num, void * bytes)
33     {
34         for (unsigned i = 0; i < num; ++i) {
35             ((unsigned char*)bytes)[i] = data[i + readPosition];
36         }
37         readPosition += num;
38     }
39     virtual void Write(size_t num, const void * bytes)
40     {
41         for (unsigned i = 0; i < num; ++i) {
42             data.push_back(((unsigned char*)bytes)[i]);
43         }
44     }
45     BinaryStream()
46     {
47         readPosition = 0;
48     }
49     virtual ~BinaryStream(){};
50
51   private:
52     std::vector<unsigned char> data;
53     unsigned readPosition;
54 };
55
56 static int return_func(int a, bool b)
57 {
58     if (b) {
59         return a;
60     } else {
61         return 0;
62     }
63 }
64
65 static int called = 0;
66
67 static void void_func(int a)
68 {
69     called = a;
70 }
71
72 static struct VoidDelegate
73 {
74         void operator()(int a)
75         {
76             called = a;
77         }
78 } voidDelegate;
79
80 static struct ReturnDelegate
81 {
82         int operator()(int a)
83         {
84             return a;
85         }
86 } returnDelegate;
87
88 RUNNER_TEST(Caller_function_void)
89 {
90     int a = 23;
91     BinaryStream stream;
92     DPL::Serialization::Serialize(stream,a);
93     called = 0;
94     DPL::Caller::Call(stream,void_func);
95     RUNNER_ASSERT(called == a);
96 }
97
98 RUNNER_TEST(Caller_function_return)
99 {
100     int a = 23;
101     bool b = true;
102     BinaryStream stream;
103     DPL::Serialization::Serialize(stream,a);
104     DPL::Serialization::Serialize(stream,b);
105     int result = DPL::Caller::Call(stream,return_func);
106     RUNNER_ASSERT(result == a);
107 }
108
109 RUNNER_TEST(Caller_delegate_void)
110 {
111     int a = 23;
112     BinaryStream stream;
113     called = 0;
114     DPL::Serialization::Serialize(stream,a);
115     DPL::Caller::CallDelegate(stream,voidDelegate);
116     RUNNER_ASSERT(called == a);
117 }
118
119 RUNNER_TEST(Caller_delegate_return)
120 {
121     int a = 23;
122     BinaryStream stream;
123     called = 0;
124     DPL::Serialization::Serialize(stream,a);
125     int result = 0;
126     DPL::Caller::CallDelegate(stream,returnDelegate,result);
127     RUNNER_ASSERT(result == a);
128 }