range_run.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <iostream>
  7. #include <cctype>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/spirit/home/support/char_set/range_run.hpp>
  10. #include <boost/dynamic_bitset.hpp>
  11. #include <boost/integer_traits.hpp>
  12. #if defined(_MSC_VER) && _MSC_VER < 1700
  13. # pragma warning(disable: 4127) // conditional expression is constant
  14. #endif
  15. #include <boost/random.hpp>
  16. #if defined(_MSC_VER)
  17. # pragma warning(disable: 4127) // conditional expression is constant
  18. # pragma warning(disable: 4800) // 'int' : forcing value to bool 'true' or 'false' warning
  19. #endif
  20. template <typename Char>
  21. void acid_test()
  22. {
  23. if (sizeof(Char) == sizeof(unsigned))
  24. return; // don't do this test if we have a Char that's very big.
  25. // the smaller chars will suffice for testing.
  26. using boost::spirit::support::detail::range_run;
  27. using boost::spirit::support::detail::range;
  28. typedef boost::integer_traits<Char> integer_traits;
  29. Char const const_min = integer_traits::const_min;
  30. Char const const_max = integer_traits::const_max;
  31. unsigned bit_set_size = unsigned(const_max)-unsigned(const_min)+1;
  32. int const test_size = 1000;
  33. boost::mt19937 rng;
  34. Char min = const_min;
  35. Char max = const_max;
  36. boost::uniform_int<Char> char_(min, max);
  37. boost::variate_generator<boost::mt19937&, boost::uniform_int<Char> >
  38. gen(rng, char_);
  39. boost::uniform_int<Char> _1of10(1, 10);
  40. boost::variate_generator<boost::mt19937&, boost::uniform_int<Char> >
  41. on_or_off(rng, _1of10);
  42. range_run<Char> rr;
  43. boost::dynamic_bitset<unsigned> bset(bit_set_size);
  44. for (int i = 0; i < test_size; ++i)
  45. {
  46. range<Char> r = range<Char>(gen(), gen());
  47. if (r.first > r.last)
  48. std::swap(r.first, r.last);
  49. bool set = on_or_off() != 1;
  50. if (set)
  51. rr.set(r);
  52. else
  53. rr.clear(r);
  54. for (int j = r.first; j <= int(r.last); ++j)
  55. bset[j-const_min] = set;
  56. }
  57. for (int i = const_min; i <= int(const_max); ++i)
  58. {
  59. BOOST_TEST(rr.test(static_cast<Char>(i)) == bset[i-const_min]);
  60. }
  61. }
  62. int
  63. main()
  64. {
  65. using boost::spirit::support::detail::range_run;
  66. using boost::spirit::support::detail::range;
  67. {
  68. range_run<char> rr;
  69. rr.set(range<char>('a', 'a'));
  70. for (char c = 0; c < 127; ++c)
  71. {
  72. BOOST_TEST((c == 'a') == rr.test(c));
  73. }
  74. }
  75. {
  76. range_run<char> rr;
  77. rr.set(range<char>('a', 'z'));
  78. rr.set(range<char>('A', 'Z'));
  79. rr.clear(range<char>('A', 'Z'));
  80. for (char c = 0; c < 127; ++c)
  81. {
  82. BOOST_TEST(bool(std::islower(c)) == rr.test(c));
  83. }
  84. }
  85. {
  86. range_run<char> rr;
  87. rr.set(range<char>(0, 0));
  88. for (char c = 0; c < 127; ++c)
  89. {
  90. BOOST_TEST((c == 0) == rr.test(c));
  91. }
  92. rr.set(range<char>(0, 50));
  93. for (char c = 0; c < 127; ++c)
  94. {
  95. BOOST_TEST(((c >= 0) && (c <= 50)) == rr.test(c));
  96. }
  97. }
  98. {
  99. range_run<unsigned char> rr;
  100. rr.set(range<unsigned char>(255, 255));
  101. for (unsigned char c = 0; c < 255; ++c)
  102. {
  103. BOOST_TEST((c == 255) == rr.test(c));
  104. }
  105. rr.set(range<unsigned char>(250, 255));
  106. for (unsigned char c = 0; c < 255; ++c)
  107. {
  108. BOOST_TEST((c >= 250) == rr.test(c));
  109. }
  110. }
  111. {
  112. range_run<char> rr;
  113. rr.set(range<char>('a', 'z'));
  114. rr.set(range<char>('A', 'Z'));
  115. for (char c = 0; c < 127; ++c)
  116. {
  117. BOOST_TEST(bool(std::isalpha(c)) == rr.test(c));
  118. }
  119. }
  120. {
  121. range_run<char> rr;
  122. rr.set(range<char>('a', 'z'));
  123. rr.set(range<char>('A', 'Z'));
  124. rr.clear(range<char>('J', 'j'));
  125. for (char c = 0; c < 127; ++c)
  126. {
  127. BOOST_TEST((bool(std::isalpha(c)) && (c < 'J' || c > 'j')) == rr.test(c));
  128. }
  129. }
  130. {
  131. range_run<char> rr;
  132. rr.set(range<char>(3, 3));
  133. rr.set(range<char>(1, 5));
  134. BOOST_TEST(rr.test(5));
  135. }
  136. {
  137. range_run<char> rr;
  138. for (char c = 0; c < 127; ++c)
  139. {
  140. if (c & 1)
  141. {
  142. rr.set(range<char>(c, c));
  143. }
  144. }
  145. for (char c = 0; c < 127; ++c)
  146. {
  147. BOOST_TEST(bool((c & 1)) == rr.test(c));
  148. }
  149. rr.clear(range<char>(90, 105));
  150. for (char c = 0; c < 127; ++c)
  151. {
  152. BOOST_TEST((bool((c & 1)) && (c < 90 || c > 105)) == rr.test(c));
  153. }
  154. }
  155. {
  156. range_run<char> rr;
  157. rr.set(range<char>('c', 'e'));
  158. rr.set(range<char>('g', 'i'));
  159. rr.set(range<char>('d', 'k'));
  160. for (char c = 'a'; c <= 'm'; ++c)
  161. {
  162. BOOST_TEST((c >= 'c' && c <= 'k') == rr.test(c));
  163. }
  164. }
  165. {
  166. typedef boost::integer_traits<char> traits;
  167. char const const_min = traits::const_min;
  168. range_run<char> rr;
  169. rr.set(range<char>(const_min, const_min+16));
  170. rr.clear(range<char>(const_min, const_min+8));
  171. BOOST_TEST(!rr.test(const_min));
  172. BOOST_TEST(!rr.test(const_min+8));
  173. BOOST_TEST(rr.test(const_min+9));
  174. BOOST_TEST(rr.test(const_min+16));
  175. BOOST_TEST(!rr.test(const_min+17));
  176. }
  177. {
  178. acid_test<char>();
  179. acid_test<signed char>();
  180. acid_test<unsigned char>();
  181. acid_test<wchar_t>();
  182. acid_test<short>();
  183. acid_test<signed short>();
  184. acid_test<unsigned short>();
  185. }
  186. return boost::report_errors();
  187. }