blob: 81630d155b39fe73c89446fa592f50055348cb6a [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"));
Alexander Afanasyev680e77a2014-08-14 22:39:17 -070062
63 BOOST_REQUIRE_EQUAL(backRef->size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070064 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("ccc"));
65 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("cd"));
Yingdi Yu5e974202014-01-29 16:59:06 -080066}
67
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070068BOOST_AUTO_TEST_CASE(ComponentSetMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -080069{
70
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070071 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
72 shared_ptr<RegexComponentSetMatcher> cm = make_shared<RegexComponentSetMatcher>("<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080073 bool res = cm->match(Name("/a/b/"), 0, 1);
74 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070075 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070076 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070077
Yingdi Yu5e974202014-01-29 16:59:06 -080078 res = cm->match(Name("/a/b/"), 1, 1);
79 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070080 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070081
Yingdi Yu5e974202014-01-29 16:59:06 -080082 res = cm->match(Name("/a/b/"), 0, 2);
83 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070084 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -080085
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("/a/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);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070091 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -080092
93 res = cm->match(Name("/a/b/d"), 2, 1);
94 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070095 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070096
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070097 backRef = make_shared<RegexBackrefManager>();
98 cm = make_shared<RegexComponentSetMatcher>("[^<a><b><c>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080099 res = cm->match(Name("/b/d"), 1, 1);
100 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700101 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700102 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800103
104}
105
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700106BOOST_AUTO_TEST_CASE(RepeatMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800107{
108
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700109 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
110 shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("[<a><b>]*", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800111 bool res = cm->match(Name("/a/b/c"), 0, 0);
112 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700113 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800114
115 cm->match(Name("/a/b/c"), 0, 2);
116 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700117 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700118 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
119 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800120
121
122
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700123 backRef = make_shared<RegexBackrefManager>();
124 cm = make_shared<RegexRepeatMatcher>("[<a><b>]+", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800125 res = cm->match(Name("/a/b/c"), 0, 0);
126 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700127 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800128
129 res = cm->match(Name("/a/b/c"), 0, 2);
130 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700131 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700132 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
133 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800134
135
136
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700137 backRef = make_shared<RegexBackrefManager>();
138 cm = make_shared<RegexRepeatMatcher>("<.*>*", backRef, 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800139 res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
140 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700141 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700142 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
143 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
144 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
145 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
146 BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toUri(), string("e"));
147 BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toUri(), string("f"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800148
149
150
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700151 backRef = make_shared<RegexBackrefManager>();
152 cm = make_shared<RegexRepeatMatcher>("<>*", backRef, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800153 res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
154 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700155 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700156 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
157 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
158 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
159 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
160 BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toUri(), string("e"));
161 BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toUri(), string("f"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800162
163
164
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700165 backRef = make_shared<RegexBackrefManager>();
166 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800167 res = cm->match(Name("/a/b/c"), 0, 0);
168 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700169 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800170
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700171 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800172 res = cm->match(Name("/a/b/c"), 0, 1);
173 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700174 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700175 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800176
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700177 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800178 res = cm->match(Name("/a/b/c"), 0, 2);
179 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700180 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800181
182
183
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700184 backRef = make_shared<RegexBackrefManager>();
185 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{3}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800186 res = cm->match(Name("/a/b/a/d/"), 0, 2);
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 res = cm->match(Name("/a/b/a/d/"), 0, 3);
191 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700192 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700193 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
194 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
195 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800196
197 res = cm->match(Name("/a/b/a/d/"), 0, 4);
198 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700199 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800200
201
202
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700203 backRef = make_shared<RegexBackrefManager>();
204 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,3}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800205 res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
206 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700207 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700208 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
209 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800210
211 res = cm->match(Name("/a/b/a/d/e/"), 0, 3);
212 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700213 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700214 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
215 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
216 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800217
218 res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
219 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700220 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800221
222 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
223 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700224 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800225
226
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700227 backRef = make_shared<RegexBackrefManager>();
228 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800229 res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
230 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700231 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700232 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
233 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800234
235 res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
236 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700237 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700238 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
239 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
240 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
241 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800242
243 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
244 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700245 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800246
247
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700248
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700249 backRef = make_shared<RegexBackrefManager>();
250 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{,2}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800251 res = cm->match(Name("/a/b/a/b/e/"), 0, 3);
252 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700253 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800254
255 res = cm->match(Name("/a/b/a/b/e/"), 0, 2);
256 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700257 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700258 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
259 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800260
261 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
262 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700263 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700264 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800265
266 res = cm->match(Name("/a/b/a/d/e/"), 0, 0);
267 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700268 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800269}
270
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700271BOOST_AUTO_TEST_CASE(BackRefMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800272{
273
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700274 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
275 shared_ptr<RegexBackrefMatcher> cm = make_shared<RegexBackrefMatcher>("(<a><b>)", backRef);
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700276 backRef->pushRef(static_pointer_cast<RegexMatcher>(cm));
Yingdi Yu5e974202014-01-29 16:59:06 -0800277 cm->lateCompile();
278 bool 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);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700281 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
282 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800283 BOOST_CHECK_EQUAL(backRef->size(), 1);
284
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700285 backRef = make_shared<RegexBackrefManager>();
286 cm = make_shared<RegexBackrefMatcher>("(<a>(<b>))", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800287 backRef->pushRef(cm);
288 cm->lateCompile();
289 res = cm->match(Name("/a/b/c"), 0, 2);
290 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700291 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700292 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
293 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800294 BOOST_CHECK_EQUAL(backRef->size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700295 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("a"));
296 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[1].toUri(), string("b"));
297 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800298}
299
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700300BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800301{
302
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700303 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
304 shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("([<a><b>])+", backRef, 10);
Yingdi Yu5e974202014-01-29 16:59:06 -0800305 bool res = cm->match(Name("/a/b/c"), 0, 2);
306 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700307 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700308 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
309 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800310 BOOST_CHECK_EQUAL(backRef->size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700311 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800312}
313
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700314BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced2)
Yingdi Yu5e974202014-01-29 16:59:06 -0800315{
316
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700317 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
318 shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("(<a>(<b>))<c>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800319 bool res = cm->match(Name("/a/b/c"), 0, 3);
320 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700321 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700322 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
323 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
324 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800325 BOOST_CHECK_EQUAL(backRef->size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700326 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("a"));
327 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[1].toUri(), string("b"));
328 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800329}
330
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700331BOOST_AUTO_TEST_CASE(PatternListMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800332{
333
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700334 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
335 shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("<a>[<a><b>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800336 bool res = cm->match(Name("/a/b/c"), 0, 2);
337 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700338 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700339 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
340 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800341
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700342 backRef = make_shared<RegexBackrefManager>();
343 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800344 res = cm->match(Name("/a/b/c"), 0, 1);
345 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700346 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700347 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800348
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700349 backRef = make_shared<RegexBackrefManager>();
350 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800351 res = cm->match(Name("/a/b/c"), 0, 2);
352 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700353 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800354
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700355 backRef = make_shared<RegexBackrefManager>();
356 cm = make_shared<RegexPatternListMatcher>("<>*<a><>*", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800357 res = cm->match(Name("/a/b/c"), 0, 3);
358 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700359 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700360 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
361 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
362 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800363
364}
365
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700366BOOST_AUTO_TEST_CASE(TopMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800367{
368
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700369 shared_ptr<RegexTopMatcher> cm = make_shared<RegexTopMatcher>("^<a><b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800370 bool res = cm->match(Name("/a/b/c/d"));
371 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700372 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700373 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
374 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
375 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
376 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800377
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700378 cm = make_shared<RegexTopMatcher>("<b><c><d>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800379 res = cm->match(Name("/a/b/c/d"));
380 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700381 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700382 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
383 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
384 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
385 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800386
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700387 cm = make_shared<RegexTopMatcher>("^<a><b><c><d>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800388 res = cm->match(Name("/a/b/c/d"));
389 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700390 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700391 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
392 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
393 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
394 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800395
396 res = cm->match(Name("/a/b/c/d/e"));
397 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700398 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800399
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700400 cm = make_shared<RegexTopMatcher>("<a><b><c><d>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800401 res = cm->match(Name("/a/b/c/d"));
402 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700403 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700404 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
405 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
406 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
407 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800408
409
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700410 cm = make_shared<RegexTopMatcher>("<b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800411 res = cm->match(Name("/a/b/c/d"));
412 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700413 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700414 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
415 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
416 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
417 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800418}
419
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700420BOOST_AUTO_TEST_CASE(TopMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800421{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700422 shared_ptr<Regex> cm = make_shared<Regex>("^(<.*>*)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800423 bool res = cm->match(Name("/n/a/b/c"));
424 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700425 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800426 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
427
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700428 cm = make_shared<Regex>("^(<.*>*)<.*><c>(<.*>)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800429 res = cm->match(Name("/n/a/b/c/d/e/"));
430 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700431 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800432 BOOST_CHECK_EQUAL(cm->expand("\\1\\2"), Name("/n/a/d/"));
433
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700434 cm = make_shared<Regex>("(<.*>*)<.*>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800435 res = cm->match(Name("/n/a/b/c/"));
436 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700437 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800438 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
439
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700440 cm = make_shared<Regex>("<.*>(<.*>*)<.*>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800441 res = cm->match(Name("/n/a/b/c/"));
442 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700443 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800444 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/a/b/"));
445
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700446 cm = make_shared<Regex>("<a>(<>*)<>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800447 res = cm->match(Name("/n/a/b/c/"));
448 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700449 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800450 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/b/"));
451
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700452 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800453 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
454 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700455 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800456 BOOST_CHECK_EQUAL(cm->expand("<ndn>\\2\\1\\3"), Name("/ndn/edu/ucla/yingdi/mac/"));
457
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700458 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>", "<ndn>\\2\\1\\3");
Yingdi Yu5e974202014-01-29 16:59:06 -0800459 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
460 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700461 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800462 BOOST_CHECK_EQUAL(cm->expand(), Name("/ndn/edu/ucla/yingdi/mac/"));
463}
464
465BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800466
467} // namespace ndn