Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/fmt/config/pushparser.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: 30 апр. 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/fmt/config/PushParser.h>
23 #include <lsp-plug.in/io/OutFileStream.h>
24 #include <lsp-plug.in/stdlib/math.h>
25 #include <lsp-plug.in/test-fw/helpers.h>
26 #include <lsp-plug.in/test-fw/utest.h>
27
28 namespace lsp
29 {
30 using namespace lsp::config;
31
32 static const char *input =
33 "# comment\n"
34 "\r\n"
35 "key1=value1\n"
36 "key2=123# comment\n"
37 "key3 = 123.456\n"
38 "key4 = 12.5db \n"
39 "#comment\n"
40 " key5=i32:123\n"
41 "\tkey6=f32:\"+12.5 dB\"\n"
42 "key7=-inf\n"
43 "key8= \"+inf\"\n"
44 "\n"
45 "key9=blob:\":5:12345\"\n"
46 "key10=blob:\"text/plain:6:123456\"\n"
47 "key11= string with spaces # comment\n"
48 "key12 = \"string \\\"with\\\" escapes\" \n"
49 "key13 = string \\\"with other\\\" escapes \n"
50 "key14 = string \\# not comment \n"
51 "key15 = \n"
52 "key16 = true \n"
53 "key17 = false \n"
54 "key18 = bool:true \n"
55 "key19 = bool:false \n"
56 "/tree/arg1 = str:\"value\" \n";
57
58 static const char *output =
59 "begin\n"
60 "key1=str[]:value1\n"
61 "key2=i32[]:123\n"
62 "key3=f32[]:123.4560\n"
63 "key4=f32[d]:12.5000\n"
64 "key5=i32[t]:123\n"
65 "key6=f32[qtd]:12.5000\n"
66 "key7=f32[]:-inf\n"
67 "key8=str[q]:+inf\n"
68 "key9=blob[qt]:5::12345\n"
69 "key10=blob[qt]:6:text/plain:123456\n"
70 "key11=str[]:string with spaces\n"
71 "key12=str[q]:string \"with\" escapes\n"
72 "key13=str[]:string \"with other\" escapes\n"
73 "key14=str[]:string # not comment\n"
74 "key15=str[]:\n"
75 "key16=bool[]:true\n"
76 "key17=bool[]:false\n"
77 "key18=bool[t]:true\n"
78 "key19=bool[t]:false\n"
79 "/tree/arg1=str[qt]:value\n"
80 "end\n";
81 }
82
83 UTEST_BEGIN("runtime.fmt.config", pushparser)
84
85 static const char *special_value(double v)
86 {
87 if (isinf(v))
88 return (v < 0) ? "-inf" : "inf";
89 if (isnan(v))
90 return "nan";
91 return NULL;
92 }
93
94 class Handler: public IConfigHandler
95 {
96 protected:
97 LSPString *pStr;
98 test_type_t *pTest;
99
100 private:
101 void dump_flags(char *dst, size_t flags)
102 {
103 if (flags & SF_QUOTED)
104 *(dst++) = 'q';
105 if (flags & SF_COMMENT)
106 *(dst++) = 'c';
107 if (flags & SF_TYPE_SET)
108 *(dst++) = 't';
109 if (flags & SF_DECIBELS)
110 *(dst++) = 'd';
111
112 *dst = '\0';
113 }
114
115 public:
116 Handler(test_type_t *t, LSPString *str) { pStr = str; pTest = t; }
117
118 public:
119 virtual status_t start()
120 {
121 pStr->append_ascii("begin\n");
122 pTest->printf("begin\n");
123 return STATUS_OK;
124 }
125
126 virtual status_t finish(status_t res)
127 {
128 pStr->append_ascii("end\n");
129 pTest->printf("end\n");
130 return STATUS_OK;
131 }
132
133 virtual status_t handle_i32(const LSPString *name, int32_t value, size_t flags)
134 {
135 char f[0x10];
136 dump_flags(f, flags);
137 pStr->fmt_append_utf8("%s=i32[%s]:%d\n", name->get_utf8(), f, int(value));
138 pTest->printf("%s=i32[%s]:%d\n", name->get_utf8(), f, int(value));
139 return STATUS_OK;
140 }
141
142 virtual status_t handle_u32(const LSPString *name, uint32_t value, size_t flags)
143 {
144 char f[0x10];
145 dump_flags(f, flags);
146 pStr->fmt_append_utf8("%s=u32[%s]:%d\n", name->get_utf8(), f, int(value));
147 pTest->printf("%s=u32[%s]:%d\n", name->get_utf8(), f, int(value));
148 return STATUS_OK;
149 }
150
151 virtual status_t handle_f32(const LSPString *name, float value, size_t flags)
152 {
153 char f[0x10];
154 dump_flags(f, flags);
155 const char *special = special_value(value);
156 if (special != NULL)
157 {
158 pStr->fmt_append_utf8("%s=f32[%s]:%s\n", name->get_utf8(), f, special);
159 pTest->printf("%s=f32[%s]:%s\n", name->get_utf8(), f, special);
160 }
161 else
162 {
163 pStr->fmt_append_utf8("%s=f32[%s]:%.4f\n", name->get_utf8(), f, value);
164 pTest->printf("%s=f32[%s]:%.4f\n", name->get_utf8(), f, value);
165 }
166 return STATUS_OK;
167 }
168
169 virtual status_t handle_i64(const LSPString *name, int64_t value, size_t flags)
170 {
171 char f[0x10];
172 dump_flags(f, flags);
173 pStr->fmt_append_utf8("%s=i64[%s]:%d\n", name->get_utf8(), f, int(value));
174 pTest->printf("%s=i64[%s]:%d\n", name->get_utf8(), f, int(value));
175 return STATUS_OK;
176 }
177
178 virtual status_t handle_u64(const LSPString *name, uint64_t value, size_t flags)
179 {
180 char f[0x10];
181 dump_flags(f, flags);
182 pStr->fmt_append_utf8("%s=u64[%s]:%d\n", name->get_utf8(), f, int(value));
183 pTest->printf("%s=u64[%s]:%d\n", name->get_utf8(), f, int(value));
184 return STATUS_OK;
185 }
186
187 virtual status_t handle_f64(const LSPString *name, double value, size_t flags)
188 {
189 char f[0x10];
190 dump_flags(f, flags);
191 const char *special = special_value(value);
192 if (special != NULL)
193 {
194 pStr->fmt_append_utf8("%s=f64[%s]:%s\n", name->get_utf8(), f, special);
195 pTest->printf("%s=f64[%s]:%s\n", name->get_utf8(), f, special);
196 }
197 else
198 {
199 pStr->fmt_append_utf8("%s=f64[%s]:%.4f\n", name->get_utf8(), f, value);
200 pTest->printf("%s=f64[%s]:%.4f\n", name->get_utf8(), f, value);
201 }
202 return STATUS_OK;
203 }
204
205 virtual status_t handle_string(const LSPString *name, const LSPString *value, size_t flags)
206 {
207 char f[0x10];
208 dump_flags(f, flags);
209 pStr->fmt_append_utf8("%s=str[%s]:%s\n", name->get_utf8(), f, value->get_utf8());
210 pTest->printf("%s=str[%s]:%s\n", name->get_utf8(), f, value->get_utf8());
211 return STATUS_OK;
212 }
213
214 virtual status_t handle_bool(const LSPString *name, bool value, size_t flags)
215 {
216 char f[0x10];
217 dump_flags(f, flags);
218 pStr->fmt_append_utf8("%s=bool[%s]:%s\n", name->get_utf8(), f, (value) ? "true" : "false");
219 pTest->printf("%s=str[%s]:%s\n", name->get_utf8(), f, (value) ? "true" : "false");
220 return STATUS_OK;
221 }
222
223 virtual status_t handle_blob(const LSPString *name, const blob_t *value, size_t flags)
224 {
225 char f[0x10];
226 dump_flags(f, flags);
227 pStr->fmt_append_utf8("%s=blob[%s]:%d:%s:%s\n", name->get_utf8(), f,
228 int(value->length),
229 (value->ctype != NULL) ? value->ctype : "",
230 (value->data != NULL) ? value->data : ""
231 );
232 pTest->printf("%s=blob[%s]:%d:%s:%s\n", name->get_utf8(), f,
233 int(value->length),
234 (value->ctype != NULL) ? value->ctype : "",
235 (value->data != NULL) ? value->data : ""
236 );
237 return STATUS_OK;
238 }
239 };
240
241 void test_valid_cases()
242 {
243 PushParser p;
244 LSPString out;
245 Handler h(this, &out);
246
247 UTEST_ASSERT(p.parse_data(&h, input, "UTF-8") == STATUS_OK);
248 UTEST_ASSERT(out.equals_utf8(output),
249 printf("Got output:\n%s\n", out.get_utf8());
250 printf("Expected output:\n%s\n", output);
251 );
252 }
253
254 void test_file()
255 {
256 PushParser p;
257 LSPString out;
258 Handler h(this, &out);
259 io::Path path;
260 UTEST_ASSERT(path.fmt("%s/%s.obj", tempdir(), full_name()) >= 0);
261
262 printf("Writing simple config data to %s...\n", path.as_native());
263 io::OutFileStream os;
264 UTEST_ASSERT(os.open(&path, io::File::FM_WRITE_NEW) == STATUS_OK);
265 UTEST_ASSERT(os.write(input, ::strlen(input)) > 0);
266 UTEST_ASSERT(os.close() == STATUS_OK);
267
268 printf("Parsing simple OBJ data from %s...\n", path.as_native());
269
270 printf("Testing simple OBJ data...\n");
271 UTEST_ASSERT(p.parse_file(&h, &path) == STATUS_OK);
272 UTEST_ASSERT(out.equals_utf8(output),
273 printf("Got output:\n%s\n", out.get_utf8());
274 printf("Expected output:\n%s\n", output);
275 );
276 }
277
278 UTEST_MAIN
279 {
280 // printf("Testing simple data...\n");
281 // test_valid_cases();
282 printf("Testing file parse...\n");
283 test_file();
284 }
285
286 UTEST_END
287
288
289