]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-cpu-set-util.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-cpu-set-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
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
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
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include "alloc-util.h"
22 #include "cpu-set-util.h"
23 #include "macro.h"
24
25 static void test_parse_cpu_set(void) {
26 cpu_set_t *c = NULL;
27 int ncpus;
28 int cpu;
29
30 /* Simple range (from CPUAffinity example) */
31 ncpus = parse_cpu_set_and_warn("1 2", &c, NULL, "fake", 1, "CPUAffinity");
32 assert_se(ncpus >= 1024);
33 assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
34 assert_se(CPU_ISSET_S(2, CPU_ALLOC_SIZE(ncpus), c));
35 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 2);
36 c = mfree(c);
37
38 /* A more interesting range */
39 ncpus = parse_cpu_set_and_warn("0 1 2 3 8 9 10 11", &c, NULL, "fake", 1, "CPUAffinity");
40 assert_se(ncpus >= 1024);
41 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
42 for (cpu = 0; cpu < 4; cpu++)
43 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
44 for (cpu = 8; cpu < 12; cpu++)
45 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
46 c = mfree(c);
47
48 /* Quoted strings */
49 ncpus = parse_cpu_set_and_warn("8 '9' 10 \"11\"", &c, NULL, "fake", 1, "CPUAffinity");
50 assert_se(ncpus >= 1024);
51 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 4);
52 for (cpu = 8; cpu < 12; cpu++)
53 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
54 c = mfree(c);
55
56 /* Use commas as separators */
57 ncpus = parse_cpu_set_and_warn("0,1,2,3 8,9,10,11", &c, NULL, "fake", 1, "CPUAffinity");
58 assert_se(ncpus >= 1024);
59 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
60 for (cpu = 0; cpu < 4; cpu++)
61 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
62 for (cpu = 8; cpu < 12; cpu++)
63 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
64 c = mfree(c);
65
66 /* Commas with spaces (and trailing comma, space) */
67 ncpus = parse_cpu_set_and_warn("0, 1, 2, 3, 4, 5, 6, 7, ", &c, NULL, "fake", 1, "CPUAffinity");
68 assert_se(ncpus >= 1024);
69 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
70 for (cpu = 0; cpu < 8; cpu++)
71 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
72 c = mfree(c);
73
74 /* Ranges */
75 ncpus = parse_cpu_set_and_warn("0-3,8-11", &c, NULL, "fake", 1, "CPUAffinity");
76 assert_se(ncpus >= 1024);
77 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
78 for (cpu = 0; cpu < 4; cpu++)
79 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
80 for (cpu = 8; cpu < 12; cpu++)
81 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
82 c = mfree(c);
83
84 /* Ranges with trailing comma, space */
85 ncpus = parse_cpu_set_and_warn("0-3 8-11, ", &c, NULL, "fake", 1, "CPUAffinity");
86 assert_se(ncpus >= 1024);
87 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
88 for (cpu = 0; cpu < 4; cpu++)
89 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
90 for (cpu = 8; cpu < 12; cpu++)
91 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
92 c = mfree(c);
93
94 /* Negative range (returns empty cpu_set) */
95 ncpus = parse_cpu_set_and_warn("3-0", &c, NULL, "fake", 1, "CPUAffinity");
96 assert_se(ncpus >= 1024);
97 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 0);
98 c = mfree(c);
99
100 /* Overlapping ranges */
101 ncpus = parse_cpu_set_and_warn("0-7 4-11", &c, NULL, "fake", 1, "CPUAffinity");
102 assert_se(ncpus >= 1024);
103 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 12);
104 for (cpu = 0; cpu < 12; cpu++)
105 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
106 c = mfree(c);
107
108 /* Mix ranges and individual CPUs */
109 ncpus = parse_cpu_set_and_warn("0,1 4-11", &c, NULL, "fake", 1, "CPUAffinity");
110 assert_se(ncpus >= 1024);
111 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 10);
112 assert_se(CPU_ISSET_S(0, CPU_ALLOC_SIZE(ncpus), c));
113 assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
114 for (cpu = 4; cpu < 12; cpu++)
115 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
116 c = mfree(c);
117
118 /* Garbage */
119 ncpus = parse_cpu_set_and_warn("0 1 2 3 garbage", &c, NULL, "fake", 1, "CPUAffinity");
120 assert_se(ncpus < 0);
121 assert_se(!c);
122
123 /* Range with garbage */
124 ncpus = parse_cpu_set_and_warn("0-3 8-garbage", &c, NULL, "fake", 1, "CPUAffinity");
125 assert_se(ncpus < 0);
126 assert_se(!c);
127
128 /* Empty string */
129 c = NULL;
130 ncpus = parse_cpu_set_and_warn("", &c, NULL, "fake", 1, "CPUAffinity");
131 assert_se(ncpus == 0); /* empty string returns 0 */
132 assert_se(!c);
133
134 /* Runaway quoted string */
135 ncpus = parse_cpu_set_and_warn("0 1 2 3 \"4 5 6 7 ", &c, NULL, "fake", 1, "CPUAffinity");
136 assert_se(ncpus < 0);
137 assert_se(!c);
138 }
139
140 int main(int argc, char *argv[]) {
141 test_parse_cpu_set();
142
143 return 0;
144 }