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_binary_queue.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of test binary queue
22 #include <dpl/test/test_runner.h>
23 #include <dpl/binary_queue.h>
24 RUNNER_TEST_GROUP_INIT(DPL)
26 inline std::string BinaryQueueToString(const DPL::BinaryQueue &queue)
28 char *buffer = new char[queue.Size()];
29 queue.Flatten(buffer, queue.Size());
30 std::string result = std::string(buffer, buffer + queue.Size());
36 Name: BinaryQueue_InitialEmpty
37 Description: tests emptiness of new constructed queue
38 Expected: new queue should be empty
40 RUNNER_TEST(BinaryQueue_InitialEmpty)
42 DPL::BinaryQueue queue;
43 RUNNER_ASSERT(queue.Empty() == true);
47 Name: BinaryQueue_InitialSize
48 Description: tests emptiness of new constructed queue
49 Expected: new queue size should be equal 0
51 RUNNER_TEST(BinaryQueue_InitialSize)
53 DPL::BinaryQueue queue;
54 RUNNER_ASSERT(queue.Size() == 0);
58 Name: BinaryQueue_InitialCopy
59 Description: tests emptiness of new copy-constructed empty queue
60 Expected: queue constructed on base on empty queue should be empty
62 RUNNER_TEST(BinaryQueue_InitialCopy)
64 DPL::BinaryQueue queue;
65 DPL::BinaryQueue copy = queue;
67 RUNNER_ASSERT(copy.Size() == 0);
71 Name: BinaryQueue_InitialConsumeZero
72 Description: tests consume method accepts 0 bytes
73 Expected: it should be avaliable to discard 0 bytes from empty queue
75 RUNNER_TEST(BinaryQueue_InitialConsumeZero)
77 DPL::BinaryQueue queue;
82 Name: BinaryQueue_InitialFlattenConsumeZero
83 Description: tests returning data from queue and discarding
84 Expected: it should be able to call flattenconsume with empty buffer if 0 bytes are read
86 RUNNER_TEST(BinaryQueue_InitialFlattenConsumeZero)
88 DPL::BinaryQueue queue;
89 queue.FlattenConsume(NULL, 0);
93 Name: BinaryQueue_InitialFlattenZero
94 Description: tests returning data from queue
95 Expected: it should be able to call flatten with empty buffer if 0 bytes are read
97 RUNNER_TEST(BinaryQueue_InitialFlattenZero)
99 DPL::BinaryQueue queue;
100 queue.Flatten(NULL, 0);
104 Name: BinaryQueue_InitialConsumeOne
105 Description: tests discarding more bytes than it is avaliable
106 Expected: exception throw
108 RUNNER_TEST(BinaryQueue_InitialConsumeOne)
110 DPL::BinaryQueue queue;
116 Catch(DPL::BinaryQueue::Exception::OutOfData)
125 Name: BinaryQueue_InitialFlattenConsumeOne
126 Description: tests reading and discarding more bytes than it is avaliable
127 Expected: exception throw
129 RUNNER_TEST(BinaryQueue_InitialFlattenConsumeOne)
131 DPL::BinaryQueue queue;
136 queue.FlattenConsume(&data, 1);
138 Catch(DPL::BinaryQueue::Exception::OutOfData)
147 Name: BinaryQueue_InitialFlattenOne
148 Description: tests reading more bytes than it is avaliable
149 Expected: exception throw
151 RUNNER_TEST(BinaryQueue_InitialFlattenOne)
153 DPL::BinaryQueue queue;
158 queue.Flatten(&data, 1);
160 Catch(DPL::BinaryQueue::Exception::OutOfData)
169 Name: BinaryQueue_ZeroCopyFrom
170 Description: tests coping content of empty queue to another (AppendCopyFrom)
171 Expected: source queue should be empty
173 RUNNER_TEST(BinaryQueue_ZeroCopyFrom)
175 DPL::BinaryQueue queue;
176 DPL::BinaryQueue copy;
178 copy.AppendCopyFrom(queue);
179 RUNNER_ASSERT(queue.Empty());
183 Name: BinaryQueue_ZeroMoveFrom
184 Description: tests moving content of empty queue to another
185 Expected: source queue should be empty
187 RUNNER_TEST(BinaryQueue_ZeroMoveFrom)
189 DPL::BinaryQueue queue;
190 DPL::BinaryQueue copy;
192 copy.AppendMoveFrom(queue);
193 RUNNER_ASSERT(queue.Empty());
197 Name: BinaryQueue_ZeroCopyTo
198 Description: tests moving content of empty queue to another (AppendCopyTo)
199 Expected: source queue should be empty
201 RUNNER_TEST(BinaryQueue_ZeroCopyTo)
203 DPL::BinaryQueue queue;
204 DPL::BinaryQueue copy;
206 queue.AppendCopyTo(copy);
207 RUNNER_ASSERT(queue.Empty());
211 Name: BinaryQueue_InsertSingleCharacters
212 Description: tests inserting single bytes to queue
213 Expected: stringified content and size shoudl match expected
215 RUNNER_TEST(BinaryQueue_InsertSingleCharacters)
217 DPL::BinaryQueue queue;
219 queue.AppendCopy("a", 1);
220 queue.AppendCopy("b", 1);
221 queue.AppendCopy("c", 1);
222 queue.AppendCopy("d", 1);
224 RUNNER_ASSERT(queue.Size() == 4);
225 RUNNER_ASSERT(BinaryQueueToString(queue) == "abcd");
229 Name: BinaryQueue_Consume
230 Description: tests comsuming portions of 1 or 2 bytes
231 Expected: stringified content and size should match expected
232 Bytes should be pope from begin.
234 RUNNER_TEST(BinaryQueue_Consume)
236 DPL::BinaryQueue queue;
238 queue.AppendCopy("abcd", 4);
239 queue.AppendCopy("ef", 2);
241 RUNNER_ASSERT(queue.Size() == 6);
244 RUNNER_ASSERT(queue.Size() == 5);
245 RUNNER_ASSERT(BinaryQueueToString(queue) == "bcdef");
248 RUNNER_ASSERT(queue.Size() == 3);
249 RUNNER_ASSERT(BinaryQueueToString(queue) == "def");
252 RUNNER_ASSERT(queue.Size() == 2);
253 RUNNER_ASSERT(BinaryQueueToString(queue) == "ef");
256 RUNNER_ASSERT(queue.Size() == 0);
257 RUNNER_ASSERT(BinaryQueueToString(queue) == "");
261 Name: BinaryQueue_Flatten
262 Description: tests comsuming portions of 1 and more bytes
263 Expected: stringified content and size should match expected
265 RUNNER_TEST(BinaryQueue_Flatten)
267 DPL::BinaryQueue queue;
269 queue.AppendCopy("abcd", 4);
270 queue.AppendCopy("ef", 2);
271 queue.AppendCopy("g", 1);
273 RUNNER_ASSERT(queue.Size() == 7);
275 RUNNER_ASSERT(BinaryQueueToString(queue) == "abcdefg");
279 Name: BinaryQueue_FlattenConsume
280 Description: tests comsuming portions of 1 and more bytes
281 Expected: stringified content and size should match expected
282 reading and discarding bytes should affect queue's size and content
284 RUNNER_TEST(BinaryQueue_FlattenConsume)
286 DPL::BinaryQueue queue;
288 queue.AppendCopy("abcd", 4);
289 queue.AppendCopy("ef", 2);
291 RUNNER_ASSERT(queue.Size() == 6);
293 char buffer[7] = { '\0' };
294 queue.FlattenConsume(buffer, 3);
296 RUNNER_ASSERT(queue.Size() == 3);
297 RUNNER_ASSERT(BinaryQueueToString(queue) == "def");
301 Name: BinaryQueue_AppendCopyFrom
302 Description: tests creating copy of not empty queue (use of: AppendCopyFrom)
303 Expected: stringified content and size should match expected
304 from original queue and it's copy
306 RUNNER_TEST(BinaryQueue_AppendCopyFrom)
308 DPL::BinaryQueue queue;
309 DPL::BinaryQueue copy;
311 queue.AppendCopy("abcd", 4);
312 queue.AppendCopy("ef", 2);
314 copy.AppendCopyFrom(queue);
316 RUNNER_ASSERT(queue.Size() == 6);
317 RUNNER_ASSERT(copy.Size() == 6);
318 RUNNER_ASSERT(BinaryQueueToString(queue) == "abcdef");
319 RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
323 Name: BinaryQueue_AppendCopyTo
324 Description: tests creating copy of not empty queue (use of: AppendCopyTo)
325 Expected: stringified content and size should match expected
326 from original queue and it's copy
328 RUNNER_TEST(BinaryQueue_AppendCopyTo)
330 DPL::BinaryQueue queue;
331 DPL::BinaryQueue copy;
333 queue.AppendCopy("abcd", 4);
334 queue.AppendCopy("ef", 2);
336 queue.AppendCopyTo(copy);
338 RUNNER_ASSERT(queue.Size() == 6);
339 RUNNER_ASSERT(copy.Size() == 6);
340 RUNNER_ASSERT(BinaryQueueToString(queue) == "abcdef");
341 RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
345 Name: BinaryQueue_AppendMoveFrom
346 Description: tests moving content of not empty queue (use of: AppendMoveFrom)
347 Expected: stringified content and size should match expected
348 for new queue. Old queue should be empty after operation
350 RUNNER_TEST(BinaryQueue_AppendMoveFrom)
352 DPL::BinaryQueue queue;
353 DPL::BinaryQueue copy;
355 queue.AppendCopy("abcd", 4);
356 queue.AppendCopy("ef", 2);
358 copy.AppendMoveFrom(queue);
360 RUNNER_ASSERT(queue.Size() == 0);
361 RUNNER_ASSERT(copy.Size() == 6);
362 RUNNER_ASSERT(BinaryQueueToString(queue) == "");
363 RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
367 Name: BinaryQueue_AppendMoveFrom
368 Description: tests moving content of not empty queue (use of: AppendMoveTo)
369 Expected: stringified content and size should match expected
370 for new queue. Old queue should be empty after operation
372 RUNNER_TEST(BinaryQueue_AppendMoveTo)
374 DPL::BinaryQueue queue;
375 DPL::BinaryQueue copy;
377 queue.AppendCopy("abcd", 4);
378 queue.AppendCopy("ef", 2);
380 queue.AppendMoveTo(copy);
382 RUNNER_ASSERT(queue.Size() == 0);
383 RUNNER_ASSERT(copy.Size() == 6);
384 RUNNER_ASSERT(BinaryQueueToString(queue) == "");
385 RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
389 public DPL::BinaryQueue::BucketVisitor
399 virtual void OnVisitBucket(const void *buffer, size_t bufferSize)
401 const char *str = static_cast<const char *>(buffer);
404 RUNNER_ASSERT(bufferSize == 4);
405 RUNNER_ASSERT(str[0] == 'a');
406 RUNNER_ASSERT(str[1] == 'b');
407 RUNNER_ASSERT(str[2] == 'c');
408 RUNNER_ASSERT(str[3] == 'd');
409 } else if (m_index == 1) {
410 RUNNER_ASSERT(bufferSize == 2);
411 RUNNER_ASSERT(str[0] == 'e');
412 RUNNER_ASSERT(str[1] == 'f');
422 Name: BinaryQueue_Visitor
423 Description: tests byte by byte content of queue by use of visitor
424 Expected: stringified content and size should match expected
425 Each byte should be at right position
427 RUNNER_TEST(BinaryQueue_Visitor)
429 DPL::BinaryQueue queue;
431 queue.AppendCopy("abcd", 4);
432 queue.AppendCopy("ef", 2);
435 queue.VisitBuckets(&visitor);
438 RUNNER_TEST(BinaryQueue_AbstracInputRead)
440 DPL::BinaryQueue queue;
442 queue.AppendCopy("abcd", 4);
446 RUNNER_ASSERT(BinaryQueueToString(*queue.Read(1).get()) == "a");
447 RUNNER_ASSERT(BinaryQueueToString(*queue.Read(2).get()) == "bc");
448 RUNNER_ASSERT(BinaryQueueToString(*queue.Read(1).get()) == "d");
450 RUNNER_ASSERT(queue.Size() == 0);
454 Name: BinaryQueue_AbstracOutputWrite
455 Description: tests appending one queue to another
456 Expected: written bytes shoudl affect content and size of queue
458 RUNNER_TEST(BinaryQueue_AbstracOutputWrite)
460 DPL::BinaryQueue queue;
461 queue.AppendCopy("abcd", 4);
463 DPL::BinaryQueue stream;
465 stream.Write(queue, 4);
467 RUNNER_ASSERT(BinaryQueueToString(*queue.Read(4).get()) == "abcd");