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