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