LCOV - code coverage report
Current view: top level - tests - catch_common.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 649 649 100.0 %
Date: 2015-05-05 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          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 used to verify the various funtions defined in
      25             :  * zipios_common.hpp.
      26             :  */
      27             : 
      28             : #include "catch_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          12 : SCENARIO("Vector append", "[zipios_common]")
      39             : {
      40          11 :     GIVEN("an empty vector")
      41             :     {
      42           5 :         std::vector<std::string> es;
      43             : 
      44           5 :         WHEN("appending another empty vector")
      45             :         {
      46           2 :             std::vector<std::string> os;
      47             : 
      48           2 :             es += os;
      49             : 
      50           2 :             THEN("the result is still an emtpy vector")
      51             :             {
      52           1 :                 REQUIRE(es.empty());
      53           2 :             }
      54           5 :         }
      55             : 
      56           5 :         WHEN("appending a non-empty vector")
      57             :         {
      58           2 :             std::vector<std::string> os{ "a", "b", "c" };
      59             : 
      60           2 :             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           2 :             }
      70           5 :         }
      71          11 :     }
      72             : 
      73          11 :     GIVEN("a non-empty vector")
      74             :     {
      75           5 :         std::vector<std::string> es{ "x", "y", "z" };
      76             : 
      77           5 :         WHEN("appending an empty vector")
      78             :         {
      79           2 :             std::vector<std::string> os;
      80             : 
      81           2 :             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           2 :             }
      91           5 :         }
      92             : 
      93           5 :         WHEN("appending a non-empty vector")
      94             :         {
      95           2 :             std::vector<std::string> os{ "a", "b", "c" };
      96             : 
      97           2 :             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           2 :             }
     110           5 :         }
     111          11 :     }
     112          11 : }
     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          27 : SCENARIO("Read from file", "[zipios_common] [io]")
     124             : {
     125          26 :     GIVEN("a simple file")
     126             :     {
     127             :         // create a file
     128             :         {
     129          25 :             std::ofstream os("io.bin", std::ios::out | std::ios::binary);
     130         425 :             for(int i(0); i < 16; ++i)
     131             :             {
     132         400 :                 os << static_cast<char>(i);
     133          25 :             }
     134             :         }
     135             : 
     136             :         // now open it for reading
     137          25 :         std::ifstream is("io.bin", std::ios::in | std::ios::binary);
     138             : 
     139          25 :         WHEN("reading two 32 bit values")
     140             :         {
     141             :             uint32_t a, b;
     142           2 :             zipios::zipRead(is, a);
     143           2 :             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           2 :             }
     150          25 :         }
     151             : 
     152          25 :         WHEN("reading one 32 bit between two 16 bit values")
     153             :         {
     154             :             uint32_t b;
     155             :             uint16_t a, c;
     156           2 :             zipios::zipRead(is, a);
     157           2 :             zipios::zipRead(is, b);
     158           2 :             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           2 :             }
     166          25 :         }
     167             : 
     168          25 :         WHEN("reading one 16 bit between two 32 bit values")
     169             :         {
     170             :             uint32_t a, c;
     171             :             uint16_t b;
     172           2 :             zipios::zipRead(is, a);
     173           2 :             zipios::zipRead(is, b);
     174           2 :             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           2 :             }
     182          25 :         }
     183             : 
     184          25 :         WHEN("reading three 16 bit values")
     185             :         {
     186             :             uint16_t a, b, c;
     187           2 :             zipios::zipRead(is, a);
     188           2 :             zipios::zipRead(is, b);
     189           2 :             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           2 :             }
     197          25 :         }
     198             : 
     199          25 :         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           2 :             zipios::zipRead(is, a);
     205           2 :             zipios::zipRead(is, b);
     206           2 :             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           2 :             }
     214          25 :         }
     215             : 
     216          25 :         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           2 :             zipios::zipRead(is, a);
     222           2 :             zipios::zipRead(is, b);
     223           2 :             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           2 :             }
     239          25 :         }
     240             : 
     241          25 :         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           2 :             zipios::zipRead(is, a);
     247           2 :             zipios::zipRead(is, b, 8);
     248           2 :             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           2 :             }
     264          25 :         }
     265             : 
     266          25 :         WHEN("reading four 32 bit values")
     267             :         {
     268             :             uint32_t a, b, c, d;
     269           2 :             zipios::zipRead(is, a);
     270           2 :             zipios::zipRead(is, b);
     271           2 :             zipios::zipRead(is, c);
     272           2 :             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           2 :             }
     284          25 :         }
     285             : 
     286          25 :         WHEN("reading four 32 bit values")
     287             :         {
     288             :             uint32_t a, b, c, d;
     289           2 :             zipios::zipRead(is, a);
     290           2 :             zipios::zipRead(is, b);
     291           2 :             zipios::zipRead(is, c);
     292           2 :             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           2 :             }
     304          25 :         }
     305             : 
     306          25 :         WHEN("reading four 32 bit values")
     307             :         {
     308             :             uint32_t a, b, c, d;
     309           2 :             zipios::zipRead(is, a);
     310           2 :             zipios::zipRead(is, b);
     311           2 :             zipios::zipRead(is, c);
     312           2 :             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           2 :             }
     324          25 :         }
     325             : 
     326          25 :         WHEN("reading two 32 bit values")
     327             :         {
     328             :             uint32_t a, b;
     329           2 :             zipios::zipRead(is, a);
     330           2 :             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           1 :                 std::string e;
     339           1 :                 REQUIRE_THROWS_AS(zipios::zipRead(is, e, 12), zipios::IOException);
     340           2 :             }
     341          25 :         }
     342             : 
     343          25 :         WHEN("reading two 32 bit values")
     344             :         {
     345             :             uint32_t a, b;
     346           2 :             zipios::zipRead(is, a);
     347           2 :             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           1 :                 zipios::buffer_t e;
     356           1 :                 REQUIRE_THROWS_AS(zipios::zipRead(is, e, 12), zipios::IOException);
     357           2 :             }
     358          25 :         }
     359             : 
     360          25 :         unlink("io.bin");
     361          26 :     }
     362          26 : }
     363             : 
     364             : 
     365          27 : SCENARIO("Read from buffer", "[zipios_common] [io]")
     366             : {
     367          26 :     GIVEN("a simple buffer")
     368             :     {
     369          25 :         zipios::buffer_t is;
     370         425 :         for(int i(0); i < 16; ++i)
     371             :         {
     372         400 :             is.push_back(static_cast<char>(i));
     373             :         }
     374             : 
     375          25 :         WHEN("reading two 32 bit values")
     376             :         {
     377             :             uint32_t a, b;
     378           2 :             size_t pos(0);
     379           2 :             zipios::zipRead(is, pos, a);
     380           2 :             REQUIRE(pos == 4);
     381           2 :             zipios::zipRead(is, pos, b);
     382           2 :             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           2 :             }
     389          25 :         }
     390             : 
     391          25 :         WHEN("reading one 32 bit between two 16 bit values")
     392             :         {
     393             :             uint32_t b;
     394             :             uint16_t a, c;
     395           2 :             size_t pos(0);
     396           2 :             zipios::zipRead(is, pos, a);
     397           2 :             REQUIRE(pos == 2);
     398           2 :             zipios::zipRead(is, pos, b);
     399           2 :             REQUIRE(pos == 6);
     400           2 :             zipios::zipRead(is, pos, c);
     401           2 :             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           2 :             }
     409          25 :         }
     410             : 
     411          25 :         WHEN("reading one 16 bit between two 32 bit values")
     412             :         {
     413             :             uint32_t a, c;
     414             :             uint16_t b;
     415           2 :             size_t pos(0);
     416           2 :             zipios::zipRead(is, pos, a);
     417           2 :             REQUIRE(pos == 4);
     418           2 :             zipios::zipRead(is, pos, b);
     419           2 :             REQUIRE(pos == 6);
     420           2 :             zipios::zipRead(is, pos, c);
     421           2 :             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           2 :             }
     429          25 :         }
     430             : 
     431          25 :         WHEN("reading three 16 bit values")
     432             :         {
     433             :             uint16_t a, b, c;
     434           2 :             size_t pos(0);
     435           2 :             zipios::zipRead(is, pos, a);
     436           2 :             REQUIRE(pos == 2);
     437           2 :             zipios::zipRead(is, pos, b);
     438           2 :             REQUIRE(pos == 4);
     439           2 :             zipios::zipRead(is, pos, c);
     440           2 :             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           2 :             }
     448          25 :         }
     449             : 
     450          25 :         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           2 :             size_t pos(0);
     456           2 :             zipios::zipRead(is, pos, a);
     457           2 :             REQUIRE(pos == 4);
     458           2 :             zipios::zipRead(is, pos, b);
     459           2 :             REQUIRE(pos == 5);
     460           2 :             zipios::zipRead(is, pos, c);
     461           2 :             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           2 :             }
     469          25 :         }
     470             : 
     471          25 :         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           2 :             size_t pos(0);
     477           2 :             zipios::zipRead(is, pos, a);
     478           2 :             REQUIRE(pos == 4);
     479           2 :             zipios::zipRead(is, pos, b);
     480           2 :             REQUIRE(pos == 5);
     481           2 :             zipios::zipRead(is, pos, c, 8);
     482           2 :             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           2 :             }
     498          25 :         }
     499             : 
     500          25 :         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           2 :             size_t pos(0);
     506           2 :             zipios::zipRead(is, pos, a);
     507           2 :             REQUIRE(pos == 4);
     508           2 :             zipios::zipRead(is, pos, b, 8);
     509           2 :             REQUIRE(pos == 12);
     510           2 :             zipios::zipRead(is, pos, c);
     511           2 :             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           2 :             }
     527          25 :         }
     528             : 
     529          25 :         WHEN("reading four 32 bit values")
     530             :         {
     531             :             uint32_t a, b, c, d;
     532           2 :             size_t pos(0);
     533           2 :             zipios::zipRead(is, pos, a);
     534           2 :             REQUIRE(pos == 4);
     535           2 :             zipios::zipRead(is, pos, b);
     536           2 :             REQUIRE(pos == 8);
     537           2 :             zipios::zipRead(is, pos, c);
     538           2 :             REQUIRE(pos == 12);
     539           2 :             zipios::zipRead(is, pos, d);
     540           2 :             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           2 :             }
     552          25 :         }
     553             : 
     554          25 :         WHEN("reading four 32 bit values")
     555             :         {
     556             :             uint32_t a, b, c, d;
     557           2 :             size_t pos(0);
     558           2 :             zipios::zipRead(is, pos, a);
     559           2 :             REQUIRE(pos == 4);
     560           2 :             zipios::zipRead(is, pos, b);
     561           2 :             REQUIRE(pos == 8);
     562           2 :             zipios::zipRead(is, pos, c);
     563           2 :             REQUIRE(pos == 12);
     564           2 :             zipios::zipRead(is, pos, d);
     565           2 :             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           2 :             }
     577          25 :         }
     578             : 
     579          25 :         WHEN("reading four 32 bit values")
     580             :         {
     581             :             uint32_t a, b, c, d;
     582           2 :             size_t pos(0);
     583           2 :             zipios::zipRead(is, pos, a);
     584           2 :             REQUIRE(pos == 4);
     585           2 :             zipios::zipRead(is, pos, b);
     586           2 :             REQUIRE(pos == 8);
     587           2 :             zipios::zipRead(is, pos, c);
     588           2 :             REQUIRE(pos == 12);
     589           2 :             zipios::zipRead(is, pos, d);
     590           2 :             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           2 :             }
     602          25 :         }
     603             : 
     604          25 :         WHEN("reading two 32 bit values")
     605             :         {
     606             :             uint32_t a, b;
     607           2 :             size_t pos(0);
     608           2 :             zipios::zipRead(is, pos, a);
     609           2 :             REQUIRE(pos == 4);
     610           2 :             zipios::zipRead(is, pos, b);
     611           2 :             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           1 :                 std::string e;
     620           1 :                 REQUIRE_THROWS_AS(zipios::zipRead(is, pos, e, 12), zipios::IOException);
     621           2 :             }
     622          25 :         }
     623             : 
     624          25 :         WHEN("reading two 32 bit values")
     625             :         {
     626             :             uint32_t a, b;
     627           2 :             size_t pos(0);
     628           2 :             zipios::zipRead(is, pos, a);
     629           2 :             REQUIRE(pos == 4);
     630           2 :             zipios::zipRead(is, pos, b);
     631           2 :             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           1 :                 zipios::buffer_t e;
     640           1 :                 REQUIRE_THROWS_AS(zipios::zipRead(is, pos, e, 12), zipios::IOException);
     641           2 :             }
     642          25 :         }
     643          26 :     }
     644          26 : }
     645             : 
     646             : 
     647          27 : SCENARIO("Write to file", "[zipios_common] [io]")
     648             : {
     649          26 :     GIVEN("create an empty file")
     650             :     {
     651             : 
     652          25 :         WHEN("writing two 32 bit values")
     653             :         {
     654           2 :             uint32_t a(0x03020100), b(0x07060504);
     655             :             {
     656           2 :                 std::ofstream os("io.bin", std::ios::out | std::ios::binary);
     657           2 :                 zipios::zipWrite(os, a);
     658           2 :                 zipios::zipWrite(os, b);
     659             :             }
     660             : 
     661           2 :             THEN("we get exactly the value we expected")
     662             :             {
     663           1 :                 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           2 :             }
     680          25 :         }
     681             : 
     682          25 :         WHEN("writing one 32 bit between two 16 bit values")
     683             :         {
     684           2 :             uint32_t b(0x55112288);
     685           2 :             uint16_t a(0x3344), c(0x6677);
     686             :             {
     687           2 :                 std::ofstream os("io.bin", std::ios::out | std::ios::binary);
     688           2 :                 zipios::zipWrite(os, a);
     689           2 :                 zipios::zipWrite(os, b);
     690           2 :                 zipios::zipWrite(os, c);
     691             :             }
     692             : 
     693           2 :             THEN("the result is exactly as expected")
     694             :             {
     695           1 :                 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           2 :             }
     712          25 :         }
     713             : 
     714          25 :         WHEN("writing one 16 bit between two 32 bit values")
     715             :         {
     716           2 :             uint32_t a(0x01050803), c(0x10508030);
     717           2 :             uint16_t b(0xFF00);
     718             :             {
     719           2 :                 std::ofstream os("io.bin", std::ios::out | std::ios::binary);
     720           2 :                 zipios::zipWrite(os, a);
     721           2 :                 zipios::zipWrite(os, b);
     722           2 :                 zipios::zipWrite(os, c);
     723             :             }
     724             : 
     725           2 :             THEN("the result is as expected")
     726             :             {
     727           1 :                 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           2 :             }
     746          25 :         }
     747             : 
     748          25 :         WHEN("writing three 16 bit values")
     749             :         {
     750           2 :             uint16_t a(0xEECC), b(0xAADD), c(0xFFBB);
     751             :             {
     752           2 :                 std::ofstream os("io.bin", std::ios::out | std::ios::binary);
     753           2 :                 zipios::zipWrite(os, a);
     754           2 :                 zipios::zipWrite(os, b);
     755           2 :                 zipios::zipWrite(os, c);
     756             :             }
     757             : 
     758           2 :             THEN("the result is as expected")
     759             :             {
     760           1 :                 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           2 :             }
     775          25 :         }
     776             : 
     777          25 :         WHEN("writing one 32 bit, one 8 bit then one 16 bit value")
     778             :         {
     779           2 :             uint32_t a(0x11223344);
     780           2 :             uint8_t  b(0xAA);
     781           2 :             uint16_t c(0x9988);
     782             :             {
     783           2 :                 std::ofstream os("io.bin", std::ios::out | std::ios::binary);
     784           2 :                 zipios::zipWrite(os, a);
     785           2 :                 zipios::zipWrite(os, b);
     786           2 :                 zipios::zipWrite(os, c);
     787             :             }
     788             : 
     789           2 :             THEN("the result is as expected")
     790             :             {
     791           1 :                 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           2 :             }
     807          25 :         }
     808             : 
     809          25 :         WHEN("writing one 32 bit, one 8 bit then one buffer value")
     810             :         {
     811           2 :             uint32_t a(0x01020304);
     812           2 :             uint8_t  b(0xFF);
     813           2 :             zipios::buffer_t c;
     814           2 :             c.push_back(0xA0);
     815           2 :             c.push_back(0xA1);
     816           2 :             c.push_back(0xA2);
     817           2 :             c.push_back(0xA3);
     818           2 :             c.push_back(0xA4);
     819           2 :             c.push_back(0xA5);
     820           2 :             c.push_back(0xA6);
     821           2 :             c.push_back(0xA7);
     822             :             {
     823           2 :                 std::ofstream os("io.bin", std::ios::out | std::ios::binary);
     824           2 :                 zipios::zipWrite(os, a);
     825           2 :                 zipios::zipWrite(os, b);
     826           2 :                 zipios::zipWrite(os, c);
     827             :             }
     828             : 
     829           2 :             THEN("the result is as expected")
     830             :             {
     831           1 :                 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           2 :             }
     853          25 :         }
     854             : 
     855          25 :         WHEN("writing one 32 bit, one string and then one 8 bit value")
     856             :         {
     857           2 :             uint32_t a(0x03050709);
     858           2 :             std::string b("TEST");
     859           2 :             uint8_t c(0x01);
     860             :             {
     861           2 :                 std::ofstream os("io.bin", std::ios::out | std::ios::binary);
     862           2 :                 zipios::zipWrite(os, a);
     863           2 :                 zipios::zipWrite(os, b);
     864           2 :                 zipios::zipWrite(os, c);
     865             :             }
     866             : 
     867           2 :             THEN("the result is as expected")
     868             :             {
     869           1 :                 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           2 :             }
     887          25 :         }
     888             : 
     889          25 :         WHEN("writing some data and mark the output as invalid")
     890             :         {
     891           2 :             uint32_t a(0x03050709);
     892           2 :             std::string b("TEST");
     893           4 :             std::ofstream os("io.bin", std::ios::out | std::ios::binary);
     894           2 :             zipios::zipWrite(os, a);
     895           2 :             zipios::zipWrite(os, b);
     896           2 :             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           4 :             }
     903          25 :         }
     904             : 
     905          25 :         WHEN("writing some data and mark the output as invalid")
     906             :         {
     907           2 :             uint32_t a(0x03050709);
     908           2 :             std::string b("TEST");
     909           4 :             std::ofstream os("io.bin", std::ios::out | std::ios::binary);
     910           2 :             zipios::zipWrite(os, a);
     911           2 :             zipios::zipWrite(os, b);
     912           2 :             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           4 :             }
     919          25 :         }
     920             : 
     921          25 :         WHEN("writing some data and mark the output as invalid")
     922             :         {
     923           2 :             uint32_t a(0x03050709);
     924           2 :             std::string b("TEST");
     925           4 :             std::ofstream os("io.bin", std::ios::out | std::ios::binary);
     926           2 :             zipios::zipWrite(os, a);
     927           2 :             zipios::zipWrite(os, b);
     928           2 :             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           4 :             }
     935          25 :         }
     936             : 
     937          25 :         WHEN("writing some data and mark the output as invalid")
     938             :         {
     939           2 :             uint32_t a(0x03050709);
     940           2 :             std::string b("TEST");
     941           4 :             std::ofstream os("io.bin", std::ios::out | std::ios::binary);
     942           2 :             zipios::zipWrite(os, a);
     943           2 :             zipios::zipWrite(os, b);
     944           2 :             os.setstate(std::ios::failbit);
     945             : 
     946           2 :             THEN("writing a string fails")
     947             :             {
     948           1 :                 std::string c("TEST");
     949           1 :                 REQUIRE_THROWS_AS(zipios::zipWrite(os, c), zipios::IOException);
     950           4 :             }
     951          25 :         }
     952             : 
     953          25 :         WHEN("writing some data and mark the output as invalid")
     954             :         {
     955           2 :             uint32_t a(0x03050709);
     956           2 :             std::string b("TEST");
     957           4 :             std::ofstream os("io.bin", std::ios::out | std::ios::binary);
     958           2 :             zipios::zipWrite(os, a);
     959           2 :             zipios::zipWrite(os, b);
     960           2 :             os.setstate(std::ios::failbit);
     961             : 
     962           2 :             THEN("writing a buffer fails")
     963             :             {
     964           1 :                 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           4 :             }
     971          25 :         }
     972             : 
     973          25 :         unlink("io.bin");
     974          26 :     }
     975          29 : }
     976             : 
     977             : 
     978             : // vim: ts=4 sw=4 et
     979             : 
     980             : // Local Variables:
     981             : // mode: cpp
     982             : // indent-tabs-mode: nil
     983             : // c-basic-offset: 4
     984             : // tab-width: 4
     985             : // End:

Generated by: LCOV version 1.10