jaulib v1.4.1-17-gd77ace3-dirty
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
test_stringfmt_footprint1.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>
26#include <jau/string_cfmt.hpp>
28#include <jau/string_util.hpp>
29#include <jau/test/catch2_ext.hpp>
30#include <jau/type_concepts.hpp>
32
33#ifdef HAS_STD_FORMAT
34 #include <format>
35#endif
36
37using namespace std::literals;
38
39using namespace jau::float_literals;
40
41using namespace jau::int_literals;
42
43//
44// Explore memory footprint of using jau::cfmt, this unit uses jau::cfmt
45//
46
47template <typename... Args>
48static void printFormat(int line, const char *fmt, const Args &...args) {
49 // std::string exp = jau::unsafe::format_string(fmt, args...);
50 // std::string has = jau::format_string(fmt, args...);
51 std::string has;
52 jau::cfmt::Result r = jau::cfmt::formatR(has, fmt, args...);
53 std::cerr << "FormatResult @ " << line << ": " << r << "\n";
54 std::cerr << "FormatResult @ " << line << ": has `" << has << "`\n\n";
55 CHECK( true == r.success());
56 CHECK( sizeof...(args) == r.argumentCount());
57}
58
59TEST_CASE("format: std::cfmt footprint", "[jau][std::string][jau::cfmt][footprint]") {
60 // type conversion
61 int32_t i32 = -1234;
62 int32_t i32_u = 1234;
63 uint32_t u32 = 1234;
64 float f32 = 123.45f; // 42.14f;
65 double f64 = 123.45; // 42.1456;
66 void *p1a = (void *)0xaabbccdd_u64; // NOLINT
67 void *p1b = (void *)0x11223344aabbccdd_u64; // NOLINT
68 void *p2a = (void *)0x112233aabbccdd_u64; // NOLINT
69 void *p2b = (void *)0xaabbcc_u64; // NOLINT
70 void *p3a = (void *)0x112233aabbccdd_u64; // NOLINT
71 void *p3b = (void *)0xaabbcc_u64; // NOLINT
72
73 printFormat(__LINE__, "%%");
74
75 printFormat(__LINE__, "%c", 'Z');
76 printFormat(__LINE__, "%s", "Hello World");
77 printFormat(__LINE__, "%p", &i32);
78 printFormat(__LINE__, "p1a %p %0p", p1a, p1a);
79 printFormat(__LINE__, "p1b %p %0p", p1b, p1b);
80 printFormat(__LINE__, "p2a %p %0p", p2a, p2a);
81 printFormat(__LINE__, "p2b %p %0p", p2b, p2b);
82 printFormat(__LINE__, "p3a %p %0p", p3a, p3a);
83 printFormat(__LINE__, "p3b %p %0p", p3b, p3b);
84
85 printFormat(__LINE__, "%d", i32);
86
87 printFormat(__LINE__, "%o", u32);
88 printFormat(__LINE__, "%x", u32);
89 printFormat(__LINE__, "%X", u32);
90 printFormat(__LINE__, "%u", u32);
91 printFormat(__LINE__, "%o", i32_u);
92 printFormat(__LINE__, "%x", i32_u);
93 printFormat(__LINE__, "%X", i32_u);
94 printFormat(__LINE__, "%u", i32_u);
95
96 printFormat(__LINE__, "%f", f64);
97 printFormat(__LINE__, "%e", f64);
98 printFormat(__LINE__, "%E", f64);
99 printFormat(__LINE__, "%a", f64);
100 printFormat(__LINE__, "%A", f64);
101 // checkFormat(__LINE__, "%g", f64);
102 // checkFormat(__LINE__, "%G", f64);
103
104 printFormat(__LINE__, "%f", f32);
105 printFormat(__LINE__, "%e", f32);
106 printFormat(__LINE__, "%E", f32);
107 printFormat(__LINE__, "%a", f32);
108 printFormat(__LINE__, "%A", f32);
109 // checkFormat(__LINE__, "%g", f32);
110 // checkFormat(__LINE__, "%G", f32);
111
112 printFormat(__LINE__, "%dZZZ", i32);
113 printFormat(__LINE__, "%dZZ", i32);
114 printFormat(__LINE__, "%dZ", i32);
115 printFormat(__LINE__, "Z%dZ Z%dZ", i32, i32);
116 printFormat(__LINE__, "Z%-6dZ Z%6dZ", i32, i32);
117
118 printFormat(__LINE__, "%#020x", 305441741);
119 printFormat(__LINE__, "%zd", 2147483647L);
120
121 printFormat(__LINE__, "%zu", 2147483647UL);
122
123 printFormat(__LINE__, "%s", "Test");
124 {
125 const char *str = nullptr;
126 size_t str_len = 2;
127 const char limiter = '3';
128 const char *limiter_pos = nullptr;
129 char *endptr = nullptr;
130
131 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);
132 }
133
134 // enums
135 {
136 enum enum1_unsigned_t { jau1_alpha, jau1_beta, jau1_gamma }; ///< unsigned
137 enum1_unsigned_t e1_u = jau1_alpha;
138
139 enum enum2_signed_t { jau2_alpha=-1, jau_beta, jau_gamma }; ///< signed
140 enum2_signed_t e2_s = jau2_alpha;
141
142 enum class enum3_signed_t { alpha=-1, beta, gamma }; ///< signed
143 enum3_signed_t e3_s = enum3_signed_t::alpha;
144
145 typedef enum { ///< unsigned
146 jau_CAP_CLEAR=0,
147 jau_CAP_SET=1
148 } enum4_unsigned_t;
149 enum4_unsigned_t e4_u = jau_CAP_CLEAR;
150
151 printFormat(__LINE__, "Enum %u, %d, %d, %u\n", e1_u, e2_s, e3_s, e4_u);
152 }
153
154}
constexpr ssize_t argumentCount() const noexcept
Arguments processed.
constexpr bool success() const noexcept
true if operation was successful, otherwise indicates error
Result formatR(std::string &s, size_t maxLen, std::string_view fmt, const Targs &...args) noexcept
Strict format with type validation of arguments against the format string, appending to the given des...
TEST_CASE("format: std::cfmt footprint", "[jau][std::string][jau::cfmt][footprint]")
static void printFormat(int line, const char *fmt, const Args &...args)