]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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> | |
32 | int array_size(const char* (&p)[N]) | |
33 | { | |
34 | for(int i = 0; i < N; ++i) | |
35 | if(p[i] == 0) | |
36 | return i; | |
37 | return N; | |
38 | } | |
39 | ||
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> | |
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 | std::wstring wre(regx.begin(), regx.end()); | |
73 | std::wstring wtext(text.begin(), text.end()); | |
74 | boost::wregex we(wre); | |
75 | boost::wsmatch wwhat; | |
76 | if(boost::regex_match(wtext, wwhat, we, boost::match_extra)) | |
77 | { | |
78 | unsigned i, j; | |
79 | #ifndef __sgi | |
80 | // strange type deduction causes this test to fail on SGI: | |
81 | BOOST_CHECK(wwhat.size() == ARRAY_SIZE(expected)); | |
82 | #endif | |
83 | for(i = 0; i < wwhat.size(); ++i) | |
84 | { | |
85 | BOOST_CHECK(wwhat.captures(i).size() == array_size(expected[i])); | |
86 | for(j = 0; j < wwhat.captures(i).size(); ++j) | |
87 | { | |
88 | BOOST_CHECK(wwhat.captures(i)[j] == make_wstring(expected[i][j])); | |
89 | } | |
90 | } | |
91 | } | |
92 | ||
93 | #ifdef BOOST_HAS_ICU | |
94 | boost::u32regex ure = boost::make_u32regex(regx); | |
95 | what = boost::smatch(); | |
96 | if(boost::u32regex_match(text, what, ure, boost::match_extra)) | |
97 | { | |
98 | unsigned i, j; | |
99 | #ifndef __sgi | |
100 | // strange type deduction causes this test to fail on SGI: | |
101 | BOOST_CHECK(what.size() == ARRAY_SIZE(expected)); | |
102 | #endif | |
103 | for(i = 0; i < what.size(); ++i) | |
104 | { | |
105 | BOOST_CHECK(what.captures(i).size() == array_size(expected[i])); | |
106 | for(j = 0; j < what.captures(i).size(); ++j) | |
107 | { | |
108 | BOOST_CHECK(what.captures(i)[j] == expected[i][j]); | |
109 | } | |
110 | } | |
111 | } | |
112 | #endif | |
113 | } | |
114 | ||
115 | int cpp_main(int , char* []) | |
116 | { | |
117 | typedef const char* pchar; | |
118 | pchar e1[4][5] = | |
119 | { | |
120 | { "aBBcccDDDDDeeeeeeee", }, | |
121 | { "a", "BB", "ccc", "DDDDD", "eeeeeeee", }, | |
122 | { "a", "ccc", "eeeeeeee", }, | |
123 | { "BB", "DDDDD", }, | |
124 | }; | |
125 | test_captures("(([[:lower:]]+)|([[:upper:]]+))+", "aBBcccDDDDDeeeeeeee", e1); | |
126 | pchar e2[4][2] = | |
127 | { | |
128 | { "abd" }, | |
129 | { "b", "" }, | |
130 | { "" }, | |
131 | }; | |
132 | test_captures("a(b+|((c)*))+d", "abd", e2); | |
133 | pchar e3[3][1] = | |
134 | { | |
135 | { "abcbar" }, | |
136 | { "abc" }, | |
137 | }; | |
138 | test_captures("(.*)bar|(.*)bah", "abcbar", e3); | |
139 | pchar e4[3][1] = | |
140 | { | |
141 | { "abcbah" }, | |
142 | { 0, }, | |
143 | { "abc" }, | |
144 | }; | |
145 | test_captures("(.*)bar|(.*)bah", "abcbah", e4); | |
146 | pchar e5[2][16] = | |
147 | { | |
148 | { "now is the time for all good men to come to the aid of the party" }, | |
149 | { "now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the", "party" }, | |
150 | }; | |
151 | test_captures("^(?:(\\w+)|(?>\\W+))*$", "now is the time for all good men to come to the aid of the party", e5); | |
152 | pchar e6[2][16] = | |
153 | { | |
154 | { "now is the time for all good men to come to the aid of the party" }, | |
155 | { "now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the", "party" }, | |
156 | }; | |
157 | test_captures("^(?>(\\w+)\\W*)*$", "now is the time for all good men to come to the aid of the party", e6); | |
158 | pchar e7[4][14] = | |
159 | { | |
160 | { "now is the time for all good men to come to the aid of the party" }, | |
161 | { "now" }, | |
162 | { "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the" }, | |
163 | { "party" }, | |
164 | }; | |
165 | test_captures("^(\\w+)\\W+(?>(\\w+)\\W+)*(\\w+)$", "now is the time for all good men to come to the aid of the party", e7); | |
166 | pchar e8[5][9] = | |
167 | { | |
168 | { "now is the time for all good men to come to the aid of the party" } , | |
169 | { "now" }, | |
170 | { "is", "for", "men", "to", "of" }, | |
171 | { "the", "time", "all", "good", "to", "come", "the", "aid", "the" }, | |
172 | { "party" }, | |
173 | }; | |
174 | 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); | |
175 | return 0; | |
176 | } | |
177 |