hb-test.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #ifdef HAVE_STDBOOL_H
  34. # include <stdbool.h>
  35. #else
  36. typedef short bool;
  37. # ifndef true
  38. # define true 1
  39. # endif
  40. # ifndef false
  41. # define false 0
  42. # endif
  43. #endif
  44. HB_BEGIN_DECLS
  45. /* Just in case */
  46. #undef G_DISABLE_ASSERT
  47. #define HB_UNUSED G_GNUC_UNUSED
  48. /* Misc */
  49. /* This is too ugly to be public API, but quite handy. */
  50. #define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
  51. ((const char *) s)[1], \
  52. ((const char *) s)[2], \
  53. ((const char *) s)[3]))
  54. #define HB_FACE_ADD_TABLE(face, tag, data) \
  55. do { \
  56. hb_blob_t *blob = hb_blob_create_or_fail ((data), \
  57. sizeof (data), \
  58. HB_MEMORY_MODE_READONLY, \
  59. NULL, NULL); \
  60. hb_face_builder_add_table ((face), \
  61. HB_TAG_CHAR4(tag), \
  62. blob); \
  63. hb_blob_destroy (blob); \
  64. } while (0)
  65. static inline const char *
  66. srcdir (void)
  67. {
  68. static const char *s;
  69. if (!s) {
  70. s = getenv ("srcdir");
  71. #ifdef SRCDIR
  72. if (!s || !s[0])
  73. s = SRCDIR;
  74. #endif
  75. if (!s || !s[0])
  76. s = ".";
  77. }
  78. return s;
  79. }
  80. /* Helpers */
  81. static inline void
  82. hb_test_init (int *argc, char ***argv)
  83. {
  84. g_test_init (argc, argv, NULL);
  85. }
  86. static inline int
  87. hb_test_run (void)
  88. {
  89. return g_test_run ();
  90. }
  91. /* Bugzilla helpers */
  92. static inline void
  93. hb_test_bug (const char *uri_base, unsigned int number)
  94. {
  95. char *s = g_strdup_printf ("%u", number);
  96. g_test_bug_base (uri_base);
  97. g_test_bug (s);
  98. g_free (s);
  99. }
  100. static inline void
  101. hb_test_bug_freedesktop (unsigned int number)
  102. {
  103. hb_test_bug ("https://bugs.freedesktop.org/", number);
  104. }
  105. static inline void
  106. hb_test_bug_gnome (unsigned int number)
  107. {
  108. hb_test_bug ("https://bugzilla.gnome.org/", number);
  109. }
  110. static inline void
  111. hb_test_bug_mozilla (unsigned int number)
  112. {
  113. hb_test_bug ("https://bugzilla.mozilla.org/", number);
  114. }
  115. static inline void
  116. hb_test_bug_redhat (unsigned int number)
  117. {
  118. hb_test_bug ("https://bugzilla.redhat.com/", number);
  119. }
  120. /* Wrap glib test functions to simplify. Should have been in glib already. */
  121. /* Drops the "test_" prefix and converts '_' to '/'.
  122. * Essentially builds test path from function name. */
  123. static inline char *
  124. hb_test_normalize_path (const char *path)
  125. {
  126. char *s, *p;
  127. g_assert (0 == strncmp (path, "test_", 5));
  128. path += 4;
  129. s = g_strdup (path);
  130. for (p = s; *p; p++)
  131. if (*p == '_')
  132. *p = '/';
  133. return s;
  134. }
  135. #if GLIB_CHECK_VERSION(2,25,12)
  136. typedef GTestFunc hb_test_func_t;
  137. typedef GTestDataFunc hb_test_data_func_t;
  138. typedef GTestFixtureFunc hb_test_fixture_func_t;
  139. #else
  140. typedef void (*hb_test_func_t) (void);
  141. typedef void (*hb_test_data_func_t) (gconstpointer user_data);
  142. typedef void (*hb_test_fixture_func_t) (void);
  143. #endif
  144. #if !GLIB_CHECK_VERSION(2,30,0)
  145. #define g_test_fail() g_error("Test failed")
  146. #endif
  147. #ifndef g_assert_true
  148. #define g_assert_true g_assert
  149. #endif
  150. #ifndef g_assert_false
  151. #define g_assert_false(expr) g_assert(!(expr))
  152. #endif
  153. #ifndef g_assert_nonnull
  154. #define g_assert_nonnull g_assert
  155. #endif
  156. #ifndef g_assert_cmpmem
  157. #define g_assert_cmpmem(m1, l1, m2, l2) g_assert_true (l1 == l2 && memcmp (m1, m2, l1) == 0)
  158. #endif
  159. static inline void hb_test_assert_blobs_equal (hb_blob_t *expected_blob, hb_blob_t *actual_blob)
  160. {
  161. unsigned int expected_length, actual_length;
  162. const char *raw_expected = hb_blob_get_data (expected_blob, &expected_length);
  163. const char *raw_actual = hb_blob_get_data (actual_blob, &actual_length);
  164. g_assert_cmpint(expected_length, ==, actual_length);
  165. if (memcmp (raw_expected, raw_actual, expected_length) != 0)
  166. {
  167. for (unsigned int i = 0; i < expected_length; i++)
  168. {
  169. int expected = *(raw_expected + i);
  170. int actual = *(raw_actual + i);
  171. if (expected != actual) fprintf(stderr, "+%u %02x != %02x\n", i, expected, actual);
  172. else fprintf(stderr, "+%u %02x\n", i, expected);
  173. }
  174. }
  175. g_assert_cmpint(0, ==, memcmp(raw_expected, raw_actual, expected_length));
  176. }
  177. static inline void
  178. hb_test_add_func (const char *test_path,
  179. hb_test_func_t test_func)
  180. {
  181. char *normal_path = hb_test_normalize_path (test_path);
  182. g_test_add_func (normal_path, test_func);
  183. g_free (normal_path);
  184. }
  185. #define hb_test_add(Func) hb_test_add_func (#Func, Func)
  186. static inline void
  187. hb_test_add_func_flavor (const char *test_path,
  188. const char *flavor,
  189. hb_test_func_t test_func)
  190. {
  191. char *path = g_strdup_printf ("%s/%s", test_path, flavor);
  192. hb_test_add_func (path, test_func);
  193. g_free (path);
  194. }
  195. #define hb_test_add_flavor(Flavor, Func) hb_test_add_func (#Func, Flavor, Func)
  196. static inline void
  197. hb_test_add_data_func (const char *test_path,
  198. gconstpointer test_data,
  199. hb_test_data_func_t test_func)
  200. {
  201. char *normal_path = hb_test_normalize_path (test_path);
  202. g_test_add_data_func (normal_path, test_data, test_func);
  203. g_free (normal_path);
  204. }
  205. #define hb_test_add_data(UserData, Func) hb_test_add_data_func (#Func, UserData, Func)
  206. static inline void
  207. hb_test_add_data_func_flavor (const char *test_path,
  208. const char *flavor,
  209. gconstpointer test_data,
  210. hb_test_data_func_t test_func)
  211. {
  212. char *path = g_strdup_printf ("%s/%s", test_path, flavor);
  213. hb_test_add_data_func (path, test_data, test_func);
  214. g_free (path);
  215. }
  216. #define hb_test_add_data_flavor(UserData, Flavor, Func) hb_test_add_data_func_flavor (#Func, Flavor, UserData, Func)
  217. static inline void
  218. hb_test_add_vtable (const char *test_path,
  219. gsize data_size,
  220. gconstpointer test_data,
  221. hb_test_fixture_func_t data_setup,
  222. hb_test_fixture_func_t data_test,
  223. hb_test_fixture_func_t data_teardown)
  224. {
  225. char *normal_path = hb_test_normalize_path (test_path);
  226. g_test_add_vtable (normal_path, data_size, test_data, data_setup, data_test, data_teardown);
  227. g_free (normal_path);
  228. }
  229. #define hb_test_add_fixture(FixturePrefix, UserData, Func) \
  230. G_STMT_START { \
  231. typedef G_PASTE (FixturePrefix, _t) Fixture; \
  232. void (*add_vtable) (const char*, gsize, gconstpointer, \
  233. void (*) (Fixture*, gconstpointer), \
  234. void (*) (Fixture*, gconstpointer), \
  235. void (*) (Fixture*, gconstpointer)) \
  236. = (void (*) (const gchar *, gsize, gconstpointer, \
  237. void (*) (Fixture*, gconstpointer), \
  238. void (*) (Fixture*, gconstpointer), \
  239. void (*) (Fixture*, gconstpointer))) hb_test_add_vtable; \
  240. add_vtable (#Func, sizeof (G_PASTE (FixturePrefix, _t)), UserData, \
  241. G_PASTE (FixturePrefix, _init), Func, G_PASTE (FixturePrefix, _finish)); \
  242. } G_STMT_END
  243. static inline void
  244. hb_test_add_vtable_flavor (const char *test_path,
  245. const char *flavor,
  246. gsize data_size,
  247. gconstpointer test_data,
  248. hb_test_fixture_func_t data_setup,
  249. hb_test_fixture_func_t data_test,
  250. hb_test_fixture_func_t data_teardown)
  251. {
  252. char *path = g_strdup_printf ("%s/%s", test_path, flavor);
  253. hb_test_add_vtable (path, data_size, test_data, data_setup, data_test, data_teardown);
  254. g_free (path);
  255. }
  256. #define hb_test_add_fixture_flavor(FixturePrefix, UserData, Flavor, Func) \
  257. G_STMT_START { \
  258. typedef G_PASTE (FixturePrefix, _t) Fixture; \
  259. void (*add_vtable) (const char*, const char *, gsize, gconstpointer, \
  260. void (*) (Fixture*, gconstpointer), \
  261. void (*) (Fixture*, gconstpointer), \
  262. void (*) (Fixture*, gconstpointer)) \
  263. = (void (*) (const gchar *, const char *, gsize, gconstpointer, \
  264. void (*) (Fixture*, gconstpointer), \
  265. void (*) (Fixture*, gconstpointer), \
  266. void (*) (Fixture*, gconstpointer))) hb_test_add_vtable_flavor; \
  267. add_vtable (#Func, Flavor, sizeof (G_PASTE (FixturePrefix, _t)), UserData, \
  268. G_PASTE (FixturePrefix, _init), Func, G_PASTE (FixturePrefix, _finish)); \
  269. } G_STMT_END
  270. static inline hb_face_t *
  271. hb_test_open_font_file (const char *font_path)
  272. {
  273. #if GLIB_CHECK_VERSION(2,37,2)
  274. char *path = g_test_build_filename (G_TEST_DIST, font_path, NULL);
  275. #else
  276. char *path = g_strdup (font_path);
  277. #endif
  278. hb_blob_t *blob = hb_blob_create_from_file_or_fail (path);
  279. hb_face_t *face;
  280. if (!blob)
  281. g_error ("Font %s not found.", path);
  282. face = hb_face_create (blob, 0);
  283. hb_blob_destroy (blob);
  284. g_free (path);
  285. return face;
  286. }
  287. HB_END_DECLS
  288. #endif /* HB_TEST_H */