]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // ----------------------------------------------------------- |
2 | // Copyright (c) 2001 Jeremy Siek | |
3 | // Copyright (c) 2003-2006, 2008 Gennaro Prota | |
4 | // Copyright (c) 2014 Ahmed Charles | |
5 | // Copyright (c) 2014 Riccardo Marcangelo | |
6 | // | |
7 | // Distributed under the Boost Software License, Version 1.0. | |
8 | // (See accompanying file LICENSE_1_0.txt or copy at | |
9 | // http://www.boost.org/LICENSE_1_0.txt) | |
10 | // | |
11 | // ----------------------------------------------------------- | |
12 | ||
13 | #ifndef BOOST_BITSET_TEST_HPP_GP_20040319 | |
14 | #define BOOST_BITSET_TEST_HPP_GP_20040319 | |
15 | ||
16 | #include "boost/config.hpp" | |
17 | #if !defined (BOOST_NO_STD_LOCALE) | |
18 | # include <locale> | |
19 | #endif | |
20 | ||
21 | #include <vector> | |
22 | #include <fstream> // used for operator<< | |
23 | #include <string> // for (basic_string and) getline() | |
24 | #include <algorithm> // for std::min | |
25 | #include <assert.h> // <cassert> is sometimes macro-guarded :-( | |
26 | ||
27 | #include "boost/limits.hpp" | |
28 | #include "boost/dynamic_bitset/dynamic_bitset.hpp" | |
29 | #include "boost/test/minimal.hpp" | |
30 | ||
7c673cae FG |
31 | template <typename Block> |
32 | inline bool nth_bit(Block num, std::size_t n) | |
33 | { | |
34 | #ifdef __BORLANDC__ | |
35 | // Borland deduces Block as a const qualified type, | |
36 | // and thus finds numeric_limits<Block> to be zero :( | |
37 | // (though not directly relevant here, see also | |
38 | // lib issue 559) | |
39 | int block_width = sizeof(Block) * CHAR_BIT; | |
40 | #else | |
41 | int block_width = std::numeric_limits<Block>::digits; | |
42 | #endif | |
43 | ||
44 | assert(n < (std::size_t) block_width); | |
45 | return (num >> n) & 1; | |
46 | } | |
47 | ||
48 | // A long, 'irregular', string useful for various tests | |
49 | std::string get_long_string() | |
50 | { | |
51 | const char * const p = | |
52 | // 6 5 4 3 2 1 | |
53 | // 3210987654321098765432109876543210987654321098765432109876543210 | |
54 | "1110011100011110000011110000011111110000000000000110101110000000" | |
55 | "1010101000011100011101010111110000101011111000001111100011100011" | |
56 | "0000000110000001000000111100000111100010101111111000000011100011" | |
57 | "1111111111111111111111111111111111111111111111111111111111111100" | |
58 | "1000001100000001111111111111110000000011111000111100001010100000" | |
59 | "101000111100011010101110011011000000010"; | |
60 | ||
61 | return std::string(p); | |
62 | } | |
63 | ||
64 | const char * test_file_name() | |
65 | { | |
66 | return "boost_dynamic_bitset_tests"; | |
67 | } | |
68 | ||
69 | #if defined BOOST_OLD_IOSTREAMS || defined BOOST_NO_STD_LOCALE | |
70 | template <typename Stream> | |
71 | bool is_one_or_zero(const Stream & /*s*/, char c) | |
72 | { | |
73 | return c == '1' || c == '0'; | |
74 | } | |
75 | template <typename Stream> | |
76 | bool is_white_space(const Stream & /*s*/, char c) | |
77 | { | |
78 | return std::isspace(c); | |
79 | } | |
80 | #else | |
81 | template <typename Stream, typename Ch> | |
82 | bool is_one_or_zero(const Stream& s, Ch c) | |
83 | { | |
84 | typedef typename Stream::traits_type Tr; | |
85 | const Ch zero = s.widen('0'); | |
86 | const Ch one = s.widen('1'); | |
87 | ||
88 | return Tr::eq(c, one) || Tr::eq(c, zero); | |
89 | } | |
90 | template <typename Stream, typename Ch> | |
91 | bool is_white_space(const Stream & s, Ch c) | |
92 | { | |
93 | // NOTE: the using directive is to satisfy Borland 5.6.4 | |
94 | // with its own library (STLport), which doesn't | |
95 | // like std::isspace(c, loc) | |
96 | using namespace std; | |
97 | return isspace(c, s.getloc()); | |
98 | } | |
99 | #endif // defined BOOST_OLD_IOSTREAMS | |
100 | ||
101 | ||
102 | template <typename Stream> | |
103 | bool has_flags(const Stream& s, std::ios::iostate flags) | |
104 | { | |
105 | return (s.rdstate() & flags) != std::ios::goodbit; | |
106 | } | |
107 | ||
108 | ||
109 | // constructors | |
110 | // default (can't do this generically) | |
111 | ||
112 | template <typename Bitset> | |
113 | struct bitset_test { | |
114 | ||
115 | typedef typename Bitset::block_type Block; | |
116 | BOOST_STATIC_CONSTANT(int, bits_per_block = Bitset::bits_per_block); | |
117 | ||
118 | // from unsigned long | |
119 | // | |
120 | // Note: this is templatized so that we check that the do-the-right-thing | |
121 | // constructor dispatch is working correctly. | |
122 | // | |
123 | template <typename NumBits, typename Value> | |
124 | static void from_unsigned_long(NumBits num_bits, Value num) | |
125 | { | |
126 | // An object of size sz = num_bits is constructed: | |
127 | // - the first m bit positions are initialized to the corresponding | |
128 | // bit values in num (m being the smaller of sz and ulong_width) | |
129 | // | |
130 | // - any remaining bit positions are initialized to zero | |
131 | // | |
132 | ||
133 | Bitset b(static_cast<typename Bitset::size_type>(num_bits), static_cast<unsigned long>(num)); | |
134 | ||
135 | // OK, we can now cast to size_type | |
136 | typedef typename Bitset::size_type size_type; | |
137 | const size_type sz = static_cast<size_type>(num_bits); | |
138 | ||
139 | BOOST_CHECK(b.size() == sz); | |
140 | ||
141 | const std::size_t ulong_width = std::numeric_limits<unsigned long>::digits; | |
142 | size_type m = sz; | |
143 | if (ulong_width < sz) | |
144 | m = ulong_width; | |
145 | ||
146 | size_type i = 0; | |
147 | for ( ; i < m; ++i) | |
148 | BOOST_CHECK(b.test(i) == nth_bit(static_cast<unsigned long>(num), i)); | |
149 | for ( ; i < sz; ++i) | |
150 | BOOST_CHECK(b.test(i) == 0); | |
151 | } | |
152 | ||
153 | // from string | |
154 | // | |
155 | // Note: The corresponding function in dynamic_bitset (constructor | |
156 | // from a string) has several default arguments. Actually we don't | |
157 | // test the correct working of those defaults here (except for the | |
158 | // default of num_bits). I'm not sure what to do in this regard. | |
159 | // | |
160 | // Note2: the default argument expression for num_bits doesn't use | |
161 | // static_cast, to avoid a gcc 2.95.3 'sorry, not implemented' | |
162 | // | |
163 | template <typename Ch, typename Tr, typename Al> | |
164 | static void from_string(const std::basic_string<Ch, Tr, Al>& str, | |
165 | std::size_t pos, | |
166 | std::size_t max_char, | |
167 | std::size_t num_bits = (std::size_t)(-1)) | |
168 | { | |
169 | ||
170 | std::size_t rlen = (std::min)(max_char, str.size() - pos); | |
171 | ||
172 | // The resulting size N of the bitset is num_bits, if | |
173 | // that is different from the default arg, rlen otherwise. | |
174 | // Put M = the smaller of N and rlen, then character | |
175 | // position pos + M - 1 corresponds to bit position zero. | |
176 | // Subsequent decreasing character positions correspond to | |
177 | // increasing bit positions. | |
178 | ||
179 | const bool size_upon_string = num_bits == (std::size_t)(-1); | |
180 | Bitset b = size_upon_string ? | |
181 | Bitset(str, pos, max_char) | |
182 | : Bitset(str, pos, max_char, num_bits); | |
183 | ||
184 | const std::size_t actual_size = size_upon_string? rlen : num_bits; | |
185 | BOOST_CHECK(b.size() == actual_size); | |
186 | std::size_t m = (std::min)(num_bits, rlen); | |
187 | std::size_t j; | |
188 | for (j = 0; j < m; ++j) | |
189 | BOOST_CHECK(b[j] == (str[pos + m - 1 - j] == '1')); | |
190 | // If M < N, remaining bit positions are zero | |
191 | for (; j < actual_size; ++j) | |
192 | BOOST_CHECK(b[j] == 0); | |
193 | ||
194 | ||
195 | } | |
196 | ||
197 | static void to_block_range(const Bitset & b /*, BlockOutputIterator result*/) | |
198 | { | |
199 | typedef typename Bitset::size_type size_type; | |
200 | ||
201 | Block sentinel = 0xF0; | |
202 | int s = 8; // number of sentinels (must be *even*) | |
203 | int offset = s/2; | |
204 | std::vector<Block> v(b.num_blocks() + s, sentinel); | |
205 | ||
206 | boost::to_block_range(b, v.begin() + offset); | |
207 | ||
208 | assert(v.size() >= (size_type)s && (s >= 2) && (s % 2 == 0)); | |
209 | // check sentinels at both ends | |
210 | for(int i = 0; i < s/2; ++i) { | |
211 | BOOST_CHECK(v[i] == sentinel); | |
212 | BOOST_CHECK(v[v.size()-1-i] == sentinel); | |
213 | } | |
214 | ||
215 | typename std::vector<Block>::const_iterator p = v.begin() + offset; | |
216 | for(size_type n = 0; n < b.num_blocks(); ++n, ++p) { | |
217 | typename Bitset::block_width_type i = 0; | |
218 | for(; i < bits_per_block; ++i) { | |
219 | size_type bit = n * bits_per_block + i; | |
220 | BOOST_CHECK(nth_bit(*p, i) == (bit < b.size()? b[bit] : 0)); | |
221 | } | |
222 | } | |
223 | } | |
224 | ||
225 | // TODO from_block_range (below) should be splitted | |
226 | ||
227 | // PRE: std::equal(first1, last1, first2) == true | |
228 | static void from_block_range(const std::vector<Block>& blocks) | |
229 | { | |
230 | { // test constructor from block range | |
231 | Bitset bset(blocks.begin(), blocks.end()); | |
232 | std::size_t n = blocks.size(); | |
233 | for (std::size_t b = 0; b < n; ++b) { | |
234 | typename Bitset::block_width_type i = 0; | |
235 | for (; i < bits_per_block; ++i) { | |
236 | std::size_t bit = b * bits_per_block + i; | |
237 | BOOST_CHECK(bset[bit] == nth_bit(blocks[b], i)); | |
238 | } | |
239 | } | |
240 | BOOST_CHECK(bset.size() == n * bits_per_block); | |
241 | } | |
242 | { // test boost::from_block_range | |
243 | const typename Bitset::size_type n = blocks.size(); | |
244 | Bitset bset(n * bits_per_block); | |
245 | boost::from_block_range(blocks.begin(), blocks.end(), bset); | |
246 | for (std::size_t b = 0; b < n; ++b) { | |
247 | typename Bitset::block_width_type i = 0; | |
248 | for (; i < bits_per_block; ++i) { | |
249 | std::size_t bit = b * bits_per_block + i; | |
250 | BOOST_CHECK(bset[bit] == nth_bit(blocks[b], i)); | |
251 | } | |
252 | } | |
253 | BOOST_CHECK(n <= bset.num_blocks()); | |
254 | } | |
255 | } | |
256 | ||
257 | // copy constructor (absent from std::bitset) | |
258 | static void copy_constructor(const Bitset& b) | |
259 | { | |
260 | Bitset copy(b); | |
261 | BOOST_CHECK(b == copy); | |
262 | ||
263 | // Changes to the copy do not affect the original | |
264 | if (b.size() > 0) { | |
265 | std::size_t pos = copy.size() / 2; | |
266 | copy.flip(pos); | |
267 | BOOST_CHECK(copy[pos] != b[pos]); | |
268 | } | |
269 | } | |
270 | ||
271 | // copy assignment operator (absent from std::bitset) | |
272 | static void copy_assignment_operator(const Bitset& lhs, const Bitset& rhs) | |
273 | { | |
274 | Bitset b(lhs); | |
275 | b = rhs; | |
276 | b = b; // self assignment check | |
277 | BOOST_CHECK(b == rhs); | |
278 | ||
279 | // Changes to the copy do not affect the original | |
280 | if (b.size() > 0) { | |
281 | std::size_t pos = b.size() / 2; | |
282 | b.flip(pos); | |
283 | BOOST_CHECK(b[pos] != rhs[pos]); | |
284 | } | |
285 | } | |
286 | ||
287 | static void max_size(const Bitset& b) | |
288 | { | |
289 | BOOST_CHECK(b.max_size() > 0); | |
290 | } | |
291 | ||
292 | #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES | |
293 | ||
294 | // move constructor (absent from std::bitset) | |
295 | static void move_constructor(const Bitset& b) | |
296 | { | |
297 | Bitset copy(boost::move(b)); | |
298 | BOOST_CHECK(b == copy); | |
299 | } | |
300 | ||
301 | // move assignment operator (absent from std::bitset) | |
302 | static void move_assignment_operator(const Bitset& lhs, const Bitset& rhs) | |
303 | { | |
304 | Bitset b(lhs); | |
305 | Bitset c(rhs); | |
306 | b = boost::move(c); | |
307 | b = boost::move(b); // self assignment check | |
308 | BOOST_CHECK(b == rhs); | |
309 | } | |
310 | ||
311 | #endif // BOOST_NO_CXX11_RVALUE_REFERENCES | |
312 | ||
313 | static void swap(const Bitset& lhs, const Bitset& rhs) | |
314 | { | |
315 | // bitsets must be swapped | |
316 | Bitset copy1(lhs); | |
317 | Bitset copy2(rhs); | |
318 | copy1.swap(copy2); | |
319 | ||
320 | BOOST_CHECK(copy1 == rhs); | |
321 | BOOST_CHECK(copy2 == lhs); | |
322 | ||
323 | // references must be stable under a swap | |
324 | for(typename Bitset::size_type i = 0; i < lhs.size(); ++i) { | |
325 | Bitset b1(lhs); | |
326 | Bitset b2(rhs); | |
327 | typename Bitset::reference ref = b1[i]; | |
328 | bool x = ref; | |
329 | if (i < b2.size()) | |
330 | b2[i] = !x; // make sure b2[i] is different | |
331 | b1.swap(b2); | |
332 | BOOST_CHECK(b2[i] == x); // now it must be equal.. | |
333 | b2.flip(i); | |
334 | BOOST_CHECK(ref == b2[i]); // .. and ref must be into b2 | |
335 | BOOST_CHECK(ref == !x); | |
336 | } | |
337 | ||
338 | } | |
339 | ||
340 | static void resize(const Bitset& lhs) | |
341 | { | |
342 | Bitset b(lhs); | |
343 | ||
344 | // Test no change in size | |
345 | b.resize(lhs.size()); | |
346 | BOOST_CHECK(b == lhs); | |
347 | ||
348 | // Test increase in size | |
349 | b.resize(lhs.size() * 2, true); | |
350 | ||
351 | std::size_t i; | |
352 | for (i = 0; i < lhs.size(); ++i) | |
353 | BOOST_CHECK(b[i] == lhs[i]); | |
354 | for (; i < b.size(); ++i) | |
355 | BOOST_CHECK(b[i] == true); | |
356 | ||
357 | // Test decrease in size | |
358 | b.resize(lhs.size()); | |
359 | for (i = 0; i < lhs.size(); ++i) | |
360 | BOOST_CHECK(b[i] == lhs[i]); | |
361 | } | |
362 | ||
363 | static void clear(const Bitset& lhs) | |
364 | { | |
365 | Bitset b(lhs); | |
366 | b.clear(); | |
367 | BOOST_CHECK(b.size() == 0); | |
368 | } | |
369 | ||
370 | static void pop_back(const Bitset& lhs) | |
371 | { | |
372 | Bitset b(lhs); | |
373 | b.pop_back(); | |
374 | BOOST_CHECK(b.size() == lhs.size() - 1); | |
375 | for (std::size_t i = 0; i < b.size(); ++i) | |
376 | BOOST_CHECK(b[i] == lhs[i]); | |
377 | ||
378 | b.pop_back(); | |
379 | BOOST_CHECK(b.size() == lhs.size() - 2); | |
380 | for (std::size_t j = 0; j < b.size(); ++j) | |
381 | BOOST_CHECK(b[j] == lhs[j]); | |
382 | } | |
383 | ||
384 | static void append_bit(const Bitset& lhs) | |
385 | { | |
386 | Bitset b(lhs); | |
387 | b.push_back(true); | |
388 | BOOST_CHECK(b.size() == lhs.size() + 1); | |
389 | BOOST_CHECK(b[b.size() - 1] == true); | |
390 | for (std::size_t i = 0; i < lhs.size(); ++i) | |
391 | BOOST_CHECK(b[i] == lhs[i]); | |
392 | ||
393 | b.push_back(false); | |
394 | BOOST_CHECK(b.size() == lhs.size() + 2); | |
395 | BOOST_CHECK(b[b.size() - 1] == false); | |
396 | BOOST_CHECK(b[b.size() - 2] == true); | |
397 | for (std::size_t j = 0; j < lhs.size(); ++j) | |
398 | BOOST_CHECK(b[j] == lhs[j]); | |
399 | } | |
400 | ||
401 | static void append_block(const Bitset& lhs) | |
402 | { | |
403 | Bitset b(lhs); | |
404 | Block value(128); | |
405 | b.append(value); | |
406 | BOOST_CHECK(b.size() == lhs.size() + bits_per_block); | |
407 | for (typename Bitset::block_width_type i = 0; i < bits_per_block; ++i) | |
408 | BOOST_CHECK(b[lhs.size() + i] == bool((value >> i) & 1)); | |
409 | } | |
410 | ||
411 | static void append_block_range(const Bitset& lhs, const std::vector<Block>& blocks) | |
412 | { | |
413 | Bitset b(lhs), c(lhs); | |
414 | b.append(blocks.begin(), blocks.end()); | |
415 | for (typename std::vector<Block>::const_iterator i = blocks.begin(); | |
416 | i != blocks.end(); ++i) | |
417 | c.append(*i); | |
418 | BOOST_CHECK(b == c); | |
419 | } | |
420 | ||
421 | // operator[] and reference members | |
422 | // PRE: b[i] == bit_vec[i] | |
423 | static void operator_bracket(const Bitset& lhs, const std::vector<bool>& bit_vec) | |
424 | { | |
425 | Bitset b(lhs); | |
426 | std::size_t i, j, k; | |
427 | ||
428 | // x = b[i] | |
429 | // x = ~b[i] | |
430 | for (i = 0; i < b.size(); ++i) { | |
431 | bool x = b[i]; | |
432 | BOOST_CHECK(x == bit_vec[i]); | |
433 | x = ~b[i]; | |
434 | BOOST_CHECK(x == !bit_vec[i]); | |
435 | } | |
436 | Bitset prev(b); | |
437 | ||
438 | // b[i] = x | |
439 | for (j = 0; j < b.size(); ++j) { | |
440 | bool x = !prev[j]; | |
441 | b[j] = x; | |
442 | for (k = 0; k < b.size(); ++k) | |
443 | if (j == k) | |
444 | BOOST_CHECK(b[k] == x); | |
445 | else | |
446 | BOOST_CHECK(b[k] == prev[k]); | |
447 | b[j] = prev[j]; | |
448 | } | |
449 | b.flip(); | |
450 | ||
451 | // b[i] = b[j] | |
452 | for (i = 0; i < b.size(); ++i) { | |
453 | b[i] = prev[i]; | |
454 | for (j = 0; j < b.size(); ++j) { | |
455 | if (i == j) | |
456 | BOOST_CHECK(b[j] == prev[j]); | |
457 | else | |
458 | BOOST_CHECK(b[j] == !prev[j]); | |
459 | } | |
460 | b[i] = !prev[i]; | |
461 | } | |
462 | ||
463 | // b[i].flip() | |
464 | for (i = 0; i < b.size(); ++i) { | |
465 | b[i].flip(); | |
466 | for (j = 0; j < b.size(); ++j) { | |
467 | if (i == j) | |
468 | BOOST_CHECK(b[j] == prev[j]); | |
469 | else | |
470 | BOOST_CHECK(b[j] == !prev[j]); | |
471 | } | |
472 | b[i].flip(); | |
473 | } | |
474 | } | |
475 | ||
476 | //=========================================================================== | |
477 | // bitwise operators | |
478 | ||
479 | // bitwise and assignment | |
480 | ||
481 | // PRE: b.size() == rhs.size() | |
482 | static void and_assignment(const Bitset& b, const Bitset& rhs) | |
483 | { | |
484 | Bitset lhs(b); | |
485 | Bitset prev(lhs); | |
486 | lhs &= rhs; | |
487 | // Clears each bit in lhs for which the corresponding bit in rhs is | |
488 | // clear, and leaves all other bits unchanged. | |
489 | for (std::size_t I = 0; I < lhs.size(); ++I) | |
490 | if (rhs[I] == 0) | |
491 | BOOST_CHECK(lhs[I] == 0); | |
492 | else | |
493 | BOOST_CHECK(lhs[I] == prev[I]); | |
494 | } | |
495 | ||
496 | // PRE: b.size() == rhs.size() | |
497 | static void or_assignment(const Bitset& b, const Bitset& rhs) | |
498 | { | |
499 | Bitset lhs(b); | |
500 | Bitset prev(lhs); | |
501 | lhs |= rhs; | |
502 | // Sets each bit in lhs for which the corresponding bit in rhs is set, and | |
503 | // leaves all other bits unchanged. | |
504 | for (std::size_t I = 0; I < lhs.size(); ++I) | |
505 | if (rhs[I] == 1) | |
506 | BOOST_CHECK(lhs[I] == 1); | |
507 | else | |
508 | BOOST_CHECK(lhs[I] == prev[I]); | |
509 | } | |
510 | ||
511 | // PRE: b.size() == rhs.size() | |
512 | static void xor_assignment(const Bitset& b, const Bitset& rhs) | |
513 | { | |
514 | Bitset lhs(b); | |
515 | Bitset prev(lhs); | |
516 | lhs ^= rhs; | |
517 | // Flips each bit in lhs for which the corresponding bit in rhs is set, | |
518 | // and leaves all other bits unchanged. | |
519 | for (std::size_t I = 0; I < lhs.size(); ++I) | |
520 | if (rhs[I] == 1) | |
521 | BOOST_CHECK(lhs[I] == !prev[I]); | |
522 | else | |
523 | BOOST_CHECK(lhs[I] == prev[I]); | |
524 | } | |
525 | ||
526 | // PRE: b.size() == rhs.size() | |
527 | static void sub_assignment(const Bitset& b, const Bitset& rhs) | |
528 | { | |
529 | Bitset lhs(b); | |
530 | Bitset prev(lhs); | |
531 | lhs -= rhs; | |
532 | // Resets each bit in lhs for which the corresponding bit in rhs is set, | |
533 | // and leaves all other bits unchanged. | |
534 | for (std::size_t I = 0; I < lhs.size(); ++I) | |
535 | if (rhs[I] == 1) | |
536 | BOOST_CHECK(lhs[I] == 0); | |
537 | else | |
538 | BOOST_CHECK(lhs[I] == prev[I]); | |
539 | } | |
540 | ||
541 | static void shift_left_assignment(const Bitset& b, std::size_t pos) | |
542 | { | |
543 | Bitset lhs(b); | |
544 | Bitset prev(lhs); | |
545 | lhs <<= pos; | |
546 | // Replaces each bit at position I in lhs with the following value: | |
547 | // - If I < pos, the new value is zero | |
548 | // - If I >= pos, the new value is the previous value of the bit at | |
549 | // position I - pos | |
550 | for (std::size_t I = 0; I < lhs.size(); ++I) | |
551 | if (I < pos) | |
552 | BOOST_CHECK(lhs[I] == 0); | |
553 | else | |
554 | BOOST_CHECK(lhs[I] == prev[I - pos]); | |
555 | } | |
556 | ||
557 | static void shift_right_assignment(const Bitset& b, std::size_t pos) | |
558 | { | |
559 | Bitset lhs(b); | |
560 | Bitset prev(lhs); | |
561 | lhs >>= pos; | |
562 | // Replaces each bit at position I in lhs with the following value: | |
563 | // - If pos >= N - I, the new value is zero | |
564 | // - If pos < N - I, the new value is the previous value of the bit at | |
565 | // position I + pos | |
566 | std::size_t N = lhs.size(); | |
567 | for (std::size_t I = 0; I < N; ++I) | |
568 | if (pos >= N - I) | |
569 | BOOST_CHECK(lhs[I] == 0); | |
570 | else | |
571 | BOOST_CHECK(lhs[I] == prev[I + pos]); | |
572 | } | |
573 | ||
574 | ||
575 | static void set_all(const Bitset& b) | |
576 | { | |
577 | Bitset lhs(b); | |
578 | lhs.set(); | |
579 | for (std::size_t I = 0; I < lhs.size(); ++I) | |
580 | BOOST_CHECK(lhs[I] == 1); | |
581 | } | |
582 | ||
583 | static void set_one(const Bitset& b, std::size_t pos, bool value) | |
584 | { | |
585 | Bitset lhs(b); | |
586 | std::size_t N = lhs.size(); | |
587 | if (pos < N) { | |
588 | Bitset prev(lhs); | |
589 | // Stores a new value in the bit at position pos in lhs. | |
590 | lhs.set(pos, value); | |
591 | BOOST_CHECK(lhs[pos] == value); | |
592 | ||
593 | // All other values of lhs remain unchanged | |
594 | for (std::size_t I = 0; I < N; ++I) | |
595 | if (I != pos) | |
596 | BOOST_CHECK(lhs[I] == prev[I]); | |
597 | } else { | |
598 | // Not in range, doesn't satisfy precondition. | |
599 | } | |
600 | } | |
601 | ||
602 | static void reset_all(const Bitset& b) | |
603 | { | |
604 | Bitset lhs(b); | |
605 | // Resets all bits in lhs | |
606 | lhs.reset(); | |
607 | for (std::size_t I = 0; I < lhs.size(); ++I) | |
608 | BOOST_CHECK(lhs[I] == 0); | |
609 | } | |
610 | ||
611 | static void reset_one(const Bitset& b, std::size_t pos) | |
612 | { | |
613 | Bitset lhs(b); | |
614 | std::size_t N = lhs.size(); | |
615 | if (pos < N) { | |
616 | Bitset prev(lhs); | |
617 | lhs.reset(pos); | |
618 | // Resets the bit at position pos in lhs | |
619 | BOOST_CHECK(lhs[pos] == 0); | |
620 | ||
621 | // All other values of lhs remain unchanged | |
622 | for (std::size_t I = 0; I < N; ++I) | |
623 | if (I != pos) | |
624 | BOOST_CHECK(lhs[I] == prev[I]); | |
625 | } else { | |
626 | // Not in range, doesn't satisfy precondition. | |
627 | } | |
628 | } | |
629 | ||
630 | static void operator_flip(const Bitset& b) | |
631 | { | |
632 | Bitset lhs(b); | |
633 | Bitset x(lhs); | |
634 | BOOST_CHECK(~lhs == x.flip()); | |
635 | } | |
636 | ||
637 | static void flip_all(const Bitset& b) | |
638 | { | |
639 | Bitset lhs(b); | |
640 | std::size_t N = lhs.size(); | |
641 | Bitset prev(lhs); | |
642 | lhs.flip(); | |
643 | // Toggles all the bits in lhs | |
644 | for (std::size_t I = 0; I < N; ++I) | |
645 | BOOST_CHECK(lhs[I] == !prev[I]); | |
646 | } | |
647 | ||
648 | static void flip_one(const Bitset& b, std::size_t pos) | |
649 | { | |
650 | Bitset lhs(b); | |
651 | std::size_t N = lhs.size(); | |
652 | if (pos < N) { | |
653 | Bitset prev(lhs); | |
654 | lhs.flip(pos); | |
655 | // Toggles the bit at position pos in lhs | |
656 | BOOST_CHECK(lhs[pos] == !prev[pos]); | |
657 | ||
658 | // All other values of lhs remain unchanged | |
659 | for (std::size_t I = 0; I < N; ++I) | |
660 | if (I != pos) | |
661 | BOOST_CHECK(lhs[I] == prev[I]); | |
662 | } else { | |
663 | // Not in range, doesn't satisfy precondition. | |
664 | } | |
665 | } | |
666 | ||
667 | // empty | |
668 | static void empty(const Bitset& b) | |
669 | { | |
670 | BOOST_CHECK(b.empty() == (b.size() == 0)); | |
671 | } | |
672 | ||
673 | // to_ulong() | |
674 | static void to_ulong(const Bitset& lhs) | |
675 | { | |
676 | typedef unsigned long result_type; | |
677 | std::size_t n = std::numeric_limits<result_type>::digits; | |
678 | std::size_t sz = lhs.size(); | |
679 | ||
680 | bool will_overflow = false; | |
681 | for (std::size_t i = n; i < sz; ++i) { | |
682 | if (lhs.test(i) != 0) { | |
683 | will_overflow = true; | |
684 | break; | |
685 | } | |
686 | } | |
687 | if (will_overflow) { | |
688 | try { | |
689 | (void)lhs.to_ulong(); | |
690 | BOOST_CHECK(false); // It should have thrown an exception | |
691 | } catch (std::overflow_error & ex) { | |
692 | // Good! | |
693 | BOOST_CHECK(!!ex.what()); | |
694 | } catch (...) { | |
695 | BOOST_CHECK(false); // threw the wrong exception | |
696 | } | |
697 | } else { | |
698 | result_type num = lhs.to_ulong(); | |
699 | // Be sure the number is right | |
700 | if (sz == 0) | |
701 | BOOST_CHECK(num == 0); | |
702 | else { | |
703 | for (std::size_t i = 0; i < sz; ++i) | |
704 | BOOST_CHECK(lhs[i] == (i < n ? nth_bit(num, i) : 0)); | |
705 | } | |
706 | } | |
707 | } | |
708 | ||
709 | // to_string() | |
710 | static void to_string(const Bitset& b) | |
711 | { | |
712 | std::string str; | |
713 | boost::to_string(b, str); | |
714 | BOOST_CHECK(str.size() == b.size()); | |
715 | for (std::size_t i = 0; i < b.size(); ++i) | |
716 | BOOST_CHECK(str[b.size() - 1 - i] ==(b.test(i)? '1':'0')); | |
717 | } | |
718 | ||
719 | static void count(const Bitset& b) | |
720 | { | |
721 | std::size_t c = b.count(); | |
722 | std::size_t actual = 0; | |
723 | for (std::size_t i = 0; i < b.size(); ++i) | |
724 | if (b[i]) | |
725 | ++actual; | |
726 | BOOST_CHECK(c == actual); | |
727 | } | |
728 | ||
729 | static void size(const Bitset& b) | |
730 | { | |
731 | BOOST_CHECK(Bitset(b).set().count() == b.size()); | |
732 | } | |
733 | ||
734 | static void capacity_test_one(const Bitset& lhs) | |
735 | { | |
736 | //empty bitset | |
737 | Bitset b(lhs); | |
738 | BOOST_CHECK(b.capacity() == 0); | |
739 | } | |
740 | ||
741 | static void capacity_test_two(const Bitset& lhs) | |
742 | { | |
743 | //bitset constructed with size "100" | |
744 | Bitset b(lhs); | |
745 | BOOST_CHECK(b.capacity() >= 100); | |
746 | b.resize(200); | |
747 | BOOST_CHECK(b.capacity() >= 200); | |
748 | } | |
749 | ||
750 | static void reserve_test_one(const Bitset& lhs) | |
751 | { | |
752 | //empty bitset | |
753 | Bitset b(lhs); | |
754 | b.reserve(16); | |
755 | BOOST_CHECK(b.capacity() >= 16); | |
756 | } | |
757 | ||
758 | static void reserve_test_two(const Bitset& lhs) | |
759 | { | |
760 | //bitset constructed with size "100" | |
761 | Bitset b(lhs); | |
762 | BOOST_CHECK(b.capacity() >= 100); | |
763 | b.reserve(60); | |
764 | BOOST_CHECK(b.size() == 100); | |
765 | BOOST_CHECK(b.capacity() >= 100); | |
766 | b.reserve(160); | |
767 | BOOST_CHECK(b.size() == 100); | |
768 | BOOST_CHECK(b.capacity() >= 160); | |
769 | } | |
770 | ||
771 | static void shrink_to_fit_test_one(const Bitset& lhs) | |
772 | { | |
773 | //empty bitset | |
774 | Bitset b(lhs); | |
775 | b.shrink_to_fit(); | |
776 | BOOST_CHECK(b.size() == 0); | |
777 | BOOST_CHECK(b.capacity() == 0); | |
778 | } | |
779 | ||
780 | static void shrink_to_fit_test_two(const Bitset& lhs) | |
781 | { | |
782 | //bitset constructed with size "100" | |
783 | Bitset b(lhs); | |
784 | b.shrink_to_fit(); | |
785 | BOOST_CHECK(b.capacity() >= 100); | |
786 | BOOST_CHECK(b.size() == 100); | |
787 | b.reserve(200); | |
788 | BOOST_CHECK(b.capacity() >= 200); | |
789 | BOOST_CHECK(b.size() == 100); | |
790 | b.shrink_to_fit(); | |
791 | BOOST_CHECK(b.capacity() < 200); | |
792 | BOOST_CHECK(b.size() == 100); | |
793 | } | |
794 | ||
795 | static void all(const Bitset& b) | |
796 | { | |
797 | BOOST_CHECK(b.all() == (b.count() == b.size())); | |
798 | bool result = true; | |
799 | for(std::size_t i = 0; i < b.size(); ++i) | |
800 | if(!b[i]) { | |
801 | result = false; | |
802 | break; | |
803 | } | |
804 | BOOST_CHECK(b.all() == result); | |
805 | } | |
806 | ||
807 | static void any(const Bitset& b) | |
808 | { | |
809 | BOOST_CHECK(b.any() == (b.count() != 0)); | |
810 | bool result = false; | |
811 | for(std::size_t i = 0; i < b.size(); ++i) | |
812 | if(b[i]) { | |
813 | result = true; | |
814 | break; | |
815 | } | |
816 | BOOST_CHECK(b.any() == result); | |
817 | } | |
818 | ||
819 | static void none(const Bitset& b) | |
820 | { | |
821 | bool result = true; | |
822 | for(std::size_t i = 0; i < b.size(); ++i) { | |
823 | if(b[i]) { | |
824 | result = false; | |
825 | break; | |
826 | } | |
827 | } | |
828 | BOOST_CHECK(b.none() == result); | |
829 | ||
830 | // sanity | |
831 | BOOST_CHECK(b.none() == !b.any()); | |
832 | BOOST_CHECK(b.none() == (b.count() == 0)); | |
833 | } | |
834 | ||
835 | static void subset(const Bitset& a, const Bitset& b) | |
836 | { | |
837 | BOOST_CHECK(a.size() == b.size()); // PRE | |
838 | ||
839 | bool is_subset = true; | |
840 | if (b.size()) { // could use b.any() but let's be safe | |
841 | for(std::size_t i = 0; i < a.size(); ++i) { | |
842 | if(a.test(i) && !b.test(i)) { | |
843 | is_subset = false; | |
844 | break; | |
845 | } | |
846 | } | |
847 | } | |
848 | else { | |
849 | // sanity | |
850 | BOOST_CHECK(a.count() == 0); | |
851 | BOOST_CHECK(a.any() == false); | |
852 | ||
853 | //is_subset = (a.any() == false); | |
854 | } | |
855 | ||
856 | BOOST_CHECK(a.is_subset_of(b) == is_subset); | |
857 | } | |
858 | ||
859 | static void proper_subset(const Bitset& a, const Bitset& b) | |
860 | { | |
861 | // PRE: a.size() == b.size() | |
862 | BOOST_CHECK(a.size() == b.size()); | |
863 | ||
864 | bool is_proper = false; | |
865 | ||
866 | if (b.size() != 0) { | |
867 | ||
868 | // check it's a subset | |
869 | subset(a, b); | |
870 | ||
871 | // is it proper? | |
872 | for (std::size_t i = 0; i < a.size(); ++i) { | |
873 | if (!a.test(i) && b.test(i)) { | |
874 | is_proper = true; | |
875 | // sanity | |
876 | BOOST_CHECK(a.count() < b.count()); | |
877 | BOOST_CHECK(b.any()); | |
878 | } | |
879 | } | |
880 | } | |
881 | ||
882 | BOOST_CHECK(a.is_proper_subset_of(b) == is_proper); | |
883 | if (is_proper) | |
884 | BOOST_CHECK(b.is_proper_subset_of(a) != is_proper);// antisymmetry | |
885 | } | |
886 | ||
887 | static void intersects(const Bitset& a, const Bitset& b) | |
888 | { | |
889 | bool have_intersection = false; | |
890 | ||
891 | typename Bitset::size_type m = a.size() < b.size() ? a.size() : b.size(); | |
892 | for(typename Bitset::size_type i = 0; i < m && !have_intersection; ++i) | |
893 | if(a[i] == true && b[i] == true) | |
894 | have_intersection = true; | |
895 | ||
896 | BOOST_CHECK(a.intersects(b) == have_intersection); | |
897 | // also check commutativity | |
898 | BOOST_CHECK(b.intersects(a) == have_intersection); | |
899 | } | |
900 | ||
901 | static void find_first(const Bitset& b) | |
902 | { | |
903 | // find first non-null bit, if any | |
904 | typename Bitset::size_type i = 0; | |
905 | while (i < b.size() && b[i] == 0) | |
906 | ++i; | |
907 | ||
908 | if (i == b.size()) | |
909 | BOOST_CHECK(b.find_first() == Bitset::npos); // not found; | |
910 | else { | |
911 | BOOST_CHECK(b.find_first() == i); | |
912 | BOOST_CHECK(b.test(i) == true); | |
913 | } | |
914 | ||
915 | } | |
916 | ||
917 | static void find_next(const Bitset& b, typename Bitset::size_type prev) | |
918 | { | |
919 | BOOST_CHECK(next_bit_on(b, prev) == b.find_next(prev)); | |
920 | } | |
921 | ||
922 | static void operator_equal(const Bitset& a, const Bitset& b) | |
923 | { | |
924 | if (a == b) { | |
925 | for (std::size_t I = 0; I < a.size(); ++I) | |
926 | BOOST_CHECK(a[I] == b[I]); | |
927 | } else { | |
928 | if (a.size() == b.size()) { | |
929 | bool diff = false; | |
930 | for (std::size_t I = 0; I < a.size(); ++I) | |
931 | if (a[I] != b[I]) { | |
932 | diff = true; | |
933 | break; | |
934 | } | |
935 | BOOST_CHECK(diff); | |
936 | } | |
937 | } | |
938 | } | |
939 | ||
940 | static void operator_not_equal(const Bitset& a, const Bitset& b) | |
941 | { | |
942 | if (a != b) { | |
943 | if (a.size() == b.size()) { | |
944 | bool diff = false; | |
945 | for (std::size_t I = 0; I < a.size(); ++I) | |
946 | if (a[I] != b[I]) { | |
947 | diff = true; | |
948 | break; | |
949 | } | |
950 | BOOST_CHECK(diff); | |
951 | } | |
952 | } else { | |
953 | for (std::size_t I = 0; I < a.size(); ++I) | |
954 | BOOST_CHECK(a[I] == b[I]); | |
955 | } | |
956 | } | |
957 | ||
958 | static bool less_than(const Bitset& a, const Bitset& b) | |
959 | { | |
b32b8144 FG |
960 | |
961 | typedef BOOST_DEDUCED_TYPENAME Bitset::size_type size_type; | |
962 | ||
963 | size_type asize(a.size()); | |
964 | size_type bsize(b.size()); | |
965 | ||
966 | if (!bsize) | |
967 | { | |
7c673cae | 968 | return false; |
b32b8144 FG |
969 | } |
970 | else if (!asize) | |
971 | { | |
972 | return true; | |
973 | } | |
7c673cae | 974 | else |
b32b8144 FG |
975 | { |
976 | ||
977 | // Compare from most significant to least. | |
978 | ||
979 | size_type leqsize(std::min BOOST_PREVENT_MACRO_SUBSTITUTION(asize,bsize)); | |
980 | size_type I; | |
981 | for (I = 0; I < leqsize; ++I,--asize,--bsize) | |
982 | { | |
983 | ||
984 | size_type i = asize-1; | |
985 | size_type j = bsize-1; | |
986 | ||
987 | if (a[i] < b[j]) | |
988 | return true; | |
989 | else if (a[i] > b[j]) | |
990 | return false; | |
991 | // if (a[i] = b[j]) skip to next | |
992 | } | |
993 | return (a.size() < b.size()); | |
994 | } | |
7c673cae FG |
995 | } |
996 | ||
997 | static typename Bitset::size_type next_bit_on(const Bitset& b, typename Bitset::size_type prev) | |
998 | { | |
999 | // helper function for find_next() | |
1000 | // | |
1001 | ||
1002 | if (b.none() == true || prev == Bitset::npos) | |
1003 | return Bitset::npos; | |
1004 | ||
1005 | ++prev; | |
1006 | ||
1007 | if (prev >= b.size()) | |
1008 | return Bitset::npos; | |
1009 | ||
1010 | typename Bitset::size_type i = prev; | |
1011 | while (i < b.size() && b[i] == 0) | |
1012 | ++i; | |
1013 | ||
1014 | return i==b.size() ? Bitset::npos : i; | |
1015 | ||
1016 | } | |
1017 | ||
1018 | static void operator_less_than(const Bitset& a, const Bitset& b) | |
1019 | { | |
1020 | if (less_than(a, b)) | |
1021 | BOOST_CHECK(a < b); | |
1022 | else | |
1023 | BOOST_CHECK(!(a < b)); | |
1024 | } | |
1025 | ||
1026 | static void operator_greater_than(const Bitset& a, const Bitset& b) | |
1027 | { | |
1028 | if (less_than(a, b) || a == b) | |
1029 | BOOST_CHECK(!(a > b)); | |
1030 | else | |
1031 | BOOST_CHECK(a > b); | |
1032 | } | |
1033 | ||
1034 | static void operator_less_than_eq(const Bitset& a, const Bitset& b) | |
1035 | { | |
1036 | if (less_than(a, b) || a == b) | |
1037 | BOOST_CHECK(a <= b); | |
1038 | else | |
1039 | BOOST_CHECK(!(a <= b)); | |
1040 | } | |
1041 | ||
1042 | static void operator_greater_than_eq(const Bitset& a, const Bitset& b) | |
1043 | { | |
1044 | if (less_than(a, b)) | |
1045 | BOOST_CHECK(!(a >= b)); | |
1046 | else | |
1047 | BOOST_CHECK(a >= b); | |
1048 | } | |
1049 | ||
1050 | static void test_bit(const Bitset& b, std::size_t pos) | |
1051 | { | |
1052 | Bitset lhs(b); | |
1053 | std::size_t N = lhs.size(); | |
1054 | if (pos < N) { | |
1055 | BOOST_CHECK(lhs.test(pos) == lhs[pos]); | |
1056 | } else { | |
1057 | // Not in range, doesn't satisfy precondition. | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | static void test_set_bit(const Bitset& b, std::size_t pos, bool value) | |
1062 | { | |
1063 | Bitset lhs(b); | |
1064 | std::size_t N = lhs.size(); | |
1065 | if (pos < N) { | |
1066 | Bitset prev(lhs); | |
1067 | // Stores a new value in the bit at position pos in lhs. | |
1068 | BOOST_CHECK(lhs.test_set(pos, value) == prev[pos]); | |
1069 | BOOST_CHECK(lhs[pos] == value); | |
1070 | ||
1071 | // All other values of lhs remain unchanged | |
1072 | for (std::size_t I = 0; I < N; ++I) | |
1073 | if (I != pos) | |
1074 | BOOST_CHECK(lhs[I] == prev[I]); | |
1075 | } else { | |
1076 | // Not in range, doesn't satisfy precondition. | |
1077 | } | |
1078 | } | |
1079 | ||
1080 | static void operator_shift_left(const Bitset& lhs, std::size_t pos) | |
1081 | { | |
1082 | Bitset x(lhs); | |
1083 | BOOST_CHECK((lhs << pos) == (x <<= pos)); | |
1084 | } | |
1085 | ||
1086 | static void operator_shift_right(const Bitset& lhs, std::size_t pos) | |
1087 | { | |
1088 | Bitset x(lhs); | |
1089 | BOOST_CHECK((lhs >> pos) == (x >>= pos)); | |
1090 | } | |
1091 | ||
1092 | // operator| | |
1093 | static | |
1094 | void operator_or(const Bitset& lhs, const Bitset& rhs) | |
1095 | { | |
1096 | Bitset x(lhs); | |
1097 | BOOST_CHECK((lhs | rhs) == (x |= rhs)); | |
1098 | } | |
1099 | ||
1100 | // operator& | |
1101 | static | |
1102 | void operator_and(const Bitset& lhs, const Bitset& rhs) | |
1103 | { | |
1104 | Bitset x(lhs); | |
1105 | BOOST_CHECK((lhs & rhs) == (x &= rhs)); | |
1106 | } | |
1107 | ||
1108 | // operator^ | |
1109 | static | |
1110 | void operator_xor(const Bitset& lhs, const Bitset& rhs) | |
1111 | { | |
1112 | Bitset x(lhs); | |
1113 | BOOST_CHECK((lhs ^ rhs) == (x ^= rhs)); | |
1114 | } | |
1115 | ||
1116 | // operator- | |
1117 | static | |
1118 | void operator_sub(const Bitset& lhs, const Bitset& rhs) | |
1119 | { | |
1120 | Bitset x(lhs); | |
1121 | BOOST_CHECK((lhs - rhs) == (x -= rhs)); | |
1122 | } | |
1123 | ||
1124 | //------------------------------------------------------------------------------ | |
1125 | // I/O TESTS | |
1126 | // The following tests assume the results of extraction (i.e.: contents, | |
1127 | // state and width of is, contents of b) only depend on input (the string | |
1128 | // str). In other words, they don't consider "unexpected" errors such as | |
1129 | // stream corruption or out of memory. The reason is simple: if e.g. the | |
1130 | // stream buffer throws, the stream layer may eat the exception and | |
1131 | // transform it into a badbit. But we can't trust the stream state here, | |
1132 | // because one of the things that we want to test is exactly whether it | |
1133 | // is set correctly. Similarly for insertion. | |
1134 | // | |
1135 | // To provide for these cases would require that the test functions know | |
1136 | // in advance whether the stream buffer and/or allocations will fail, and | |
1137 | // when; that is, we should write both a special allocator and a special | |
1138 | // stream buffer capable of throwing "on demand" and pass them here. | |
1139 | ||
1140 | // Seems overkill for these kinds of unit tests. | |
1141 | //------------------------------------------------------------------------- | |
1142 | ||
1143 | // operator<<( [basic_]ostream, | |
1144 | template <typename Stream> | |
1145 | static void stream_inserter(const Bitset & b, | |
1146 | Stream & s, | |
1147 | const char * file_name | |
1148 | ) | |
1149 | { | |
1150 | #if defined BOOST_OLD_IOSTREAMS | |
1151 | typedef char char_type; | |
1152 | typedef std::string string_type; | |
1153 | typedef ifstream corresponding_input_stream_type; | |
1154 | #else | |
1155 | typedef typename Stream::char_type char_type; | |
1156 | typedef std::basic_string<char_type> string_type; | |
1157 | typedef std::basic_ifstream<char_type> corresponding_input_stream_type; | |
1158 | ||
1159 | std::ios::iostate except = s.exceptions(); | |
1160 | #endif | |
1161 | ||
1162 | typedef typename Bitset::size_type size_type; | |
1163 | std::streamsize w = s.width(); | |
1164 | char_type fill_char = s.fill(); | |
1165 | std::ios::iostate oldstate = s.rdstate(); | |
1166 | bool stream_was_good = s.good(); | |
1167 | ||
1168 | bool did_throw = false; | |
1169 | try { | |
1170 | s << b; | |
1171 | } | |
1172 | #if defined BOOST_OLD_IOSTREAMS | |
1173 | catch(...) { | |
1174 | BOOST_CHECK(false); | |
1175 | } | |
1176 | #else | |
1177 | catch (const std::ios_base::failure &) { | |
1178 | BOOST_CHECK((except & s.rdstate()) != 0); | |
1179 | did_throw = true; | |
1180 | } catch (...) { | |
1181 | did_throw = true; | |
1182 | } | |
1183 | #endif | |
1184 | ||
1185 | BOOST_CHECK(did_throw || !stream_was_good || (s.width() == 0)); | |
1186 | ||
1187 | if (!stream_was_good) { | |
1188 | BOOST_CHECK(s.good() == false); | |
1189 | ||
1190 | // this should actually be oldstate == s.rdstate() | |
1191 | // but some implementations add badbit in the | |
1192 | // sentry constructor | |
1193 | // | |
1194 | BOOST_CHECK((oldstate & s.rdstate()) == oldstate); | |
1195 | BOOST_CHECK(s.width() == w); | |
1196 | } | |
1197 | else { | |
1198 | if(!did_throw) | |
1199 | BOOST_CHECK(s.width() == 0); | |
1200 | // This test require that os be an output _and_ input stream. | |
1201 | // Of course dynamic_bitset's operator << doesn't require that. | |
1202 | ||
1203 | size_type total_len = w <= 0 || static_cast<size_type>(w) < b.size()? b.size() : static_cast<size_type>(w); | |
1204 | const string_type padding (total_len - b.size(), fill_char); | |
1205 | string_type expected; | |
1206 | boost::to_string(b, expected); | |
1207 | if ((s.flags() & std::ios::adjustfield) != std::ios::left) | |
1208 | expected = padding + expected; | |
1209 | else | |
1210 | expected = expected + padding; | |
1211 | ||
1212 | assert(expected.length() == total_len); | |
1213 | ||
1214 | // close, and reopen the file stream to verify contents | |
1215 | s.close(); | |
1216 | corresponding_input_stream_type is(file_name); | |
1217 | string_type contents; | |
1218 | std::getline(is, contents, char_type()); | |
1219 | BOOST_CHECK(contents == expected); | |
1220 | } | |
1221 | } | |
1222 | ||
1223 | // operator>>( [basic_]istream | |
1224 | template <typename Stream, typename String> | |
1225 | static void stream_extractor(Bitset& b, | |
1226 | Stream& is, | |
1227 | String& str | |
1228 | ) | |
1229 | { | |
1230 | // save necessary info then do extraction | |
1231 | // | |
1232 | const std::streamsize w = is.width(); | |
1233 | Bitset a_copy(b); | |
1234 | bool stream_was_good = is.good(); | |
1235 | ||
1236 | bool did_throw = false; | |
1237 | ||
1238 | #if defined BOOST_OLD_IOSTREAMS | |
1239 | bool has_stream_exceptions = false; | |
1240 | is >> b; | |
1241 | #else | |
1242 | const std::ios::iostate except = is.exceptions(); | |
1243 | bool has_stream_exceptions = true; | |
1244 | try { | |
1245 | is >> b; | |
1246 | } | |
1247 | catch(const std::ios::failure &) { | |
1248 | did_throw = true; | |
1249 | } | |
1250 | ||
1251 | // postconditions | |
1252 | BOOST_CHECK(except == is.exceptions()); // paranoid | |
1253 | #endif | |
1254 | //------------------------------------------------------------------ | |
1255 | ||
1256 | // postconditions | |
1257 | BOOST_CHECK(b.size() <= b.max_size()); | |
1258 | if(w > 0) | |
1259 | BOOST_CHECK(b.size() <= static_cast<typename Bitset::size_type>(w)); | |
1260 | ||
1261 | // throw if and only if required | |
1262 | if(has_stream_exceptions) { | |
1263 | const bool exceptional_state = has_flags(is, is.exceptions()); | |
1264 | BOOST_CHECK(exceptional_state == did_throw); | |
1265 | } | |
1266 | ||
1267 | typedef typename String::size_type size_type; | |
1268 | typedef typename String::value_type Ch; | |
1269 | size_type after_digits = 0; | |
1270 | ||
1271 | if(!stream_was_good) { | |
1272 | BOOST_CHECK(has_flags(is, std::ios::failbit)); | |
1273 | BOOST_CHECK(b == a_copy); | |
1274 | BOOST_CHECK(is.width() == (did_throw ? w : 0)); | |
1275 | } | |
1276 | else { | |
1277 | // stream was good(), parse the string; | |
1278 | // it may contain three parts, all of which are optional | |
1279 | // {spaces} {digits} {non-digits} | |
1280 | // opt opt opt | |
1281 | // | |
1282 | // The values of b.max_size() and is.width() may lead to | |
1283 | // ignore part of the digits, if any. | |
1284 | ||
1285 | size_type pos = 0; | |
1286 | size_type len = str.length(); | |
1287 | // {spaces} | |
1288 | for( ; pos < len && is_white_space(is, str[pos]); ++pos) | |
1289 | {} | |
1290 | size_type after_spaces = pos; | |
1291 | // {digits} or part of them | |
1292 | const typename Bitset::size_type max_digits = | |
1293 | w > 0 && static_cast<typename Bitset::size_type>(w) < b.max_size() | |
1294 | ? static_cast<typename Bitset::size_type>(w) : b.max_size(); | |
1295 | ||
1296 | for( ; pos < len && (pos - after_spaces) < max_digits; ++pos) { | |
1297 | if(!is_one_or_zero(is, str[pos])) | |
1298 | break; | |
1299 | } | |
1300 | after_digits = pos; | |
1301 | size_type num_digits = after_digits - after_spaces; | |
1302 | ||
1303 | // eofbit | |
1304 | if((after_digits == len && max_digits > num_digits )) | |
1305 | BOOST_CHECK(has_flags(is, std::ios::eofbit)); | |
1306 | else | |
1307 | BOOST_CHECK(!has_flags(is, std::ios::eofbit)); | |
1308 | ||
1309 | // failbit <=> there are no digits, except for the library | |
1310 | // issue explained below. | |
1311 | // | |
1312 | if(num_digits == 0) { | |
1313 | if(after_digits == len && has_stream_exceptions && | |
1314 | (is.exceptions() & std::ios::eofbit) != std::ios::goodbit) { | |
1315 | // This is a special case related to library issue 195: | |
1316 | // reaching eof when skipping whitespaces in the sentry ctor. | |
1317 | // The resolution says the sentry constructor should set *both* | |
1318 | // eofbit and failbit; but many implementations deliberately | |
1319 | // set eofbit only. See for instance: | |
1320 | // http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00086.html | |
1321 | // | |
1322 | BOOST_CHECK(did_throw); | |
1323 | ||
1324 | } | |
1325 | else { | |
1326 | BOOST_CHECK(has_flags(is, std::ios::failbit)); | |
1327 | } | |
1328 | } | |
1329 | else | |
1330 | BOOST_CHECK(!has_flags(is, std::ios::failbit)); | |
1331 | ||
1332 | ||
1333 | if(num_digits == 0 && after_digits == len) { | |
1334 | // The VC6 library has a bug/non-conformity in the sentry | |
1335 | // constructor. It uses code like | |
1336 | // // skip whitespaces... | |
1337 | // int_type _C = rdbuf()->sgetc(); | |
1338 | // while (!_Tr::eq_int_type(_Tr::eof(), _C) ... | |
1339 | // | |
1340 | // For an empty file the while statement is never "entered" | |
1341 | // and the stream remains in good() state; thus the sentry | |
1342 | // object gives "true" when converted to bool. This is worse | |
1343 | // than the case above, because not only failbit is not set, | |
1344 | // but no bit is set at all, end we end up clearing the | |
1345 | // bitset though there's nothing in the file to be extracted. | |
1346 | // Note that the dynamic_bitset docs say a sentry object is | |
1347 | // constructed and then converted to bool, thus we rely on | |
1348 | // what the underlying library does. | |
1349 | // | |
1350 | #if !defined(BOOST_DINKUMWARE_STDLIB) || (BOOST_DINKUMWARE_STDLIB >= 306) | |
1351 | BOOST_CHECK(b == a_copy); | |
1352 | #else | |
1353 | BOOST_CHECK(b.empty() == true); | |
1354 | #endif | |
1355 | } | |
1356 | else { | |
1357 | String sub = str.substr(after_spaces, num_digits); | |
1358 | BOOST_CHECK(b == Bitset(sub)); | |
1359 | } | |
1360 | ||
1361 | // check width | |
1362 | BOOST_CHECK(is.width() == 0 | |
1363 | || (after_digits == len && num_digits == 0 && did_throw)); | |
1364 | } | |
1365 | ||
1366 | ||
1367 | // clear the stream to allow further reading then | |
1368 | // retrieve any remaining chars with a single getline() | |
1369 | is.exceptions(std::ios::goodbit); | |
1370 | is.clear(); | |
1371 | String remainder; | |
1372 | std::getline(is, remainder, Ch()); | |
1373 | if(stream_was_good) | |
1374 | BOOST_CHECK(remainder == str.substr(after_digits)); | |
1375 | else | |
1376 | BOOST_CHECK(remainder == str); | |
1377 | ||
1378 | } | |
1379 | ||
1380 | ||
1381 | }; | |
1382 | ||
1383 | ||
1384 | ||
1385 | #endif // include guard |