LCOV - code coverage report
Current view: top level - tests - catch_to_string_literal.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 24 24
Test Date: 2025-07-03 19:05:49 Functions: 100.0 % 1 1
Legend: Lines: hit not hit

            Line data    Source code
       1              : // Copyright (c) 2011-2025  Made to Order Software Corp.  All Rights Reserved
       2              : //
       3              : // https://snapwebsites.org/project/snapdev
       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              : /** \file
      20              :  * \brief Verify the to_string_literal templates.
      21              :  *
      22              :  * This file verifies that the to_string_literal template works as expected.
      23              :  */
      24              : 
      25              : // snapdev
      26              : //
      27              : #include    <snapdev/to_string_literal.h>
      28              : 
      29              : 
      30              : // self
      31              : //
      32              : #include    "catch_main.h"
      33              : 
      34              : 
      35              : // C++
      36              : //
      37              : #include    <iomanip>
      38              : 
      39              : 
      40              : 
      41              : namespace
      42              : {
      43              : 
      44              : // convert a port number to a string at compile time
      45              : //
      46              : constexpr int const         LOCAL_PORT = 9123;
      47              : constexpr std::string_view  g_port_decimal = snapdev::integer_to_string_literal<LOCAL_PORT>.data();
      48              : constexpr std::string_view  g_port_decimal_explicit = snapdev::integer_to_string_literal<LOCAL_PORT, 10>.data();
      49              : constexpr std::string_view  g_port_hexadecimal = snapdev::integer_to_string_literal<LOCAL_PORT, 16>.data();
      50              : constexpr std::string_view  g_port_hexadecimal_uppercase = snapdev::integer_to_string_literal<LOCAL_PORT, 16, true>.data();
      51              : constexpr std::string_view  g_port_octal = snapdev::integer_to_string_literal<LOCAL_PORT, 8>.data();
      52              : constexpr std::string_view  g_port_binary = snapdev::integer_to_string_literal<LOCAL_PORT, 2>.data();
      53              : 
      54              : // these require C++20, becaue double_wrapper() is not otherwise accepted as a type to pass values as is
      55              : //constexpr float const       CONSTANT_ROOT_TWO = 1.414213562373095048801688724209698078;
      56              : //constexpr double const      CONSTANT_PI = 3.141592653589793238462643383279502884;
      57              : //constexpr long double const CONSTANT_E = 2.718281828459045235360287471352662497;
      58              : //constexpr std::string_view  g_root_two = snapdev::floating_point_to_string_literal<snapdev::detail::double_wrapper(CONSTANT_ROOT_TWO)>.data();
      59              : //constexpr std::string_view  g_pi = snapdev::floating_point_to_string_literal<CONSTANT_PI>.data();
      60              : //constexpr std::string_view  g_e = snapdev::floating_point_to_string_literal<CONSTANT_E>.data();
      61              : 
      62              : }
      63              : 
      64              : 
      65            1 : CATCH_TEST_CASE("to_string_literal", "[string]")
      66              : {
      67            1 :     CATCH_START_SECTION("integer_to_string_literal: verify integral literals")
      68              :     {
      69            1 :         std::stringstream ds;
      70            1 :         ds << std::setbase(10) << LOCAL_PORT;
      71            3 :         CATCH_REQUIRE(ds.str() == std::string(g_port_decimal));
      72            3 :         CATCH_REQUIRE(ds.str() == std::string(g_port_decimal_explicit));
      73              : 
      74            1 :         std::stringstream hs;
      75            1 :         hs << std::setbase(16) << LOCAL_PORT;
      76            3 :         CATCH_REQUIRE(hs.str() == std::string(g_port_hexadecimal));
      77              : 
      78            1 :         std::stringstream hsu;
      79            1 :         hsu << std::uppercase << std::setbase(16) << LOCAL_PORT;
      80            3 :         CATCH_REQUIRE(hsu.str() == std::string(g_port_hexadecimal_uppercase));
      81              : 
      82            1 :         std::stringstream os;
      83            1 :         os << std::setbase(8) << LOCAL_PORT;
      84            3 :         CATCH_REQUIRE(os.str() == std::string(g_port_octal));
      85              : 
      86              :         // unfortunately, for some really odd reasons, the std::setbase()
      87              :         // is limited to 8, 10, and 16...
      88              :         //
      89            1 :         char binary[64] = {};
      90            1 :         std::size_t q(std::size(binary) - 1);
      91           15 :         for(auto p(LOCAL_PORT); p != 0; p /= 2)
      92              :         {
      93           14 :             --q;
      94           14 :             binary[q] = (p & 1) + '0';
      95              :         }
      96            5 :         CATCH_REQUIRE(std::string(binary + q) == std::string(g_port_binary));
      97            1 :     }
      98            1 :     CATCH_END_SECTION()
      99              : 
     100              : // this requires C++20 to compile...
     101              : //
     102              : //    CATCH_START_SECTION("floating_point_to_string_literal: verify float literals")
     103              : //    {
     104              : ////constexpr float const       CONSTANT_ROOT_TWO = 1.414213562373095048801688724209698078;
     105              : ////constexpr double const      CONSTANT_PI = 3.141592653589793238462643383279502884;
     106              : ////constexpr long double const CONSTANT_E = 2.718281828459045235360287471352662497;
     107              : ////constexpr std::string_view  g_root_two = snapdev::floating_point_to_string_literal(CONSTANT_ROOT_TWO);
     108              : ////constexpr std::string_view  g_pi = snapdev::floating_point_to_string_literal(CONSTANT_PI);
     109              : ////constexpr std::string_view  g_e = snapdev::floating_point_to_string_literal(CONSTANT_E);
     110              : //
     111              : //        std::stringstream ds;
     112              : //        ds << std::setprecision(12) << CONSTANT_ROOT_TWO;
     113              : //        CATCH_REQUIRE(ds.str() == std::string(g_root_two));
     114              : //    }
     115              : //    CATCH_END_SECTION()
     116            1 : }
     117              : 
     118              : 
     119              : // vim: ts=4 sw=4 et
        

Generated by: LCOV version 2.0-1

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