jaulib v1.4.1-17-gd77ace3-dirty
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
test_stringfmt_footprint0.cpp
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2024 Gothel Software e.K.
4 *
5 * ***
6 *
7 * SPDX-License-Identifier: MIT
8 *
9 * This Source Code Form is subject to the terms of the MIT License
10 * If a copy of the MIT was not distributed with this
11 * file, You can obtain one at https://opensource.org/license/mit/.
12 *
13 */
14#include <sys/types.h>
15#include <cassert>
16#include <cstdint>
17#include <cstdio>
18#include <cstring>
19#include <iostream>
20
21#include <jau/basic_types.hpp>
22#include <jau/cpp_lang_util.hpp>
23#include <jau/cpp_pragma.hpp>
24#include <jau/float_types.hpp>
25#include <jau/int_types.hpp>
27#include <jau/string_util.hpp>
28#include <jau/test/catch2_ext.hpp>
29#include <jau/type_concepts.hpp>
31
32#ifdef HAS_STD_FORMAT
33 #include <format>
34#endif
35
36using namespace std::literals;
37
38using namespace jau::float_literals;
39
40using namespace jau::int_literals;
41
42//
43// Explore memory footprint of using jau::cfmt, this unit uses unsafe variadic-argument format (not jau::cfmt)
44//
45
46template <typename... Args>
47static void printFormat(int line, const char *fmt, const Args &...args) {
48 // std::string exp = jau::unsafe::format_string(fmt, args...);
49 // std::string has = jau::format_string(fmt, args...);
50 std::string has = jau::unsafe::format_string(fmt, args...);
51 std::cerr << "FormatResult @ " << line << ": has `" << has << "`\n\n";
52}
53
54TEST_CASE("format: std::cfmt footprint", "[jau][std::string][jau::cfmt][footprint]") {
55 // type conversion
56 int32_t i32 = -1234;
57 int32_t i32_u = 1234;
58 uint32_t u32 = 1234;
59 float f32 = 123.45f; // 42.14f;
60 double f64 = 123.45; // 42.1456;
61 void *p1a = (void *)0xaabbccdd_u64; // NOLINT
62 void *p1b = (void *)0x11223344aabbccdd_u64; // NOLINT
63 void *p2a = (void *)0x112233aabbccdd_u64; // NOLINT
64 void *p2b = (void *)0xaabbcc_u64; // NOLINT
65 void *p3a = (void *)0x112233aabbccdd_u64; // NOLINT
66 void *p3b = (void *)0xaabbcc_u64; // NOLINT
67
68 printFormat(__LINE__, "%%");
69
70 printFormat(__LINE__, "%c", 'Z');
71 printFormat(__LINE__, "%s", "Hello World");
72 printFormat(__LINE__, "%p", &i32);
73 printFormat(__LINE__, "p1a %p %0p", p1a, p1a);
74 printFormat(__LINE__, "p1b %p %0p", p1b, p1b);
75 printFormat(__LINE__, "p2a %p %0p", p2a, p2a);
76 printFormat(__LINE__, "p2b %p %0p", p2b, p2b);
77 printFormat(__LINE__, "p3a %p %0p", p3a, p3a);
78 printFormat(__LINE__, "p3b %p %0p", p3b, p3b);
79
80 printFormat(__LINE__, "%d", i32);
81
82 printFormat(__LINE__, "%o", u32);
83 printFormat(__LINE__, "%x", u32);
84 printFormat(__LINE__, "%X", u32);
85 printFormat(__LINE__, "%u", u32);
86 printFormat(__LINE__, "%o", i32_u);
87 printFormat(__LINE__, "%x", i32_u);
88 printFormat(__LINE__, "%X", i32_u);
89 printFormat(__LINE__, "%u", i32_u);
90
91 printFormat(__LINE__, "%f", f64);
92 printFormat(__LINE__, "%e", f64);
93 printFormat(__LINE__, "%E", f64);
94 printFormat(__LINE__, "%a", f64);
95 printFormat(__LINE__, "%A", f64);
96 // checkFormat(__LINE__, "%g", f64);
97 // checkFormat(__LINE__, "%G", f64);
98
99 printFormat(__LINE__, "%f", f32);
100 printFormat(__LINE__, "%e", f32);
101 printFormat(__LINE__, "%E", f32);
102 printFormat(__LINE__, "%a", f32);
103 printFormat(__LINE__, "%A", f32);
104 // checkFormat(__LINE__, "%g", f32);
105 // checkFormat(__LINE__, "%G", f32);
106
107 printFormat(__LINE__, "%dZZZ", i32);
108 printFormat(__LINE__, "%dZZ", i32);
109 printFormat(__LINE__, "%dZ", i32);
110 printFormat(__LINE__, "Z%dZ Z%dZ", i32, i32);
111 printFormat(__LINE__, "Z%-6dZ Z%6dZ", i32, i32);
112
113 printFormat(__LINE__, "%#020x", 305441741);
114 printFormat(__LINE__, "%zd", 2147483647L);
115
116 printFormat(__LINE__, "%zu", 2147483647UL);
117
118 printFormat(__LINE__, "%s", "Test");
119 {
120 const char *str = nullptr;
121 size_t str_len = 2;
122 const char limiter = '3';
123 const char *limiter_pos = nullptr;
124 char *endptr = nullptr;
125
126 printFormat(__LINE__, "Value end not '%c' @ idx %zd, %p != %p, in: %p '%s' len %zu", limiter, endptr - str, endptr, limiter_pos, str, str, str_len);
127 }
128
129 // enums
130 {
131 enum enum1_unsigned_t { jau1_alpha, jau1_beta, jau1_gamma }; ///< unsigned
132 enum1_unsigned_t e1_u = jau1_alpha;
133
134 enum enum2_signed_t { jau2_alpha=-1, jau_beta, jau_gamma }; ///< signed
135 enum2_signed_t e2_s = jau2_alpha;
136
137 enum class enum3_signed_t { alpha=-1, beta, gamma }; ///< signed
138 enum3_signed_t e3_s = enum3_signed_t::alpha;
139
140 typedef enum { ///< unsigned
141 jau_CAP_CLEAR=0,
142 jau_CAP_SET=1
143 } enum4_unsigned_t;
144 enum4_unsigned_t e4_u = jau_CAP_CLEAR;
145
146 printFormat(__LINE__, "Enum %u, %d, %d, %u\n", e1_u, e2_s, e3_s, e4_u);
147 }
148
149}
std::string format_string(const char *format,...) noexcept
Returns a (non-truncated) string according to snprintf() formatting rules and variable number of argu...
TEST_CASE("format: std::cfmt footprint", "[jau][std::string][jau::cfmt][footprint]")
static void printFormat(int line, const char *fmt, const Args &...args)