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

Generated by: LCOV version 1.14

Snap C++ | List of projects | List of versions