summaryrefslogtreecommitdiff
path: root/tests/test.h
blob: 38f5d6ea50cbcad05389e452db6aa29d5c5fba6e (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
#ifndef TEST_H
#define TEST_H

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

#define ASSERT_INT_EQ(X, Y)                                                                        \
    do {                                                                                           \
        int xxx = (X);                                                                             \
        int yyy = (Y);                                                                             \
        if (xxx != yyy) {                                                                          \
            fprintf(stderr,                                                                        \
                    "*** %s ***\n%s is not equal to %s\nwhere\n%s is %d\nand\n%s "                 \
                    "is %d\n",                                                                     \
                    __FUNCTION__,                                                                  \
                    #X,                                                                            \
                    #Y,                                                                            \
                    #X,                                                                            \
                    xxx,                                                                           \
                    #Y,                                                                            \
                    yyy);                                                                          \
            exit(1);                                                                               \
        }                                                                                          \
    } while (0)

#define ASSERT_STR_EQ(X, Y)                                                                        \
    do {                                                                                           \
        const char* xxx = (X);                                                                     \
        const char* yyy = (Y);                                                                     \
        if (strcmp(xxx, yyy) != 0) {                                                               \
            fprintf(stderr,                                                                        \
                    "*** %s ***\n%s is not equal to %s\nwhere\n%s is %s\nand\n%s "                 \
                    "is %s\n",                                                                     \
                    __FUNCTION__,                                                                  \
                    #X,                                                                            \
                    #Y,                                                                            \
                    #X,                                                                            \
                    xxx,                                                                           \
                    #Y,                                                                            \
                    yyy);                                                                          \
            exit(1);                                                                               \
        }                                                                                          \
    } while (0)

#define ASSERT_MEM_EQ(X, Y, SIZE)                                                                  \
    do {                                                                                           \
        const char* xxx = (X);                                                                     \
        const char* yyy = (Y);                                                                     \
        if (memcmp(xxx, yyy, SIZE) != 0) {                                                         \
            fprintf(stderr,                                                                        \
                    "*** %s ***\n%s is not equal to %s\nwhere\n%s is %.*s\nand\n%s "               \
                    "is %.*s\n",                                                                   \
                    __FUNCTION__,                                                                  \
                    #X,                                                                            \
                    #Y,                                                                            \
                    #X,                                                                            \
                    SIZE,                                                                          \
                    xxx,                                                                           \
                    #Y,                                                                            \
                    SIZE,                                                                          \
                    yyy);                                                                          \
            exit(1);                                                                               \
        }                                                                                          \
    } while (0)

#define ASSERT_NULL(X)                                                                             \
    do {                                                                                           \
        const void* xxx = (X);                                                                     \
        if (xxx != NULL) {                                                                         \
            fprintf(stderr, "*** %s ***\n%s is not NULL (was %p)\n", __FUNCTION__, #X, xxx);       \
            exit(1);                                                                               \
        }                                                                                          \
    } while (0)

#endif