Gamp v0.0.7-67-g7798ac4
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
test_stringconv01.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020-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
27#include "jau/base_math.hpp"
28#include "test_datatype01.hpp"
29
30#include <jau/basic_types.hpp>
31#include <jau/cpp_lang_util.hpp>
32#include <jau/string_cfmt.hpp>
33#include <jau/string_util.hpp>
34#include <jau/test/catch2_ext.hpp>
36
37typedef std::vector<int> std_vec_int;
38
39typedef std_vec_int::iterator std_vec_int_iter;
40
41typedef std_vec_int::const_iterator std_vec_int_citer;
42
43typedef std_vec_int_citer::pointer std_vec_int_citer_pointer;
44
45typedef decltype( std::declval<std_vec_int_citer>().operator->() ) std_vec_int_citer_ptrop_retval;
46
47using namespace jau::int_literals;
48
49template<typename value_type>
50static void testDecTo(int line, value_type v, std::string_view exp_s,
51 const uint32_t min_width = 0, const char separator = 0)
52{
53 std::string has1_s = jau::to_string(v, 10, jau::LoUpCase::lower, jau::PrefixOpt::none, min_width, separator, ' ');
54 std::string has2_s = jau::to_decstring(v, separator, min_width);
55 std::cerr << "line " << line << ": v '" << v
56 << ", exp_s '" << exp_s << "' (l " << exp_s.length()
57 << "), has1_s '" << has1_s << "' (l " << has1_s.length() << ", c " << has1_s.capacity() << ", m " << (exp_s == has1_s)
58 << "), has2_s '" << has2_s << "' (l " << has2_s.length() << ", c " << has2_s.capacity() << ", m " << (exp_s == has2_s)
59 << ")\n";
60 CHECK( exp_s.length() == has1_s.length() );
61 CHECK( exp_s.length() == has2_s.length() );
62 REQUIRE( exp_s == has1_s );
63 REQUIRE( exp_s == has2_s );
64}
65
66template<typename value_type>
67static void testTo(int line, value_type v, std::string_view exp_s,
69 const uint32_t min_width = 0, const char separator = 0, const char padding = '0')
70{
71 if( radix == 10 && padding == ' ' ) {
72 testDecTo(line, v, exp_s, min_width, separator);
73 }
74 std::string has1_s = jau::to_string(v, radix, capitalization, prefix, min_width, separator, padding);
76 bool use_cfm=false;
77 {
79 opts.addFlag('#');
80 }
81 if (padding == '0') {
82 opts.addFlag('0');
83 }
84 if (separator == '\'' || separator == ',') {
85 opts.addFlag(separator);
86 }
87 if( min_width > 0 ) {
88 opts.setWidth(min_width);
89 }
90 if( sizeof(v) >= sizeof(uint64_t)) {
92 }
93 if ( jau::is_positive(v)) {
94 switch (radix) {
95 case 16: use_cfm=true; opts.setConversion(capitalization==jau::LoUpCase::lower ? 'x' : 'X'); break;
96 case 10: use_cfm=true; opts.setConversion('u'); break;
97 case 8: use_cfm=true; opts.setConversion('o'); break;
98 case 2: use_cfm=true; opts.setConversion('b'); break;
99 default: break;
100 }
101 } else {
102 if( radix==10) {
103 use_cfm=true;
104 opts.setConversion('d');
105 }
106 }
107 }
108 std::string fmt2 = opts.toFormat();
109 std::string has2_s = use_cfm ? jau::cfmt::format(fmt2, v) : "";
110 std::cerr << "line " << line << ": v '" << v << ", radix " << radix
111 << ", exp_s '" << exp_s << "' (l " << exp_s.length()
112 << "), has1_s '" << has1_s << "' (l " << has1_s.length() << ", c " << has1_s.capacity() << ", m " << (exp_s == has1_s) << ")"
113 ;
114 if(use_cfm) {
115 std::cerr << ", has2_s '" << has2_s << "' (l " << has2_s.length() << ", c " << has2_s.capacity() << ", m " << (exp_s == has1_s)
116 << ", fmt2 '" << fmt2 << ", " << opts.toString() << ")";
117 }
118 std::cerr << "\n";
119 CHECK( exp_s.length() == has1_s.length() );
120 REQUIRE( exp_s == has1_s );
121 if(use_cfm) {
122 CHECK( exp_s.length() == has2_s.length() );
123 REQUIRE( exp_s == has2_s );
124 }
125}
126template<typename value_type>
127static void testToFrom(int line, value_type exp_v, std::string_view exp_s, std::string_view in_s,
129 const uint32_t min_width = 0, const char separator = 0, const char padding = '0')
130{
131 testTo(line, exp_v, exp_s, radix, capitalization, prefix, min_width, separator, padding);
132 value_type v;
133 auto [consumed, ok] = jau::fromIntString(v, in_s, radix, separator);
134 std::cerr << "line " << line << ": exp_v " << exp_v << ", in_s '" << in_s << "', radix " << radix
135 << ": ok " << ok << ", consumed " << consumed << " (match " << (consumed == in_s.length()) << "), value " << v << " (match " << (exp_v == v) << ")\n";
136 REQUIRE( true == ok );
137 REQUIRE( exp_v == v );
138 // REQUIRE( exp_s.length() == consumed );
139}
140template<typename value_type>
141static void testToFrom(int line, value_type exp_v, const std::string& exp_s,
143 const uint32_t min_width = 0, const char separator = 0, const char padding = '0')
144{
145 testToFrom(line, exp_v, exp_s, exp_s, radix, capitalization, prefix, min_width, separator, padding);
146}
147
148template<typename value_type>
149static void testFrom(int line, value_type exp_v, std::string_view in_s,uint32_t radix=10, const char separator = 0)
150{
151 value_type v;
152 auto [consumed, ok] = jau::fromIntString(v, in_s, radix, separator);
153 std::cerr << "line " << line << ": exp_v " << exp_v << ", in_s '" << in_s << "', radix " << radix
154 << ": ok " << ok << ", consumed " << consumed << " (match " << (consumed == in_s.length()) << "), value " << v << " (match " << (exp_v == v) << ")\n";
155 REQUIRE( true == ok );
156 REQUIRE( exp_v == v );
157 // REQUIRE( in_s.length() == consumed );
158}
159
160TEST_CASE( "Test 00 - to_string/appendIntString, fromIntString", "[jau][string][to_string][from_string]" ) {
161 int i1 = 1;
162 uint64_t u64_1 = 1116791496961ull;
163 void * p_v_1 = (void *)0xAFFE;
164 float float_1 = 1.65f;
165
166 Addr48Bit addr48bit_1(u64_1);
167
168 CHECK("1" == jau::to_string<int>(i1));
169 CHECK("1116791496961" == jau::to_string(u64_1));
170 CHECK("0xaffe" == jau::to_string(p_v_1));
171 CHECK("0xaffe" == jau::toHexString(0xaffe_u32));
172 {
173 // radix, default: no-width, prefix, no-separator, no padding
174 testToFrom(__LINE__, 0xdeadbeef_u32, "0xdeadbeef", 16); // hex
175 testToFrom(__LINE__, 0xdeadbeef_u32, "0xdead'beef", " 0x'dead'beef la", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // hex
176
177 testToFrom(__LINE__, 876543210_u64, "876543210", 10); // dec
178 testToFrom(__LINE__, 876543210_u64, "876'543'210", " '876'543'210 la", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // dec
179
180 testToFrom(__LINE__, 077652_u32, "077652", 8); // oct
181 testToFrom(__LINE__, 077652_u32, "07'7652", " 07'7652 la", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // oct
182
183 testToFrom(__LINE__, 0b11010101101_u32, "0b110'1010'1101", " 0b'110'1010'1101 la", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // bin
184
185 // no-prefix, radix, default: no-width, no-separator, no padding
186 testToFrom(__LINE__, 0xaffe_u32, "affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none); // hex
187 testToFrom(__LINE__, 0x1affe_u32, "1affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none); // hex
188 testToFrom(__LINE__, 876543210_u64, "876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none); // dec
189 testToFrom(__LINE__, 1876543210_u64, "1876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none); // dec
190 testToFrom(__LINE__, 043217652_u32, "43217652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none); // oct
191 testToFrom(__LINE__, 0143217652_u32, "143217652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none); // oct
192 testToFrom(__LINE__, 0b11010101101_u32, "11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none); // bin
193 testToFrom(__LINE__, 0b111010101101_u32, "111010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none); // bin
194
195 // radix, width-expansion, default: prefix, no-separator, '0' padding
196 testToFrom(__LINE__, 0xaffe_u32, "0x00affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 8); // hex
197 testToFrom(__LINE__, 0x1affe_u32, "0x01affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 8); // hex
198 testToFrom(__LINE__, 876543210_u64, "000876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 12); // dec
199 testToFrom(__LINE__, 1876543210_u64, "001876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 12); // dec
200 testToFrom(__LINE__, 043217652_u32, "0043217652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 10); // oct
201 testToFrom(__LINE__, 0143217652_u32, "0143217652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 10); // oct
202 testToFrom(__LINE__, 0b11010101101_u32, "0b00011010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 16); // bin
203 testToFrom(__LINE__, 0b111010101101_u32, "0b00111010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 16); // bin
204
205 // no-prefix, radix, width-expansion, default: no-separator, '0' padding
206 testToFrom(__LINE__, 0xaffe_u32, "0000affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 8); // hex
207 testToFrom(__LINE__, 0x1affe_u32, "0001affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 8); // hex
208 testToFrom(__LINE__, 876543210_u64, "000876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 12); // dec
209 testToFrom(__LINE__, 1876543210_u64, "001876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 12); // dec
210 testToFrom(__LINE__, 043217652_u32, "0043217652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 10); // oct
211 testToFrom(__LINE__, 0143217652_u32, "0143217652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 10); // oct
212 testToFrom(__LINE__, 0b11010101101_u32, "0000011010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 16); // bin
213 testToFrom(__LINE__, 0b111010101101_u32, "0000111010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 16); // bin
214
215 // radix, separator, default: no-width, prefix, '0' padding
216 testToFrom(__LINE__, 0xaffe_u32, "0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // hex
217 testToFrom(__LINE__, 0x1affe_u32, "0x1'affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // hex
218 testToFrom(__LINE__, 876543210_u64, "876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // dec
219 testToFrom(__LINE__, 1876543210_u64, "1'876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // dec
220 testToFrom(__LINE__, 043217652_u32, "04321'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // oct
221 testToFrom(__LINE__, 0143217652_u32, "01'4321'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // oct
222 testToFrom(__LINE__, 0b10101101_u32, "0b1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // bin
223 testToFrom(__LINE__, 0b110101101_u32, "0b1'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // bin
224
225 // no-prefix, radix, separator, default: no-width, '0' padding
226 testToFrom(__LINE__, 0xaffe_u32, "affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // hex
227 testToFrom(__LINE__, 0x1affe_u32, "1'affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // hex
228 testToFrom(__LINE__, 876543210_u64, "876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // dec
229 testToFrom(__LINE__, 1876543210_u64, "1'876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // dec
230 testToFrom(__LINE__, 043217652_u32, "4321'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // oct
231 testToFrom(__LINE__, 0143217652_u32, "1'4321'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // oct
232 testToFrom(__LINE__, 0b10101101_u32, "1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // bin
233 testToFrom(__LINE__, 0b110101101_u32, "1'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // bin
234
235 // radix, width-expansion, separator, default: prefix, '0' padding
236 testToFrom(__LINE__, 0xaffe_u32, "0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 6, '\''); // hex
237 testToFrom(__LINE__, 0xaffe_u32, "0x'affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 7, '\''); // hex
238 testToFrom(__LINE__, 0xaffe_u32, "0x0'affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 8, '\''); // hex
239
240 testToFrom(__LINE__, 876543210_u64, "876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 11, '\''); // dec
241 testToFrom(__LINE__, 876543210_u64, "'876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 12, '\''); // dec
242 testToFrom(__LINE__, 876543210_u64, "0'876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 13, '\''); // dec
243
244 testToFrom(__LINE__, 07652_u32, "07652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 5, '\''); // oct
245 testToFrom(__LINE__, 07652_u32, "0'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 6, '\''); // oct
246 testToFrom(__LINE__, 07652_u32, "00'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 7, '\''); // oct
247
248 testToFrom(__LINE__, 0b111010101101_u32, "0b1110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 16, '\''); // bin
249 testToFrom(__LINE__, 0b111010101101_u32, "0b'1110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 17, '\''); // bin
250 testToFrom(__LINE__, 0b111010101101_u32, "0b0'1110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 18, '\''); // bin
251
252 // no-prefix, radix, width-expansion, separator, default: '0' padding
253 testToFrom(__LINE__, 0xaffe_u32, "affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 4, '\''); // hex
254 testToFrom(__LINE__, 0xaffe_u32, "'affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 5, '\''); // hex
255 testToFrom(__LINE__, 0xaffe_u32, "0'affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 6, '\''); // hex
256
257 testToFrom(__LINE__, 876543210_u64, "876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 11, '\''); // dec
258 testToFrom(__LINE__, 876543210_u64, "'876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 12, '\''); // dec
259 testToFrom(__LINE__, 876543210_u64, "0'876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 13, '\''); // dec
260
261 testToFrom(__LINE__, 07652_u32, "7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 4, '\''); // oct
262 testToFrom(__LINE__, 07652_u32, "'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 5, '\''); // oct
263 testToFrom(__LINE__, 07652_u32, "0'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 6, '\''); // oct
264
265 testToFrom(__LINE__, 0b111010101101_u32, "1110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 14, '\''); // bin
266 testToFrom(__LINE__, 0b111010101101_u32, "'1110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 15, '\''); // bin
267 testToFrom(__LINE__, 0b111010101101_u32, "0'1110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 16, '\''); // bin
268
269 // Also testing to_decstring() due to radix==10 and padding==' '
270
271 //
272 // a.b.c radix, no-width, space padding ' ', [prefix], [separator], [signed]
273 // |
274 // 0 - unsigned
275 // 1 - signed
276 // |
277 // 0 = no-separator
278 // 1 = separator
279 // |
280 // 0 - no-prefix,
281 // 1 - prefix,
282
283 // 0.0.0 unsigned, no-prefix, radix, space padding ' ', default: no-width, no-separator
284 testToFrom(__LINE__, 0xaffe_u32, "affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // hex
285 testToFrom(__LINE__, 876543210_u64, "876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // dec
286 testToFrom(__LINE__, 077652_u32, "77652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // oct
287 testToFrom(__LINE__, 0b11010101101_u32, "11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // bin
288
289 // 0.0.1 signed, no-prefix, radix, space padding ' ', default: no-width, no-separator
290 testToFrom(__LINE__, -0xaffe_i32, "-affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // hex
291 testToFrom(__LINE__, -876543210_i64, "-876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // dec
292 testToFrom(__LINE__, -077652_i32, "-77652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // oct
293 testToFrom(__LINE__, -0b11010101101_i32, "-11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // bin
294
295 // 0.1.0 unsigned, no-prefix, radix, separator, space padding ' ', default: no-width
296 testToFrom(__LINE__, 0xaffe_u32, "affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // hex
297 testToFrom(__LINE__, 876543210_u64, "876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // dec
298 testToFrom(__LINE__, 077652_u32, "7'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // oct
299 testToFrom(__LINE__, 0b11010101101_u32, "110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // bin
300
301 // 0.1.1 signed, no-prefix, radix, separator, space padding ' ', default: no-width
302 testToFrom(__LINE__, -0xaffe_i32, "-affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // hex
303 testToFrom(__LINE__, -876543210_i64, "-876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // dec
304 testToFrom(__LINE__, -077652_i32, "-7'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // oct
305 testToFrom(__LINE__, -0b11010101101_i32, "-110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // bin
306
307 // 1.0.0 unsigned, radix, space padding ' ', default: prefix, no-width, no-separator
308 testToFrom(__LINE__, 0xaffe_u32, "0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // hex
309 testToFrom(__LINE__, 876543210_u64, "876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // dec
310 testToFrom(__LINE__, 077652_u32, "077652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // oct
311 testToFrom(__LINE__, 0b11010101101_u32, "0b11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // bin
312
313 // 1.0.1 signed, radix, space padding ' ', default: prefix, no-width, no-separator
314 testToFrom(__LINE__, -0xaffe_i32, "-0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // hex
315 testToFrom(__LINE__, -876543210_i64, "-876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // dec
316 testToFrom(__LINE__, -077652_i32, "-077652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // oct
317 testToFrom(__LINE__, -0b11010101101_i32, "-0b11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // bin
318
319 // 1.1.0 unsigned, radix, separator, space padding ' ', default: prefix, no-width
320 testToFrom(__LINE__, 0xaffe_u32, "0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // hex
321 testToFrom(__LINE__, 876543210_u64, "876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // dec
322 testToFrom(__LINE__, 077652_u32, "07'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // oct
323 testToFrom(__LINE__, 0b11010101101_u32, "0b110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // bin
324
325 // 1.1.1 signed, radix, separator, space padding ' ', default: prefix, no-width
326 testToFrom(__LINE__, -0xaffe_i32, "-0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // hex
327 testToFrom(__LINE__, -876543210_i64, "-876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // dec
328 testToFrom(__LINE__, -077652_i32, "-07'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // oct
329 testToFrom(__LINE__, -0b11010101101_i32, "-0b110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // bin
330
331 //
332 // a.b.c radix, width-expansion, space padding ' ', [prefix], [separator], [signed]
333 // |
334 // 0 - unsigned
335 // 1 - signed
336 // |
337 // 0 = no-separator
338 // 1 = separator
339 // |
340 // 0 - no-prefix,
341 // 1 - prefix,
342
343 // 0.0.0 unsigned, no-prefix, radix, width-expansion, space padding ' ', default: no-separator
344 testToFrom(__LINE__, 0xaffe_u32, " affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 8, 0, ' '); // hex
345 testToFrom(__LINE__, 876543210_u64, " 876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 15, 0, ' '); // dec
346 testToFrom(__LINE__, 077652_u32, " 77652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 10, 0, ' '); // oct
347 testToFrom(__LINE__, 0b11010101101_u32, " 11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 17, 0, ' '); // bin
348
349 // 0.0.1 signed, no-prefix, radix, width-expansion, space padding ' ', default: no-separator
350 testToFrom(__LINE__, -0xaffe_i32, " -affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 8, 0, ' '); // hex
351 testToFrom(__LINE__, -876543210_i64, " -876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 15, 0, ' '); // dec
352 testToFrom(__LINE__, -077652_i32, " -77652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 10, 0, ' '); // oct
353 testToFrom(__LINE__, -0b11010101101_i32, " -11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 17, 0, ' '); // bin
354
355 // 0.1.0 unsigned, no-prefix, radix, width-expansion, separator, space padding ' '
356 testToFrom(__LINE__, 0xaffe_u32, " affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 8, '\'', ' '); // hex
357 testToFrom(__LINE__, 876543210_u64, " 876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 15, '\'', ' '); // dec
358 testToFrom(__LINE__, 077652_u32, " 7'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 10, '\'', ' '); // oct
359 testToFrom(__LINE__, 0b11010101101_u32, " 110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 17, '\'', ' '); // bin
360
361 // 0.1.1 signed, no-prefix, radix, width-expansion, separator, space padding ' '
362 testToFrom(__LINE__, -0xaffe_i32, " -affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 8, '\'', ' '); // hex
363 testToFrom(__LINE__, -876543210_i64, " -876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 15, '\'', ' '); // dec
364 testToFrom(__LINE__, -077652_i32, " -7'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 10, '\'', ' '); // oct
365 testToFrom(__LINE__, -0b11010101101_i32, " -110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 17, '\'', ' '); // bin
366
367 // 1.0.0 unsigned, radix, width-expansion, space padding ' ', default: prefix, no-separator
368 testToFrom(__LINE__, 0xaffe_u32, " 0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 8, 0, ' '); // hex
369 testToFrom(__LINE__, 876543210_u64, " 876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 15, 0, ' '); // dec
370 testToFrom(__LINE__, 077652_u32, " 077652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 10, 0, ' '); // oct
371 testToFrom(__LINE__, 0b11010101101_u32, " 0b11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 17, 0, ' '); // bin
372
373 // 1.0.1 signed, radix, width-expansion, space padding ' ', default: prefix, no-separator
374 testToFrom(__LINE__, -0xaffe_i32, " -0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 8, 0, ' '); // hex
375 testToFrom(__LINE__, -876543210_i64, " -876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 15, 0, ' '); // dec
376 testToFrom(__LINE__, -077652_i32, " -077652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 10, 0, ' '); // oct
377 testToFrom(__LINE__, -0b11010101101_i32, " -0b11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 17, 0, ' '); // bin
378
379 // 1.1.0 unsigned, radix, width-expansion, separator, space padding ' '
380 testToFrom(__LINE__, 0xaffe_u32, " 0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 8, '\'', ' '); // hex
381 testToFrom(__LINE__, 876543210_u64, " 876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 15, '\'', ' '); // dec
382 testToFrom(__LINE__, 077652_u32, " 07'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 10, '\'', ' '); // oct
383 testToFrom(__LINE__, 0b11010101101_u32, " 0b110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 17, '\'', ' '); // bin
384
385 // 1.1.1 signed, radix, width-expansion, separator, space padding ' '
386 testToFrom(__LINE__, -0xaffe_i32, " -0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 8, '\'', ' '); // hex
387 testToFrom(__LINE__, -876543210_i64, " -876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 15, '\'', ' '); // dec
388 testToFrom(__LINE__, -077652_i32, " -07'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 10, '\'', ' '); // oct
389 testToFrom(__LINE__, -0b11010101101_i32, " -0b110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 17, '\'', ' '); // bin
390
391 }
392 CHECK("1.650000" == jau::to_string(float_1));
393
394 CHECK("01:04:05:F5:E1:01" == jau::to_string(addr48bit_1));
395
396 //
397 // Validating 'pointer std::vector::const_iterator.operator->()'
398 // and the to_string type trait logic of it.
399 //
400
401 // jau::type_cue<std_vec_int_citer>::print("std_vec_int_citer", jau::TypeTraitGroup::ALL);
402 // jau::type_cue<std_vec_int_citer_pointer>::print("std_vec_int_citer_pointer", jau::TypeTraitGroup::ALL);
403
404 // jau::type_cue<std_vec_int_citer_ptrop_retval>::print("std_vec_int_citer_ptrop_retval", jau::TypeTraitGroup::ALL);
405 printf("jau::has_member_of_pointer<std_vec_int_citer>) %d\n", jau::has_member_of_pointer<std_vec_int_citer>::value);
406
407 std_vec_int vec_int_1;
408 vec_int_1.push_back(1); vec_int_1.push_back(2); vec_int_1.push_back(3);
409 std_vec_int_citer vec_int_citer_1B = vec_int_1.cbegin();
410 uint8_t* vec_int_citer_1B_ptr = (uint8_t*)(vec_int_citer_1B.operator->());
411 std::string vec_int_citer_1B_str = jau::toHexString(vec_int_citer_1B_ptr);
412
413 std_vec_int_citer vec_int_citer_1E = vec_int_1.cend();
414 uint8_t* vec_int_citer_1E_ptr = (uint8_t*)(vec_int_citer_1E.operator->());
415 std::string vec_int_citer_1E_str = jau::toHexString(vec_int_citer_1E_ptr);
416
417 std::ptrdiff_t vec_int_citer_1E_1B_ptrdiff = vec_int_citer_1E_ptr - vec_int_citer_1B_ptr;
418 size_t vec_int_citer_1E_1B_ptr_count = vec_int_citer_1E_1B_ptrdiff / sizeof(int);
419 size_t vec_int_citer_1E_1B_itr_count = vec_int_citer_1E - vec_int_citer_1B;
420
421 printf("vec_int_citer_1E - vec_int_citer_1B = itr_count %zu, ptr_count %zu\n",
422 vec_int_citer_1E_1B_itr_count, vec_int_citer_1E_1B_ptr_count);
423 printf("vec_int_citer_1E - vec_int_citer_1B = %zu\n", vec_int_citer_1E_1B_itr_count);
424 printf("vec_int_citer_1B_ptr %s, vec_int_citer_1E1_ptr = %s\n", vec_int_citer_1B_str.c_str(), vec_int_citer_1E_str.c_str());
425
426 CHECK(vec_int_citer_1E_1B_itr_count == 3);
427 CHECK(vec_int_citer_1E_1B_itr_count == vec_int_citer_1E_1B_ptr_count);
428
429 CHECK(vec_int_citer_1E_str == jau::to_string(vec_int_citer_1E));
430}
431
432#if 0
433TEST_CASE( "Test 01 - to_string(radix)", "[jau][string][to_string(radix)]" ) {
434 REQUIRE( true == true );
435}
436
437TEST_CASE( "Test 02 - toHexString()", "[jau][string][toHexString]" ) {
438 REQUIRE( true == true );
439}
440#endif
441
442static void testToBitString(std::string_view prefix, std::string_view exp_be_s, const uint64_t &exp_be_v, size_t max_bits, bool check_value=true) {
443 std::cout << prefix << ": max_bits " << max_bits << "\n";
444 std::string has_be_s1 = jau::toBitString(exp_be_v, jau::bit_order_t::msb, jau::PrefixOpt::none, max_bits);
445 std::cout << " exp_be_s : " << exp_be_s << "\n";
446 std::cout << " has_be_s1: " << has_be_s1 << "\n";
447 REQUIRE( exp_be_s == has_be_s1 );
448
449 if( check_value ) {
450 const auto [has_be_v, len_be, ok_be] = jau::fromBitString(exp_be_s);
451 REQUIRE(true == ok_be);
452 REQUIRE(exp_be_s.size() == len_be);
453 std::string has_be_s2 = jau::toBitString(has_be_v, jau::bit_order_t::msb, jau::PrefixOpt::none, max_bits);
454 std::cout << " has_be_s2: " << has_be_s2 << "\n";
455 REQUIRE(exp_be_v == has_be_v);
456 }
457}
458static void testToBitString(std::string_view prefix, std::string_view s_be1, const uint32_t &v_be1) {
459 testToBitString(prefix, s_be1, v_be1, s_be1.size(), true);
460}
461
462TEST_CASE( "Test 03 - toBitString()", "[jau][string][toBitString]" ) {
463 {
464 testToBitString("Test 03.01.01", "000101100101110111011001", 0b101100101110111011001_u64, 0);
465 testToBitString("Test 03.01.02", "000101100101110111011001", 0b101100101110111011001_u64);
466 testToBitString("Test 03.01.03", "101110111011001", 0b101100101110111011001_u64, 15, false);
467 testToBitString("Test 03.01.04", "00000000000101100101110111011001", 0b101100101110111011001_u64);
468 testToBitString("Test 03.01.05", "000000000000101100101110111011001", 0b101100101110111011001_u64, 33);
469
470 testToBitString("Test 03.02.01", "11011001011101110110011110001101", 0b11011001011101110110011110001101_u64, 0);
471 testToBitString("Test 03.02.02", "11011001011101110110011110001101", 0b11011001011101110110011110001101_u64, 32);
472 testToBitString("Test 03.02.03", "01011001011101110110011110001101", 0b01011001011101110110011110001101_u64, 0);
473 testToBitString("Test 03.02.04", "01011001011101110110011110001101", 0b01011001011101110110011110001101_u64, 32);
474 testToBitString("Test 03.02.05", "0101110111011001", 0b0101100101110111011001_u64, 16, false);
475
476 testToBitString("Test 03.03.01", "1101100101110111011001111000110111011001011101110110011110001101",
477 0b1101100101110111011001111000110111011001011101110110011110001101_u64, 0);
478 testToBitString("Test 03.03.02", "1101100101110111011001111000110111011001011101110110011110001101",
479 0b1101100101110111011001111000110111011001011101110110011110001101_u64, 64);
480
481 testToBitString("Test 03.03.03", "0101100101110111011001111000110111011001011101110110011110001101",
482 0b0101100101110111011001111000110111011001011101110110011110001101_u64, 0);
483 testToBitString("Test 03.03.04", "0101100101110111011001111000110111011001011101110110011110001101",
484 0b0101100101110111011001111000110111011001011101110110011110001101_u64, 64);
485
486 testToBitString("Test 03.03.05", "0001100101110111011001111000110111011001011101110110011110001101",
487 0b0001100101110111011001111000110111011001011101110110011110001101_u64, 0);
488 testToBitString("Test 03.03.06", "0001100101110111011001111000110111011001011101110110011110001101",
489 0b0001100101110111011001111000110111011001011101110110011110001101_u64, 64);
490
491 testToBitString("Test 03.03.07", "1111111111101010111101101011111000000000000000000000000000000000",
492 0b1111111111101010111101101011111000000000000000000000000000000000_u64, 0);
493 testToBitString("Test 03.03.08", "1111111111101010111101101011111000000000000000000000000000000000",
494 0b1111111111101010111101101011111000000000000000000000000000000000_u64, 64);
495
496 testToBitString("Test 03.03.09", "11111110101001111110101011110110",
497 0b0000000000000000000000000000000011111110101001111110101011110110_u64, 0);
498 testToBitString("Test 03.03.10", "0000000000000000000000000000000011111110101001111110101011110110",
499 0b0000000000000000000000000000000011111110101001111110101011110110_u64, 64);
500 testToBitString("Test 03.03.11", "011111110101001111110101011110110",
501 0b0000000000000000000000000000000011111110101001111110101011110110_u64, 33);
502
503 testToBitString("Test 03.03.12", "00000000",
504 0b0000000000000000000000000000000000000000000000000000000000000000_u64, 0);
505 testToBitString("Test 03.03.13", "0000000000000000000000000000000000000000000000000000000000000000",
506 0b0000000000000000000000000000000000000000000000000000000000000000_u64, 64);
507 }
508}
509
std::string_view to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
@ msb
Identifier for most-significant-bit (msb) first.
constexpr bool is_positive(const T a) noexcept
Returns true of the given integral is positive, i.e.
Definition base_math.hpp:70
std::string to_decstring(const value_type &v, const char separator='\'', const nsize_t min_width=0) noexcept
Produces a decimal integer string representation of an integral integer value with given radix.
std::string toBitString(const void *data, const nsize_t length, const bit_order_t bitOrder=bit_order_t::msb, const PrefixOpt prefix=PrefixOpt::prefix, size_t bit_len=0) noexcept
Produce a binary string representation of the given lsb-first byte values.
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...
SizeBoolPair fromBitString(std::vector< uint8_t > &out, const uint8_t bitstr[], const size_t bitstr_len, const bit_order_t bitOrder=bit_order_t::msb, const Bool checkPrefix=Bool::True)
Converts a given binary string representation, appending to a byte vector (lsb-first).
std::string format(std::string_view fmt, const Targs &...args) noexcept
Strict format with type validation of arguments against the format string.
std::string toHexString(const void *data, const nsize_t length, const lb_endian_t byteOrder=lb_endian_t::big, const LoUpCase capitalization=LoUpCase::lower, const PrefixOpt prefix=PrefixOpt::prefix) noexcept
Produce a hexadecimal string representation of the given lsb-first byte values.
constexpr bool setConversion(char fmt_literal) noexcept
constexpr bool addFlag(char c) noexcept
std::string toString() const
constexpr void setWidth(uint32_t v)
std::string toFormat() const
Reconstructs format string.
Checker for member of pointer '->' operator with convertible pointer return, no arguments.
int printf(const char *format,...)
Operating Systems predefined macros.
static void testFrom(int line, value_type exp_v, std::string_view in_s, uint32_t radix=10, const char separator=0)
std::vector< int > std_vec_int
static void testToBitString(std::string_view prefix, std::string_view exp_be_s, const uint64_t &exp_be_v, size_t max_bits, bool check_value=true)
std_vec_int_citer::pointer std_vec_int_citer_pointer
decltype(std::declval< std_vec_int_citer >().operator->()) std_vec_int_citer_ptrop_retval
static void testDecTo(int line, value_type v, std::string_view exp_s, const uint32_t min_width=0, const char separator=0)
static void testTo(int line, value_type v, std::string_view exp_s, uint32_t radix, 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')
std_vec_int::const_iterator std_vec_int_citer
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')
TEST_CASE("Test 00 - to_string/appendIntString, fromIntString", "[jau][string][to_string][from_string]")
std_vec_int::iterator std_vec_int_iter