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