aboutsummaryrefslogtreecommitdiff
path: root/unit/include/BH/Unit.h
blob: c715c23b323690f62a1ed22099c4fbf8da523206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef BH_UNIT_H
#define BH_UNIT_H

#include <stdio.h>
#include <math.h>

typedef int (*BH_UnitCallback)(void);

#define BH_VERIFY(e) \
    do { \
        if (!(e)) { \
            printf("%s:%d\t%s\n", __FILE__, __LINE__, #e); \
            return -1; \
        } \
    } while(0)


#define BH_FAIL(msg) \
    do { \
        printf("%s:%d\t%s\n", __FILE__, __LINE__, msg); \
        return -1; \
    } while(0)


#define BH_VERIFY_DELTA(x, y, e) \
    do { \
        double BH_VERIFY_DELTA = (x)-(y); \
        if (BH_VERIFY_DELTA < 0.0) \
            BH_VERIFY_DELTA = -BH_VERIFY_DELTA; \
        if (BH_VERIFY_DELTA > (e)) { \
            printf("%s:%d\t%s (differs by %f)\n", \
                   __FILE__, __LINE__, #x " == " #y, BH_VERIFY_DELTA); \
            return -1; \
        } \
    } while(0)


#define BH_UNIT_TEST(name) \
    static int unit##name(void)


#define     BH_UNIT_ADD(name) \
    BH_UnitAdd(#name, unit##name)


/**
 * Adds unit test \a cb with name \a name for the testing.
 *
 * \param name  Unit test name
 * \param cb    Unit test function
 */
void BH_UnitAdd(const char *name,
                BH_UnitCallback cb);


/**
 * Runs unit tests.
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_UnitRun(void);

#endif /* BH_UNIT_H */