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