blob: c5ef435c9d284eb4f27950f30e5819c982c669c8 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu5e974202014-01-29 16:59:06 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu5e974202014-01-29 16:59:06 -080022 */
23
Yingdi Yu5e974202014-01-29 16:59:06 -080024#include "util/regex/regex-backref-manager.hpp"
25#include "util/regex/regex-component-matcher.hpp"
26#include "util/regex/regex-component-set-matcher.hpp"
27#include "util/regex/regex-pattern-list-matcher.hpp"
28#include "util/regex/regex-repeat-matcher.hpp"
29#include "util/regex/regex-backref-matcher.hpp"
30#include "util/regex/regex-top-matcher.hpp"
31#include "util/regex.hpp"
32
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070033#include "boost-test.hpp"
Yingdi Yu5e974202014-01-29 16:59:06 -080034
Yingdi Yu5e974202014-01-29 16:59:06 -080035using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080036namespace ndn {
Yingdi Yu5e974202014-01-29 16:59:06 -080037
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070038BOOST_AUTO_TEST_SUITE(UtilTestRegex)
Yingdi Yu5e974202014-01-29 16:59:06 -080039
40BOOST_AUTO_TEST_CASE(ComponentMatcher)
41{
42
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070043 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
44 shared_ptr<RegexComponentMatcher> cm = make_shared<RegexComponentMatcher>("a", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080045 bool res = cm->match(Name("/a/b/"), 0, 1);
46 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070047 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070048 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -080049
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070050 backRef = make_shared<RegexBackrefManager>();
51 cm = make_shared<RegexComponentMatcher>("a", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080052 res = cm->match(Name("/a/b/"), 1, 1);
53 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070054 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -080055
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070056 backRef = make_shared<RegexBackrefManager>();
57 cm = make_shared<RegexComponentMatcher>("(c+)\\.(cd)", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080058 res = cm->match(Name("/ccc.cd/b/"), 0, 1);
59 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070060 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070061 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("ccc.cd"));
62 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("ccc"));
63 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("cd"));
Yingdi Yu5e974202014-01-29 16:59:06 -080064}
65
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070066BOOST_AUTO_TEST_CASE(ComponentSetMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -080067{
68
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070069 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
70 shared_ptr<RegexComponentSetMatcher> cm = make_shared<RegexComponentSetMatcher>("<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080071 bool res = cm->match(Name("/a/b/"), 0, 1);
72 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070073 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070074 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070075
Yingdi Yu5e974202014-01-29 16:59:06 -080076 res = cm->match(Name("/a/b/"), 1, 1);
77 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070078 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070079
Yingdi Yu5e974202014-01-29 16:59:06 -080080 res = cm->match(Name("/a/b/"), 0, 2);
81 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070082 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -080083
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070084 backRef = make_shared<RegexBackrefManager>();
85 cm = make_shared<RegexComponentSetMatcher>("[<a><b><c>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080086 res = cm->match(Name("/a/b/d"), 1, 1);
87 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070088 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070089 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -080090
91 res = cm->match(Name("/a/b/d"), 2, 1);
92 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070093 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070094
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070095 backRef = make_shared<RegexBackrefManager>();
96 cm = make_shared<RegexComponentSetMatcher>("[^<a><b><c>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080097 res = cm->match(Name("/b/d"), 1, 1);
98 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070099 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700100 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800101
102}
103
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700104BOOST_AUTO_TEST_CASE(RepeatMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800105{
106
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700107 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
108 shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("[<a><b>]*", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800109 bool res = cm->match(Name("/a/b/c"), 0, 0);
110 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700111 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800112
113 cm->match(Name("/a/b/c"), 0, 2);
114 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700115 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700116 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
117 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800118
119
120
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700121 backRef = make_shared<RegexBackrefManager>();
122 cm = make_shared<RegexRepeatMatcher>("[<a><b>]+", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800123 res = cm->match(Name("/a/b/c"), 0, 0);
124 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700125 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800126
127 res = cm->match(Name("/a/b/c"), 0, 2);
128 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700129 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700130 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
131 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800132
133
134
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700135 backRef = make_shared<RegexBackrefManager>();
136 cm = make_shared<RegexRepeatMatcher>("<.*>*", backRef, 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800137 res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
138 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700139 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700140 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
141 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
142 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
143 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
144 BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toUri(), string("e"));
145 BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toUri(), string("f"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800146
147
148
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700149 backRef = make_shared<RegexBackrefManager>();
150 cm = make_shared<RegexRepeatMatcher>("<>*", backRef, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800151 res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
152 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700153 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700154 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
155 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
156 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
157 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
158 BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toUri(), string("e"));
159 BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toUri(), string("f"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800160
161
162
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700163 backRef = make_shared<RegexBackrefManager>();
164 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800165 res = cm->match(Name("/a/b/c"), 0, 0);
166 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700167 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800168
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700169 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800170 res = cm->match(Name("/a/b/c"), 0, 1);
171 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700172 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700173 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800174
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700175 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800176 res = cm->match(Name("/a/b/c"), 0, 2);
177 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700178 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800179
180
181
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700182 backRef = make_shared<RegexBackrefManager>();
183 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{3}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800184 res = cm->match(Name("/a/b/a/d/"), 0, 2);
185 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700186 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800187
188 res = cm->match(Name("/a/b/a/d/"), 0, 3);
189 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700190 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700191 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
192 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
193 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800194
195 res = cm->match(Name("/a/b/a/d/"), 0, 4);
196 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700197 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800198
199
200
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700201 backRef = make_shared<RegexBackrefManager>();
202 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,3}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800203 res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
204 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700205 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700206 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
207 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800208
209 res = cm->match(Name("/a/b/a/d/e/"), 0, 3);
210 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700211 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700212 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
213 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
214 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800215
216 res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
217 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700218 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800219
220 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
221 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700222 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800223
224
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700225 backRef = make_shared<RegexBackrefManager>();
226 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800227 res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
228 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700229 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700230 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
231 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800232
233 res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
234 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700235 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700236 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
237 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
238 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
239 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800240
241 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
242 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700243 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800244
245
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700246
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700247 backRef = make_shared<RegexBackrefManager>();
248 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{,2}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800249 res = cm->match(Name("/a/b/a/b/e/"), 0, 3);
250 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700251 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800252
253 res = cm->match(Name("/a/b/a/b/e/"), 0, 2);
254 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700255 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700256 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
257 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800258
259 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
260 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700261 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700262 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800263
264 res = cm->match(Name("/a/b/a/d/e/"), 0, 0);
265 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700266 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800267}
268
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700269BOOST_AUTO_TEST_CASE(BackRefMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800270{
271
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700272 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
273 shared_ptr<RegexBackrefMatcher> cm = make_shared<RegexBackrefMatcher>("(<a><b>)", backRef);
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700274 backRef->pushRef(static_pointer_cast<RegexMatcher>(cm));
Yingdi Yu5e974202014-01-29 16:59:06 -0800275 cm->lateCompile();
276 bool res = cm->match(Name("/a/b/c"), 0, 2);
277 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700278 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700279 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
280 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800281 BOOST_CHECK_EQUAL(backRef->size(), 1);
282
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700283 backRef = make_shared<RegexBackrefManager>();
284 cm = make_shared<RegexBackrefMatcher>("(<a>(<b>))", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800285 backRef->pushRef(cm);
286 cm->lateCompile();
287 res = cm->match(Name("/a/b/c"), 0, 2);
288 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700289 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700290 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
291 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800292 BOOST_CHECK_EQUAL(backRef->size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700293 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("a"));
294 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[1].toUri(), string("b"));
295 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800296}
297
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700298BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800299{
300
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700301 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
302 shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("([<a><b>])+", backRef, 10);
Yingdi Yu5e974202014-01-29 16:59:06 -0800303 bool res = cm->match(Name("/a/b/c"), 0, 2);
304 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700305 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700306 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
307 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800308 BOOST_CHECK_EQUAL(backRef->size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700309 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800310}
311
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700312BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced2)
Yingdi Yu5e974202014-01-29 16:59:06 -0800313{
314
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700315 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
316 shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("(<a>(<b>))<c>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800317 bool res = cm->match(Name("/a/b/c"), 0, 3);
318 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700319 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700320 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
321 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
322 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800323 BOOST_CHECK_EQUAL(backRef->size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700324 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("a"));
325 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[1].toUri(), string("b"));
326 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800327}
328
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700329BOOST_AUTO_TEST_CASE(PatternListMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800330{
331
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700332 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
333 shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("<a>[<a><b>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800334 bool res = cm->match(Name("/a/b/c"), 0, 2);
335 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700336 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700337 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
338 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800339
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700340 backRef = make_shared<RegexBackrefManager>();
341 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800342 res = cm->match(Name("/a/b/c"), 0, 1);
343 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700344 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700345 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800346
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700347 backRef = make_shared<RegexBackrefManager>();
348 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800349 res = cm->match(Name("/a/b/c"), 0, 2);
350 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700351 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800352
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700353 backRef = make_shared<RegexBackrefManager>();
354 cm = make_shared<RegexPatternListMatcher>("<>*<a><>*", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800355 res = cm->match(Name("/a/b/c"), 0, 3);
356 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700357 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700358 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
359 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
360 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800361
362}
363
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700364BOOST_AUTO_TEST_CASE(TopMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800365{
366
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700367 shared_ptr<RegexTopMatcher> cm = make_shared<RegexTopMatcher>("^<a><b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800368 bool 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);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700371 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
372 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
373 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
374 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800375
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700376 cm = make_shared<RegexTopMatcher>("<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);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700380 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
381 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
382 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
383 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800384
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700385 cm = make_shared<RegexTopMatcher>("^<a><b><c><d>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800386 res = cm->match(Name("/a/b/c/d"));
387 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700388 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700389 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
390 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
391 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
392 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800393
394 res = cm->match(Name("/a/b/c/d/e"));
395 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700396 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800397
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700398 cm = make_shared<RegexTopMatcher>("<a><b><c><d>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800399 res = cm->match(Name("/a/b/c/d"));
400 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700401 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700402 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
403 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
404 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
405 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800406
407
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700408 cm = make_shared<RegexTopMatcher>("<b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800409 res = cm->match(Name("/a/b/c/d"));
410 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700411 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700412 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
413 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
414 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
415 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800416}
417
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700418BOOST_AUTO_TEST_CASE(TopMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800419{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700420 shared_ptr<Regex> cm = make_shared<Regex>("^(<.*>*)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800421 bool res = cm->match(Name("/n/a/b/c"));
422 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700423 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800424 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
425
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700426 cm = make_shared<Regex>("^(<.*>*)<.*><c>(<.*>)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800427 res = cm->match(Name("/n/a/b/c/d/e/"));
428 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700429 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800430 BOOST_CHECK_EQUAL(cm->expand("\\1\\2"), Name("/n/a/d/"));
431
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700432 cm = make_shared<Regex>("(<.*>*)<.*>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800433 res = cm->match(Name("/n/a/b/c/"));
434 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700435 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800436 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
437
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700438 cm = make_shared<Regex>("<.*>(<.*>*)<.*>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800439 res = cm->match(Name("/n/a/b/c/"));
440 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700441 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800442 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/a/b/"));
443
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700444 cm = make_shared<Regex>("<a>(<>*)<>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800445 res = cm->match(Name("/n/a/b/c/"));
446 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700447 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800448 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/b/"));
449
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700450 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800451 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
452 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700453 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800454 BOOST_CHECK_EQUAL(cm->expand("<ndn>\\2\\1\\3"), Name("/ndn/edu/ucla/yingdi/mac/"));
455
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700456 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>", "<ndn>\\2\\1\\3");
Yingdi Yu5e974202014-01-29 16:59:06 -0800457 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
458 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700459 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800460 BOOST_CHECK_EQUAL(cm->expand(), Name("/ndn/edu/ucla/yingdi/mac/"));
461}
462
463BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800464
465} // namespace ndn