Debian Codegraph @

tests/unit_test.cxx

raw symbols SCIP
1 /* Copyright 2021-2024 Dimitrij Mijoski
2 *
3 * This file is part of Nuspell.
4 *
5 * Nuspell is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * Nuspell is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with Nuspell. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <catch2/catch_test_macros.hpp>
20 #include <catch2/matchers/catch_matchers.hpp>
21 #include <nuspell/dictionary.hxx>
22 #include <nuspell/utils.hxx>
23
24 using namespace std;
25 using namespace nuspell;
26
27 TEST_CASE("Subrange")
28 {
29 auto str = "abc"s;
30 auto s = Subrange(str);
31 STATIC_REQUIRE(is_same_v<decltype(s), Subrange<string::iterator>>);
32 REQUIRE(s.begin() == begin(str));
33 REQUIRE(s.end() == end(str));
34 }
35
36 TEST_CASE("String_Set")
37 {
38 auto ss = String_Set(u"zaZAa");
39 STATIC_REQUIRE(is_same_v<decltype(ss), String_Set<char16_t>>);
40 REQUIRE(ss == String_Set(u"AZaz"));
41 REQUIRE(ss.str() == u"AZaz");
42 REQUIRE(ss.size() == 4);
43 REQUIRE_FALSE(ss.empty());
44 REQUIRE(ss.contains(u'a'));
45 REQUIRE(ss.contains(u'A'));
46 REQUIRE(ss.contains(u'z'));
47 REQUIRE(ss.contains(u'Z'));
48 REQUIRE_FALSE(ss.contains(u'\0'));
49 REQUIRE_FALSE(ss.contains(u'b'));
50 REQUIRE_FALSE(ss.contains(u'B'));
51 REQUIRE(ss.lower_bound(u'a') == ss.begin() + 2);
52 REQUIRE(ss.upper_bound(u'a') == ss.begin() + 3);
53 REQUIRE(ss.equal_range(u'a') == pair(ss.begin() + 2, ss.begin() + 3));
54 REQUIRE(ss.lower_bound(u'b') == ss.begin() + 3);
55 REQUIRE(ss.upper_bound(u'b') == ss.begin() + 3);
56 REQUIRE(ss.equal_range(u'b') == pair(ss.begin() + 3, ss.begin() + 3));
57
58 ss.insert(u'b');
59 REQUIRE(ss == String_Set(u"AZabz"));
60 REQUIRE(ss.str() == u"AZabz");
61 REQUIRE(ss.size() == 5);
62 REQUIRE_FALSE(ss.empty());
63 REQUIRE(ss.contains(u'a'));
64 REQUIRE(ss.contains(u'A'));
65 REQUIRE(ss.contains(u'z'));
66 REQUIRE(ss.contains(u'Z'));
67 REQUIRE(ss.contains(u'b'));
68 REQUIRE_FALSE(ss.contains(u'\0'));
69 REQUIRE_FALSE(ss.contains(u'B'));
70
71 ss.erase(u'A');
72 ss.erase(u'b');
73 REQUIRE(ss == String_Set(u"Zaz"));
74 REQUIRE(ss.str() == u"Zaz");
75 REQUIRE(ss.size() == 3);
76 REQUIRE_FALSE(ss.empty());
77 REQUIRE(ss.contains(u'a'));
78 REQUIRE(ss.contains(u'z'));
79 REQUIRE(ss.contains(u'Z'));
80 REQUIRE_FALSE(ss.contains(u'\0'));
81 REQUIRE_FALSE(ss.contains(u'A'));
82 REQUIRE_FALSE(ss.contains(u'b'));
83 REQUIRE_FALSE(ss.contains(u'B'));
84 }
85
86 TEST_CASE("Substr_Replacer")
87 {
88 auto rep = Substr_Replacer({{"asd", "zxc"},
89 {"as", "rtt"},
90 {"a", "A"},
91 {"abbb", "ABBB"},
92 {"asd ", ""},
93 {"asd ZXC", "YES"},
94 {"sd ZXC as", "NO"},
95 {"", "123"},
96 {" TT", ""}});
97 REQUIRE(rep.replace_copy("QWE asd ZXC as TT") == "QWE YES rtt");
98 }
99
100 TEST_CASE("Break_Table")
101 {
102 auto b = Break_Table({
103 "bsd",
104 "zxc",
105 "asd",
106 "^bar",
107 "^zoo",
108 "^abc",
109 "car$",
110 "yoyo$",
111 "air$",
112 });
113 auto swb = b.start_word_breaks();
114 auto swb_v = vector<string>(begin(swb), end(swb));
115 sort(begin(swb_v), end(swb_v));
116 REQUIRE(swb_v == vector{"abc"s, "bar"s, "zoo"s});
117
118 auto mwb = b.middle_word_breaks();
119 auto mwb_v = vector<string>(begin(mwb), end(mwb));
120 sort(begin(mwb_v), end(mwb_v));
121 REQUIRE(mwb_v == vector{"asd"s, "bsd"s, "zxc"s});
122
123 auto ewb = b.end_word_breaks();
124 auto ewb_v = vector<string>(begin(ewb), end(ewb));
125 sort(begin(ewb_v), end(ewb_v));
126 REQUIRE(ewb_v == vector{"air"s, "car"s, "yoyo"s});
127 }
128
129 TEST_CASE("Hash_Multimap")
130 {
131 auto h = Hash_Multimap<string, int>();
132 REQUIRE(h.empty());
133 REQUIRE(h.size() == 0);
134 auto res = h.equal_range("hello");
135 #if !defined(_GLIBCXX_DEBUG) || _GLIBCXX_RELEASE >= 11
136 // ifdef because of
137 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70303
138 REQUIRE(res.first == res.second);
139 #endif
140
141 h.emplace("hello", 5);
142 res = h.equal_range("hello");
143 REQUIRE_FALSE(h.empty());
144 REQUIRE(h.size() == 1);
145 REQUIRE(res.first != res.second);
146 REQUIRE(next(res.first) == res.second);
147 REQUIRE(*res.first == pair("hello"s, 5));
148 res = h.equal_range("Hi");
149 REQUIRE(res.first == res.second);
150 res = h.equal_range("");
151 REQUIRE(res.first == res.second);
152
153 h.emplace("hello", 4);
154 res = h.equal_range("hello");
155 REQUIRE_FALSE(h.empty());
156 REQUIRE(h.size() == 2);
157 REQUIRE(res.first != res.second);
158 REQUIRE(next(res.first, 2) == res.second);
159 REQUIRE(*res.first == pair("hello"s, 5));
160 REQUIRE(*next(res.first) == pair("hello"s, 4));
161 res = h.equal_range("Hi");
162 REQUIRE(res.first == res.second);
163 res = h.equal_range("");
164 REQUIRE(res.first == res.second);
165 }
166
167 TEST_CASE("Condition")
168 {
169 auto c = Condition();
170 REQUIRE(c.match_prefix(""));
171 REQUIRE(c.match_prefix("a"));
172
173 REQUIRE(c.match_suffix(""));
174 REQUIRE(c.match_suffix("b"));
175
176 c = "abcd";
177 REQUIRE(c.match_prefix("abcd"));
178 REQUIRE(c.match_prefix("abcdXYZ"));
179 REQUIRE(c.match_prefix("abcdБВГДШ\uABCD\U0010ABCD"));
180 REQUIRE_FALSE(c.match_prefix(""));
181 REQUIRE_FALSE(c.match_prefix("abc"));
182 REQUIRE_FALSE(c.match_prefix("abcX"));
183 REQUIRE_FALSE(c.match_prefix("XYZ"));
184 REQUIRE_FALSE(c.match_prefix("АаБбВвГгШш\uABCD\U0010ABCD"));
185
186 REQUIRE(c.match_suffix("abcd"));
187 REQUIRE(c.match_suffix("XYZabcd"));
188 REQUIRE(c.match_suffix("БВГДШ\uABCD\U0010ABCDabcd"));
189 REQUIRE_FALSE(c.match_suffix(""));
190 REQUIRE_FALSE(c.match_suffix("bcd"));
191 REQUIRE_FALSE(c.match_suffix("Xbcd"));
192 REQUIRE_FALSE(c.match_suffix("XYZ"));
193 REQUIRE_FALSE(c.match_suffix("АаБбВвГгШш\uABCD\U0010ABCD"));
194
195 c = ".";
196 REQUIRE(c.match_prefix("Y"));
197 REQUIRE(c.match_prefix("abc"));
198 REQUIRE(c.match_prefix("БВГДШ\uABCD\U0010ABCD"));
199 REQUIRE_FALSE(c.match_prefix(""));
200
201 REQUIRE(c.match_suffix("Y"));
202 REQUIRE(c.match_suffix("qwe"));
203 REQUIRE(c.match_suffix("БВГДШ\uABCD\U0010ABCD"));
204 REQUIRE_FALSE(c.match_suffix(""));
205
206 c = "[vbn]";
207 REQUIRE(c.match_prefix("v"));
208 REQUIRE(c.match_prefix("vAAш"));
209 REQUIRE(c.match_prefix("b"));
210 REQUIRE(c.match_prefix("bBBш"));
211 REQUIRE(c.match_prefix("n"));
212 REQUIRE(c.match_prefix("nCCш"));
213 REQUIRE_FALSE(c.match_prefix(""));
214 REQUIRE_FALSE(c.match_prefix("Q"));
215 REQUIRE_FALSE(c.match_prefix("Qqqq"));
216 REQUIRE_FALSE(c.match_prefix("1342234"));
217 REQUIRE_FALSE(c.match_prefix("V"));
218 REQUIRE_FALSE(c.match_prefix("бвгдш"));
219
220 REQUIRE(c.match_suffix("v"));
221 REQUIRE(c.match_suffix("шVVv"));
222 REQUIRE(c.match_suffix("b"));
223 REQUIRE(c.match_suffix("шBBb"));
224 REQUIRE(c.match_suffix("n"));
225 REQUIRE(c.match_suffix("шNNn"));
226 REQUIRE_FALSE(c.match_suffix(""));
227 REQUIRE_FALSE(c.match_suffix("Q"));
228 REQUIRE_FALSE(c.match_suffix("Qqqq"));
229 REQUIRE_FALSE(c.match_suffix("123123"));
230 REQUIRE_FALSE(c.match_suffix("V"));
231 REQUIRE_FALSE(c.match_suffix("бвгдш"));
232
233 c = "[бш\u1234]";
234 REQUIRE(c.match_prefix("б"));
235 REQUIRE(c.match_prefix("беТ"));
236 REQUIRE(c.match_prefix("ш"));
237 REQUIRE(c.match_prefix("шок"));
238 REQUIRE(c.match_prefix("\u1234кош"));
239 REQUIRE_FALSE(c.match_prefix(""));
240 REQUIRE_FALSE(c.match_prefix("Q"));
241 REQUIRE_FALSE(c.match_prefix("Qqqq"));
242 REQUIRE_FALSE(c.match_prefix("пан"));
243 REQUIRE_FALSE(c.match_prefix("\uABCD\u1234"));
244 REQUIRE_FALSE(c.match_prefix("вбгдш"));
245
246 REQUIRE(c.match_suffix("б"));
247 REQUIRE(c.match_suffix("еТб"));
248 REQUIRE(c.match_suffix("ш"));
249 REQUIRE(c.match_suffix("кош"));
250 REQUIRE(c.match_suffix("кош\u1234"));
251 REQUIRE_FALSE(c.match_suffix(""));
252 REQUIRE_FALSE(c.match_suffix("Q"));
253 REQUIRE_FALSE(c.match_suffix("Qqqq"));
254 REQUIRE_FALSE(c.match_suffix("пан"));
255 REQUIRE_FALSE(c.match_suffix("\u1234\uABCD"));
256 REQUIRE_FALSE(c.match_suffix("вбгдз"));
257
258 c = "[^zш\u1234\U0010ABCD]";
259 REQUIRE_FALSE(c.match_prefix("z"));
260 REQUIRE_FALSE(c.match_prefix("ш"));
261 REQUIRE_FALSE(c.match_prefix("\u1234"));
262 REQUIRE_FALSE(c.match_prefix("\U0010ABCD"));
263 REQUIRE_FALSE(c.match_prefix("zљње"));
264 REQUIRE_FALSE(c.match_prefix("шabc"));
265 REQUIRE_FALSE(c.match_prefix("\u1234 ytyty"));
266 REQUIRE_FALSE(c.match_prefix("\U0010ABCD tytyty"));
267 REQUIRE(c.match_prefix("q"));
268 REQUIRE(c.match_prefix("r"));
269 REQUIRE(c.match_prefix("\uFFFD"));
270 REQUIRE(c.match_prefix("\U0010FFFF"));
271 REQUIRE(c.match_prefix("qљње"));
272 REQUIRE(c.match_prefix("фabc"));
273 REQUIRE(c.match_prefix("\uFFFD ytyty"));
274 REQUIRE(c.match_prefix("\U0010FFFF tytyty"));
275
276 REQUIRE_FALSE(c.match_suffix("z"));
277 REQUIRE_FALSE(c.match_suffix("ш"));
278 REQUIRE_FALSE(c.match_suffix("\u1234"));
279 REQUIRE_FALSE(c.match_suffix("\U0010ABCD"));
280 REQUIRE_FALSE(c.match_suffix("љњеz"));
281 REQUIRE_FALSE(c.match_suffix("abcш"));
282 REQUIRE_FALSE(c.match_suffix("ytyty \u1234"));
283 REQUIRE_FALSE(c.match_suffix("tytyty \U0010ABCD"));
284 REQUIRE(c.match_suffix("q"));
285 REQUIRE(c.match_suffix("r"));
286 REQUIRE(c.match_suffix("\uFFFD"));
287 REQUIRE(c.match_suffix("\U0010FFFF"));
288 REQUIRE(c.match_suffix("љњеq"));
289 REQUIRE(c.match_suffix("abcф"));
290 REQUIRE(c.match_suffix("ytyty \uFFFD"));
291 REQUIRE(c.match_suffix("tytyty \U0010FFFF"));
292
293 c = "abc АБВ..[zбш\u1234][^zш\u1234\U0010ABCD]X";
294 REQUIRE(c.match_prefix("abc АБВ \u2345z\U00011111X"));
295 REQUIRE(c.match_prefix("abc АБВ\u2345 ш\U00011112Xопop"));
296 REQUIRE_FALSE(c.match_prefix("abc ШШШ \u2345z\U00011111X"));
297 REQUIRE_FALSE(c.match_prefix("abc АБВ\u2345 t\U00011112Xопop"));
298 REQUIRE_FALSE(c.match_prefix("abc АБВ \u2345z\u1234X"));
299 }
300
301 TEST_CASE("Prefix")
302 {
303 auto pfx = Prefix{'F', false, "qw", "Qwe", {}, {}};
304 REQUIRE(pfx.to_derived_copy("qwrty") == "Qwerty");
305 REQUIRE(pfx.to_root_copy("Qwerty") == "qwrty");
306 }
307
308 TEST_CASE("Suffix")
309 {
310 auto sfx = Suffix{'F', false, "ie", "ying", {}, {}};
311 REQUIRE(sfx.to_derived_copy("pie") == "pying");
312 REQUIRE(sfx.to_root_copy("pying") == "pie");
313 }
314
315 TEST_CASE("Prefix_Multiset")
316 {
317 auto set =
318 Prefix_Multiset<string>{"", "a", "", "ab", "abx", "as",
319 "asdf", "axx", "as", "bqwe", "ba", "rqwe"};
320 auto expected = vector<string>{"", "", "a", "as", "as", "asdf"};
321 auto out = vector<string>();
322 set.copy_all_prefixes_of("asdfg", back_inserter(out));
323 CHECK(out == expected);
324
325 auto word = string("asdfg");
326 auto it = set.iterate_prefixes_of(word);
327 out.assign(begin(it), end(it));
328 REQUIRE(out == expected);
329 }
330
331 TEST_CASE("Suffix_Multiset")
332 {
333 auto set =
334 Suffix_Multiset<string>{"", "", "a", "b", "b", "ab",
335 "ub", "zb", "aub", "uub", "xub", "huub"};
336 auto expected = vector<string>{"", "", "b", "b", "ub", "uub", "huub"};
337 auto out = vector<string>();
338 auto word = string("ahahuub");
339 set.copy_all_prefixes_of(word, back_inserter(out));
340 CHECK(out == expected);
341
342 auto it = set.iterate_prefixes_of(word);
343 out.assign(begin(it), end(it));
344 REQUIRE(out == expected);
345 }
346
347 TEST_CASE("String_Pair")
348 {
349 auto x = String_Pair();
350 REQUIRE(x.str() == "");
351 REQUIRE(x.idx() == 0);
352 REQUIRE(x.first() == "");
353 REQUIRE(x.second() == "");
354
355 x.first("123qwe");
356 REQUIRE(x.str() == "123qwe");
357 REQUIRE(x.idx() == 6);
358 REQUIRE(x.first() == "123qwe");
359 REQUIRE(x.second() == "");
360
361 x.second("456z");
362 REQUIRE(x.str() == "123qwe456z");
363 REQUIRE(x.idx() == 6);
364 REQUIRE(x.first() == "123qwe");
365 REQUIRE(x.second() == "456z");
366
367 x = String_Pair("6789"s, "zxcvbnm"s);
368 REQUIRE(x.str() == "6789zxcvbnm");
369 REQUIRE(x.idx() == 4);
370 REQUIRE(x.first() == "6789");
371 REQUIRE(x.second() == "zxcvbnm");
372
373 x = String_Pair("6789zxcvbnm", 4);
374 REQUIRE(x.str() == "6789zxcvbnm");
375 REQUIRE(x.idx() == 4);
376 REQUIRE(x.first() == "6789");
377 REQUIRE(x.second() == "zxcvbnm");
378
379 REQUIRE_NOTHROW(String_Pair("6789", 4));
380 REQUIRE_THROWS_AS(String_Pair("6789", 5), std::out_of_range);
381 REQUIRE_THROWS_WITH(String_Pair("6789", 5), "word split is too long");
382 }
383
384 TEST_CASE("match_simple_regex()")
385 {
386 REQUIRE(match_simple_regex("abdff"s, "abc?de*ff"s));
387 REQUIRE(match_simple_regex("abcdff"s, "abc?de*ff"s));
388 REQUIRE(match_simple_regex("abdeeff"s, "abc?de*ff"s));
389 REQUIRE(match_simple_regex("abcdeff"s, "abc?de*ff"s));
390 REQUIRE_FALSE(match_simple_regex("abcdeeeefff"s, "abc?de*ff"s));
391 REQUIRE_FALSE(match_simple_regex("abccdeeeeff"s, "abc?de*ff"s));
392 REQUIRE_FALSE(match_simple_regex("qwerty"s, "abc?de*ff"s));
393 }
394
395 TEST_CASE("Similarity_Group")
396 {
397 auto sg = Similarity_Group("abc(AB)БШП(ghgh)");
398 REQUIRE(sg.chars == "abcБШП");
399 REQUIRE(sg.strings == vector{"AB"s, "ghgh"s});
400 }
401
402 TEST_CASE("utf32_to_utf8()")
403 {
404 REQUIRE(utf32_to_utf8(U"") == "");
405 REQUIRE(utf32_to_utf8(U"abcАбвг\uABCD\u1234\U0010ABCD") ==
406 "abcАбвг\uABCD\u1234\U0010ABCD");
407 }
408
409 TEST_CASE("is_all_ascii()")
410 {
411 REQUIRE(is_all_ascii(""));
412 REQUIRE(is_all_ascii("abcd\x7f"));
413 REQUIRE_FALSE(is_all_ascii("abcd\x80"));
414 REQUIRE_FALSE(is_all_ascii("abcd\xFF"));
415 }
416
417 TEST_CASE("latin1_to_ucs2()")
418 {
419 REQUIRE(latin1_to_ucs2("abcd\x7F\x80\xFF") ==
420 u"abcd\u007F\u0080\u00FF");
421 }
422
423 TEST_CASE("is_all_bmp()")
424 {
425 REQUIRE(is_all_bmp(u"abc\u00FF\uFFFF"));
426 REQUIRE_FALSE(is_all_bmp(u"abc\u00FF\uFFFF\U00010000"));
427 REQUIRE_FALSE(is_all_bmp(u"abc\U0010FFFF\u00FF\uFFFF"));
428 }
429
430 TEST_CASE("case conversion")
431 {
432 // These are simple tests that only test if we wrap properly.
433 // There is no need for extensive testing, ICU is well tested.
434 auto in = "grüßEN";
435 auto l = icu::Locale();
436 CHECK(to_lower(in, l) == "grüßen");
437 CHECK(to_upper(in, l) == "GRÜSSEN");
438 CHECK(to_title(in, l) == "Grüßen");
439
440 in = "isTAnbulI";
441 CHECK(to_lower(in, l) == "istanbuli");
442 CHECK(to_upper(in, l) == "ISTANBULI");
443 CHECK(to_title(in, l) == "Istanbuli");
444
445 l = {"tr_TR"};
446 CHECK(to_lower(in, l) == "istanbulı");
447 CHECK(to_upper(in, l) == "İSTANBULI");
448 CHECK(to_title(in, l) == "İstanbulı");
449 }
450
451 TEST_CASE("classify_casing()")
452 {
453 REQUIRE(classify_casing("") == Casing::SMALL);
454 REQUIRE(classify_casing("здраво") == Casing::SMALL);
455 REQUIRE(classify_casing("Здраво") == Casing::INIT_CAPITAL);
456 REQUIRE(classify_casing("ЗДРАВО") == Casing::ALL_CAPITAL);
457 REQUIRE(classify_casing("здРаВо") == Casing::CAMEL);
458 REQUIRE(classify_casing("ЗдрАво") == Casing::PASCAL);
459 }
460
461 TEST_CASE("is_number()")
462 {
463 REQUIRE_FALSE(is_number(""));
464 REQUIRE(is_number("1234567890"));
465 REQUIRE(is_number("-1234567890"));
466 REQUIRE(is_number("123.456.78-9,0"));
467 REQUIRE(is_number("-123.456.78-9,0"));
468 REQUIRE_FALSE(is_number("123..456.78-9,0"));
469 REQUIRE_FALSE(is_number("123.456.-78-9,0"));
470 REQUIRE_FALSE(is_number("123..456.78-9-,0"));
471 }
472
473 TEST_CASE("Dict_Base::forgotten_char_suggest()")
474 {
475 auto d = nuspell::Suggester();
476 d.words.emplace("Забвгд", u"");
477 d.words.emplace("абвШгд", u"");
478 d.words.emplace("абвгдИ", u"");
479 d.words.emplace("абвгдК", u"");
480 d.try_chars = "шизШИЗ";
481 auto in = "абвгд"s;
482 auto sugs = vector<string>();
483 d.forgotten_char_suggest(in, sugs);
484 REQUIRE(sugs == vector{"абвШгд"s, "абвгдИ"s, "Забвгд"s});
485 }