test-unicode.c 6.7 KB
Newer Older
1
/*
2 3
 * Copyright © 2011  Codethink Limited
 * Copyright © 2011  Google, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Codethink Author(s): Ryan Lortie
26
 * Google Author(s): Behdad Esfahbod
27 28 29 30
 */

#include "hb-test.h"

31 32
/* Unit tests for hb-unicode.h */

33 34

static void
B
Behdad Esfahbod 已提交
35
test_unicode_nil (void)
36 37 38 39 40 41 42 43 44
{
  hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);

  g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_UNKNOWN);

  hb_unicode_funcs_destroy (uf);
}

static void
B
Behdad Esfahbod 已提交
45
test_unicode_glib (void)
46 47 48 49 50 51
{
  hb_unicode_funcs_t *uf = hb_glib_get_unicode_funcs ();

  g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
}

52
static void
B
Behdad Esfahbod 已提交
53
test_unicode_default (void)
54 55 56 57 58 59
{
  hb_unicode_funcs_t *uf = hb_unicode_funcs_get_default ();

  g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

#define MAGIC0 0x12345678
#define MAGIC1 0x76543210

typedef struct {
  int value;
  gboolean freed;
} data_t;

typedef struct {
  data_t data[2];
} data_fixture_t;
static void
data_fixture_init (data_fixture_t *f, gconstpointer user_data)
{
  f->data[0].value = MAGIC0;
  f->data[1].value = MAGIC1;
}
static void
data_fixture_finish (data_fixture_t *f, gconstpointer user_data)
80
{
81 82 83 84 85 86 87 88 89
}

static void free_up (void *p)
{
  data_t *data = (data_t *) p;

  g_assert (data->value == MAGIC0 || data->value == MAGIC1);
  g_assert (data->freed == FALSE);
  data->freed = TRUE;
90 91 92 93 94 95 96
}

static hb_script_t
simple_get_script (hb_unicode_funcs_t *ufuncs,
                   hb_codepoint_t      codepoint,
                   void               *user_data)
{
97 98
  data_t *data = (data_t *) user_data;

99
  g_assert (hb_unicode_funcs_get_parent (ufuncs) == NULL);
100 101
  g_assert (data->value == MAGIC0);
  g_assert (data->freed == FALSE);
102 103 104 105 106 107 108 109

  if ('a' <= codepoint && codepoint <= 'z')
    return HB_SCRIPT_LATIN;
  else
    return HB_SCRIPT_UNKNOWN;
}

static void
110
test_unicode_custom (data_fixture_t *f, gconstpointer user_data)
111 112 113 114
{
  hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);

  hb_unicode_funcs_set_script_func (uf, simple_get_script,
115
                                    &f->data[0], free_up);
116 117 118 119

  g_assert_cmpint (hb_unicode_get_script (uf, 'a'), ==, HB_SCRIPT_LATIN);
  g_assert_cmpint (hb_unicode_get_script (uf, '0'), ==, HB_SCRIPT_UNKNOWN);

120
  g_assert (!f->data[0].freed && !f->data[1].freed);
121
  hb_unicode_funcs_destroy (uf);
122
  g_assert (f->data[0].freed && !f->data[1].freed);
123 124 125 126 127 128 129
}

static hb_script_t
a_is_for_arabic_get_script (hb_unicode_funcs_t *ufuncs,
                            hb_codepoint_t      codepoint,
                            void               *user_data)
{
130 131
  data_t *data = (data_t *) user_data;

132
  g_assert (hb_unicode_funcs_get_parent (ufuncs) != NULL);
133 134
  g_assert (data->value == MAGIC1);
  g_assert (data->freed == FALSE);
135 136 137 138 139 140 141 142 143 144 145

  if (codepoint == 'a') {
    return HB_SCRIPT_ARABIC;
  } else {
    hb_unicode_funcs_t *parent = hb_unicode_funcs_get_parent (ufuncs);

    return hb_unicode_get_script (parent, codepoint);
  }
}

static void
146
test_unicode_subclassing_nil (data_fixture_t *f, gconstpointer user_data)
147
{
148 149 150 151 152 153
  hb_unicode_funcs_t *uf, *aa;

  uf = hb_unicode_funcs_create (NULL);

  aa = hb_unicode_funcs_create (uf);

154 155 156
  hb_unicode_funcs_destroy (uf);

  hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
157
                                    &f->data[1], free_up);
158 159 160 161

  g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
  g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_UNKNOWN);

162
  g_assert (!f->data[0].freed && !f->data[1].freed);
163
  hb_unicode_funcs_destroy (aa);
164
  g_assert (!f->data[0].freed && f->data[1].freed);
165 166 167
}

static void
168
test_unicode_subclassing_default (data_fixture_t *f, gconstpointer user_data)
169
{
170 171
  hb_unicode_funcs_t *uf, *aa;

172
  uf = hb_unicode_funcs_get_default ();
173
  aa = hb_unicode_funcs_create (uf);
174 175

  hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
176
                                    &f->data[1], free_up);
177 178 179 180

  g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
  g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);

181
  g_assert (!f->data[0].freed && !f->data[1].freed);
182
  hb_unicode_funcs_destroy (aa);
183
  g_assert (!f->data[0].freed && f->data[1].freed);
184 185 186
}

static void
187
test_unicode_subclassing_deep (data_fixture_t *f, gconstpointer user_data)
188
{
189 190 191
  hb_unicode_funcs_t *uf, *aa;

  uf = hb_unicode_funcs_create (NULL);
192 193

  hb_unicode_funcs_set_script_func (uf, simple_get_script,
194
                                    &f->data[0], free_up);
195

196
  aa = hb_unicode_funcs_create (uf);
197 198 199 200

  hb_unicode_funcs_destroy (uf);

  /* make sure the 'uf' didn't get freed, since 'aa' holds a ref */
201
  g_assert (!f->data[0].freed);
202 203

  hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
204
                                    &f->data[1], free_up);
205 206 207 208 209

  g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
  g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
  g_assert_cmpint (hb_unicode_get_script (aa, '0'), ==, HB_SCRIPT_UNKNOWN);

210
  g_assert (!f->data[0].freed && !f->data[1].freed);
211
  hb_unicode_funcs_destroy (aa);
212
  g_assert (f->data[0].freed && f->data[1].freed);
213 214
}

215

216 217 218
int
main (int argc, char **argv)
{
B
Behdad Esfahbod 已提交
219
  hb_test_init (&argc, &argv);
220

B
Behdad Esfahbod 已提交
221 222 223
  hb_test_add (test_unicode_nil);
  hb_test_add (test_unicode_glib);
  hb_test_add (test_unicode_default);
224 225 226 227 228

  hb_test_add_fixture (data_fixture, NULL, test_unicode_custom);
  hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_nil);
  hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_default);
  hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_deep);
229

230
  /* XXX test all methods for their defaults and various (glib, icu, default) implementations. */
B
Behdad Esfahbod 已提交
231 232
  /* XXX test glib & icu two-way script conversion */

B
Behdad Esfahbod 已提交
233
  return hb_test_run ();
234
}