Tizen 2.0 Release
[framework/web/wrt-commons.git] / tests / dpl / 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 RUNNER_TEST(BinaryQueue_InitialEmpty)
36 {
37     DPL::BinaryQueue queue;
38     RUNNER_ASSERT(queue.Empty() == true);
39 }
40
41 RUNNER_TEST(BinaryQueue_InitialSize)
42 {
43     DPL::BinaryQueue queue;
44     RUNNER_ASSERT(queue.Size() == 0);
45 }
46
47 RUNNER_TEST(BinaryQueue_InitialCopy)
48 {
49     DPL::BinaryQueue queue;
50     DPL::BinaryQueue copy = queue;
51
52     RUNNER_ASSERT(copy.Size() == 0);
53 }
54
55 RUNNER_TEST(BinaryQueue_InitialConsumeZero)
56 {
57     DPL::BinaryQueue queue;
58     queue.Consume(0);
59 }
60
61 RUNNER_TEST(BinaryQueue_InitialFlattenConsumeZero)
62 {
63     DPL::BinaryQueue queue;
64     queue.FlattenConsume(NULL, 0);
65 }
66
67 RUNNER_TEST(BinaryQueue_InitialFlattenZero)
68 {
69     DPL::BinaryQueue queue;
70     queue.Flatten(NULL, 0);
71 }
72
73 RUNNER_TEST(BinaryQueue_InitialConsumeOne)
74 {
75     DPL::BinaryQueue queue;
76
77     Try
78     {
79         queue.Consume(1);
80     }
81     Catch (DPL::BinaryQueue::Exception::OutOfData)
82     {
83         return;
84     }
85
86     RUNNER_FAIL;
87 }
88
89 RUNNER_TEST(BinaryQueue_InitialFlattenConsumeOne)
90 {
91     DPL::BinaryQueue queue;
92
93     Try
94     {
95         char data;
96         queue.FlattenConsume(&data, 1);
97     }
98     Catch (DPL::BinaryQueue::Exception::OutOfData)
99     {
100         return;
101     }
102
103     RUNNER_FAIL;
104 }
105
106 RUNNER_TEST(BinaryQueue_InitialFlattenOne)
107 {
108     DPL::BinaryQueue queue;
109
110     Try
111     {
112         char data;
113         queue.Flatten(&data, 1);
114     }
115     Catch (DPL::BinaryQueue::Exception::OutOfData)
116     {
117         return;
118     }
119
120     RUNNER_FAIL;
121 }
122
123 RUNNER_TEST(BinaryQueue_ZeroCopyFrom)
124 {
125     DPL::BinaryQueue queue;
126     DPL::BinaryQueue copy;
127
128     copy.AppendCopyFrom(queue);
129     RUNNER_ASSERT(queue.Empty());
130 }
131
132 RUNNER_TEST(BinaryQueue_ZeroMoveFrom)
133 {
134     DPL::BinaryQueue queue;
135     DPL::BinaryQueue copy;
136
137     copy.AppendMoveFrom(queue);
138     RUNNER_ASSERT(queue.Empty());
139 }
140
141 RUNNER_TEST(BinaryQueue_ZeroCopyTo)
142 {
143     DPL::BinaryQueue queue;
144     DPL::BinaryQueue copy;
145
146     queue.AppendCopyTo(copy);
147     RUNNER_ASSERT(queue.Empty());
148 }
149
150 RUNNER_TEST(BinaryQueue_InsertSingleCharacters)
151 {
152     DPL::BinaryQueue queue;
153
154     queue.AppendCopy("a", 1);
155     queue.AppendCopy("b", 1);
156     queue.AppendCopy("c", 1);
157     queue.AppendCopy("d", 1);
158
159     RUNNER_ASSERT(queue.Size() == 4);
160     RUNNER_ASSERT(BinaryQueueToString(queue) == "abcd");
161 }
162
163 RUNNER_TEST(BinaryQueue_Consume)
164 {
165     DPL::BinaryQueue queue;
166
167     queue.AppendCopy("abcd", 4);
168     queue.AppendCopy("ef", 2);
169
170     RUNNER_ASSERT(queue.Size() == 6);
171
172     queue.Consume(1);
173     RUNNER_ASSERT(queue.Size() == 5);
174     RUNNER_ASSERT(BinaryQueueToString(queue) == "bcdef");
175
176     queue.Consume(2);
177     RUNNER_ASSERT(queue.Size() == 3);
178     RUNNER_ASSERT(BinaryQueueToString(queue) == "def");
179
180     queue.Consume(1);
181     RUNNER_ASSERT(queue.Size() == 2);
182     RUNNER_ASSERT(BinaryQueueToString(queue) == "ef");
183
184     queue.Consume(2);
185     RUNNER_ASSERT(queue.Size() == 0);
186     RUNNER_ASSERT(BinaryQueueToString(queue) == "");
187 }
188
189 RUNNER_TEST(BinaryQueue_Flatten)
190 {
191     DPL::BinaryQueue queue;
192
193     queue.AppendCopy("abcd", 4);
194     queue.AppendCopy("ef", 2);
195     queue.AppendCopy("g", 1);
196
197     RUNNER_ASSERT(queue.Size() == 7);
198
199     RUNNER_ASSERT(BinaryQueueToString(queue) == "abcdefg");
200 }
201
202 RUNNER_TEST(BinaryQueue_FlattenConsume)
203 {
204     DPL::BinaryQueue queue;
205
206     queue.AppendCopy("abcd", 4);
207     queue.AppendCopy("ef", 2);
208
209     RUNNER_ASSERT(queue.Size() == 6);
210
211     char buffer[7] = { '\0' };
212     queue.FlattenConsume(buffer, 3);
213
214     RUNNER_ASSERT(queue.Size() == 3);
215     RUNNER_ASSERT(BinaryQueueToString(queue) == "def");
216 }
217
218 RUNNER_TEST(BinaryQueue_AppendCopyFrom)
219 {
220     DPL::BinaryQueue queue;
221     DPL::BinaryQueue copy;
222
223     queue.AppendCopy("abcd", 4);
224     queue.AppendCopy("ef", 2);
225
226     copy.AppendCopyFrom(queue);
227
228     RUNNER_ASSERT(queue.Size() == 6);
229     RUNNER_ASSERT(copy.Size() == 6);
230     RUNNER_ASSERT(BinaryQueueToString(queue) == "abcdef");
231     RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
232 }
233
234 RUNNER_TEST(BinaryQueue_AppendCopyTo)
235 {
236     DPL::BinaryQueue queue;
237     DPL::BinaryQueue copy;
238
239     queue.AppendCopy("abcd", 4);
240     queue.AppendCopy("ef", 2);
241
242     queue.AppendCopyTo(copy);
243
244     RUNNER_ASSERT(queue.Size() == 6);
245     RUNNER_ASSERT(copy.Size() == 6);
246     RUNNER_ASSERT(BinaryQueueToString(queue) == "abcdef");
247     RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
248 }
249
250 RUNNER_TEST(BinaryQueue_AppendMoveFrom)
251 {
252     DPL::BinaryQueue queue;
253     DPL::BinaryQueue copy;
254
255     queue.AppendCopy("abcd", 4);
256     queue.AppendCopy("ef", 2);
257
258     copy.AppendMoveFrom(queue);
259
260     RUNNER_ASSERT(queue.Size() == 0);
261     RUNNER_ASSERT(copy.Size() == 6);
262     RUNNER_ASSERT(BinaryQueueToString(queue) == "");
263     RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
264 }
265
266 RUNNER_TEST(BinaryQueue_AppendMoveTo)
267 {
268     DPL::BinaryQueue queue;
269     DPL::BinaryQueue copy;
270
271     queue.AppendCopy("abcd", 4);
272     queue.AppendCopy("ef", 2);
273
274     queue.AppendMoveTo(copy);
275
276     RUNNER_ASSERT(queue.Size() == 0);
277     RUNNER_ASSERT(copy.Size() == 6);
278     RUNNER_ASSERT(BinaryQueueToString(queue) == "");
279     RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
280 }
281
282 class Visitor
283     : public DPL::BinaryQueue::BucketVisitor
284 {
285 private:
286     int m_index;
287
288 public:
289     Visitor()
290         : m_index(0)
291     {        
292     }
293
294     virtual void OnVisitBucket(const void *buffer, size_t bufferSize)
295     {
296         const char *str = static_cast<const char *>(buffer);
297         
298         if (m_index == 0)
299         {
300             RUNNER_ASSERT(bufferSize == 4);
301             RUNNER_ASSERT(str[0] == 'a');
302             RUNNER_ASSERT(str[1] == 'b');
303             RUNNER_ASSERT(str[2] == 'c');
304             RUNNER_ASSERT(str[3] == 'd');
305         }
306         else if (m_index == 1)
307         {
308             RUNNER_ASSERT(bufferSize == 2);
309             RUNNER_ASSERT(str[0] == 'e');
310             RUNNER_ASSERT(str[1] == 'f');
311         }
312         else
313         {
314             RUNNER_FAIL;
315         }
316         
317         ++m_index;
318     }
319 };
320
321 RUNNER_TEST(BinaryQueue_Visitor)
322 {
323     DPL::BinaryQueue queue;
324
325     queue.AppendCopy("abcd", 4);
326     queue.AppendCopy("ef", 2);
327
328     Visitor visitor;
329     queue.VisitBuckets(&visitor);
330 }
331
332 RUNNER_TEST(BinaryQueue_AbstracInputRead)
333 {
334     DPL::BinaryQueue queue;
335
336     queue.AppendCopy("abcd", 4);
337
338     queue.Read(0);
339
340     RUNNER_ASSERT(BinaryQueueToString(*queue.Read(1).get()) == "a");
341     RUNNER_ASSERT(BinaryQueueToString(*queue.Read(2).get()) == "bc");
342     RUNNER_ASSERT(BinaryQueueToString(*queue.Read(1).get()) == "d");
343
344     RUNNER_ASSERT(queue.Size() == 0);
345 }
346
347 RUNNER_TEST(BinaryQueue_AbstracOutputWrite)
348 {
349     DPL::BinaryQueue queue;
350     queue.AppendCopy("abcd", 4);
351
352     DPL::BinaryQueue stream;
353
354     stream.Write(queue, 4);
355
356     RUNNER_ASSERT(BinaryQueueToString(*queue.Read(4).get()) == "abcd");
357 }