1#include "config.h"
2
3#include <locale.h>
4#include <glib.h>
5
6typedef struct
7{
8 const gchar *string;
9 const gchar *prefix;
10 const gchar *locale;
11 gboolean should_match;
12} SearchTest;
13
14/* Test word separators and case */
15SearchTest basic[] = {
16 { "Hello World", "he", "C", TRUE },
17 { "Hello World", "wo", "C", TRUE },
18 { "Hello World", "lo", "C", FALSE },
19 { "Hello World", "ld", "C", FALSE },
20 { "Hello-World", "wo", "C", TRUE },
21 { "HelloWorld", "wo", "C", FALSE },
22 { NULL, NULL, NULL, FALSE }
23};
24
25/* Test composed chars (accentued letters) */
26SearchTest composed[] = {
27 { "Jörgen", "jor", "sv_SE.UTF-8", TRUE },
28 { "Gaëtan", "gaetan", "fr_FR.UTF-8", TRUE },
29 { "élève", "ele", "fr_FR.UTF-8", TRUE },
30 { "Azais", "AzaÏs", "fr_FR.UTF-8", FALSE },
31 { "AzaÏs", "Azais", "fr_FR.UTF-8", TRUE },
32 { NULL, NULL, NULL, FALSE }
33};
34
35/* Test decomposed chars, they looks the same, but are actually
36 * composed of multiple unicodes */
37SearchTest decomposed[] = {
38 { "Jorgen", "Jör", "sv_SE.UTF-8", FALSE },
39 { "Jörgen", "jor", "sv_SE.UTF-8", TRUE },
40 { NULL, NULL, NULL, FALSE }
41};
42
43/* Turkish special case */
44SearchTest turkish[] = {
45 { "İstanbul", "ist", "tr_TR.UTF-8", TRUE },
46 { "Diyarbakır", "diyarbakir", "tr_TR.UTF-8", TRUE },
47 { NULL, NULL, NULL, FALSE }
48};
49
50/* Test unicode chars when no locale is available */
51SearchTest c_locale_unicode[] = {
52 { "Jörgen", "jor", "C", TRUE },
53 { "Jorgen", "Jör", "C", FALSE },
54 { "Jörgen", "jor", "C", TRUE },
55 { NULL, NULL, NULL, FALSE }
56};
57
58/* Multi words */
59SearchTest multi_words[] = {
60 { "Xavier Claessens", "Xav Cla", "C", TRUE },
61 { "Xavier Claessens", "Cla Xav", "C", TRUE },
62 { "Foo Bar Baz", " b ", "C", TRUE },
63 { "Foo Bar Baz", "bar bazz", "C", FALSE },
64 { NULL, NULL, NULL, FALSE }
65};
66
67static void
68test_search (gconstpointer d)
69{
70 const SearchTest *tests = d;
71 guint i;
72 gboolean all_skipped = TRUE;
73
74 g_debug ("Started");
75
76 for (i = 0; tests[i].string != NULL; i++)
77 {
78 gboolean match;
79 gboolean ok;
80 gboolean skipped;
81
82 if (setlocale (LC_ALL, locale: tests[i].locale))
83 {
84 skipped = FALSE;
85 all_skipped = FALSE;
86 match = g_str_match_string (search_term: tests[i].prefix, potential_hit: tests[i].string, TRUE);
87 ok = (match == tests[i].should_match);
88 }
89 else
90 {
91 skipped = TRUE;
92 g_test_message (format: "Locale '%s' is unavailable", tests[i].locale);
93 }
94
95 g_debug ("'%s' - '%s' %s: %s", tests[i].prefix, tests[i].string,
96 tests[i].should_match ? "should match" : "should NOT match",
97 skipped ? "SKIPPED" : ok ? "OK" : "FAILED");
98
99 g_assert (skipped || ok);
100 }
101
102 if (all_skipped)
103 g_test_skip (msg: "No locales for the test set are available");
104}
105
106int
107main (int argc,
108 char **argv)
109{
110 gchar *user_locale;
111
112 g_test_init (argc: &argc, argv: &argv, NULL);
113
114 setlocale (LC_ALL, locale: "");
115 user_locale = setlocale (LC_ALL, NULL);
116 g_debug ("Current user locale: %s", user_locale);
117
118 g_test_add_data_func (testpath: "/search/basic", test_data: basic, test_func: test_search);
119 g_test_add_data_func (testpath: "/search/composed", test_data: composed, test_func: test_search);
120 g_test_add_data_func (testpath: "/search/decomposed", test_data: decomposed, test_func: test_search);
121 g_test_add_data_func (testpath: "/search/turkish", test_data: turkish, test_func: test_search);
122 g_test_add_data_func (testpath: "/search/c_locale_unicode", test_data: c_locale_unicode, test_func: test_search);
123 g_test_add_data_func (testpath: "/search/multi_words", test_data: multi_words, test_func: test_search);
124
125 return g_test_run ();
126}
127

source code of gtk/subprojects/glib/glib/tests/search-utils.c