LCOV - code coverage report
Current view: top level - tests - catch_hash.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 56 56 100.0 %
Date: 2024-02-03 18:59:18 Functions: 2 2 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/file/hash.h>
      27             : 
      28             : 
      29             : // advgetopt
      30             : //
      31             : #include    <advgetopt/options.h>
      32             : 
      33             : 
      34             : 
      35             : namespace
      36             : {
      37             : 
      38             : 
      39             : 
      40             : // hash function taken from: https://github.com/ArashPartow/bloom
      41             : //
      42       51439 : prinbee::hash_t compute_hash(uint8_t const * v, std::size_t size, prinbee::hash_t const seed)
      43             : {
      44       51439 :     prinbee::hash_t hash(seed);
      45       51439 :     prinbee::hash_t loop(0);
      46             : 
      47   228162867 :     for(; size >= 8; v += 8, size -= 8)
      48             :     {
      49   228111428 :         prinbee::hash_t i1((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3]);
      50   228111428 :         prinbee::hash_t i2((v[4] << 24) + (v[5] << 16) + (v[6] << 8) + v[7]);
      51             : 
      52   228111428 :         hash ^= (hash <<  7) ^  i1 * (hash >> 3) ^
      53   228111428 :              (~((hash << 11) + (i2 ^ (hash >> 5))));
      54             :     }
      55             : 
      56       51439 :     if(size >= 4)
      57             :     {
      58       25771 :         prinbee::hash_t i((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3]);
      59       25771 :         hash ^= (~((hash << 11) + (i ^ (hash >> 5))));
      60       25771 :         ++loop;
      61       25771 :         size -= 4;
      62       25771 :         v += 4;
      63             :     }
      64             : 
      65       51439 :     if(size >= 2)
      66             :     {
      67       25617 :         prinbee::hash_t i((v[0] << 8) + v[1]);
      68       25617 :         if(loop != 0)
      69             :         {
      70       12844 :             hash ^=    (hash <<  7) ^  i * (hash >> 3);
      71             :         }
      72             :         else
      73             :         {
      74       12773 :             hash ^= (~((hash << 11) + (i ^ (hash >> 5))));
      75             :         }
      76       25617 :         ++loop;
      77       25617 :         size -= 2;
      78       25617 :         v += 2;
      79             :     }
      80             : 
      81       51439 :     if(size > 0)
      82             :     {
      83       25639 :         hash += (v[0] ^ (hash * 0xA5A5A5A5)) + loop;
      84             :     }
      85             : 
      86       51439 :     return hash;
      87             : }
      88             : 
      89             : 
      90             : 
      91             : }
      92             : // no name namespace
      93             : 
      94             : 
      95           1 : CATCH_TEST_CASE("hash", "[hash] [valid]")
      96             : {
      97           1 :     CATCH_START_SECTION("hash")
      98             :     {
      99         101 :         for(int count(0); count < 100; ++count)
     100             :         {
     101         100 :             std::size_t const size(rand() % 65536 + 32768);
     102             : 
     103         200 :             std::vector<std::uint8_t> buffer(size);
     104     6583740 :             for(std::size_t idx(0); idx < size; ++idx)
     105             :             {
     106     6583640 :                 buffer[idx] = rand();
     107             :             }
     108             : 
     109             :             // try hash entire buffer at once
     110             :             {
     111         100 :                 prinbee::hash_t const seed(rand());
     112         100 :                 prinbee::hash_t const expected(compute_hash(buffer.data(), size, seed));
     113         100 :                 prinbee::hash h(seed);
     114         100 :                 h.add(buffer.data(), size);
     115         100 :                 CATCH_REQUIRE(h.get() == expected);
     116             :             }
     117             : 
     118             :             // try a little at a time
     119             :             {
     120         100 :                 prinbee::hash_t const seed(rand());
     121         100 :                 prinbee::hash h(seed);
     122         100 :                 CATCH_REQUIRE(h.size() == 0);
     123         100 :                 std::size_t processed(0);
     124       51439 :                 while(processed < size)
     125             :                 {
     126       51339 :                     std::size_t incr(rand() % 256 + 1);
     127       51339 :                     if(processed + incr > size)
     128             :                     {
     129          99 :                         incr = size - processed;
     130             :                     }
     131       51339 :                     h.add(buffer.data() + processed, incr);
     132       51339 :                     processed += incr;
     133       51339 :                     CATCH_REQUIRE(h.size() == processed);
     134       51339 :                     prinbee::hash_t const expected(compute_hash(buffer.data(), processed, seed));
     135             : 
     136       51339 :                     prinbee::hash once(seed);
     137       51339 :                     once.add(buffer.data(), processed);
     138       51339 :                     CATCH_REQUIRE(once.get() == expected);
     139             : 
     140       51339 :                     CATCH_REQUIRE(h.get() == expected);
     141             :                 }
     142             :             }
     143             : 
     144             : //hash_t compute_hash(uint8_t const * v, std::size_t size, hash_t hash)
     145             : //
     146             : //        hash(hash_t seed);
     147             : //void    add(std::uint8_t const * v, std::size_t size);
     148             : //hash_t  get() const;
     149             : 
     150         100 :         }
     151             :     }
     152           1 :     CATCH_END_SECTION()
     153           1 : }
     154             : 
     155             : 
     156             : 
     157             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.14

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