blob: 86bad95b65511696573edc073867333ef3584deb [file] [log] [blame]
Yingdi Yu5e974202014-01-29 16:59:06 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
Yingdi Yu5e974202014-01-29 16:59:06 -08008#include "util/regex/regex-backref-manager.hpp"
9#include "util/regex/regex-component-matcher.hpp"
10#include "util/regex/regex-component-set-matcher.hpp"
11#include "util/regex/regex-pattern-list-matcher.hpp"
12#include "util/regex/regex-repeat-matcher.hpp"
13#include "util/regex/regex-backref-matcher.hpp"
14#include "util/regex/regex-top-matcher.hpp"
15#include "util/regex.hpp"
16
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070017#include "boost-test.hpp"
Yingdi Yu5e974202014-01-29 16:59:06 -080018
Yingdi Yu5e974202014-01-29 16:59:06 -080019using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080020namespace ndn {
Yingdi Yu5e974202014-01-29 16:59:06 -080021
22BOOST_AUTO_TEST_SUITE(TestRegex)
23
24BOOST_AUTO_TEST_CASE(ComponentMatcher)
25{
26
27 ptr_lib::shared_ptr<RegexBackrefManager> backRef = ptr_lib::make_shared<RegexBackrefManager>();
28 ptr_lib::shared_ptr<RegexComponentMatcher> cm = ptr_lib::make_shared<RegexComponentMatcher>("a", backRef);
29 bool res = cm->match(Name("/a/b/"), 0, 1);
30 BOOST_CHECK_EQUAL(res, true);
31 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 1);
32 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
33
34 backRef = ptr_lib::make_shared<RegexBackrefManager>();
35 cm = ptr_lib::make_shared<RegexComponentMatcher>("a", backRef);
36 res = cm->match(Name("/a/b/"), 1, 1);
37 BOOST_CHECK_EQUAL(res, false);
38 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
39
40 backRef = ptr_lib::make_shared<RegexBackrefManager>();
41 cm = ptr_lib::make_shared<RegexComponentMatcher>("(c+)\\.(cd)", backRef);
42 res = cm->match(Name("/ccc.cd/b/"), 0, 1);
43 BOOST_CHECK_EQUAL(res, true);
44 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 1);
45 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("ccc.cd"));
46 BOOST_CHECK_EQUAL(backRef->getBackRef (0)->getMatchResult ()[0].toEscapedString(), string("ccc"));
47 BOOST_CHECK_EQUAL(backRef->getBackRef (1)->getMatchResult ()[0].toEscapedString(), string("cd"));
48}
49
50BOOST_AUTO_TEST_CASE (ComponentSetMatcher)
51{
52
53 ptr_lib::shared_ptr<RegexBackrefManager> backRef = ptr_lib::make_shared<RegexBackrefManager>();
54 ptr_lib::shared_ptr<RegexComponentSetMatcher> cm = ptr_lib::make_shared<RegexComponentSetMatcher>("<a>", backRef);
55 bool res = cm->match(Name("/a/b/"), 0, 1);
56 BOOST_CHECK_EQUAL(res, true);
57 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 1);
58 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070059
Yingdi Yu5e974202014-01-29 16:59:06 -080060 res = cm->match(Name("/a/b/"), 1, 1);
61 BOOST_CHECK_EQUAL(res, false);
62 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070063
Yingdi Yu5e974202014-01-29 16:59:06 -080064 res = cm->match(Name("/a/b/"), 0, 2);
65 BOOST_CHECK_EQUAL(res, false);
66 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
67
68 backRef = ptr_lib::make_shared<RegexBackrefManager>();
69 cm = ptr_lib::make_shared<RegexComponentSetMatcher>("[<a><b><c>]", backRef);
70 res = cm->match(Name("/a/b/d"), 1, 1);
71 BOOST_CHECK_EQUAL(res, true);
72 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 1);
73 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("b"));
74
75 res = cm->match(Name("/a/b/d"), 2, 1);
76 BOOST_CHECK_EQUAL(res, false);
77 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070078
Yingdi Yu5e974202014-01-29 16:59:06 -080079 backRef = ptr_lib::make_shared<RegexBackrefManager>();
80 cm = ptr_lib::make_shared<RegexComponentSetMatcher>("[^<a><b><c>]", backRef);
81 res = cm->match(Name("/b/d"), 1, 1);
82 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070083 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 1);
Yingdi Yu5e974202014-01-29 16:59:06 -080084 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("d"));
85
86}
87
88BOOST_AUTO_TEST_CASE (RepeatMatcher)
89{
90
91 ptr_lib::shared_ptr<RegexBackrefManager> backRef = ptr_lib::make_shared<RegexBackrefManager>();
92 ptr_lib::shared_ptr<RegexRepeatMatcher> cm = ptr_lib::make_shared<RegexRepeatMatcher>("[<a><b>]*", backRef, 8);
93 bool res = cm->match(Name("/a/b/c"), 0, 0);
94 BOOST_CHECK_EQUAL(res, true);
95 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
96
97 cm->match(Name("/a/b/c"), 0, 2);
98 BOOST_CHECK_EQUAL(res, true);
99 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 2);
100 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
101 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
102
103
104
105 backRef = ptr_lib::make_shared<RegexBackrefManager>();
106 cm = ptr_lib::make_shared<RegexRepeatMatcher>("[<a><b>]+", backRef, 8);
107 res = cm->match(Name("/a/b/c"), 0, 0);
108 BOOST_CHECK_EQUAL(res, false);
109 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
110
111 res = cm->match(Name("/a/b/c"), 0, 2);
112 BOOST_CHECK_EQUAL(res, true);
113 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 2);
114 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
115 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
116
117
118
119 backRef = ptr_lib::make_shared<RegexBackrefManager>();
120 cm = ptr_lib::make_shared<RegexRepeatMatcher>("<.*>*", backRef, 4);
121 res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
122 BOOST_CHECK_EQUAL(res, true);
123 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 6);
124 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
125 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
126 BOOST_CHECK_EQUAL(cm->getMatchResult ()[2].toEscapedString(), string("c"));
127 BOOST_CHECK_EQUAL(cm->getMatchResult ()[3].toEscapedString(), string("d"));
128 BOOST_CHECK_EQUAL(cm->getMatchResult ()[4].toEscapedString(), string("e"));
129 BOOST_CHECK_EQUAL(cm->getMatchResult ()[5].toEscapedString(), string("f"));
130
131
132
133 backRef = ptr_lib::make_shared<RegexBackrefManager>();
134 cm = ptr_lib::make_shared<RegexRepeatMatcher>("<>*", backRef, 2);
135 res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
136 BOOST_CHECK_EQUAL(res, true);
137 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 6);
138 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
139 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
140 BOOST_CHECK_EQUAL(cm->getMatchResult ()[2].toEscapedString(), string("c"));
141 BOOST_CHECK_EQUAL(cm->getMatchResult ()[3].toEscapedString(), string("d"));
142 BOOST_CHECK_EQUAL(cm->getMatchResult ()[4].toEscapedString(), string("e"));
143 BOOST_CHECK_EQUAL(cm->getMatchResult ()[5].toEscapedString(), string("f"));
144
145
146
147 backRef = ptr_lib::make_shared<RegexBackrefManager>();
148 cm = ptr_lib::make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
149 res = cm->match(Name("/a/b/c"), 0, 0);
150 BOOST_CHECK_EQUAL(res, true);
151 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
152
153 cm = ptr_lib::make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
154 res = cm->match(Name("/a/b/c"), 0, 1);
155 BOOST_CHECK_EQUAL(res, true);
156 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 1);
157 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
158
159 cm = ptr_lib::make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
160 res = cm->match(Name("/a/b/c"), 0, 2);
161 BOOST_CHECK_EQUAL(res, false);
162 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
163
164
165
166 backRef = ptr_lib::make_shared<RegexBackrefManager>();
167 cm = ptr_lib::make_shared<RegexRepeatMatcher>("[<a><b>]{3}", backRef, 8);
168 res = cm->match(Name("/a/b/a/d/"), 0, 2);
169 BOOST_CHECK_EQUAL(res, false);
170 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
171
172 res = cm->match(Name("/a/b/a/d/"), 0, 3);
173 BOOST_CHECK_EQUAL(res, true);
174 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 3);
175 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
176 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
177 BOOST_CHECK_EQUAL(cm->getMatchResult ()[2].toEscapedString(), string("a"));
178
179 res = cm->match(Name("/a/b/a/d/"), 0, 4);
180 BOOST_CHECK_EQUAL(res, false);
181 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
182
183
184
185 backRef = ptr_lib::make_shared<RegexBackrefManager>();
186 cm = ptr_lib::make_shared<RegexRepeatMatcher>("[<a><b>]{2,3}", backRef, 8);
187 res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
188 BOOST_CHECK_EQUAL(res, true);
189 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 2);
190 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
191 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
192
193 res = cm->match(Name("/a/b/a/d/e/"), 0, 3);
194 BOOST_CHECK_EQUAL(res, true);
195 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 3);
196 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
197 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
198 BOOST_CHECK_EQUAL(cm->getMatchResult ()[2].toEscapedString(), string("a"));
199
200 res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
201 BOOST_CHECK_EQUAL(res, false);
202 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
203
204 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
205 BOOST_CHECK_EQUAL(res, false);
206 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
207
208
209 backRef = ptr_lib::make_shared<RegexBackrefManager>();
210 cm = ptr_lib::make_shared<RegexRepeatMatcher>("[<a><b>]{2,}", backRef, 8);
211 res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
212 BOOST_CHECK_EQUAL(res, true);
213 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 2);
214 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
215 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
216
217 res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
218 BOOST_CHECK_EQUAL(res, true);
219 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 4);
220 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
221 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
222 BOOST_CHECK_EQUAL(cm->getMatchResult ()[2].toEscapedString(), string("a"));
223 BOOST_CHECK_EQUAL(cm->getMatchResult ()[3].toEscapedString(), string("b"));
224
225 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
226 BOOST_CHECK_EQUAL(res, false);
227 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
228
229
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700230
Yingdi Yu5e974202014-01-29 16:59:06 -0800231 backRef = ptr_lib::make_shared<RegexBackrefManager>();
232 cm = ptr_lib::make_shared<RegexRepeatMatcher>("[<a><b>]{,2}", backRef, 8);
233 res = cm->match(Name("/a/b/a/b/e/"), 0, 3);
234 BOOST_CHECK_EQUAL(res, false);
235 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
236
237 res = cm->match(Name("/a/b/a/b/e/"), 0, 2);
238 BOOST_CHECK_EQUAL(res, true);
239 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 2);
240 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
241 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
242
243 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
244 BOOST_CHECK_EQUAL(res, true);
245 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 1);
246 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
247
248 res = cm->match(Name("/a/b/a/d/e/"), 0, 0);
249 BOOST_CHECK_EQUAL(res, true);
250 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
251}
252
253BOOST_AUTO_TEST_CASE (BackRefMatcher)
254{
255
256 ptr_lib::shared_ptr<RegexBackrefManager> backRef = ptr_lib::make_shared<RegexBackrefManager>();
257 ptr_lib::shared_ptr<RegexBackrefMatcher> cm = ptr_lib::make_shared<RegexBackrefMatcher>("(<a><b>)", backRef);
258 backRef->pushRef(boost::static_pointer_cast<RegexMatcher>(cm));
259 cm->lateCompile();
260 bool res = cm->match(Name("/a/b/c"), 0, 2);
261 BOOST_CHECK_EQUAL(res, true);
262 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 2);
263 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
264 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
265 BOOST_CHECK_EQUAL(backRef->size(), 1);
266
267 backRef = ptr_lib::make_shared<RegexBackrefManager>();
268 cm = ptr_lib::make_shared<RegexBackrefMatcher>("(<a>(<b>))", backRef);
269 backRef->pushRef(cm);
270 cm->lateCompile();
271 res = cm->match(Name("/a/b/c"), 0, 2);
272 BOOST_CHECK_EQUAL(res, true);
273 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 2);
274 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
275 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
276 BOOST_CHECK_EQUAL(backRef->size(), 2);
277 BOOST_CHECK_EQUAL(backRef->getBackRef (0)->getMatchResult ()[0].toEscapedString(), string("a"));
278 BOOST_CHECK_EQUAL(backRef->getBackRef (0)->getMatchResult ()[1].toEscapedString(), string("b"));
279 BOOST_CHECK_EQUAL(backRef->getBackRef (1)->getMatchResult ()[0].toEscapedString(), string("b"));
280}
281
282BOOST_AUTO_TEST_CASE (BackRefMatcherAdvanced)
283{
284
285 ptr_lib::shared_ptr<RegexBackrefManager> backRef = ptr_lib::make_shared<RegexBackrefManager>();
286 ptr_lib::shared_ptr<RegexRepeatMatcher> cm = ptr_lib::make_shared<RegexRepeatMatcher>("([<a><b>])+", backRef, 10);
287 bool res = cm->match(Name("/a/b/c"), 0, 2);
288 BOOST_CHECK_EQUAL(res, true);
289 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 2);
290 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
291 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
292 BOOST_CHECK_EQUAL(backRef->size(), 1);
293 BOOST_CHECK_EQUAL(backRef->getBackRef (0)->getMatchResult ()[0].toEscapedString(), string("b"));
294}
295
296BOOST_AUTO_TEST_CASE (BackRefMatcherAdvanced2)
297{
298
299 ptr_lib::shared_ptr<RegexBackrefManager> backRef = ptr_lib::make_shared<RegexBackrefManager>();
300 ptr_lib::shared_ptr<RegexPatternListMatcher> cm = ptr_lib::make_shared<RegexPatternListMatcher>("(<a>(<b>))<c>", backRef);
301 bool res = cm->match(Name("/a/b/c"), 0, 3);
302 BOOST_CHECK_EQUAL(res, true);
303 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 3);
304 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
305 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
306 BOOST_CHECK_EQUAL(cm->getMatchResult ()[2].toEscapedString(), string("c"));
307 BOOST_CHECK_EQUAL(backRef->size(), 2);
308 BOOST_CHECK_EQUAL(backRef->getBackRef (0)->getMatchResult ()[0].toEscapedString(), string("a"));
309 BOOST_CHECK_EQUAL(backRef->getBackRef (0)->getMatchResult ()[1].toEscapedString(), string("b"));
310 BOOST_CHECK_EQUAL(backRef->getBackRef (1)->getMatchResult ()[0].toEscapedString(), string("b"));
311}
312
313BOOST_AUTO_TEST_CASE (PatternListMatcher)
314{
315
316 ptr_lib::shared_ptr<RegexBackrefManager> backRef = ptr_lib::make_shared<RegexBackrefManager>();
317 ptr_lib::shared_ptr<RegexPatternListMatcher> cm = ptr_lib::make_shared<RegexPatternListMatcher>("<a>[<a><b>]", backRef);
318 bool res = cm->match(Name("/a/b/c"), 0, 2);
319 BOOST_CHECK_EQUAL(res, true);
320 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 2);
321 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
322 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
323
324 backRef = ptr_lib::make_shared<RegexBackrefManager>();
325 cm = ptr_lib::make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
326 res = cm->match(Name("/a/b/c"), 0, 1);
327 BOOST_CHECK_EQUAL(res, true);
328 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 1);
329 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
330
331 backRef = ptr_lib::make_shared<RegexBackrefManager>();
332 cm = ptr_lib::make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
333 res = cm->match(Name("/a/b/c"), 0, 2);
334 BOOST_CHECK_EQUAL(res, false);
335 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
336
337 backRef = ptr_lib::make_shared<RegexBackrefManager>();
338 cm = ptr_lib::make_shared<RegexPatternListMatcher>("<>*<a><>*", backRef);
339 res = cm->match(Name("/a/b/c"), 0, 3);
340 BOOST_CHECK_EQUAL(res, true);
341 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 3);
342 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
343 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
344 BOOST_CHECK_EQUAL(cm->getMatchResult ()[2].toEscapedString(), string("c"));
345
346}
347
348BOOST_AUTO_TEST_CASE (TopMatcher)
349{
350
351 ptr_lib::shared_ptr<RegexTopMatcher> cm = ptr_lib::make_shared<RegexTopMatcher>("^<a><b><c>");
352 bool res = cm->match(Name("/a/b/c/d"));
353 BOOST_CHECK_EQUAL(res, true);
354 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 4);
355 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
356 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
357 BOOST_CHECK_EQUAL(cm->getMatchResult ()[2].toEscapedString(), string("c"));
358 BOOST_CHECK_EQUAL(cm->getMatchResult ()[3].toEscapedString(), string("d"));
359
360 cm = ptr_lib::make_shared<RegexTopMatcher>("<b><c><d>$");
361 res = cm->match(Name("/a/b/c/d"));
362 BOOST_CHECK_EQUAL(res, true);
363 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 4);
364 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
365 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
366 BOOST_CHECK_EQUAL(cm->getMatchResult ()[2].toEscapedString(), string("c"));
367 BOOST_CHECK_EQUAL(cm->getMatchResult ()[3].toEscapedString(), string("d"));
368
369 cm = ptr_lib::make_shared<RegexTopMatcher>("^<a><b><c><d>$");
370 res = cm->match(Name("/a/b/c/d"));
371 BOOST_CHECK_EQUAL(res, true);
372 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 4);
373 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
374 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
375 BOOST_CHECK_EQUAL(cm->getMatchResult ()[2].toEscapedString(), string("c"));
376 BOOST_CHECK_EQUAL(cm->getMatchResult ()[3].toEscapedString(), string("d"));
377
378 res = cm->match(Name("/a/b/c/d/e"));
379 BOOST_CHECK_EQUAL(res, false);
380 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 0);
381
382 cm = ptr_lib::make_shared<RegexTopMatcher>("<a><b><c><d>");
383 res = cm->match(Name("/a/b/c/d"));
384 BOOST_CHECK_EQUAL(res, true);
385 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 4);
386 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
387 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
388 BOOST_CHECK_EQUAL(cm->getMatchResult ()[2].toEscapedString(), string("c"));
389 BOOST_CHECK_EQUAL(cm->getMatchResult ()[3].toEscapedString(), string("d"));
390
391
392 cm = ptr_lib::make_shared<RegexTopMatcher>("<b><c>");
393 res = cm->match(Name("/a/b/c/d"));
394 BOOST_CHECK_EQUAL(res, true);
395 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 4);
396 BOOST_CHECK_EQUAL(cm->getMatchResult ()[0].toEscapedString(), string("a"));
397 BOOST_CHECK_EQUAL(cm->getMatchResult ()[1].toEscapedString(), string("b"));
398 BOOST_CHECK_EQUAL(cm->getMatchResult ()[2].toEscapedString(), string("c"));
399 BOOST_CHECK_EQUAL(cm->getMatchResult ()[3].toEscapedString(), string("d"));
400}
401
402BOOST_AUTO_TEST_CASE (TopMatcherAdvanced)
403{
404 ptr_lib::shared_ptr<Regex> cm = ptr_lib::make_shared<Regex>("^(<.*>*)<.*>");
405 bool res = cm->match(Name("/n/a/b/c"));
406 BOOST_CHECK_EQUAL(res, true);
407 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 4);
408 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
409
410 cm = ptr_lib::make_shared<Regex>("^(<.*>*)<.*><c>(<.*>)<.*>");
411 res = cm->match(Name("/n/a/b/c/d/e/"));
412 BOOST_CHECK_EQUAL(res, true);
413 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 6);
414 BOOST_CHECK_EQUAL(cm->expand("\\1\\2"), Name("/n/a/d/"));
415
416 cm = ptr_lib::make_shared<Regex>("(<.*>*)<.*>$");
417 res = cm->match(Name("/n/a/b/c/"));
418 BOOST_CHECK_EQUAL(res, true);
419 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 4);
420 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
421
422 cm = ptr_lib::make_shared<Regex>("<.*>(<.*>*)<.*>$");
423 res = cm->match(Name("/n/a/b/c/"));
424 BOOST_CHECK_EQUAL(res, true);
425 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 4);
426 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/a/b/"));
427
428 cm = ptr_lib::make_shared<Regex>("<a>(<>*)<>$");
429 res = cm->match(Name("/n/a/b/c/"));
430 BOOST_CHECK_EQUAL(res, true);
431 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 4);
432 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/b/"));
433
434 cm = ptr_lib::make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>");
435 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
436 BOOST_CHECK_EQUAL(res, true);
437 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 6);
438 BOOST_CHECK_EQUAL(cm->expand("<ndn>\\2\\1\\3"), Name("/ndn/edu/ucla/yingdi/mac/"));
439
440 cm = ptr_lib::make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>", "<ndn>\\2\\1\\3");
441 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
442 BOOST_CHECK_EQUAL(res, true);
443 BOOST_CHECK_EQUAL(cm->getMatchResult ().size(), 6);
444 BOOST_CHECK_EQUAL(cm->expand(), Name("/ndn/edu/ucla/yingdi/mac/"));
445}
446
447BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800448
449} // namespace ndn