Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "gate/tests.h" | ||
2 | #include "test_runnables.h" | ||
3 | #include "gate/runnables.h" | ||
4 | |||
5 | 1 | static gate_result_t basic_runner(gate_dataptr_t arg) | |
6 | { | ||
7 | 1 | int* ptr_data = (int*)arg; | |
8 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK(ptr_data != NULL); |
9 | 1 | *ptr_data = 42; | |
10 | 1 | return GATE_RESULT_OK; | |
11 | } | ||
12 | |||
13 | 1 | GATE_TEST_FUNCTION(test_basic_runnable) | |
14 | { | ||
15 | 1 | int data = 1; | |
16 | 1 | gate_runnable_t* ptr_runner = NULL; | |
17 | |||
18 | 1 | GATE_TEST_UNIT_BEGIN(test_basic_runnable); | |
19 | |||
20 | 1 | ptr_runner = gate_runnable_create(&basic_runner, &data, 0, NULL, NULL); | |
21 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_REQUIRE(ptr_runner != NULL); |
22 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_OK(gate_runnable_run(ptr_runner)); |
23 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(data, 42); |
24 | 1 | gate_object_release(ptr_runner); | |
25 | |||
26 | 1 | GATE_TEST_UNIT_END; | |
27 | } | ||
28 | |||
29 | |||
30 | |||
31 | |||
32 | 2 | static gate_result_t my_runnable_sample(int a, gate_int64_t b, gate_int64_t* c) | |
33 | { | ||
34 | 2 | *c = b * a; | |
35 | 2 | return GATE_RESULT_OK; | |
36 | } | ||
37 | |||
38 | 1 | GATE_RUNNABLE_DISPATCHER_DECLARE_3(runnable_sample_dispatcher, int, gate_int64_t, gate_int64_t*); | |
39 | |||
40 | |||
41 | 1 | GATE_TEST_FUNCTION(test_runnable_dispatcher_types) | |
42 | { | ||
43 | 1 | int arg_a = 1000; | |
44 | 1 | gate_int64_t arg_b = 2000; | |
45 | 1 | gate_int64_t var_c = 0; | |
46 | 1 | gate_int64_t* arg_c = &var_c; | |
47 | 1 | gate_int64_t expected_var_c = 0; | |
48 | 1 | gate_runnable_t* ptr_runnable = NULL; | |
49 | |||
50 | 1 | GATE_TEST_UNIT_BEGIN(test_runnable_dispatcher_types); | |
51 | |||
52 | 1 | ptr_runnable = GATE_RUNNABLE_DISPATCHER_CREATE_3(runnable_sample_dispatcher, &my_runnable_sample, arg_a, arg_b, arg_c); | |
53 | |||
54 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK(ptr_runnable != NULL); |
55 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_OK(gate_runnable_run(ptr_runnable)); |
56 | 1 | gate_object_release(ptr_runnable); | |
57 | |||
58 | 1 | my_runnable_sample(arg_a, arg_b, &expected_var_c); | |
59 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK(var_c == expected_var_c); |
60 | |||
61 | |||
62 | 1 | GATE_TEST_UNIT_END; | |
63 | } | ||
64 | |||
65 | |||
66 | |||
67 | |||
68 | 2 | static gate_result_t sum_function(int* ptr_c, int a, int b) | |
69 | { | ||
70 | 2 | *ptr_c = a + b; | |
71 | 2 | return GATE_RESULT_OK; | |
72 | } | ||
73 | |||
74 | 1 | GATE_RUNNABLE_DISPATCHER_DECLARE_3(sum_function, int*, int, int); | |
75 | |||
76 | 1 | GATE_TEST_FUNCTION(test_runnable_dispatcher) | |
77 | { | ||
78 | 1 | int a = 12; | |
79 | 1 | int b = 5; | |
80 | 1 | int c = 0; | |
81 | 1 | int* ptr_c = &c; | |
82 | 1 | gate_runnable_t* ptr_runnable = NULL; | |
83 | gate_result_t result; | ||
84 | |||
85 | 1 | GATE_TEST_UNIT_BEGIN(test_runnable_dispatcher); | |
86 | |||
87 | 1 | ptr_runnable = GATE_RUNNABLE_DISPATCHER_CREATE_3(sum_function, &sum_function, ptr_c, a, b); | |
88 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_REQUIRE(ptr_runnable); |
89 | |||
90 | 1 | result = gate_runnable_run(ptr_runnable); | |
91 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_REQUIRE_OK(result); |
92 | |||
93 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(ptr_c, &c); |
94 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(c, (a + b)); |
95 | |||
96 | 1 | gate_object_release(ptr_runnable); | |
97 | |||
98 | 1 | GATE_TEST_UNIT_END; | |
99 | } | ||
100 | |||
101 | |||
102 | |||
103 | 1 | GATE_TASK_DISPATCHER_DECLARE_2(sum_task, int, int, int); | |
104 | |||
105 | 1 | GATE_TEST_FUNCTION(test_task_dispatcher) | |
106 | { | ||
107 | 1 | int a = 12; | |
108 | 1 | int b = 5; | |
109 | 1 | int const* ptr_c = NULL; | |
110 | 1 | gate_task_t* ptr_task = NULL; | |
111 | gate_result_t result; | ||
112 | 1 | gate_result_t task_result = GATE_RESULT_NOTDEFINED; | |
113 | |||
114 | 1 | GATE_TEST_UNIT_BEGIN(test_task_dispatcher); | |
115 | |||
116 | { | ||
117 | /* canceled task */ | ||
118 | 1 | ptr_task = GATE_TASK_DISPATCHER_CREATE_2(sum_task, &sum_function, int, a, b); | |
119 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_REQUIRE(ptr_task); |
120 | |||
121 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_task_get_status(ptr_task), GATE_TASK_STATUS_PREPARED); |
122 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_task_completed(ptr_task), false); |
123 | |||
124 | 1 | result = gate_task_timed_wait(ptr_task, 1); | |
125 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK_RESULT(result, GATE_RESULT_TIMEOUT); |
126 | |||
127 | 1 | result = gate_task_cancel(ptr_task); | |
128 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_REQUIRE_OK(result); |
129 | |||
130 | 1 | result = gate_task_timed_wait(ptr_task, 1); | |
131 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK_RESULT(result, GATE_RESULT_OK); |
132 | |||
133 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_task_get_status(ptr_task), GATE_TASK_STATUS_CANCELED); |
134 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_task_completed(ptr_task), true); |
135 | 1 | result = gate_task_get_result(ptr_task, &task_result, (void const**)&ptr_c); | |
136 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK_RESULT(result, GATE_RESULT_OK); |
137 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK_RESULT(task_result, GATE_RESULT_CANCELED); |
138 | |||
139 | 1 | gate_object_release(ptr_task); | |
140 | } | ||
141 | |||
142 | { | ||
143 | /* completed task */ | ||
144 | 1 | ptr_task = GATE_TASK_DISPATCHER_CREATE_2(sum_task, &sum_function, int, a, b); | |
145 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_REQUIRE(ptr_task); |
146 | |||
147 | 1 | result = gate_task_run(ptr_task); | |
148 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK_OK(result); |
149 | |||
150 | 1 | result = gate_task_timed_wait(ptr_task, 1); | |
151 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK_RESULT(result, GATE_RESULT_OK); |
152 | |||
153 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_task_get_status(ptr_task), GATE_TASK_STATUS_COMPLETED); |
154 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_task_completed(ptr_task), true); |
155 | 1 | result = gate_task_get_result(ptr_task, &task_result, (void const**)&ptr_c); | |
156 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK_OK(result); |
157 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK_OK(task_result); |
158 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_REQUIRE(ptr_c); |
159 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(*ptr_c, (a + b)); |
160 | |||
161 | 1 | gate_object_release(ptr_task); | |
162 | } | ||
163 | |||
164 | 1 | GATE_TEST_UNIT_END; | |
165 | } | ||
166 | |||
167 | 1 | GATE_TEST_FUNCTION(test_runnables) | |
168 | { | ||
169 | 1 | gate_bool_t succeeded = true; | |
170 | |||
171 | 1 | succeeded &= test_basic_runnable(); | |
172 | 1 | succeeded &= test_runnable_dispatcher(); | |
173 | 1 | succeeded &= test_runnable_dispatcher_types(); | |
174 | 1 | succeeded &= test_task_dispatcher(); | |
175 | |||
176 | 1 | return succeeded; | |
177 | } | ||
178 |