JayBeams  0.1
Another project to have fun coding.
ut_cpu_set.cpp
Go to the documentation of this file.
1 #include <jb/convert_cpu_set.hpp>
2 #include <jb/cpu_set.hpp>
3 
4 #include <boost/test/unit_test.hpp>
5 
6 #include <sstream>
7 
8 /**
9  * @test Verify that basic operations on jb::cpu_set work as expected.
10  */
11 BOOST_AUTO_TEST_CASE(cpu_set_basic) {
12  jb::cpu_set a;
13  BOOST_REQUIRE_GT(a.capacity(), 0);
14 
15  BOOST_TEST_MESSAGE("cpu_set capacity is " << a.capacity());
16 
17  a.set(1);
18  a.set(3);
19  BOOST_CHECK_EQUAL(a.count(), 2);
20  BOOST_CHECK_EQUAL(a.status(0), false);
21  BOOST_CHECK_EQUAL(a.status(1), true);
22  BOOST_CHECK_EQUAL(a.status(3), true);
23 
24  jb::cpu_set b(a);
25  BOOST_CHECK_EQUAL(b.count(), 2);
26  BOOST_CHECK_EQUAL(b.status(0), false);
27  BOOST_CHECK_EQUAL(b.status(1), true);
28  BOOST_CHECK_EQUAL(b.status(3), true);
29 
30  b.clear(3);
31  BOOST_CHECK_EQUAL(b.status(3), false);
32  BOOST_CHECK_EQUAL(a.count(), 2);
33  BOOST_CHECK_EQUAL(b.count(), 1);
34 
35  jb::cpu_set c = std::move(a);
36  BOOST_CHECK_EQUAL(c.count(), 2);
37  BOOST_CHECK_EQUAL(c.status(0), false);
38  BOOST_CHECK_EQUAL(c.status(1), true);
39  BOOST_CHECK_EQUAL(c.status(3), true);
40 
41  a = std::move(b);
42  BOOST_CHECK_EQUAL(a.count(), 1);
43  BOOST_CHECK_EQUAL(a.status(0), false);
44  BOOST_CHECK_EQUAL(a.status(1), true);
45  BOOST_CHECK_EQUAL(a.status(3), false);
46 
47  b = c;
48  BOOST_CHECK_EQUAL(b.count(), 2);
49  BOOST_CHECK_EQUAL(b.status(0), false);
50  BOOST_CHECK_EQUAL(b.status(1), true);
51  BOOST_CHECK_EQUAL(b.status(3), true);
52 
53  a.set(1, 5);
54  BOOST_CHECK_EQUAL(a.count(), 5);
55  for (int i = 1; i != 6; ++i) {
56  BOOST_CHECK_MESSAGE(a.status(i) == true, "b.status(i) is false i=" << i);
57  }
58 
59  BOOST_CHECK_THROW(b.set(b.capacity()), std::exception);
60  BOOST_CHECK_THROW(b.set(b.capacity() + 1), std::exception);
61  BOOST_CHECK_THROW(b.set(-1), std::exception);
62  BOOST_CHECK_THROW(b.set(0, b.capacity()), std::exception);
63  BOOST_CHECK_NO_THROW(b.set(0, b.capacity() - 1));
64 
65  a.reset().set(1).set(2).set(3);
66  b = a;
67  BOOST_CHECK_EQUAL(a, b);
68  b.set(10);
69  BOOST_CHECK_NE(a, b);
70 
71  a.reset();
72  BOOST_CHECK_EQUAL(a.count(), 0);
73 
74  b.reset().set(2).set(3);
75  c.reset().set(0).set(3);
76 
77  a = b | c;
78  BOOST_CHECK_EQUAL(a.status(0), true);
79  BOOST_CHECK_EQUAL(a.status(1), false);
80  BOOST_CHECK_EQUAL(a.status(2), true);
81  BOOST_CHECK_EQUAL(a.status(3), true);
82 
83  a = b & c;
84  BOOST_CHECK_EQUAL(a.status(0), false);
85  BOOST_CHECK_EQUAL(a.status(1), false);
86  BOOST_CHECK_EQUAL(a.status(2), false);
87  BOOST_CHECK_EQUAL(a.status(3), true);
88 
89  a = b ^ c;
90  BOOST_CHECK_EQUAL(a.status(0), true);
91  BOOST_CHECK_EQUAL(a.status(1), false);
92  BOOST_CHECK_EQUAL(a.status(2), true);
93  BOOST_CHECK_EQUAL(a.status(3), false);
94 }
95 
96 /**
97  * @test Verify that jb::cpu_set output streaming works as expected.
98  */
99 BOOST_AUTO_TEST_CASE(cpu_set_ostream) {
100  {
101  jb::cpu_set a;
102  std::ostringstream os;
103  os << a;
104  BOOST_CHECK_EQUAL(os.str(), std::string(""));
105  }
106 
107  {
108  jb::cpu_set a;
109  a.set(1);
110  std::ostringstream os;
111  os << a;
112  BOOST_CHECK_EQUAL(os.str(), std::string("1"));
113  }
114 
115  {
116  jb::cpu_set a;
117  a.set(1, 5);
118  std::ostringstream os;
119  os << a;
120  BOOST_CHECK_EQUAL(os.str(), std::string("1-5"));
121  }
122 
123  {
124  jb::cpu_set a;
125  a.set(1, 5);
126  a.set(7);
127  std::ostringstream os;
128  os << a;
129  BOOST_CHECK_EQUAL(os.str(), std::string("1-5,7"));
130  }
131 
132  {
133  jb::cpu_set a;
134  a.set(10, 200);
135  a.set(7);
136  a.set(11);
137  a.set(1, 5);
138  a.set(300);
139  a.set(301);
140  std::ostringstream os;
141  os << a;
142  BOOST_CHECK_EQUAL(os.str(), std::string("1-5,7,10-200,300-301"));
143  }
144 }
145 
146 /**
147  * @test Verify that jb::cpu_set input streaming works as expected.
148  */
149 BOOST_AUTO_TEST_CASE(cpu_set_istream) {
150  {
151  std::istringstream is("");
152  jb::cpu_set a;
153  is >> a;
154  BOOST_CHECK_EQUAL(a.count(), 0);
155  }
156 
157  {
158  std::istringstream is("1");
159  jb::cpu_set a;
160  is >> a;
161  BOOST_CHECK_EQUAL(a.count(), 1);
162  BOOST_CHECK_EQUAL(a.status(1), true);
163  }
164 
165  {
166  jb::cpu_set a;
167  std::istringstream is("1-5");
168  is >> a;
169  BOOST_CHECK_EQUAL(a.count(), 5);
170  for (int i = 1; i != 6; ++i) {
171  BOOST_CHECK_MESSAGE(a.status(i), "a.status(i) not true for i=" << i);
172  }
173  }
174 
175  {
176  jb::cpu_set a;
177  std::istringstream is("1-5,7");
178  is >> a;
179  BOOST_CHECK_EQUAL(a.count(), 6);
180  for (int i = 1; i != 6; ++i) {
181  BOOST_CHECK_MESSAGE(a.status(i), "a.status(i) not true for i=" << i);
182  }
183  BOOST_CHECK_EQUAL(a.status(7), true);
184  }
185 
186  {
187  jb::cpu_set a;
188  std::istringstream is("1-5,7,10-200,300-301");
189  is >> a;
190  BOOST_CHECK_EQUAL(a.count(), 199);
191  for (int i = 1; i != 6; ++i) {
192  BOOST_CHECK_MESSAGE(a.status(i), "a.status(i) not true for i=" << i);
193  }
194  for (int i = 10; i != 200; ++i) {
195  BOOST_CHECK_MESSAGE(a.status(i), "a.status(i) not true for i=" << i);
196  }
197  BOOST_CHECK_EQUAL(a.status(7), true);
198  BOOST_CHECK_EQUAL(a.status(300), true);
199  BOOST_CHECK_EQUAL(a.status(301), true);
200  }
201 }
202 
203 /**
204  * @test Verify that the clear() operation works as expected for a range.
205  */
206 BOOST_AUTO_TEST_CASE(cpu_set_clear) {
207  jb::cpu_set a;
208  a.set(2);
209  a.set(3);
210  a.set(4);
211  BOOST_CHECK_EQUAL(a.count(), 3);
212  BOOST_CHECK_EQUAL(a.status(3), true);
213  a.clear(2, 4);
214  BOOST_CHECK_EQUAL(a.count(), 0);
215  BOOST_CHECK_EQUAL(a.status(3), false);
216 }
217 
218 /**
219  * @test Verify that the parse() function works as expected for
220  * invalid inputs.
221  */
222 BOOST_AUTO_TEST_CASE(cpu_set_parse_invalid) {
223  BOOST_CHECK_THROW(jb::cpu_set::parse("zzz"), std::exception);
224  BOOST_CHECK_THROW(jb::cpu_set::parse("1-zzz"), std::exception);
225  BOOST_CHECK_THROW(jb::cpu_set::parse("zzz-2"), std::exception);
226  BOOST_CHECK_THROW(jb::cpu_set::parse("1-2-zzz"), std::exception);
227  BOOST_CHECK_THROW(jb::cpu_set::parse("1-2-3"), std::exception);
228  BOOST_CHECK_THROW(jb::cpu_set::parse("-"), std::exception);
229  BOOST_CHECK_THROW(jb::cpu_set::parse("--"), std::exception);
230  BOOST_CHECK_NO_THROW(jb::cpu_set::parse("1-2"));
231 }
232 
233 /**
234  * @test Verify that the YAML conversion functions work as expected.
235  */
236 BOOST_AUTO_TEST_CASE(cpu_set_yaml_convert) {
237  jb::cpu_set empty;
238  YAML::Node encoded = YAML::convert<jb::cpu_set>::encode(empty);
239  BOOST_CHECK_EQUAL(encoded.as<std::string>(), std::string(""));
240  jb::cpu_set decoded;
241  YAML::convert<jb::cpu_set>::decode(encoded, decoded);
242  BOOST_CHECK_EQUAL(empty, decoded);
243 
244  jb::cpu_set a = jb::cpu_set::parse("1,3-5");
246  BOOST_CHECK_EQUAL(encoded.as<std::string>(), std::string("1,3-5"));
247  YAML::convert<jb::cpu_set>::decode(encoded, decoded);
248  BOOST_CHECK_EQUAL(a, decoded);
249 }
std::size_t capacity() const
Return the number of CPUs that can be stored in the cpu set.
Definition: cpu_set.hpp:45
bool status(int cpu) const
Returns true if cpu is included in the cpu set.
Definition: cpu_set.hpp:50
cpu_set & clear(int cpu)
Remove cpu from the cpu set.
Definition: cpu_set.hpp:73
static cpu_set parse(std::string const &value)
Interpret value as a cpu set in list format.
Definition: cpu_set.cpp:24
static bool decode(YAML::Node node, jb::cpu_set &rhs)
cpu_set & set(int cpu)
Add cpu to the cpu set.
Definition: cpu_set.hpp:66
BOOST_AUTO_TEST_CASE(cpu_set_basic)
Definition: ut_cpu_set.cpp:11
A wrapper for the Linux CPU_SET data structure.
Definition: cpu_set.hpp:37
int count() const
Return the number of cpus included in the cpu set.
Definition: cpu_set.hpp:55
static YAML::Node encode(jb::cpu_set const &x)