Thu, 30 Oct 2025 19:27:18 +0100
fix typo bug in cxListDifference() - resolves #745
| 1143 
0559812df10c
assign proper names to the documentation topics
 Mike Becker <universe@uap-core.de> parents: 
1141diff
changeset | 1 | # Test Framework | 
| 1148 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 2 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
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: 
1143diff
changeset | 4 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
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: 
1143diff
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: 
1143diff
changeset | 7 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 8 | ```C | 
| 1174 
ee473780cc0d
add missing documentation about what header to include
 Mike Becker <universe@uap-core.de> parents: 
1148diff
changeset | 9 | #include <cx/test.h> | 
| 
ee473780cc0d
add missing documentation about what header to include
 Mike Becker <universe@uap-core.de> parents: 
1148diff
changeset | 10 | |
| 1148 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 11 | CxTestSuite *suite = cx_test_suite_new("My Suite"); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 12 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 13 | cx_test_register(suite, test_foo); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 14 | cx_test_register(suite, test_bar); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 15 | cx_test_register(suite, test_baz); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 16 | ``` | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 17 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
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: 
1143diff
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: 
1143diff
changeset | 20 | In UCX this is realized by the `CX_TEST_DO` macro with which you can define a scope for the actual test. | 
| 1424 
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
 Mike Becker <universe@uap-core.de> parents: 
1420diff
changeset | 21 | Whenever a test assertion fails, the scope is exited and the tear-down code is executed. | 
| 1148 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 22 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 23 | <tabs> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 24 | <tab title="Test"> | 
| 1277 
637d4775e79e
fixes some docs compiler complaints
 Mike Becker <universe@uap-core.de> parents: 
1174diff
changeset | 25 | <code-block lang="C"><![CDATA[ | 
| 1148 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 26 | CX_TEST(test_foo) { | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 27 | struct mystruct *x = malloc(sizeof(*x)); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 28 | x->d = 42; | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 29 | CX_TEST_DO { | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 30 | int y = foo(x); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 31 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 32 | // auto-generated failure message | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 33 | CX_TEST_ASSERT(y == 42); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 34 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 35 | // alternatively: custom failure message | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 36 | CX_TEST_ASSERTM(y == 42, | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 37 | "foo does not return correct value"); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 38 | } | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 39 | free(x); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 40 | } | 
| 1277 
637d4775e79e
fixes some docs compiler complaints
 Mike Becker <universe@uap-core.de> parents: 
1174diff
changeset | 41 | ]]></code-block> | 
| 1148 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 42 | </tab> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 43 | <tab title="Implementation"> | 
| 1277 
637d4775e79e
fixes some docs compiler complaints
 Mike Becker <universe@uap-core.de> parents: 
1174diff
changeset | 44 | <code-block lang="C"><![CDATA[ | 
| 1148 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 45 | struct mystruct { | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 46 | int d; | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 47 | }; | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 48 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 49 | int foo(struct mystruct *s) { | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 50 | return s->d; | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 51 | } | 
| 1277 
637d4775e79e
fixes some docs compiler complaints
 Mike Becker <universe@uap-core.de> parents: 
1174diff
changeset | 52 | ]]></code-block> | 
| 1148 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 53 | </tab> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 54 | </tabs> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 55 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
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: 
1143diff
changeset | 57 | or one of the convenience macros. | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 58 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 59 | <tabs> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 60 | <tab title="stdout"> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 61 | <code-block lang="C"> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 62 | cx_test_run_stdout(suite); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 63 | </code-block> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 64 | </tab> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 65 | <tab title="FILE*"> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 66 | <code-block lang="C"> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 67 | FILE *f = fopen("test-result.txt", "w"); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 68 | cx_test_run_f(suite, f); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 69 | fclose(f); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 70 | </code-block> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 71 | </tab> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 72 | <tab title="Other"> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 73 | <code-block lang="C"> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 74 | // for example: UCX buffer | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 75 | CxBuffer buf; | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 76 | cxBufferInit(&buf, NULL, 1024, NULL, 0); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 77 | cx_test_run(suite, &buf, cxBufferWriteFunc); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 78 | // do something with the buffer | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 79 | cxBufferDestroy(&buf); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 80 | </code-block> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 81 | </tab> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 82 | </tabs> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 83 | Finally, clean up all resources consumed by the test suite. | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 84 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 85 | ```C | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 86 | cx_test_suite_free(suite); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 87 | ``` | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 88 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 89 | ## Test Subroutines | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 90 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 91 | For parameterized testing you can define and call test subroutines. | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
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: 
1143diff
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: 
1143diff
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: 
1143diff
changeset | 95 | |
| 1420 
c6f55a2b3495
fix various typos in the web documentation
 Mike Becker <universe@uap-core.de> parents: 
1277diff
changeset | 96 | ```c | 
| 1148 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
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: 
1143diff
changeset | 98 | x->d = d; | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 99 | CX_TEST_ASSERT(foo(x) == d); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 100 | } | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 101 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 102 | CX_TEST(test_foo) { | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 103 | struct mystruct *x = malloc(sizeof(*x)); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 104 | CX_TEST_DO { | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 105 | CX_TEST_CALL_SUBROUTINE(test_foo_params, x, 42); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 106 | CX_TEST_CALL_SUBROUTINE(test_foo_params, x, -42); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 107 | CX_TEST_CALL_SUBROUTINE(test_foo_params, x, 1337); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 108 | } | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 109 | free(x); | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 110 | } | 
| 1420 
c6f55a2b3495
fix various typos in the web documentation
 Mike Becker <universe@uap-core.de> parents: 
1277diff
changeset | 111 | ``` | 
| 1148 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 112 | |
| 1424 
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
 Mike Becker <universe@uap-core.de> parents: 
1420diff
changeset | 113 | > Any test function, test case or test subroutine is a normal C function. | 
| 1148 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 114 | > As such you can decide to make them `static` when you do not want external linkage. | 
| 1424 
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
 Mike Becker <universe@uap-core.de> parents: 
1420diff
changeset | 115 | > For example, you can define the test as `static CX_TEST(test_foo)` instead of `CX_TEST(test_foo)`. | 
| 1148 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 116 | |
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 117 | <seealso> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 118 | <category ref="apidoc"> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
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: 
1143diff
changeset | 120 | </category> | 
| 
8ff82697f2c3
documentation of test.h
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 121 | </seealso> |