• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright (c) 2004
4  * John Maddock
5  *
6  * Use, modification and distribution are subject to the
7  * Boost Software License, Version 1.0. (See accompanying file
8  * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9  *
10  */
11 
12  /*
13   *   LOCATION:    see http://www.boost.org for most recent version.
14   *   FILE         captures_test.cpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: Basic tests for additional captures information.
17   */
18 
19 #include <boost/regex.hpp>
20 #include <boost/detail/lightweight_main.hpp>
21 #include "../test_macros.hpp"
22 #include <boost/array.hpp>
23 #include <cstring>
24 
25 #ifdef BOOST_HAS_ICU
26 #include <boost/regex/icu.hpp>
27 #endif
28 
29 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
30 
31 template <int N>
array_size(const char * (& p)[N])32 size_t array_size(const char* (&p)[N])
33 {
34    for(size_t i = 0; i < N; ++i)
35       if(p[i] == 0)
36          return i;
37    return N;
38 }
39 
make_wstring(const char * p)40 std::wstring make_wstring(const char* p)
41 {
42    return std::wstring(p, p + std::strlen(p));
43 }
44 
45 #ifdef __sgi
46 template <class T>
test_captures(const std::string & regx,const std::string & text,const T & expected)47 void test_captures(const std::string& regx, const std::string& text, const T& expected)
48 #else
49 template <class T>
50 void test_captures(const std::string& regx, const std::string& text, T& expected)
51 #endif
52 {
53    boost::regex e(regx);
54    boost::smatch what;
55    if(boost::regex_match(text, what, e, boost::match_extra))
56    {
57       unsigned i, j;
58 #ifndef __sgi
59       // strange type deduction causes this test to fail on SGI:
60       BOOST_CHECK(what.size() == ARRAY_SIZE(expected));
61 #endif
62       for(i = 0; i < what.size(); ++i)
63       {
64          BOOST_CHECK(what.captures(i).size() == array_size(expected[i]));
65          for(j = 0; j < what.captures(i).size(); ++j)
66          {
67             BOOST_CHECK(what.captures(i)[j] == expected[i][j]);
68          }
69       }
70    }
71 
72 #if !defined(BOOST_NO_WREGEX)
73 
74    std::wstring wre(regx.begin(), regx.end());
75    std::wstring wtext(text.begin(), text.end());
76    boost::wregex we(wre);
77    boost::wsmatch wwhat;
78    if(boost::regex_match(wtext, wwhat, we, boost::match_extra))
79    {
80       unsigned i, j;
81 #ifndef __sgi
82       // strange type deduction causes this test to fail on SGI:
83       BOOST_CHECK(wwhat.size() == ARRAY_SIZE(expected));
84 #endif
85       for(i = 0; i < wwhat.size(); ++i)
86       {
87          BOOST_CHECK(wwhat.captures(i).size() == array_size(expected[i]));
88          for(j = 0; j < wwhat.captures(i).size(); ++j)
89          {
90             BOOST_CHECK(wwhat.captures(i)[j] == make_wstring(expected[i][j]));
91          }
92       }
93    }
94 
95 #endif
96 
97 #ifdef BOOST_HAS_ICU
98    boost::u32regex ure = boost::make_u32regex(regx);
99    what = boost::smatch();
100    if(boost::u32regex_match(text, what, ure, boost::match_extra))
101    {
102       unsigned i, j;
103 #ifndef __sgi
104       // strange type deduction causes this test to fail on SGI:
105       BOOST_CHECK(what.size() == ARRAY_SIZE(expected));
106 #endif
107       for(i = 0; i < what.size(); ++i)
108       {
109          BOOST_CHECK(what.captures(i).size() == array_size(expected[i]));
110          for(j = 0; j < what.captures(i).size(); ++j)
111          {
112             BOOST_CHECK(what.captures(i)[j] == expected[i][j]);
113          }
114       }
115    }
116 #endif
117 }
118 
cpp_main(int,char * [])119 int cpp_main(int , char* [])
120 {
121    typedef const char* pchar;
122    pchar e1[4][5] =
123    {
124       { "aBBcccDDDDDeeeeeeee", },
125       { "a", "BB", "ccc", "DDDDD", "eeeeeeee", },
126       { "a", "ccc", "eeeeeeee", },
127       { "BB", "DDDDD", },
128    };
129    test_captures("(([[:lower:]]+)|([[:upper:]]+))+", "aBBcccDDDDDeeeeeeee", e1);
130    pchar e2[4][2] =
131    {
132       { "abd" },
133       { "b", "" },
134       { "" },
135    };
136    test_captures("a(b+|((c)*))+d", "abd", e2);
137    pchar e3[3][1] =
138    {
139       { "abcbar" },
140       { "abc" },
141    };
142    test_captures("(.*)bar|(.*)bah", "abcbar", e3);
143    pchar e4[3][1] =
144    {
145       { "abcbah" },
146       { 0, },
147       { "abc" },
148    };
149    test_captures("(.*)bar|(.*)bah", "abcbah", e4);
150    pchar e5[2][16] =
151    {
152       { "now is the time for all good men to come to the aid of the party" },
153       { "now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the", "party" },
154    };
155    test_captures("^(?:(\\w+)|(?>\\W+))*$", "now is the time for all good men to come to the aid of the party", e5);
156    pchar e6[2][16] =
157    {
158       { "now is the time for all good men to come to the aid of the party" },
159       { "now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the", "party" },
160    };
161    test_captures("^(?>(\\w+)\\W*)*$", "now is the time for all good men to come to the aid of the party", e6);
162    pchar e7[4][14] =
163    {
164       { "now is the time for all good men to come to the aid of the party" },
165       { "now" },
166       { "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the" },
167       { "party" },
168    };
169    test_captures("^(\\w+)\\W+(?>(\\w+)\\W+)*(\\w+)$", "now is the time for all good men to come to the aid of the party", e7);
170    pchar e8[5][9] =
171    {
172       { "now is the time for all good men to come to the aid of the party" } ,
173       { "now" },
174       { "is", "for", "men", "to", "of" },
175       { "the", "time", "all", "good", "to", "come", "the", "aid", "the" },
176       { "party" },
177    };
178    test_captures("^(\\w+)\\W+(?>(\\w+)\\W+(?:(\\w+)\\W+){0,2})*(\\w+)$", "now is the time for all good men to come to the aid of the party", e8);
179    return 0;
180 }
181 
182