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
77
78
79
80
81
82
|
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
static int setup_only(void **state)
{
*state = malloc(1);
return 0;
}
static int teardown_only(void **state)
{
free(*state);
return 0;
}
static void malloc_setup_test(void **state)
{
assert_non_null(*state);
free(*state);
}
static void malloc_teardown_test(void **state)
{
*state = malloc(1);
assert_non_null(*state);
}
static int prestate_setup(void **state)
{
int *val = (int *)*state, *a;
a = malloc(sizeof(int));
*a = *val + 1;
*state = a;
return 0;
}
static int prestate_teardown(void **state)
{
free(*state);
return 0;
}
static void prestate_setup_test(void **state)
{
int *a = (int *)*state;
assert_non_null(a);
assert_int_equal(*a, 43);
}
static void prestate_test(void **state)
{
int *a = (int *)*state;
assert_non_null(a);
assert_int_equal(*a, 42);
}
int main(void) {
int prestate = 42;
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup(malloc_setup_test, setup_only),
cmocka_unit_test_setup(malloc_setup_test, setup_only),
cmocka_unit_test_teardown(malloc_teardown_test, teardown_only),
cmocka_unit_test_teardown(malloc_teardown_test, teardown_only),
cmocka_unit_test_teardown(malloc_teardown_test, teardown_only),
cmocka_unit_test_teardown(malloc_teardown_test, teardown_only),
cmocka_unit_test_prestate(prestate_test, &prestate),
cmocka_unit_test_prestate_setup_teardown(prestate_setup_test, prestate_setup, prestate_teardown, &prestate),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
|