1 | /* -*- mode: C; c-file-style: "gnu" -*- */ |
2 | /* Pango |
3 | * testscript.c: Test cases for PangoScriptIter |
4 | * |
5 | * Copyright (C) 2002 Red Hat Software |
6 | * |
7 | * This library is free software; you can redistribute it and/or |
8 | * modify it under the terms of the GNU Library General Public |
9 | * License as published by the Free Software Foundation; either |
10 | * version 2 of the License, or (at your option) any later version. |
11 | * |
12 | * This library is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | * Library General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU Library General Public |
18 | * License along with this library; if not, write to the |
19 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
20 | * Boston, MA 02111-1307, USA. |
21 | * |
22 | * The test strings here come from ICU: |
23 | * |
24 | * icu/sources/test/cintltst/cucdtst.c |
25 | * |
26 | ******************************************************************** |
27 | * COPYRIGHT: |
28 | * Copyright (c) 1997-2001, International Business Machines Corporation and |
29 | * others. All Rights Reserved. |
30 | ******************************************************************** |
31 | * |
32 | * Permission is hereby granted, free of charge, to any person obtaining a |
33 | * copy of this software and associated documentation files (the |
34 | * "Software"), to deal in the Software without restriction, including |
35 | * without limitation the rights to use, copy, modify, merge, publish, |
36 | * distribute, and/or sell copies of the Software, and to permit persons |
37 | * to whom the Software is furnished to do so, provided that the above |
38 | * copyright notice(s) and this permission notice appear in all copies of |
39 | * the Software and that both the above copyright notice(s) and this |
40 | * permission notice appear in supporting documentation. |
41 | * |
42 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
43 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
44 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT |
45 | * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
46 | * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL |
47 | * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING |
48 | * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
49 | * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
50 | * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
51 | * |
52 | * Except as contained in this notice, the name of a copyright holder |
53 | * shall not be used in advertising or otherwise to promote the sale, use |
54 | * or other dealings in this Software without prior written authorization |
55 | * of the copyright holder. |
56 | */ |
57 | |
58 | #include <stdlib.h> |
59 | #include <string.h> |
60 | |
61 | #include "pango/pango-script.h" |
62 | |
63 | typedef struct |
64 | { |
65 | const char *run_text_escaped; |
66 | char *run_text; |
67 | PangoScript run_code; |
68 | } RunTestData; |
69 | |
70 | static gchar * |
71 | unescape (const char *text) |
72 | { |
73 | gboolean escaped = FALSE; |
74 | GString *result = g_string_new (NULL); |
75 | const gchar *p; |
76 | |
77 | for (p = text; *p; p = g_utf8_next_char (p)) |
78 | { |
79 | gunichar ch = g_utf8_get_char (p); |
80 | |
81 | if (escaped) |
82 | { |
83 | if (ch == 'u' || ch == 'U') |
84 | { |
85 | int n_chars = ch == 'u' ? 4 : 8; |
86 | int i; |
87 | |
88 | ch = 0; |
89 | for (i = 0; i < n_chars; i++) |
90 | { |
91 | p++; |
92 | ch <<= 4; |
93 | if (*p <= '9' && *p >= '0') |
94 | ch += *p - '0'; |
95 | else if (*p <= 'F' && *p >= 'A') |
96 | ch += 10 + *p - 'A'; |
97 | else if (*p <= 'f' && *p >= 'a') |
98 | ch += 10 + *p - 'a'; |
99 | else |
100 | g_assert_not_reached (); |
101 | } |
102 | } |
103 | else if (ch == '\\') |
104 | { |
105 | ch = '\\'; |
106 | } |
107 | else |
108 | { |
109 | g_assert_not_reached (); |
110 | } |
111 | |
112 | escaped = FALSE; |
113 | } |
114 | else |
115 | { |
116 | if (ch == '\\') |
117 | { |
118 | escaped = TRUE; |
119 | continue; |
120 | } |
121 | } |
122 | |
123 | g_string_append_unichar (string: result, wc: ch); |
124 | } |
125 | |
126 | return g_string_free (string: result, FALSE); |
127 | } |
128 | |
129 | static void |
130 | test_script_iter (void) |
131 | { |
132 | static RunTestData test_data[] = { |
133 | { "\\u0020\\u0946\\u0939\\u093F\\u0928\\u094D\\u0926\\u0940\\u0020" , NULL, PANGO_SCRIPT_DEVANAGARI }, |
134 | { "\\u0627\\u0644\\u0639\\u0631\\u0628\\u064A\\u0629\\u0020" , NULL, PANGO_SCRIPT_ARABIC }, |
135 | { "\\u0420\\u0443\\u0441\\u0441\\u043A\\u0438\\u0439\\u0020" , NULL, PANGO_SCRIPT_CYRILLIC }, |
136 | { "English (" , NULL, PANGO_SCRIPT_LATIN }, |
137 | { "\\u0E44\\u0E17\\u0E22" , NULL, PANGO_SCRIPT_THAI }, |
138 | { ") " , NULL, PANGO_SCRIPT_LATIN }, |
139 | { "\\u6F22\\u5B75" , NULL, PANGO_SCRIPT_HAN }, |
140 | { "\\u3068\\u3072\\u3089\\u304C\\u306A\\u3068" , NULL, PANGO_SCRIPT_HIRAGANA }, |
141 | { "\\u30AB\\u30BF\\u30AB\\u30CA" , NULL, PANGO_SCRIPT_KATAKANA }, |
142 | { "\\U00010400\\U00010401\\U00010402\\U00010403" , NULL, PANGO_SCRIPT_DESERET } |
143 | }; |
144 | |
145 | PangoScriptIter *iter; |
146 | PangoScriptIter *iter2; |
147 | GString *all = g_string_new (FALSE); |
148 | char *pos; |
149 | const char *start; |
150 | const char *end; |
151 | PangoScript script; |
152 | unsigned int i; |
153 | |
154 | for (i = 0; i < G_N_ELEMENTS(test_data); i++) |
155 | { |
156 | test_data[i].run_text = unescape (text: test_data[i].run_text_escaped); |
157 | g_string_append (string: all, val: test_data[i].run_text); |
158 | } |
159 | |
160 | iter = pango_script_iter_new (text: all->str, length: -1); |
161 | |
162 | iter2 = g_boxed_copy (boxed_type: pango_script_iter_get_type (), src_boxed: iter); |
163 | |
164 | g_test_message (format: "Total length: %" G_GSIZE_FORMAT "\n" , all->len); |
165 | |
166 | pos = all->str; |
167 | for (i = 0; i < G_N_ELEMENTS(test_data); i++) |
168 | { |
169 | char *next_pos = pos + strlen (s: test_data[i].run_text); |
170 | gboolean result; |
171 | |
172 | pango_script_iter_get_range (iter, start: &start, end: &end, script: &script); |
173 | |
174 | g_test_message (format: "Range: %d-%d: %d\n" , |
175 | (int) (start - all->str), |
176 | (int) (end - all->str), |
177 | script); |
178 | |
179 | g_assert_true (start == pos); |
180 | g_assert_true (end == next_pos); |
181 | g_assert_true (script == test_data[i].run_code); |
182 | |
183 | result = pango_script_iter_next (iter); |
184 | g_assert_true (result == (i != G_N_ELEMENTS (test_data) - 1)); |
185 | |
186 | pos = next_pos; |
187 | } |
188 | |
189 | /* Check that copying the iter worked */ |
190 | pango_script_iter_get_range (iter: iter2, start: &start, end: &end, script: &script); |
191 | g_assert_true (start == all->str); |
192 | g_assert_true (end == all->str + strlen (test_data[0].run_text)); |
193 | g_assert_true (script == test_data[0].run_code); |
194 | pango_script_iter_free (iter: iter2); |
195 | |
196 | pango_script_iter_free (iter); |
197 | |
198 | /* |
199 | * Test an empty string. |
200 | */ |
201 | iter = pango_script_iter_new (text: all->str, length: 0); |
202 | |
203 | pango_script_iter_get_range (iter, start: &start, end: &end, script: &script); |
204 | |
205 | g_assert_true (start == all->str); |
206 | g_assert_true (end == all->str); |
207 | g_assert_true (script == PANGO_SCRIPT_COMMON); |
208 | g_assert_true (!pango_script_iter_next (iter)); |
209 | |
210 | pango_script_iter_free (iter); |
211 | |
212 | /* Cleanup */ |
213 | |
214 | for (i = 0; i < G_N_ELEMENTS (test_data); i++) |
215 | g_free (mem: test_data[i].run_text); |
216 | |
217 | g_string_free (string: all, TRUE); |
218 | } |
219 | |
220 | int |
221 | main (int argc, char **argv) |
222 | { |
223 | g_test_init (argc: &argc, argv: &argv, NULL); |
224 | |
225 | g_test_add_func (testpath: "/script/iter" , test_func: test_script_iter); |
226 | |
227 | return g_test_run (); |
228 | } |
229 | |