dte test coverage


Directory: ./
File: test/test.h
Date: 2025-07-19 20:13:10
Exec Total Coverage
Lines: 9 9 100.0%
Functions: 3 3 100.0%
Branches: 0 0 -%

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