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