dte test coverage


Directory: ./
File: test/test.h
Date: 2025-02-14 16:55:22
Exec Total Coverage
Lines: 3 3 100.0%
Functions: 1 1 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
11 typedef struct {
12 unsigned int passed;
13 unsigned int failed;
14 unsigned int nfuncs;
15 unsigned int ngroups;
16 void *userdata;
17 char boldred[8];
18 char dim[5];
19 char sgr0[5];
20 bool timing;
21 } TestContext;
22
23 typedef struct {
24 const char *name;
25 void (*func)(TestContext *ctx);
26 } TestEntry;
27
28 typedef struct {
29 const TestEntry *tests;
30 size_t nr_tests;
31 } TestGroup;
32
33 #define TEST(e) { \
34 .name = #e, \
35 .func = e \
36 }
37
38 #define TEST_GROUP(t) { \
39 .tests = t, \
40 .nr_tests = ARRAYLEN(t) \
41 }
42
43 #define FOR_EACH_I(i, array) \
44 for (size_t i = 0; i < ARRAYLEN(array); i++)
45
46 #define TEST_FAIL(...) test_fail(ctx, __FILE__, __LINE__, __VA_ARGS__)
47 #define EXPECT(fn, ...) expect_##fn(ctx, __FILE__, __LINE__, __VA_ARGS__)
48 #define IEXPECT(fn, ...) iexpect_##fn(ctx, __FILE__, __LINE__, i, __VA_ARGS__)
49 #define ASSERT(fn, ...) assert_##fn(ctx, __FILE__, __LINE__, __VA_ARGS__)
50
51 #define EXPECT_STREQ(s1, s2) EXPECT(streq, s1, s2)
52 #define EXPECT_PTREQ(p1, p2) EXPECT(ptreq, p1, p2)
53 #define EXPECT_MEMEQ(p1, n1, p2, n2) EXPECT(memeq, p1, n1, p2, n2)
54 #define EXPECT_EQ(a, b) EXPECT(eq, a, b)
55 #define EXPECT_UINT_EQ(a, b) EXPECT(uint_eq, a, b)
56 #define EXPECT_NULL(p) EXPECT(null, p)
57 #define EXPECT_NONNULL(p) EXPECT(nonnull, p)
58 #define EXPECT_TRUE(x) EXPECT(true, x)
59 #define EXPECT_FALSE(x) EXPECT(false, x)
60 #define IEXPECT_EQ(a, b) IEXPECT(eq, a, b)
61 #define IEXPECT_STREQ(s1, s2) IEXPECT(streq, s1, s2)
62 #define IEXPECT_TRUE(x) IEXPECT(true, x)
63 #define ASSERT_PTREQ(p1, p2) ASSERT(ptreq, p1, p2)
64 #define ASSERT_EQ(a, b) ASSERT(eq, a, b)
65 #define ASSERT_TRUE(x) ASSERT(true, x)
66 #define ASSERT_NONNULL(ptr) ASSERT(nonnull, ptr)
67
68 34955 static inline void test_pass(TestContext *ctx)
69 {
70 34955 ctx->passed++;
71 34955 }
72
73 void test_fail(TestContext *ctx, const char *file, int line, const char *format, ...) COLD PRINTF(4);
74 void expect_streq(TestContext *ctx, const char *file, int line, const char *s1, const char *s2);
75 void expect_ptreq(TestContext *ctx, const char *file, int line, const void *p1, const void *p2);
76 void expect_memeq(TestContext *ctx, const char *file, int line, const void *p1, size_t n1, const void *p2, size_t n2);
77 void expect_eq(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b);
78 void expect_uint_eq(TestContext *ctx, const char *file, int line, uintmax_t a, uintmax_t b);
79 void expect_true(TestContext *ctx, const char *file, int line, bool x);
80 void expect_false(TestContext *ctx, const char *file, int line, bool x);
81 void expect_null(TestContext *ctx, const char *file, int line, const void *p);
82 void expect_nonnull(TestContext *ctx, const char *file, int line, const void *p);
83 void iexpect_streq(TestContext *ctx, const char *file, int line, size_t i, const char *s1, const char *s2);
84 void iexpect_eq(TestContext *ctx, const char *file, int line, size_t i, intmax_t a, intmax_t b);
85 void iexpect_true(TestContext *ctx, const char *file, int line, size_t i, bool x);
86 void assert_ptreq(TestContext *ctx, const char *file, int line, const void *p1, const void *p2);
87 void assert_eq(TestContext *ctx, const char *file, int line, intmax_t a, intmax_t b);
88 void assert_true(TestContext *ctx, const char *file, int line, bool x);
89 void assert_nonnull(TestContext *ctx, const char *file, int line, const void *ptr);
90
91 #endif
92