==> tests/text.c <==1/* mesh-text against Rust 1.88: {:?} of f64, f32 and str, and str::trim (vectors printed by the Rust). */
2#include "mesh/check.h"
3#include "mesh/text.h"
4
5#include <math.h>
6#include <stdlib.h>
7#include <string.h>
8
9static void f64_is(double v, const char *want) {
10 mesh_text t = {0};
11 mesh_text_debug_f64(&t, v);
12 CHECK_S(mesh_text_str(&t), want);
13 mesh_text_free(&t);
14}
15
16static void f32_is(float v, const char *want) {
17 mesh_text t = {0};
18 mesh_text_debug_f32(&t, v);
19 CHECK_S(mesh_text_str(&t), want);
20 mesh_text_free(&t);
21}
22
23static void str_is(const char *s, size_t n, const char *want) {
24 mesh_text t = {0};
25 mesh_text_debug_str(&t, s, n);
26 CHECK_S(mesh_text_str(&t), want);
27 mesh_text_free(&t);
28}
29
30static void trim_is(const char *s, const char *want) {
31 size_t n = strlen(s);
32 mesh_trim(&s, &n);
33 char *got = mesh_strndup(s, n);
34 CHECK_S(got, want);
35 free(got);
36}
37
38int main(void) {
39 f64_is(0.0, "0.0");
40 f64_is(-0.0, "-0.0");
41 f64_is(1.0, "1.0");
42 f64_is(2.0, "2.0");
43 f64_is(0.5, "0.5");
44 f64_is(0.1, "0.1");
45 f64_is(0.2, "0.2");
46 f64_is(0.3, "0.3");
47 f64_is(1.0/3.0, "0.3333333333333333");
48 f64_is(2.0/3.0, "0.6666666666666666");
49 f64_is(123.456, "123.456");
50 f64_is(1e15, "1000000000000000.0");
51 f64_is(1e16, "1e16");
52 f64_is(1e17, "1e17");
53 f64_is(1.5e16, "1.5e16");
54 f64_is(9.999999999999998e15, "9999999999999998.0");
55 f64_is(1e-4, "0.0001");
56 f64_is(1e-5, "1e-5");
57 f64_is(1.5e-7, "1.5e-7");
58 f64_is(0.00012, "0.00012");
59 f64_is(5e-324, "5e-324");
60 f64_is(1.7976931348623157e308, "1.7976931348623157e308");
61 f64_is(NAN, "NaN");
62 f64_is(INFINITY, "inf");
63 f64_is(-INFINITY, "-inf");
64 f64_is(100.0, "100.0");
65 f64_is(1024.0, "1024.0");
66 f64_is(0.01, "0.01");
67 f64_is(2.5, "2.5");
68 f64_is(1234567.0, "1234567.0");
69 f64_is(0.000123456, "0.000123456");
70 f64_is(3.14159, "3.14159");
71 f64_is(-2.75, "-2.75");
72 f64_is(4096.0, "4096.0");
73 f64_is(1e300, "1e300");
74 f64_is(1e-300, "1e-300");
75 f64_is(12345678901234567.0, "1.2345678901234568e16");
76 f32_is(0.1f, "0.1");
77 f32_is(1.0f/3.0f, "0.33333334");
78 f32_is(16777216.0f, "16777216.0");
79 f32_is(1e-7f, "1e-7");
80 f32_is(3.4028235e38f, "3.4028235e38");
81 f32_is(1.0e10f, "10000000000.0");
82 f32_is(0.3f, "0.3");
83 str_is("", 0, "\"\"");
84 str_is("abc", 3, "\"abc\"");
85 str_is("a\"b", 3, "\"a\\\"b\"");
86 str_is("back\\slash", 10, "\"back\\\\slash\"");
87 str_is("nl\012x", 4, "\"nl\\nx\"");
88 str_is("tab\011", 4, "\"tab\\t\"");
89 str_is("cr\015", 3, "\"cr\\r\"");
90 str_is("nul\0x", 5, "\"nul\\0x\"");
91 str_is("\033[0m", 4, "\"\\u{1b}[0m\"");
92 str_is("\177", 1, "\"\\u{7f}\"");
93 str_is("\303\251 \303\274", 5, "\"\303\251 \303\274\"");
94 str_is("\302\205", 2, "\"\\u{85}\"");
95 str_is("\302\240x", 3, "\"\\u{a0}x\"");
96 str_is("'single'", 8, "\"'single'\"");
97 str_is("\342\200\213", 3, "\"\\u{200b}\"");
98 str_is("\346\227\245\346\234\254", 6, "\"\346\227\245\346\234\254\"");
99 trim_is(" a ", "a");
100 trim_is("\302\240b\343\200\200", "b");
101 trim_is("\011\012 x \015\012", "x");
102 trim_is("\342\200\250y\302\205", "y");
103 trim_is("z\357\273\277", "z\357\273\277");
104 /* Parsing as Rust's from_str. */
105 uint32_t u;
106 CHECK(mesh_parse_u32("+5", 2, 10, &u) && u == 5);
107 CHECK(!mesh_parse_u32("-5", 2, 10, &u));
108 CHECK(!mesh_parse_u32("", 0, 10, &u));
109 CHECK(!mesh_parse_u32("4294967296", 10, 10, &u));
110 CHECK(mesh_parse_u32("ffffffff", 8, 16, &u) && u == 0xffffffffu);
111 CHECK(!mesh_parse_u32("1 ", 2, 10, &u));
112 double d;
113 CHECK(mesh_parse_f64("2.0", 3, &d) && d == 2.0);
114 CHECK(mesh_parse_f64(".5", 2, &d) && d == 0.5);
115 CHECK(mesh_parse_f64("1.", 2, &d) && d == 1.0);
116 CHECK(mesh_parse_f64("-1e3", 4, &d) && d == -1000.0);
117 CHECK(mesh_parse_f64("inf", 3, &d) && isinf(d));
118 CHECK(mesh_parse_f64("NaN", 3, &d) && isnan(d));
119 CHECK(!mesh_parse_f64("0x1.8p3", 7, &d));
120 CHECK(!mesh_parse_f64(" 1.0", 4, &d));
121 CHECK(!mesh_parse_f64("1e", 2, &d));
122 CHECK(!mesh_parse_f64(".", 1, &d));
123 CHECK(!mesh_parse_f64("1_0.0", 5, &d));
124 /* Errors and buffers. */
125 char *err = NULL;
126 CHECK(!mesh_errf(&err, "x %d", 1));
127 mesh_err_prefix(&err, "manifest: ");
128 CHECK_S(err, "manifest: x 1");
129 free(err);
130 char *q = mesh_debug_str("say \"hi\"");
131 CHECK_S(q, "\"say \\\"hi\\\"\"");
132 free(q);
133 return mesh_check_done();
134}