Tizen 2.1 base
[platform/upstream/glib2.0.git] / glib / tests / utf8-misc.c
1 /* Unit tests for utilities
2  * Copyright (C) 2010 Red Hat, Inc.
3  *
4  * This work is provided "as is"; redistribution and modification
5  * in whole or in part, in any medium, physical or electronic is
6  * permitted without restriction.
7  *
8  * This work is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * In no event shall the authors or contributors be liable for any
13  * direct, indirect, incidental, special, exemplary, or consequential
14  * damages (including, but not limited to, procurement of substitute
15  * goods or services; loss of use, data, or profits; or business
16  * interruption) however caused and on any theory of liability, whether
17  * in contract, strict liability, or tort (including negligence or
18  * otherwise) arising in any way out of the use of this software, even
19  * if advised of the possibility of such damage.
20  *
21  * Author: Matthias Clasen
22  */
23
24 #include "glib.h"
25
26 static void
27 test_utf8_strlen (void)
28 {
29   const gchar *string = "\xe2\x82\xa0gh\xe2\x82\xa4jl";
30
31   g_assert_cmpint (g_utf8_strlen (string, -1), ==, 6);
32   g_assert_cmpint (g_utf8_strlen (string, 0), ==, 0);
33   g_assert_cmpint (g_utf8_strlen (string, 1), ==, 0);
34   g_assert_cmpint (g_utf8_strlen (string, 2), ==, 0);
35   g_assert_cmpint (g_utf8_strlen (string, 3), ==, 1);
36   g_assert_cmpint (g_utf8_strlen (string, 4), ==, 2);
37   g_assert_cmpint (g_utf8_strlen (string, 5), ==, 3);
38   g_assert_cmpint (g_utf8_strlen (string, 6), ==, 3);
39   g_assert_cmpint (g_utf8_strlen (string, 7), ==, 3);
40   g_assert_cmpint (g_utf8_strlen (string, 8), ==, 4);
41   g_assert_cmpint (g_utf8_strlen (string, 9), ==, 5);
42   g_assert_cmpint (g_utf8_strlen (string, 10), ==, 6);
43 }
44
45 static void
46 test_utf8_strncpy (void)
47 {
48   const gchar *string = "\xe2\x82\xa0gh\xe2\x82\xa4jl";
49   gchar dest[20];
50
51   g_utf8_strncpy (dest, string, 0);
52   g_assert_cmpstr (dest, ==, "");
53
54   g_utf8_strncpy (dest, string, 1);
55   g_assert_cmpstr (dest, ==, "\xe2\x82\xa0");
56
57   g_utf8_strncpy (dest, string, 2);
58   g_assert_cmpstr (dest, ==, "\xe2\x82\xa0g");
59
60   g_utf8_strncpy (dest, string, 3);
61   g_assert_cmpstr (dest, ==, "\xe2\x82\xa0gh");
62
63   g_utf8_strncpy (dest, string, 4);
64   g_assert_cmpstr (dest, ==, "\xe2\x82\xa0gh\xe2\x82\xa4");
65
66   g_utf8_strncpy (dest, string, 5);
67   g_assert_cmpstr (dest, ==, "\xe2\x82\xa0gh\xe2\x82\xa4j");
68
69   g_utf8_strncpy (dest, string, 6);
70   g_assert_cmpstr (dest, ==, "\xe2\x82\xa0gh\xe2\x82\xa4jl");
71
72   g_utf8_strncpy (dest, string, 20);
73   g_assert_cmpstr (dest, ==, "\xe2\x82\xa0gh\xe2\x82\xa4jl");
74 }
75
76 static void
77 test_utf8_strrchr (void)
78 {
79   const gchar *string = "\xe2\x82\xa0gh\xe2\x82\xa4jl\xe2\x82\xa4jl";
80
81   g_assert (g_utf8_strrchr (string, -1, 'j') == string + 13);
82   g_assert (g_utf8_strrchr (string, -1, 8356) == string + 10);
83   g_assert (g_utf8_strrchr (string, 9, 8356) == string + 5);
84   g_assert (g_utf8_strrchr (string, 3, 'j') == NULL);
85   g_assert (g_utf8_strrchr (string, -1, 'x') == NULL);
86 }
87
88 static void
89 test_utf8_reverse (void)
90 {
91   gchar *r;
92
93   r = g_utf8_strreverse ("abcdef", -1);
94   g_assert_cmpstr (r, ==, "fedcba");
95   g_free (r);
96
97   r = g_utf8_strreverse ("abcdef", 4);
98   g_assert_cmpstr (r, ==, "dcba");
99   g_free (r);
100
101   /* U+0B0B Oriya Letter Vocalic R
102    * U+10900 Phoenician Letter Alf
103    * U+0041 Latin Capital Letter A
104    * U+1EB6 Latin Capital Letter A With Breve And Dot Below
105    */
106   r = g_utf8_strreverse ("\340\254\213\360\220\244\200\101\341\272\266", -1);
107   g_assert_cmpstr (r, ==, "\341\272\266\101\360\220\244\200\340\254\213");
108   g_free (r);
109 }
110
111 static void
112 test_utf8_substring (void)
113 {
114   gchar *r;
115
116   r = g_utf8_substring ("abcd", 1, 3);
117   g_assert_cmpstr (r, ==, "bc");
118   g_free (r);
119
120   r = g_utf8_substring ("abcd", 0, 4);
121   g_assert_cmpstr (r, ==, "abcd");
122   g_free (r);
123
124   r = g_utf8_substring ("abcd", 2, 2);
125   g_assert_cmpstr (r, ==, "");
126   g_free (r);
127
128   r = g_utf8_substring ("abc\xe2\x82\xa0gh\xe2\x82\xa4", 2, 5);
129   g_assert_cmpstr (r, ==, "c\xe2\x82\xa0g");
130   g_free (r);
131 }
132
133 int
134 main (int   argc,
135       char *argv[])
136 {
137   g_test_init (&argc, &argv, NULL);
138
139   g_test_add_func ("/utf8/strlen", test_utf8_strlen);
140   g_test_add_func ("/utf8/strncpy", test_utf8_strncpy);
141   g_test_add_func ("/utf8/strrchr", test_utf8_strrchr);
142   g_test_add_func ("/utf8/reverse", test_utf8_reverse);
143   g_test_add_func ("/utf8/substring", test_utf8_substring);
144
145   return g_test_run();
146 }