| 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 yellow[6]; | ||
| 21 | char cyan[6]; | ||
| 22 | char dim[5]; | ||
| 23 | char sgr0[5]; | ||
| 24 | bool timing; // Print the time taken to run each TestGroup | ||
| 25 | bool quiet; // Print output only for test failures | ||
| 26 | } TestContext; | ||
| 27 | |||
| 28 | typedef struct { | ||
| 29 | const char *name; | ||
| 30 | void (*func)(TestContext *ctx); | ||
| 31 | } TestEntry; | ||
| 32 | |||
| 33 | typedef struct { | ||
| 34 | const TestEntry *tests; | ||
| 35 | size_t nr_tests; | ||
| 36 | } TestGroup; | ||
| 37 | |||
| 38 | #define TEST(e) { \ | ||
| 39 | .name = #e, \ | ||
| 40 | .func = e \ | ||
| 41 | } | ||
| 42 | |||
| 43 | #define TEST_GROUP(t) { \ | ||
| 44 | .tests = t, \ | ||
| 45 | .nr_tests = ARRAYLEN(t) \ | ||
| 46 | } | ||
| 47 | |||
| 48 | #define FOR_EACH_I(i, array) \ | ||
| 49 | for (size_t i = 0; i < ARRAYLEN(array); i++) | ||
| 50 | |||
| 51 | #define TEST_FAIL(...) test_fail(ctx, __FILE__, __LINE__, __VA_ARGS__) | ||
| 52 | #define EXPECT(fn, ...) expect_##fn(ctx, __FILE__, __LINE__, __VA_ARGS__) | ||
| 53 | #define IEXPECT(fn, ...) iexpect_##fn(ctx, __FILE__, __LINE__, i, __VA_ARGS__) | ||
| 54 | #define ASSERT(fn, ...) assert_##fn(ctx, __FILE__, __LINE__, __VA_ARGS__) | ||
| 55 | |||
| 56 | #define EXPECT_STREQ(s1, s2) EXPECT(streq, s1, s2) | ||
| 57 | #define EXPECT_PTREQ(p1, p2) EXPECT(ptreq, p1, p2) | ||
| 58 | #define EXPECT_MEMEQ(p1, n1, p2, n2) EXPECT(memeq, p1, n1, p2, n2) | ||
| 59 | #define EXPECT_EQ(a, b) EXPECT(eq, a, b) | ||
| 60 | #define EXPECT_EQ3(a, b, c) EXPECT(eq3, a, b, c) | ||
| 61 | #define EXPECT_NE(a, b) EXPECT(ne, a, b) | ||
| 62 | #define EXPECT_UINT_EQ(a, b) EXPECT(uint_eq, a, b) | ||
| 63 | #define EXPECT_STRVIEW_EQ_CSTRING(sv, cstr) EXPECT(strview_eq_cstring, sv, cstr) | ||
| 64 | #define EXPECT_STRING_EQ_CSTRING(s, cstr) EXPECT(string_eq_cstring, s, cstr) | ||
| 65 | #define EXPECT_NULL(p) EXPECT(null, p) | ||
| 66 | #define EXPECT_NONNULL(p) EXPECT(nonnull, p) | ||
| 67 | #define EXPECT_TRUE(x) EXPECT(true, x) | ||
| 68 | #define EXPECT_FALSE(x) EXPECT(false, x) | ||
| 69 | #define IEXPECT_EQ(a, b) IEXPECT(eq, a, b) | ||
| 70 | #define IEXPECT_STREQ(s1, s2) IEXPECT(streq, s1, s2) | ||
| 71 | #define IEXPECT_TRUE(x) IEXPECT(true, x) | ||
| 72 | #define ASSERT_PTREQ(p1, p2) ASSERT(ptreq, p1, p2) | ||
| 73 | #define ASSERT_EQ(a, b) ASSERT(eq, a, b) | ||
| 74 | #define ASSERT_NE(a, b) ASSERT(ne, a, b) | ||
| 75 | #define ASSERT_TRUE(x) ASSERT(true, x) | ||
| 76 | #define ASSERT_NONNULL(ptr) ASSERT(nonnull, ptr) | ||
| 77 | |||
| 78 | 37087 | static inline void test_pass(TestContext *ctx) | |
| 79 | { | ||
| 80 | 37087 | ctx->passed++; | |
| 81 | 37087 | } | |
| 82 | |||
| 83 | void test_fail(TestContext *ctx, const char *file, int line, const char *format, ...) COLD PRINTF(4); | ||
| 84 | void expect_streq(TestContext *ctx, const char *file, int line, const char *s1, const char *s2); | ||
| 85 | void expect_ptreq(TestContext *ctx, const char *file, int line, const void *p1, const void *p2); | ||
| 86 | void expect_memeq(TestContext *ctx, const char *file, int line, const void *p1, size_t n1, const void *p2, size_t n2); | ||
| 87 | void expect_eq(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b); | ||
| 88 | void expect_eq3(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b, intmax_t c); | ||
| 89 | void expect_ne(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b); | ||
| 90 | void expect_uint_eq(TestContext *ctx, const char *file, int line, uintmax_t a, uintmax_t b); | ||
| 91 | void expect_true(TestContext *ctx, const char *file, int line, bool x); | ||
| 92 | void expect_false(TestContext *ctx, const char *file, int line, bool x); | ||
| 93 | void expect_null(TestContext *ctx, const char *file, int line, const void *p); | ||
| 94 | void expect_nonnull(TestContext *ctx, const char *file, int line, const void *p); | ||
| 95 | void iexpect_streq(TestContext *ctx, const char *file, int line, size_t i, const char *s1, const char *s2); | ||
| 96 | void iexpect_eq(TestContext *ctx, const char *file, int line, size_t i, intmax_t a, intmax_t b); | ||
| 97 | void iexpect_true(TestContext *ctx, const char *file, int line, size_t i, bool x); | ||
| 98 | void assert_ptreq(TestContext *ctx, const char *file, int line, const void *p1, const void *p2); | ||
| 99 | void assert_eq(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b); | ||
| 100 | void assert_ne(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b); | ||
| 101 | void assert_true(TestContext *ctx, const char *file, int line, bool x); | ||
| 102 | void assert_nonnull(TestContext *ctx, const char *file, int line, const void *ptr); | ||
| 103 | |||
| 104 | 74 | static inline void expect_strview_eq_cstring ( | |
| 105 | TestContext *ctx, const char *file, int line, | ||
| 106 | StringView sv, const char *cstr | ||
| 107 | ) { | ||
| 108 | 74 | expect_memeq(ctx, file, line, sv.data, sv.length, cstr, strlen(cstr)); | |
| 109 | 74 | } | |
| 110 | |||
| 111 | 141 | static inline void expect_string_eq_cstring ( | |
| 112 | TestContext *ctx, const char *file, int line, | ||
| 113 | const String *s, const char *cstr | ||
| 114 | ) { | ||
| 115 | 141 | expect_memeq(ctx, file, line, s->buffer, s->len, cstr, strlen(cstr)); | |
| 116 | 141 | } | |
| 117 | |||
| 118 | #endif | ||
| 119 |