jaulib v1.4.1-10-ga2c96e0
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
test_stringconv_from.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2025 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#include <cassert>
25#include <cstring>
26#include <limits>
27
28#include <jau/basic_types.hpp>
29#include <jau/cpp_lang_util.hpp>
30#include <jau/string_cfmt.hpp>
31#include <jau/string_util.hpp>
32#include <jau/test/catch2_ext.hpp>
34
35using namespace jau::int_literals;
36
37static void testToFrom(uint64_t exp_v, std::string_view exp_s) {
38 REQUIRE(exp_s == jau::to_string(exp_v, 10, jau::LoUpCase::lower, jau::PrefixOpt::none));
39 uint64_t v;
40 REQUIRE( true == jau::from_chars(v, exp_s) );
41 REQUIRE( exp_v == v );
42}
43
44template<typename value_type>
45static constexpr value_type testFrom(std::string_view exp_s) {
46 value_type v;
47 if( jau::from_chars(v, exp_s) ) {
48 return v;
49 }
50 std::cerr << "from_chars " << exp_s << " -> " << v << "\n";
51 return 0;
52}
53
54template<typename value_type>
56 std::string_view from;
57 value_type to;
58};
59
60TEST_CASE( "Test 01 - from_chars()", "[jau][string][toBitString]" ) {
61 {
62 int64_t v;
63 REQUIRE(false == jau::from_chars(v, "-9223372036854775808888888") );
64 REQUIRE(false == jau::from_chars(v, "9223372036854775808888888") );
65 }
66 {
67 REQUIRE( -1 == testFrom<int64_t>("-1"));
68 REQUIRE( 9 == testFrom<int64_t>("09.10"));
69
70 // NOLINTBEGIN
71 DataFromTo01<int64_t> data[] = {
72 {"0", 0}, {"1", 1}, {"2", 2}, {"3", 3}, {"4", 4}, {"5", 5}, {"6", 6}, {"7", 7}, {"8", 8}, {"9", 9},
73 {"-1", -1}, {"-2", -2}, {"-3", -3}, {"-4", -4}, {"-5", -5}, {"-6", -6}, {"-7", -7}, {"-8", -8}, {"-9", -9},
74 {"10", 10}, {"-10", -10}, {"123", 123}, {"-123", -123}, {"65432", 65432}, {"-65432", -65432},
75 {" -9223372036854775808 ", std::numeric_limits<int64_t>::min() },
76 {" 9223372036854775807 ", std::numeric_limits<int64_t>::max() }
77 };
78 // NOLINTEND
79
80 for(auto d : data) {
81 REQUIRE( d.to == testFrom<int64_t>(d.from));
82 }
83 }
84 {
85 // NOLINTBEGIN
86 DataFromTo01<uint64_t> data[] = {
87 {"0", 0}, {"1", 1}, {"2", 2}, {"3", 3}, {"4", 4}, {"5", 5}, {"6", 6}, {"7", 7}, {"8", 8}, {"9", 9},
88 {"10", 10}, {"123", 123}, {"65432", 65432},
89 { "9223372036854775807", std::numeric_limits<int64_t>::max() },
90 {"18446744073709551615", std::numeric_limits<uint64_t>::max() }
91 };
92 // NOLINTEND
93
94 for(auto d : data) {
95 REQUIRE( d.to == testFrom<uint64_t>(d.from));
96 }
97 for(auto d : data) {
98 testToFrom(d.to, d.from);
99 }
100 }
101}
102
103
std::string to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
constexpr bool from_chars(value_type &result, std::string_view str) noexcept
std::string_view from
static void testToFrom(uint64_t exp_v, std::string_view exp_s)
TEST_CASE("Test 01 - from_chars()", "[jau][string][toBitString]")
static constexpr value_type testFrom(std::string_view exp_s)