]>
Commit | Line | Data |
---|---|---|
7ba365a9 RC |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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/>. | |
18 | ***/ | |
19 | ||
20 | #include "alloc-util.h" | |
21 | #include "cpu-set-util.h" | |
22 | #include "macro.h" | |
23 | ||
24 | static void test_parse_cpu_set(void) { | |
25 | cpu_set_t *c = NULL; | |
26 | int ncpus; | |
27 | int cpu; | |
28 | ||
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); | |
35 | c = mfree(c); | |
36 | ||
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)); | |
45 | c = mfree(c); | |
46 | ||
47 | /* Quoted strings */ | |
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)); | |
53 | c = mfree(c); | |
54 | ||
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)); | |
63 | c = mfree(c); | |
64 | ||
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)); | |
71 | c = mfree(c); | |
72 | ||
73 | /* Ranges */ | |
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)); | |
81 | c = mfree(c); | |
82 | ||
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)); | |
91 | c = mfree(c); | |
92 | ||
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); | |
97 | c = mfree(c); | |
98 | ||
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)); | |
105 | c = mfree(c); | |
106 | ||
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)); | |
115 | c = mfree(c); | |
116 | ||
117 | /* Garbage */ | |
118 | ncpus = parse_cpu_set_and_warn("0 1 2 3 garbage", &c, NULL, "fake", 1, "CPUAffinity"); | |
119 | assert_se(ncpus < 0); | |
120 | assert_se(!c); | |
121 | ||
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); | |
125 | assert_se(!c); | |
126 | ||
127 | /* Empty string */ | |
128 | c = NULL; | |
129 | ncpus = parse_cpu_set_and_warn("", &c, NULL, "fake", 1, "CPUAffinity"); | |
130 | assert_se(ncpus == 0); /* empty string returns 0 */ | |
131 | assert_se(!c); | |
132 | ||
133 | /* Runnaway 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); | |
136 | assert_se(!c); | |
137 | } | |
138 | ||
139 | int main(int argc, char *argv[]) { | |
140 | test_parse_cpu_set(); | |
141 | ||
142 | return 0; | |
143 | } |