hb-test.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright © 2011 Google, Inc.
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. *
  24. * Google Author(s): Behdad Esfahbod
  25. */
  26. #ifndef HB_TEST_H
  27. #define HB_TEST_H
  28. #include <hb-config.hh>
  29. #include <hb-glib.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. HB_BEGIN_DECLS
  34. /* Just in case */
  35. #undef G_DISABLE_ASSERT
  36. #define HB_UNUSED G_GNUC_UNUSED
  37. /* Misc */
  38. /* This is too ugly to be public API, but quite handy. */
  39. #define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
  40. ((const char *) s)[1], \
  41. ((const char *) s)[2], \
  42. ((const char *) s)[3]))
  43. #define HB_FACE_ADD_TABLE(face, tag, data) \
  44. do { \
  45. hb_blob_t *blob = hb_blob_create_or_fail ((data), \
  46. sizeof (data), \
  47. HB_MEMORY_MODE_READONLY, \
  48. NULL, NULL); \
  49. hb_face_builder_add_table ((face), \
  50. HB_TAG_CHAR4(tag), \
  51. blob); \
  52. hb_blob_destroy (blob); \
  53. } while (0)
  54. static inline const char *
  55. srcdir (void)
  56. {
  57. static const char *s;
  58. if (!s) {
  59. s = getenv ("srcdir");
  60. #ifdef SRCDIR
  61. if (!s || !s[0])
  62. s = SRCDIR;
  63. #endif
  64. if (!s || !s[0])
  65. s = ".";
  66. }
  67. return s;
  68. }
  69. /* Helpers */
  70. static inline void
  71. hb_test_init (int *argc, char ***argv)
  72. {
  73. g_test_init (argc, argv, NULL);
  74. }
  75. static inline int
  76. hb_test_run (void)
  77. {
  78. return g_test_run ();
  79. }
  80. /* Bugzilla helpers */
  81. static inline void
  82. hb_test_bug (const char *uri_base, unsigned int number)
  83. {
  84. char *s = g_strdup_printf ("%u", number);
  85. g_test_bug_base (uri_base);
  86. g_test_bug (s);
  87. g_free (s);
  88. }
  89. static inline void
  90. hb_test_bug_freedesktop (unsigned int number)
  91. {
  92. hb_test_bug ("https://bugs.freedesktop.org/", number);
  93. }
  94. static inline void
  95. hb_test_bug_gnome (unsigned int number)
  96. {
  97. hb_test_bug ("https://bugzilla.gnome.org/", number);
  98. }
  99. static inline void
  100. hb_test_bug_mozilla (unsigned int number)
  101. {
  102. hb_test_bug ("https://bugzilla.mozilla.org/", number);
  103. }
  104. static inline void
  105. hb_test_bug_redhat (unsigned int number)
  106. {
  107. hb_test_bug ("https://bugzilla.redhat.com/", number);
  108. }
  109. /* Wrap glib test functions to simplify. Should have been in glib already. */
  110. /* Drops the "test_" prefix and converts '_' to '/'.
  111. * Essentially builds test path from function name. */
  112. static inline char *
  113. hb_test_normalize_path (const char *path)
  114. {
  115. char *s, *p;
  116. g_assert (0 == strncmp (path, "test_", 5));
  117. path += 4;
  118. s = g_strdup (path);
  119. for (p = s; *p; p++)
  120. if (*p == '_')
  121. *p = '/';
  122. return s;
  123. }
  124. #if GLIB_CHECK_VERSION(2,25,12)
  125. typedef GTestFunc hb_test_func_t;
  126. typedef GTestDataFunc hb_test_data_func_t;
  127. typedef GTestFixtureFunc hb_test_fixture_func_t;
  128. #else
  129. typedef void (*hb_test_func_t) (void);
  130. typedef void (*hb_test_data_func_t) (gconstpointer user_data);
  131. typedef void (*hb_test_fixture_func_t) (void);
  132. #endif
  133. #if !GLIB_CHECK_VERSION(2,30,0)
  134. #define g_test_fail() g_error("Test failed")
  135. #endif
  136. #ifndef g_assert_true
  137. #define g_assert_true g_assert
  138. #endif
  139. #ifndef g_assert_cmpmem
  140. #define g_assert_cmpmem(m1, l1, m2, l2) g_assert_true (l1 == l2 && memcmp (m1, m2, l1) == 0)
  141. #endif
  142. static inline void hb_test_assert_blobs_equal (hb_blob_t *expected_blob, hb_blob_t *actual_blob)
  143. {
  144. unsigned int expected_length, actual_length;
  145. const char *raw_expected = hb_blob_get_data (expected_blob, &expected_length);
  146. const char *raw_actual = hb_blob_get_data (actual_blob, &actual_length);
  147. g_assert_cmpint(expected_length, ==, actual_length);
  148. if (memcmp (raw_expected, raw_actual, expected_length) != 0)
  149. {
  150. for (unsigned int i = 0; i < expected_length; i++)
  151. {
  152. int expected = *(raw_expected + i);
  153. int actual = *(raw_actual + i);
  154. if (expected != actual) fprintf(stderr, "+%u %02x != %02x\n", i, expected, actual);
  155. else fprintf(stderr, "+%u %02x\n", i, expected);
  156. }
  157. }
  158. g_assert_cmpint(0, ==, memcmp(raw_expected, raw_actual, expected_length));
  159. }
  160. static inline void
  161. hb_test_add_func (const char *test_path,
  162. hb_test_func_t test_func)
  163. {
  164. char *normal_path = hb_test_normalize_path (test_path);
  165. g_test_add_func (normal_path, test_func);
  166. g_free (normal_path);
  167. }
  168. #define hb_test_add(Func) hb_test_add_func (#Func, Func)
  169. static inline void
  170. hb_test_add_func_flavor (const char *test_path,
  171. const char *flavor,
  172. hb_test_func_t test_func)
  173. {
  174. char *path = g_strdup_printf ("%s/%s", test_path, flavor);
  175. hb_test_add_func (path, test_func);
  176. g_free (path);
  177. }
  178. #define hb_test_add_flavor(Flavor, Func) hb_test_add_func (#Func, Flavor, Func)
  179. static inline void
  180. hb_test_add_data_func (const char *test_path,
  181. gconstpointer test_data,
  182. hb_test_data_func_t test_func)
  183. {
  184. char *normal_path = hb_test_normalize_path (test_path);
  185. g_test_add_data_func (normal_path, test_data, test_func);
  186. g_free (normal_path);
  187. }
  188. #define hb_test_add_data(UserData, Func) hb_test_add_data_func (#Func, UserData, Func)
  189. static inline void
  190. hb_test_add_data_func_flavor (const char *test_path,
  191. const char *flavor,
  192. gconstpointer test_data,
  193. hb_test_data_func_t test_func)
  194. {
  195. char *path = g_strdup_printf ("%s/%s", test_path, flavor);
  196. hb_test_add_data_func (path, test_data, test_func);
  197. g_free (path);
  198. }
  199. #define hb_test_add_data_flavor(UserData, Flavor, Func) hb_test_add_data_func_flavor (#Func, Flavor, UserData, Func)
  200. static inline void
  201. hb_test_add_vtable (const char *test_path,
  202. gsize data_size,
  203. gconstpointer test_data,
  204. hb_test_fixture_func_t data_setup,
  205. hb_test_fixture_func_t data_test,
  206. hb_test_fixture_func_t data_teardown)
  207. {
  208. char *normal_path = hb_test_normalize_path (test_path);
  209. g_test_add_vtable (normal_path, data_size, test_data, data_setup, data_test, data_teardown);
  210. g_free (normal_path);
  211. }
  212. #define hb_test_add_fixture(FixturePrefix, UserData, Func) \
  213. G_STMT_START { \
  214. typedef G_PASTE (FixturePrefix, _t) Fixture; \
  215. void (*add_vtable) (const char*, gsize, gconstpointer, \
  216. void (*) (Fixture*, gconstpointer), \
  217. void (*) (Fixture*, gconstpointer), \
  218. void (*) (Fixture*, gconstpointer)) \
  219. = (void (*) (const gchar *, gsize, gconstpointer, \
  220. void (*) (Fixture*, gconstpointer), \
  221. void (*) (Fixture*, gconstpointer), \
  222. void (*) (Fixture*, gconstpointer))) hb_test_add_vtable; \
  223. add_vtable (#Func, sizeof (G_PASTE (FixturePrefix, _t)), UserData, \
  224. G_PASTE (FixturePrefix, _init), Func, G_PASTE (FixturePrefix, _finish)); \
  225. } G_STMT_END
  226. static inline void
  227. hb_test_add_vtable_flavor (const char *test_path,
  228. const char *flavor,
  229. gsize data_size,
  230. gconstpointer test_data,
  231. hb_test_fixture_func_t data_setup,
  232. hb_test_fixture_func_t data_test,
  233. hb_test_fixture_func_t data_teardown)
  234. {
  235. char *path = g_strdup_printf ("%s/%s", test_path, flavor);
  236. hb_test_add_vtable (path, data_size, test_data, data_setup, data_test, data_teardown);
  237. g_free (path);
  238. }
  239. #define hb_test_add_fixture_flavor(FixturePrefix, UserData, Flavor, Func) \
  240. G_STMT_START { \
  241. typedef G_PASTE (FixturePrefix, _t) Fixture; \
  242. void (*add_vtable) (const char*, const char *, gsize, gconstpointer, \
  243. void (*) (Fixture*, gconstpointer), \
  244. void (*) (Fixture*, gconstpointer), \
  245. void (*) (Fixture*, gconstpointer)) \
  246. = (void (*) (const gchar *, const char *, gsize, gconstpointer, \
  247. void (*) (Fixture*, gconstpointer), \
  248. void (*) (Fixture*, gconstpointer), \
  249. void (*) (Fixture*, gconstpointer))) hb_test_add_vtable_flavor; \
  250. add_vtable (#Func, Flavor, sizeof (G_PASTE (FixturePrefix, _t)), UserData, \
  251. G_PASTE (FixturePrefix, _init), Func, G_PASTE (FixturePrefix, _finish)); \
  252. } G_STMT_END
  253. static inline hb_face_t *
  254. hb_test_open_font_file (const char *font_path)
  255. {
  256. #if GLIB_CHECK_VERSION(2,37,2)
  257. char *path = g_test_build_filename (G_TEST_DIST, font_path, NULL);
  258. #else
  259. char *path = g_strdup (font_path);
  260. #endif
  261. hb_blob_t *blob = hb_blob_create_from_file_or_fail (path);
  262. hb_face_t *face;
  263. if (!blob)
  264. g_error ("Font %s not found.", path);
  265. face = hb_face_create (blob, 0);
  266. hb_blob_destroy (blob);
  267. g_free (path);
  268. return face;
  269. }
  270. HB_END_DECLS
  271. #endif /* HB_TEST_H */