2011-04-20 04:19:20 +00:00
|
|
|
/*
|
2011-04-29 16:00:38 +00:00
|
|
|
* Copyright © 2011 Codethink Limited
|
|
|
|
* Copyright © 2011 Google, Inc.
|
2011-04-20 04:19:20 +00:00
|
|
|
*
|
|
|
|
* 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
|
2011-04-29 16:00:38 +00:00
|
|
|
* Google Author(s): Behdad Esfahbod
|
2011-04-20 04:19:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hb-test.h"
|
|
|
|
|
2011-04-29 16:00:38 +00:00
|
|
|
/* Unit tests for hb-unicode.h */
|
|
|
|
|
2011-04-20 04:19:20 +00:00
|
|
|
|
|
|
|
static void
|
2011-04-28 21:10:44 +00:00
|
|
|
test_unicode_nil (void)
|
2011-04-20 04:19:20 +00:00
|
|
|
{
|
|
|
|
hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
|
|
|
|
|
2011-04-29 16:17:09 +00:00
|
|
|
g_assert (!hb_unicode_funcs_is_immutable (uf));
|
|
|
|
|
2011-04-20 04:19:20 +00:00
|
|
|
g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_UNKNOWN);
|
|
|
|
|
|
|
|
hb_unicode_funcs_destroy (uf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-28 21:10:44 +00:00
|
|
|
test_unicode_glib (void)
|
2011-04-20 04:19:20 +00:00
|
|
|
{
|
|
|
|
hb_unicode_funcs_t *uf = hb_glib_get_unicode_funcs ();
|
|
|
|
|
2011-04-29 16:17:09 +00:00
|
|
|
g_assert (hb_unicode_funcs_is_immutable (uf));
|
|
|
|
|
2011-04-20 04:19:20 +00:00
|
|
|
g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
|
|
|
|
}
|
|
|
|
|
2011-04-27 13:24:37 +00:00
|
|
|
static void
|
2011-04-28 21:10:44 +00:00
|
|
|
test_unicode_default (void)
|
2011-04-27 13:24:37 +00:00
|
|
|
{
|
|
|
|
hb_unicode_funcs_t *uf = hb_unicode_funcs_get_default ();
|
|
|
|
|
2011-04-29 16:17:09 +00:00
|
|
|
g_assert (hb_unicode_funcs_is_immutable (uf));
|
|
|
|
|
2011-04-27 13:24:37 +00:00
|
|
|
g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
|
|
|
|
}
|
|
|
|
|
2011-04-29 16:00:38 +00:00
|
|
|
|
|
|
|
#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)
|
2011-04-20 04:19:20 +00:00
|
|
|
{
|
2011-04-29 16:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2011-04-20 04:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static hb_script_t
|
|
|
|
simple_get_script (hb_unicode_funcs_t *ufuncs,
|
|
|
|
hb_codepoint_t codepoint,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2011-04-29 16:00:38 +00:00
|
|
|
data_t *data = (data_t *) user_data;
|
|
|
|
|
2011-04-20 04:19:20 +00:00
|
|
|
g_assert (hb_unicode_funcs_get_parent (ufuncs) == NULL);
|
2011-04-29 16:00:38 +00:00
|
|
|
g_assert (data->value == MAGIC0);
|
|
|
|
g_assert (data->freed == FALSE);
|
2011-04-20 04:19:20 +00:00
|
|
|
|
|
|
|
if ('a' <= codepoint && codepoint <= 'z')
|
|
|
|
return HB_SCRIPT_LATIN;
|
|
|
|
else
|
|
|
|
return HB_SCRIPT_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static hb_script_t
|
|
|
|
a_is_for_arabic_get_script (hb_unicode_funcs_t *ufuncs,
|
|
|
|
hb_codepoint_t codepoint,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2011-04-29 16:00:38 +00:00
|
|
|
data_t *data = (data_t *) user_data;
|
|
|
|
|
2011-04-20 04:19:20 +00:00
|
|
|
g_assert (hb_unicode_funcs_get_parent (ufuncs) != NULL);
|
2011-04-29 16:00:38 +00:00
|
|
|
g_assert (data->value == MAGIC1);
|
|
|
|
g_assert (data->freed == FALSE);
|
2011-04-20 04:19:20 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-29 16:17:09 +00:00
|
|
|
static void
|
|
|
|
test_unicode_custom (data_fixture_t *f, gconstpointer user_data)
|
|
|
|
{
|
|
|
|
hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
|
|
|
|
|
|
|
|
hb_unicode_funcs_set_script_func (uf, simple_get_script,
|
|
|
|
&f->data[0], free_up);
|
|
|
|
|
|
|
|
g_assert_cmpint (hb_unicode_get_script (uf, 'a'), ==, HB_SCRIPT_LATIN);
|
|
|
|
g_assert_cmpint (hb_unicode_get_script (uf, '0'), ==, HB_SCRIPT_UNKNOWN);
|
|
|
|
|
|
|
|
g_assert (!hb_unicode_funcs_is_immutable (uf));
|
|
|
|
hb_unicode_funcs_make_immutable (uf);
|
|
|
|
g_assert (hb_unicode_funcs_is_immutable (uf));
|
|
|
|
|
|
|
|
/* Since uf is immutable now, the following setter should do nothing. */
|
|
|
|
hb_unicode_funcs_set_script_func (uf, a_is_for_arabic_get_script,
|
|
|
|
&f->data[1], free_up);
|
|
|
|
|
|
|
|
g_assert (!f->data[0].freed && !f->data[1].freed);
|
|
|
|
hb_unicode_funcs_destroy (uf);
|
|
|
|
g_assert (f->data[0].freed && !f->data[1].freed);
|
|
|
|
}
|
|
|
|
|
2011-04-20 04:19:20 +00:00
|
|
|
static void
|
2011-04-29 16:00:38 +00:00
|
|
|
test_unicode_subclassing_nil (data_fixture_t *f, gconstpointer user_data)
|
2011-04-20 04:19:20 +00:00
|
|
|
{
|
2011-04-20 06:00:47 +00:00
|
|
|
hb_unicode_funcs_t *uf, *aa;
|
|
|
|
|
|
|
|
uf = hb_unicode_funcs_create (NULL);
|
|
|
|
|
|
|
|
aa = hb_unicode_funcs_create (uf);
|
|
|
|
|
2011-04-20 04:19:20 +00:00
|
|
|
hb_unicode_funcs_destroy (uf);
|
|
|
|
|
|
|
|
hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
|
2011-04-29 16:00:38 +00:00
|
|
|
&f->data[1], free_up);
|
2011-04-20 04:19:20 +00:00
|
|
|
|
|
|
|
g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
|
|
|
|
g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_UNKNOWN);
|
|
|
|
|
2011-04-29 16:00:38 +00:00
|
|
|
g_assert (!f->data[0].freed && !f->data[1].freed);
|
2011-04-20 04:19:20 +00:00
|
|
|
hb_unicode_funcs_destroy (aa);
|
2011-04-29 16:00:38 +00:00
|
|
|
g_assert (!f->data[0].freed && f->data[1].freed);
|
2011-04-20 04:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-29 16:00:38 +00:00
|
|
|
test_unicode_subclassing_default (data_fixture_t *f, gconstpointer user_data)
|
2011-04-20 04:19:20 +00:00
|
|
|
{
|
2011-04-20 06:00:47 +00:00
|
|
|
hb_unicode_funcs_t *uf, *aa;
|
|
|
|
|
2011-04-29 16:00:38 +00:00
|
|
|
uf = hb_unicode_funcs_get_default ();
|
2011-04-20 06:00:47 +00:00
|
|
|
aa = hb_unicode_funcs_create (uf);
|
2011-04-20 04:19:20 +00:00
|
|
|
|
|
|
|
hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
|
2011-04-29 16:00:38 +00:00
|
|
|
&f->data[1], free_up);
|
2011-04-20 04:19:20 +00:00
|
|
|
|
|
|
|
g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
|
|
|
|
g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
|
|
|
|
|
2011-04-29 16:00:38 +00:00
|
|
|
g_assert (!f->data[0].freed && !f->data[1].freed);
|
2011-04-20 04:19:20 +00:00
|
|
|
hb_unicode_funcs_destroy (aa);
|
2011-04-29 16:00:38 +00:00
|
|
|
g_assert (!f->data[0].freed && f->data[1].freed);
|
2011-04-20 04:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-29 16:00:38 +00:00
|
|
|
test_unicode_subclassing_deep (data_fixture_t *f, gconstpointer user_data)
|
2011-04-20 04:19:20 +00:00
|
|
|
{
|
2011-04-20 06:00:47 +00:00
|
|
|
hb_unicode_funcs_t *uf, *aa;
|
|
|
|
|
|
|
|
uf = hb_unicode_funcs_create (NULL);
|
2011-04-20 04:19:20 +00:00
|
|
|
|
|
|
|
hb_unicode_funcs_set_script_func (uf, simple_get_script,
|
2011-04-29 16:00:38 +00:00
|
|
|
&f->data[0], free_up);
|
2011-04-20 04:19:20 +00:00
|
|
|
|
2011-04-20 06:00:47 +00:00
|
|
|
aa = hb_unicode_funcs_create (uf);
|
2011-04-20 04:19:20 +00:00
|
|
|
|
|
|
|
hb_unicode_funcs_destroy (uf);
|
|
|
|
|
|
|
|
/* make sure the 'uf' didn't get freed, since 'aa' holds a ref */
|
2011-04-29 16:00:38 +00:00
|
|
|
g_assert (!f->data[0].freed);
|
2011-04-20 04:19:20 +00:00
|
|
|
|
|
|
|
hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
|
2011-04-29 16:00:38 +00:00
|
|
|
&f->data[1], free_up);
|
2011-04-20 04:19:20 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2011-04-29 16:00:38 +00:00
|
|
|
g_assert (!f->data[0].freed && !f->data[1].freed);
|
2011-04-20 04:19:20 +00:00
|
|
|
hb_unicode_funcs_destroy (aa);
|
2011-04-29 16:00:38 +00:00
|
|
|
g_assert (f->data[0].freed && f->data[1].freed);
|
2011-04-20 04:19:20 +00:00
|
|
|
}
|
|
|
|
|
2011-04-29 16:00:38 +00:00
|
|
|
|
2011-04-20 04:19:20 +00:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2011-04-28 21:10:44 +00:00
|
|
|
hb_test_init (&argc, &argv);
|
2011-04-20 04:19:20 +00:00
|
|
|
|
2011-04-28 21:10:44 +00:00
|
|
|
hb_test_add (test_unicode_nil);
|
|
|
|
hb_test_add (test_unicode_glib);
|
|
|
|
hb_test_add (test_unicode_default);
|
2011-04-29 16:00:38 +00:00
|
|
|
|
|
|
|
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);
|
2011-04-20 04:19:20 +00:00
|
|
|
|
2011-04-27 13:24:37 +00:00
|
|
|
/* XXX test all methods for their defaults and various (glib, icu, default) implementations. */
|
2011-04-20 07:04:56 +00:00
|
|
|
/* XXX test glib & icu two-way script conversion */
|
|
|
|
|
2011-04-28 21:10:44 +00:00
|
|
|
return hb_test_run ();
|
2011-04-20 04:19:20 +00:00
|
|
|
}
|