Line data Source code
1 : // Copyright (c) 2021-2024 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 various \<number>_to_string() functions. 21 : * 22 : * This checks a few transformations to make sure they work as expected. 23 : * 24 : * The supported functions are: 25 : * 26 : * \li integer_to_string() 27 : */ 28 : 29 : // self 30 : // 31 : #include "catch_main.h" 32 : 33 : 34 : 35 : // snapdev 36 : // 37 : #include <snapdev/number_to_string.h> 38 : 39 : 40 : // last include 41 : // 42 : #include <snapdev/poison.h> 43 : 44 : 45 : // __int128 is not ISO C++ yet 46 : #pragma GCC diagnostic ignored "-Wpedantic" 47 : 48 : 49 1 : CATCH_TEST_CASE("number_to_string(int)", "[string]") 50 : { 51 1 : CATCH_START_SECTION("number_to_string(int): convert integers (int) to a string") 52 : { 53 1001 : for(int i(0); i < 1000; ++i) 54 : { 55 1000 : int n(rand()); 56 : { // default base 57 1000 : std::string const result(snapdev::integer_to_string(n)); 58 1000 : std::string const expected(std::to_string(n)); 59 1000 : CATCH_REQUIRE(expected == result); 60 1000 : } 61 : { // explicit base 10 62 1000 : std::string const result(snapdev::integer_to_string(n, 10)); 63 1000 : std::string const expected(std::to_string(n)); 64 1000 : CATCH_REQUIRE(expected == result); 65 1000 : } 66 : { // base 8 67 1000 : std::string const result(snapdev::integer_to_string(n, 8)); 68 1000 : std::stringstream ss; 69 1000 : ss << std::oct << n; 70 1000 : std::string const expected(ss.str()); 71 1000 : CATCH_REQUIRE(expected == result); 72 1000 : } 73 : { // base 16 -- lowercase (default) 74 1000 : std::string const result(snapdev::integer_to_string(n, 16)); 75 1000 : std::stringstream ss; 76 1000 : ss << std::hex << n; 77 1000 : std::string const expected(ss.str()); 78 1000 : CATCH_REQUIRE(expected == result); 79 1000 : } 80 : { // base 16 -- lowercase (explicit) 81 1000 : std::string const result(snapdev::integer_to_string(n, 16, false)); 82 1000 : std::stringstream ss; 83 1000 : ss << std::hex << n; 84 1000 : std::string const expected(ss.str()); 85 1000 : CATCH_REQUIRE(expected == result); 86 1000 : } 87 : { // base 16 -- uppercase 88 1000 : std::string const result(snapdev::integer_to_string(n, 16, true)); 89 1000 : std::stringstream ss; 90 1000 : ss << std::hex << std::uppercase << n; 91 1000 : std::string const expected(ss.str()); 92 1000 : CATCH_REQUIRE(expected == result); 93 1000 : } 94 : } 95 : } 96 1 : CATCH_END_SECTION() 97 1 : } 98 : 99 : 100 : 101 : // vim: ts=4 sw=4 et