Clean up spec file for packaging
[profile/ivi/pango.git] / tests / testscript.c
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 #undef VERBOSE
64
65 #define ASSERT(stmt) G_STMT_START {                                     \
66     if (stmt) { }                                                       \
67     else                                                                \
68       {                                                                 \
69         g_warning ("%s:%d (%s): assertion '%s' failed",                 \
70                  __FILE__, __LINE__, G_STRFUNC, #stmt);                 \
71         exit (1);                                                       \
72       }                                                                 \
73 } G_STMT_END
74
75 typedef struct
76 {
77   const char *run_text_escaped;
78   char *run_text;
79   PangoScript run_code;
80 } RunTestData;
81
82 static gchar *
83 unescape (const char *text)
84 {
85   gboolean escaped = FALSE;
86   GString *result = g_string_new (NULL);
87   const gchar *p;
88
89   for (p = text; *p; p = g_utf8_next_char (p))
90     {
91       gunichar ch = g_utf8_get_char (p);
92
93       if (escaped)
94         {
95           if (ch == 'u' || ch == 'U')
96             {
97               int n_chars = ch == 'u' ? 4 : 8;
98               int i;
99
100               ch = 0;
101               for (i = 0; i < n_chars; i++)
102                 {
103                   p++;
104                   ch <<= 4;
105                   if (*p <= '9' && *p >= '0')
106                     ch += *p - '0';
107                   else if (*p <= 'F' && *p >= 'A')
108                     ch += 10 + *p - 'A';
109                   else if (*p <= 'f' && *p >= 'a')
110                     ch += 10 + *p - 'a';
111                   else
112                     g_assert_not_reached ();
113                 }
114             }
115           else if (ch == '\\')
116             {
117               ch = '\\';
118             }
119           else
120             {
121               g_assert_not_reached ();
122             }
123
124           escaped = FALSE;
125         }
126       else
127         {
128           if (ch == '\\')
129             {
130               escaped = TRUE;
131               continue;
132             }
133         }
134
135       g_string_append_unichar (result, ch);
136     }
137
138   return g_string_free (result, FALSE);
139 }
140
141 static void
142 test_script_iter (void)
143 {
144   static RunTestData test_data[] = {
145     { "\\u0020\\u0946\\u0939\\u093F\\u0928\\u094D\\u0926\\u0940\\u0020", NULL, PANGO_SCRIPT_DEVANAGARI },
146     { "\\u0627\\u0644\\u0639\\u0631\\u0628\\u064A\\u0629\\u0020", NULL, PANGO_SCRIPT_ARABIC },
147     { "\\u0420\\u0443\\u0441\\u0441\\u043A\\u0438\\u0439\\u0020", NULL, PANGO_SCRIPT_CYRILLIC },
148     { "English (", NULL, PANGO_SCRIPT_LATIN },
149     { "\\u0E44\\u0E17\\u0E22", NULL, PANGO_SCRIPT_THAI },
150     { ") ", NULL, PANGO_SCRIPT_LATIN },
151     { "\\u6F22\\u5B75", NULL, PANGO_SCRIPT_HAN },
152     { "\\u3068\\u3072\\u3089\\u304C\\u306A\\u3068", NULL, PANGO_SCRIPT_HIRAGANA },
153     { "\\u30AB\\u30BF\\u30AB\\u30CA", NULL, PANGO_SCRIPT_KATAKANA },
154     { "\\U00010400\\U00010401\\U00010402\\U00010403", NULL, PANGO_SCRIPT_DESERET }
155   };
156
157   PangoScriptIter *iter;
158   GString *all = g_string_new (FALSE);
159   char *pos;
160   const char *start;
161   const char *end;
162   PangoScript script;
163   unsigned int i;
164
165   for (i = 0; i < G_N_ELEMENTS(test_data); i++)
166     {
167       test_data[i].run_text = unescape (test_data[i].run_text_escaped);
168       g_string_append (all, test_data[i].run_text);
169     }
170
171   iter = pango_script_iter_new (all->str, -1);
172
173 #ifdef VERBOSE
174   g_print ("Total length: %d\n", all->len);
175 #endif
176
177   pos = all->str;
178   for (i = 0; i < G_N_ELEMENTS(test_data); i++)
179     {
180       char *next_pos = pos + strlen (test_data[i].run_text);
181       gboolean result;
182
183       pango_script_iter_get_range (iter, &start, &end, &script);
184 #ifdef VERBOSE
185       g_print ("Range: %d-%d: %d\n", start - all->str, end - all->str, script);
186 #endif
187
188       ASSERT (start == pos);
189       ASSERT (end == next_pos);
190       ASSERT (script == test_data[i].run_code);
191
192       result = pango_script_iter_next (iter);
193       ASSERT (result == (i != G_N_ELEMENTS (test_data) - 1));
194
195       pos = next_pos;
196     }
197
198   pango_script_iter_free (iter);
199
200   /*
201    * Test an empty string.
202    */
203   iter = pango_script_iter_new (all->str, 0);
204
205   pango_script_iter_get_range (iter, &start, &end, &script);
206
207   ASSERT (start == all->str);
208   ASSERT (end == all->str);
209   ASSERT (script == PANGO_SCRIPT_COMMON);
210   ASSERT (!pango_script_iter_next (iter));
211
212   pango_script_iter_free (iter);
213
214   /* Cleanup */
215
216   for (i = 0; i < G_N_ELEMENTS (test_data); i++)
217     g_free (test_data[i].run_text);
218
219   g_string_free (all, TRUE);
220 }
221
222 int
223 main (int argc, char **argv)
224 {
225   g_setenv ("PANGO_RC_FILE", "./pangorc", TRUE);
226
227   test_script_iter ();
228
229   return 0;
230 }