bitset_test.hpp 39 KB

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