summaryrefslogtreecommitdiff
path: root/tests/testscript.c
blob: 33e8ad2ed5175817f7ca7dd2c03cb493c44f5100 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* -*- mode: C; c-file-style: "gnu" -*- */
/* Pango
 * testscript.c: Test cases for PangoScriptIter
 *
 * Copyright (C) 2002 Red Hat Software
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * The test strings here come from ICU:
 *
 *  icu/sources/test/cintltst/cucdtst.c
 *
 ********************************************************************
 * COPYRIGHT:
 * Copyright (c) 1997-2001, International Business Machines Corporation and
 * others. All Rights Reserved.
 ********************************************************************
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, and/or sell copies of the Software, and to permit persons
 * to whom the Software is furnished to do so, provided that the above
 * copyright notice(s) and this permission notice appear in all copies of
 * the Software and that both the above copyright notice(s) and this
 * permission notice appear in supporting documentation.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Except as contained in this notice, the name of a copyright holder
 * shall not be used in advertising or otherwise to promote the sale, use
 * or other dealings in this Software without prior written authorization
 * of the copyright holder.
 */

#include <stdlib.h>
#include <string.h>

#include "pango/pango-script.h"

typedef struct
{
  const char *run_text_escaped;
  char *run_text;
  PangoScript run_code;
} RunTestData;

static gchar *
unescape (const char *text)
{
  gboolean escaped = FALSE;
  GString *result = g_string_new (NULL);
  const gchar *p;

  for (p = text; *p; p = g_utf8_next_char (p))
    {
      gunichar ch = g_utf8_get_char (p);

      if (escaped)
	{
	  if (ch == 'u' || ch == 'U')
	    {
	      int n_chars = ch == 'u' ? 4 : 8;
	      int i;

	      ch = 0;
	      for (i = 0; i < n_chars; i++)
		{
		  p++;
		  ch <<= 4;
		  if (*p <= '9' && *p >= '0')
		    ch += *p - '0';
		  else if (*p <= 'F' && *p >= 'A')
		    ch += 10 + *p - 'A';
		  else if (*p <= 'f' && *p >= 'a')
		    ch += 10 + *p - 'a';
		  else
		    g_assert_not_reached ();
		}
	    }
	  else if (ch == '\\')
	    {
	      ch = '\\';
	    }
	  else
	    {
	      g_assert_not_reached ();
	    }

	  escaped = FALSE;
	}
      else
	{
	  if (ch == '\\')
	    {
	      escaped = TRUE;
	      continue;
	    }
	}

      g_string_append_unichar (result, ch);
    }

  return g_string_free (result, FALSE);
}

static void
test_script_iter (void)
{
  static RunTestData test_data[] = {
    { "\\u0020\\u0946\\u0939\\u093F\\u0928\\u094D\\u0926\\u0940\\u0020", NULL, PANGO_SCRIPT_DEVANAGARI },
    { "\\u0627\\u0644\\u0639\\u0631\\u0628\\u064A\\u0629\\u0020", NULL, PANGO_SCRIPT_ARABIC },
    { "\\u0420\\u0443\\u0441\\u0441\\u043A\\u0438\\u0439\\u0020", NULL, PANGO_SCRIPT_CYRILLIC },
    { "English (", NULL, PANGO_SCRIPT_LATIN },
    { "\\u0E44\\u0E17\\u0E22", NULL, PANGO_SCRIPT_THAI },
    { ") ", NULL, PANGO_SCRIPT_LATIN },
    { "\\u6F22\\u5B75", NULL, PANGO_SCRIPT_HAN },
    { "\\u3068\\u3072\\u3089\\u304C\\u306A\\u3068", NULL, PANGO_SCRIPT_HIRAGANA },
    { "\\u30AB\\u30BF\\u30AB\\u30CA", NULL, PANGO_SCRIPT_KATAKANA },
    { "\\U00010400\\U00010401\\U00010402\\U00010403", NULL, PANGO_SCRIPT_DESERET }
  };

  PangoScriptIter *iter;
  GString *all = g_string_new (FALSE);
  char *pos;
  const char *start;
  const char *end;
  PangoScript script;
  unsigned int i;

  for (i = 0; i < G_N_ELEMENTS(test_data); i++)
    {
      test_data[i].run_text = unescape (test_data[i].run_text_escaped);
      g_string_append (all, test_data[i].run_text);
    }

  iter = pango_script_iter_new (all->str, -1);

  g_test_message ("Total length: %" G_GSIZE_FORMAT "\n", all->len);

  pos = all->str;
  for (i = 0; i < G_N_ELEMENTS(test_data); i++)
    {
      char *next_pos = pos + strlen (test_data[i].run_text);
      gboolean result;

      pango_script_iter_get_range (iter, &start, &end, &script);

      g_test_message ("Range: %d-%d: %d\n",
                      (int) (start - all->str),
                      (int) (end - all->str),
                      script);

      g_assert_true (start == pos);
      g_assert_true (end == next_pos);
      g_assert_true (script == test_data[i].run_code);

      result = pango_script_iter_next (iter);
      g_assert_true (result == (i != G_N_ELEMENTS (test_data) - 1));

      pos = next_pos;
    }

  pango_script_iter_free (iter);

  /*
   * Test an empty string.
   */
  iter = pango_script_iter_new (all->str, 0);

  pango_script_iter_get_range (iter, &start, &end, &script);

  g_assert_true (start == all->str);
  g_assert_true (end == all->str);
  g_assert_true (script == PANGO_SCRIPT_COMMON);
  g_assert_true (!pango_script_iter_next (iter));

  pango_script_iter_free (iter);

  /* Cleanup */

  for (i = 0; i < G_N_ELEMENTS (test_data); i++)
    g_free (test_data[i].run_text);

  g_string_free (all, TRUE);
}

int
main (int argc, char **argv)
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/script/iter", test_script_iter);

  return g_test_run ();
}