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