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   : std::unary_function<xpr_test_case<BidiIter>, void>
131 {
operator ()test_runner132     void operator ()(xpr_test_case<BidiIter> const &test) const
133     {
134         test.run();
135     }
136 };
137 
138 ///////////////////////////////////////////////////////////////////////////////
139 // helpful debug routines
140 ///////////////////////////////////////////////////////////////////////////////
141 
142 ///////////////////////////////////////////////////////////////////////////////
143 // remove all occurances of sz from str
string_remove(std::string & str,char const * sz)144 inline void string_remove(std::string &str, char const *sz)
145 {
146     std::string::size_type i = 0, n = std::strlen(sz);
147     while(std::string::npos != (i=str.find(sz,i)))
148     {
149         str.erase(i,n);
150     }
151 }
152 
153 ///////////////////////////////////////////////////////////////////////////////
154 // display_type2
155 //  for type T, write typeid::name after performing some substitutions
156 template<typename T>
display_type2()157 inline void display_type2()
158 {
159     std::string str = typeid(T).name();
160 
161     string_remove(str, "struct ");
162     string_remove(str, "boost::");
163     string_remove(str, "xpressive::");
164     string_remove(str, "detail::");
165     string_remove(str, "fusion::");
166 
167     //std::printf("%s\n\n", str.c_str());
168     std::printf("%s\nwdith=%d\nuse_simple_repeat=%s\n\n", str.c_str()
169         , detail::width_of<T, char>::value
170         , detail::use_simple_repeat<T, char>::value ? "true" : "false");
171 }
172 
173 ///////////////////////////////////////////////////////////////////////////////
174 // display_type
175 //  display the type of the deduced template argument
176 template<typename T>
display_type(T const &)177 inline void display_type(T const &)
178 {
179     display_type2<T>();
180 }
181 
182 #endif
183