Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/expr/format.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 26 февр. 2020 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/test-fw/utest.h>
23 #include <lsp-plug.in/test-fw/helpers.h>
24 #include <lsp-plug.in/expr/format.h>
25 #include <lsp-plug.in/stdlib/math.h>
26
27 namespace lsp
28 {
29 using namespace lsp::expr;
30 }
31
32 #define OK(arg) UTEST_ASSERT((arg) == STATUS_OK)
33
34 UTEST_BEGIN("runtime.expr", format)
35
36 void test_simple(Parameters *p)
37 {
38 LSPString out;
39
40 OK(format(&out, "123", p));
41 printf("out = %s\n", out.get_utf8());
42 UTEST_ASSERT(out.equals_ascii("123"));
43
44 OK(format(&out, "{}", p));
45 printf("out = %s\n", out.get_utf8());
46 UTEST_ASSERT(out.equals_ascii("100500"));
47
48 OK(format(&out, "{} {} {}", p));
49 printf("out = %s\n", out.get_utf8());
50 UTEST_ASSERT(out.equals_ascii("100500 440.000000 true"));
51
52 OK(format(&out, "{@bool} {@int} {@float} {@strA} {@strB} {@null} {@undef} {@nan} {@pinf} {@ninf}", p));
53 printf("out = %s\n", out.get_utf8());
54 UTEST_ASSERT(out.equals_ascii("true 100500 440.000000 string CaMeL <null> <undef> nan inf -inf"));
55
56 OK(format(&out, "{[0]} {[1]} {[2]} {[3]} {[4]} {[5]} {[6]} {[7]} {[8]} {[9]}", p));
57 printf("out = %s\n", out.get_utf8());
58 UTEST_ASSERT(out.equals_ascii("100500 440.000000 true string CaMeL nan inf -inf <null> <undef>"));
59
60 OK(format(&out, "\\{[0]} {} {[1]} {} {[2]}", p));
61 printf("out = %s\n", out.get_utf8());
62 UTEST_ASSERT(out.equals_ascii("{[0]} 100500 440.000000 440.000000 true"));
63
64 OK(format(&out, "{\\} {@1} {[int]} {[]} {^} {$} {>>} {||} {@int$} {^0$1[a]} {%Z} {[", p));
65 printf("out = %s\n", out.get_utf8());
66 UTEST_ASSERT(out.equals_ascii("{\\} {@1} {[int]} {[]} {^} {$} {>>} {||} {@int$} {^0$1[a]} {%Z} {["));
67
68 OK(format(&out, "{@a@b} {[1][2]} {^0^9} {$0$9} {>|>|} {%d%d} {%.f}", p));
69 printf("out = %s\n", out.get_utf8());
70 UTEST_ASSERT(out.equals_ascii("{@a@b} {[1][2]} {^0^9} {$0$9} {>|>|} {%d%d} {%.f}"));
71 }
72
73 void test_format(Parameters *p)
74 {
75 LSPString out;
76
77 OK(format(&out, "{@int%d} {@neg%d} {@int%+d} {@neg%+d} {@int%b} {@int%o} {@hex%x} {@hex%X} {@hex%+X} {@neg%x}", p));
78 printf("out = %s\n", out.get_utf8());
79 UTEST_ASSERT(out.equals_ascii("100500 -1234 +100500 -1234 11000100010010100 304224 c0de C0DE +C0DE -4d2"));
80
81 OK(format(&out, "{@int%8d} {@neg%8d} {@int%+8d} {@neg%+8d} {@int%20b} {@int%8o} {@hex%8x} {@hex%8X} {@hex%+8X} {@neg%8x}", p));
82 printf("out = %s\n", out.get_utf8());
83 UTEST_ASSERT(out.equals_ascii("00100500 -00001234 +00100500 -00001234 00011000100010010100 00304224 0000c0de 0000C0DE +0000C0DE -000004d2"));
84
85 OK(format(&out, "{@null%d} {@undef%d} {@null%b} {@undef%b} {@null%o} {@undef%o} {@null%x} {@undef%x}", p));
86 printf("out = %s\n", out.get_utf8());
87 UTEST_ASSERT(out.equals_ascii("<null> <undef> <null> <undef> <null> <undef> <null> <undef>"));
88
89 OK(format(&out, "{@float%f} {@float%.2f} {@float%.0f} {@float%+.2f} {@nan%f} {@pinf%f} {@ninf%f} {@pinf%+f} {@null%f} {@undef%f}", p));
90 printf("out = %s\n", out.get_utf8());
91 UTEST_ASSERT(out.equals_ascii("440.000000 440.00 440 +440.00 nan inf -inf +inf <null> <undef>"));
92
93 OK(format(&out, "{@float%16f} {@float%8.2f} {@float%8.0f} {@float%+8.2f} {@neg_float%8.2f}", p));
94 printf("out = %s\n", out.get_utf8());
95 UTEST_ASSERT(out.equals_ascii("000000440.000000 00440.00 00000440 +00440.00 -00123.45"));
96
97 OK(format(&out, "{@float%F} {@float%.2F} {@float%.0F} {@float%+.2F} {@nan%F} {@pinf%F} {@ninf%F} {@pinf%+F} {@null%F} {@undef%F}", p));
98 printf("out = %s\n", out.get_utf8());
99 UTEST_ASSERT(out.equals_ascii("440.000000 440.00 440 +440.00 NAN INF -INF +INF <null> <undef>"));
100
101 OK(format(&out, "{@bool%l} {@bool%L} {@bool%Ll} {@bool%lL} {@null%l} {@undef%l}", p));
102 printf("out = %s\n", out.get_utf8());
103 UTEST_ASSERT(out.equals_ascii("true TRUE True tRUE <null> <undef>"));
104
105 OK(format(&out, "{@strA%s} {@strA%t} {@strA%T} {@strA%Tt} {@strA%tT} {@null%s} {@null%t} {@undef%s} {@undef%t}", p));
106 printf("out = %s\n", out.get_utf8());
107 UTEST_ASSERT(out.equals_ascii("string string STRING String sTRING <null> <null> <undef> <undef>"));
108
109 OK(format(&out, "{@strB%s} {@strB%t} {@strB%T} {@strB%Tt} {@strB%tT}", p));
110 printf("out = %s\n", out.get_utf8());
111 UTEST_ASSERT(out.equals_ascii("CaMeL camel CAMEL Camel cAMEL"));
112
113 OK(format(&out, "{@strC%s} {@strC%t} {@strC%T} {@strC%Tt} {@strC%tT}", p));
114 printf("out = %s\n", out.get_utf8());
115 UTEST_ASSERT(out.equals_ascii(" "));
116 }
117
118 void test_padding(Parameters *p)
119 {
120 LSPString out;
121
122 OK(format(&out, "{>@strA^0%10s$1} {@strA^0%10s$1<} {|@strA^0%10s$1} {|>@strA^0%10s$1} {<|@strA^0%10s$1} {>|@strA^0%10s$1} {|<@strA^0%10s$1}", p));
123 printf("out = %s\n", out.get_utf8());
124 UTEST_ASSERT(out.equals_ascii("0000string string1111 00string11 000string1 0string111 00string11 00string11"));
125
126 OK(format(&out, "{>@strB^0%10s$1} {@strB^0%10s$1<} {|@strB^0%10s$1} {|>@strB^0%10s$1} {<|@strB^0%10s$1} {>|@strB^0%10s$1} {|<@strB^0%10s$1}", p));
127 printf("out = %s\n", out.get_utf8());
128 UTEST_ASSERT(out.equals_ascii("00000CaMeL CaMeL11111 00CaMeL111 0000CaMeL1 0CaMeL1111 00CaMeL111 000CaMeL11"));
129
130 OK(format(&out, "{>@strC^0%10s$1} {@strC^0%10s$1<} {|@strC^0%10s$1} {|>@strC^0%10s$1} {<|@strC^0%10s$1} {>|@strC^0%10s$1} {|<@strC^0%10s$1}", p));
131 printf("out = %s\n", out.get_utf8());
132 UTEST_ASSERT(out.equals_ascii("0000000000 1111111111 0000011111 0000000011 0011111111 0000011111 0000011111"));
133
134 OK(format(&out, "{>@null^0%10s$1} {@null^0%10s$1<} {|@null^0%10s$1} {|>@null^0%10s$1} {<|@null^0%10s$1} {>|@null^0%10s$1} {|<@null^0%10s$1}", p));
135 printf("out = %s\n", out.get_utf8());
136 UTEST_ASSERT(out.equals_ascii("0000<null> <null>1111 00<null>11 000<null>1 0<null>111 00<null>11 00<null>11"));
137
138 OK(format(&out, "{>@null%10s} {@null%10s<} {|@null%10s} {|>@null%10s} {<|@null%10s} {>|@null%10s} {|<@null%10s}", p));
139 printf("out = %s\n", out.get_utf8());
140 UTEST_ASSERT(out.equals_ascii(" <null> <null> <null> <null> <null> <null> <null> "));
141 }
142
143 void test_xpadding(Parameters *p)
144 {
145 LSPString out;
146
147 OK(format(&out, "{>@hex^_%8x:16$_} {@hex^_%8x:16$_<} {|@hex^_%8x:16$_} {|>@hex^_%8x:16$_} {<|@hex^_%8x:16$_} {>|@hex^_%7x:16$_} {|<@hex^_%7x:16$_}", p));
148 printf("out = %s\n", out.get_utf8());
149 UTEST_ASSERT(out.equals_ascii("________0000c0de 0000c0de________ ____0000c0de____ ______0000c0de__ __0000c0de______ ____000c0de_____ _____000c0de____"));
150 }
151
152 UTEST_MAIN
153 {
154 Parameters p;
155 OK(p.add_int("int", 100500));
156 OK(p.add_float("float", 440.0));
157 OK(p.add_bool("bool", true));
158 OK(p.add_cstring("strA", "string"));
159 OK(p.add_cstring("strB", "CaMeL"));
160 OK(p.add_float("nan", NAN));
161 OK(p.add_float("pinf", +INFINITY));
162 OK(p.add_float("ninf", -INFINITY));
163 OK(p.add_null("null"));
164 OK(p.add_undef("undef"));
165 OK(p.add_int("neg", -1234));
166 OK(p.add_int("hex", 0xc0de));
167 OK(p.add_cstring("strC", ""));
168 OK(p.add_float("neg_float", -123.45));
169
170 printf("\nTesting simple types...\n");
171 test_simple(&p);
172
173 printf("\nTesting format specifiers...\n");
174 test_format(&p);
175
176 printf("\nTesting padding...\n");
177 test_padding(&p);
178
179 printf("\nTesting external padding...\n");
180 test_xpadding(&p);
181 }
182
183 UTEST_END
184