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