aboutsummaryrefslogtreecommitdiff
path: root/tests/test.h
blob: 7d38fd094a782fae0daeff7c60453530d8394d0f (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * test.h
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#ifndef TEST_H
#define TEST_H

#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <errno.h>

#if defined(__GNUC__) || defined(__clang__)
#	define ATTRIB_UNUSED __attribute__ ((unused))
#else
#	define ATTRIB_UNUSED
#endif

static ATTRIB_UNUSED FILE *test_open_read(const char *path)
{
	FILE *fp = fopen(path, "rb");

	if (fp == NULL) {
		perror(path);
		abort();
	}

	return fp;
}

static ATTRIB_UNUSED void test_assert(const char *expr, int value,
				      int linenum)
{
	if (value == 0) {
		fprintf(stderr, "%d: '%s' is false!\n", linenum, expr);
		abort();
	}
}

static ATTRIB_UNUSED void test_str_equal(const char *expr, const char *value,
					 const char *ref, int linenum)
{
	if (strcmp(value, ref) != 0) {
		fprintf(stderr,
			"%d: '%s' should be '%s', but actually is '%s'!\n",
			linenum, expr, ref, value);
		abort();
	}
}

static ATTRIB_UNUSED void test_not_null(const void *value, const char *expr,
					int linenum)
{
	if (value == NULL) {
		fprintf(stderr, "%d: '%s' should not be NULL, but is!\n",
			linenum, expr);
		abort();
	}
}

static ATTRIB_UNUSED void test_null(const void *value, const char *expr,
				    int linenum)
{
	if (value != NULL) {
		fprintf(stderr, "%d: '%s' should be NULL, but is!\n",
			linenum, expr);
		abort();
	}
}

static ATTRIB_UNUSED void test_equal_ul(const char *lhs, const char *rhs,
					unsigned long lval, unsigned long rval,
					int linenum)
{
	if (lval != rval) {
		fprintf(stderr, "%d: %s (%lu) does not equal %s (%lu)!\n",
			linenum, lhs, lval, rhs, rval);
		abort();
	}
}

static ATTRIB_UNUSED void test_equal_sl(const char *lhs, const char *rhs,
					long lval, long rval, int linenum)
{
	if (lval != rval) {
		fprintf(stderr, "%d: %s (%ld) does not equal %s (%ld)!\n",
			linenum, lhs, lval, rhs, rval);
		abort();
	}
}

static ATTRIB_UNUSED void test_lt_ul(const char *lhs, const char *rhs,
				     unsigned long lval, unsigned long rval,
				     int linenum)
{
	if (lval >= rval) {
		fprintf(stderr, "%d: %s (%lu) is not less than %s (%lu)!\n",
			linenum, lhs, lval, rhs, rval);
		abort();
	}
}

#define TEST_STR_EQUAL(str, ref) test_str_equal(#str, str, ref, __LINE__)

#define TEST_NULL(expr) test_null(expr, #expr, __LINE__)

#define TEST_NOT_NULL(expr) test_not_null(expr, #expr, __LINE__)

#define TEST_EQUAL_I(a, b) test_equal_sl(#a, #b, a, b, __LINE__)

#define TEST_EQUAL_UI(a, b) test_equal_ul(#a, #b, a, b, __LINE__)

#define TEST_LESS_THAN_UI(a, b) test_lt_ul(#a, #b, a, b, __LINE__)

#define TEST_ASSERT(expr) test_assert(#expr, (expr), __LINE__)

#endif /* TEST_H */