Sat, 25 Jan 2025 15:22:01 +0100
documentation of test.h
relates to #451
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 |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
9 | CxTestSuite *suite = cx_test_suite_new("My Suite"); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
10 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
11 | cx_test_register(suite, test_foo); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
12 | cx_test_register(suite, test_bar); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
13 | cx_test_register(suite, test_baz); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
14 | ``` |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
15 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
16 | 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
|
17 | 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
|
18 | 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
|
19 | 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
|
20 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
21 | <tabs> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
22 | <tab title="Test"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
23 | <code-block lang="C"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
24 | CX_TEST(test_foo) { |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
25 | struct mystruct *x = malloc(sizeof(*x)); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
26 | x->d = 42; |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
27 | CX_TEST_DO { |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
28 | int y = foo(x); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
29 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
30 | // auto-generated failure message |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
31 | CX_TEST_ASSERT(y == 42); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
32 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
33 | // alternatively: custom failure message |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
34 | CX_TEST_ASSERTM(y == 42, |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
35 | "foo does not return correct value"); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
36 | } |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
37 | free(x); |
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 | </code-block> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
40 | </tab> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
41 | <tab title="Implementation"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
42 | <code-block lang="C"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
43 | struct mystruct { |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
44 | int d; |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
45 | }; |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
46 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
47 | int foo(struct mystruct *s) { |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
48 | return s->d; |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
49 | } |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
50 | </code-block> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
51 | </tab> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
52 | </tabs> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
53 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
54 | 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
|
55 | or one of the convenience macros. |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
56 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
57 | <tabs> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
58 | <tab title="stdout"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
59 | <code-block lang="C"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
60 | cx_test_run_stdout(suite); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
61 | </code-block> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
62 | </tab> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
63 | <tab title="FILE*"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
64 | <code-block lang="C"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
65 | FILE *f = fopen("test-result.txt", "w"); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
66 | cx_test_run_f(suite, f); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
67 | fclose(f); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
68 | </code-block> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
69 | </tab> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
70 | <tab title="Other"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
71 | <code-block lang="C"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
72 | // for example: UCX buffer |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
73 | CxBuffer buf; |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
74 | cxBufferInit(&buf, NULL, 1024, NULL, 0); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
75 | cx_test_run(suite, &buf, cxBufferWriteFunc); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
76 | // do something with the buffer |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
77 | cxBufferDestroy(&buf); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
78 | </code-block> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
79 | </tab> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
80 | </tabs> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
81 | 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
|
82 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
83 | ```C |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
84 | cx_test_suite_free(suite); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
85 | ``` |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
86 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
87 | ## Test Subroutines |
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 | 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
|
90 | 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
|
91 | 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
|
92 | 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
|
93 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
94 | <code-block lang="C"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
95 | 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
|
96 | x->d = d; |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
97 | CX_TEST_ASSERT(foo(x) == d); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
98 | } |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
99 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
100 | CX_TEST(test_foo) { |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
101 | struct mystruct *x = malloc(sizeof(*x)); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
102 | CX_TEST_DO { |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
103 | CX_TEST_CALL_SUBROUTINE(test_foo_params, x, 42); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
104 | CX_TEST_CALL_SUBROUTINE(test_foo_params, x, -42); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
105 | CX_TEST_CALL_SUBROUTINE(test_foo_params, x, 1337); |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
106 | } |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
107 | free(x); |
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 | </code-block> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
110 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
111 | > 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
|
112 | > 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
|
113 | > 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
|
114 | |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
115 | <seealso> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
116 | <category ref="apidoc"> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
117 | <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
|
118 | </category> |
8ff82697f2c3
documentation of test.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
119 | </seealso> |