1 /* Unit tests for utilities
2 * Copyright (C) 2010 Red Hat, Inc.
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.
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.
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.
21 * Author: Matthias Clasen
27 test_utf8_strlen (void)
29 const gchar
*string
= "\xe2\x82\xa0gh\xe2\x82\xa4jl";
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);
46 test_utf8_strncpy (void)
48 const gchar
*string
= "\xe2\x82\xa0gh\xe2\x82\xa4jl";
51 g_utf8_strncpy (dest
, string
, 0);
52 g_assert_cmpstr (dest
, ==, "");
54 g_utf8_strncpy (dest
, string
, 1);
55 g_assert_cmpstr (dest
, ==, "\xe2\x82\xa0");
57 g_utf8_strncpy (dest
, string
, 2);
58 g_assert_cmpstr (dest
, ==, "\xe2\x82\xa0g");
60 g_utf8_strncpy (dest
, string
, 3);
61 g_assert_cmpstr (dest
, ==, "\xe2\x82\xa0gh");
63 g_utf8_strncpy (dest
, string
, 4);
64 g_assert_cmpstr (dest
, ==, "\xe2\x82\xa0gh\xe2\x82\xa4");
66 g_utf8_strncpy (dest
, string
, 5);
67 g_assert_cmpstr (dest
, ==, "\xe2\x82\xa0gh\xe2\x82\xa4j");
69 g_utf8_strncpy (dest
, string
, 6);
70 g_assert_cmpstr (dest
, ==, "\xe2\x82\xa0gh\xe2\x82\xa4jl");
72 g_utf8_strncpy (dest
, string
, 20);
73 g_assert_cmpstr (dest
, ==, "\xe2\x82\xa0gh\xe2\x82\xa4jl");
77 test_utf8_strrchr (void)
79 const gchar
*string
= "\xe2\x82\xa0gh\xe2\x82\xa4jl\xe2\x82\xa4jl";
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
);
89 test_utf8_reverse (void)
93 r
= g_utf8_strreverse ("abcdef", -1);
94 g_assert_cmpstr (r
, ==, "fedcba");
97 r
= g_utf8_strreverse ("abcdef", 4);
98 g_assert_cmpstr (r
, ==, "dcba");
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
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");
112 test_utf8_substring (void)
116 r
= g_utf8_substring ("abcd", 1, 3);
117 g_assert_cmpstr (r
, ==, "bc");
120 r
= g_utf8_substring ("abcd", 0, 4);
121 g_assert_cmpstr (r
, ==, "abcd");
124 r
= g_utf8_substring ("abcd", 2, 2);
125 g_assert_cmpstr (r
, ==, "");
128 r
= g_utf8_substring ("abc\xe2\x82\xa0gh\xe2\x82\xa4", 2, 5);
129 g_assert_cmpstr (r
, ==, "c\xe2\x82\xa0g");
134 test_utf8_make_valid (void)
139 r
= g_utf8_make_valid ("\xe2\x82\xa0gh\xe2\x82\xa4jl", -1);
140 g_assert_cmpstr (r
, ==, "\xe2\x82\xa0gh\xe2\x82\xa4jl");
144 r
= g_utf8_make_valid ("\xe2\x82\xa0gh\xe2\xffjl", -1);
145 g_assert_cmpstr (r
, ==, "\xe2\x82\xa0gh\xef\xbf\xbd\xef\xbf\xbdjl");
148 /* invalid UTF8 without nul terminator followed by something unfortunate */
149 r
= g_utf8_make_valid ("Bj\xc3\xb8", 3);
150 g_assert_cmpstr (r
, ==, "Bj\xef\xbf\xbd");
153 /* invalid UTF8 with embedded nul */
154 r
= g_utf8_make_valid ("\xe2\x82\xa0gh\xe2\x00jl", 9);
155 g_assert_cmpstr (r
, ==, "\xe2\x82\xa0gh\xef\xbf\xbd\xef\xbf\xbdjl");
163 g_test_init (&argc
, &argv
, NULL
);
165 g_test_add_func ("/utf8/strlen", test_utf8_strlen
);
166 g_test_add_func ("/utf8/strncpy", test_utf8_strncpy
);
167 g_test_add_func ("/utf8/strrchr", test_utf8_strrchr
);
168 g_test_add_func ("/utf8/reverse", test_utf8_reverse
);
169 g_test_add_func ("/utf8/substring", test_utf8_substring
);
170 g_test_add_func ("/utf8/make-valid", test_utf8_make_valid
);