]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-cpu-set-util.c
2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
21 #include "cpu-set-util.h"
24 static void test_parse_cpu_set(void) {
29 /* Simple range (from CPUAffinity example) */
30 ncpus
= parse_cpu_set_and_warn("1 2", &c
, NULL
, "fake", 1, "CPUAffinity");
31 assert_se(ncpus
>= 1024);
32 assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus
), c
));
33 assert_se(CPU_ISSET_S(2, CPU_ALLOC_SIZE(ncpus
), c
));
34 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus
), c
) == 2);
37 /* A more interesting range */
38 ncpus
= parse_cpu_set_and_warn("0 1 2 3 8 9 10 11", &c
, NULL
, "fake", 1, "CPUAffinity");
39 assert_se(ncpus
>= 1024);
40 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus
), c
) == 8);
41 for (cpu
= 0; cpu
< 4; cpu
++)
42 assert_se(CPU_ISSET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
));
43 for (cpu
= 8; cpu
< 12; cpu
++)
44 assert_se(CPU_ISSET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
));
48 ncpus
= parse_cpu_set_and_warn("8 '9' 10 \"11\"", &c
, NULL
, "fake", 1, "CPUAffinity");
49 assert_se(ncpus
>= 1024);
50 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus
), c
) == 4);
51 for (cpu
= 8; cpu
< 12; cpu
++)
52 assert_se(CPU_ISSET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
));
55 /* Use commas as separators */
56 ncpus
= parse_cpu_set_and_warn("0,1,2,3 8,9,10,11", &c
, NULL
, "fake", 1, "CPUAffinity");
57 assert_se(ncpus
>= 1024);
58 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus
), c
) == 8);
59 for (cpu
= 0; cpu
< 4; cpu
++)
60 assert_se(CPU_ISSET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
));
61 for (cpu
= 8; cpu
< 12; cpu
++)
62 assert_se(CPU_ISSET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
));
65 /* Commas with spaces (and trailing comma, space) */
66 ncpus
= parse_cpu_set_and_warn("0, 1, 2, 3, 4, 5, 6, 7, ", &c
, NULL
, "fake", 1, "CPUAffinity");
67 assert_se(ncpus
>= 1024);
68 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus
), c
) == 8);
69 for (cpu
= 0; cpu
< 8; cpu
++)
70 assert_se(CPU_ISSET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
));
74 ncpus
= parse_cpu_set_and_warn("0-3,8-11", &c
, NULL
, "fake", 1, "CPUAffinity");
75 assert_se(ncpus
>= 1024);
76 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus
), c
) == 8);
77 for (cpu
= 0; cpu
< 4; cpu
++)
78 assert_se(CPU_ISSET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
));
79 for (cpu
= 8; cpu
< 12; cpu
++)
80 assert_se(CPU_ISSET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
));
83 /* Ranges with trailing comma, space */
84 ncpus
= parse_cpu_set_and_warn("0-3 8-11, ", &c
, NULL
, "fake", 1, "CPUAffinity");
85 assert_se(ncpus
>= 1024);
86 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus
), c
) == 8);
87 for (cpu
= 0; cpu
< 4; cpu
++)
88 assert_se(CPU_ISSET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
));
89 for (cpu
= 8; cpu
< 12; cpu
++)
90 assert_se(CPU_ISSET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
));
93 /* Negative range (returns empty cpu_set) */
94 ncpus
= parse_cpu_set_and_warn("3-0", &c
, NULL
, "fake", 1, "CPUAffinity");
95 assert_se(ncpus
>= 1024);
96 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus
), c
) == 0);
99 /* Overlapping ranges */
100 ncpus
= parse_cpu_set_and_warn("0-7 4-11", &c
, NULL
, "fake", 1, "CPUAffinity");
101 assert_se(ncpus
>= 1024);
102 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus
), c
) == 12);
103 for (cpu
= 0; cpu
< 12; cpu
++)
104 assert_se(CPU_ISSET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
));
107 /* Mix ranges and individual CPUs */
108 ncpus
= parse_cpu_set_and_warn("0,1 4-11", &c
, NULL
, "fake", 1, "CPUAffinity");
109 assert_se(ncpus
>= 1024);
110 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus
), c
) == 10);
111 assert_se(CPU_ISSET_S(0, CPU_ALLOC_SIZE(ncpus
), c
));
112 assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus
), c
));
113 for (cpu
= 4; cpu
< 12; cpu
++)
114 assert_se(CPU_ISSET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
));
118 ncpus
= parse_cpu_set_and_warn("0 1 2 3 garbage", &c
, NULL
, "fake", 1, "CPUAffinity");
119 assert_se(ncpus
< 0);
122 /* Range with garbage */
123 ncpus
= parse_cpu_set_and_warn("0-3 8-garbage", &c
, NULL
, "fake", 1, "CPUAffinity");
124 assert_se(ncpus
< 0);
129 ncpus
= parse_cpu_set_and_warn("", &c
, NULL
, "fake", 1, "CPUAffinity");
130 assert_se(ncpus
== 0); /* empty string returns 0 */
133 /* Runaway quoted string */
134 ncpus
= parse_cpu_set_and_warn("0 1 2 3 \"4 5 6 7 ", &c
, NULL
, "fake", 1, "CPUAffinity");
135 assert_se(ncpus
< 0);
139 int main(int argc
, char *argv
[]) {
140 test_parse_cpu_set();