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 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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 : */
21 :
22 : /** \file
23 : *
24 : * Zipios++ unit tests for the BackBuffer class.
25 : */
26 :
27 : #include "catch_tests.hpp"
28 :
29 : #include "src/backbuffer.hpp"
30 : #include "zipios++/zipiosexceptions.hpp"
31 :
32 : #include <fstream>
33 :
34 :
35 7 : SCENARIO("BackBuffer read a file", "[BackBuffer]")
36 : {
37 6 : GIVEN("a binary file of 256 bytes")
38 : {
39 : // create a file of 256 bytes
40 5 : zipios_test::auto_unlink_t auto_unlink("file256.bin");
41 : {
42 5 : std::ofstream os("file256.bin", std::ios::out | std::ios::binary);
43 1285 : for(int i(0); i < 256; ++i)
44 : {
45 1280 : os << static_cast<char>(i);
46 5 : }
47 : }
48 :
49 5 : WHEN("setting up a backbuffer")
50 : {
51 4 : std::ifstream is("file256.bin", std::ios::in | std::ios::binary);
52 8 : zipios::BackBuffer back_buffer(is, zipios::VirtualSeeker(), 16);
53 :
54 4 : THEN("we can read the file going backward")
55 : {
56 1 : ssize_t read_pointer(0);
57 17 : for(int i(0), j(256 - 16); i < 256; i += 16, j -= 16)
58 : {
59 16 : back_buffer.readChunk(read_pointer);
60 16 : REQUIRE(read_pointer == i + 16);
61 272 : for(int k(0); k < 16; ++k)
62 : {
63 256 : REQUIRE(back_buffer[k] == j + k);
64 : }
65 : }
66 4 : }
67 :
68 4 : THEN("try with an invalid chunk size")
69 : {
70 18 : for(int i(-16); i <= 0; ++i)
71 : {
72 17 : REQUIRE_THROWS_AS(zipios::BackBuffer bb(is, zipios::VirtualSeeker(), i), zipios::InvalidException);
73 : }
74 4 : }
75 :
76 4 : THEN("we close the file and we get an exception")
77 : {
78 1 : is.close();
79 : // first time the seek fails
80 1 : REQUIRE_THROWS_AS(zipios::BackBuffer bb(is), zipios::IOException);
81 : // second time the file is marked as invalid
82 1 : REQUIRE_THROWS_AS(zipios::BackBuffer bb(is), zipios::InvalidException);
83 8 : }
84 5 : }
85 6 : }
86 9 : }
87 :
88 :
89 : // vim: ts=4 sw=4 et
90 :
91 : // Local Variables:
92 : // mode: cpp
93 : // indent-tabs-mode: nil
94 : // c-basic-offset: 4
95 : // tab-width: 4
96 : // End:
|