Debian Codegraph @

src/test/test-job-type.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "service.h"
6 #include "tests.h"
7 #include "unit.h"
8
9 int main(int argc, char *argv[]) {
10 const ServiceState test_states[] = { SERVICE_DEAD, SERVICE_RUNNING };
11
12 test_setup_logging(LOG_DEBUG);
13
14 FOREACH_ELEMENT(state, test_states) {
15 /* fake a unit */
16 Service s = {
17 .meta.load_state = UNIT_LOADED,
18 .type = SERVICE_SIMPLE,
19 .state = *state,
20 };
21 Unit *u = UNIT(&s);
22
23 printf("\nWith collapsing for service state %s\n"
24 "=========================================\n", service_state_to_string(s.state));
25 for (JobType a = 0; a < _JOB_TYPE_MAX_MERGING; a++) {
26 for (JobType b = 0; b < _JOB_TYPE_MAX_MERGING; b++) {
27
28 JobType ab = a;
29 bool merged_ab = job_type_merge_and_collapse(&ab, b, u) >= 0;
30
31 if (!job_type_is_mergeable(a, b)) {
32 assert_se(!merged_ab);
33 printf("Not mergeable: %s + %s\n", job_type_to_string(a), job_type_to_string(b));
34 continue;
35 }
36
37 assert_se(merged_ab);
38 printf("%s + %s = %s\n", job_type_to_string(a), job_type_to_string(b), job_type_to_string(ab));
39
40 for (JobType c = 0; c < _JOB_TYPE_MAX_MERGING; c++) {
41
42 /* Verify transitivity of mergeability of job types */
43 assert_se(!job_type_is_mergeable(a, b) ||
44 !job_type_is_mergeable(b, c) ||
45 job_type_is_mergeable(a, c));
46
47 /* Verify that merged entries can be merged with the same entries
48 * they can be merged with separately */
49 assert_se(!job_type_is_mergeable(a, c) || job_type_is_mergeable(ab, c));
50 assert_se(!job_type_is_mergeable(b, c) || job_type_is_mergeable(ab, c));
51
52 /* Verify that if a merged with b is not mergeable with c, then
53 * either a or b is not mergeable with c either. */
54 assert_se(job_type_is_mergeable(ab, c) || !job_type_is_mergeable(a, c) || !job_type_is_mergeable(b, c));
55
56 JobType bc = b;
57 if (job_type_merge_and_collapse(&bc, c, u) >= 0) {
58
59 /* Verify associativity */
60
61 JobType ab_c = ab;
62 assert_se(job_type_merge_and_collapse(&ab_c, c, u) == 0);
63
64 JobType bc_a = bc;
65 assert_se(job_type_merge_and_collapse(&bc_a, a, u) == 0);
66
67 JobType a_bc = a;
68 assert_se(job_type_merge_and_collapse(&a_bc, bc, u) == 0);
69
70 assert_se(ab_c == bc_a);
71 assert_se(ab_c == a_bc);
72
73 printf("%s + %s + %s = %s\n", job_type_to_string(a), job_type_to_string(b), job_type_to_string(c), job_type_to_string(ab_c));
74 }
75 }
76 }
77 }
78 }
79
80 return 0;
81 }