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());
35 RUNNER_TEST(BinaryQueue_InitialEmpty)
37 DPL::BinaryQueue queue;
38 RUNNER_ASSERT(queue.Empty() == true);
41 RUNNER_TEST(BinaryQueue_InitialSize)
43 DPL::BinaryQueue queue;
44 RUNNER_ASSERT(queue.Size() == 0);
47 RUNNER_TEST(BinaryQueue_InitialCopy)
49 DPL::BinaryQueue queue;
50 DPL::BinaryQueue copy = queue;
52 RUNNER_ASSERT(copy.Size() == 0);
55 RUNNER_TEST(BinaryQueue_InitialConsumeZero)
57 DPL::BinaryQueue queue;
61 RUNNER_TEST(BinaryQueue_InitialFlattenConsumeZero)
63 DPL::BinaryQueue queue;
64 queue.FlattenConsume(NULL, 0);
67 RUNNER_TEST(BinaryQueue_InitialFlattenZero)
69 DPL::BinaryQueue queue;
70 queue.Flatten(NULL, 0);
73 RUNNER_TEST(BinaryQueue_InitialConsumeOne)
75 DPL::BinaryQueue queue;
81 Catch (DPL::BinaryQueue::Exception::OutOfData)
89 RUNNER_TEST(BinaryQueue_InitialFlattenConsumeOne)
91 DPL::BinaryQueue queue;
96 queue.FlattenConsume(&data, 1);
98 Catch (DPL::BinaryQueue::Exception::OutOfData)
106 RUNNER_TEST(BinaryQueue_InitialFlattenOne)
108 DPL::BinaryQueue queue;
113 queue.Flatten(&data, 1);
115 Catch (DPL::BinaryQueue::Exception::OutOfData)
123 RUNNER_TEST(BinaryQueue_ZeroCopyFrom)
125 DPL::BinaryQueue queue;
126 DPL::BinaryQueue copy;
128 copy.AppendCopyFrom(queue);
129 RUNNER_ASSERT(queue.Empty());
132 RUNNER_TEST(BinaryQueue_ZeroMoveFrom)
134 DPL::BinaryQueue queue;
135 DPL::BinaryQueue copy;
137 copy.AppendMoveFrom(queue);
138 RUNNER_ASSERT(queue.Empty());
141 RUNNER_TEST(BinaryQueue_ZeroCopyTo)
143 DPL::BinaryQueue queue;
144 DPL::BinaryQueue copy;
146 queue.AppendCopyTo(copy);
147 RUNNER_ASSERT(queue.Empty());
150 RUNNER_TEST(BinaryQueue_InsertSingleCharacters)
152 DPL::BinaryQueue queue;
154 queue.AppendCopy("a", 1);
155 queue.AppendCopy("b", 1);
156 queue.AppendCopy("c", 1);
157 queue.AppendCopy("d", 1);
159 RUNNER_ASSERT(queue.Size() == 4);
160 RUNNER_ASSERT(BinaryQueueToString(queue) == "abcd");
163 RUNNER_TEST(BinaryQueue_Consume)
165 DPL::BinaryQueue queue;
167 queue.AppendCopy("abcd", 4);
168 queue.AppendCopy("ef", 2);
170 RUNNER_ASSERT(queue.Size() == 6);
173 RUNNER_ASSERT(queue.Size() == 5);
174 RUNNER_ASSERT(BinaryQueueToString(queue) == "bcdef");
177 RUNNER_ASSERT(queue.Size() == 3);
178 RUNNER_ASSERT(BinaryQueueToString(queue) == "def");
181 RUNNER_ASSERT(queue.Size() == 2);
182 RUNNER_ASSERT(BinaryQueueToString(queue) == "ef");
185 RUNNER_ASSERT(queue.Size() == 0);
186 RUNNER_ASSERT(BinaryQueueToString(queue) == "");
189 RUNNER_TEST(BinaryQueue_Flatten)
191 DPL::BinaryQueue queue;
193 queue.AppendCopy("abcd", 4);
194 queue.AppendCopy("ef", 2);
195 queue.AppendCopy("g", 1);
197 RUNNER_ASSERT(queue.Size() == 7);
199 RUNNER_ASSERT(BinaryQueueToString(queue) == "abcdefg");
202 RUNNER_TEST(BinaryQueue_FlattenConsume)
204 DPL::BinaryQueue queue;
206 queue.AppendCopy("abcd", 4);
207 queue.AppendCopy("ef", 2);
209 RUNNER_ASSERT(queue.Size() == 6);
211 char buffer[7] = { '\0' };
212 queue.FlattenConsume(buffer, 3);
214 RUNNER_ASSERT(queue.Size() == 3);
215 RUNNER_ASSERT(BinaryQueueToString(queue) == "def");
218 RUNNER_TEST(BinaryQueue_AppendCopyFrom)
220 DPL::BinaryQueue queue;
221 DPL::BinaryQueue copy;
223 queue.AppendCopy("abcd", 4);
224 queue.AppendCopy("ef", 2);
226 copy.AppendCopyFrom(queue);
228 RUNNER_ASSERT(queue.Size() == 6);
229 RUNNER_ASSERT(copy.Size() == 6);
230 RUNNER_ASSERT(BinaryQueueToString(queue) == "abcdef");
231 RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
234 RUNNER_TEST(BinaryQueue_AppendCopyTo)
236 DPL::BinaryQueue queue;
237 DPL::BinaryQueue copy;
239 queue.AppendCopy("abcd", 4);
240 queue.AppendCopy("ef", 2);
242 queue.AppendCopyTo(copy);
244 RUNNER_ASSERT(queue.Size() == 6);
245 RUNNER_ASSERT(copy.Size() == 6);
246 RUNNER_ASSERT(BinaryQueueToString(queue) == "abcdef");
247 RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
250 RUNNER_TEST(BinaryQueue_AppendMoveFrom)
252 DPL::BinaryQueue queue;
253 DPL::BinaryQueue copy;
255 queue.AppendCopy("abcd", 4);
256 queue.AppendCopy("ef", 2);
258 copy.AppendMoveFrom(queue);
260 RUNNER_ASSERT(queue.Size() == 0);
261 RUNNER_ASSERT(copy.Size() == 6);
262 RUNNER_ASSERT(BinaryQueueToString(queue) == "");
263 RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
266 RUNNER_TEST(BinaryQueue_AppendMoveTo)
268 DPL::BinaryQueue queue;
269 DPL::BinaryQueue copy;
271 queue.AppendCopy("abcd", 4);
272 queue.AppendCopy("ef", 2);
274 queue.AppendMoveTo(copy);
276 RUNNER_ASSERT(queue.Size() == 0);
277 RUNNER_ASSERT(copy.Size() == 6);
278 RUNNER_ASSERT(BinaryQueueToString(queue) == "");
279 RUNNER_ASSERT(BinaryQueueToString(copy) == "abcdef");
283 : public DPL::BinaryQueue::BucketVisitor
294 virtual void OnVisitBucket(const void *buffer, size_t bufferSize)
296 const char *str = static_cast<const char *>(buffer);
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');
306 else if (m_index == 1)
308 RUNNER_ASSERT(bufferSize == 2);
309 RUNNER_ASSERT(str[0] == 'e');
310 RUNNER_ASSERT(str[1] == 'f');
321 RUNNER_TEST(BinaryQueue_Visitor)
323 DPL::BinaryQueue queue;
325 queue.AppendCopy("abcd", 4);
326 queue.AppendCopy("ef", 2);
329 queue.VisitBuckets(&visitor);
332 RUNNER_TEST(BinaryQueue_AbstracInputRead)
334 DPL::BinaryQueue queue;
336 queue.AppendCopy("abcd", 4);
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");
344 RUNNER_ASSERT(queue.Size() == 0);
347 RUNNER_TEST(BinaryQueue_AbstracOutputWrite)
349 DPL::BinaryQueue queue;
350 queue.AppendCopy("abcd", 4);
352 DPL::BinaryQueue stream;
354 stream.Write(queue, 4);
356 RUNNER_ASSERT(BinaryQueueToString(*queue.Read(4).get()) == "abcd");