2025-01-18 17:24:36 +03:00
|
|
|
#ifndef BH_UNIT_H
|
|
|
|
|
#define BH_UNIT_H
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2025-11-08 18:50:13 +03:00
|
|
|
#include <string.h>
|
2025-01-29 09:19:34 +03:00
|
|
|
#include <math.h>
|
2025-01-18 17:24:36 +03:00
|
|
|
|
2025-11-08 18:50:13 +03:00
|
|
|
|
2025-01-30 13:53:26 +03:00
|
|
|
typedef int (*BH_UnitCallback)(void);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
2025-11-08 18:50:13 +03:00
|
|
|
|
2025-01-18 17:24:36 +03:00
|
|
|
#define BH_VERIFY(e) \
|
2025-01-29 09:19:34 +03:00
|
|
|
do { \
|
|
|
|
|
if (!(e)) { \
|
|
|
|
|
printf("%s:%d\t%s\n", __FILE__, __LINE__, #e); \
|
|
|
|
|
return -1; \
|
|
|
|
|
} \
|
|
|
|
|
} while(0)
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#define BH_FAIL(msg) \
|
2025-01-29 09:19:34 +03:00
|
|
|
do { \
|
|
|
|
|
printf("%s:%d\t%s\n", __FILE__, __LINE__, msg); \
|
|
|
|
|
return -1; \
|
|
|
|
|
} while(0)
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#define BH_VERIFY_DELTA(x, y, e) \
|
2025-01-29 09:19:34 +03:00
|
|
|
do { \
|
|
|
|
|
double BH_VERIFY_DELTA = (x)-(y); \
|
|
|
|
|
if (BH_VERIFY_DELTA < 0.0) \
|
|
|
|
|
BH_VERIFY_DELTA = -BH_VERIFY_DELTA; \
|
|
|
|
|
if (BH_VERIFY_DELTA > (e)) { \
|
2025-02-24 09:37:22 +03:00
|
|
|
printf("%s:%d\t%s (differs by %f)\n", \
|
|
|
|
|
__FILE__, __LINE__, #x " == " #y, BH_VERIFY_DELTA); \
|
2025-01-29 09:19:34 +03:00
|
|
|
return -1; \
|
|
|
|
|
} \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
2025-01-18 17:24:36 +03:00
|
|
|
|
2025-11-08 18:50:13 +03:00
|
|
|
#define BH_VERIFY_STR_EQ(actual, expected) \
|
|
|
|
|
do { \
|
|
|
|
|
BH_VERIFY((actual) != NULL && (expected) != NULL); \
|
|
|
|
|
if (strcmp((actual), (expected)) != 0) { \
|
|
|
|
|
printf("%s:%d\tExpected '%s', got '%s'\n", \
|
|
|
|
|
__FILE__, __LINE__, (expected), (actual)); \
|
|
|
|
|
return -1; \
|
|
|
|
|
} \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
|
2025-02-24 09:37:22 +03:00
|
|
|
#define BH_UNIT_TEST(name) \
|
|
|
|
|
static int unit##name(void)
|
|
|
|
|
|
|
|
|
|
|
2025-10-12 10:20:09 +03:00
|
|
|
#define BH_UNIT_ADD(name) \
|
2025-02-24 09:37:22 +03:00
|
|
|
BH_UnitAdd(#name, unit##name)
|
|
|
|
|
|
2025-01-30 13:53:26 +03:00
|
|
|
|
|
|
|
|
void BH_UnitAdd(const char *name,
|
|
|
|
|
BH_UnitCallback cb);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int BH_UnitRun(void);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
2025-10-12 10:20:09 +03:00
|
|
|
|
2025-01-18 17:24:36 +03:00
|
|
|
#endif /* BH_UNIT_H */
|