Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/io/inmarksequence.cpp

raw
1 /*
2 * Copyright (C) 2021 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2021 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 12 февр. 2021 г.
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/ByteBuffer.h>
24 #include <lsp-plug.in/io/InMarkSequence.h>
25 #include <lsp-plug.in/io/InStringSequence.h>
26
27 UTEST_BEGIN("runtime.io", inmarksequence)
28
29 bool check_string(const LSPString *s, size_t first, size_t len)
30 {
31 if (s->length() != len)
32 return false;
33 for (size_t i=0; i<len; ++i)
34 if (s->char_at(i) != (first + i + 1))
35 return false;
36 return true;
37 }
38
39 bool check_buffer(const lsp_wchar_t *b, size_t first, size_t count)
40 {
41 for (size_t i=0; i<count; ++i)
42 if (b[i] != (first + i))
43 {
44 printf("b[%d] = %d != %d\n", i, b[i], int(first + i));
45 return false;
46 }
47 return true;
48 }
49
50 void test_full_read(const LSPString *s)
51 {
52 printf("Testing full read of %d characters\n", int(s->length()));
53
54 io::InStringSequence in(s);
55 io::InMarkSequence ims;
56 LSPString out;
57 ByteBuffer buf(s->length() * sizeof(lsp_wchar_t));
58 lsp_wchar_t *data = buf.data<lsp_wchar_t>();
59
60 // Test for mark() and wrap
61 UTEST_ASSERT(ims.mark(-1) == STATUS_CLOSED);
62 UTEST_ASSERT(ims.reset() == STATUS_CLOSED);
63 UTEST_ASSERT(ims.wrap(&in, true) == STATUS_OK);
64 UTEST_ASSERT(ims.mark(-1) == STATUS_OK);
65 UTEST_ASSERT(ims.reset() == STATUS_NOT_FOUND);
66
67 // Perform first read
68 ssize_t nread = ims.read(data, s->length() * 2);
69 UTEST_ASSERT(nread == ssize_t(s->length()));
70 UTEST_ASSERT(!buf.corrupted());
71 UTEST_ASSERT(check_buffer(data, 1, s->length()));
72
73 // Perform second read - EOF should be returned
74 nread = ims.read(data, s->length() * 2);
75 UTEST_ASSERT(nread == -STATUS_EOF);
76
77 // reset() should return NOT_FOUND error
78 UTEST_ASSERT(ims.reset() == STATUS_NOT_FOUND);
79
80 // Close the stream
81 UTEST_ASSERT(ims.close() == STATUS_OK);
82 }
83
84 void test_partial_read(const LSPString *s, size_t blk_size)
85 {
86 printf("Testing partial read for block size %d\n", int(blk_size));
87
88 io::InStringSequence in(s);
89 io::InMarkSequence ims;
90 LSPString out;
91 ByteBuffer buf(s->length() * sizeof(lsp_wchar_t));
92 lsp_wchar_t *data = buf.data<lsp_wchar_t>();
93
94 // Wrap
95 UTEST_ASSERT(ims.wrap(&in, true) == STATUS_OK);
96 ssize_t nread = 0, total = 0;
97
98 // Perform read with specific block size
99 while ((nread = ims.read(data, blk_size)) >= 0)
100 {
101 data += nread;
102 total += nread;
103 }
104
105 UTEST_ASSERT(nread == -STATUS_EOF);
106 UTEST_ASSERT(total == ssize_t(s->length()));
107
108 // Check buffer
109 data = buf.data<lsp_wchar_t>();
110 UTEST_ASSERT(check_buffer(data, 1, s->length()));
111
112 // Close the stream
113 UTEST_ASSERT(ims.close() == STATUS_OK);
114 }
115
116 void test_character_read(const LSPString *s)
117 {
118 printf("Testing character read\n");
119
120 io::InStringSequence in(s);
121 io::InMarkSequence ims;
122 LSPString out;
123 ByteBuffer buf(s->length() * sizeof(lsp_wchar_t));
124 lsp_wchar_t *data = buf.data<lsp_wchar_t>();
125
126 // Wrap
127 UTEST_ASSERT(ims.wrap(&in, true) == STATUS_OK);
128 ssize_t total = 0;
129 lsp_swchar_t ch;
130
131 // Perform read with specific block size
132 while ((ch = ims.read()) >= 0)
133 data[total++] = ch;
134
135 UTEST_ASSERT(ch == -STATUS_EOF);
136 UTEST_ASSERT(total == ssize_t(s->length()));
137
138 // Check buffer
139 data = buf.data<lsp_wchar_t>();
140 UTEST_ASSERT(check_buffer(data, 1, s->length()));
141
142 // Close the stream
143 UTEST_ASSERT(ims.close() == STATUS_OK);
144 }
145
146 void test_full_with_mark(const LSPString *s)
147 {
148 printf("Testing full read of %d characters with mark\n", int(s->length()));
149
150 io::InStringSequence in(s);
151 io::InMarkSequence ims;
152 LSPString out;
153 ByteBuffer buf(s->length() * sizeof(lsp_wchar_t) * 2);
154 lsp_wchar_t *data = buf.data<lsp_wchar_t>();
155
156 // Test for mark() and wrap
157 UTEST_ASSERT(ims.wrap(&in, true) == STATUS_OK);
158 UTEST_ASSERT(ims.mark(s->length()) == STATUS_OK);
159
160 // Perform first read
161 ssize_t nread = ims.read(data, s->length() * 2);
162 UTEST_ASSERT(nread == ssize_t(s->length()));
163 UTEST_ASSERT(!buf.corrupted());
164 UTEST_ASSERT(check_buffer(data, 1, s->length()));
165
166 // Perform reset and second read
167 UTEST_ASSERT(ims.reset() == STATUS_OK);
168 nread = ims.read(&data[s->length()], s->length() * 2);
169 UTEST_ASSERT(nread == ssize_t(s->length()));
170 UTEST_ASSERT(!buf.corrupted());
171 UTEST_ASSERT(check_buffer(&data[s->length()], 1, s->length()));
172
173 // Perform third read - EOF should be returned
174 nread = ims.read(&data[s->length() * 2], s->length() * 2);
175 UTEST_ASSERT(!buf.corrupted());
176 UTEST_ASSERT(nread == -STATUS_EOF);
177
178 // Close the stream
179 UTEST_ASSERT(ims.close() == STATUS_OK);
180 }
181
182 void test_character_with_mark(const LSPString *s)
183 {
184 printf("Testing full read of %d characters with mark\n", int(s->length()));
185
186 io::InStringSequence in(s);
187 io::InMarkSequence ims;
188 LSPString out;
189 ByteBuffer buf(s->length() * sizeof(lsp_wchar_t) * 2);
190 lsp_wchar_t *data = buf.data<lsp_wchar_t>();
191
192 // Test for mark() and wrap
193 UTEST_ASSERT(ims.wrap(&in, true) == STATUS_OK);
194 UTEST_ASSERT(ims.mark(s->length()) == STATUS_OK);
195
196 // Perform first read
197 ssize_t total = 0;
198 lsp_swchar_t ch;
199 while ((ch = ims.read()) >= 0)
200 data[total++] = ch;
201
202 UTEST_ASSERT(ch == -STATUS_EOF);
203 UTEST_ASSERT(total == ssize_t(s->length()));
204 UTEST_ASSERT(!buf.corrupted());
205 UTEST_ASSERT(check_buffer(data, 1, s->length()));
206
207 // Perform reset and second read
208 UTEST_ASSERT(ims.reset() == STATUS_OK);
209 while ((ch = ims.read()) >= 0)
210 data[total++] = ch;
211
212 UTEST_ASSERT(ch == -STATUS_EOF);
213 UTEST_ASSERT(total == ssize_t(s->length() * 2));
214 UTEST_ASSERT(!buf.corrupted());
215 UTEST_ASSERT(check_buffer(&data[s->length()], 1, s->length()));
216
217 // Close the stream
218 UTEST_ASSERT(ims.close() == STATUS_OK);
219 }
220
221 void test_partial_with_mark(const LSPString *s, ssize_t start, ssize_t mark, ssize_t blk_size)
222 {
223 printf("Testing partial read of %d-%d/%d characters with mark\n", int(start), int(mark), int(blk_size));
224
225 io::InStringSequence in(s);
226 io::InMarkSequence ims;
227 LSPString out;
228 ByteBuffer buf(s->length() * sizeof(lsp_wchar_t) * 3);
229 lsp_wchar_t *data = buf.data<lsp_wchar_t>();
230 lsp_wchar_t *ptr = data;
231
232 // Test for mark() and wrap
233 UTEST_ASSERT(ims.wrap(&in, true) == STATUS_OK);
234 ssize_t nread = 0, total = 0;
235
236 // Perform read with specific block size
237 while (total < start)
238 {
239 nread = ims.read(data, lsp_min(blk_size, start - total));
240 UTEST_ASSERT(nread >= 0);
241 data += nread;
242 total += nread;
243 }
244 UTEST_ASSERT(total == start);
245 UTEST_ASSERT(check_buffer(ptr, 1, total));
246
247 // Perform mark and read next value
248 UTEST_ASSERT(ims.mark(mark) == STATUS_OK);
249 ptr += total;
250 total = 0;
251 while (total < mark)
252 {
253 nread = ims.read(data, lsp_min(blk_size, mark - total));
254 UTEST_ASSERT(nread >= 0);
255 data += nread;
256 total += nread;
257 }
258 UTEST_ASSERT(total == mark);
259 UTEST_ASSERT(check_buffer(ptr, start + 1, total));
260
261 // Perform reset and read again
262 UTEST_ASSERT(ims.reset() == STATUS_OK);
263 ptr += total;
264 total = 0;
265 while (total < mark*2)
266 {
267 nread = ims.read(data, lsp_min(blk_size, mark*2 - total));
268 UTEST_ASSERT(nread >= 0);
269 data += nread;
270 total += nread;
271 }
272 UTEST_ASSERT(total == mark*2);
273 UTEST_ASSERT(check_buffer(ptr, start + 1, total));
274
275 // Now reset() should fail, read the rest data
276 UTEST_ASSERT(ims.reset() == STATUS_NOT_FOUND);
277 ptr += total;
278 total = 0;
279 while ((nread = ims.read(data, blk_size)) >= 0)
280 {
281 data += nread;
282 total += nread;
283 }
284
285 UTEST_ASSERT(nread == -STATUS_EOF);
286 UTEST_ASSERT(total == ssize_t(s->length() - start - mark*2));
287 UTEST_ASSERT(check_buffer(ptr, start + mark*2 + 1, total));
288
289 // Close the stream
290 UTEST_ASSERT(ims.close() == STATUS_OK);
291 }
292
293 void test_char_partial_with_mark(const LSPString *s, ssize_t start, ssize_t mark)
294 {
295 printf("Testing partial character read of %d-%d characters with mark\n", int(start), int(mark));
296
297 io::InStringSequence in(s);
298 io::InMarkSequence ims;
299 LSPString out;
300 ByteBuffer buf(s->length() * sizeof(lsp_wchar_t) * 3);
301 lsp_wchar_t *data = buf.data<lsp_wchar_t>();
302 lsp_wchar_t *ptr = data;
303 lsp_swchar_t ch;
304
305 // Test for mark() and wrap
306 UTEST_ASSERT(ims.wrap(&in, true) == STATUS_OK);
307 ssize_t total = 0;
308
309 // Perform initial read
310 while (total < start)
311 {
312 UTEST_ASSERT((ch = ims.read()) >= 0);
313 *data++ = ch;
314 total++;
315 }
316 UTEST_ASSERT(total == start);
317 UTEST_ASSERT(check_buffer(ptr, 1, total));
318
319 // Perform mark and read next value
320 UTEST_ASSERT(ims.mark(mark) == STATUS_OK);
321 ptr = data;
322 total = 0;
323 while (total < mark)
324 {
325 UTEST_ASSERT((ch = ims.read()) >= 0);
326 *data++ = ch;
327 total++;
328 }
329 UTEST_ASSERT(total == mark);
330 UTEST_ASSERT(check_buffer(ptr, start + 1, total));
331
332 // Perform reset and read again
333 UTEST_ASSERT(ims.reset() == STATUS_OK);
334 ptr += total;
335 total = 0;
336 while (total < mark*2)
337 {
338 UTEST_ASSERT((ch = ims.read()) >= 0);
339 *data++ = ch;
340 total++;
341 }
342 UTEST_ASSERT(total == mark*2);
343 UTEST_ASSERT(check_buffer(ptr, start + 1, total));
344
345 // Now reset() should fail, read the rest data
346 UTEST_ASSERT(ims.reset() == STATUS_NOT_FOUND);
347 ptr += total;
348 total = 0;
349 while ((ch = ims.read()) >= 0)
350 {
351 *data++ = ch;
352 total++;
353 }
354
355 UTEST_ASSERT(ch == -STATUS_EOF);
356 UTEST_ASSERT(total == ssize_t(s->length() - start - mark*2));
357 UTEST_ASSERT(check_buffer(ptr, start + mark*2 + 1, total));
358
359 // Close the stream
360 UTEST_ASSERT(ims.close() == STATUS_OK);
361 }
362
363 UTEST_MAIN
364 {
365 LSPString src;
366 for (size_t i=0; i<10000; ++i)
367 {
368 UTEST_ASSERT(src.append(lsp_wchar_t(i + 1)));
369 }
370 UTEST_ASSERT(check_string(&src, 0, 10000));
371
372 // Perform tests
373 test_full_read(&src);
374 test_partial_read(&src, 1);
375 test_partial_read(&src, 31);
376 test_partial_read(&src, 1023);
377 test_partial_read(&src, 1024);
378 test_character_read(&src);
379
380 test_full_with_mark(&src);
381 test_character_with_mark(&src);
382
383 test_partial_with_mark(&src, 2048, 512, 512);
384 test_partial_with_mark(&src, 2048, 1024, 512);
385 test_partial_with_mark(&src, 2048, 2048, 512);
386 test_partial_with_mark(&src, 2048, 512, 31);
387 test_partial_with_mark(&src, 2048, 1024, 31);
388 test_partial_with_mark(&src, 2048, 2048, 31);
389 test_partial_with_mark(&src, 2048, 512, 1);
390 test_partial_with_mark(&src, 2048, 1024, 1);
391 test_partial_with_mark(&src, 2048, 2048, 1);
392
393 test_char_partial_with_mark(&src, 2048, 512);
394 test_char_partial_with_mark(&src, 2048, 1024);
395 test_char_partial_with_mark(&src, 2048, 2048);
396 test_char_partial_with_mark(&src, 2048, 512);
397 test_char_partial_with_mark(&src, 2048, 1024);
398 test_char_partial_with_mark(&src, 2048, 2048);
399 test_char_partial_with_mark(&src, 2048, 512);
400 test_char_partial_with_mark(&src, 2048, 1024);
401 test_char_partial_with_mark(&src, 2048, 2048);
402 }
403 UTEST_END
404
405
406
407
408