• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "config.h"
2 
3 #include <locale.h>
4 #include <glib.h>
5 
6 typedef 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 */
15 SearchTest 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) */
26 SearchTest 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 */
37 SearchTest 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 */
44 SearchTest 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 */
51 SearchTest 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 */
59 SearchTest 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 
67 static void
test_search(gconstpointer d)68 test_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, tests[i].locale))
83         {
84           skipped = FALSE;
85           all_skipped = FALSE;
86           match = g_str_match_string (tests[i].prefix, tests[i].string, TRUE);
87           ok = (match == tests[i].should_match);
88         }
89       else
90         {
91           skipped = TRUE;
92           g_test_message ("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 ("No locales for the test set are available");
104 }
105 
106 int
main(int argc,char ** argv)107 main (int argc,
108       char **argv)
109 {
110   gchar *user_locale;
111 
112   g_test_init (&argc, &argv, NULL);
113 
114   setlocale (LC_ALL, "");
115   user_locale = setlocale (LC_ALL, NULL);
116   g_debug ("Current user locale: %s", user_locale);
117 
118   g_test_add_data_func ("/search/basic", basic, test_search);
119   g_test_add_data_func ("/search/composed", composed, test_search);
120   g_test_add_data_func ("/search/decomposed", decomposed, test_search);
121   g_test_add_data_func ("/search/turkish", turkish, test_search);
122   g_test_add_data_func ("/search/c_locale_unicode", c_locale_unicode, test_search);
123   g_test_add_data_func ("/search/multi_words", multi_words, test_search);
124 
125   return g_test_run ();
126 }
127