Add testcase for search utilities functions
[platform/upstream/glib.git] / glib / tests / search-utils.c
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   gboolean should_match;
11 } SearchTest;
12
13 static void
14 test_search (void)
15 {
16   SearchTest tests[] =
17     {
18       /* Test word separators and case */
19       { "Hello World", "he", TRUE },
20       { "Hello World", "wo", TRUE },
21       { "Hello World", "lo", FALSE },
22       { "Hello World", "ld", FALSE },
23       { "Hello-World", "wo", TRUE },
24       { "HelloWorld", "wo", FALSE },
25
26       /* Test composed chars (accentued letters) */
27       { "Jörgen", "jor", TRUE },
28       { "Gaëtan", "gaetan", TRUE },
29       { "élève", "ele", TRUE },
30       { "Azais", "AzaÏs", FALSE },
31       { "AzaÏs", "Azais", TRUE },
32
33       /* Test decomposed chars, they looks the same, but are actually
34        * composed of multiple unicodes */
35       { "Jorgen", "Jör", FALSE },
36       { "Jörgen", "jor", TRUE },
37
38       /* Multi words */
39       { "Xavier Claessens", "Xav Cla", TRUE },
40       { "Xavier Claessens", "Cla Xav", TRUE },
41       { "Foo Bar Baz", "   b  ", TRUE },
42       { "Foo Bar Baz", "bar bazz", FALSE },
43
44       { NULL, NULL, FALSE }
45     };
46   guint i;
47
48   setlocale(LC_ALL, "");
49
50   g_debug ("Started");
51   for (i = 0; tests[i].string != NULL; i ++)
52     {
53       gboolean match;
54       gboolean ok;
55
56       match = g_str_match_string (tests[i].prefix, tests[i].string, TRUE);
57       ok = (match == tests[i].should_match);
58
59       g_debug ("'%s' - '%s' %s: %s", tests[i].prefix, tests[i].string,
60           tests[i].should_match ? "should match" : "should NOT match",
61           ok ? "OK" : "FAILED");
62
63       g_assert (ok);
64     }
65 }
66
67 int
68 main (int argc,
69       char **argv)
70 {
71   g_test_init (&argc, &argv, NULL);
72
73   g_test_add_func ("/search", test_search);
74
75   return g_test_run ();
76 }