Blame test/api/hb-test.h

Packit 874993
/*
Packit 874993
 * Copyright © 2011  Google, Inc.
Packit 874993
 *
Packit 874993
 *  This is part of HarfBuzz, a text shaping library.
Packit 874993
 *
Packit 874993
 * Permission is hereby granted, without written agreement and without
Packit 874993
 * license or royalty fees, to use, copy, modify, and distribute this
Packit 874993
 * software and its documentation for any purpose, provided that the
Packit 874993
 * above copyright notice and the following two paragraphs appear in
Packit 874993
 * all copies of this software.
Packit 874993
 *
Packit 874993
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
Packit 874993
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
Packit 874993
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
Packit 874993
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
Packit 874993
 * DAMAGE.
Packit 874993
 *
Packit 874993
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
Packit 874993
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
Packit 874993
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
Packit 874993
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
Packit 874993
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Packit 874993
 *
Packit 874993
 * Google Author(s): Behdad Esfahbod
Packit 874993
 */
Packit 874993
Packit 874993
#ifndef HB_TEST_H
Packit 874993
#define HB_TEST_H
Packit 874993
Packit 874993
#include <config.h>
Packit 874993
Packit 874993
#include <hb-glib.h>
Packit 874993
Packit 874993
#include <stdlib.h>
Packit 874993
#include <string.h>
Packit 874993
#include <stdio.h>
Packit 874993
Packit 874993
HB_BEGIN_DECLS
Packit 874993
Packit 874993
/* Just in case */
Packit 874993
#undef G_DISABLE_ASSERT
Packit 874993
Packit 874993
Packit 874993
/* Misc */
Packit 874993
Packit 874993
/* This is too ugly to be public API, but quite handy. */
Packit 874993
#define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
Packit 874993
				  ((const char *) s)[1], \
Packit 874993
				  ((const char *) s)[2], \
Packit 874993
				  ((const char *) s)[3]))
Packit 874993
Packit 874993
Packit 874993
static inline const char *
Packit 874993
srcdir (void)
Packit 874993
{
Packit 874993
  static const char *s;
Packit 874993
Packit 874993
  if (!s) {
Packit 874993
    s = getenv ("srcdir");
Packit 874993
Packit 874993
#ifdef SRCDIR
Packit 874993
    if (!s || !s[0])
Packit 874993
      s = SRCDIR;
Packit 874993
#endif
Packit 874993
Packit 874993
    if (!s || !s[0])
Packit 874993
      s = ".";
Packit 874993
  }
Packit 874993
Packit 874993
  return s;
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/* Helpers */
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_test_init (int *argc, char ***argv)
Packit 874993
{
Packit 874993
  g_test_init (argc, argv, NULL);
Packit 874993
}
Packit 874993
Packit 874993
static inline int
Packit 874993
hb_test_run (void)
Packit 874993
{
Packit 874993
  return g_test_run ();
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/* Bugzilla helpers */
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_test_bug (const char *uri_base, unsigned int number)
Packit 874993
{
Packit 874993
  char *s = g_strdup_printf ("%u", number);
Packit 874993
Packit 874993
  g_test_bug_base (uri_base);
Packit 874993
  g_test_bug (s);
Packit 874993
Packit 874993
  g_free (s);
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_test_bug_freedesktop (unsigned int number)
Packit 874993
{
Packit 874993
  hb_test_bug ("http://bugs.freedesktop.org/", number);
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_test_bug_gnome (unsigned int number)
Packit 874993
{
Packit 874993
  hb_test_bug ("http://bugzilla.gnome.org/", number);
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_test_bug_mozilla (unsigned int number)
Packit 874993
{
Packit 874993
  hb_test_bug ("http://bugzilla.mozilla.org/", number);
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_test_bug_redhat (unsigned int number)
Packit 874993
{
Packit 874993
  hb_test_bug ("http://bugzilla.redhat.com/", number);
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/* Wrap glib test functions to simplify.  Should have been in glib already. */
Packit 874993
Packit 874993
/* Drops the "test_" prefix and converts '_' to '/'.
Packit 874993
 * Essentially builds test path from function name. */
Packit 874993
static inline char *
Packit 874993
hb_test_normalize_path (const char *path)
Packit 874993
{
Packit 874993
  char *s, *p;
Packit 874993
Packit 874993
  g_assert (0 == strncmp (path, "test_", 5));
Packit 874993
  path += 4;
Packit 874993
Packit 874993
  s = g_strdup (path);
Packit 874993
  for (p = s; *p; p++)
Packit 874993
    if (*p == '_')
Packit 874993
      *p = '/';
Packit 874993
Packit 874993
  return s;
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
#if GLIB_CHECK_VERSION(2,25,12)
Packit 874993
typedef GTestFunc        hb_test_func_t;
Packit 874993
typedef GTestDataFunc    hb_test_data_func_t;
Packit 874993
typedef GTestFixtureFunc hb_test_fixture_func_t;
Packit 874993
#else
Packit 874993
typedef void (*hb_test_func_t)         (void);
Packit 874993
typedef void (*hb_test_data_func_t)    (gconstpointer user_data);
Packit 874993
typedef void (*hb_test_fixture_func_t) (void);
Packit 874993
#endif
Packit 874993
Packit 874993
#if !GLIB_CHECK_VERSION(2,30,0)
Packit 874993
#define g_test_fail() g_error("Test failed")
Packit 874993
#endif
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_test_add_func (const char *test_path,
Packit 874993
		  hb_test_func_t   test_func)
Packit 874993
{
Packit 874993
  char *normal_path = hb_test_normalize_path (test_path);
Packit 874993
  g_test_add_func (normal_path, test_func);
Packit 874993
  g_free (normal_path);
Packit 874993
}
Packit 874993
#define hb_test_add(Func) hb_test_add_func (#Func, Func)
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_test_add_func_flavor (const char *test_path,
Packit 874993
			 const char *flavor,
Packit 874993
			 hb_test_func_t   test_func)
Packit 874993
{
Packit 874993
  char *path = g_strdup_printf ("%s/%s", test_path, flavor);
Packit 874993
  hb_test_add_func (path, test_func);
Packit 874993
  g_free (path);
Packit 874993
}
Packit 874993
#define hb_test_add_flavor(Flavor, Func) hb_test_add_func (#Func, Flavor, Func)
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_test_add_data_func (const char          *test_path,
Packit 874993
		       gconstpointer        test_data,
Packit 874993
		       hb_test_data_func_t  test_func)
Packit 874993
{
Packit 874993
  char *normal_path = hb_test_normalize_path (test_path);
Packit 874993
  g_test_add_data_func (normal_path, test_data, test_func);
Packit 874993
  g_free (normal_path);
Packit 874993
}
Packit 874993
#define hb_test_add_data(UserData, Func) hb_test_add_data_func (#Func, UserData, Func)
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_test_add_data_func_flavor (const char          *test_path,
Packit 874993
			      const char          *flavor,
Packit 874993
			      gconstpointer        test_data,
Packit 874993
			      hb_test_data_func_t  test_func)
Packit 874993
{
Packit 874993
  char *path = g_strdup_printf ("%s/%s", test_path, flavor);
Packit 874993
  hb_test_add_data_func (path, test_data, test_func);
Packit 874993
  g_free (path);
Packit 874993
}
Packit 874993
#define hb_test_add_data_flavor(UserData, Flavor, Func) hb_test_add_data_func_flavor (#Func, Flavor, UserData, Func)
Packit 874993
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_test_add_vtable (const char             *test_path,
Packit 874993
		    gsize                   data_size,
Packit 874993
		    gconstpointer           test_data,
Packit 874993
		    hb_test_fixture_func_t  data_setup,
Packit 874993
		    hb_test_fixture_func_t  data_test,
Packit 874993
		    hb_test_fixture_func_t  data_teardown)
Packit 874993
{
Packit 874993
  char *normal_path = hb_test_normalize_path (test_path);
Packit 874993
  g_test_add_vtable (normal_path, data_size, test_data, data_setup, data_test, data_teardown);
Packit 874993
  g_free (normal_path);
Packit 874993
}
Packit 874993
#define hb_test_add_fixture(FixturePrefix, UserData, Func) \
Packit 874993
G_STMT_START { \
Packit 874993
  typedef G_PASTE (FixturePrefix, _t) Fixture; \
Packit 874993
  void (*add_vtable) (const char*, gsize, gconstpointer, \
Packit 874993
		      void (*) (Fixture*, gconstpointer), \
Packit 874993
		      void (*) (Fixture*, gconstpointer), \
Packit 874993
		      void (*) (Fixture*, gconstpointer)) \
Packit 874993
	= (void (*) (const gchar *, gsize, gconstpointer, \
Packit 874993
		     void (*) (Fixture*, gconstpointer), \
Packit 874993
		     void (*) (Fixture*, gconstpointer), \
Packit 874993
		     void (*) (Fixture*, gconstpointer))) hb_test_add_vtable; \
Packit 874993
  add_vtable (#Func, sizeof (G_PASTE (FixturePrefix, _t)), UserData, \
Packit 874993
	      G_PASTE (FixturePrefix, _init), Func, G_PASTE (FixturePrefix, _finish)); \
Packit 874993
} G_STMT_END
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_test_add_vtable_flavor (const char             *test_path,
Packit 874993
			   const char             *flavor,
Packit 874993
			   gsize                   data_size,
Packit 874993
			   gconstpointer           test_data,
Packit 874993
			   hb_test_fixture_func_t  data_setup,
Packit 874993
			   hb_test_fixture_func_t  data_test,
Packit 874993
			   hb_test_fixture_func_t  data_teardown)
Packit 874993
{
Packit 874993
  char *path = g_strdup_printf ("%s/%s", test_path, flavor);
Packit 874993
  hb_test_add_vtable (path, data_size, test_data, data_setup, data_test, data_teardown);
Packit 874993
  g_free (path);
Packit 874993
}
Packit 874993
#define hb_test_add_fixture_flavor(FixturePrefix, UserData, Flavor, Func) \
Packit 874993
G_STMT_START { \
Packit 874993
  typedef G_PASTE (FixturePrefix, _t) Fixture; \
Packit 874993
  void (*add_vtable) (const char*, const char *, gsize, gconstpointer, \
Packit 874993
		      void (*) (Fixture*, gconstpointer), \
Packit 874993
		      void (*) (Fixture*, gconstpointer), \
Packit 874993
		      void (*) (Fixture*, gconstpointer)) \
Packit 874993
	= (void (*) (const gchar *, const char *, gsize, gconstpointer, \
Packit 874993
		     void (*) (Fixture*, gconstpointer), \
Packit 874993
		     void (*) (Fixture*, gconstpointer), \
Packit 874993
		     void (*) (Fixture*, gconstpointer))) hb_test_add_vtable_flavor; \
Packit 874993
  add_vtable (#Func, Flavor, sizeof (G_PASTE (FixturePrefix, _t)), UserData, \
Packit 874993
	      G_PASTE (FixturePrefix, _init), Func, G_PASTE (FixturePrefix, _finish)); \
Packit 874993
} G_STMT_END
Packit 874993
Packit 874993
Packit 874993
HB_END_DECLS
Packit 874993
Packit 874993
#endif /* HB_TEST_H */