Line |
Branch |
Exec |
Source |
1 |
|
|
#ifndef TEST_TEST_H |
2 |
|
|
#define TEST_TEST_H |
3 |
|
|
|
4 |
|
|
#include <errno.h> |
5 |
|
|
#include <inttypes.h> |
6 |
|
|
#include <stdbool.h> |
7 |
|
|
#include <stddef.h> |
8 |
|
|
#include <string.h> |
9 |
|
|
#include "util/macros.h" |
10 |
|
|
#include "util/string-view.h" |
11 |
|
|
#include "util/string.h" |
12 |
|
|
|
13 |
|
|
typedef struct { |
14 |
|
|
unsigned int passed; |
15 |
|
|
unsigned int failed; |
16 |
|
|
unsigned int nfuncs; |
17 |
|
|
unsigned int ngroups; |
18 |
|
|
void *userdata; |
19 |
|
|
char boldred[8]; |
20 |
|
|
char dim[5]; |
21 |
|
|
char sgr0[5]; |
22 |
|
|
bool timing; |
23 |
|
|
} TestContext; |
24 |
|
|
|
25 |
|
|
typedef struct { |
26 |
|
|
const char *name; |
27 |
|
|
void (*func)(TestContext *ctx); |
28 |
|
|
} TestEntry; |
29 |
|
|
|
30 |
|
|
typedef struct { |
31 |
|
|
const TestEntry *tests; |
32 |
|
|
size_t nr_tests; |
33 |
|
|
} TestGroup; |
34 |
|
|
|
35 |
|
|
#define TEST(e) { \ |
36 |
|
|
.name = #e, \ |
37 |
|
|
.func = e \ |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
#define TEST_GROUP(t) { \ |
41 |
|
|
.tests = t, \ |
42 |
|
|
.nr_tests = ARRAYLEN(t) \ |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
#define FOR_EACH_I(i, array) \ |
46 |
|
|
for (size_t i = 0; i < ARRAYLEN(array); i++) |
47 |
|
|
|
48 |
|
|
#define TEST_FAIL(...) test_fail(ctx, __FILE__, __LINE__, __VA_ARGS__) |
49 |
|
|
#define EXPECT(fn, ...) expect_##fn(ctx, __FILE__, __LINE__, __VA_ARGS__) |
50 |
|
|
#define IEXPECT(fn, ...) iexpect_##fn(ctx, __FILE__, __LINE__, i, __VA_ARGS__) |
51 |
|
|
#define ASSERT(fn, ...) assert_##fn(ctx, __FILE__, __LINE__, __VA_ARGS__) |
52 |
|
|
|
53 |
|
|
#define EXPECT_STREQ(s1, s2) EXPECT(streq, s1, s2) |
54 |
|
|
#define EXPECT_PTREQ(p1, p2) EXPECT(ptreq, p1, p2) |
55 |
|
|
#define EXPECT_MEMEQ(p1, n1, p2, n2) EXPECT(memeq, p1, n1, p2, n2) |
56 |
|
|
#define EXPECT_EQ(a, b) EXPECT(eq, a, b) |
57 |
|
|
#define EXPECT_NE(a, b) EXPECT(ne, a, b) |
58 |
|
|
#define EXPECT_UINT_EQ(a, b) EXPECT(uint_eq, a, b) |
59 |
|
|
#define EXPECT_STRVIEW_EQ_CSTRING(sv, cstr) EXPECT(strview_eq_cstring, sv, cstr) |
60 |
|
|
#define EXPECT_STRING_EQ_CSTRING(s, cstr) EXPECT(string_eq_cstring, s, cstr) |
61 |
|
|
#define EXPECT_NULL(p) EXPECT(null, p) |
62 |
|
|
#define EXPECT_NONNULL(p) EXPECT(nonnull, p) |
63 |
|
|
#define EXPECT_TRUE(x) EXPECT(true, x) |
64 |
|
|
#define EXPECT_FALSE(x) EXPECT(false, x) |
65 |
|
|
#define IEXPECT_EQ(a, b) IEXPECT(eq, a, b) |
66 |
|
|
#define IEXPECT_STREQ(s1, s2) IEXPECT(streq, s1, s2) |
67 |
|
|
#define IEXPECT_TRUE(x) IEXPECT(true, x) |
68 |
|
|
#define ASSERT_PTREQ(p1, p2) ASSERT(ptreq, p1, p2) |
69 |
|
|
#define ASSERT_EQ(a, b) ASSERT(eq, a, b) |
70 |
|
|
#define ASSERT_NE(a, b) ASSERT(ne, a, b) |
71 |
|
|
#define ASSERT_TRUE(x) ASSERT(true, x) |
72 |
|
|
#define ASSERT_NONNULL(ptr) ASSERT(nonnull, ptr) |
73 |
|
|
|
74 |
|
34961 |
static inline void test_pass(TestContext *ctx) |
75 |
|
|
{ |
76 |
|
34961 |
ctx->passed++; |
77 |
|
34961 |
} |
78 |
|
|
|
79 |
|
|
void test_fail(TestContext *ctx, const char *file, int line, const char *format, ...) COLD PRINTF(4); |
80 |
|
|
void expect_streq(TestContext *ctx, const char *file, int line, const char *s1, const char *s2); |
81 |
|
|
void expect_ptreq(TestContext *ctx, const char *file, int line, const void *p1, const void *p2); |
82 |
|
|
void expect_memeq(TestContext *ctx, const char *file, int line, const void *p1, size_t n1, const void *p2, size_t n2); |
83 |
|
|
void expect_eq(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b); |
84 |
|
|
void expect_ne(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b); |
85 |
|
|
void expect_uint_eq(TestContext *ctx, const char *file, int line, uintmax_t a, uintmax_t b); |
86 |
|
|
void expect_true(TestContext *ctx, const char *file, int line, bool x); |
87 |
|
|
void expect_false(TestContext *ctx, const char *file, int line, bool x); |
88 |
|
|
void expect_null(TestContext *ctx, const char *file, int line, const void *p); |
89 |
|
|
void expect_nonnull(TestContext *ctx, const char *file, int line, const void *p); |
90 |
|
|
void iexpect_streq(TestContext *ctx, const char *file, int line, size_t i, const char *s1, const char *s2); |
91 |
|
|
void iexpect_eq(TestContext *ctx, const char *file, int line, size_t i, intmax_t a, intmax_t b); |
92 |
|
|
void iexpect_true(TestContext *ctx, const char *file, int line, size_t i, bool x); |
93 |
|
|
void assert_ptreq(TestContext *ctx, const char *file, int line, const void *p1, const void *p2); |
94 |
|
|
void assert_eq(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b); |
95 |
|
|
void assert_ne(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b); |
96 |
|
|
void assert_true(TestContext *ctx, const char *file, int line, bool x); |
97 |
|
|
void assert_nonnull(TestContext *ctx, const char *file, int line, const void *ptr); |
98 |
|
|
|
99 |
|
53 |
static inline void expect_strview_eq_cstring ( |
100 |
|
|
TestContext *ctx, const char *file, int line, |
101 |
|
|
const StringView *sv, const char *cstr |
102 |
|
|
) { |
103 |
|
53 |
expect_memeq(ctx, file, line, sv->data, sv->length, cstr, strlen(cstr)); |
104 |
|
53 |
} |
105 |
|
|
|
106 |
|
120 |
static inline void expect_string_eq_cstring ( |
107 |
|
|
TestContext *ctx, const char *file, int line, |
108 |
|
|
const String *s, const char *cstr |
109 |
|
|
) { |
110 |
|
120 |
expect_memeq(ctx, file, line, s->buffer, s->len, cstr, strlen(cstr)); |
111 |
|
120 |
} |
112 |
|
|
|
113 |
|
|
#endif |
114 |
|
|
|