tizen 2.3 release
[framework/web/wearable/wrt-commons.git] / tests / core / test_binary_queue.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_binary_queue.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of test binary queue
21  */
22 #include <dpl/test/test_runner.h>
23 #include <dpl/binary_queue.h>
24 RUNNER_TEST_GROUP_INIT(DPL)
25
26 inline std::string BinaryQueueToString(const DPL::BinaryQueue &queue)
27 {
28     char *buffer = new char[queue.Size()];
29     queue.Flatten(buffer, queue.Size());
30     std::string result = std::string(buffer, buffer + queue.Size());
31     delete[] buffer;
32     return result;
33 }
34
35 /*
36 Name: BinaryQueue_InitialEmpty
37 Description: tests emptiness of new constructed queue
38 Expected: new queue should be empty
39 */
40 RUNNER_TEST(BinaryQueue_InitialEmpty)
41 {
42     DPL::BinaryQueue queue;
43     RUNNER_ASSERT(queue.Empty() == true);
44 }
45
46 /*
47 Name: BinaryQueue_InitialSize
48 Description: tests emptiness of new constructed queue
49 Expected: new queue size should be equal 0
50 */
51 RUNNER_TEST(BinaryQueue_InitialSize)
52 {
53     DPL::BinaryQueue queue;
54     RUNNER_ASSERT(queue.Size() == 0);
55 }
56
57 /*
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
61 */
62 RUNNER_TEST(BinaryQueue_InitialCopy)
63 {
64     DPL::BinaryQueue queue;
65     DPL::BinaryQueue copy = queue;
66
67     RUNNER_ASSERT(copy.Size() == 0);
68 }
69
70 /*
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
74 */
75 RUNNER_TEST(BinaryQueue_InitialConsumeZero)
76 {
77     DPL::BinaryQueue queue;
78     queue.Consume(0);
79 }
80
81 /*
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
85 */
86 RUNNER_TEST(BinaryQueue_InitialFlattenConsumeZero)
87 {
88     DPL::BinaryQueue queue;
89     queue.FlattenConsume(NULL, 0);
90 }
91
92 /*
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
96 */
97 RUNNER_TEST(BinaryQueue_InitialFlattenZero)
98 {
99     DPL::BinaryQueue queue;
100     queue.Flatten(NULL, 0);
101 }
102
103 /*
104 Name: BinaryQueue_InitialConsumeOne
105 Description: tests discarding more bytes than it is avaliable
106 Expected: exception throw
107 */
108 RUNNER_TEST(BinaryQueue_InitialConsumeOne)
109 {
110     DPL::BinaryQueue queue;
111
112     Try
113     {
114         queue.Consume(1);
115     }
116     Catch(DPL::BinaryQueue::Exception::OutOfData)
117     {
118         return;
119     }
120
121     RUNNER_FAIL;
122 }
123
124 /*
125 Name: BinaryQueue_InitialFlattenConsumeOne
126 Description: tests reading and discarding more bytes than it is avaliable
127 Expected: exception throw
128 */
129 RUNNER_TEST(BinaryQueue_InitialFlattenConsumeOne)
130 {
131     DPL::BinaryQueue queue;
132
133     Try
134     {
135         char data;
136         queue.FlattenConsume(&data, 1);
137     }
138     Catch(DPL::BinaryQueue::Exception::OutOfData)
139     {
140         return;
141     }
142
143     RUNNER_FAIL;
144 }
145
146 /*
147 Name: BinaryQueue_InitialFlattenOne
148 Description: tests reading more bytes than it is avaliable
149 Expected: exception throw
150 */
151 RUNNER_TEST(BinaryQueue_InitialFlattenOne)
152 {
153     DPL::BinaryQueue queue;
154
155     Try
156     {
157         char data;
158         queue.Flatten(&data, 1);
159     }
160     Catch(DPL::BinaryQueue::Exception::OutOfData)
161     {
162         return;
163     }
164
165     RUNNER_FAIL;
166 }
167
168 /*
169 Name: BinaryQueue_ZeroCopyFrom
170 Description: tests coping content of empty queue to another (AppendCopyFrom)
171 Expected: source queue should be empty
172 */
173 RUNNER_TEST(BinaryQueue_ZeroCopyFrom)
174 {
175     DPL::BinaryQueue queue;
176     DPL::BinaryQueue copy;
177
178     copy.AppendCopyFrom(queue);
179     RUNNER_ASSERT(queue.Empty());
180 }
181
182 /*
183 Name: BinaryQueue_ZeroMoveFrom
184 Description: tests moving content of empty queue to another
185 Expected: source queue should be empty
186 */
187 RUNNER_TEST(BinaryQueue_ZeroMoveFrom)
188 {
189     DPL::BinaryQueue queue;
190     DPL::BinaryQueue copy;
191
192     copy.AppendMoveFrom(queue);
193     RUNNER_ASSERT(queue.Empty());
194 }
195
196 /*
197 Name: BinaryQueue_ZeroCopyTo
198 Description: tests moving content of empty queue to another (AppendCopyTo)
199 Expected: source queue should be empty
200 */
201 RUNNER_TEST(BinaryQueue_ZeroCopyTo)
202 {
203     DPL::BinaryQueue queue;
204     DPL::BinaryQueue copy;
205
206     queue.AppendCopyTo(copy);
207     RUNNER_ASSERT(queue.Empty());
208 }
209
210 /*
211 Name: BinaryQueue_InsertSingleCharacters
212 Description: tests inserting single bytes to queue
213 Expected: stringified content and size shoudl match expected
214 */
215 RUNNER_TEST(BinaryQueue_InsertSingleCharacters)
216 {
217     DPL::BinaryQueue queue;
218
219     queue.AppendCopy("a", 1);
220     queue.AppendCopy("b", 1);
221     queue.AppendCopy("c", 1);
222     queue.AppendCopy("d", 1);
223
224     RUNNER_ASSERT(queue.Size() == 4);
225     RUNNER_ASSERT(BinaryQueueToString(queue) == "abcd");
226 }
227
228 /*
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.
233 */
234 RUNNER_TEST(BinaryQueue_Consume)
235 {
236     DPL::BinaryQueue queue;
237
238     queue.AppendCopy("abcd", 4);
239     queue.AppendCopy("ef", 2);
240
241     RUNNER_ASSERT(queue.Size() == 6);
242
243     queue.Consume(1);
244     RUNNER_ASSERT(queue.Size() == 5);
245     RUNNER_ASSERT(BinaryQueueToString(queue) == "bcdef");
246
247     queue.Consume(2);
248     RUNNER_ASSERT(queue.Size() == 3);
249     RUNNER_ASSERT(BinaryQueueToString(queue) == "def");
250
251     queue.Consume(1);
252     RUNNER_ASSERT(queue.Size() == 2);
253     RUNNER_ASSERT(BinaryQueueToString(queue) == "ef");
254
255     queue.Consume(2);
256     RUNNER_ASSERT(queue.Size() == 0);
257     RUNNER_ASSERT(BinaryQueueToString(queue) == "");
258 }
259
260 /*
261 Name: BinaryQueue_Flatten
262 Description: tests comsuming portions of 1 and more bytes
263 Expected: stringified content and size should match expected
264 */
265 RUNNER_TEST(BinaryQueue_Flatten)
266 {
267     DPL::BinaryQueue queue;
268
269     queue.AppendCopy("abcd", 4);
270     queue.AppendCopy("ef", 2);
271     queue.AppendCopy("g", 1);
272
273     RUNNER_ASSERT(queue.Size() == 7);
274
275     RUNNER_ASSERT(BinaryQueueToString(queue) == "abcdefg");
276 }
277
278 /*
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
283 */
284 RUNNER_TEST(BinaryQueue_FlattenConsume)
285 {
286     DPL::BinaryQueue queue;
287
288     queue.AppendCopy("abcd", 4);
289     queue.AppendCopy("ef", 2);
290
291     RUNNER_ASSERT(queue.Size() == 6);
292
293     char buffer[7] = { '\0' };
294     queue.FlattenConsume(buffer, 3);
295
296     RUNNER_ASSERT(queue.Size() == 3);
297     RUNNER_ASSERT(BinaryQueueToString(queue) == "def");
298 }
299
300 /*
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
305 */
306 RUNNER_TEST(BinaryQueue_AppendCopyFrom)
307 {
308     DPL::BinaryQueue queue;
309     DPL::BinaryQueue copy;
310
311     queue.AppendCopy("abcd", 4);
312     queue.AppendCopy("ef", 2);
313
314     copy.AppendCopyFrom(queue);
315
316     RUNNER_ASSERT(queue.Size() == 6);
317     RUNNER_ASSERT(copy.Size() == 6);
318     RUNNER_ASSERT(BinaryQueueToString(queue) == "abcdef");
319     RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
320 }
321
322 /*
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
327 */
328 RUNNER_TEST(BinaryQueue_AppendCopyTo)
329 {
330     DPL::BinaryQueue queue;
331     DPL::BinaryQueue copy;
332
333     queue.AppendCopy("abcd", 4);
334     queue.AppendCopy("ef", 2);
335
336     queue.AppendCopyTo(copy);
337
338     RUNNER_ASSERT(queue.Size() == 6);
339     RUNNER_ASSERT(copy.Size() == 6);
340     RUNNER_ASSERT(BinaryQueueToString(queue) == "abcdef");
341     RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
342 }
343
344 /*
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
349 */
350 RUNNER_TEST(BinaryQueue_AppendMoveFrom)
351 {
352     DPL::BinaryQueue queue;
353     DPL::BinaryQueue copy;
354
355     queue.AppendCopy("abcd", 4);
356     queue.AppendCopy("ef", 2);
357
358     copy.AppendMoveFrom(queue);
359
360     RUNNER_ASSERT(queue.Size() == 0);
361     RUNNER_ASSERT(copy.Size() == 6);
362     RUNNER_ASSERT(BinaryQueueToString(queue) == "");
363     RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
364 }
365
366 /*
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
371 */
372 RUNNER_TEST(BinaryQueue_AppendMoveTo)
373 {
374     DPL::BinaryQueue queue;
375     DPL::BinaryQueue copy;
376
377     queue.AppendCopy("abcd", 4);
378     queue.AppendCopy("ef", 2);
379
380     queue.AppendMoveTo(copy);
381
382     RUNNER_ASSERT(queue.Size() == 0);
383     RUNNER_ASSERT(copy.Size() == 6);
384     RUNNER_ASSERT(BinaryQueueToString(queue) == "");
385     RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
386 }
387
388 class Visitor :
389     public DPL::BinaryQueue::BucketVisitor
390 {
391   private:
392     int m_index;
393
394   public:
395     Visitor() :
396         m_index(0)
397     {}
398
399     virtual void OnVisitBucket(const void *buffer, size_t bufferSize)
400     {
401         const char *str = static_cast<const char *>(buffer);
402
403         if (m_index == 0) {
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');
413         } else {
414             RUNNER_FAIL;
415         }
416
417         ++m_index;
418     }
419 };
420
421 /*
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
426 */
427 RUNNER_TEST(BinaryQueue_Visitor)
428 {
429     DPL::BinaryQueue queue;
430
431     queue.AppendCopy("abcd", 4);
432     queue.AppendCopy("ef", 2);
433
434     Visitor visitor;
435     queue.VisitBuckets(&visitor);
436 }
437
438 RUNNER_TEST(BinaryQueue_AbstracInputRead)
439 {
440     DPL::BinaryQueue queue;
441
442     queue.AppendCopy("abcd", 4);
443
444     queue.Read(0);
445
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");
449
450     RUNNER_ASSERT(queue.Size() == 0);
451 }
452
453 /*
454 Name: BinaryQueue_AbstracOutputWrite
455 Description: tests appending one queue to another
456 Expected: written bytes shoudl affect content and size of queue
457 */
458 RUNNER_TEST(BinaryQueue_AbstracOutputWrite)
459 {
460     DPL::BinaryQueue queue;
461     queue.AppendCopy("abcd", 4);
462
463     DPL::BinaryQueue stream;
464
465     stream.Write(queue, 4);
466
467     RUNNER_ASSERT(BinaryQueueToString(*queue.Read(4).get()) == "abcd");
468 }