Some more utf8 tests
[platform/upstream/glib.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 int
89 main (int   argc,
90       char *argv[])
91 {
92   g_test_init (&argc, &argv, NULL);
93
94   g_test_add_func ("/utf8/strlen", test_utf8_strlen);
95   g_test_add_func ("/utf8/strncpy", test_utf8_strncpy);
96   g_test_add_func ("/utf8/strrchr", test_utf8_strrchr);
97
98   return g_test_run();
99 }