Gamp v0.0.8
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
161 public:
162 std::string toString() const { return "SomeClass toString"; }
163};
164enum class game_t : uint16_t {
169};
171
172enum class plainenum_t : uint16_t {
176};
177
178TEST_CASE( "Test 00 - to_string/appendIntString, fromIntString", "[jau][string][to_string][from_string]" ) {
179 int i1 = 1;
180 uint64_t u64_1 = 1116791496961ull;
181 void * p_v_1 = (void *)0xAFFE;
182 float float_1 = 1.65f;
183
184 Addr48Bit addr48bit_1(u64_1);
185
186 CHECK("1" == jau::to_string<int>(i1));
187 CHECK("1116791496961" == jau::to_string(u64_1));
188 CHECK("0xaffe" == jau::to_string(p_v_1));
189 CHECK("0xaffe" == jau::toHexString(0xaffe_u32));
190 CHECK("SomeClass toString" == jau::to_string(SomeClass()));
191 CHECK("chess" == jau::to_string(game_t::chess));
192 CHECK("pacman" == jau::to_string(game_t::pacman));
193 CHECK("lala" != jau::to_string(plainenum_t::lala));
194 CHECK("little" == jau::to_string(jau::lb_endian_t::little));
195 {
196 // radix, default: no-width, prefix, no-separator, no padding
197 testToFrom(__LINE__, 0xdeadbeef_u32, "0xdeadbeef", 16); // hex
198 testToFrom(__LINE__, 0xdeadbeef_u32, "0xdead'beef", " 0x'dead'beef la", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // hex
199
200 testToFrom(__LINE__, 876543210_u64, "876543210", 10); // dec
201 testToFrom(__LINE__, 876543210_u64, "876'543'210", " '876'543'210 la", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // dec
202
203 testToFrom(__LINE__, 077652_u32, "077652", 8); // oct
204 testToFrom(__LINE__, 077652_u32, "07'7652", " 07'7652 la", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // oct
205
206 testToFrom(__LINE__, 0b11010101101_u32, "0b110'1010'1101", " 0b'110'1010'1101 la", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // bin
207
208 // no-prefix, radix, default: no-width, no-separator, no padding
209 testToFrom(__LINE__, 0xaffe_u32, "affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none); // hex
210 testToFrom(__LINE__, 0x1affe_u32, "1affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none); // hex
211 testToFrom(__LINE__, 876543210_u64, "876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none); // dec
212 testToFrom(__LINE__, 1876543210_u64, "1876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none); // dec
213 testToFrom(__LINE__, 043217652_u32, "43217652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none); // oct
214 testToFrom(__LINE__, 0143217652_u32, "143217652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none); // oct
215 testToFrom(__LINE__, 0b11010101101_u32, "11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none); // bin
216 testToFrom(__LINE__, 0b111010101101_u32, "111010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none); // bin
217
218 // radix, width-expansion, default: prefix, no-separator, '0' padding
219 testToFrom(__LINE__, 0xaffe_u32, "0x00affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 8); // hex
220 testToFrom(__LINE__, 0x1affe_u32, "0x01affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 8); // hex
221 testToFrom(__LINE__, 876543210_u64, "000876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 12); // dec
222 testToFrom(__LINE__, 1876543210_u64, "001876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 12); // dec
223 testToFrom(__LINE__, 043217652_u32, "0043217652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 10); // oct
224 testToFrom(__LINE__, 0143217652_u32, "0143217652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 10); // oct
225 testToFrom(__LINE__, 0b11010101101_u32, "0b00011010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 16); // bin
226 testToFrom(__LINE__, 0b111010101101_u32, "0b00111010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 16); // bin
227
228 // no-prefix, radix, width-expansion, default: no-separator, '0' padding
229 testToFrom(__LINE__, 0xaffe_u32, "0000affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 8); // hex
230 testToFrom(__LINE__, 0x1affe_u32, "0001affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 8); // hex
231 testToFrom(__LINE__, 876543210_u64, "000876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 12); // dec
232 testToFrom(__LINE__, 1876543210_u64, "001876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 12); // dec
233 testToFrom(__LINE__, 043217652_u32, "0043217652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 10); // oct
234 testToFrom(__LINE__, 0143217652_u32, "0143217652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 10); // oct
235 testToFrom(__LINE__, 0b11010101101_u32, "0000011010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 16); // bin
236 testToFrom(__LINE__, 0b111010101101_u32, "0000111010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 16); // bin
237
238 // radix, separator, default: no-width, prefix, '0' padding
239 testToFrom(__LINE__, 0xaffe_u32, "0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // hex
240 testToFrom(__LINE__, 0x1affe_u32, "0x1'affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // hex
241 testToFrom(__LINE__, 876543210_u64, "876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // dec
242 testToFrom(__LINE__, 1876543210_u64, "1'876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // dec
243 testToFrom(__LINE__, 043217652_u32, "04321'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // oct
244 testToFrom(__LINE__, 0143217652_u32, "01'4321'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // oct
245 testToFrom(__LINE__, 0b10101101_u32, "0b1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // bin
246 testToFrom(__LINE__, 0b110101101_u32, "0b1'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\''); // bin
247
248 // no-prefix, radix, separator, default: no-width, '0' padding
249 testToFrom(__LINE__, 0xaffe_u32, "affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // hex
250 testToFrom(__LINE__, 0x1affe_u32, "1'affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // hex
251 testToFrom(__LINE__, 876543210_u64, "876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // dec
252 testToFrom(__LINE__, 1876543210_u64, "1'876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // dec
253 testToFrom(__LINE__, 043217652_u32, "4321'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // oct
254 testToFrom(__LINE__, 0143217652_u32, "1'4321'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // oct
255 testToFrom(__LINE__, 0b10101101_u32, "1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // bin
256 testToFrom(__LINE__, 0b110101101_u32, "1'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\''); // bin
257
258 // radix, width-expansion, separator, default: prefix, '0' padding
259 testToFrom(__LINE__, 0xaffe_u32, "0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 6, '\''); // hex
260 testToFrom(__LINE__, 0xaffe_u32, "0x'affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 7, '\''); // hex
261 testToFrom(__LINE__, 0xaffe_u32, "0x0'affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 8, '\''); // hex
262
263 testToFrom(__LINE__, 876543210_u64, "876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 11, '\''); // dec
264 testToFrom(__LINE__, 876543210_u64, "'876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 12, '\''); // dec
265 testToFrom(__LINE__, 876543210_u64, "0'876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 13, '\''); // dec
266
267 testToFrom(__LINE__, 07652_u32, "07652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 5, '\''); // oct
268 testToFrom(__LINE__, 07652_u32, "0'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 6, '\''); // oct
269 testToFrom(__LINE__, 07652_u32, "00'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 7, '\''); // oct
270
271 testToFrom(__LINE__, 0b111010101101_u32, "0b1110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 16, '\''); // bin
272 testToFrom(__LINE__, 0b111010101101_u32, "0b'1110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 17, '\''); // bin
273 testToFrom(__LINE__, 0b111010101101_u32, "0b0'1110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 18, '\''); // bin
274
275 // no-prefix, radix, width-expansion, separator, default: '0' padding
276 testToFrom(__LINE__, 0xaffe_u32, "affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 4, '\''); // hex
277 testToFrom(__LINE__, 0xaffe_u32, "'affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 5, '\''); // hex
278 testToFrom(__LINE__, 0xaffe_u32, "0'affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 6, '\''); // hex
279
280 testToFrom(__LINE__, 876543210_u64, "876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 11, '\''); // dec
281 testToFrom(__LINE__, 876543210_u64, "'876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 12, '\''); // dec
282 testToFrom(__LINE__, 876543210_u64, "0'876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 13, '\''); // dec
283
284 testToFrom(__LINE__, 07652_u32, "7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 4, '\''); // oct
285 testToFrom(__LINE__, 07652_u32, "'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 5, '\''); // oct
286 testToFrom(__LINE__, 07652_u32, "0'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 6, '\''); // oct
287
288 testToFrom(__LINE__, 0b111010101101_u32, "1110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 14, '\''); // bin
289 testToFrom(__LINE__, 0b111010101101_u32, "'1110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 15, '\''); // bin
290 testToFrom(__LINE__, 0b111010101101_u32, "0'1110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 16, '\''); // bin
291
292 // Also testing to_decstring() due to radix==10 and padding==' '
293
294 //
295 // a.b.c radix, no-width, space padding ' ', [prefix], [separator], [signed]
296 // |
297 // 0 - unsigned
298 // 1 - signed
299 // |
300 // 0 = no-separator
301 // 1 = separator
302 // |
303 // 0 - no-prefix,
304 // 1 - prefix,
305
306 // 0.0.0 unsigned, no-prefix, radix, space padding ' ', default: no-width, no-separator
307 testToFrom(__LINE__, 0xaffe_u32, "affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // hex
308 testToFrom(__LINE__, 876543210_u64, "876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // dec
309 testToFrom(__LINE__, 077652_u32, "77652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // oct
310 testToFrom(__LINE__, 0b11010101101_u32, "11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // bin
311
312 // 0.0.1 signed, no-prefix, radix, space padding ' ', default: no-width, no-separator
313 testToFrom(__LINE__, -0xaffe_i32, "-affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // hex
314 testToFrom(__LINE__, -876543210_i64, "-876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // dec
315 testToFrom(__LINE__, -077652_i32, "-77652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // oct
316 testToFrom(__LINE__, -0b11010101101_i32, "-11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, 0, ' '); // bin
317
318 // 0.1.0 unsigned, no-prefix, radix, separator, space padding ' ', default: no-width
319 testToFrom(__LINE__, 0xaffe_u32, "affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // hex
320 testToFrom(__LINE__, 876543210_u64, "876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // dec
321 testToFrom(__LINE__, 077652_u32, "7'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // oct
322 testToFrom(__LINE__, 0b11010101101_u32, "110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // bin
323
324 // 0.1.1 signed, no-prefix, radix, separator, space padding ' ', default: no-width
325 testToFrom(__LINE__, -0xaffe_i32, "-affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // hex
326 testToFrom(__LINE__, -876543210_i64, "-876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // dec
327 testToFrom(__LINE__, -077652_i32, "-7'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // oct
328 testToFrom(__LINE__, -0b11010101101_i32, "-110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 0, '\'', ' '); // bin
329
330 // 1.0.0 unsigned, radix, space padding ' ', default: prefix, no-width, no-separator
331 testToFrom(__LINE__, 0xaffe_u32, "0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // hex
332 testToFrom(__LINE__, 876543210_u64, "876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // dec
333 testToFrom(__LINE__, 077652_u32, "077652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // oct
334 testToFrom(__LINE__, 0b11010101101_u32, "0b11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // bin
335
336 // 1.0.1 signed, radix, space padding ' ', default: prefix, no-width, no-separator
337 testToFrom(__LINE__, -0xaffe_i32, "-0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // hex
338 testToFrom(__LINE__, -876543210_i64, "-876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // dec
339 testToFrom(__LINE__, -077652_i32, "-077652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // oct
340 testToFrom(__LINE__, -0b11010101101_i32, "-0b11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, 0, ' '); // bin
341
342 // 1.1.0 unsigned, radix, separator, space padding ' ', default: prefix, no-width
343 testToFrom(__LINE__, 0xaffe_u32, "0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // hex
344 testToFrom(__LINE__, 876543210_u64, "876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // dec
345 testToFrom(__LINE__, 077652_u32, "07'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // oct
346 testToFrom(__LINE__, 0b11010101101_u32, "0b110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // bin
347
348 // 1.1.1 signed, radix, separator, space padding ' ', default: prefix, no-width
349 testToFrom(__LINE__, -0xaffe_i32, "-0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // hex
350 testToFrom(__LINE__, -876543210_i64, "-876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // dec
351 testToFrom(__LINE__, -077652_i32, "-07'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // oct
352 testToFrom(__LINE__, -0b11010101101_i32, "-0b110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 0, '\'', ' '); // bin
353
354 //
355 // a.b.c radix, width-expansion, space padding ' ', [prefix], [separator], [signed]
356 // |
357 // 0 - unsigned
358 // 1 - signed
359 // |
360 // 0 = no-separator
361 // 1 = separator
362 // |
363 // 0 - no-prefix,
364 // 1 - prefix,
365
366 // 0.0.0 unsigned, no-prefix, radix, width-expansion, space padding ' ', default: no-separator
367 testToFrom(__LINE__, 0xaffe_u32, " affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 8, 0, ' '); // hex
368 testToFrom(__LINE__, 876543210_u64, " 876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 15, 0, ' '); // dec
369 testToFrom(__LINE__, 077652_u32, " 77652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 10, 0, ' '); // oct
370 testToFrom(__LINE__, 0b11010101101_u32, " 11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 17, 0, ' '); // bin
371
372 // 0.0.1 signed, no-prefix, radix, width-expansion, space padding ' ', default: no-separator
373 testToFrom(__LINE__, -0xaffe_i32, " -affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 8, 0, ' '); // hex
374 testToFrom(__LINE__, -876543210_i64, " -876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 15, 0, ' '); // dec
375 testToFrom(__LINE__, -077652_i32, " -77652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 10, 0, ' '); // oct
376 testToFrom(__LINE__, -0b11010101101_i32, " -11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 17, 0, ' '); // bin
377
378 // 0.1.0 unsigned, no-prefix, radix, width-expansion, separator, space padding ' '
379 testToFrom(__LINE__, 0xaffe_u32, " affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 8, '\'', ' '); // hex
380 testToFrom(__LINE__, 876543210_u64, " 876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 15, '\'', ' '); // dec
381 testToFrom(__LINE__, 077652_u32, " 7'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 10, '\'', ' '); // oct
382 testToFrom(__LINE__, 0b11010101101_u32, " 110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 17, '\'', ' '); // bin
383
384 // 0.1.1 signed, no-prefix, radix, width-expansion, separator, space padding ' '
385 testToFrom(__LINE__, -0xaffe_i32, " -affe", 16, jau::LoUpCase::lower, jau::PrefixOpt::none, 8, '\'', ' '); // hex
386 testToFrom(__LINE__, -876543210_i64, " -876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::none, 15, '\'', ' '); // dec
387 testToFrom(__LINE__, -077652_i32, " -7'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::none, 10, '\'', ' '); // oct
388 testToFrom(__LINE__, -0b11010101101_i32, " -110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::none, 17, '\'', ' '); // bin
389
390 // 1.0.0 unsigned, radix, width-expansion, space padding ' ', default: prefix, no-separator
391 testToFrom(__LINE__, 0xaffe_u32, " 0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 8, 0, ' '); // hex
392 testToFrom(__LINE__, 876543210_u64, " 876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 15, 0, ' '); // dec
393 testToFrom(__LINE__, 077652_u32, " 077652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 10, 0, ' '); // oct
394 testToFrom(__LINE__, 0b11010101101_u32, " 0b11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 17, 0, ' '); // bin
395
396 // 1.0.1 signed, radix, width-expansion, space padding ' ', default: prefix, no-separator
397 testToFrom(__LINE__, -0xaffe_i32, " -0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 8, 0, ' '); // hex
398 testToFrom(__LINE__, -876543210_i64, " -876543210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 15, 0, ' '); // dec
399 testToFrom(__LINE__, -077652_i32, " -077652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 10, 0, ' '); // oct
400 testToFrom(__LINE__, -0b11010101101_i32, " -0b11010101101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 17, 0, ' '); // bin
401
402 // 1.1.0 unsigned, radix, width-expansion, separator, space padding ' '
403 testToFrom(__LINE__, 0xaffe_u32, " 0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 8, '\'', ' '); // hex
404 testToFrom(__LINE__, 876543210_u64, " 876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 15, '\'', ' '); // dec
405 testToFrom(__LINE__, 077652_u32, " 07'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 10, '\'', ' '); // oct
406 testToFrom(__LINE__, 0b11010101101_u32, " 0b110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 17, '\'', ' '); // bin
407
408 // 1.1.1 signed, radix, width-expansion, separator, space padding ' '
409 testToFrom(__LINE__, -0xaffe_i32, " -0xaffe", 16, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 8, '\'', ' '); // hex
410 testToFrom(__LINE__, -876543210_i64, " -876'543'210", 10, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 15, '\'', ' '); // dec
411 testToFrom(__LINE__, -077652_i32, " -07'7652", 8, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 10, '\'', ' '); // oct
412 testToFrom(__LINE__, -0b11010101101_i32, " -0b110'1010'1101", 2, jau::LoUpCase::lower, jau::PrefixOpt::prefix, 17, '\'', ' '); // bin
413
414 }
415 CHECK("1.650000" == jau::to_string(float_1));
416
417 CHECK("01:04:05:F5:E1:01" == jau::to_string(addr48bit_1));
418
419 //
420 // Validating 'pointer std::vector::const_iterator.operator->()'
421 // and the to_string type trait logic of it.
422 //
423
424 // jau::type_cue<std_vec_int_citer>::print("std_vec_int_citer", jau::TypeTraitGroup::ALL);
425 // jau::type_cue<std_vec_int_citer_pointer>::print("std_vec_int_citer_pointer", jau::TypeTraitGroup::ALL);
426
427 // jau::type_cue<std_vec_int_citer_ptrop_retval>::print("std_vec_int_citer_ptrop_retval", jau::TypeTraitGroup::ALL);
428 printf("jau::has_member_of_pointer<std_vec_int_citer>) %d\n", jau::has_member_of_pointer<std_vec_int_citer>::value);
429
430 std_vec_int vec_int_1;
431 vec_int_1.push_back(1); vec_int_1.push_back(2); vec_int_1.push_back(3);
432 std_vec_int_citer vec_int_citer_1B = vec_int_1.cbegin();
433 uint8_t* vec_int_citer_1B_ptr = (uint8_t*)(vec_int_citer_1B.operator->());
434 std::string vec_int_citer_1B_str = jau::toHexString(vec_int_citer_1B_ptr);
435
436 std_vec_int_citer vec_int_citer_1E = vec_int_1.cend();
437 uint8_t* vec_int_citer_1E_ptr = (uint8_t*)(vec_int_citer_1E.operator->());
438 std::string vec_int_citer_1E_str = jau::toHexString(vec_int_citer_1E_ptr);
439
440 std::ptrdiff_t vec_int_citer_1E_1B_ptrdiff = vec_int_citer_1E_ptr - vec_int_citer_1B_ptr;
441 size_t vec_int_citer_1E_1B_ptr_count = vec_int_citer_1E_1B_ptrdiff / sizeof(int);
442 size_t vec_int_citer_1E_1B_itr_count = vec_int_citer_1E - vec_int_citer_1B;
443
444 printf("vec_int_citer_1E - vec_int_citer_1B = itr_count %zu, ptr_count %zu\n",
445 vec_int_citer_1E_1B_itr_count, vec_int_citer_1E_1B_ptr_count);
446 printf("vec_int_citer_1E - vec_int_citer_1B = %zu\n", vec_int_citer_1E_1B_itr_count);
447 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());
448
449 CHECK(vec_int_citer_1E_1B_itr_count == 3);
450 CHECK(vec_int_citer_1E_1B_itr_count == vec_int_citer_1E_1B_ptr_count);
451
452 CHECK(vec_int_citer_1E_str == jau::to_string(vec_int_citer_1E));
453}
454
455#if 0
456TEST_CASE( "Test 01 - to_string(radix)", "[jau][string][to_string(radix)]" ) {
457 REQUIRE( true == true );
458}
459
460TEST_CASE( "Test 02 - toHexString()", "[jau][string][toHexString]" ) {
461 REQUIRE( true == true );
462}
463#endif
464
465static 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) {
466 std::cout << prefix << ": max_bits " << max_bits << "\n";
467 std::string has_be_s1 = jau::toBitString(exp_be_v, jau::bit_order_t::msb, jau::PrefixOpt::none, max_bits);
468 std::cout << " exp_be_s : " << exp_be_s << "\n";
469 std::cout << " has_be_s1: " << has_be_s1 << "\n";
470 REQUIRE( exp_be_s == has_be_s1 );
471
472 if( check_value ) {
473 const auto [has_be_v, len_be, ok_be] = jau::fromBitString(exp_be_s);
474 REQUIRE(true == ok_be);
475 REQUIRE(exp_be_s.size() == len_be);
476 std::string has_be_s2 = jau::toBitString(has_be_v, jau::bit_order_t::msb, jau::PrefixOpt::none, max_bits);
477 std::cout << " has_be_s2: " << has_be_s2 << "\n";
478 REQUIRE(exp_be_v == has_be_v);
479 }
480}
481static void testToBitString(std::string_view prefix, std::string_view s_be1, const uint32_t &v_be1) {
482 testToBitString(prefix, s_be1, v_be1, s_be1.size(), true);
483}
484
485TEST_CASE( "Test 03 - toBitString()", "[jau][string][toBitString]" ) {
486 {
487 testToBitString("Test 03.01.01", "000101100101110111011001", 0b101100101110111011001_u64, 0);
488 testToBitString("Test 03.01.02", "000101100101110111011001", 0b101100101110111011001_u64);
489 testToBitString("Test 03.01.03", "101110111011001", 0b101100101110111011001_u64, 15, false);
490 testToBitString("Test 03.01.04", "00000000000101100101110111011001", 0b101100101110111011001_u64);
491 testToBitString("Test 03.01.05", "000000000000101100101110111011001", 0b101100101110111011001_u64, 33);
492
493 testToBitString("Test 03.02.01", "11011001011101110110011110001101", 0b11011001011101110110011110001101_u64, 0);
494 testToBitString("Test 03.02.02", "11011001011101110110011110001101", 0b11011001011101110110011110001101_u64, 32);
495 testToBitString("Test 03.02.03", "01011001011101110110011110001101", 0b01011001011101110110011110001101_u64, 0);
496 testToBitString("Test 03.02.04", "01011001011101110110011110001101", 0b01011001011101110110011110001101_u64, 32);
497 testToBitString("Test 03.02.05", "0101110111011001", 0b0101100101110111011001_u64, 16, false);
498
499 testToBitString("Test 03.03.01", "1101100101110111011001111000110111011001011101110110011110001101",
500 0b1101100101110111011001111000110111011001011101110110011110001101_u64, 0);
501 testToBitString("Test 03.03.02", "1101100101110111011001111000110111011001011101110110011110001101",
502 0b1101100101110111011001111000110111011001011101110110011110001101_u64, 64);
503
504 testToBitString("Test 03.03.03", "0101100101110111011001111000110111011001011101110110011110001101",
505 0b0101100101110111011001111000110111011001011101110110011110001101_u64, 0);
506 testToBitString("Test 03.03.04", "0101100101110111011001111000110111011001011101110110011110001101",
507 0b0101100101110111011001111000110111011001011101110110011110001101_u64, 64);
508
509 testToBitString("Test 03.03.05", "0001100101110111011001111000110111011001011101110110011110001101",
510 0b0001100101110111011001111000110111011001011101110110011110001101_u64, 0);
511 testToBitString("Test 03.03.06", "0001100101110111011001111000110111011001011101110110011110001101",
512 0b0001100101110111011001111000110111011001011101110110011110001101_u64, 64);
513
514 testToBitString("Test 03.03.07", "1111111111101010111101101011111000000000000000000000000000000000",
515 0b1111111111101010111101101011111000000000000000000000000000000000_u64, 0);
516 testToBitString("Test 03.03.08", "1111111111101010111101101011111000000000000000000000000000000000",
517 0b1111111111101010111101101011111000000000000000000000000000000000_u64, 64);
518
519 testToBitString("Test 03.03.09", "11111110101001111110101011110110",
520 0b0000000000000000000000000000000011111110101001111110101011110110_u64, 0);
521 testToBitString("Test 03.03.10", "0000000000000000000000000000000011111110101001111110101011110110",
522 0b0000000000000000000000000000000011111110101001111110101011110110_u64, 64);
523 testToBitString("Test 03.03.11", "011111110101001111110101011110110",
524 0b0000000000000000000000000000000011111110101001111110101011110110_u64, 33);
525
526 testToBitString("Test 03.03.12", "00000000",
527 0b0000000000000000000000000000000000000000000000000000000000000000_u64, 0);
528 testToBitString("Test 03.03.13", "0000000000000000000000000000000000000000000000000000000000000000",
529 0b0000000000000000000000000000000000000000000000000000000000000000_u64, 64);
530 }
531}
532
std::string toString() const
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.
@ little
Identifier for little endian, equivalent to endian::little.
#define JAU_MAKE_ENUM_STRING(type,...)
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