2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file test_address.cpp
18 * @author Tomasz Swierczek (t.swierczek@samsung.com)
20 * @brief This file is the implementation file of serialization tests
28 #include <dpl/test/test_runner.h>
29 #include <dpl/serialization.h>
31 RUNNER_TEST_GROUP_INIT(DPL)
34 class BinaryStream : public DPL::IStream
37 virtual void Read(size_t num, void * bytes)
39 for (unsigned i = 0; i < num; ++i) {
40 ((unsigned char*)bytes)[i] = data[i + readPosition];
44 virtual void Write(size_t num, const void * bytes)
46 for (unsigned i = 0; i < num; ++i) {
47 data.push_back(((unsigned char*)bytes)[i]);
54 virtual ~BinaryStream(){}
57 std::vector<unsigned char> data;
58 unsigned readPosition;
61 //test ISerializable class
62 class TestClass : public DPL::ISerializable
65 TestClass(int val, std::string str1, std::string str2)
71 c.push_back(str1 + str2);
73 TestClass(DPL::IStream& stream) :
74 a(0) //TODO: consider the need (g.rynkowski)
76 DPL::Deserialization::Deserialize(stream, a);
77 DPL::Deserialization::Deserialize(stream, b);
78 DPL::Deserialization::Deserialize(stream, c);
80 virtual void Serialize(DPL::IStream& stream) const
82 DPL::Serialization::Serialize(stream, a);
83 DPL::Serialization::Serialize(stream, b);
84 DPL::Serialization::Serialize(stream, c);
86 virtual ~TestClass(){}
87 virtual bool operator==(const TestClass& other)
89 return (a == other.a &&
91 c.size() == other.c.size() &&
100 std::vector<std::string> c;
104 Name: Serialize_primitives
105 Description: Tests serialization of primitives types
106 Expected: serialized value after deserialization
107 should be equal to deserialied value
109 RUNNER_TEST(Serialize_primitives)
115 DPL::Serialization::Serialize(stream, a);
116 DPL::Serialization::Serialize(stream, b);
117 DPL::Serialization::Serialize(stream, c);
119 DPL::Deserialization::Deserialize(stream, test_int);
120 RUNNER_ASSERT(test_int == a);
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);
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
135 RUNNER_TEST(Serialize_primitive_pointers)
141 DPL::Serialization::Serialize(stream, &a);
142 DPL::Serialization::Serialize(stream, &b);
143 DPL::Serialization::Serialize(stream, &c);
145 DPL::Deserialization::Deserialize(stream, test_int);
146 RUNNER_ASSERT(test_int != NULL && *test_int == a);
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);
155 delete test_unsigned;
159 Name: Serialize_strings
160 Description: Tests serialization of strings
161 Expected: serialized value after deserialization
162 should be equal to deserialied value
164 RUNNER_TEST(Serialize_strings)
166 std::string str1 = "ALA MA KOTA";
167 std::string str2 = "MULTILINE\nTEST";
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);
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
185 RUNNER_TEST(Serialize_string_pointers)
187 std::string str1 = "ALA MA KOTA";
188 std::string str2 = "MULTILINE\nTEST";
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);
203 Name: Serialize_containers
204 Description: Tests serialization of containers
205 Expected: serialized value after deserialization
206 should be equal to deserialied value
208 RUNNER_TEST(Serialize_containers)
210 std::vector<int> vec;
213 std::list<bool> list;
214 list.push_back(true);
215 list.push_back(false);
216 std::pair<int, unsigned> pair;
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"));
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));
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
253 RUNNER_TEST(Serialize_objects)
255 TestClass a(123, "ASDGHUADB\n\n5679b^^()*", "TEST_STRING"),
256 b(679, "HUSPIDNSAHDPA", "\nASDSADASD\naDSADASD8");
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);
269 Description: Tests serialization of compound objects
270 Expected: serialized value after deserialization
271 should be equal to deserialied value
273 RUNNER_TEST(Serialize_all)
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));
283 DPL::Serialization::Serialize(stream, map);
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());
293 for (i = 0; i < test_vec1.size(); ++i) {
294 RUNNER_ASSERT((*test_vec1[i]) == (*test_vec2[i]));
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]));