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