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

source code of boost/libs/spirit/test/qi/range_run.cpp