blob: 6feb6efc79c0b018c42c65148fea7acaac96e5fb [file] [log] [blame]
Yingdi Yu5e974202014-01-29 16:59:06 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu5e974202014-01-29 16:59:06 -080013 */
14
Yingdi Yu5e974202014-01-29 16:59:06 -080015#include "util/regex/regex-backref-manager.hpp"
16#include "util/regex/regex-component-matcher.hpp"
17#include "util/regex/regex-component-set-matcher.hpp"
18#include "util/regex/regex-pattern-list-matcher.hpp"
19#include "util/regex/regex-repeat-matcher.hpp"
20#include "util/regex/regex-backref-matcher.hpp"
21#include "util/regex/regex-top-matcher.hpp"
22#include "util/regex.hpp"
23
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070024#include "boost-test.hpp"
Yingdi Yu5e974202014-01-29 16:59:06 -080025
Yingdi Yu5e974202014-01-29 16:59:06 -080026using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080027namespace ndn {
Yingdi Yu5e974202014-01-29 16:59:06 -080028
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070029BOOST_AUTO_TEST_SUITE(UtilTestRegex)
Yingdi Yu5e974202014-01-29 16:59:06 -080030
31BOOST_AUTO_TEST_CASE(ComponentMatcher)
32{
33
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070034 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
35 shared_ptr<RegexComponentMatcher> cm = make_shared<RegexComponentMatcher>("a", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080036 bool res = cm->match(Name("/a/b/"), 0, 1);
37 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070038 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
39 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -080040
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070041 backRef = make_shared<RegexBackrefManager>();
42 cm = make_shared<RegexComponentMatcher>("a", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080043 res = cm->match(Name("/a/b/"), 1, 1);
44 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070045 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -080046
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070047 backRef = make_shared<RegexBackrefManager>();
48 cm = make_shared<RegexComponentMatcher>("(c+)\\.(cd)", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080049 res = cm->match(Name("/ccc.cd/b/"), 0, 1);
50 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070051 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
52 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("ccc.cd"));
53 BOOST_CHECK_EQUAL(backRef->getBackRef(0)->getMatchResult()[0].toEscapedString(), string("ccc"));
54 BOOST_CHECK_EQUAL(backRef->getBackRef(1)->getMatchResult()[0].toEscapedString(), string("cd"));
Yingdi Yu5e974202014-01-29 16:59:06 -080055}
56
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070057BOOST_AUTO_TEST_CASE(ComponentSetMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -080058{
59
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070060 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
61 shared_ptr<RegexComponentSetMatcher> cm = make_shared<RegexComponentSetMatcher>("<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080062 bool res = cm->match(Name("/a/b/"), 0, 1);
63 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070064 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
65 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070066
Yingdi Yu5e974202014-01-29 16:59:06 -080067 res = cm->match(Name("/a/b/"), 1, 1);
68 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070069 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070070
Yingdi Yu5e974202014-01-29 16:59:06 -080071 res = cm->match(Name("/a/b/"), 0, 2);
72 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070073 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -080074
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070075 backRef = make_shared<RegexBackrefManager>();
76 cm = make_shared<RegexComponentSetMatcher>("[<a><b><c>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080077 res = cm->match(Name("/a/b/d"), 1, 1);
78 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070079 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
80 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -080081
82 res = cm->match(Name("/a/b/d"), 2, 1);
83 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070084 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070085
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070086 backRef = make_shared<RegexBackrefManager>();
87 cm = make_shared<RegexComponentSetMatcher>("[^<a><b><c>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080088 res = cm->match(Name("/b/d"), 1, 1);
89 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070090 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
91 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -080092
93}
94
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070095BOOST_AUTO_TEST_CASE(RepeatMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -080096{
97
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070098 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
99 shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("[<a><b>]*", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800100 bool res = cm->match(Name("/a/b/c"), 0, 0);
101 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700102 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800103
104 cm->match(Name("/a/b/c"), 0, 2);
105 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700106 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
107 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
108 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800109
110
111
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700112 backRef = make_shared<RegexBackrefManager>();
113 cm = make_shared<RegexRepeatMatcher>("[<a><b>]+", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800114 res = cm->match(Name("/a/b/c"), 0, 0);
115 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700116 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800117
118 res = cm->match(Name("/a/b/c"), 0, 2);
119 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700120 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
121 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
122 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800123
124
125
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700126 backRef = make_shared<RegexBackrefManager>();
127 cm = make_shared<RegexRepeatMatcher>("<.*>*", backRef, 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800128 res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
129 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700130 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
131 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
132 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
133 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c"));
134 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("d"));
135 BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toEscapedString(), string("e"));
136 BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toEscapedString(), string("f"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800137
138
139
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700140 backRef = make_shared<RegexBackrefManager>();
141 cm = make_shared<RegexRepeatMatcher>("<>*", backRef, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800142 res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
143 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700144 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
145 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
146 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
147 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c"));
148 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("d"));
149 BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toEscapedString(), string("e"));
150 BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toEscapedString(), string("f"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800151
152
153
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700154 backRef = make_shared<RegexBackrefManager>();
155 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800156 res = cm->match(Name("/a/b/c"), 0, 0);
157 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700158 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800159
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700160 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800161 res = cm->match(Name("/a/b/c"), 0, 1);
162 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700163 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
164 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800165
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700166 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800167 res = cm->match(Name("/a/b/c"), 0, 2);
168 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700169 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800170
171
172
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700173 backRef = make_shared<RegexBackrefManager>();
174 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{3}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800175 res = cm->match(Name("/a/b/a/d/"), 0, 2);
176 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700177 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800178
179 res = cm->match(Name("/a/b/a/d/"), 0, 3);
180 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700181 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
182 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
183 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
184 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800185
186 res = cm->match(Name("/a/b/a/d/"), 0, 4);
187 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700188 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800189
190
191
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700192 backRef = make_shared<RegexBackrefManager>();
193 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,3}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800194 res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
195 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700196 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
197 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
198 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800199
200 res = cm->match(Name("/a/b/a/d/e/"), 0, 3);
201 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700202 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
203 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
204 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
205 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800206
207 res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
208 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700209 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800210
211 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
212 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700213 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800214
215
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700216 backRef = make_shared<RegexBackrefManager>();
217 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800218 res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
219 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700220 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
221 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
222 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800223
224 res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
225 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700226 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
227 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
228 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
229 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("a"));
230 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800231
232 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
233 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700234 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800235
236
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700237
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700238 backRef = make_shared<RegexBackrefManager>();
239 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{,2}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800240 res = cm->match(Name("/a/b/a/b/e/"), 0, 3);
241 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700242 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800243
244 res = cm->match(Name("/a/b/a/b/e/"), 0, 2);
245 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700246 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
247 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
248 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800249
250 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
251 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700252 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
253 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800254
255 res = cm->match(Name("/a/b/a/d/e/"), 0, 0);
256 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700257 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800258}
259
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700260BOOST_AUTO_TEST_CASE(BackRefMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800261{
262
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700263 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
264 shared_ptr<RegexBackrefMatcher> cm = make_shared<RegexBackrefMatcher>("(<a><b>)", backRef);
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700265 backRef->pushRef(static_pointer_cast<RegexMatcher>(cm));
Yingdi Yu5e974202014-01-29 16:59:06 -0800266 cm->lateCompile();
267 bool res = cm->match(Name("/a/b/c"), 0, 2);
268 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700269 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
270 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
271 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800272 BOOST_CHECK_EQUAL(backRef->size(), 1);
273
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700274 backRef = make_shared<RegexBackrefManager>();
275 cm = make_shared<RegexBackrefMatcher>("(<a>(<b>))", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800276 backRef->pushRef(cm);
277 cm->lateCompile();
278 res = cm->match(Name("/a/b/c"), 0, 2);
279 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700280 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
281 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
282 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800283 BOOST_CHECK_EQUAL(backRef->size(), 2);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700284 BOOST_CHECK_EQUAL(backRef->getBackRef(0)->getMatchResult()[0].toEscapedString(), string("a"));
285 BOOST_CHECK_EQUAL(backRef->getBackRef(0)->getMatchResult()[1].toEscapedString(), string("b"));
286 BOOST_CHECK_EQUAL(backRef->getBackRef(1)->getMatchResult()[0].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800287}
288
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700289BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800290{
291
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700292 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
293 shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("([<a><b>])+", backRef, 10);
Yingdi Yu5e974202014-01-29 16:59:06 -0800294 bool res = cm->match(Name("/a/b/c"), 0, 2);
295 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700296 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
297 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
298 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800299 BOOST_CHECK_EQUAL(backRef->size(), 1);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700300 BOOST_CHECK_EQUAL(backRef->getBackRef(0)->getMatchResult()[0].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800301}
302
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700303BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced2)
Yingdi Yu5e974202014-01-29 16:59:06 -0800304{
305
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700306 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
307 shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("(<a>(<b>))<c>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800308 bool res = cm->match(Name("/a/b/c"), 0, 3);
309 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700310 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
311 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
312 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
313 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800314 BOOST_CHECK_EQUAL(backRef->size(), 2);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700315 BOOST_CHECK_EQUAL(backRef->getBackRef(0)->getMatchResult()[0].toEscapedString(), string("a"));
316 BOOST_CHECK_EQUAL(backRef->getBackRef(0)->getMatchResult()[1].toEscapedString(), string("b"));
317 BOOST_CHECK_EQUAL(backRef->getBackRef(1)->getMatchResult()[0].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800318}
319
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700320BOOST_AUTO_TEST_CASE(PatternListMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800321{
322
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700323 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
324 shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("<a>[<a><b>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800325 bool res = cm->match(Name("/a/b/c"), 0, 2);
326 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700327 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
328 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
329 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800330
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700331 backRef = make_shared<RegexBackrefManager>();
332 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800333 res = cm->match(Name("/a/b/c"), 0, 1);
334 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700335 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
336 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800337
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700338 backRef = make_shared<RegexBackrefManager>();
339 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800340 res = cm->match(Name("/a/b/c"), 0, 2);
341 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700342 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800343
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700344 backRef = make_shared<RegexBackrefManager>();
345 cm = make_shared<RegexPatternListMatcher>("<>*<a><>*", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800346 res = cm->match(Name("/a/b/c"), 0, 3);
347 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700348 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
349 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
350 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
351 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800352
353}
354
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700355BOOST_AUTO_TEST_CASE(TopMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800356{
357
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700358 shared_ptr<RegexTopMatcher> cm = make_shared<RegexTopMatcher>("^<a><b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800359 bool res = cm->match(Name("/a/b/c/d"));
360 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700361 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
362 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
363 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
364 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c"));
365 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800366
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700367 cm = make_shared<RegexTopMatcher>("<b><c><d>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800368 res = cm->match(Name("/a/b/c/d"));
369 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700370 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
371 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
372 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
373 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c"));
374 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800375
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700376 cm = make_shared<RegexTopMatcher>("^<a><b><c><d>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800377 res = cm->match(Name("/a/b/c/d"));
378 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700379 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
380 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
381 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
382 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c"));
383 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800384
385 res = cm->match(Name("/a/b/c/d/e"));
386 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700387 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800388
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700389 cm = make_shared<RegexTopMatcher>("<a><b><c><d>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800390 res = cm->match(Name("/a/b/c/d"));
391 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700392 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
393 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
394 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
395 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c"));
396 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800397
398
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700399 cm = make_shared<RegexTopMatcher>("<b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800400 res = cm->match(Name("/a/b/c/d"));
401 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700402 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
403 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
404 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b"));
405 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c"));
406 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800407}
408
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700409BOOST_AUTO_TEST_CASE(TopMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800410{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700411 shared_ptr<Regex> cm = make_shared<Regex>("^(<.*>*)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800412 bool res = cm->match(Name("/n/a/b/c"));
413 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700414 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800415 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
416
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700417 cm = make_shared<Regex>("^(<.*>*)<.*><c>(<.*>)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800418 res = cm->match(Name("/n/a/b/c/d/e/"));
419 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700420 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800421 BOOST_CHECK_EQUAL(cm->expand("\\1\\2"), Name("/n/a/d/"));
422
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700423 cm = make_shared<Regex>("(<.*>*)<.*>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800424 res = cm->match(Name("/n/a/b/c/"));
425 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700426 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800427 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
428
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700429 cm = make_shared<Regex>("<.*>(<.*>*)<.*>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800430 res = cm->match(Name("/n/a/b/c/"));
431 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700432 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800433 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/a/b/"));
434
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700435 cm = make_shared<Regex>("<a>(<>*)<>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800436 res = cm->match(Name("/n/a/b/c/"));
437 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700438 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800439 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/b/"));
440
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700441 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800442 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
443 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700444 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800445 BOOST_CHECK_EQUAL(cm->expand("<ndn>\\2\\1\\3"), Name("/ndn/edu/ucla/yingdi/mac/"));
446
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700447 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>", "<ndn>\\2\\1\\3");
Yingdi Yu5e974202014-01-29 16:59:06 -0800448 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
449 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700450 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800451 BOOST_CHECK_EQUAL(cm->expand(), Name("/ndn/edu/ucla/yingdi/mac/"));
452}
453
454BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800455
456} // namespace ndn