LCOV - code coverage report
Current view: top level - home/snapwebsites/snapcpp/snapwebsites/snapdatabase/tests - virtual_buffer.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 47 47 100.0 %
Date: 2019-12-15 17:13:15 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2019  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/snapdatabase
       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 2 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 along
      17             : // with this program; if not, write to the Free Software Foundation, Inc.,
      18             : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      19             : 
      20             : // self
      21             : //
      22             : #include    "main.h"
      23             : 
      24             : 
      25             : // snapdatabase lib
      26             : //
      27             : #include    <snapdatabase/data/virtual_buffer.h>
      28             : 
      29             : 
      30             : // advgetopt lib
      31             : //
      32             : #include    <advgetopt/options.h>
      33             : 
      34             : 
      35             : 
      36             : 
      37           5 : CATCH_TEST_CASE("VirtualBuffer", "[virtual-buffer]")
      38             : {
      39           6 :     CATCH_START_SECTION("simple write + read")
      40             :     {
      41           2 :         snapdatabase::virtual_buffer::pointer_t v(std::make_shared<snapdatabase::virtual_buffer>());
      42           1 :         CATCH_REQUIRE(v->size() == 0);
      43           1 :         CATCH_REQUIRE(v->count_buffers() == 0);
      44             : 
      45           1 :         constexpr size_t const buf_size(1024);
      46             :         char buf[buf_size];
      47        1025 :         for(size_t i(0); i < sizeof(buf); ++i)
      48             :         {
      49        1024 :             buf[i] = rand();
      50             :         }
      51           1 :         CATCH_REQUIRE(v->pwrite(buf, sizeof(buf), 0, true) == sizeof(buf));
      52             : 
      53           1 :         CATCH_REQUIRE(v->size() == sizeof(buf));
      54           1 :         CATCH_REQUIRE(v->count_buffers() == 1);  // one write means at most 1 buffer
      55             : 
      56             :         char saved[buf_size];
      57           1 :         CATCH_REQUIRE(v->pread(saved, sizeof(saved), 0, true) == sizeof(saved));
      58             : 
      59           1 :         CATCH_REQUIRE(memcmp(buf, saved, sizeof(buf)) == 0);
      60             :     }
      61             :     CATCH_END_SECTION()
      62             : 
      63           6 :     CATCH_START_SECTION("write once + read many times")
      64             :     {
      65           2 :         snapdatabase::virtual_buffer::pointer_t v(std::make_shared<snapdatabase::virtual_buffer>());
      66           1 :         CATCH_REQUIRE(v->size() == 0);
      67           1 :         CATCH_REQUIRE(v->count_buffers() == 0);
      68             : 
      69           1 :         constexpr size_t const buf_size(1024);
      70             :         char buf[buf_size];
      71        1025 :         for(size_t i(0); i < sizeof(buf); ++i)
      72             :         {
      73        1024 :             buf[i] = rand();
      74             :         }
      75           1 :         CATCH_REQUIRE(v->pwrite(buf, sizeof(buf), 0, true) == sizeof(buf));
      76             : 
      77           1 :         CATCH_REQUIRE(v->size() == sizeof(buf));
      78           1 :         CATCH_REQUIRE(v->count_buffers() == 1);  // one write means at most 1 buffer
      79             : 
      80        1025 :         for(size_t i(0); i < sizeof(buf); ++i)
      81             :         {
      82             :             char c;
      83        1024 :             CATCH_REQUIRE(v->pread(&c, sizeof(c), i, true) == sizeof(c));
      84        1024 :             CATCH_REQUIRE(buf[i] == c);
      85             :         }
      86             :     }
      87             :     CATCH_END_SECTION()
      88             : 
      89           6 :     CATCH_START_SECTION("short write + read several times")
      90             :     {
      91           2 :         snapdatabase::virtual_buffer::pointer_t v(std::make_shared<snapdatabase::virtual_buffer>());
      92           1 :         CATCH_REQUIRE(v->size() == 0);
      93           1 :         CATCH_REQUIRE(v->count_buffers() == 0);
      94             : 
      95           1 :         constexpr size_t const buf_size(1024);
      96             :         char buf[buf_size];
      97        1025 :         for(size_t i(0); i < sizeof(buf); ++i)
      98             :         {
      99        1024 :             buf[i] = rand();
     100             :         }
     101           1 :         CATCH_REQUIRE(v->pwrite(buf, sizeof(buf), 0, true) == sizeof(buf));
     102             : 
     103           1 :         CATCH_REQUIRE(v->size() == sizeof(buf));
     104           1 :         CATCH_REQUIRE(v->count_buffers() == 1);  // one write means at most 1 buffer
     105             : 
     106             :         // update the first 4 bytes
     107             :         //
     108           1 :         buf[0] = rand();
     109           1 :         buf[1] = rand();
     110           1 :         buf[2] = rand();
     111           1 :         buf[3] = rand();
     112           1 :         CATCH_REQUIRE(v->pwrite(buf, 4, 0, false) == 4);
     113             : 
     114           1 :         CATCH_REQUIRE(v->size() == sizeof(buf));
     115           1 :         CATCH_REQUIRE(v->count_buffers() == 1);  // one write means at most 1 buffer
     116             : 
     117        1025 :         for(size_t i(0); i < sizeof(buf); ++i)
     118             :         {
     119             :             char c;
     120        1024 :             CATCH_REQUIRE(v->pread(&c, sizeof(c), i, true) == sizeof(c));
     121        1024 :             CATCH_REQUIRE(buf[i] == c);
     122             :         }
     123             :     }
     124             :     CATCH_END_SECTION()
     125           9 : }
     126             : 
     127             : 
     128             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13