LCOV - code coverage report
Current view: top level - tests - catch_caseinsensitive.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 99.4 % 154 153
Test Date: 2025-06-22 07:49:47 Functions: 100.0 % 3 3
Legend: Lines: hit not hit

            Line data    Source code
       1              : // Copyright (c) 2021-2023  Made to Order Software Corp.  All Rights Reserved
       2              : //
       3              : // https://snapwebsites.org/project/libutf8
       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              : // libutf8
      21              : //
      22              : #include    <libutf8/caseinsensitivestring.h>
      23              : 
      24              : 
      25              : // unit test
      26              : //
      27              : #include    "catch_main.h"
      28              : 
      29              : 
      30              : // C++
      31              : //
      32              : #include    <cctype>
      33              : #include    <iostream>
      34              : 
      35              : 
      36              : // last include
      37              : //
      38              : #include    <snapdev/poison.h>
      39              : 
      40              : 
      41              : 
      42              : namespace
      43              : {
      44              : 
      45              : 
      46            2 : libutf8::case_insensitive_string get_time(std::string & result)
      47              : {
      48            2 :     time_t const now(time(nullptr));
      49            2 :     struct tm t;
      50            2 :     localtime_r(&now, &t);
      51            2 :     char buf[256];
      52            2 :     strftime(buf, sizeof(buf), "%T", &t);
      53            2 :     buf[sizeof(buf) - 1] = '\0';
      54            2 :     result = buf;
      55            6 :     libutf8::case_insensitive_string r(buf);
      56            2 :     r += " PST";
      57            4 :     return r;
      58            0 : }
      59              : 
      60            2 : std::string get_date(std::string & result)
      61              : {
      62            2 :     time_t const now(time(nullptr));
      63            2 :     struct tm t;
      64            2 :     localtime_r(&now, &t);
      65            2 :     char buf[256];
      66            2 :     strftime(buf, sizeof(buf), "%F", &t);
      67            2 :     buf[sizeof(buf) - 1] = '\0';
      68            2 :     result = buf;
      69            6 :     libutf8::case_insensitive_string r(buf);
      70            2 :     r += " plus a few days";
      71            4 :     return r;
      72            2 : }
      73              : 
      74              : 
      75              : 
      76              : }
      77              : 
      78              : 
      79              : 
      80            2 : CATCH_TEST_CASE("case_insensitive", "[string],[compare],[insensitive]")
      81              : {
      82            2 :     CATCH_START_SECTION("case_insensitive: Verify Case Insensitive String Constructors")
      83              :     {
      84              :         {
      85            1 :             libutf8::case_insensitive_string empty;
      86            1 :             CATCH_REQUIRE(empty.empty());
      87            1 :         }
      88              : 
      89              :         {
      90            1 :             std::allocator<char> allocator;
      91            1 :             libutf8::case_insensitive_string empty(allocator);
      92            1 :             CATCH_REQUIRE(empty.empty());
      93            1 :         }
      94              : 
      95              :         {
      96            3 :             libutf8::case_insensitive_string dashes(10, '-');
      97            1 :             CATCH_REQUIRE(dashes == "----------");
      98            1 :         }
      99              : 
     100              :         {
     101            3 :             libutf8::case_insensitive_string name("alexis");
     102            1 :             CATCH_REQUIRE(name == "alexis");
     103            1 :         }
     104              : 
     105              :         {
     106            3 :             libutf8::case_insensitive_string name("alexis", 4);
     107            1 :             CATCH_REQUIRE(name == "alex");
     108            1 :         }
     109              : 
     110              :         {
     111            3 :             libutf8::case_insensitive_string name("alexis");
     112            1 :             CATCH_REQUIRE(name == "alexis");
     113              : 
     114            3 :             libutf8::case_insensitive_string section(name, 2);
     115            1 :             CATCH_REQUIRE(section == "exis");
     116            1 :         }
     117              : 
     118              :         {
     119            3 :             libutf8::case_insensitive_string name("alexis");
     120            1 :             CATCH_REQUIRE(name == "alexis");
     121              : 
     122            3 :             libutf8::case_insensitive_string section(name, 2, 2);
     123            1 :             CATCH_REQUIRE(section == "ex");
     124            1 :         }
     125              : 
     126              :         {
     127            3 :             std::string name("alexis");
     128            1 :             CATCH_REQUIRE(name == "alexis");
     129              : 
     130            3 :             libutf8::case_insensitive_string section(name, 2);
     131            1 :             CATCH_REQUIRE(section == "exis");
     132            1 :         }
     133              : 
     134              :         {
     135            3 :             std::string name("alexis");
     136            1 :             CATCH_REQUIRE(name == "alexis");
     137              : 
     138            3 :             libutf8::case_insensitive_string section(name, 2, 2);
     139            1 :             CATCH_REQUIRE(section == "ex");
     140            1 :         }
     141              : 
     142              :         {
     143            3 :             libutf8::case_insensitive_string name("alexis");
     144            1 :             CATCH_REQUIRE(name == "alexis");
     145              : 
     146            3 :             libutf8::case_insensitive_string section(name.begin() + 2, name.end() - 2);
     147            1 :             CATCH_REQUIRE(section == "ex");
     148            1 :         }
     149              : 
     150              :         {
     151            3 :             std::string name("alexis");
     152            1 :             CATCH_REQUIRE(name == "alexis");
     153              : 
     154            1 :             libutf8::case_insensitive_string full(name);
     155            1 :             CATCH_REQUIRE(full == "alexis");
     156            1 :         }
     157              : 
     158              :         {
     159            3 :             libutf8::case_insensitive_string name("alexis");
     160            1 :             CATCH_REQUIRE(name == "alexis");
     161              : 
     162            1 :             libutf8::case_insensitive_string full(name);
     163            1 :             CATCH_REQUIRE(full == "alexis");
     164            1 :         }
     165              : 
     166              :         {
     167            3 :             libutf8::case_insensitive_string name({'a', 'l', 'e', 'x', 'i', 's'});
     168            1 :             CATCH_REQUIRE(name == "alexis");
     169            1 :         }
     170              : 
     171              :         {
     172            3 :             std::string expected("not this");
     173            1 :             libutf8::case_insensitive_string now(get_time(expected));
     174            1 :             CATCH_REQUIRE(expected + " PST" == now);
     175            1 :         }
     176              : 
     177              :         {
     178            1 :             std::allocator<char> allocator;
     179            3 :             std::string expected("not this");
     180            1 :             libutf8::case_insensitive_string now(get_time(expected), allocator);
     181            1 :             CATCH_REQUIRE(expected + " PST" == now);
     182            1 :         }
     183              : 
     184              :         {
     185            3 :             std::string expected("not this");
     186            1 :             libutf8::case_insensitive_string now(get_date(expected));
     187            1 :             CATCH_REQUIRE(now == expected + " plus a few days");
     188            1 :         }
     189              : 
     190              :         {
     191            1 :             std::allocator<char> allocator;
     192            3 :             std::string expected("not this");
     193            1 :             libutf8::case_insensitive_string now(get_date(expected), allocator);
     194            1 :             CATCH_REQUIRE(now == expected + " plus a few days");
     195            1 :         }
     196              :     }
     197            2 :     CATCH_END_SECTION()
     198              : 
     199            2 :     CATCH_START_SECTION("case_insensitive: Verify Case Insensitive String Comparators")
     200              :     {
     201              :         {
     202            3 :             libutf8::case_insensitive_string name1("Alexis");
     203            3 :             libutf8::case_insensitive_string name2("alexis");
     204            1 :             CATCH_REQUIRE(name1 == name2);
     205            1 :             CATCH_REQUIRE_FALSE(name1 != name2);
     206            1 :             CATCH_REQUIRE_FALSE(name1 > name2);
     207            1 :             CATCH_REQUIRE(name1 >= name2);
     208            1 :             CATCH_REQUIRE_FALSE(name1 < name2);
     209            1 :             CATCH_REQUIRE(name1 <= name2);
     210            1 :         }
     211              : 
     212              :         {
     213            3 :             libutf8::case_insensitive_string name1("Alexis");
     214            3 :             libutf8::case_insensitive_string name2("Wilke");
     215            1 :             CATCH_REQUIRE_FALSE(name1 == name2);
     216            1 :             CATCH_REQUIRE(name1 != name2);
     217            1 :             CATCH_REQUIRE_FALSE(name1 > name2);
     218            1 :             CATCH_REQUIRE_FALSE(name1 >= name2);
     219            1 :             CATCH_REQUIRE(name1 < name2);
     220            1 :             CATCH_REQUIRE(name1 <= name2);
     221            1 :         }
     222              : 
     223              :         {
     224            3 :             libutf8::case_insensitive_string name1("Alexis");
     225            3 :             std::string name2("alexis");
     226            1 :             CATCH_REQUIRE(name1 == name2);
     227            1 :             CATCH_REQUIRE_FALSE(name1 != name2);
     228            1 :             CATCH_REQUIRE_FALSE(name1 > name2);
     229            1 :             CATCH_REQUIRE(name1 >= name2);
     230            1 :             CATCH_REQUIRE_FALSE(name1 < name2);
     231            1 :             CATCH_REQUIRE(name1 <= name2);
     232            1 :         }
     233              : 
     234              :         {
     235            3 :             std::string name1("Alexis");
     236            3 :             libutf8::case_insensitive_string name2("Wilke");
     237            1 :             CATCH_REQUIRE_FALSE(name1 == name2);
     238            1 :             CATCH_REQUIRE(name1 != name2);
     239            1 :             CATCH_REQUIRE_FALSE(name1 > name2);
     240            1 :             CATCH_REQUIRE_FALSE(name1 >= name2);
     241            1 :             CATCH_REQUIRE(name1 < name2);
     242            1 :             CATCH_REQUIRE(name1 <= name2);
     243            1 :         }
     244              : 
     245              :         {
     246            3 :             libutf8::case_insensitive_string name1("Alexis");
     247            1 :             CATCH_REQUIRE(name1 == "alexis");
     248            1 :             CATCH_REQUIRE_FALSE(name1 != "alexis");
     249            1 :             CATCH_REQUIRE_FALSE(name1 > "alexis");
     250            1 :             CATCH_REQUIRE(name1 >= "alexis");
     251            1 :             CATCH_REQUIRE_FALSE(name1 < "alexis");
     252            1 :             CATCH_REQUIRE(name1 <= "alexis");
     253            1 :         }
     254              : 
     255              :         {
     256            3 :             libutf8::case_insensitive_string name2("Wilke");
     257            1 :             CATCH_REQUIRE_FALSE("Alexis" == name2);
     258            1 :             CATCH_REQUIRE("Alexis" != name2);
     259            1 :             CATCH_REQUIRE_FALSE("Alexis" > name2);
     260            1 :             CATCH_REQUIRE_FALSE("Alexis" >= name2);
     261            1 :             CATCH_REQUIRE("Alexis" < name2);
     262            1 :             CATCH_REQUIRE("Alexis" <= name2);
     263            1 :         }
     264              :     }
     265            2 :     CATCH_END_SECTION()
     266            2 : }
     267              : 
     268              : 
     269              : // vim: ts=4 sw=4 et
        

Generated by: LCOV version 2.0-1

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