Fri, 23 May 2025 12:44:24 +0200
make test-compile depend on both static and shared
the shared lib is not needed for the tests,
but when run with coverage, gcov will be confused
when outdated line information is available from
a previous shared build
1143
0559812df10c
assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents:
1141
diff
changeset
|
1 | # Test Framework |
1148
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
2 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
3 | UCX brings its own testing framework, which can also be used by your applications. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
4 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
5 | A test new suite is created by `cx_test_suite_new()`. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
6 | An arbitrary number of test cases can be registered with `cx_test_register()`. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
7 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
8 | ```C |
1174
ee473780cc0d
add missing documentation about what header to include
Mike Becker <universe@uap-core.de>
parents:
1148
diff
changeset
|
9 | #include <cx/test.h> |
ee473780cc0d
add missing documentation about what header to include
Mike Becker <universe@uap-core.de>
parents:
1148
diff
changeset
|
10 | |
1148
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
11 | CxTestSuite *suite = cx_test_suite_new("My Suite"); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
12 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
13 | cx_test_register(suite, test_foo); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
14 | cx_test_register(suite, test_bar); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
15 | cx_test_register(suite, test_baz); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
16 | ``` |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
17 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
18 | A test case needs to be defined with the `CX_TEST` macro. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
19 | The general structure of a test case is 1) setup code, 2) the actual test, 3) tear down code. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
20 | In UCX this is realized by the `CX_TEST_DO` macro with which you can define a scope for the actual test. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
21 | Whenever a test assertion fails, the scope is exited and the tear down code is executed. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
22 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
23 | <tabs> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
24 | <tab title="Test"> |
1277
637d4775e79e
fixes some docs compiler complaints
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
25 | <code-block lang="C"><![CDATA[ |
1148
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
26 | CX_TEST(test_foo) { |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
27 | struct mystruct *x = malloc(sizeof(*x)); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
28 | x->d = 42; |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
29 | CX_TEST_DO { |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
30 | int y = foo(x); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
31 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
32 | // auto-generated failure message |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
33 | CX_TEST_ASSERT(y == 42); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
34 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
35 | // alternatively: custom failure message |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
36 | CX_TEST_ASSERTM(y == 42, |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
37 | "foo does not return correct value"); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
38 | } |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
39 | free(x); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
40 | } |
1277
637d4775e79e
fixes some docs compiler complaints
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
41 | ]]></code-block> |
1148
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
42 | </tab> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
43 | <tab title="Implementation"> |
1277
637d4775e79e
fixes some docs compiler complaints
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
44 | <code-block lang="C"><![CDATA[ |
1148
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
45 | struct mystruct { |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
46 | int d; |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
47 | }; |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
48 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
49 | int foo(struct mystruct *s) { |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
50 | return s->d; |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
51 | } |
1277
637d4775e79e
fixes some docs compiler complaints
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
52 | ]]></code-block> |
1148
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
53 | </tab> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
54 | </tabs> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
55 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
56 | Once you have registered all test cases, you can run the test suite with `cx_test_run()` |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
57 | or one of the convenience macros. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
58 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
59 | <tabs> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
60 | <tab title="stdout"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
61 | <code-block lang="C"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
62 | cx_test_run_stdout(suite); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
63 | </code-block> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
64 | </tab> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
65 | <tab title="FILE*"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
66 | <code-block lang="C"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
67 | FILE *f = fopen("test-result.txt", "w"); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
68 | cx_test_run_f(suite, f); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
69 | fclose(f); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
70 | </code-block> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
71 | </tab> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
72 | <tab title="Other"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
73 | <code-block lang="C"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
74 | // for example: UCX buffer |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
75 | CxBuffer buf; |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
76 | cxBufferInit(&buf, NULL, 1024, NULL, 0); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
77 | cx_test_run(suite, &buf, cxBufferWriteFunc); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
78 | // do something with the buffer |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
79 | cxBufferDestroy(&buf); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
80 | </code-block> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
81 | </tab> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
82 | </tabs> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
83 | Finally, clean up all resources consumed by the test suite. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
84 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
85 | ```C |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
86 | cx_test_suite_free(suite); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
87 | ``` |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
88 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
89 | ## Test Subroutines |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
90 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
91 | For parameterized testing you can define and call test subroutines. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
92 | It is _not_ possible to call arbitrary functions and use the `CX_TEST_ASSERT` macro. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
93 | Instead, you define a callable test subroutine with `CX_TEST_SUBROUTINE` and call it with `CX_TEST_CALL_SUBROUTINE`. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
94 | The following example illustrates this with an adaption of the above test case. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
95 | |
1277
637d4775e79e
fixes some docs compiler complaints
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
96 | <code-block lang="C"><![CDATA[ |
1148
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
97 | CX_TEST_SUBROUTINE(test_foo_params, struct mystruct *x, int d) { |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
98 | x->d = d; |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
99 | CX_TEST_ASSERT(foo(x) == d); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
100 | } |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
101 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
102 | CX_TEST(test_foo) { |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
103 | struct mystruct *x = malloc(sizeof(*x)); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
104 | CX_TEST_DO { |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
105 | CX_TEST_CALL_SUBROUTINE(test_foo_params, x, 42); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
106 | CX_TEST_CALL_SUBROUTINE(test_foo_params, x, -42); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
107 | CX_TEST_CALL_SUBROUTINE(test_foo_params, x, 1337); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
108 | } |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
109 | free(x); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
110 | } |
1277
637d4775e79e
fixes some docs compiler complaints
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
111 | ]]></code-block> |
1148
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
112 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
113 | > Any test function, test case or test subroutine, is a normal C function. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
114 | > As such you can decide to make them `static` when you do not want external linkage. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
115 | > For example, just define the test as `static CX_TEST(test_foo)` instead of `CX_TEST(test_foo)`. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
116 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
117 | <seealso> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
118 | <category ref="apidoc"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
119 | <a href="https://ucx.sourceforge.io/api/test_8h.html">test.h</a> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
120 | </category> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
121 | </seealso> |