Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/fmt/config/pullparser.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
23 #include <lsp-plug.in/test-fw/utest.h>
24 #include <lsp-plug.in/test-fw/helpers.h>
25 #include <lsp-plug.in/fmt/config/PullParser.h>
26 #include <stdarg.h>
27 #include <math.h>
28
29 namespace lsp
30 {
31 using namespace lsp::config;
32 }
33
34 UTEST_BEGIN("runtime.fmt.config", pullparser)
35
36 void test_valid_cases()
37 {
38 static const char *text =
39 "# comment\n"
40 "\n\r"
41 "key1=value1\n"
42 "key2=123# comment\n"
43 "key3 = 123.456\n"
44 "key4 = 12.5db \n"
45 "#comment\n"
46 " key5=i32:123\n"
47 "\tkey6=f32:\"+12.5 dB\"\n"
48 "key7=-inf\n"
49 "key8= \"+inf\"\n"
50 "\n"
51 "key9=blob:\":5:12345\"\n"
52 "key10=blob:\"text/plain:6:123456\"\n"
53 "key11= string with spaces # comment\n"
54 "key12 = \"string \\\"with\\\" escapes\" \n"
55 "key13 = string \\\"with other\\\" escapes \n"
56 "key14 = string \\# not comment \n"
57 "key15 = \n"
58 "key16 = true \n"
59 "key17 = false \n"
60 "key18 = bool:true \n"
61 "key19 = bool:false \n"
62 "/tree/arg1 = str:\"value\" \n";
63
64 PullParser p;
65 param_t xp;
66 const param_t *pp;
67
68 // Wrap the string
69 UTEST_ASSERT((pp = p.current()) == NULL);
70 UTEST_ASSERT(p.wrap(text, "UTF-8") == STATUS_OK);
71 UTEST_ASSERT((pp = p.current()) == NULL);
72
73 // key1
74 UTEST_ASSERT(p.next() == STATUS_OK);
75 UTEST_ASSERT((pp = p.current()) != NULL);
76 UTEST_ASSERT(pp->name.equals_ascii("key1"));
77 UTEST_ASSERT(pp->flags == SF_TYPE_STR);
78 UTEST_ASSERT(::strcmp(pp->v.str, "value1") == 0);
79
80 // key2
81 UTEST_ASSERT(p.next() == STATUS_OK);
82 UTEST_ASSERT((pp = p.current()) != NULL);
83 UTEST_ASSERT(pp->name.equals_ascii("key2"));
84 UTEST_ASSERT(pp->flags == SF_TYPE_I32);
85 UTEST_ASSERT(pp->v.i32 == 123);
86
87 // key3
88 UTEST_ASSERT(p.next() == STATUS_OK);
89 UTEST_ASSERT((pp = p.current()) != NULL);
90 UTEST_ASSERT(pp->name.equals_ascii("key3"));
91 UTEST_ASSERT(pp->flags == SF_TYPE_F32);
92 UTEST_ASSERT(float_equals_relative(pp->v.f32, 123.456, 0.0001));
93
94 // key4
95 UTEST_ASSERT(p.next() == STATUS_OK);
96 UTEST_ASSERT((pp = p.current()) != NULL);
97 UTEST_ASSERT(pp->name.equals_ascii("key4"));
98 UTEST_ASSERT(pp->flags == (SF_TYPE_F32 | SF_DECIBELS));
99 UTEST_ASSERT(float_equals_relative(pp->v.f32, 12.5, 1e-5));
100
101 // key5
102 UTEST_ASSERT(p.next() == STATUS_OK);
103 UTEST_ASSERT((pp = p.current()) != NULL);
104 UTEST_ASSERT(pp->name.equals_ascii("key5"));
105 UTEST_ASSERT(pp->flags == (SF_TYPE_I32 | SF_TYPE_SET));
106 UTEST_ASSERT(pp->v.i32 == 123);
107
108 // key6
109 UTEST_ASSERT(p.next() == STATUS_OK);
110 UTEST_ASSERT((pp = p.current()) != NULL);
111 UTEST_ASSERT(pp->name.equals_ascii("key6"));
112 UTEST_ASSERT(pp->flags == (SF_TYPE_F32 | SF_TYPE_SET | SF_QUOTED | SF_DECIBELS));
113 UTEST_ASSERT(float_equals_relative(pp->v.f32, 12.5, 1e-5));
114
115 // key7
116 UTEST_ASSERT(p.next(&xp) == STATUS_OK);
117 UTEST_ASSERT(xp.name.equals_ascii("key7"));
118 UTEST_ASSERT(xp.flags == SF_TYPE_F32);
119 UTEST_ASSERT(isinf(xp.v.f32) && (xp.v.f32 < 0.0f)); // -inf
120
121 // key8
122 UTEST_ASSERT(p.next(&xp) == STATUS_OK);
123 UTEST_ASSERT(xp.name.equals_ascii("key8"));
124 UTEST_ASSERT(xp.flags == (SF_TYPE_STR | SF_QUOTED));
125 UTEST_ASSERT(::strcmp(xp.v.str, "+inf") == 0); // +inf
126
127 // key9
128 UTEST_ASSERT(p.next() == STATUS_OK);
129 UTEST_ASSERT((pp = p.current()) != NULL);
130 UTEST_ASSERT(pp->name.equals_ascii("key9"));
131 UTEST_ASSERT(pp->flags == (SF_TYPE_BLOB | SF_TYPE_SET | SF_QUOTED));
132 UTEST_ASSERT(pp->v.blob.length == 5);
133 UTEST_ASSERT(pp->v.blob.ctype == NULL);
134 UTEST_ASSERT(::strcmp(pp->v.blob.data, "12345") == 0);
135
136 // key10
137 UTEST_ASSERT(p.next() == STATUS_OK);
138 UTEST_ASSERT((pp = p.current()) != NULL);
139 UTEST_ASSERT(pp->name.equals_ascii("key10"));
140 UTEST_ASSERT(pp->flags == (SF_TYPE_BLOB | SF_TYPE_SET | SF_QUOTED));
141 UTEST_ASSERT(pp->v.blob.length == 6);
142 UTEST_ASSERT(::strcmp(pp->v.blob.ctype, "text/plain") == 0);
143 UTEST_ASSERT(::strcmp(pp->v.blob.data, "123456") == 0);
144
145 // key11
146 UTEST_ASSERT(p.next() == STATUS_OK);
147 UTEST_ASSERT((pp = p.current()) != NULL);
148 UTEST_ASSERT(pp->name.equals_ascii("key11"));
149 UTEST_ASSERT(pp->flags == SF_TYPE_STR);
150 UTEST_ASSERT(::strcmp(pp->v.str, "string with spaces") == 0);
151
152 // key12
153 UTEST_ASSERT(p.next() == STATUS_OK);
154 UTEST_ASSERT((pp = p.current()) != NULL);
155 UTEST_ASSERT(pp->name.equals_ascii("key12"));
156 UTEST_ASSERT(pp->flags == (SF_TYPE_STR | SF_QUOTED));
157 UTEST_ASSERT(::strcmp(pp->v.str, "string \"with\" escapes") == 0);
158
159 // key13
160 UTEST_ASSERT(p.next() == STATUS_OK);
161 UTEST_ASSERT((pp = p.current()) != NULL);
162 UTEST_ASSERT(pp->name.equals_ascii("key13"));
163 UTEST_ASSERT(pp->flags == SF_TYPE_STR);
164 UTEST_ASSERT(::strcmp(pp->v.str, "string \"with other\" escapes") == 0);
165
166 // key14
167 UTEST_ASSERT(p.next() == STATUS_OK);
168 UTEST_ASSERT((pp = p.current()) != NULL);
169 UTEST_ASSERT(pp->name.equals_ascii("key14"));
170 UTEST_ASSERT(pp->flags == SF_TYPE_STR);
171 UTEST_ASSERT(::strcmp(pp->v.str, "string # not comment") == 0);
172
173 // key15
174 UTEST_ASSERT(p.next() == STATUS_OK);
175 UTEST_ASSERT((pp = p.current()) != NULL);
176 UTEST_ASSERT(pp->name.equals_ascii("key15"));
177 UTEST_ASSERT(pp->flags == SF_TYPE_STR);
178 UTEST_ASSERT(::strcmp(pp->v.str, "") == 0);
179
180 // key16
181 UTEST_ASSERT(p.next() == STATUS_OK);
182 UTEST_ASSERT((pp = p.current()) != NULL);
183 UTEST_ASSERT(pp->name.equals_ascii("key16"));
184 UTEST_ASSERT(pp->flags == SF_TYPE_BOOL);
185 UTEST_ASSERT(pp->v.bval == true);
186
187 // key17
188 UTEST_ASSERT(p.next() == STATUS_OK);
189 UTEST_ASSERT((pp = p.current()) != NULL);
190 UTEST_ASSERT(pp->name.equals_ascii("key17"));
191 UTEST_ASSERT(pp->flags == SF_TYPE_BOOL);
192 UTEST_ASSERT(pp->v.bval == false);
193
194 // key18
195 UTEST_ASSERT(p.next() == STATUS_OK);
196 UTEST_ASSERT((pp = p.current()) != NULL);
197 UTEST_ASSERT(pp->name.equals_ascii("key18"));
198 UTEST_ASSERT(pp->flags == (SF_TYPE_SET | SF_TYPE_BOOL));
199 UTEST_ASSERT(pp->v.bval == true);
200
201 // key19
202 UTEST_ASSERT(p.next() == STATUS_OK);
203 UTEST_ASSERT((pp = p.current()) != NULL);
204 UTEST_ASSERT(pp->name.equals_ascii("key19"));
205 UTEST_ASSERT(pp->flags == (SF_TYPE_SET | SF_TYPE_BOOL));
206 UTEST_ASSERT(pp->v.bval == false);
207
208 // /tree/arg1
209 UTEST_ASSERT(p.next() == STATUS_OK);
210 UTEST_ASSERT((pp = p.current()) != NULL);
211 UTEST_ASSERT(pp->name.equals_ascii("/tree/arg1"));
212 UTEST_ASSERT(pp->flags == (SF_TYPE_STR | SF_TYPE_SET | SF_QUOTED));
213 UTEST_ASSERT(::strcmp(pp->v.str, "value") == 0);
214
215 // eof
216 UTEST_ASSERT(p.next() == STATUS_EOF);
217
218 UTEST_ASSERT(p.close() == STATUS_OK);
219 }
220
221 void test_invalid_cases()
222 {
223 static const char *lines[] =
224 {
225 "abcdef",
226 "123=",
227 "1abc=def",
228 "abc=\"def",
229 "abc=f32:",
230 "abc=i32:",
231 "abc=i32:12 db",
232 "abc=f32:12 .34",
233 "abc=f32:\"12\" db",
234 "abc=\"invalid\" escape\"",
235 "tree/node=10.11",
236 NULL
237 };
238
239 for (const char **xp=lines; *xp != NULL; ++xp)
240 {
241 PullParser p;
242 printf(" testing bad line: %s\n", *xp);
243 UTEST_ASSERT(p.wrap(*xp) == STATUS_OK);
244 UTEST_ASSERT(p.next() == STATUS_BAD_FORMAT);
245 UTEST_ASSERT(p.close() == STATUS_OK);
246 }
247 }
248
249 void test_file_load()
250 {
251 io::Path path;
252 UTEST_ASSERT(path.fmt("%s/%s", resources(), "config/rbm.cfg"));
253
254 PullParser p;
255 status_t res;
256
257 printf("Reading file %s...\n", path.as_native());
258 UTEST_ASSERT(p.open(&path) == STATUS_OK);
259 while ((res = p.next()) != STATUS_EOF)
260 printf(" got parameter: %s\n", p.current()->name.get_native());
261
262 UTEST_ASSERT(res == STATUS_EOF);
263 UTEST_ASSERT(p.close() == STATUS_OK);
264 }
265
266 UTEST_MAIN
267 {
268 printf("Testing valid cases...\n");
269 test_valid_cases();
270 printf("Testing invalid cases...\n");
271 test_invalid_cases();
272 printf("Testing file load...\n");
273 test_file_load();
274 }
275
276 UTEST_END