tizen 2.3.1 release
[framework/web/wearable/wrt-commons.git] / tests / core / test_serialization.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 serialization tests
21  */
22
23 #include <vector>
24 #include <string>
25 #include <list>
26 #include <map>
27
28 #include <dpl/test/test_runner.h>
29 #include <dpl/serialization.h>
30
31 RUNNER_TEST_GROUP_INIT(DPL)
32
33 // test stream class
34 class BinaryStream : public DPL::IStream
35 {
36   public:
37     virtual void Read(size_t num, void * bytes)
38     {
39         for (unsigned i = 0; i < num; ++i) {
40             ((unsigned char*)bytes)[i] = data[i + readPosition];
41         }
42         readPosition += num;
43     }
44     virtual void Write(size_t num, const void * bytes)
45     {
46         for (unsigned i = 0; i < num; ++i) {
47             data.push_back(((unsigned char*)bytes)[i]);
48         }
49     }
50     BinaryStream()
51     {
52         readPosition = 0;
53     }
54     virtual ~BinaryStream(){}
55
56   private:
57     std::vector<unsigned char> data;
58     unsigned readPosition;
59 };
60
61 //test ISerializable class
62 class TestClass : public DPL::ISerializable
63 {
64   public:
65     TestClass(int val, std::string str1, std::string str2)
66     {
67         a = val;
68         b = str1;
69         c.push_back(str1);
70         c.push_back(str2);
71         c.push_back(str1 + str2);
72     }
73     TestClass(DPL::IStream& stream) :
74         a(0)    //TODO: consider the need (g.rynkowski)
75     {
76         DPL::Deserialization::Deserialize(stream, a);
77         DPL::Deserialization::Deserialize(stream, b);
78         DPL::Deserialization::Deserialize(stream, c);
79     }
80     virtual void Serialize(DPL::IStream& stream) const
81     {
82         DPL::Serialization::Serialize(stream, a);
83         DPL::Serialization::Serialize(stream, b);
84         DPL::Serialization::Serialize(stream, c);
85     }
86     virtual ~TestClass(){}
87     virtual bool operator==(const TestClass& other)
88     {
89         return (a == other.a &&
90                 b == other.b &&
91                 c.size() == other.c.size() &&
92                 c[0] == other.c[0] &&
93                 c[1] == other.c[1] &&
94                 c[2] == other.c[2]);
95     }
96
97   private:
98     int a;
99     std::string b;
100     std::vector<std::string> c;
101 };
102
103 /*
104 Name: Serialize_primitives
105 Description: Tests serialization of primitives types
106 Expected: serialized value after deserialization
107  should be equal to deserialied value
108 */
109 RUNNER_TEST(Serialize_primitives)
110 {
111     int a = 1;
112     bool b = true;
113     unsigned c = 23;
114     BinaryStream stream;
115     DPL::Serialization::Serialize(stream, a);
116     DPL::Serialization::Serialize(stream, b);
117     DPL::Serialization::Serialize(stream, c);
118     int test_int;
119     DPL::Deserialization::Deserialize(stream, test_int);
120     RUNNER_ASSERT(test_int == a);
121     bool test_bool;
122     DPL::Deserialization::Deserialize(stream, test_bool);
123     RUNNER_ASSERT(test_bool == b);
124     unsigned test_unsigned;
125     DPL::Deserialization::Deserialize(stream, test_unsigned);
126     RUNNER_ASSERT(test_unsigned == c);
127 }
128
129 /*
130 Name: Serialize_primitive_pointers
131 Description: Tests serialization of primitives pointer types
132 Expected: serialized value after deserialization
133  should be equal to deserialied value
134 */
135 RUNNER_TEST(Serialize_primitive_pointers)
136 {
137     int a = 1;
138     bool b = true;
139     unsigned c = 23;
140     BinaryStream stream;
141     DPL::Serialization::Serialize(stream, &a);
142     DPL::Serialization::Serialize(stream, &b);
143     DPL::Serialization::Serialize(stream, &c);
144     int* test_int;
145     DPL::Deserialization::Deserialize(stream, test_int);
146     RUNNER_ASSERT(test_int != NULL && *test_int == a);
147     bool* test_bool;
148     DPL::Deserialization::Deserialize(stream, test_bool);
149     RUNNER_ASSERT(test_bool != NULL && *test_bool == b);
150     unsigned* test_unsigned;
151     DPL::Deserialization::Deserialize(stream, test_unsigned);
152     RUNNER_ASSERT(test_unsigned != NULL && *test_unsigned == c);
153     delete test_int;
154     delete test_bool;
155     delete test_unsigned;
156 }
157
158 /*
159 Name: Serialize_strings
160 Description: Tests serialization of strings
161 Expected: serialized value after deserialization
162  should be equal to deserialied value
163 */
164 RUNNER_TEST(Serialize_strings)
165 {
166     std::string str1 = "ALA MA KOTA";
167     std::string str2 = "MULTILINE\nTEST";
168     BinaryStream stream;
169     DPL::Serialization::Serialize(stream, str1);
170     DPL::Serialization::Serialize(stream, str2);
171     std::string test_str1;
172     DPL::Deserialization::Deserialize(stream, test_str1);
173     RUNNER_ASSERT(test_str1 == str1);
174     std::string test_str2;
175     DPL::Deserialization::Deserialize(stream, test_str2);
176     RUNNER_ASSERT(test_str2 == str2);
177 }
178
179 /*
180 Name: Serialize_string_pointers
181 Description: Tests serialization of string pointers
182 Expected: serialized value after deserialization
183  should be equal to deserialied value
184 */
185 RUNNER_TEST(Serialize_string_pointers)
186 {
187     std::string str1 = "ALA MA KOTA";
188     std::string str2 = "MULTILINE\nTEST";
189     BinaryStream stream;
190     DPL::Serialization::Serialize(stream, &str1);
191     DPL::Serialization::Serialize(stream, &str2);
192     std::string* test_str1;
193     DPL::Deserialization::Deserialize(stream, test_str1);
194     RUNNER_ASSERT(test_str1 != NULL && *test_str1 == str1);
195     std::string* test_str2;
196     DPL::Deserialization::Deserialize(stream, test_str2);
197     RUNNER_ASSERT(test_str2 != NULL && *test_str2 == str2);
198     delete test_str1;
199     delete test_str2;
200 }
201
202 /*
203 Name: Serialize_containers
204 Description: Tests serialization of containers
205 Expected: serialized value after deserialization
206  should be equal to deserialied value
207 */
208 RUNNER_TEST(Serialize_containers)
209 {
210     std::vector<int> vec;
211     vec.push_back(134);
212     vec.push_back(265);
213     std::list<bool> list;
214     list.push_back(true);
215     list.push_back(false);
216     std::pair<int, unsigned> pair;
217     pair.first = -23;
218     pair.second = 1234;
219     std::map<int, std::string> map;
220     map.insert(std::pair<int, std::string>(45, "ALA MA CZARNEGO KOTA"));
221     map.insert(std::pair<int, std::string>(-78, "...A MOZE\nMA\nWIELE LINIJEK"));
222     BinaryStream stream;
223     DPL::Serialization::Serialize(stream, vec);
224     DPL::Serialization::Serialize(stream, list);
225     DPL::Serialization::Serialize(stream, pair);
226     DPL::Serialization::Serialize(stream, map);
227     std::vector<int> test_vec;
228     DPL::Deserialization::Deserialize(stream, test_vec);
229     RUNNER_ASSERT(test_vec.size() == vec.size() &&
230                   test_vec[0] == vec[0] && test_vec[1] == vec[1]);
231     std::list<bool> test_list;
232     DPL::Deserialization::Deserialize(stream, test_list);
233     RUNNER_ASSERT(test_list.size() == list.size() &&
234                   test_list.front() == list.front() &&
235                   test_list.back() == test_list.back());
236     std::pair<int, unsigned> test_pair;
237     DPL::Deserialization::Deserialize(stream, test_pair);
238     RUNNER_ASSERT(test_pair.first == pair.first &&
239                   test_pair.second == pair.second);
240     std::map<int, std::string> test_map;
241     DPL::Deserialization::Deserialize(stream, test_map);
242     RUNNER_ASSERT(test_map.size() == map.size() &&
243                   test_map.at(45) == map.at(45) &&
244                   test_map.at(-78) == map.at(-78));
245 }
246
247 /*
248 Name: Serialize_objects
249 Description: Tests serialization of DPL::ISerializable derived objects
250 Expected: serialized value after deserialization
251  should be equal to deserialied value
252 */
253 RUNNER_TEST(Serialize_objects)
254 {
255     TestClass a(123, "ASDGHUADB\n\n5679b^^()*", "TEST_STRING"),
256     b(679, "HUSPIDNSAHDPA", "\nASDSADASD\naDSADASD8");
257     BinaryStream stream;
258     DPL::Serialization::Serialize(stream, a);
259     DPL::Serialization::Serialize(stream, b);
260     TestClass test_a(0, "", ""), test_b(0, "", "");
261     DPL::Deserialization::Deserialize(stream, test_a);
262     RUNNER_ASSERT(test_a == a);
263     DPL::Deserialization::Deserialize(stream, test_b);
264     RUNNER_ASSERT(test_b == b);
265 }
266
267 /*
268 Name: Serialize_all
269 Description: Tests serialization of compound objects
270 Expected: serialized value after deserialization
271  should be equal to deserialied value
272 */
273 RUNNER_TEST(Serialize_all)
274 {
275     std::map<std::string, std::vector<TestClass*> > map;
276     std::vector<TestClass*> vec;
277     vec.push_back(new TestClass(123, "ASDGHUADB\n\n5679b^^()*", "TEST_STRING"));
278     vec.push_back(new TestClass(679, "HUSPIDNSAHDPA", "\nASDSADASD\naDSADASD8"));
279     map.insert(std::pair<std::string, std::vector<TestClass*> >("KEY1", vec));
280     map.insert(std::pair<std::string, std::vector<TestClass*> >("KEY2", vec));
281     BinaryStream stream;
282
283     DPL::Serialization::Serialize(stream, map);
284
285     std::map<std::string, std::vector<TestClass*> > test_map;
286     DPL::Deserialization::Deserialize(stream, test_map);
287     RUNNER_ASSERT(map.size() == test_map.size());
288     std::vector<TestClass*> test_vec1, test_vec2;
289     test_vec1 = map.at("KEY1");
290     test_vec2 = test_map.at("KEY1");
291     RUNNER_ASSERT(test_vec1.size() == test_vec2.size());
292     unsigned i;
293     for (i = 0; i < test_vec1.size(); ++i) {
294         RUNNER_ASSERT((*test_vec1[i]) == (*test_vec2[i]));
295     }
296     test_vec1 = map.at("KEY2");
297     test_vec2 = test_map.at("KEY2");
298     RUNNER_ASSERT(test_vec1.size() == test_vec2.size());
299     for (i = 0; i < test_vec1.size(); ++i) {
300         RUNNER_ASSERT((*test_vec1[i]) == (*test_vec2[i]));
301     }
302 }
303