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, src: string, n: 0); |
52 | g_assert_cmpstr (dest, ==, "" ); |
53 | |
54 | g_utf8_strncpy (dest, src: string, n: 1); |
55 | g_assert_cmpstr (dest, ==, "\xe2\x82\xa0" ); |
56 | |
57 | g_utf8_strncpy (dest, src: string, n: 2); |
58 | g_assert_cmpstr (dest, ==, "\xe2\x82\xa0g" ); |
59 | |
60 | g_utf8_strncpy (dest, src: string, n: 3); |
61 | g_assert_cmpstr (dest, ==, "\xe2\x82\xa0gh" ); |
62 | |
63 | g_utf8_strncpy (dest, src: string, n: 4); |
64 | g_assert_cmpstr (dest, ==, "\xe2\x82\xa0gh\xe2\x82\xa4" ); |
65 | |
66 | g_utf8_strncpy (dest, src: string, n: 5); |
67 | g_assert_cmpstr (dest, ==, "\xe2\x82\xa0gh\xe2\x82\xa4j" ); |
68 | |
69 | g_utf8_strncpy (dest, src: string, n: 6); |
70 | g_assert_cmpstr (dest, ==, "\xe2\x82\xa0gh\xe2\x82\xa4jl" ); |
71 | |
72 | g_utf8_strncpy (dest, src: string, n: 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 (str: "abcdef" , len: -1); |
94 | g_assert_cmpstr (r, ==, "fedcba" ); |
95 | g_free (mem: r); |
96 | |
97 | r = g_utf8_strreverse (str: "abcdef" , len: 4); |
98 | g_assert_cmpstr (r, ==, "dcba" ); |
99 | g_free (mem: 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 (str: "\340\254\213\360\220\244\200\101\341\272\266" , len: -1); |
107 | g_assert_cmpstr (r, ==, "\341\272\266\101\360\220\244\200\340\254\213" ); |
108 | g_free (mem: r); |
109 | } |
110 | |
111 | static void |
112 | test_utf8_substring (void) |
113 | { |
114 | gchar *r; |
115 | |
116 | r = g_utf8_substring (str: "abcd" , start_pos: 1, end_pos: 3); |
117 | g_assert_cmpstr (r, ==, "bc" ); |
118 | g_free (mem: r); |
119 | |
120 | r = g_utf8_substring (str: "abcd" , start_pos: 0, end_pos: 4); |
121 | g_assert_cmpstr (r, ==, "abcd" ); |
122 | g_free (mem: r); |
123 | |
124 | r = g_utf8_substring (str: "abcd" , start_pos: 2, end_pos: 2); |
125 | g_assert_cmpstr (r, ==, "" ); |
126 | g_free (mem: r); |
127 | |
128 | r = g_utf8_substring (str: "abc\xe2\x82\xa0gh\xe2\x82\xa4" , start_pos: 2, end_pos: 5); |
129 | g_assert_cmpstr (r, ==, "c\xe2\x82\xa0g" ); |
130 | g_free (mem: r); |
131 | } |
132 | |
133 | static void |
134 | test_utf8_make_valid (void) |
135 | { |
136 | gchar *r; |
137 | |
138 | /* valid UTF8 */ |
139 | r = g_utf8_make_valid (str: "\xe2\x82\xa0gh\xe2\x82\xa4jl" , len: -1); |
140 | g_assert_cmpstr (r, ==, "\xe2\x82\xa0gh\xe2\x82\xa4jl" ); |
141 | g_free (mem: r); |
142 | |
143 | /* invalid UTF8 */ |
144 | r = g_utf8_make_valid (str: "\xe2\x82\xa0gh\xe2\xffjl" , len: -1); |
145 | g_assert_cmpstr (r, ==, "\xe2\x82\xa0gh\xef\xbf\xbd\xef\xbf\xbdjl" ); |
146 | g_free (mem: r); |
147 | |
148 | /* invalid UTF8 without nul terminator followed by something unfortunate */ |
149 | r = g_utf8_make_valid (str: "Bj\xc3\xb8" , len: 3); |
150 | g_assert_cmpstr (r, ==, "Bj\xef\xbf\xbd" ); |
151 | g_free (mem: r); |
152 | |
153 | /* invalid UTF8 with embedded nul */ |
154 | r = g_utf8_make_valid (str: "\xe2\x82\xa0gh\xe2\x00jl" , len: 9); |
155 | g_assert_cmpstr (r, ==, "\xe2\x82\xa0gh\xef\xbf\xbd\xef\xbf\xbdjl" ); |
156 | g_free (mem: r); |
157 | } |
158 | |
159 | int |
160 | main (int argc, |
161 | char *argv[]) |
162 | { |
163 | g_test_init (argc: &argc, argv: &argv, NULL); |
164 | |
165 | g_test_add_func (testpath: "/utf8/strlen" , test_func: test_utf8_strlen); |
166 | g_test_add_func (testpath: "/utf8/strncpy" , test_func: test_utf8_strncpy); |
167 | g_test_add_func (testpath: "/utf8/strrchr" , test_func: test_utf8_strrchr); |
168 | g_test_add_func (testpath: "/utf8/reverse" , test_func: test_utf8_reverse); |
169 | g_test_add_func (testpath: "/utf8/substring" , test_func: test_utf8_substring); |
170 | g_test_add_func (testpath: "/utf8/make-valid" , test_func: test_utf8_make_valid); |
171 | |
172 | return g_test_run(); |
173 | } |
174 | |