]> git.ipfire.org Git - thirdparty/git.git/blob - t/unit-tests/test-lib.h
Merge branch 'jc/test-i18ngrep'
[thirdparty/git.git] / t / unit-tests / test-lib.h
1 #ifndef TEST_LIB_H
2 #define TEST_LIB_H
3
4 #include "git-compat-util.h"
5
6 /*
7 * Run a test function, returns 1 if the test succeeds, 0 if it
8 * fails. If test_skip_all() has been called then the test will not be
9 * run. The description for each test should be unique. For example:
10 *
11 * TEST(test_something(arg1, arg2), "something %d %d", arg1, arg2)
12 */
13 #define TEST(t, ...) \
14 test__run_end(test__run_begin() ? 0 : (t, 1), \
15 TEST_LOCATION(), __VA_ARGS__)
16
17 /*
18 * Print a test plan, should be called before any tests. If the number
19 * of tests is not known in advance test_done() will automatically
20 * print a plan at the end of the test program.
21 */
22 void test_plan(int count);
23
24 /*
25 * test_done() must be called at the end of main(). It will print the
26 * plan if plan() was not called at the beginning of the test program
27 * and returns the exit code for the test program.
28 */
29 int test_done(void);
30
31 /* Skip the current test. */
32 __attribute__((format (printf, 1, 2)))
33 void test_skip(const char *format, ...);
34
35 /* Skip all remaining tests. */
36 __attribute__((format (printf, 1, 2)))
37 void test_skip_all(const char *format, ...);
38
39 /* Print a diagnostic message to stdout. */
40 __attribute__((format (printf, 1, 2)))
41 void test_msg(const char *format, ...);
42
43 /*
44 * Test checks are built around test_assert(). checks return 1 on
45 * success, 0 on failure. If any check fails then the test will fail. To
46 * create a custom check define a function that wraps test_assert() and
47 * a macro to wrap that function to provide a source location and
48 * stringified arguments. Custom checks that take pointer arguments
49 * should be careful to check that they are non-NULL before
50 * dereferencing them. For example:
51 *
52 * static int check_oid_loc(const char *loc, const char *check,
53 * struct object_id *a, struct object_id *b)
54 * {
55 * int res = test_assert(loc, check, a && b && oideq(a, b));
56 *
57 * if (!res) {
58 * test_msg(" left: %s", a ? oid_to_hex(a) : "NULL";
59 * test_msg(" right: %s", b ? oid_to_hex(a) : "NULL";
60 *
61 * }
62 * return res;
63 * }
64 *
65 * #define check_oid(a, b) \
66 * check_oid_loc(TEST_LOCATION(), "oideq("#a", "#b")", a, b)
67 */
68 int test_assert(const char *location, const char *check, int ok);
69
70 /* Helper macro to pass the location to checks */
71 #define TEST_LOCATION() TEST__MAKE_LOCATION(__LINE__)
72
73 /* Check a boolean condition. */
74 #define check(x) \
75 check_bool_loc(TEST_LOCATION(), #x, x)
76 int check_bool_loc(const char *loc, const char *check, int ok);
77
78 /*
79 * Compare two integers. Prints a message with the two values if the
80 * comparison fails. NB this is not thread safe.
81 */
82 #define check_int(a, op, b) \
83 (test__tmp[0].i = (a), test__tmp[1].i = (b), \
84 check_int_loc(TEST_LOCATION(), #a" "#op" "#b, \
85 test__tmp[0].i op test__tmp[1].i, \
86 test__tmp[0].i, test__tmp[1].i))
87 int check_int_loc(const char *loc, const char *check, int ok,
88 intmax_t a, intmax_t b);
89
90 /*
91 * Compare two unsigned integers. Prints a message with the two values
92 * if the comparison fails. NB this is not thread safe.
93 */
94 #define check_uint(a, op, b) \
95 (test__tmp[0].u = (a), test__tmp[1].u = (b), \
96 check_uint_loc(TEST_LOCATION(), #a" "#op" "#b, \
97 test__tmp[0].u op test__tmp[1].u, \
98 test__tmp[0].u, test__tmp[1].u))
99 int check_uint_loc(const char *loc, const char *check, int ok,
100 uintmax_t a, uintmax_t b);
101
102 /*
103 * Compare two chars. Prints a message with the two values if the
104 * comparison fails. NB this is not thread safe.
105 */
106 #define check_char(a, op, b) \
107 (test__tmp[0].c = (a), test__tmp[1].c = (b), \
108 check_char_loc(TEST_LOCATION(), #a" "#op" "#b, \
109 test__tmp[0].c op test__tmp[1].c, \
110 test__tmp[0].c, test__tmp[1].c))
111 int check_char_loc(const char *loc, const char *check, int ok,
112 char a, char b);
113
114 /* Check whether two strings are equal. */
115 #define check_str(a, b) \
116 check_str_loc(TEST_LOCATION(), "!strcmp("#a", "#b")", a, b)
117 int check_str_loc(const char *loc, const char *check,
118 const char *a, const char *b);
119
120 /*
121 * Wrap a check that is known to fail. If the check succeeds then the
122 * test will fail. Returns 1 if the check fails, 0 if it
123 * succeeds. For example:
124 *
125 * TEST_TODO(check(0));
126 */
127 #define TEST_TODO(check) \
128 (test__todo_begin(), test__todo_end(TEST_LOCATION(), #check, check))
129
130 /* Private helpers */
131
132 #define TEST__STR(x) #x
133 #define TEST__MAKE_LOCATION(line) __FILE__ ":" TEST__STR(line)
134
135 union test__tmp {
136 intmax_t i;
137 uintmax_t u;
138 char c;
139 };
140
141 extern union test__tmp test__tmp[2];
142
143 int test__run_begin(void);
144 __attribute__((format (printf, 3, 4)))
145 int test__run_end(int, const char *, const char *, ...);
146 void test__todo_begin(void);
147 int test__todo_end(const char *, const char *, int);
148
149 #endif /* TEST_LIB_H */