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