]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-cpu-set-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
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.
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.
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/>.
21 #include "alloc-util.h"
22 #include "cpu-set-util.h"
25 static void test_parse_cpu_set(void) {
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);
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
));
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
));
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
));
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
));
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
));
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
));
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);
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
= cpu_set_mfree(c
);
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
= cpu_set_mfree(c
);
119 ncpus
= parse_cpu_set_and_warn("0 1 2 3 garbage", &c
, NULL
, "fake", 1, "CPUAffinity");
120 assert_se(ncpus
< 0);
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);
130 ncpus
= parse_cpu_set_and_warn("", &c
, NULL
, "fake", 1, "CPUAffinity");
131 assert_se(ncpus
== 0); /* empty string returns 0 */
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);
140 int main(int argc
, char *argv
[]) {
141 test_parse_cpu_set();