]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-job-type.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / test / test-job-type.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
a7334b09
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
a7334b09
LP
6***/
7
1ffba6fe 8#include <stdio.h>
1ffba6fe
LP
9
10#include "job.h"
e0209d83 11#include "service.h"
cf0fbc49 12#include "unit.h"
1ffba6fe
LP
13
14int main(int argc, char*argv[]) {
e0209d83
MS
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,
335c8d5a 23 .type = SERVICE_SIMPLE,
e0209d83
MS
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)) {
bdf7026e 38 assert_se(!merged_ab);
e0209d83
MS
39 printf("Not mergeable: %s + %s\n", job_type_to_string(a), job_type_to_string(b));
40 continue;
41 }
1ffba6fe 42
bdf7026e 43 assert_se(merged_ab);
e0209d83 44 printf("%s + %s = %s\n", job_type_to_string(a), job_type_to_string(b), job_type_to_string(ab));
1ffba6fe 45
e0209d83 46 for (c = 0; c < _JOB_TYPE_MAX_MERGING; c++) {
1ffba6fe 47
e0209d83 48 /* Verify transitivity of mergeability of job types */
bdf7026e 49 assert_se(!job_type_is_mergeable(a, b) ||
e0209d83
MS
50 !job_type_is_mergeable(b, c) ||
51 job_type_is_mergeable(a, c));
1ffba6fe 52
e0209d83
MS
53 /* Verify that merged entries can be merged with the same entries
54 * they can be merged with separately */
bdf7026e
TA
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));
1ffba6fe 57
e0209d83
MS
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. */
bdf7026e 60 assert_se(job_type_is_mergeable(ab, c) || !job_type_is_mergeable(a, c) || !job_type_is_mergeable(b, c));
1ffba6fe 61
e0209d83
MS
62 bc = b;
63 if (job_type_merge_and_collapse(&bc, c, u) >= 0) {
1ffba6fe
LP
64
65 /* Verify associativity */
66
e0209d83 67 ab_c = ab;
bdf7026e 68 assert_se(job_type_merge_and_collapse(&ab_c, c, u) == 0);
e0209d83
MS
69
70 bc_a = bc;
bdf7026e 71 assert_se(job_type_merge_and_collapse(&bc_a, a, u) == 0);
1ffba6fe 72
e0209d83 73 a_bc = a;
bdf7026e 74 assert_se(job_type_merge_and_collapse(&a_bc, bc, u) == 0);
1ffba6fe 75
bdf7026e
TA
76 assert_se(ab_c == bc_a);
77 assert_se(ab_c == a_bc);
1ffba6fe 78
e0209d83 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));
1ffba6fe
LP
80 }
81 }
82 }
83 }
e0209d83 84 }
1ffba6fe
LP
85
86
87 return 0;
88}