aboutsummaryrefslogtreecommitdiff
path: root/src/include/test-util.h
blob: 4ec36fc6fe368c7608e009e5be39716740716b8d (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
#ifndef TEST_UTIL_H
#define TEST_UTIL_H

#include <stdio.h>
#include <string.h>

unsigned int tests = 0, failed = 0, cur_test = 0;

void description(char *str)
{
    printf("# DESCRIPTION: %s\n", str);
}

void plan(int n)
{
    tests = n; failed = 0; cur_test = 0;
    printf("1..%d\n", tests);
}

int conclude()
{
    float perc = 100.0f * (1.0f - ((float)failed / tests));
    printf("# CONCLUSION: (%.2f%%) %d out of %d tests failed\n", perc, failed, tests);

    if(tests != cur_test) {
        printf("#     expected %d, but got %d tests\n", tests, cur_test);
    }

    if(failed != 0) {
        return 1;
    }

    return 0;
}

void ok(int val, char *desc)
{
    cur_test++;
    if(val == 0) {
        printf("ok %d - %s\n", cur_test, desc);
    } else {
        printf("not ok %d - %s\n", cur_test, desc);
        printf("    returned \'%d\'\n", val);
        failed++;
    }
}
void is_i(int val, int exp, char *desc)
{
    cur_test++;
    if(val == exp) {
        printf("ok %d - %s\n", cur_test, desc);
    } else {
        printf("not ok %d - %s\n", cur_test, desc);
        printf("    expected \'%d\', but got \'%d\'\n", exp, val);
        failed++;
    }
}

void is_s(char *val, char *exp, char *desc)
{
    cur_test++;
    if(strcmp(val, exp) == 0) {
        printf("ok %d - %s\n", cur_test, desc);
    } else {
        printf("not ok %d - %s\n", cur_test, desc);
        printf("    expected \'%s\', but got \'%s\'\n", exp, val);
        failed++;
    }
}

#endif