Line data Source code
1 : /*
2 : Zipios -- a small C++ library that provides easy access to .zip files.
3 :
4 : Copyright (C) 2000-2007 Thomas Sondergaard
5 : Copyright (C) 2015-2019 Made to Order Software Corporation
6 :
7 : This library is free software; you can redistribute it and/or
8 : modify it under the terms of the GNU Lesser General Public
9 : License as published by the Free Software Foundation; either
10 : version 2.1 of the License, or (at your option) any later version.
11 :
12 : This library is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : Lesser General Public License for more details.
16 :
17 : You should have received a copy of the GNU Lesser General Public
18 : License along with this library; if not, write to the Free Software
19 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 : */
21 :
22 : /** \file
23 : *
24 : * Zipios unit tests used to verify the various funtions defined in
25 : * zipios_common.hpp.
26 : */
27 :
28 : #include "tests.hpp"
29 :
30 : #include "src/zipios_common.hpp"
31 : #include "zipios/zipiosexceptions.hpp"
32 :
33 : #include <fstream>
34 :
35 : #include <unistd.h>
36 :
37 :
38 5 : SCENARIO("Vector append", "[zipios_common]")
39 : {
40 8 : GIVEN("an empty vector")
41 : {
42 4 : std::vector<std::string> es;
43 :
44 4 : WHEN("appending another empty vector")
45 : {
46 2 : std::vector<std::string> os;
47 :
48 1 : es += os;
49 :
50 2 : THEN("the result is still an emtpy vector")
51 : {
52 1 : REQUIRE(es.empty());
53 : }
54 : }
55 :
56 4 : WHEN("appending a non-empty vector")
57 : {
58 2 : std::vector<std::string> os{ "a", "b", "c" };
59 :
60 1 : es += os;
61 :
62 2 : THEN("the result is like that non-empty vector")
63 : {
64 1 : REQUIRE(es.size() == 3);
65 :
66 1 : REQUIRE(es[0] == "a");
67 1 : REQUIRE(es[1] == "b");
68 1 : REQUIRE(es[2] == "c");
69 : }
70 : }
71 : }
72 :
73 8 : GIVEN("a non-empty vector")
74 : {
75 4 : std::vector<std::string> es{ "x", "y", "z" };
76 :
77 4 : WHEN("appending an empty vector")
78 : {
79 2 : std::vector<std::string> os;
80 :
81 1 : es += os;
82 :
83 2 : THEN("the result is still the 3 element vector")
84 : {
85 1 : REQUIRE(es.size() == 3);
86 :
87 1 : REQUIRE(es[0] == "x");
88 1 : REQUIRE(es[1] == "y");
89 1 : REQUIRE(es[2] == "z");
90 : }
91 : }
92 :
93 4 : WHEN("appending a non-empty vector")
94 : {
95 2 : std::vector<std::string> os{ "a", "b", "c" };
96 :
97 1 : es += os;
98 :
99 2 : THEN("the result is the original vector with the other vector appended")
100 : {
101 1 : REQUIRE(es.size() == 6);
102 :
103 1 : REQUIRE(es[0] == "x");
104 1 : REQUIRE(es[1] == "y");
105 1 : REQUIRE(es[2] == "z");
106 1 : REQUIRE(es[3] == "a");
107 1 : REQUIRE(es[4] == "b");
108 1 : REQUIRE(es[5] == "c");
109 : }
110 : }
111 : }
112 4 : }
113 :
114 :
115 2 : TEST_CASE("Verify the g_separator", "[zipios_common]")
116 : {
117 : // Not too sure why we have that as a variable since it is always
118 : // a slash (/) and never a backslash (\) but it is there...
119 1 : REQUIRE(zipios::g_separator == '/');
120 1 : }
121 :
122 :
123 13 : SCENARIO("Read from file", "[zipios_common] [io]")
124 : {
125 24 : GIVEN("a simple file")
126 : {
127 : // create a file
128 : {
129 24 : std::ofstream os("io.bin", std::ios::out | std::ios::binary);
130 204 : for(int i(0); i < 16; ++i)
131 : {
132 192 : os << static_cast<char>(i);
133 : }
134 : }
135 :
136 : // now open it for reading
137 24 : std::ifstream is("io.bin", std::ios::in | std::ios::binary);
138 :
139 24 : WHEN("reading two 32 bit values")
140 : {
141 : uint32_t a, b;
142 1 : zipios::zipRead(is, a);
143 1 : zipios::zipRead(is, b);
144 :
145 2 : THEN("we get exactly the value we expected")
146 : {
147 1 : REQUIRE(a == 0x03020100);
148 1 : REQUIRE(b == 0x07060504);
149 : }
150 : }
151 :
152 24 : WHEN("reading one 32 bit between two 16 bit values")
153 : {
154 : uint32_t b;
155 : uint16_t a, c;
156 1 : zipios::zipRead(is, a);
157 1 : zipios::zipRead(is, b);
158 1 : zipios::zipRead(is, c);
159 :
160 2 : THEN("the result is exactly as expected")
161 : {
162 1 : REQUIRE(a == 0x0100);
163 1 : REQUIRE(b == 0x05040302);
164 1 : REQUIRE(c == 0x0706);
165 : }
166 : }
167 :
168 24 : WHEN("reading one 16 bit between two 32 bit values")
169 : {
170 : uint32_t a, c;
171 : uint16_t b;
172 1 : zipios::zipRead(is, a);
173 1 : zipios::zipRead(is, b);
174 1 : zipios::zipRead(is, c);
175 :
176 2 : THEN("the result is as expected")
177 : {
178 1 : REQUIRE(a == 0x03020100);
179 1 : REQUIRE(b == 0x0504);
180 1 : REQUIRE(c == 0x09080706);
181 : }
182 : }
183 :
184 24 : WHEN("reading three 16 bit values")
185 : {
186 : uint16_t a, b, c;
187 1 : zipios::zipRead(is, a);
188 1 : zipios::zipRead(is, b);
189 1 : zipios::zipRead(is, c);
190 :
191 2 : THEN("the result is as expected")
192 : {
193 1 : REQUIRE(a == 0x0100);
194 1 : REQUIRE(b == 0x0302);
195 1 : REQUIRE(c == 0x0504);
196 : }
197 : }
198 :
199 24 : WHEN("reading one 32 bit, one 8 bit then one 16 bit value")
200 : {
201 : uint32_t a;
202 : uint8_t b;
203 : uint16_t c;
204 1 : zipios::zipRead(is, a);
205 1 : zipios::zipRead(is, b);
206 1 : zipios::zipRead(is, c);
207 :
208 2 : THEN("the result is as expected")
209 : {
210 1 : REQUIRE(a == 0x03020100);
211 1 : REQUIRE(b == 0x04);
212 1 : REQUIRE(c == 0x0605);
213 : }
214 : }
215 :
216 24 : WHEN("reading one 32 bit, one 8 bit then one buffer value")
217 : {
218 : uint32_t a;
219 : uint8_t b;
220 2 : zipios::buffer_t c;
221 1 : zipios::zipRead(is, a);
222 1 : zipios::zipRead(is, b);
223 1 : zipios::zipRead(is, c, 8);
224 :
225 2 : THEN("the result is as expected")
226 : {
227 1 : REQUIRE(a == 0x03020100);
228 1 : REQUIRE(b == 0x04);
229 1 : REQUIRE(c.size() == 8);
230 1 : REQUIRE(c[0] == 0x05);
231 1 : REQUIRE(c[1] == 0x06);
232 1 : REQUIRE(c[2] == 0x07);
233 1 : REQUIRE(c[3] == 0x08);
234 1 : REQUIRE(c[4] == 0x09);
235 1 : REQUIRE(c[5] == 0x0A);
236 1 : REQUIRE(c[6] == 0x0B);
237 1 : REQUIRE(c[7] == 0x0C);
238 : }
239 : }
240 :
241 24 : WHEN("reading one 32 bit, one string and then one 8 bit value")
242 : {
243 : uint32_t a;
244 2 : std::string b;
245 : uint8_t c;
246 1 : zipios::zipRead(is, a);
247 1 : zipios::zipRead(is, b, 8);
248 1 : zipios::zipRead(is, c);
249 :
250 2 : THEN("the result is as expected")
251 : {
252 1 : REQUIRE(a == 0x03020100);
253 1 : REQUIRE(b.size() == 8);
254 1 : REQUIRE(b[0] == 0x04);
255 1 : REQUIRE(b[1] == 0x05);
256 1 : REQUIRE(b[2] == 0x06);
257 1 : REQUIRE(b[3] == 0x07);
258 1 : REQUIRE(b[4] == 0x08);
259 1 : REQUIRE(b[5] == 0x09);
260 1 : REQUIRE(b[6] == 0x0A);
261 1 : REQUIRE(b[7] == 0x0B);
262 1 : REQUIRE(c == 0x0C);
263 : }
264 : }
265 :
266 24 : WHEN("reading four 32 bit values")
267 : {
268 : uint32_t a, b, c, d;
269 1 : zipios::zipRead(is, a);
270 1 : zipios::zipRead(is, b);
271 1 : zipios::zipRead(is, c);
272 1 : zipios::zipRead(is, d);
273 :
274 2 : THEN("another 8 bit value")
275 : {
276 1 : REQUIRE(a == 0x03020100);
277 1 : REQUIRE(b == 0x07060504);
278 1 : REQUIRE(c == 0x0B0A0908);
279 1 : REQUIRE(d == 0x0F0E0D0C);
280 :
281 : uint8_t e;
282 1 : REQUIRE_THROWS_AS(zipios::zipRead(is, e), zipios::IOException);
283 : }
284 : }
285 :
286 24 : WHEN("reading four 32 bit values")
287 : {
288 : uint32_t a, b, c, d;
289 1 : zipios::zipRead(is, a);
290 1 : zipios::zipRead(is, b);
291 1 : zipios::zipRead(is, c);
292 1 : zipios::zipRead(is, d);
293 :
294 2 : THEN("another 16 bit value")
295 : {
296 1 : REQUIRE(a == 0x03020100);
297 1 : REQUIRE(b == 0x07060504);
298 1 : REQUIRE(c == 0x0B0A0908);
299 1 : REQUIRE(d == 0x0F0E0D0C);
300 :
301 : uint16_t e;
302 1 : REQUIRE_THROWS_AS(zipios::zipRead(is, e), zipios::IOException);
303 : }
304 : }
305 :
306 24 : WHEN("reading four 32 bit values")
307 : {
308 : uint32_t a, b, c, d;
309 1 : zipios::zipRead(is, a);
310 1 : zipios::zipRead(is, b);
311 1 : zipios::zipRead(is, c);
312 1 : zipios::zipRead(is, d);
313 :
314 2 : THEN("another 32 bit value")
315 : {
316 1 : REQUIRE(a == 0x03020100);
317 1 : REQUIRE(b == 0x07060504);
318 1 : REQUIRE(c == 0x0B0A0908);
319 1 : REQUIRE(d == 0x0F0E0D0C);
320 :
321 : uint32_t e;
322 1 : REQUIRE_THROWS_AS(zipios::zipRead(is, e), zipios::IOException);
323 : }
324 : }
325 :
326 24 : WHEN("reading two 32 bit values")
327 : {
328 : uint32_t a, b;
329 1 : zipios::zipRead(is, a);
330 1 : zipios::zipRead(is, b);
331 :
332 2 : THEN("then a string that's too long")
333 : {
334 1 : REQUIRE(a == 0x03020100);
335 1 : REQUIRE(b == 0x07060504);
336 :
337 : // we have 8 bytes left, trying to read 12 fails
338 2 : std::string e;
339 1 : REQUIRE_THROWS_AS(zipios::zipRead(is, e, 12), zipios::IOException);
340 : }
341 : }
342 :
343 24 : WHEN("reading two 32 bit values")
344 : {
345 : uint32_t a, b;
346 1 : zipios::zipRead(is, a);
347 1 : zipios::zipRead(is, b);
348 :
349 2 : THEN("then a buffer that's too long")
350 : {
351 1 : REQUIRE(a == 0x03020100);
352 1 : REQUIRE(b == 0x07060504);
353 :
354 : // we have 8 bytes left, trying to read 12 fails
355 2 : zipios::buffer_t e;
356 1 : REQUIRE_THROWS_AS(zipios::zipRead(is, e, 12), zipios::IOException);
357 : }
358 : }
359 :
360 12 : unlink("io.bin");
361 : }
362 12 : }
363 :
364 :
365 13 : SCENARIO("Read from buffer", "[zipios_common] [io]")
366 : {
367 24 : GIVEN("a simple buffer")
368 : {
369 24 : zipios::buffer_t is;
370 204 : for(int i(0); i < 16; ++i)
371 : {
372 192 : is.push_back(static_cast<char>(i));
373 : }
374 :
375 24 : WHEN("reading two 32 bit values")
376 : {
377 : uint32_t a, b;
378 1 : size_t pos(0);
379 1 : zipios::zipRead(is, pos, a);
380 1 : REQUIRE(pos == 4);
381 1 : zipios::zipRead(is, pos, b);
382 1 : REQUIRE(pos == 8);
383 :
384 2 : THEN("we get exactly the value we expected")
385 : {
386 1 : REQUIRE(a == 0x03020100);
387 1 : REQUIRE(b == 0x07060504);
388 : }
389 : }
390 :
391 24 : WHEN("reading one 32 bit between two 16 bit values")
392 : {
393 : uint32_t b;
394 : uint16_t a, c;
395 1 : size_t pos(0);
396 1 : zipios::zipRead(is, pos, a);
397 1 : REQUIRE(pos == 2);
398 1 : zipios::zipRead(is, pos, b);
399 1 : REQUIRE(pos == 6);
400 1 : zipios::zipRead(is, pos, c);
401 1 : REQUIRE(pos == 8);
402 :
403 2 : THEN("the result is exactly as expected")
404 : {
405 1 : REQUIRE(a == 0x0100);
406 1 : REQUIRE(b == 0x05040302);
407 1 : REQUIRE(c == 0x0706);
408 : }
409 : }
410 :
411 24 : WHEN("reading one 16 bit between two 32 bit values")
412 : {
413 : uint32_t a, c;
414 : uint16_t b;
415 1 : size_t pos(0);
416 1 : zipios::zipRead(is, pos, a);
417 1 : REQUIRE(pos == 4);
418 1 : zipios::zipRead(is, pos, b);
419 1 : REQUIRE(pos == 6);
420 1 : zipios::zipRead(is, pos, c);
421 1 : REQUIRE(pos == 10);
422 :
423 2 : THEN("the result is as expected")
424 : {
425 1 : REQUIRE(a == 0x03020100);
426 1 : REQUIRE(b == 0x0504);
427 1 : REQUIRE(c == 0x09080706);
428 : }
429 : }
430 :
431 24 : WHEN("reading three 16 bit values")
432 : {
433 : uint16_t a, b, c;
434 1 : size_t pos(0);
435 1 : zipios::zipRead(is, pos, a);
436 1 : REQUIRE(pos == 2);
437 1 : zipios::zipRead(is, pos, b);
438 1 : REQUIRE(pos == 4);
439 1 : zipios::zipRead(is, pos, c);
440 1 : REQUIRE(pos == 6);
441 :
442 2 : THEN("the result is as expected")
443 : {
444 1 : REQUIRE(a == 0x0100);
445 1 : REQUIRE(b == 0x0302);
446 1 : REQUIRE(c == 0x0504);
447 : }
448 : }
449 :
450 24 : WHEN("reading one 32 bit, one 8 bit then one 16 bit value")
451 : {
452 : uint32_t a;
453 : uint8_t b;
454 : uint16_t c;
455 1 : size_t pos(0);
456 1 : zipios::zipRead(is, pos, a);
457 1 : REQUIRE(pos == 4);
458 1 : zipios::zipRead(is, pos, b);
459 1 : REQUIRE(pos == 5);
460 1 : zipios::zipRead(is, pos, c);
461 1 : REQUIRE(pos == 7);
462 :
463 2 : THEN("the result is as expected")
464 : {
465 1 : REQUIRE(a == 0x03020100);
466 1 : REQUIRE(b == 0x04);
467 1 : REQUIRE(c == 0x0605);
468 : }
469 : }
470 :
471 24 : WHEN("reading one 32 bit, one 8 bit then one buffer value")
472 : {
473 : uint32_t a;
474 : uint8_t b;
475 2 : zipios::buffer_t c;
476 1 : size_t pos(0);
477 1 : zipios::zipRead(is, pos, a);
478 1 : REQUIRE(pos == 4);
479 1 : zipios::zipRead(is, pos, b);
480 1 : REQUIRE(pos == 5);
481 1 : zipios::zipRead(is, pos, c, 8);
482 1 : REQUIRE(pos == 13);
483 :
484 2 : THEN("the result is as expected")
485 : {
486 1 : REQUIRE(a == 0x03020100);
487 1 : REQUIRE(b == 0x04);
488 1 : REQUIRE(c.size() == 8);
489 1 : REQUIRE(c[0] == 0x05);
490 1 : REQUIRE(c[1] == 0x06);
491 1 : REQUIRE(c[2] == 0x07);
492 1 : REQUIRE(c[3] == 0x08);
493 1 : REQUIRE(c[4] == 0x09);
494 1 : REQUIRE(c[5] == 0x0A);
495 1 : REQUIRE(c[6] == 0x0B);
496 1 : REQUIRE(c[7] == 0x0C);
497 : }
498 : }
499 :
500 24 : WHEN("reading one 32 bit, one string and then one 8 bit value")
501 : {
502 : uint32_t a;
503 2 : std::string b;
504 : uint8_t c;
505 1 : size_t pos(0);
506 1 : zipios::zipRead(is, pos, a);
507 1 : REQUIRE(pos == 4);
508 1 : zipios::zipRead(is, pos, b, 8);
509 1 : REQUIRE(pos == 12);
510 1 : zipios::zipRead(is, pos, c);
511 1 : REQUIRE(pos == 13);
512 :
513 2 : THEN("the result is as expected")
514 : {
515 1 : REQUIRE(a == 0x03020100);
516 1 : REQUIRE(b.size() == 8);
517 1 : REQUIRE(b[0] == 0x04);
518 1 : REQUIRE(b[1] == 0x05);
519 1 : REQUIRE(b[2] == 0x06);
520 1 : REQUIRE(b[3] == 0x07);
521 1 : REQUIRE(b[4] == 0x08);
522 1 : REQUIRE(b[5] == 0x09);
523 1 : REQUIRE(b[6] == 0x0A);
524 1 : REQUIRE(b[7] == 0x0B);
525 1 : REQUIRE(c == 0x0C);
526 : }
527 : }
528 :
529 24 : WHEN("reading four 32 bit values")
530 : {
531 : uint32_t a, b, c, d;
532 1 : size_t pos(0);
533 1 : zipios::zipRead(is, pos, a);
534 1 : REQUIRE(pos == 4);
535 1 : zipios::zipRead(is, pos, b);
536 1 : REQUIRE(pos == 8);
537 1 : zipios::zipRead(is, pos, c);
538 1 : REQUIRE(pos == 12);
539 1 : zipios::zipRead(is, pos, d);
540 1 : REQUIRE(pos == 16);
541 :
542 2 : THEN("another 8 bit value")
543 : {
544 1 : REQUIRE(a == 0x03020100);
545 1 : REQUIRE(b == 0x07060504);
546 1 : REQUIRE(c == 0x0B0A0908);
547 1 : REQUIRE(d == 0x0F0E0D0C);
548 :
549 : uint8_t e;
550 1 : REQUIRE_THROWS_AS(zipios::zipRead(is, pos, e), zipios::IOException);
551 : }
552 : }
553 :
554 24 : WHEN("reading four 32 bit values")
555 : {
556 : uint32_t a, b, c, d;
557 1 : size_t pos(0);
558 1 : zipios::zipRead(is, pos, a);
559 1 : REQUIRE(pos == 4);
560 1 : zipios::zipRead(is, pos, b);
561 1 : REQUIRE(pos == 8);
562 1 : zipios::zipRead(is, pos, c);
563 1 : REQUIRE(pos == 12);
564 1 : zipios::zipRead(is, pos, d);
565 1 : REQUIRE(pos == 16);
566 :
567 2 : THEN("another 16 bit value")
568 : {
569 1 : REQUIRE(a == 0x03020100);
570 1 : REQUIRE(b == 0x07060504);
571 1 : REQUIRE(c == 0x0B0A0908);
572 1 : REQUIRE(d == 0x0F0E0D0C);
573 :
574 : uint16_t e;
575 1 : REQUIRE_THROWS_AS(zipios::zipRead(is, pos, e), zipios::IOException);
576 : }
577 : }
578 :
579 24 : WHEN("reading four 32 bit values")
580 : {
581 : uint32_t a, b, c, d;
582 1 : size_t pos(0);
583 1 : zipios::zipRead(is, pos, a);
584 1 : REQUIRE(pos == 4);
585 1 : zipios::zipRead(is, pos, b);
586 1 : REQUIRE(pos == 8);
587 1 : zipios::zipRead(is, pos, c);
588 1 : REQUIRE(pos == 12);
589 1 : zipios::zipRead(is, pos, d);
590 1 : REQUIRE(pos == 16);
591 :
592 2 : THEN("another 32 bit value")
593 : {
594 1 : REQUIRE(a == 0x03020100);
595 1 : REQUIRE(b == 0x07060504);
596 1 : REQUIRE(c == 0x0B0A0908);
597 1 : REQUIRE(d == 0x0F0E0D0C);
598 :
599 : uint32_t e;
600 1 : REQUIRE_THROWS_AS(zipios::zipRead(is, pos, e), zipios::IOException);
601 : }
602 : }
603 :
604 24 : WHEN("reading two 32 bit values")
605 : {
606 : uint32_t a, b;
607 1 : size_t pos(0);
608 1 : zipios::zipRead(is, pos, a);
609 1 : REQUIRE(pos == 4);
610 1 : zipios::zipRead(is, pos, b);
611 1 : REQUIRE(pos == 8);
612 :
613 2 : THEN("then a string that's too long")
614 : {
615 1 : REQUIRE(a == 0x03020100);
616 1 : REQUIRE(b == 0x07060504);
617 :
618 : // we have 8 bytes left, trying to read 12 fails
619 2 : std::string e;
620 1 : REQUIRE_THROWS_AS(zipios::zipRead(is, pos, e, 12), zipios::IOException);
621 : }
622 : }
623 :
624 24 : WHEN("reading two 32 bit values")
625 : {
626 : uint32_t a, b;
627 1 : size_t pos(0);
628 1 : zipios::zipRead(is, pos, a);
629 1 : REQUIRE(pos == 4);
630 1 : zipios::zipRead(is, pos, b);
631 1 : REQUIRE(pos == 8);
632 :
633 2 : THEN("then a buffer that's too long")
634 : {
635 1 : REQUIRE(a == 0x03020100);
636 1 : REQUIRE(b == 0x07060504);
637 :
638 : // we have 8 bytes left, trying to read 12 fails
639 2 : zipios::buffer_t e;
640 1 : REQUIRE_THROWS_AS(zipios::zipRead(is, pos, e, 12), zipios::IOException);
641 : }
642 : }
643 : }
644 12 : }
645 :
646 :
647 13 : SCENARIO("Write to file", "[zipios_common] [io]")
648 : {
649 24 : GIVEN("create an empty file")
650 : {
651 :
652 24 : WHEN("writing two 32 bit values")
653 : {
654 1 : uint32_t a(0x03020100), b(0x07060504);
655 : {
656 2 : std::ofstream os("io.bin", std::ios::out | std::ios::binary);
657 1 : zipios::zipWrite(os, a);
658 1 : zipios::zipWrite(os, b);
659 : }
660 :
661 2 : THEN("we get exactly the value we expected")
662 : {
663 2 : std::ifstream is("io.bin", std::ios::in | std::ios::binary);
664 1 : is.seekg(0, std::ios::end);
665 1 : REQUIRE(is.tellg() == 8);
666 1 : is.seekg(0, std::ios::beg);
667 :
668 : char buf[8];
669 1 : is.read(buf, 8);
670 :
671 1 : REQUIRE(buf[0] == 0x00);
672 1 : REQUIRE(buf[1] == 0x01);
673 1 : REQUIRE(buf[2] == 0x02);
674 1 : REQUIRE(buf[3] == 0x03);
675 1 : REQUIRE(buf[4] == 0x04);
676 1 : REQUIRE(buf[5] == 0x05);
677 1 : REQUIRE(buf[6] == 0x06);
678 1 : REQUIRE(buf[7] == 0x07);
679 : }
680 : }
681 :
682 24 : WHEN("writing one 32 bit between two 16 bit values")
683 : {
684 1 : uint32_t b(0x55112288);
685 1 : uint16_t a(0x3344), c(0x6677);
686 : {
687 2 : std::ofstream os("io.bin", std::ios::out | std::ios::binary);
688 1 : zipios::zipWrite(os, a);
689 1 : zipios::zipWrite(os, b);
690 1 : zipios::zipWrite(os, c);
691 : }
692 :
693 2 : THEN("the result is exactly as expected")
694 : {
695 2 : std::ifstream is("io.bin", std::ios::in | std::ios::binary);
696 1 : is.seekg(0, std::ios::end);
697 1 : REQUIRE(is.tellg() == 8);
698 1 : is.seekg(0, std::ios::beg);
699 :
700 : char buf[8];
701 1 : is.read(buf, 8);
702 :
703 1 : REQUIRE(buf[0] == 0x44);
704 1 : REQUIRE(buf[1] == 0x33);
705 1 : REQUIRE(buf[2] == static_cast<char>(0x88));
706 1 : REQUIRE(buf[3] == 0x22);
707 1 : REQUIRE(buf[4] == 0x11);
708 1 : REQUIRE(buf[5] == 0x55);
709 1 : REQUIRE(buf[6] == 0x77);
710 1 : REQUIRE(buf[7] == 0x66);
711 : }
712 : }
713 :
714 24 : WHEN("writing one 16 bit between two 32 bit values")
715 : {
716 1 : uint32_t a(0x01050803), c(0x10508030);
717 1 : uint16_t b(0xFF00);
718 : {
719 2 : std::ofstream os("io.bin", std::ios::out | std::ios::binary);
720 1 : zipios::zipWrite(os, a);
721 1 : zipios::zipWrite(os, b);
722 1 : zipios::zipWrite(os, c);
723 : }
724 :
725 2 : THEN("the result is as expected")
726 : {
727 2 : std::ifstream is("io.bin", std::ios::in | std::ios::binary);
728 1 : is.seekg(0, std::ios::end);
729 1 : REQUIRE(is.tellg() == 10);
730 1 : is.seekg(0, std::ios::beg);
731 :
732 : char buf[10];
733 1 : is.read(buf, 10);
734 :
735 1 : REQUIRE(buf[0] == 0x03);
736 1 : REQUIRE(buf[1] == 0x08);
737 1 : REQUIRE(buf[2] == 0x05);
738 1 : REQUIRE(buf[3] == 0x01);
739 1 : REQUIRE(buf[4] == 0x00);
740 1 : REQUIRE(buf[5] == static_cast<char>(0xFF));
741 1 : REQUIRE(buf[6] == 0x30);
742 1 : REQUIRE(buf[7] == static_cast<char>(0x80));
743 1 : REQUIRE(buf[8] == 0x50);
744 1 : REQUIRE(buf[9] == 0x10);
745 : }
746 : }
747 :
748 24 : WHEN("writing three 16 bit values")
749 : {
750 1 : uint16_t a(0xEECC), b(0xAADD), c(0xFFBB);
751 : {
752 2 : std::ofstream os("io.bin", std::ios::out | std::ios::binary);
753 1 : zipios::zipWrite(os, a);
754 1 : zipios::zipWrite(os, b);
755 1 : zipios::zipWrite(os, c);
756 : }
757 :
758 2 : THEN("the result is as expected")
759 : {
760 2 : std::ifstream is("io.bin", std::ios::in | std::ios::binary);
761 1 : is.seekg(0, std::ios::end);
762 1 : REQUIRE(is.tellg() == 6);
763 1 : is.seekg(0, std::ios::beg);
764 :
765 : char buf[6];
766 1 : is.read(buf, 6);
767 :
768 1 : REQUIRE(buf[0] == static_cast<char>(0xCC));
769 1 : REQUIRE(buf[1] == static_cast<char>(0xEE));
770 1 : REQUIRE(buf[2] == static_cast<char>(0xDD));
771 1 : REQUIRE(buf[3] == static_cast<char>(0xAA));
772 1 : REQUIRE(buf[4] == static_cast<char>(0xBB));
773 1 : REQUIRE(buf[5] == static_cast<char>(0xFF));
774 : }
775 : }
776 :
777 24 : WHEN("writing one 32 bit, one 8 bit then one 16 bit value")
778 : {
779 1 : uint32_t a(0x11223344);
780 1 : uint8_t b(0xAA);
781 1 : uint16_t c(0x9988);
782 : {
783 2 : std::ofstream os("io.bin", std::ios::out | std::ios::binary);
784 1 : zipios::zipWrite(os, a);
785 1 : zipios::zipWrite(os, b);
786 1 : zipios::zipWrite(os, c);
787 : }
788 :
789 2 : THEN("the result is as expected")
790 : {
791 2 : std::ifstream is("io.bin", std::ios::in | std::ios::binary);
792 1 : is.seekg(0, std::ios::end);
793 1 : REQUIRE(is.tellg() == 7);
794 1 : is.seekg(0, std::ios::beg);
795 :
796 : char buf[7];
797 1 : is.read(buf, 7);
798 :
799 1 : REQUIRE(buf[0] == 0x44);
800 1 : REQUIRE(buf[1] == 0x33);
801 1 : REQUIRE(buf[2] == 0x22);
802 1 : REQUIRE(buf[3] == 0x11);
803 1 : REQUIRE(buf[4] == static_cast<char>(0xAA));
804 1 : REQUIRE(buf[5] == static_cast<char>(0x88));
805 1 : REQUIRE(buf[6] == static_cast<char>(0x99));
806 : }
807 : }
808 :
809 24 : WHEN("writing one 32 bit, one 8 bit then one buffer value")
810 : {
811 1 : uint32_t a(0x01020304);
812 1 : uint8_t b(0xFF);
813 2 : zipios::buffer_t c;
814 1 : c.push_back(0xA0);
815 1 : c.push_back(0xA1);
816 1 : c.push_back(0xA2);
817 1 : c.push_back(0xA3);
818 1 : c.push_back(0xA4);
819 1 : c.push_back(0xA5);
820 1 : c.push_back(0xA6);
821 1 : c.push_back(0xA7);
822 : {
823 2 : std::ofstream os("io.bin", std::ios::out | std::ios::binary);
824 1 : zipios::zipWrite(os, a);
825 1 : zipios::zipWrite(os, b);
826 1 : zipios::zipWrite(os, c);
827 : }
828 :
829 2 : THEN("the result is as expected")
830 : {
831 2 : std::ifstream is("io.bin", std::ios::in | std::ios::binary);
832 1 : is.seekg(0, std::ios::end);
833 1 : REQUIRE(is.tellg() == 13);
834 1 : is.seekg(0, std::ios::beg);
835 :
836 : char buf[13];
837 1 : is.read(buf, 13);
838 :
839 1 : REQUIRE(buf[ 0] == 0x04);
840 1 : REQUIRE(buf[ 1] == 0x03);
841 1 : REQUIRE(buf[ 2] == 0x02);
842 1 : REQUIRE(buf[ 3] == 0x01);
843 1 : REQUIRE(buf[ 4] == static_cast<char>(0xFF));
844 1 : REQUIRE(buf[ 5] == static_cast<char>(0xA0));
845 1 : REQUIRE(buf[ 6] == static_cast<char>(0xA1));
846 1 : REQUIRE(buf[ 7] == static_cast<char>(0xA2));
847 1 : REQUIRE(buf[ 8] == static_cast<char>(0xA3));
848 1 : REQUIRE(buf[ 9] == static_cast<char>(0xA4));
849 1 : REQUIRE(buf[10] == static_cast<char>(0xA5));
850 1 : REQUIRE(buf[11] == static_cast<char>(0xA6));
851 1 : REQUIRE(buf[12] == static_cast<char>(0xA7));
852 : }
853 : }
854 :
855 24 : WHEN("writing one 32 bit, one string and then one 8 bit value")
856 : {
857 1 : uint32_t a(0x03050709);
858 2 : std::string b("TEST");
859 1 : uint8_t c(0x01);
860 : {
861 2 : std::ofstream os("io.bin", std::ios::out | std::ios::binary);
862 1 : zipios::zipWrite(os, a);
863 1 : zipios::zipWrite(os, b);
864 1 : zipios::zipWrite(os, c);
865 : }
866 :
867 2 : THEN("the result is as expected")
868 : {
869 2 : std::ifstream is("io.bin", std::ios::in | std::ios::binary);
870 1 : is.seekg(0, std::ios::end);
871 1 : REQUIRE(is.tellg() == 9);
872 1 : is.seekg(0, std::ios::beg);
873 :
874 : char buf[9];
875 1 : is.read(buf, 9);
876 :
877 1 : REQUIRE(buf[0] == 0x09);
878 1 : REQUIRE(buf[1] == 0x07);
879 1 : REQUIRE(buf[2] == 0x05);
880 1 : REQUIRE(buf[3] == 0x03);
881 1 : REQUIRE(buf[4] == 'T');
882 1 : REQUIRE(buf[5] == 'E');
883 1 : REQUIRE(buf[6] == 'S');
884 1 : REQUIRE(buf[7] == 'T');
885 1 : REQUIRE(buf[8] == 0x01);
886 : }
887 : }
888 :
889 24 : WHEN("writing some data and mark the output as invalid")
890 : {
891 1 : uint32_t a(0x03050709);
892 2 : std::string b("TEST");
893 2 : std::ofstream os("io.bin", std::ios::out | std::ios::binary);
894 1 : zipios::zipWrite(os, a);
895 1 : zipios::zipWrite(os, b);
896 1 : os.setstate(std::ios::failbit);
897 :
898 2 : THEN("writing a 8 bit value fails")
899 : {
900 1 : uint8_t c(0xFF);
901 1 : REQUIRE_THROWS_AS(zipios::zipWrite(os, c), zipios::IOException);
902 : }
903 : }
904 :
905 24 : WHEN("writing some data and mark the output as invalid")
906 : {
907 1 : uint32_t a(0x03050709);
908 2 : std::string b("TEST");
909 2 : std::ofstream os("io.bin", std::ios::out | std::ios::binary);
910 1 : zipios::zipWrite(os, a);
911 1 : zipios::zipWrite(os, b);
912 1 : os.setstate(std::ios::failbit);
913 :
914 2 : THEN("writing a 16 bit value fails")
915 : {
916 1 : uint16_t c(0xFFEE);
917 1 : REQUIRE_THROWS_AS(zipios::zipWrite(os, c), zipios::IOException);
918 : }
919 : }
920 :
921 24 : WHEN("writing some data and mark the output as invalid")
922 : {
923 1 : uint32_t a(0x03050709);
924 2 : std::string b("TEST");
925 2 : std::ofstream os("io.bin", std::ios::out | std::ios::binary);
926 1 : zipios::zipWrite(os, a);
927 1 : zipios::zipWrite(os, b);
928 1 : os.setstate(std::ios::failbit);
929 :
930 2 : THEN("writing a 32 bit value fails")
931 : {
932 1 : uint32_t c(0xFFEEDDCC);
933 1 : REQUIRE_THROWS_AS(zipios::zipWrite(os, c), zipios::IOException);
934 : }
935 : }
936 :
937 24 : WHEN("writing some data and mark the output as invalid")
938 : {
939 1 : uint32_t a(0x03050709);
940 2 : std::string b("TEST");
941 2 : std::ofstream os("io.bin", std::ios::out | std::ios::binary);
942 1 : zipios::zipWrite(os, a);
943 1 : zipios::zipWrite(os, b);
944 1 : os.setstate(std::ios::failbit);
945 :
946 2 : THEN("writing a string fails")
947 : {
948 2 : std::string c("TEST");
949 1 : REQUIRE_THROWS_AS(zipios::zipWrite(os, c), zipios::IOException);
950 : }
951 : }
952 :
953 24 : WHEN("writing some data and mark the output as invalid")
954 : {
955 1 : uint32_t a(0x03050709);
956 2 : std::string b("TEST");
957 2 : std::ofstream os("io.bin", std::ios::out | std::ios::binary);
958 1 : zipios::zipWrite(os, a);
959 1 : zipios::zipWrite(os, b);
960 1 : os.setstate(std::ios::failbit);
961 :
962 2 : THEN("writing a buffer fails")
963 : {
964 2 : zipios::buffer_t c;
965 1 : c.push_back('F');
966 1 : c.push_back('A');
967 1 : c.push_back('I');
968 1 : c.push_back('L');
969 1 : REQUIRE_THROWS_AS(zipios::zipWrite(os, c), zipios::IOException);
970 : }
971 : }
972 :
973 12 : unlink("io.bin");
974 : }
975 15 : }
976 :
977 :
978 : // Local Variables:
979 : // mode: cpp
980 : // indent-tabs-mode: nil
981 : // c-basic-offset: 4
982 : // tab-width: 4
983 : // End:
984 :
985 : // vim: ts=4 sw=4 et
|