-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.c
76 lines (66 loc) · 2.18 KB
/
example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* SPDX-License-Identifier: MIT
* Copyright (c) 2022 Karel Kočí <[email protected]>
*/
#include <stdlib.h>
#include <string.h>
#include <signal.h>
/* Every C source file file is a single suite and need to have unique name
* defined in this macro before you include check_suite.h.
*/
#define SUITE "example"
#include <check_suite.h>
/* Every test is part of some test case that is automatically included in the
* suite of this file. Suites can have setup and teardown functions. You can
* also set timeout for tests in this test-case.
* The part of the definition is also function body that can contain optionally
* some code that is executed on check framework initialization. This function
* gets argument `TCase *tcase` which you can use to further modify the check's
* test case.
*/
TEST_CASE(simple) {}
/* The basic test looks like this. You need to specify test-case it belongs to
* and its name. The body of the function contains the test code itself.
*/
TEST(simple, numeric) {
ck_assert_int_eq(42, 42);
}
END_TEST
/* You can also run multiple tests in loop. This effectively is like if you
* would add `for` loop to the testing code. The iteration variable is available
* as `int _i`.
*/
LOOP_TEST(simple, multiple_numeric, 8, 42) {
ck_assert_int_ne(_i, 0);
}
END_TEST
/* The common usage for LOOP_TEST is to iterate over array of tested data. For
* that we have ARRAY_TEST.
* The array name is deduced from the test name (just append `_d`). If you want
* to use a different array name you can specify it as optional third argument
* to the `ARRAY_TEST`.
*/
static const struct {
const char *const a;
const char *const b;
} str_cmp_d[] = {
{"", ""},
{"foo", "foo"},
};
ARRAY_TEST(simple, str_cmp) {
ck_assert_int_eq(strcmp(_d.a, _d.b), 0);
}
TEST_CASE(signals) {}
/* You can write tests that expect to be terminated with some specific signal.
*/
TEST_RAISE_SIGNAL(signals, signal_abort, SIGABRT) {
abort();
}
END_TEST
/* You can write tests that result in program termination like this.
* This is possibly only because check is set to perform fork for every test.
* This is preferred to separate tests between each other.
*/
TEST_EXIT(signals, signal_exit, 42) {
exit(42);
}
END_TEST