1 ///////////////////////////////////////////////////////////////////////////////
2 // test.hpp
3 //
4 //  Copyright 2008 Eric Niebler. Distributed under the Boost
5 //  Software License, Version 1.0. (See accompanying file
6 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 
8 #ifndef BOOST_XPRESSIVE_TEST_TEST_HPP_EAN_10_04_2005
9 #define BOOST_XPRESSIVE_TEST_TEST_HPP_EAN_10_04_2005
10 
11 // MS compatible compilers support #pragma once
12 #if defined(_MSC_VER)
13 # pragma once
14 #endif
15 
16 #include <string>
17 #include <vector>
18 #include <cstdio>
19 #include <cstdarg>
20 #include <functional>
21 #include <boost/range/iterator_range.hpp>
22 #include <boost/xpressive/xpressive_static.hpp>
23 #include <boost/test/unit_test.hpp>
24 
25 using namespace boost::unit_test;
26 using namespace boost::xpressive;
27 
28 #define L(x) BOOST_XPR_CSTR_(char_type, x)
29 
30 #define BOOST_XPR_CHECK(pred)                                                   \
31     if( pred ) {} else { BOOST_ERROR( this->section_ << " : " << #pred ); }
32 
33 using namespace boost::xpressive;
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 // backrefs
37 //
38 #if defined(__cplusplus_cli)
39 #pragma managed(push, off)
40 #endif
41 template<typename Char>
backrefs(Char const * br0,...)42 inline std::vector<std::basic_string<Char> > backrefs(Char const *br0, ...)
43 {
44     using namespace std;
45     std::vector<std::basic_string<Char> > backrefs;
46     if(0 != br0)
47     {
48         backrefs.push_back(br0);
49         va_list va;
50         va_start(va, br0);
51         Char const *brN;
52         while(0 != (brN = va_arg(va, Char const *)))
53         {
54             backrefs.push_back(brN);
55         }
56         va_end(va);
57     }
58     return backrefs;
59 }
60 #if defined(__cplusplus_cli)
61 #pragma managed(pop)
62 #endif
63 
64 ///////////////////////////////////////////////////////////////////////////////
65 //
66 struct no_match_t {};
67 no_match_t const no_match = {};
68 
69 ///////////////////////////////////////////////////////////////////////////////
70 // xpr_test_case
71 //
72 template<typename BidiIter>
73 struct xpr_test_case
74 {
75     typedef BidiIter iterator_type;
76     typedef typename boost::iterator_value<iterator_type>::type char_type;
77     typedef basic_regex<iterator_type> regex_type;
78     typedef std::basic_string<char_type> string_type;
79     typedef std::vector<string_type> backrefs_type;
80 
xpr_test_casexpr_test_case81     xpr_test_case(std::string section, string_type str, regex_type rex, backrefs_type brs)
82       : section_(section)
83       , str_(str)
84       , rex_(rex)
85       , brs_(brs)
86     {
87     }
88 
xpr_test_casexpr_test_case89     xpr_test_case(std::string section, string_type str, regex_type rex, no_match_t)
90       : section_(section)
91       , str_(str)
92       , rex_(rex)
93       , brs_()
94     {
95     }
96 
runxpr_test_case97     void run() const
98     {
99         char_type const empty[] = {0};
100         match_results<BidiIter> what;
101         if(regex_search(this->str_, what, this->rex_))
102         {
103             // match succeeded: was it expected to succeed?
104             BOOST_XPR_CHECK(what.size() == this->brs_.size());
105 
106             for(std::size_t i = 0; i < what.size() && i < this->brs_.size(); ++i)
107             {
108                 BOOST_XPR_CHECK((!what[i].matched && this->brs_[i] == empty) || this->brs_[i] == what[i].str());
109             }
110         }
111         else
112         {
113             // match failed: was it expected to fail?
114             BOOST_XPR_CHECK(0 == this->brs_.size());
115         }
116     }
117 
118 private:
119 
120     std::string section_;
121     string_type str_;
122     regex_type rex_;
123     std::vector<string_type> brs_;
124 };
125 
126 ///////////////////////////////////////////////////////////////////////////////
127 // test_runner
128 template<typename BidiIter>
129 struct test_runner
130 {
131     typedef xpr_test_case<BidiIter> argument_type;
132     typedef void result_type;
133 
operator ()test_runner134     void operator ()(xpr_test_case<BidiIter> const &test) const
135     {
136         test.run();
137     }
138 };
139 
140 ///////////////////////////////////////////////////////////////////////////////
141 // helpful debug routines
142 ///////////////////////////////////////////////////////////////////////////////
143 
144 ///////////////////////////////////////////////////////////////////////////////
145 // remove all occurances of sz from str
string_remove(std::string & str,char const * sz)146 inline void string_remove(std::string &str, char const *sz)
147 {
148     std::string::size_type i = 0, n = std::strlen(sz);
149     while(std::string::npos != (i=str.find(sz,i)))
150     {
151         str.erase(i,n);
152     }
153 }
154 
155 ///////////////////////////////////////////////////////////////////////////////
156 // display_type2
157 //  for type T, write typeid::name after performing some substitutions
158 template<typename T>
display_type2()159 inline void display_type2()
160 {
161     std::string str = typeid(T).name();
162 
163     string_remove(str, "struct ");
164     string_remove(str, "boost::");
165     string_remove(str, "xpressive::");
166     string_remove(str, "detail::");
167     string_remove(str, "fusion::");
168 
169     //std::printf("%s\n\n", str.c_str());
170     std::printf("%s\nwdith=%d\nuse_simple_repeat=%s\n\n", str.c_str()
171         , detail::width_of<T, char>::value
172         , detail::use_simple_repeat<T, char>::value ? "true" : "false");
173 }
174 
175 ///////////////////////////////////////////////////////////////////////////////
176 // display_type
177 //  display the type of the deduced template argument
178 template<typename T>
display_type(T const &)179 inline void display_type(T const &)
180 {
181     display_type2<T>();
182 }
183 
184 #endif
185