Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/runtime/string.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: 28 авг. 2018 г.
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/runtime/LSPString.h>
24 #include <lsp-plug.in/lltl/pphash.h>
25
26 namespace lsp
27 {
28 static const lsp_utf16_t utf16_ja[] =
29 {
30 0x6DBC, 0x5BAE, 0x30CF, 0x30EB, 0x30D2, 0x306E, 0x6182, 0x9B31, 0xFF1A, 0x7B2C, 0x4E00, 0x7AE0, 0x002E,
31 0
32 };
33
34 static const lsp_utf16_t utf16_ru[] =
35 {
36 0x0412, 0x0441, 0x0435, 0x043C, 0x0020, 0x043F, 0x0440, 0x0438, 0x0432, 0x0435, 0x0442, 0x0021,
37 0
38 };
39
40 static const lsp_utf16_t utf16_ja_inv[] =
41 {
42 0x6DBC, 0x5BAE, 0x30CF, 0x30EB, 0x30D2, 0x306E, 0x6182, 0x9B31, 0xFF1A, 0x7B2C, 0x4E00, 0x7AE0, 0x002E,
43 0xDC00, 0
44 };
45
46 static const lsp_utf16_t utf16_ru_inv[] =
47 {
48 0x0412, 0x0441, 0x0435, 0x043C, 0x0020, 0x043F, 0x0440, 0x0438, 0x0432, 0x0435, 0x0442, 0x0021,
49 0xD800, 0
50 };
51 }
52
53 UTEST_BEGIN("runtime.runtime", string)
54
55 void test_basic()
56 {
57 LSPString s1, s2, s3, s4, s5, s6, s7;
58
59 printf("Performing basic test...\n");
60
61 // Settings
62 UTEST_ASSERT(s1.set_ascii("This is some text"));
63 UTEST_ASSERT(s1.get_native() != NULL);
64 printf("s1 = %s\n", s1.get_native());
65
66 UTEST_ASSERT(s2.set_utf8("Всем привет!"));
67 UTEST_ASSERT(s2.get_native() != NULL);
68 printf("s2 = %s\n", s2.get_native());
69
70 UTEST_ASSERT(s3.set_utf8("涼宮ハルヒの憂鬱:第一章."));
71 UTEST_ASSERT(s3.get_native() != NULL);
72 printf("s3 = %s\n", s3.get_native());
73
74 UTEST_ASSERT(s4.set_ascii("Всем привет!"));
75 UTEST_ASSERT(s4.get_native() != NULL);
76 printf("s4 = %s\n", s4.get_native());
77
78 UTEST_ASSERT(s5.set(&s1));
79 UTEST_ASSERT(s5.get_native() != NULL);
80 printf("s5 = %s\n", s5.get_native());
81
82 UTEST_ASSERT(s6.set_utf16(utf16_ja));
83 UTEST_ASSERT(s6.get_native() != NULL);
84 printf("s6 = %s\n", s6.get_native());
85
86 UTEST_ASSERT(s7.set_utf16(utf16_ru));
87 UTEST_ASSERT(s7.get_native() != NULL);
88 printf("s7 = %s\n", s7.get_native());
89
90 UTEST_ASSERT(s1.length() == 17);
91 UTEST_ASSERT(s2.length() == 12);
92 UTEST_ASSERT(s3.length() == 13);
93 UTEST_ASSERT(s4.length() == 22);
94 UTEST_ASSERT(s5.length() == s1.length());
95 UTEST_ASSERT(s6.length() == 13);
96 UTEST_ASSERT(s7.length() == 12);
97 UTEST_ASSERT(s2.equals(&s7));
98 UTEST_ASSERT(s3.equals(&s6));
99
100 // Test data coding
101 UTEST_ASSERT(s6.set_utf8("涼宮ハルヒの憂鬱:第一章."));
102 UTEST_ASSERT(s7.set_utf16(s6.get_utf16()));
103 UTEST_ASSERT(s6.compare_to(&s7) == 0);
104 s7.clear();
105 UTEST_ASSERT(s7.set_utf8(s6.get_utf8()));
106 UTEST_ASSERT(s6.compare_to(&s7) == 0);
107
108 UTEST_ASSERT(s6.set_utf8("Всем привет!"));
109 UTEST_ASSERT(s7.set_utf16(s6.get_utf16()));
110 UTEST_ASSERT(s6.equals(&s7));
111 s7.clear();
112 UTEST_ASSERT(s7.set_utf8(s6.get_utf8()));
113 UTEST_ASSERT(s6.equals(&s7));
114
115 UTEST_ASSERT(s6.set_utf8("This is some text"));
116 UTEST_ASSERT(s7.set_utf16(s6.get_utf16()));
117 UTEST_ASSERT(s6.equals(&s7));
118 s7.clear();
119 UTEST_ASSERT(s7.set_utf8(s6.get_utf8()));
120 UTEST_ASSERT(s6.equals(&s7));
121
122 // Test some invalid sequences
123 UTEST_ASSERT(s6.set_utf8("涼宮ハルヒの憂鬱:第一章.\xff"));
124 UTEST_ASSERT(s6.length() == 14);
125 UTEST_ASSERT(s6.last() == 0xfffd);
126 UTEST_ASSERT(s6.get_native() != NULL);
127 printf("s6 = %s\n", s6.get_native());
128
129 UTEST_ASSERT(s6.set_utf16(utf16_ja_inv));
130 UTEST_ASSERT(s6.length() == 14);
131 UTEST_ASSERT(s6.last() == 0xfffd);
132 UTEST_ASSERT(s6.get_native() != NULL);
133 printf("s6 = %s\n", s6.get_native());
134
135 UTEST_ASSERT(s6.set_utf16(utf16_ru_inv));
136 UTEST_ASSERT(s6.length() == 13);
137 UTEST_ASSERT(s6.last() == 0xfffd);
138 UTEST_ASSERT(s6.get_native() != NULL);
139 printf("s6 = %s\n", s6.get_native());
140
141 // Perform operations
142 UTEST_ASSERT(s3.set(&s1, 8, 12)); // "some"
143 UTEST_ASSERT(s3.get_native() != NULL);
144 printf("s3 = %s\n", s3.get_native());
145
146 UTEST_ASSERT(s4.set(&s1, -9, 12)); // "some"
147 UTEST_ASSERT(s4.get_native() != NULL);
148 printf("s4 = %s\n", s4.get_native());
149
150 UTEST_ASSERT(s5.set(&s1, -4, -10)); // empty because -5 < -9
151 UTEST_ASSERT(s5.is_empty());
152 UTEST_ASSERT(s5.get_native() != NULL);
153 printf("s5 = %s\n", s5.get_native());
154 UTEST_ASSERT(s3.equals(&s4));
155
156 UTEST_ASSERT(s5.length() == 0);
157
158 s3.toupper(); // "SOME"
159 printf("s3 = %s\n", s3.get_native());
160 UTEST_ASSERT(s3.get_native() != NULL);
161
162 s1.toupper(); // "THIS IS SOME TEXT"
163 UTEST_ASSERT(s1.get_native() != NULL);
164 printf("s1 = %s\n", s1.get_native());
165
166 s4.clear(); // ""
167 UTEST_ASSERT(s4.get_native() != NULL);
168 printf("s4 = %s\n", s4.get_native());
169
170 UTEST_ASSERT(s4.is_empty());
171 UTEST_ASSERT(s1.index_of(&s3) == 8);
172 s1.swap(&s3); // s1 = "SOME", s3 = "THIS IS SOME TEXT"
173
174 UTEST_ASSERT(s1.index_of(&s1) == 0);
175 UTEST_ASSERT(s1.index_of(0, &s1) == 0);
176
177 UTEST_ASSERT(s1.get_native() != NULL);
178 printf("s1 = %s\n", s1.get_native());
179
180 UTEST_ASSERT(s3.get_native() != NULL);
181 printf("s3 = %s\n", s3.get_native());
182 UTEST_ASSERT(s3.index_of(&s1) == 8);
183
184 s4.truncate(); // ""
185 UTEST_ASSERT(s4.get_native() != NULL);
186 printf("s4 = %s\n", s4.get_native());
187 UTEST_ASSERT(s4.is_empty());
188 UTEST_ASSERT(s4.capacity() == s4.length());
189
190 // Search and insert
191 UTEST_ASSERT(s1.set_ascii("ABAABBAAABBBAAAABBBB")); // "ABAABBAAABBBAAAABBBB"
192 UTEST_ASSERT(s1.get_native() != NULL);
193 printf("s1 = %s\n", s1.get_native());
194
195 UTEST_ASSERT(s2.set_ascii("CCCC")); // "CCCC"
196 UTEST_ASSERT(s2.get_native() != NULL);
197 printf("s2 = %s\n", s2.get_native());
198
199 UTEST_ASSERT(s3.set_ascii("CC")); // "CC"
200 UTEST_ASSERT(s3.get_native() != NULL);
201 printf("s3 = %s\n", s3.get_native());
202
203 UTEST_ASSERT(s4.set_ascii("abaaccccbbccccaaabbbaaaabbbb")); // "abaaccccbbccccaaabbbaaaabbbb"
204 UTEST_ASSERT(s4.get_native() != NULL);
205 printf("s4 = %s\n", s4.get_native());
206
207 UTEST_ASSERT(s1.insert(4, &s2)); // ABAACCCCBBAAABBBAAAABBBB
208 UTEST_ASSERT(s1.get_native() != NULL);
209 printf("s1 = %s\n", s1.get_native());
210
211 UTEST_ASSERT(s1.insert(10, &s2)); // ABAACCCCBBCCCCAAABBBAAAABBBB
212 UTEST_ASSERT(s1.get_native() != NULL);
213 printf("s1 = %s\n", s1.get_native());
214 UTEST_ASSERT(s1.equals_nocase(&s4));
215
216 UTEST_ASSERT(s1.index_of(&s3) == 4);
217 UTEST_ASSERT(s1.index_of(5, &s3) == 5);
218 UTEST_ASSERT(s1.index_of(6, &s3) == 6);
219 UTEST_ASSERT(s1.index_of(7, &s3) == 10);
220 UTEST_ASSERT(s1.index_of(-21, &s3) == 10);
221 UTEST_ASSERT(s1.rindex_of(&s3) == 12);
222 UTEST_ASSERT(s1.rindex_of(10, &s3) == 6);
223 UTEST_ASSERT(s1.rindex_of(15, &s3) == 12);
224 UTEST_ASSERT(s1.rindex_of(s1.length(), &s3) == 12);
225
226 UTEST_ASSERT(s1.rindex_of(&s1) == 0);
227 UTEST_ASSERT(s1.rindex_of(s1.length(), &s1) == 0);
228
229 UTEST_ASSERT(s5.set_ascii("BBBBCCCC"));
230 UTEST_ASSERT(s5.get_native() != NULL);
231 printf("s5 = %s\n", s5.get_native());
232
233 UTEST_ASSERT(s4.set_ascii("CCABAACCCCBB"));
234 UTEST_ASSERT(s4.get_native() != NULL);
235 printf("s4 = %s\n", s4.get_native());
236
237 UTEST_ASSERT(s1.append(&s2));
238 UTEST_ASSERT(s1.get_native() != NULL);
239 printf("s1 = %s\n", s1.get_native());
240
241 UTEST_ASSERT(s1.prepend(&s3));
242 UTEST_ASSERT(s1.get_native() != NULL);
243 printf("s1 = %s\n", s1.get_native());
244
245 UTEST_ASSERT(s1.starts_with(&s4));
246 UTEST_ASSERT(s1.ends_with(&s5));
247 }
248
249 void test_start_end()
250 {
251 LSPString s;
252 UTEST_ASSERT(s.set_ascii("abcdefghij"));
253
254 // starts_with
255 UTEST_ASSERT(s.starts_with('a'));
256 UTEST_ASSERT(!s.starts_with('b'));
257 UTEST_ASSERT(s.starts_with('e', 4));
258 UTEST_ASSERT(!s.starts_with('d', 4));
259 UTEST_ASSERT(!s.starts_with('f', 4));
260
261 // starts_with_nocase
262 UTEST_ASSERT(s.starts_with_nocase('A'));
263 UTEST_ASSERT(!s.starts_with_nocase('B'));
264 UTEST_ASSERT(s.starts_with_nocase('E', 4));
265 UTEST_ASSERT(!s.starts_with_nocase('D', 4));
266 UTEST_ASSERT(!s.starts_with_nocase('F', 4));
267
268 // ends_with
269 UTEST_ASSERT(s.ends_with('j'));
270 UTEST_ASSERT(!s.ends_with('i'));
271
272 // ends_with_nocase
273 UTEST_ASSERT(s.ends_with_nocase('J'));
274 UTEST_ASSERT(!s.ends_with_nocase('I'));
275
276 // starts_with_ascii
277 UTEST_ASSERT(s.starts_with_ascii(""));
278 UTEST_ASSERT(s.starts_with_ascii("a"));
279 UTEST_ASSERT(s.starts_with_ascii("ab"));
280 UTEST_ASSERT(!s.starts_with_ascii("b"));
281 UTEST_ASSERT(s.starts_with_ascii("b", 1));
282 UTEST_ASSERT(s.starts_with_ascii("bc", 1));
283 UTEST_ASSERT(!s.starts_with_ascii("c", 1));
284 UTEST_ASSERT(s.starts_with_ascii("", 10));
285 UTEST_ASSERT(!s.starts_with_ascii("j", 10));
286 UTEST_ASSERT(!s.starts_with_ascii("", 11));
287 UTEST_ASSERT(s.starts_with_ascii("abcdefghij"));
288 UTEST_ASSERT(!s.starts_with_ascii("abcdefghijk"));
289
290 // starts_with_ascii_nocase
291 UTEST_ASSERT(s.starts_with_ascii_nocase(""));
292 UTEST_ASSERT(s.starts_with_ascii_nocase("A"));
293 UTEST_ASSERT(s.starts_with_ascii_nocase("AB"));
294 UTEST_ASSERT(!s.starts_with_ascii_nocase("B"));
295 UTEST_ASSERT(s.starts_with_ascii_nocase("B", 1));
296 UTEST_ASSERT(s.starts_with_ascii_nocase("BC", 1));
297 UTEST_ASSERT(!s.starts_with_ascii_nocase("C", 1));
298 UTEST_ASSERT(s.starts_with_ascii_nocase("", 10));
299 UTEST_ASSERT(!s.starts_with_ascii_nocase("J", 10));
300 UTEST_ASSERT(!s.starts_with_ascii_nocase("", 11));
301
302 // ends_with_ascii
303 UTEST_ASSERT(s.ends_with_ascii(""));
304 UTEST_ASSERT(s.ends_with_ascii("j"));
305 UTEST_ASSERT(!s.ends_with_ascii("i"));
306 UTEST_ASSERT(s.ends_with_ascii("ij"));
307 UTEST_ASSERT(s.ends_with_ascii("abcdefghij"));
308 UTEST_ASSERT(!s.ends_with_ascii("zabcdefghij"));
309
310 // ends_with_ascii_nocase
311 UTEST_ASSERT(s.ends_with_ascii_nocase(""));
312 UTEST_ASSERT(s.ends_with_ascii_nocase("J"));
313 UTEST_ASSERT(!s.ends_with_ascii_nocase("I"));
314 UTEST_ASSERT(s.ends_with_ascii_nocase("IJ"));
315 UTEST_ASSERT(s.ends_with_ascii_nocase("ABCDEFGHIJ"));
316 UTEST_ASSERT(!s.ends_with_ascii_nocase("ZABCDEFGHIJ"));
317 }
318
319 void test_base_hashing()
320 {
321 printf("Performing hashing test...\n");
322
323 size_t h;
324 LSPString s;
325 h = s.hash();
326 UTEST_ASSERT(h == 0);
327
328 UTEST_ASSERT(s.set_ascii("test"));
329 h = s.hash();
330 UTEST_ASSERT(h != 0);
331
332 UTEST_ASSERT(s.append('1'));
333 UTEST_ASSERT(h != s.hash());
334 h = s.hash();
335
336 UTEST_ASSERT(s.prepend('A'));
337 UTEST_ASSERT(h != s.hash());
338 h = s.hash();
339
340 UTEST_ASSERT(s.prepend_ascii("__"));
341 UTEST_ASSERT(h != s.hash());
342 h = s.hash();
343
344 UTEST_ASSERT(s.append_ascii("__"));
345 UTEST_ASSERT(h != s.hash());
346 h = s.hash();
347
348 UTEST_ASSERT(s.set_length(0) == 0);
349 UTEST_ASSERT(h != s.hash());
350 h = s.hash();
351 UTEST_ASSERT(h == 0);
352 }
353
354 void test_hash_key()
355 {
356 lltl::pphash<LSPString, LSPString> h;
357
358 printf("Testing lltl::pphash support...\n");
359
360 // Put values
361 for (size_t i=0; i<10; ++i)
362 {
363 LSPString *v = new LSPString();
364 UTEST_ASSERT(v != NULL);
365 UTEST_ASSERT(v->fmt_ascii("String %d", int(i)) >= 0);
366 UTEST_ASSERT(h.put(v, v, NULL));
367 }
368 UTEST_ASSERT(h.size() == 10);
369
370 // Fetch values
371 for (size_t i=0; i<10; ++i)
372 {
373 LSPString tmp;
374 UTEST_ASSERT(tmp.fmt_ascii("String %d", int(i)) >= 0);
375 LSPString **s = h.wbget(&tmp);
376 UTEST_ASSERT(s != NULL);
377 UTEST_ASSERT(*s != NULL);
378 UTEST_ASSERT((*s)->equals(&tmp));
379 delete *s;
380 *s = NULL;
381 }
382 UTEST_ASSERT(h.size() == 10);
383 }
384
385 void test_line_convert()
386 {
387 typedef struct cv_t {
388 const char *src;
389 const char *dos;
390 const char *unx;
391 } cv_t;
392
393 static const cv_t cases[] =
394 {
395 { "", "", "" },
396 { "test", "test", "test" },
397 { "some line", "some line", "some line" },
398
399 { "\n", "\r\n", "\n" },
400 { "\r", "\r", "\r" },
401 { "\n\n", "\r\n\r\n", "\n\n" },
402 { "\r\n", "\r\n", "\n" },
403 { "\n\r\n", "\r\n\r\n", "\n\n" },
404 { "\n\r\n\r", "\r\n\r\n\r", "\n\n\r" },
405 { "\r\n\r\n", "\r\n\r\n", "\n\n" },
406 { "\n\r\r\n\r\r", "\r\n\r\r\n\r\r", "\n\r\n\r\r" },
407
408 { "Two\nlines", "Two\r\nlines", "Two\nlines" },
409 { "Two\r\nlines", "Two\r\nlines", "Two\nlines" },
410 { "Three\nlines\n", "Three\r\nlines\r\n", "Three\nlines\n" },
411 { "Three\r\nlines\r\n", "Three\r\nlines\r\n", "Three\nlines\n" },
412 { "Three\r\nlines\n", "Three\r\nlines\r\n", "Three\nlines\n" },
413 { "Three\nlines\r\n", "Three\r\nlines\r\n", "Three\nlines\n" },
414
415 { "\nYet\nAnother\nTest\nCase\n", "\r\nYet\r\nAnother\r\nTest\r\nCase\r\n", "\nYet\nAnother\nTest\nCase\n" },
416 { "\r\nYet\nAnother\nTest\nCase\n", "\r\nYet\r\nAnother\r\nTest\r\nCase\r\n", "\nYet\nAnother\nTest\nCase\n" },
417 { "\n\rYet\nAnother\nTest\nCase\r", "\r\n\rYet\r\nAnother\r\nTest\r\nCase\r", "\n\rYet\nAnother\nTest\nCase\r" },
418 };
419
420 printf("Testing dos/unix line conversion\n");
421
422 for (size_t i=0, n=sizeof(cases)/sizeof(cv_t); i<n; ++i)
423 {
424 LSPString a, b;
425 printf("Testing string #%d: %s\n", int(i), cases[i].src);
426 UTEST_ASSERT(a.set_ascii(cases[i].src));
427 UTEST_ASSERT(b.set_ascii(cases[i].src));
428
429 UTEST_ASSERT(a.to_dos());
430 UTEST_ASSERT(a.equals_ascii(cases[i].dos));
431
432 UTEST_ASSERT(b.to_unix());
433 UTEST_ASSERT(b.equals_ascii(cases[i].unx));
434 }
435 }
436
437 UTEST_MAIN
438 {
439 test_basic();
440 test_start_end();
441 test_base_hashing();
442 test_hash_key();
443 test_line_convert();
444 }
445 UTEST_END;
446
447
448
449