Gamp v0.0.7-67-g7798ac4
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
test_stringconv02.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2025-2026 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
37template<typename value_type>
38static void testTo(int line, value_type v, std::string_view exp_s,
39 uint32_t radix = 10, jau::LoUpCase capitalization = jau::LoUpCase::lower, jau::PrefixOpt prefix = jau::PrefixOpt::prefix,
40 const uint32_t min_width = 0, const char separator = 0, const char padding = '0')
41{
42 std::string has_s = jau::to_string(v, radix, capitalization, prefix, min_width, separator, padding);
43 std::cerr << "line " << line << ": v '" << v << ", radix " << radix
44 << ", exp_s '" << exp_s << "' (l " << exp_s.length()
45 << "), has_s '" << has_s << "' (l " << has_s.length() << ", c " << has_s.capacity()
46 << "), match " << (exp_s == has_s) << "\n";
47 CHECK( exp_s.length() == has_s.length() );
48 REQUIRE( exp_s == has_s );
49}
50template<typename value_type>
51static void testToFrom(int line, value_type exp_v, std::string_view exp_s, std::string_view in_s,
52 uint32_t radix = 10, jau::LoUpCase capitalization = jau::LoUpCase::lower, jau::PrefixOpt prefix = jau::PrefixOpt::prefix,
53 const uint32_t min_width = 0, const char separator = 0, const char padding = '0')
54{
55 testTo(line, exp_v, exp_s, radix, capitalization, prefix, min_width, separator, padding);
56 value_type v;
57 auto [consumed, ok] = jau::fromIntString(v, in_s, radix, separator);
58 std::cerr << "line " << line << ": exp_v " << exp_v << ", in_s '" << in_s << "', radix " << radix
59 << ": ok " << ok << ", consumed " << consumed << " (match " << (consumed == in_s.length()) << "), value " << v << " (match " << (exp_v == v) << ")\n";
60 REQUIRE( true == ok );
61 REQUIRE( exp_v == v );
62 // REQUIRE( exp_s.length() == consumed );
63}
64
65template<typename value_type>
67 std::string_view from;
68 value_type to;
69};
70
71TEST_CASE( "Test 01 - from_chars()", "[jau][string][toBitString]" ) {
72 {
73 int64_t v;
74 REQUIRE(false == jau::fromIntString(v, "").b ); // empty
75 REQUIRE(true == jau::fromIntString(v, " 123").b ); // leading space
76 REQUIRE(true == jau::fromIntString(v, "123 ").b ); // space tail OK
77 REQUIRE(123 == v);
78 REQUIRE(false == jau::fromIntString(v, "XXDK123").b ); // leading garbage
79 REQUIRE(true == jau::fromIntString(v, "123SJKXNC").b ); // garbage tail OK
80 REQUIRE(123 == v);
81 REQUIRE(false == jau::fromIntString(v, "-9223372036854775808888888").b ); // underflow
82 REQUIRE(false == jau::fromIntString(v, "9223372036854775808888888").b ); // overflow
83 }
84 {
85 testToFrom<int64_t>(__LINE__, -1, "-1", "-1");
86 testToFrom<int64_t>(__LINE__, 9, "9", "09.10");
87
88 // NOLINTBEGIN
89 DataFromTo01<int64_t> data[] = {
90 {"0", 0}, {"1", 1}, {"2", 2}, {"3", 3}, {"4", 4}, {"5", 5}, {"6", 6}, {"7", 7}, {"8", 8}, {"9", 9},
91 {"-1", -1}, {"-2", -2}, {"-3", -3}, {"-4", -4}, {"-5", -5}, {"-6", -6}, {"-7", -7}, {"-8", -8}, {"-9", -9},
92 {"10", 10}, {"-10", -10}, {"123", 123}, {"-123", -123}, {"65432", 65432}, {"-65432", -65432},
93 {"-9223372036854775808", std::numeric_limits<int64_t>::min() },
94 {"9223372036854775807", std::numeric_limits<int64_t>::max() }
95 };
96 // NOLINTEND
97
98 for(auto d : data) {
99 testToFrom(__LINE__, d.to, d.from, d.from);
100 }
101 }
102 {
103 // NOLINTBEGIN
104 DataFromTo01<uint64_t> data[] = {
105 {"0", 0}, {"1", 1}, {"2", 2}, {"3", 3}, {"4", 4}, {"5", 5}, {"6", 6}, {"7", 7}, {"8", 8}, {"9", 9},
106 {"10", 10}, {"123", 123}, {"65432", 65432},
107 { "9223372036854775807", std::numeric_limits<int64_t>::max() },
108 {"18446744073709551615", std::numeric_limits<uint64_t>::max() }
109 };
110 // NOLINTEND
111
112 for(auto d : data) {
113 testToFrom(__LINE__, d.to, d.from, d.from);
114 }
115 }
116}
117
118
std::string_view to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
constexpr SizeBoolPair fromIntString(value_type &result, std::string_view str, uint32_t radix=10, const char separator=0) noexcept
Converts a given integer string representation to the given result reference, compatible with ::strto...
std::string_view from
static void testTo(int line, value_type v, std::string_view exp_s, uint32_t radix=10, jau::LoUpCase capitalization=jau::LoUpCase::lower, jau::PrefixOpt prefix=jau::PrefixOpt::prefix, const uint32_t min_width=0, const char separator=0, const char padding='0')
TEST_CASE("Test 01 - from_chars()", "[jau][string][toBitString]")
static void testToFrom(int line, value_type exp_v, std::string_view exp_s, std::string_view in_s, uint32_t radix=10, jau::LoUpCase capitalization=jau::LoUpCase::lower, jau::PrefixOpt prefix=jau::PrefixOpt::prefix, const uint32_t min_width=0, const char separator=0, const char padding='0')