Debian Codegraph @

tests/test_bytevector.cpp

raw symbols SCIP
1 /***************************************************************************
2 copyright : (C) 2007 by Lukas Lalinsky
3 email : [email protected]
4 ***************************************************************************/
5
6 /***************************************************************************
7 * This library is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License version *
9 * 2.1 as published by the Free Software Foundation. *
10 * *
11 * This library is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the Free Software *
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
19 * 02110-1301 USA *
20 * *
21 * Alternatively, this file is available under the Mozilla Public *
22 * License Version 1.1. You may obtain a copy of the License at *
23 * http://www.mozilla.org/MPL/ *
24 ***************************************************************************/
25
26 #include <cstring>
27 #define _USE_MATH_DEFINES
28 #include <cmath>
29
30 #include "tbytevector.h"
31 #include "tbytevectorlist.h"
32 #include <cppunit/extensions/HelperMacros.h>
33
34 using namespace std;
35 using namespace TagLib;
36
37 class TestByteVector : public CppUnit::TestFixture
38 {
39 CPPUNIT_TEST_SUITE(TestByteVector);
40 CPPUNIT_TEST(testByteVector);
41 CPPUNIT_TEST(testFind1);
42 CPPUNIT_TEST(testFind2);
43 CPPUNIT_TEST(testFind3);
44 CPPUNIT_TEST(testRfind1);
45 CPPUNIT_TEST(testRfind2);
46 CPPUNIT_TEST(testRfind3);
47 CPPUNIT_TEST(testToHex);
48 CPPUNIT_TEST(testIntegerConversion);
49 CPPUNIT_TEST(testFloatingPointConversion);
50 CPPUNIT_TEST(testReplace);
51 CPPUNIT_TEST(testReplaceAndDetach);
52 CPPUNIT_TEST(testIterator);
53 CPPUNIT_TEST(testResize);
54 CPPUNIT_TEST(testAppend1);
55 CPPUNIT_TEST(testAppend2);
56 CPPUNIT_TEST(testBase64);
57 CPPUNIT_TEST(testEmpty);
58 CPPUNIT_TEST_SUITE_END();
59
60 public:
61 void testByteVector()
62 {
63 ByteVector s1("foo");
64 CPPUNIT_ASSERT(ByteVectorList::split(s1, " ").size() == 1);
65
66 ByteVector s2("f");
67 CPPUNIT_ASSERT(ByteVectorList::split(s2, " ").size() == 1);
68
69 CPPUNIT_ASSERT(ByteVector().isEmpty());
70 CPPUNIT_ASSERT_EQUAL(0U, ByteVector().size());
71 CPPUNIT_ASSERT(ByteVector("asdf").clear().isEmpty());
72 CPPUNIT_ASSERT_EQUAL(0U, ByteVector("asdf").clear().size());
73 CPPUNIT_ASSERT_EQUAL(ByteVector(), ByteVector("asdf").clear());
74
75 ByteVector i("blah blah");
76 ByteVector j("blah");
77 CPPUNIT_ASSERT(i.containsAt(j, 5, 0));
78 CPPUNIT_ASSERT(i.containsAt(j, 6, 1));
79 CPPUNIT_ASSERT(i.containsAt(j, 6, 1, 3));
80
81 i.clear();
82 CPPUNIT_ASSERT(i.isEmpty());
83 }
84
85 void testFind1()
86 {
87 CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find("SggO"));
88 CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find("SggO", 0));
89 CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find("SggO", 1));
90 CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find("SggO", 2));
91 CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find("SggO", 3));
92 CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find("SggO", 4));
93 CPPUNIT_ASSERT_EQUAL(-1, ByteVector("....SggO."). find("SggO", 5));
94 CPPUNIT_ASSERT_EQUAL(-1, ByteVector("....SggO."). find("SggO", 6));
95 CPPUNIT_ASSERT_EQUAL(-1, ByteVector("....SggO."). find("SggO", 7));
96 CPPUNIT_ASSERT_EQUAL(-1, ByteVector("....SggO."). find("SggO", 8));
97
98 // Intentional out-of-bounds access.
99 ByteVector v("0123456789x");
100 v.resize(10);
101 v.data()[10] = 'x';
102 CPPUNIT_ASSERT_EQUAL(-1, v.find("789x", 7));
103 }
104
105 void testFind2()
106 {
107 CPPUNIT_ASSERT_EQUAL(0, ByteVector("\x01", 1).find("\x01"));
108 CPPUNIT_ASSERT_EQUAL(0, ByteVector("\x01\x02", 2).find("\x01\x02"));
109 CPPUNIT_ASSERT_EQUAL(-1, ByteVector("\x01", 1).find("\x02"));
110 CPPUNIT_ASSERT_EQUAL(-1, ByteVector("\x01\x02", 2).find("\x01\x03"));
111 }
112
113 void testFind3()
114 {
115 CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find('S'));
116 CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find('S', 0));
117 CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find('S', 1));
118 CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find('S', 2));
119 CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find('S', 3));
120 CPPUNIT_ASSERT_EQUAL(4, ByteVector("....SggO."). find('S', 4));
121 CPPUNIT_ASSERT_EQUAL(-1, ByteVector("....SggO."). find('S', 5));
122 CPPUNIT_ASSERT_EQUAL(-1, ByteVector("....SggO."). find('S', 6));
123 CPPUNIT_ASSERT_EQUAL(-1, ByteVector("....SggO."). find('S', 7));
124 CPPUNIT_ASSERT_EQUAL(-1, ByteVector("....SggO."). find('S', 8));
125 }
126
127 void testRfind1()
128 {
129 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 0));
130 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 1));
131 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 2));
132 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 3));
133 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 4));
134 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 5));
135 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 6));
136 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 7));
137 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS", 8));
138 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind("OggS"));
139 }
140
141 void testRfind2()
142 {
143 ByteVector r0("**************");
144 ByteVector r1("OggS**********");
145 ByteVector r2("**********OggS");
146 ByteVector r3("OggS******OggS");
147 ByteVector r4("OggS*OggS*OggS");
148
149 CPPUNIT_ASSERT_EQUAL(-1, r0.find("OggS"));
150 CPPUNIT_ASSERT_EQUAL(-1, r0.rfind("OggS"));
151 CPPUNIT_ASSERT_EQUAL(0, r1.find("OggS"));
152 CPPUNIT_ASSERT_EQUAL(0, r1.rfind("OggS"));
153 CPPUNIT_ASSERT_EQUAL(10, r2.find("OggS"));
154 CPPUNIT_ASSERT_EQUAL(10, r2.rfind("OggS"));
155 CPPUNIT_ASSERT_EQUAL(0, r3.find("OggS"));
156 CPPUNIT_ASSERT_EQUAL(10, r3.rfind("OggS"));
157 CPPUNIT_ASSERT_EQUAL(10, r4.rfind("OggS"));
158 CPPUNIT_ASSERT_EQUAL(10, r4.rfind("OggS", 0));
159 CPPUNIT_ASSERT_EQUAL(5, r4.rfind("OggS", 7));
160 CPPUNIT_ASSERT_EQUAL(10, r4.rfind("OggS", 12));
161 }
162
163 void testRfind3()
164 {
165 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind('O', 0));
166 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind('O', 1));
167 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind('O', 2));
168 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind('O', 3));
169 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind('O', 4));
170 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind('O', 5));
171 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind('O', 6));
172 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind('O', 7));
173 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind('O', 8));
174 CPPUNIT_ASSERT_EQUAL(1, ByteVector(".OggS....").rfind('O'));
175 }
176
177 void testToHex()
178 {
179 ByteVector v("\xf0\xe1\xd2\xc3\xb4\xa5\x96\x87\x78\x69\x5a\x4b\x3c\x2d\x1e\x0f", 16);
180
181 CPPUNIT_ASSERT_EQUAL(ByteVector("f0e1d2c3b4a5968778695a4b3c2d1e0f"), v.toHex());
182 }
183
184 void testIntegerConversion()
185 {
186 const ByteVector data("\x00\xff\x01\xff\x00\xff\x01\xff\x00\xff\x01\xff\x00\xff", 14);
187
188 CPPUNIT_ASSERT_EQUAL(static_cast<short>(0x00ff), data.toShort());
189 CPPUNIT_ASSERT_EQUAL(static_cast<short>(0xff00), data.toShort(false));
190 CPPUNIT_ASSERT_EQUAL(static_cast<short>(0xff01), data.toShort(5U));
191 CPPUNIT_ASSERT_EQUAL(static_cast<short>(0x01ff), data.toShort(5U, false));
192 CPPUNIT_ASSERT_EQUAL(static_cast<short>(0xff), data.toShort(13U));
193 CPPUNIT_ASSERT_EQUAL(static_cast<short>(0xff), data.toShort(13U, false));
194 CPPUNIT_ASSERT_EQUAL(ByteVector::fromShort(0x00ff), ByteVector("\x00\xff", 2));
195 CPPUNIT_ASSERT_EQUAL(ByteVector::fromShort(0x00ff, false), ByteVector("\xff\x00", 2));
196
197 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(0x00ff), data.toUShort());
198 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(0xff00), data.toUShort(false));
199 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(0xff01), data.toUShort(5U));
200 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(0x01ff), data.toUShort(5U, false));
201 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(0xff), data.toUShort(13U));
202 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(0xff), data.toUShort(13U, false));
203 CPPUNIT_ASSERT_EQUAL(ByteVector::fromUShort(0xff00), ByteVector("\xff\x00", 2));
204 CPPUNIT_ASSERT_EQUAL(ByteVector::fromUShort(0xff00, false), ByteVector("\x00\xff", 2));
205
206 CPPUNIT_ASSERT_EQUAL(0x00ff01ffU, data.toUInt());
207 CPPUNIT_ASSERT_EQUAL(0xff01ff00U, data.toUInt(false));
208 CPPUNIT_ASSERT_EQUAL(0xff01ff00U, data.toUInt(5U));
209 CPPUNIT_ASSERT_EQUAL(0x00ff01ffU, data.toUInt(5U, false));
210 CPPUNIT_ASSERT_EQUAL(0x00ffU, data.toUInt(12U));
211 CPPUNIT_ASSERT_EQUAL(0xff00U, data.toUInt(12U, false));
212
213 CPPUNIT_ASSERT_EQUAL(0x00ff01U, data.toUInt(0U, 3U));
214 CPPUNIT_ASSERT_EQUAL(0x01ff00U, data.toUInt(0U, 3U, false));
215 CPPUNIT_ASSERT_EQUAL(0xff01ffU, data.toUInt(5U, 3U));
216 CPPUNIT_ASSERT_EQUAL(0xff01ffU, data.toUInt(5U, 3U, false));
217 CPPUNIT_ASSERT_EQUAL(0x00ffU, data.toUInt(12U, 3U));
218 CPPUNIT_ASSERT_EQUAL(0xff00U, data.toUInt(12U, 3U, false));
219 CPPUNIT_ASSERT_EQUAL(ByteVector::fromUInt(0xff00ff00), ByteVector("\xff\x00\xff\x00", 4));
220 CPPUNIT_ASSERT_EQUAL(ByteVector::fromUInt(0xff00ff00, false), ByteVector("\x00\xff\x00\xff", 4));
221
222 CPPUNIT_ASSERT_EQUAL(static_cast<long long>(0x00ff01ff00ff01ffULL), data.toLongLong());
223 CPPUNIT_ASSERT_EQUAL(static_cast<long long>(0xff01ff00ff01ff00ULL), data.toLongLong(false));
224 CPPUNIT_ASSERT_EQUAL(static_cast<long long>(0xff01ff00ff01ff00ULL), data.toLongLong(5U));
225 CPPUNIT_ASSERT_EQUAL(static_cast<long long>(0x00ff01ff00ff01ffULL), data.toLongLong(5U, false));
226 CPPUNIT_ASSERT_EQUAL(static_cast<long long>(0x00ffU), data.toLongLong(12U));
227 CPPUNIT_ASSERT_EQUAL(static_cast<long long>(0xff00U), data.toLongLong(12U, false));
228 CPPUNIT_ASSERT_EQUAL(ByteVector::fromLongLong(0x00ff01ff00ff01ff), ByteVector("\x00\xff\x01\xff\x00\xff\x01\xff", 8));
229 CPPUNIT_ASSERT_EQUAL(ByteVector::fromLongLong(0x00ff01ff00ff01ff, false), ByteVector("\xff\x01\xff\x00\xff\x01\xff\x00", 8));
230
231 const ByteVector data2("\xff\x01\xff\x00\xff\x01\xff\x00\xff\x01\xff\x00\xff\x01", 14);
232 CPPUNIT_ASSERT_EQUAL(0xff01ff00ff01ff00ULL, data2.toULongLong());
233 CPPUNIT_ASSERT_EQUAL(0x00ff01ff00ff01ffULL, data2.toULongLong(false));
234 CPPUNIT_ASSERT_EQUAL(0x01ff00ff01ff00ffULL, data2.toULongLong(5U));
235 CPPUNIT_ASSERT_EQUAL(0xff00ff01ff00ff01ULL, data2.toULongLong(5U, false));
236 CPPUNIT_ASSERT_EQUAL(0xff01ULL, data2.toULongLong(12U));
237 CPPUNIT_ASSERT_EQUAL(0x01ffULL, data2.toULongLong(12U, false));
238 CPPUNIT_ASSERT_EQUAL(ByteVector::fromULongLong(0xff01ff00ff01ff00ULL), ByteVector("\xff\x01\xff\x00\xff\x01\xff\x00", 8));
239 CPPUNIT_ASSERT_EQUAL(ByteVector::fromULongLong(0xff01ff00ff01ff00ULL, false), ByteVector("\x00\xff\x01\xff\x00\xff\x01\xff", 8));
240 }
241
242
243 void testFloatingPointConversion()
244 {
245 constexpr double Tolerance = 1.0e-7;
246
247 const ByteVector pi32le("\xdb\x0f\x49\x40", 4);
248 CPPUNIT_ASSERT(std::abs(pi32le.toFloat32LE(0) - M_PI) < Tolerance);
249 CPPUNIT_ASSERT_EQUAL(pi32le, ByteVector::fromFloat32LE(pi32le.toFloat32LE(0)));
250
251 const ByteVector pi32be("\x40\x49\x0f\xdb", 4);
252 CPPUNIT_ASSERT(std::abs(pi32be.toFloat32BE(0) - M_PI) < Tolerance);
253 CPPUNIT_ASSERT_EQUAL(pi32be, ByteVector::fromFloat32BE(pi32be.toFloat32BE(0)));
254
255 const ByteVector pi64le("\x18\x2d\x44\x54\xfb\x21\x09\x40", 8);
256 CPPUNIT_ASSERT(std::abs(pi64le.toFloat64LE(0) - M_PI) < Tolerance);
257 CPPUNIT_ASSERT_EQUAL(pi64le, ByteVector::fromFloat64LE(pi64le.toFloat64LE(0)));
258
259 const ByteVector pi64be("\x40\x09\x21\xfb\x54\x44\x2d\x18", 8);
260 CPPUNIT_ASSERT(std::abs(pi64be.toFloat64BE(0) - M_PI) < Tolerance);
261 CPPUNIT_ASSERT_EQUAL(pi64be, ByteVector::fromFloat64BE(pi64be.toFloat64BE(0)));
262
263 const ByteVector pi80le("\x00\xc0\x68\x21\xa2\xda\x0f\xc9\x00\x40", 10);
264 CPPUNIT_ASSERT(std::abs(pi80le.toFloat80LE(0) - M_PI) < Tolerance);
265
266 const ByteVector pi80be("\x40\x00\xc9\x0f\xda\xa2\x21\x68\xc0\x00", 10);
267 CPPUNIT_ASSERT(std::abs(pi80be.toFloat80BE(0) - M_PI) < Tolerance);
268 }
269
270 void testReplace()
271 {
272 {
273 ByteVector a("abcdabf");
274 a.replace(ByteVector(""), ByteVector("<a>"));
275 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdabf"), a);
276 }
277 {
278 ByteVector a("abcdabf");
279 a.replace(ByteVector("foobartoolong"), ByteVector("<a>"));
280 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdabf"), a);
281 }
282 {
283 ByteVector a("abcdabf");
284 a.replace(ByteVector("xx"), ByteVector("yy"));
285 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdabf"), a);
286 }
287 {
288 ByteVector a("abcdabf");
289 a.replace(ByteVector("a"), ByteVector("x"));
290 CPPUNIT_ASSERT_EQUAL(ByteVector("xbcdxbf"), a);
291 a.replace(ByteVector("x"), ByteVector("a"));
292 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdabf"), a);
293 }
294 {
295 ByteVector a("abcdabf");
296 a.replace('a', 'x');
297 CPPUNIT_ASSERT_EQUAL(ByteVector("xbcdxbf"), a);
298 a.replace('x', 'a');
299 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdabf"), a);
300 }
301 {
302 ByteVector a("abcdabf");
303 a.replace(ByteVector("ab"), ByteVector("xy"));
304 CPPUNIT_ASSERT_EQUAL(ByteVector("xycdxyf"), a);
305 a.replace(ByteVector("xy"), ByteVector("ab"));
306 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdabf"), a);
307 }
308 {
309 ByteVector a("abcdabf");
310 a.replace(ByteVector("a"), ByteVector("<a>"));
311 CPPUNIT_ASSERT_EQUAL(ByteVector("<a>bcd<a>bf"), a);
312 a.replace(ByteVector("<a>"), ByteVector("a"));
313 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdabf"), a);
314 }
315 {
316 ByteVector a("abcdabf");
317 a.replace(ByteVector("b"), ByteVector("<b>"));
318 CPPUNIT_ASSERT_EQUAL(ByteVector("a<b>cda<b>f"), a);
319 a.replace(ByteVector("<b>"), ByteVector("b"));
320 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdabf"), a);
321 }
322 {
323 ByteVector a("abcdabc");
324 a.replace(ByteVector("c"), ByteVector("<c>"));
325 CPPUNIT_ASSERT_EQUAL(ByteVector("ab<c>dab<c>"), a);
326 a.replace(ByteVector("<c>"), ByteVector("c"));
327 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdabc"), a);
328 }
329 {
330 ByteVector a("abcdaba");
331 a.replace(ByteVector("a"), ByteVector("<a>"));
332 CPPUNIT_ASSERT_EQUAL(ByteVector("<a>bcd<a>b<a>"), a);
333 a.replace(ByteVector("<a>"), ByteVector("a"));
334 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdaba"), a);
335 }
336 }
337
338 void testReplaceAndDetach()
339 {
340 {
341 ByteVector a("abcdabf");
342 ByteVector b = a;
343 a.replace(ByteVector("a"), ByteVector("x"));
344 CPPUNIT_ASSERT_EQUAL(ByteVector("xbcdxbf"), a);
345 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdabf"), b);
346 }
347 {
348 ByteVector a("abcdabf");
349 ByteVector b = a;
350 a.replace('a', 'x');
351 CPPUNIT_ASSERT_EQUAL(ByteVector("xbcdxbf"), a);
352 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdabf"), b);
353 }
354 {
355 ByteVector a("abcdabf");
356 ByteVector b = a;
357 a.replace(ByteVector("ab"), ByteVector("xy"));
358 CPPUNIT_ASSERT_EQUAL(ByteVector("xycdxyf"), a);
359 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdabf"), b);
360 }
361 {
362 ByteVector a("abcdabf");
363 ByteVector b = a;
364 a.replace(ByteVector("a"), ByteVector("<a>"));
365 CPPUNIT_ASSERT_EQUAL(ByteVector("<a>bcd<a>bf"), a);
366 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdabf"), b);
367 }
368 {
369 ByteVector a("ab<c>dab<c>");
370 ByteVector b = a;
371 a.replace(ByteVector("<c>"), ByteVector("c"));
372 CPPUNIT_ASSERT_EQUAL(ByteVector("abcdabc"), a);
373 CPPUNIT_ASSERT_EQUAL(ByteVector("ab<c>dab<c>"), b);
374 }
375 }
376
377 void testIterator()
378 {
379 ByteVector v1("taglib");
380 ByteVector v2 = v1;
381
382 auto it1 = v1.begin();
383 auto it2 = v2.begin();
384
385 CPPUNIT_ASSERT_EQUAL('t', *it1);
386 CPPUNIT_ASSERT_EQUAL('t', *it2);
387
388 std::advance(it1, 4);
389 std::advance(it2, 4);
390 *it2 = 'I';
391 CPPUNIT_ASSERT_EQUAL('i', *it1);
392 CPPUNIT_ASSERT_EQUAL('I', *it2);
393 CPPUNIT_ASSERT_EQUAL(ByteVector("taglib"), v1);
394 CPPUNIT_ASSERT_EQUAL(ByteVector("taglIb"), v2);
395
396 auto it3 = v1.rbegin();
397 auto it4 = v2.rbegin();
398
399 CPPUNIT_ASSERT_EQUAL('b', *it3);
400 CPPUNIT_ASSERT_EQUAL('b', *it4);
401
402 std::advance(it3, 4);
403 std::advance(it4, 4);
404 *it4 = 'A';
405 CPPUNIT_ASSERT_EQUAL('a', *it3);
406 CPPUNIT_ASSERT_EQUAL('A', *it4);
407 CPPUNIT_ASSERT_EQUAL(ByteVector("taglib"), v1);
408 CPPUNIT_ASSERT_EQUAL(ByteVector("tAglIb"), v2);
409
410 ByteVector v3 = ByteVector("0123456789").mid(3, 4);
411
412 it1 = v3.begin();
413 it2 = v3.end() - 1;
414 CPPUNIT_ASSERT_EQUAL('3', *it1);
415 CPPUNIT_ASSERT_EQUAL('6', *it2);
416
417 it3 = v3.rbegin();
418 it4 = v3.rend() - 1;
419 CPPUNIT_ASSERT_EQUAL('6', *it3);
420 CPPUNIT_ASSERT_EQUAL('3', *it4);
421 }
422
423 void testResize()
424 {
425 auto a = ByteVector("0123456789");
426 ByteVector b = a.mid(3, 4);
427 b.resize(6, 'A');
428 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(6), b.size());
429 CPPUNIT_ASSERT_EQUAL('6', b[3]);
430 CPPUNIT_ASSERT_EQUAL('A', b[4]);
431 CPPUNIT_ASSERT_EQUAL('A', b[5]);
432 b.resize(10, 'B');
433 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(10), b.size());
434 CPPUNIT_ASSERT_EQUAL('6', b[3]);
435 CPPUNIT_ASSERT_EQUAL('B', b[6]);
436 CPPUNIT_ASSERT_EQUAL('B', b[9]);
437 b.resize(3, 'C');
438 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), b.size());
439 CPPUNIT_ASSERT_EQUAL(-1, b.find('C'));
440 b.resize(3);
441 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), b.size());
442
443 // Check if a and b were properly detached.
444
445 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(10), a.size());
446 CPPUNIT_ASSERT_EQUAL('3', a[3]);
447 CPPUNIT_ASSERT_EQUAL('5', a[5]);
448
449 // Special case that refCount == 1 and d->offset != 0.
450
451 ByteVector c = ByteVector("0123456789").mid(3, 4);
452 c.resize(6, 'A');
453 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(6), c.size());
454 CPPUNIT_ASSERT_EQUAL('6', c[3]);
455 CPPUNIT_ASSERT_EQUAL('A', c[4]);
456 CPPUNIT_ASSERT_EQUAL('A', c[5]);
457 c.resize(10, 'B');
458 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(10), c.size());
459 CPPUNIT_ASSERT_EQUAL('6', c[3]);
460 CPPUNIT_ASSERT_EQUAL('B', c[6]);
461 CPPUNIT_ASSERT_EQUAL('B', c[9]);
462 c.resize(3, 'C');
463 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), c.size());
464 CPPUNIT_ASSERT_EQUAL(-1, c.find('C'));
465 }
466
467 void testAppend1()
468 {
469 ByteVector v1("foo");
470 v1.append("bar");
471 CPPUNIT_ASSERT_EQUAL(ByteVector("foobar"), v1);
472
473 ByteVector v2("foo");
474 v2.append("b");
475 CPPUNIT_ASSERT_EQUAL(ByteVector("foob"), v2);
476
477 ByteVector v3;
478 v3.append("b");
479 CPPUNIT_ASSERT_EQUAL(ByteVector("b"), v3);
480
481 ByteVector v4("foo");
482 v4.append(v1);
483 CPPUNIT_ASSERT_EQUAL(ByteVector("foofoobar"), v4);
484
485 ByteVector v5("foo");
486 v5.append('b');
487 CPPUNIT_ASSERT_EQUAL(ByteVector("foob"), v5);
488
489 ByteVector v6;
490 v6.append('b');
491 CPPUNIT_ASSERT_EQUAL(ByteVector("b"), v6);
492
493 ByteVector v7("taglib");
494 ByteVector v8 = v7;
495
496 v7.append("ABC");
497 CPPUNIT_ASSERT_EQUAL(ByteVector("taglibABC"), v7);
498 v7.append('1');
499 v7.append('2');
500 v7.append('3');
501 CPPUNIT_ASSERT_EQUAL(ByteVector("taglibABC123"), v7);
502 CPPUNIT_ASSERT_EQUAL(ByteVector("taglib"), v8);
503 }
504
505 void testAppend2()
506 {
507 ByteVector a("1234");
508 a.append(a);
509 CPPUNIT_ASSERT_EQUAL(ByteVector("12341234"), a);
510 }
511
512 void testBase64()
513 {
514 ByteVector sempty;
515 ByteVector t0("a"); // test 1 byte
516 ByteVector t1("any carnal pleasure.");
517 ByteVector t2("any carnal pleasure");
518 ByteVector t3("any carnal pleasur");
519 ByteVector s0("a"); // test 1 byte
520 ByteVector s1("any carnal pleasure.");
521 ByteVector s2("any carnal pleasure");
522 ByteVector s3("any carnal pleasur");
523 ByteVector eempty;
524 ByteVector e0("YQ==");
525 ByteVector e1("YW55IGNhcm5hbCBwbGVhc3VyZS4=");
526 ByteVector e2("YW55IGNhcm5hbCBwbGVhc3VyZQ==");
527 ByteVector e3("YW55IGNhcm5hbCBwbGVhc3Vy");
528
529 // Encode
530 CPPUNIT_ASSERT_EQUAL(eempty, sempty.toBase64());
531 CPPUNIT_ASSERT_EQUAL(e0, s0.toBase64());
532 CPPUNIT_ASSERT_EQUAL(e1, s1.toBase64());
533 CPPUNIT_ASSERT_EQUAL(e2, s2.toBase64());
534 CPPUNIT_ASSERT_EQUAL(e3, s3.toBase64());
535
536 // Decode
537 CPPUNIT_ASSERT_EQUAL(sempty, ByteVector::fromBase64(eempty));
538 CPPUNIT_ASSERT_EQUAL(s0, ByteVector::fromBase64(e0));
539 CPPUNIT_ASSERT_EQUAL(s1, ByteVector::fromBase64(e1));
540 CPPUNIT_ASSERT_EQUAL(s2, ByteVector::fromBase64(e2));
541 CPPUNIT_ASSERT_EQUAL(s3, ByteVector::fromBase64(e3));
542
543 CPPUNIT_ASSERT_EQUAL(t0, ByteVector::fromBase64(s0.toBase64()));
544 CPPUNIT_ASSERT_EQUAL(t1, ByteVector::fromBase64(s1.toBase64()));
545 CPPUNIT_ASSERT_EQUAL(t2, ByteVector::fromBase64(s2.toBase64()));
546 CPPUNIT_ASSERT_EQUAL(t3, ByteVector::fromBase64(s3.toBase64()));
547
548 ByteVector all(static_cast<unsigned int>(256));
549
550 // in order
551 {
552 for(int i = 0; i < 256; i++){
553 all[i]=static_cast<unsigned char>(i);
554 }
555 ByteVector b64 = all.toBase64();
556 ByteVector original = ByteVector::fromBase64(b64);
557 CPPUNIT_ASSERT_EQUAL(all,original);
558 }
559
560 // reverse
561 {
562 for(int i = 0; i < 256; i++){
563 all[i]=static_cast<unsigned char>(255 - i);
564 }
565 ByteVector b64 = all.toBase64();
566 ByteVector original = ByteVector::fromBase64(b64);
567 CPPUNIT_ASSERT_EQUAL(all,original);
568 }
569
570 // all zeroes
571 {
572 for(int i = 0; i < 256; i++){
573 all[i]=0;
574 }
575 ByteVector b64 = all.toBase64();
576 ByteVector original = ByteVector::fromBase64(b64);
577 CPPUNIT_ASSERT_EQUAL(all,original);
578 }
579
580 // all ones
581 {
582 for(int i = 0; i < 256; i++){
583 all[i]=static_cast<unsigned char>(0xff);
584 }
585 ByteVector b64 = all.toBase64();
586 ByteVector original = ByteVector::fromBase64(b64);
587 CPPUNIT_ASSERT_EQUAL(all,original);
588 }
589
590 // Missing end bytes
591 {
592 // No missing bytes
593 ByteVector m0("YW55IGNhcm5hbCBwbGVhc3VyZQ==");
594 CPPUNIT_ASSERT_EQUAL(s2,ByteVector::fromBase64(m0));
595
596 // 1 missing byte
597 ByteVector m1("YW55IGNhcm5hbCBwbGVhc3VyZQ=");
598 CPPUNIT_ASSERT_EQUAL(sempty,ByteVector::fromBase64(m1));
599
600 // 2 missing bytes
601 ByteVector m2("YW55IGNhcm5hbCBwbGVhc3VyZQ");
602 CPPUNIT_ASSERT_EQUAL(sempty,ByteVector::fromBase64(m2));
603
604 // 3 missing bytes
605 ByteVector m3("YW55IGNhcm5hbCBwbGVhc3VyZ");
606 CPPUNIT_ASSERT_EQUAL(sempty,ByteVector::fromBase64(m3));
607 }
608
609 // Grok invalid characters
610 {
611 ByteVector invalid("abd\x00\x01\x02\x03\x04");
612 CPPUNIT_ASSERT_EQUAL(sempty,ByteVector::fromBase64(invalid));
613 }
614
615 }
616
617 void testEmpty()
618 {
619 const ByteVector empty;
620 const ByteVector notEmpty("A");
621 ByteVector mutEmpty;
622
623 CPPUNIT_ASSERT_EQUAL(empty, ByteVector(""));
624 CPPUNIT_ASSERT_EQUAL(empty, ByteVector("", 0));
625 CPPUNIT_ASSERT_EQUAL(empty, ByteVector(0U));
626 CPPUNIT_ASSERT_EQUAL(empty, ByteVector(empty, 0, 0));
627 CPPUNIT_ASSERT_EQUAL(empty, ByteVector(notEmpty, 1, 0));
628 CPPUNIT_ASSERT_EQUAL(empty, ByteVector(static_cast<const char *>(nullptr)));
629 CPPUNIT_ASSERT_EQUAL(empty, ByteVector(static_cast<const char *>(nullptr), 0));
630 CPPUNIT_ASSERT_EQUAL(mutEmpty.setData("", 0), empty);
631 CPPUNIT_ASSERT_EQUAL(mutEmpty.setData(""), empty);
632 CPPUNIT_ASSERT_EQUAL(mutEmpty.setData(nullptr, 0), empty);
633 CPPUNIT_ASSERT_EQUAL(mutEmpty.setData(nullptr), empty);
634 CPPUNIT_ASSERT(!empty.data());
635 CPPUNIT_ASSERT(!mutEmpty.data());
636 CPPUNIT_ASSERT_EQUAL(empty.mid(0), empty);
637 CPPUNIT_ASSERT_EQUAL(empty.at(0), '\0');
638 // Note that the behavior of ByteVector::find() with an empty pattern is
639 // not consistent with String::find() and std::string::find().
640 CPPUNIT_ASSERT_EQUAL(empty.find(mutEmpty), -1);
641 CPPUNIT_ASSERT_EQUAL(empty.find(notEmpty), -1);
642 CPPUNIT_ASSERT_EQUAL(notEmpty.find(empty), -1);
643 CPPUNIT_ASSERT_EQUAL(empty.find('\0'), -1);
644 CPPUNIT_ASSERT_EQUAL(empty.rfind(mutEmpty), -1);
645 CPPUNIT_ASSERT_EQUAL(empty.rfind(notEmpty), -1);
646 CPPUNIT_ASSERT_EQUAL(notEmpty.rfind(empty), -1);
647 CPPUNIT_ASSERT_EQUAL(empty.containsAt(mutEmpty, 0), false);
648 CPPUNIT_ASSERT_EQUAL(empty.startsWith(mutEmpty), false);
649 CPPUNIT_ASSERT_EQUAL(empty.startsWith(notEmpty), false);
650 CPPUNIT_ASSERT_EQUAL(notEmpty.startsWith(empty), false);
651 CPPUNIT_ASSERT_EQUAL(empty.endsWith(mutEmpty), false);
652 CPPUNIT_ASSERT_EQUAL(empty.endsWithPartialMatch(mutEmpty), -1);
653 CPPUNIT_ASSERT_EQUAL(mutEmpty.replace('a', 'b'), empty);
654 CPPUNIT_ASSERT_EQUAL(mutEmpty.replace("abc", ""), empty);
655 CPPUNIT_ASSERT_EQUAL(mutEmpty.append(empty), empty);
656 CPPUNIT_ASSERT_EQUAL(mutEmpty.append(notEmpty), notEmpty);
657 mutEmpty.clear();
658 CPPUNIT_ASSERT_EQUAL(mutEmpty, empty);
659 CPPUNIT_ASSERT_EQUAL(ByteVector(notEmpty).append(empty), notEmpty);
660 CPPUNIT_ASSERT_EQUAL(mutEmpty.append('A'), notEmpty);
661 CPPUNIT_ASSERT_EQUAL(mutEmpty.resize(0), empty);
662 CPPUNIT_ASSERT_EQUAL(empty.size(), 0U);
663 CPPUNIT_ASSERT(empty.begin() == empty.end());
664 CPPUNIT_ASSERT(empty.cbegin() == empty.cend());
665 CPPUNIT_ASSERT(empty.rbegin() == empty.rend());
666 CPPUNIT_ASSERT(mutEmpty.begin() == mutEmpty.end());
667 CPPUNIT_ASSERT(mutEmpty.rbegin() == mutEmpty.rend());
668 CPPUNIT_ASSERT(empty.isEmpty());
669 CPPUNIT_ASSERT_EQUAL(empty.toUInt(), 0U);
670 CPPUNIT_ASSERT_EQUAL(empty.toUInt(0, true), 0U);
671 CPPUNIT_ASSERT_EQUAL(empty.toUInt(0, 0, true), 0U);
672 CPPUNIT_ASSERT_EQUAL(empty.toShort(), static_cast<short>(0));
673 CPPUNIT_ASSERT_EQUAL(empty.toShort(0, true), static_cast<short>(0));
674 CPPUNIT_ASSERT_EQUAL(empty.toUShort(), static_cast<unsigned short>(0));
675 CPPUNIT_ASSERT_EQUAL(empty.toUShort(0, true), static_cast<unsigned short>(0));
676 CPPUNIT_ASSERT_EQUAL(empty.toLongLong(), 0LL);
677 CPPUNIT_ASSERT_EQUAL(empty.toLongLong(0, true), 0LL);
678 CPPUNIT_ASSERT_EQUAL(empty.toULongLong(), 0ULL);
679 CPPUNIT_ASSERT_EQUAL(empty.toULongLong(0, true), 0ULL);
680 CPPUNIT_ASSERT_EQUAL(empty.toFloat32LE(0), 0.f);
681 CPPUNIT_ASSERT_EQUAL(empty.toFloat32BE(0), 0.f);
682 CPPUNIT_ASSERT_EQUAL(empty.toFloat64LE(0), 0.);
683 CPPUNIT_ASSERT_EQUAL(empty.toFloat64BE(0), 0.);
684 CPPUNIT_ASSERT_EQUAL(empty.toFloat80LE(0), 0.l);
685 CPPUNIT_ASSERT_EQUAL(empty.toFloat80BE(0), 0.l);
686 CPPUNIT_ASSERT(empty == mutEmpty);
687 CPPUNIT_ASSERT(empty != notEmpty);
688 CPPUNIT_ASSERT(empty == "");
689 CPPUNIT_ASSERT(empty != " ");
690 CPPUNIT_ASSERT(empty == static_cast<const char *>(nullptr));
691 CPPUNIT_ASSERT(!(empty != static_cast<const char *>(nullptr)));
692 CPPUNIT_ASSERT(empty < notEmpty);
693 CPPUNIT_ASSERT(!(empty > notEmpty));
694 CPPUNIT_ASSERT_EQUAL(empty + mutEmpty, empty);
695 CPPUNIT_ASSERT_EQUAL(empty + notEmpty, notEmpty);
696 CPPUNIT_ASSERT_EQUAL(mutEmpty = static_cast<const char *>(nullptr), empty);
697 CPPUNIT_ASSERT_EQUAL(mutEmpty = notEmpty, notEmpty);
698 ByteVector tmp;
699 mutEmpty.swap(tmp);
700 CPPUNIT_ASSERT_EQUAL(mutEmpty, empty);
701 CPPUNIT_ASSERT_EQUAL(empty.toHex(), empty);
702 CPPUNIT_ASSERT_EQUAL(empty.toBase64(), empty);
703 }
704
705 };
706
707 CPPUNIT_TEST_SUITE_REGISTRATION(TestByteVector);