test-buffer.c 8.8 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2011  Google, Inc.
B
Behdad Esfahbod 已提交
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
 *
 *  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.
 *
 * Google Author(s): Behdad Esfahbod
 */

#include "hb-test.h"

/* This file tests types defined in hb-buffer.h */


static const char utf8[10] = "ab\360\240\200\200defg";
static const uint16_t utf16[8] = {'a', 'b', 0xD840, 0xDC00, 'd', 'e', 'f', 'g'};
static const uint32_t utf32[7] = {'a', 'b', 0x20000, 'd', 'e', 'f', 'g'};

typedef enum {
  BUFFER_EMPTY,
  BUFFER_ONE_BY_ONE,
  BUFFER_UTF32,
  BUFFER_UTF16,
  BUFFER_UTF8,
  BUFFER_NUM_TYPES,
} buffer_type_t;

static const char *buffer_names[] = {
  "empty",
  "one-by-one",
  "utf32",
  "utf16",
  "utf8"
};

typedef struct
{
  hb_buffer_t *b;
} Fixture;

static void
fixture_init (Fixture *fixture, gconstpointer user_data)
{
  unsigned int i;

  fixture->b = hb_buffer_create (0);

  switch (GPOINTER_TO_INT (user_data)) {
    case BUFFER_EMPTY:
      break;

    case BUFFER_ONE_BY_ONE:
      for (i = 1; i < G_N_ELEMENTS (utf32) - 1; i++)
71
	hb_buffer_add (fixture->b, utf32[i], 1, i);
B
Behdad Esfahbod 已提交
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
      break;

    case BUFFER_UTF32:
      hb_buffer_add_utf32 (fixture->b, utf32, G_N_ELEMENTS (utf32), 1, G_N_ELEMENTS (utf32) - 2);
      break;

    case BUFFER_UTF16:
      hb_buffer_add_utf16 (fixture->b, utf16, G_N_ELEMENTS (utf16), 1, G_N_ELEMENTS (utf16) - 2);
      break;

    case BUFFER_UTF8:
      hb_buffer_add_utf8  (fixture->b, utf8,  G_N_ELEMENTS (utf8),  1, G_N_ELEMENTS (utf8)  - 2);
      break;

    default:
      g_assert_not_reached ();
  }
}

static void
fixture_fini (Fixture *fixture, gconstpointer user_data)
{
  hb_buffer_destroy (fixture->b);
}


static void
test_buffer_properties (Fixture *fixture, gconstpointer user_data)
{
101
  hb_unicode_funcs_t *ufuncs;
B
Behdad Esfahbod 已提交
102

103 104 105
  /* test default properties */

  g_assert (hb_buffer_get_unicode_funcs (fixture->b) == hb_unicode_funcs_get_default ());
106 107
  g_assert (hb_buffer_get_direction (fixture->b) == HB_DIRECTION_INVALID);
  g_assert (hb_buffer_get_script (fixture->b) == HB_SCRIPT_INVALID);
B
Behdad Esfahbod 已提交
108 109
  g_assert (hb_buffer_get_language (fixture->b) == NULL);

110 111 112 113 114 115 116

  /* test property changes are retained */
  ufuncs = hb_unicode_funcs_create (NULL);
  hb_buffer_set_unicode_funcs (fixture->b, ufuncs);
  hb_unicode_funcs_destroy (ufuncs);
  g_assert (hb_buffer_get_unicode_funcs (fixture->b) == ufuncs);

B
Behdad Esfahbod 已提交
117 118 119 120 121 122 123 124
  hb_buffer_set_direction (fixture->b, HB_DIRECTION_RTL);
  g_assert (hb_buffer_get_direction (fixture->b) == HB_DIRECTION_RTL);

  hb_buffer_set_script (fixture->b, HB_SCRIPT_ARABIC);
  g_assert (hb_buffer_get_script (fixture->b) == HB_SCRIPT_ARABIC);

  hb_buffer_set_language (fixture->b, hb_language_from_string ("fa"));
  g_assert (hb_buffer_get_language (fixture->b) == hb_language_from_string ("Fa"));
125 126 127 128 129 130 131 132 133 134


  /* test reset clears properties */

  hb_buffer_reset (fixture->b);

  g_assert (hb_buffer_get_unicode_funcs (fixture->b) == hb_unicode_funcs_get_default ());
  g_assert (hb_buffer_get_direction (fixture->b) == HB_DIRECTION_INVALID);
  g_assert (hb_buffer_get_script (fixture->b) == HB_SCRIPT_INVALID);
  g_assert (hb_buffer_get_language (fixture->b) == NULL);
B
Behdad Esfahbod 已提交
135 136 137 138 139
}

static void
test_buffer_contents (Fixture *fixture, gconstpointer user_data)
{
140
  unsigned int i, len, len2;
B
Behdad Esfahbod 已提交
141 142 143 144 145 146 147 148
  buffer_type_t buffer_type = GPOINTER_TO_INT (user_data);
  hb_glyph_info_t *glyphs;

  if (buffer_type == BUFFER_EMPTY) {
    g_assert_cmpint (hb_buffer_get_length (fixture->b), ==, 0);
    return;
  }

149 150 151 152
  len = hb_buffer_get_length (fixture->b);
  glyphs = hb_buffer_get_glyph_infos (fixture->b, NULL); /* test NULL */
  glyphs = hb_buffer_get_glyph_infos (fixture->b, &len2);
  g_assert_cmpint (len, ==, len2);
B
Behdad Esfahbod 已提交
153 154
  g_assert_cmpint (len, ==, 5);

155 156 157 158 159 160
  for (i = 0; i < len; i++) {
    g_assert_cmphex (glyphs[i].mask,      ==, 1);
    g_assert_cmphex (glyphs[i].var1.u32,  ==, 0);
    g_assert_cmphex (glyphs[i].var2.u32,  ==, 0);
  }

B
Behdad Esfahbod 已提交
161 162 163 164 165 166 167 168 169 170 171 172
  for (i = 0; i < len; i++) {
    unsigned int cluster;
    cluster = 1+i;
    if (i >= 2) {
      if (buffer_type == BUFFER_UTF16)
	cluster++;
      else if (buffer_type == BUFFER_UTF8)
        cluster += 3;
    }
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
    g_assert_cmphex (glyphs[i].cluster,   ==, cluster);
  }
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 219 220 221 222 223

  /* reverse, test, and reverse back */

  hb_buffer_reverse (fixture->b);
  for (i = 0; i < len; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);

  hb_buffer_reverse (fixture->b);
  for (i = 0; i < len; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);

  /* reverse_clusters works same as reverse for now since each codepoint is
   * in its own cluster */

  hb_buffer_reverse_clusters (fixture->b);
  for (i = 0; i < len; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);

  hb_buffer_reverse_clusters (fixture->b);
  for (i = 0; i < len; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);

  /* now form a cluster and test again */
  glyphs[2].cluster = glyphs[1].cluster;

  /* reverse, test, and reverse back */

  hb_buffer_reverse (fixture->b);
  for (i = 0; i < len; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);

  hb_buffer_reverse (fixture->b);
  for (i = 0; i < len; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);

  /* reverse_clusters twice still should return the original string,
   * but when applied once, the 1-2 cluster should be retained. */

  hb_buffer_reverse_clusters (fixture->b);
  for (i = 0; i < len; i++) {
    unsigned int j = len-1-i;
    if (j == 1)
      j = 2;
    else if (j == 2)
      j = 1;
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+j]);
  }

  hb_buffer_reverse_clusters (fixture->b);
  for (i = 0; i < len; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247


  /* test setting length */

  /* enlarge */
  g_assert (hb_buffer_set_length (fixture->b, 10));
  glyphs = hb_buffer_get_glyph_infos (fixture->b, NULL);
  g_assert_cmpint (hb_buffer_get_length (fixture->b), ==, 10);
  for (i = 0; i < 5; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
  for (i = 5; i < 10; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, 0);
  /* shrink */
  g_assert (hb_buffer_set_length (fixture->b, 3));
  glyphs = hb_buffer_get_glyph_infos (fixture->b, NULL);
  g_assert_cmpint (hb_buffer_get_length (fixture->b), ==, 3);
  for (i = 0; i < 3; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);


  /* test reset clears content */

  hb_buffer_reset (fixture->b);
  g_assert_cmpint (hb_buffer_get_length (fixture->b), ==, 0);
B
Behdad Esfahbod 已提交
248 249 250 251 252
}

static void
test_buffer_positions (Fixture *fixture, gconstpointer user_data)
{
253
  unsigned int i, len, len2;
B
Behdad Esfahbod 已提交
254 255 256
  hb_glyph_position_t *positions;

  /* Without shaping, positions should all be zero */
257 258 259 260
  len = hb_buffer_get_length (fixture->b);
  positions = hb_buffer_get_glyph_positions (fixture->b, NULL); /* test NULL */
  positions = hb_buffer_get_glyph_positions (fixture->b, &len2);
  g_assert_cmpint (len, ==, len2);
B
Behdad Esfahbod 已提交
261 262 263 264 265 266 267
  for (i = 0; i < len; i++) {
    g_assert_cmpint (0, ==, positions[i].x_advance);
    g_assert_cmpint (0, ==, positions[i].y_advance);
    g_assert_cmpint (0, ==, positions[i].x_offset);
    g_assert_cmpint (0, ==, positions[i].y_offset);
    g_assert_cmpint (0, ==, positions[i].var.i32);
  }
268 269 270 271

  /* test reset clears content */
  hb_buffer_reset (fixture->b);
  g_assert_cmpint (hb_buffer_get_length (fixture->b), ==, 0);
B
Behdad Esfahbod 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
}

int
main (int argc, char **argv)
{
  int i;

  g_test_init (&argc, &argv, NULL);

  for (i = 0; i < BUFFER_NUM_TYPES; i++) {
#define TEST_ADD(path, func) \
    G_STMT_START { \
      char *s = g_strdup_printf ("%s/%s", path, buffer_names[i]); \
      g_test_add (s, Fixture, GINT_TO_POINTER (i), fixture_init, func, fixture_fini); \
      g_free (s); \
    } G_STMT_END
    TEST_ADD ("/buffer/properties", test_buffer_properties);
    TEST_ADD ("/buffer/contents", test_buffer_contents);
    TEST_ADD ("/buffer/positions", test_buffer_positions);
#undef TEST_ADD
  }

B
Behdad Esfahbod 已提交
294
  /* XXX test invalid UTF-8 / UTF-16 text input (also overlong sequences) */
295
  /* XXX test pre_allocate(), allocation_successful(), and memory management */
B
Behdad Esfahbod 已提交
296 297 298

  return g_test_run();
}