dte test coverage


Directory: ./
File: test/test.h
Date: 2025-09-07 23:01:39
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 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_NE(a, b) EXPECT(ne, a, b)
61 #define EXPECT_UINT_EQ(a, b) EXPECT(uint_eq, a, b)
62 #define EXPECT_STRVIEW_EQ_CSTRING(sv, cstr) EXPECT(strview_eq_cstring, sv, cstr)
63 #define EXPECT_STRING_EQ_CSTRING(s, cstr) EXPECT(string_eq_cstring, s, cstr)
64 #define EXPECT_NULL(p) EXPECT(null, p)
65 #define EXPECT_NONNULL(p) EXPECT(nonnull, p)
66 #define EXPECT_TRUE(x) EXPECT(true, x)
67 #define EXPECT_FALSE(x) EXPECT(false, x)
68 #define IEXPECT_EQ(a, b) IEXPECT(eq, a, b)
69 #define IEXPECT_STREQ(s1, s2) IEXPECT(streq, s1, s2)
70 #define IEXPECT_TRUE(x) IEXPECT(true, x)
71 #define ASSERT_PTREQ(p1, p2) ASSERT(ptreq, p1, p2)
72 #define ASSERT_EQ(a, b) ASSERT(eq, a, b)
73 #define ASSERT_NE(a, b) ASSERT(ne, a, b)
74 #define ASSERT_TRUE(x) ASSERT(true, x)
75 #define ASSERT_NONNULL(ptr) ASSERT(nonnull, ptr)
76
77 37080 static inline void test_pass(TestContext *ctx)
78 {
79 37080 ctx->passed++;
80 37080 }
81
82 void test_fail(TestContext *ctx, const char *file, int line, const char *format, ...) COLD PRINTF(4);
83 void expect_streq(TestContext *ctx, const char *file, int line, const char *s1, const char *s2);
84 void expect_ptreq(TestContext *ctx, const char *file, int line, const void *p1, const void *p2);
85 void expect_memeq(TestContext *ctx, const char *file, int line, const void *p1, size_t n1, const void *p2, size_t n2);
86 void expect_eq(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b);
87 void expect_ne(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b);
88 void expect_uint_eq(TestContext *ctx, const char *file, int line, uintmax_t a, uintmax_t b);
89 void expect_true(TestContext *ctx, const char *file, int line, bool x);
90 void expect_false(TestContext *ctx, const char *file, int line, bool x);
91 void expect_null(TestContext *ctx, const char *file, int line, const void *p);
92 void expect_nonnull(TestContext *ctx, const char *file, int line, const void *p);
93 void iexpect_streq(TestContext *ctx, const char *file, int line, size_t i, const char *s1, const char *s2);
94 void iexpect_eq(TestContext *ctx, const char *file, int line, size_t i, intmax_t a, intmax_t b);
95 void iexpect_true(TestContext *ctx, const char *file, int line, size_t i, bool x);
96 void assert_ptreq(TestContext *ctx, const char *file, int line, const void *p1, const void *p2);
97 void assert_eq(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b);
98 void assert_ne(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b);
99 void assert_true(TestContext *ctx, const char *file, int line, bool x);
100 void assert_nonnull(TestContext *ctx, const char *file, int line, const void *ptr);
101
102 74 static inline void expect_strview_eq_cstring (
103 TestContext *ctx, const char *file, int line,
104 StringView sv, const char *cstr
105 ) {
106 74 expect_memeq(ctx, file, line, sv.data, sv.length, cstr, strlen(cstr));
107 74 }
108
109 122 static inline void expect_string_eq_cstring (
110 TestContext *ctx, const char *file, int line,
111 const String *s, const char *cstr
112 ) {
113 122 expect_memeq(ctx, file, line, s->buffer, s->len, cstr, strlen(cstr));
114 122 }
115
116 #endif
117