blob: e7cdea57651ff1ef1d5ba824f20aa8fa5af6f8af [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/**
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08003 * Copyright (c) 2013-2015 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
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080035namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080036namespace tests {
Yingdi Yu5e974202014-01-29 16:59:06 -080037
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080038using std::string;
39
40BOOST_AUTO_TEST_SUITE(UtilRegex)
Yingdi Yu5e974202014-01-29 16:59:06 -080041
42BOOST_AUTO_TEST_CASE(ComponentMatcher)
43{
44
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070045 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
46 shared_ptr<RegexComponentMatcher> cm = make_shared<RegexComponentMatcher>("a", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080047 bool res = cm->match(Name("/a/b/"), 0, 1);
48 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070049 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070050 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -080051
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070052 backRef = make_shared<RegexBackrefManager>();
53 cm = make_shared<RegexComponentMatcher>("a", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080054 res = cm->match(Name("/a/b/"), 1, 1);
55 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070056 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -080057
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070058 backRef = make_shared<RegexBackrefManager>();
59 cm = make_shared<RegexComponentMatcher>("(c+)\\.(cd)", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080060 res = cm->match(Name("/ccc.cd/b/"), 0, 1);
61 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070062 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070063 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("ccc.cd"));
Alexander Afanasyev680e77a2014-08-14 22:39:17 -070064
65 BOOST_REQUIRE_EQUAL(backRef->size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070066 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("ccc"));
67 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("cd"));
Yingdi Yu5e974202014-01-29 16:59:06 -080068}
69
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070070BOOST_AUTO_TEST_CASE(ComponentSetMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -080071{
72
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070073 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
74 shared_ptr<RegexComponentSetMatcher> cm = make_shared<RegexComponentSetMatcher>("<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080075 bool res = cm->match(Name("/a/b/"), 0, 1);
76 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070077 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070078 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070079
Yingdi Yu5e974202014-01-29 16:59:06 -080080 res = cm->match(Name("/a/b/"), 1, 1);
81 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070082 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070083
Yingdi Yu5e974202014-01-29 16:59:06 -080084 res = cm->match(Name("/a/b/"), 0, 2);
85 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070086 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -080087
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070088 backRef = make_shared<RegexBackrefManager>();
89 cm = make_shared<RegexComponentSetMatcher>("[<a><b><c>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080090 res = cm->match(Name("/a/b/d"), 1, 1);
91 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070092 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070093 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -080094
95 res = cm->match(Name("/a/b/d"), 2, 1);
96 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070097 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070098
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070099 backRef = make_shared<RegexBackrefManager>();
100 cm = make_shared<RegexComponentSetMatcher>("[^<a><b><c>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800101 res = cm->match(Name("/b/d"), 1, 1);
102 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700103 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700104 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800105
106}
107
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700108BOOST_AUTO_TEST_CASE(RepeatMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800109{
110
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700111 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
112 shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("[<a><b>]*", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800113 bool res = cm->match(Name("/a/b/c"), 0, 0);
114 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700115 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800116
117 cm->match(Name("/a/b/c"), 0, 2);
118 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700119 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700120 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
121 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800122
123
124
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700125 backRef = make_shared<RegexBackrefManager>();
126 cm = make_shared<RegexRepeatMatcher>("[<a><b>]+", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800127 res = cm->match(Name("/a/b/c"), 0, 0);
128 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700129 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800130
131 res = cm->match(Name("/a/b/c"), 0, 2);
132 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700133 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700134 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
135 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800136
137
138
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700139 backRef = make_shared<RegexBackrefManager>();
140 cm = make_shared<RegexRepeatMatcher>("<.*>*", backRef, 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800141 res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
142 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700143 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700144 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
145 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
146 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
147 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
148 BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toUri(), string("e"));
149 BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toUri(), string("f"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800150
151
152
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700153 backRef = make_shared<RegexBackrefManager>();
154 cm = make_shared<RegexRepeatMatcher>("<>*", backRef, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800155 res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
156 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700157 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700158 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
159 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
160 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
161 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
162 BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toUri(), string("e"));
163 BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toUri(), string("f"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800164
165
166
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700167 backRef = make_shared<RegexBackrefManager>();
168 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800169 res = cm->match(Name("/a/b/c"), 0, 0);
170 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700171 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800172
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700173 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800174 res = cm->match(Name("/a/b/c"), 0, 1);
175 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700176 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700177 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800178
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700179 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800180 res = cm->match(Name("/a/b/c"), 0, 2);
181 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700182 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800183
184
185
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700186 backRef = make_shared<RegexBackrefManager>();
187 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{3}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800188 res = cm->match(Name("/a/b/a/d/"), 0, 2);
189 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700190 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800191
192 res = cm->match(Name("/a/b/a/d/"), 0, 3);
193 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700194 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700195 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
196 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
197 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800198
199 res = cm->match(Name("/a/b/a/d/"), 0, 4);
200 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700201 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800202
203
204
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700205 backRef = make_shared<RegexBackrefManager>();
206 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,3}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800207 res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
208 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700209 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700210 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
211 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800212
213 res = cm->match(Name("/a/b/a/d/e/"), 0, 3);
214 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700215 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700216 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
217 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
218 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800219
220 res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
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 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
225 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700226 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800227
228
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700229 backRef = make_shared<RegexBackrefManager>();
230 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800231 res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
232 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700233 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700234 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
235 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800236
237 res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
238 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700239 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700240 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
241 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
242 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
243 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800244
245 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
246 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700247 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800248
249
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700250
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700251 backRef = make_shared<RegexBackrefManager>();
252 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{,2}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800253 res = cm->match(Name("/a/b/a/b/e/"), 0, 3);
254 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700255 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800256
257 res = cm->match(Name("/a/b/a/b/e/"), 0, 2);
258 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700259 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700260 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
261 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800262
263 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
264 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700265 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700266 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800267
268 res = cm->match(Name("/a/b/a/d/e/"), 0, 0);
269 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700270 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800271}
272
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700273BOOST_AUTO_TEST_CASE(BackRefMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800274{
275
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700276 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
277 shared_ptr<RegexBackrefMatcher> cm = make_shared<RegexBackrefMatcher>("(<a><b>)", backRef);
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700278 backRef->pushRef(static_pointer_cast<RegexMatcher>(cm));
Yingdi Yu5e974202014-01-29 16:59:06 -0800279 cm->lateCompile();
280 bool res = cm->match(Name("/a/b/c"), 0, 2);
281 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700282 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700283 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
284 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800285 BOOST_CHECK_EQUAL(backRef->size(), 1);
286
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700287 backRef = make_shared<RegexBackrefManager>();
288 cm = make_shared<RegexBackrefMatcher>("(<a>(<b>))", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800289 backRef->pushRef(cm);
290 cm->lateCompile();
291 res = cm->match(Name("/a/b/c"), 0, 2);
292 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700293 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700294 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
295 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800296 BOOST_CHECK_EQUAL(backRef->size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700297 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("a"));
298 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[1].toUri(), string("b"));
299 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800300}
301
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700302BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800303{
304
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700305 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
306 shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("([<a><b>])+", backRef, 10);
Yingdi Yu5e974202014-01-29 16:59:06 -0800307 bool res = cm->match(Name("/a/b/c"), 0, 2);
308 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700309 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700310 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
311 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800312 BOOST_CHECK_EQUAL(backRef->size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700313 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800314}
315
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700316BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced2)
Yingdi Yu5e974202014-01-29 16:59:06 -0800317{
318
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700319 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
320 shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("(<a>(<b>))<c>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800321 bool res = cm->match(Name("/a/b/c"), 0, 3);
322 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700323 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700324 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
325 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
326 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800327 BOOST_CHECK_EQUAL(backRef->size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700328 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("a"));
329 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[1].toUri(), string("b"));
330 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800331}
332
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700333BOOST_AUTO_TEST_CASE(PatternListMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800334{
335
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700336 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
337 shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("<a>[<a><b>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800338 bool res = cm->match(Name("/a/b/c"), 0, 2);
339 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700340 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700341 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
342 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
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, 1);
347 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700348 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700349 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800350
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700351 backRef = make_shared<RegexBackrefManager>();
352 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800353 res = cm->match(Name("/a/b/c"), 0, 2);
354 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700355 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800356
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700357 backRef = make_shared<RegexBackrefManager>();
358 cm = make_shared<RegexPatternListMatcher>("<>*<a><>*", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800359 res = cm->match(Name("/a/b/c"), 0, 3);
360 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700361 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700362 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
363 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
364 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800365
366}
367
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700368BOOST_AUTO_TEST_CASE(TopMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800369{
370
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700371 shared_ptr<RegexTopMatcher> cm = make_shared<RegexTopMatcher>("^<a><b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800372 bool res = cm->match(Name("/a/b/c/d"));
373 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700374 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700375 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
376 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
377 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
378 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800379
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700380 cm = make_shared<RegexTopMatcher>("<b><c><d>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800381 res = cm->match(Name("/a/b/c/d"));
382 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700383 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700384 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
385 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
386 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
387 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
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);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700393 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
394 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
395 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
396 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800397
398 res = cm->match(Name("/a/b/c/d/e"));
399 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700400 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800401
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700402 cm = make_shared<RegexTopMatcher>("<a><b><c><d>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800403 res = cm->match(Name("/a/b/c/d"));
404 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700405 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700406 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
407 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
408 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
409 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800410
411
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700412 cm = make_shared<RegexTopMatcher>("<b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800413 res = cm->match(Name("/a/b/c/d"));
414 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700415 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700416 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
417 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
418 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
419 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800420}
421
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700422BOOST_AUTO_TEST_CASE(TopMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800423{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700424 shared_ptr<Regex> cm = make_shared<Regex>("^(<.*>*)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800425 bool res = cm->match(Name("/n/a/b/c"));
426 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700427 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800428 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
429
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700430 cm = make_shared<Regex>("^(<.*>*)<.*><c>(<.*>)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800431 res = cm->match(Name("/n/a/b/c/d/e/"));
432 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700433 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800434 BOOST_CHECK_EQUAL(cm->expand("\\1\\2"), Name("/n/a/d/"));
435
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700436 cm = make_shared<Regex>("(<.*>*)<.*>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800437 res = cm->match(Name("/n/a/b/c/"));
438 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700439 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800440 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
441
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700442 cm = make_shared<Regex>("<.*>(<.*>*)<.*>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800443 res = cm->match(Name("/n/a/b/c/"));
444 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700445 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800446 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/a/b/"));
447
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700448 cm = make_shared<Regex>("<a>(<>*)<>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800449 res = cm->match(Name("/n/a/b/c/"));
450 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700451 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800452 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/b/"));
453
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700454 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800455 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
456 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700457 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800458 BOOST_CHECK_EQUAL(cm->expand("<ndn>\\2\\1\\3"), Name("/ndn/edu/ucla/yingdi/mac/"));
459
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700460 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>", "<ndn>\\2\\1\\3");
Yingdi Yu5e974202014-01-29 16:59:06 -0800461 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
462 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700463 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800464 BOOST_CHECK_EQUAL(cm->expand(), Name("/ndn/edu/ucla/yingdi/mac/"));
465}
466
467BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800468
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800469} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800470} // namespace ndn