LCOV - code coverage report
Current view: top level - tests - catch_virtual_buffer.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 59 59 100.0 %
Date: 2024-02-03 18:59:18 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2019-2024  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/prinbee
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software: you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation, either version 3 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License
      17             : // along with this program.  If not, see <https://www.gnu.org/licenses/>.
      18             : 
      19             : // self
      20             : //
      21             : #include    "catch_main.h"
      22             : 
      23             : 
      24             : // prinbee
      25             : //
      26             : #include    <prinbee/data/virtual_buffer.h>
      27             : 
      28             : 
      29             : // advgetopt
      30             : //
      31             : #include    <advgetopt/options.h>
      32             : 
      33             : 
      34             : 
      35             : 
      36           3 : CATCH_TEST_CASE("VirtualBuffer", "[virtual-buffer]")
      37             : {
      38           3 :     CATCH_START_SECTION("simple write + read")
      39             :     {
      40           1 :         prinbee::virtual_buffer::pointer_t v(std::make_shared<prinbee::virtual_buffer>());
      41           1 :         CATCH_REQUIRE(v->size() == 0);
      42           1 :         CATCH_REQUIRE(v->count_buffers() == 0);
      43             : 
      44           1 :         constexpr size_t const buf_size(1024);
      45           1 :         char buf[buf_size];
      46        1025 :         for(size_t i(0); i < sizeof(buf); ++i)
      47             :         {
      48        1024 :             buf[i] = rand();
      49             :         }
      50           1 :         CATCH_REQUIRE(v->pwrite(buf, sizeof(buf), 0, true) == sizeof(buf));
      51             : 
      52           1 :         CATCH_REQUIRE(v->size() == sizeof(buf));
      53           1 :         CATCH_REQUIRE(v->count_buffers() == 1);  // one write means at most 1 buffer
      54             : 
      55           1 :         char saved[buf_size];
      56           1 :         CATCH_REQUIRE(v->pread(saved, sizeof(saved), 0, true) == sizeof(saved));
      57             : 
      58           1 :         CATCH_REQUIRE(memcmp(buf, saved, sizeof(buf)) == 0);
      59           1 :     }
      60           3 :     CATCH_END_SECTION()
      61             : 
      62           3 :     CATCH_START_SECTION("write once + read many times")
      63             :     {
      64           1 :         prinbee::virtual_buffer::pointer_t v(std::make_shared<prinbee::virtual_buffer>());
      65           1 :         CATCH_REQUIRE(v->size() == 0);
      66           1 :         CATCH_REQUIRE(v->count_buffers() == 0);
      67             : 
      68           1 :         constexpr size_t const buf_size(1024);
      69           1 :         char buf[buf_size];
      70        1025 :         for(size_t i(0); i < sizeof(buf); ++i)
      71             :         {
      72        1024 :             buf[i] = rand();
      73             :         }
      74           1 :         CATCH_REQUIRE(v->pwrite(buf, sizeof(buf), 0, true) == sizeof(buf));
      75             : 
      76           1 :         CATCH_REQUIRE(v->size() == sizeof(buf));
      77           1 :         CATCH_REQUIRE(v->count_buffers() == 1);  // one write means at most 1 buffer
      78             : 
      79        1025 :         for(size_t i(0); i < sizeof(buf); ++i)
      80             :         {
      81        1024 :             char c;
      82        1024 :             CATCH_REQUIRE(v->pread(&c, sizeof(c), i, true) == sizeof(c));
      83        1024 :             CATCH_REQUIRE(buf[i] == c);
      84             :         }
      85           1 :     }
      86           3 :     CATCH_END_SECTION()
      87             : 
      88           3 :     CATCH_START_SECTION("short write + read several times")
      89             :     {
      90           1 :         prinbee::virtual_buffer::pointer_t v(std::make_shared<prinbee::virtual_buffer>());
      91           1 :         CATCH_REQUIRE(v->size() == 0);
      92           1 :         CATCH_REQUIRE(v->count_buffers() == 0);
      93             : 
      94           1 :         constexpr size_t const buf_size(1024);
      95           1 :         char buf[buf_size];
      96        1025 :         for(size_t i(0); i < sizeof(buf); ++i)
      97             :         {
      98        1024 :             buf[i] = rand();
      99             :         }
     100           1 :         CATCH_REQUIRE(v->pwrite(buf, sizeof(buf), 0, true) == sizeof(buf));
     101             : 
     102           1 :         CATCH_REQUIRE(v->size() == sizeof(buf));
     103           1 :         CATCH_REQUIRE(v->count_buffers() == 1);  // one write means at most 1 buffer
     104             : 
     105             :         // update the first 4 bytes
     106             :         //
     107           1 :         buf[0] = rand();
     108           1 :         buf[1] = rand();
     109           1 :         buf[2] = rand();
     110           1 :         buf[3] = rand();
     111           1 :         CATCH_REQUIRE(v->pwrite(buf, 4, 0, false) == 4);
     112             : 
     113           1 :         CATCH_REQUIRE(v->size() == sizeof(buf));
     114           1 :         CATCH_REQUIRE(v->count_buffers() == 1);  // one write means at most 1 buffer
     115             : 
     116        1025 :         for(size_t i(0); i < sizeof(buf); ++i)
     117             :         {
     118        1024 :             char c;
     119        1024 :             CATCH_REQUIRE(v->pread(&c, sizeof(c), i, true) == sizeof(c));
     120        1024 :             CATCH_REQUIRE(buf[i] == c);
     121             :         }
     122           1 :     }
     123           3 :     CATCH_END_SECTION()
     124           3 : }
     125             : 
     126             : 
     127             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.14

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