ocarina/include/tests/test.h

119 lines
2.9 KiB
C

/*
* Copyright 2014 (c) Anna Schumaker.
*/
#ifndef OCARINA_TESTS_TEST_H
#define OCARINA_TESTS_TEST_H
#include <fcntl.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#define g_assert_cmpstr_free(lhs, cmp, rhs) \
do { \
g_assert_cmpstr(lhs, cmp, rhs); \
g_free(lhs); \
} while (0)
#ifndef __cplusplus
static inline gchar *lutos(long unsigned int u)
{ return g_strdup_printf("%lu", u); }
static inline gchar *stos(const char *s) { return g_strdup(s); }
static inline gchar *utos(unsigned int u) { return g_strdup_printf("%u", u); }
static inline gchar *ltos(long int l) { return g_strdup_printf("%lu", l); }
static inline gchar *itos(int i) { return g_strdup_printf("%i", i); }
static inline gchar *ftos(float f) { return g_strdup_printf("%f", f); }
static inline gchar *btos(bool b) { return g_strdup(b ? "true" : "false"); }
static inline gchar *ptos(void *p) { return g_strdup_printf("%p", p); }
#define tostring(x) (_Generic((x), \
char *: stos, \
const char *: stos, \
unsigned char: utos, \
bool: btos, \
long unsigned int: lutos, \
unsigned int: utos, \
short unsigned int: utos, \
long int: ltos, \
int: itos, \
float:ftos, \
void *:ptos \
) (x))
#endif /* __cplusplus */
struct UnitTest {
const char *t_name;
void (*t_func)();
};
extern struct UnitTest unit_tests[];
extern unsigned int unit_tests_size;;
#define UNIT_TEST(name, func) \
[__COUNTER__] = { \
.t_name = name, \
.t_func = func, \
}
#define DECLARE_UNIT_TESTS(...) \
struct UnitTest unit_tests[] = {\
__VA_ARGS__ \
}; \
unsigned int unit_tests_size = __COUNTER__
extern unsigned int tests_failed;
void test_strings_equal(gchar *, gchar *, unsigned int);
void test_strings_not_equal(gchar *, gchar *, unsigned int);
void loop_strings_equal(gchar *, gchar *, unsigned int, unsigned int);
void loop_strings_not_equal(gchar *, gchar *, unsigned int, unsigned int);
#define test_equal(lhs, rhs) \
test_strings_equal(tostring(lhs), tostring(rhs), __LINE__)
#define test_not_equal(lhs, rhs) \
test_strings_not_equal(tostring(lhs), tostring(rhs), __LINE__)
#define test_str_equal(lhs, rhs) \
test_strings_equal(lhs, g_strdup(rhs), __LINE__)
#define test_loop_equal(lhs, rhs, i) \
if (1) { \
loop_strings_equal(tostring(lhs), tostring(rhs), i, __LINE__); \
if (tests_failed > 0) \
break; \
}
#define test_loop_not_equal(lhs, rhs, i) \
if (1) { \
loop_strings_not_equal(tostring(lhs), tostring(rhs), i, __LINE__); \
if (tests_failed > 0) \
break; \
}
#define test_loop_str_equal(lhs, rhs, i) \
if (1) { \
loop_strings_equal(lhs, tostring(rhs), i, __LINE__); \
if (tests_failed > 0) \
break; \
}
#define test_loop_passed() \
if (tests_failed == 0) \
test_equal(tests_failed, 0)
gchar *test_data_file(const gchar *);
bool test_data_file_exists(const gchar *);
#endif /* OCARINA_TESTS_TEST_H */