aboutsummaryrefslogtreecommitdiff
path: root/test/src/Unit.c
blob: dd7123a26b92b2638e5631bb4aa5a031de6af558 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <BH/Unit.h>
#include <stdlib.h>


typedef struct BH_Unit
{
    struct BH_Unit *next;
    const char *name;
    BH_UnitCallback cb;
} BH_Unit;


static BH_Unit *root = NULL;


static void cleanup(void)
{
    BH_Unit *current;

    current = root;
    while (current)
    {
        BH_Unit *next = current->next;

        free(current);
        current = next;
    }
}


void BH_UnitAdd(const char *name, BH_UnitCallback cb)
{
    BH_Unit *unit, *current;

    /* Allocate and fill new unit test entry */
    unit = malloc(sizeof(*unit));
    if (!unit)
        return;

    unit->next = NULL;
    unit->name = name;
    unit->cb = cb;

    /* Append unit test entry */
    current = root;
    while (current && current->next)
        current = current->next;

    if (current)
        current->next = unit;
    else
        root = unit;
}


int BH_UnitRun(void)
{
    BH_Unit *current;
    int result = 0;
    printf("Running tests...\n");

    current = root;
    while (current)
    {
        printf("%s\n", current->name);
        if (current->cb())
        {
            printf("\tFAIL\n");
            result = -1;
        }
        else
            printf("\tPASS\n");

        fflush(stdout);
        current = current->next;
    }

    cleanup();
    return result;
}