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