]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a7334b09 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
8 | under the terms of the GNU Lesser General Public License as published by |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
10 | (at your option) any later version. |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 15 | Lesser General Public License for more details. |
a7334b09 | 16 | |
5430f7f2 | 17 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
19 | ***/ | |
20 | ||
1ffba6fe | 21 | #include <stdio.h> |
1ffba6fe LP |
22 | |
23 | #include "job.h" | |
e0209d83 | 24 | #include "service.h" |
cf0fbc49 | 25 | #include "unit.h" |
1ffba6fe LP |
26 | |
27 | int main(int argc, char*argv[]) { | |
e0209d83 MS |
28 | JobType a, b, c, ab, bc, ab_c, bc_a, a_bc; |
29 | const ServiceState test_states[] = { SERVICE_DEAD, SERVICE_RUNNING }; | |
30 | unsigned i; | |
31 | bool merged_ab; | |
32 | ||
33 | /* fake a unit */ | |
34 | static Service s = { | |
35 | .meta.load_state = UNIT_LOADED, | |
335c8d5a | 36 | .type = SERVICE_SIMPLE, |
e0209d83 MS |
37 | }; |
38 | Unit *u = UNIT(&s); | |
39 | ||
40 | for (i = 0; i < ELEMENTSOF(test_states); i++) { | |
41 | s.state = test_states[i]; | |
42 | printf("\nWith collapsing for service state %s\n" | |
43 | "=========================================\n", service_state_to_string(s.state)); | |
44 | for (a = 0; a < _JOB_TYPE_MAX_MERGING; a++) { | |
45 | for (b = 0; b < _JOB_TYPE_MAX_MERGING; b++) { | |
46 | ||
47 | ab = a; | |
48 | merged_ab = (job_type_merge_and_collapse(&ab, b, u) >= 0); | |
49 | ||
50 | if (!job_type_is_mergeable(a, b)) { | |
bdf7026e | 51 | assert_se(!merged_ab); |
e0209d83 MS |
52 | printf("Not mergeable: %s + %s\n", job_type_to_string(a), job_type_to_string(b)); |
53 | continue; | |
54 | } | |
1ffba6fe | 55 | |
bdf7026e | 56 | assert_se(merged_ab); |
e0209d83 | 57 | printf("%s + %s = %s\n", job_type_to_string(a), job_type_to_string(b), job_type_to_string(ab)); |
1ffba6fe | 58 | |
e0209d83 | 59 | for (c = 0; c < _JOB_TYPE_MAX_MERGING; c++) { |
1ffba6fe | 60 | |
e0209d83 | 61 | /* Verify transitivity of mergeability of job types */ |
bdf7026e | 62 | assert_se(!job_type_is_mergeable(a, b) || |
e0209d83 MS |
63 | !job_type_is_mergeable(b, c) || |
64 | job_type_is_mergeable(a, c)); | |
1ffba6fe | 65 | |
e0209d83 MS |
66 | /* Verify that merged entries can be merged with the same entries |
67 | * they can be merged with separately */ | |
bdf7026e TA |
68 | assert_se(!job_type_is_mergeable(a, c) || job_type_is_mergeable(ab, c)); |
69 | assert_se(!job_type_is_mergeable(b, c) || job_type_is_mergeable(ab, c)); | |
1ffba6fe | 70 | |
e0209d83 MS |
71 | /* Verify that if a merged with b is not mergeable with c, then |
72 | * either a or b is not mergeable with c either. */ | |
bdf7026e | 73 | assert_se(job_type_is_mergeable(ab, c) || !job_type_is_mergeable(a, c) || !job_type_is_mergeable(b, c)); |
1ffba6fe | 74 | |
e0209d83 MS |
75 | bc = b; |
76 | if (job_type_merge_and_collapse(&bc, c, u) >= 0) { | |
1ffba6fe LP |
77 | |
78 | /* Verify associativity */ | |
79 | ||
e0209d83 | 80 | ab_c = ab; |
bdf7026e | 81 | assert_se(job_type_merge_and_collapse(&ab_c, c, u) == 0); |
e0209d83 MS |
82 | |
83 | bc_a = bc; | |
bdf7026e | 84 | assert_se(job_type_merge_and_collapse(&bc_a, a, u) == 0); |
1ffba6fe | 85 | |
e0209d83 | 86 | a_bc = a; |
bdf7026e | 87 | assert_se(job_type_merge_and_collapse(&a_bc, bc, u) == 0); |
1ffba6fe | 88 | |
bdf7026e TA |
89 | assert_se(ab_c == bc_a); |
90 | assert_se(ab_c == a_bc); | |
1ffba6fe | 91 | |
e0209d83 | 92 | 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 |
93 | } |
94 | } | |
95 | } | |
96 | } | |
e0209d83 | 97 | } |
1ffba6fe LP |
98 | |
99 | ||
100 | return 0; | |
101 | } |