blob: fc46c23ea3d9e232a0aa78b86a3c427de86440cd [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/**
Davide Pesaventoeee3e822016-11-26 19:19:34 +01003 * Copyright (c) 2013-2016 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
Davide Pesaventoeee3e822016-11-26 19:19:34 +010040BOOST_AUTO_TEST_SUITE(Util)
41BOOST_AUTO_TEST_SUITE(TestRegex)
Yingdi Yu5e974202014-01-29 16:59:06 -080042
43BOOST_AUTO_TEST_CASE(ComponentMatcher)
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{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070072 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
73 shared_ptr<RegexComponentSetMatcher> cm = make_shared<RegexComponentSetMatcher>("<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080074 bool res = cm->match(Name("/a/b/"), 0, 1);
75 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070076 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070077 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070078
Yingdi Yu5e974202014-01-29 16:59:06 -080079 res = cm->match(Name("/a/b/"), 1, 1);
80 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070081 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070082
Yingdi Yu5e974202014-01-29 16:59:06 -080083 res = cm->match(Name("/a/b/"), 0, 2);
84 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070085 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -080086
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070087 backRef = make_shared<RegexBackrefManager>();
88 cm = make_shared<RegexComponentSetMatcher>("[<a><b><c>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080089 res = cm->match(Name("/a/b/d"), 1, 1);
90 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070091 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070092 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -080093
94 res = cm->match(Name("/a/b/d"), 2, 1);
95 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070096 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070097
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070098 backRef = make_shared<RegexBackrefManager>();
99 cm = make_shared<RegexComponentSetMatcher>("[^<a><b><c>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800100 res = cm->match(Name("/b/d"), 1, 1);
101 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700102 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700103 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800104}
105
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700106BOOST_AUTO_TEST_CASE(RepeatMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800107{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700108 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
109 shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("[<a><b>]*", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800110 bool res = cm->match(Name("/a/b/c"), 0, 0);
111 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700112 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800113
114 cm->match(Name("/a/b/c"), 0, 2);
115 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700116 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700117 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
118 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800119
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700120 backRef = make_shared<RegexBackrefManager>();
121 cm = make_shared<RegexRepeatMatcher>("[<a><b>]+", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800122 res = cm->match(Name("/a/b/c"), 0, 0);
123 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700124 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800125
126 res = cm->match(Name("/a/b/c"), 0, 2);
127 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700128 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700129 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
130 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800131
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700132 backRef = make_shared<RegexBackrefManager>();
133 cm = make_shared<RegexRepeatMatcher>("<.*>*", backRef, 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800134 res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
135 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700136 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700137 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
138 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
139 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
140 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
141 BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toUri(), string("e"));
142 BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toUri(), string("f"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800143
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700144 backRef = make_shared<RegexBackrefManager>();
145 cm = make_shared<RegexRepeatMatcher>("<>*", backRef, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800146 res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
147 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700148 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700149 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
150 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
151 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
152 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
153 BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toUri(), string("e"));
154 BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toUri(), string("f"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800155
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700156 backRef = make_shared<RegexBackrefManager>();
157 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800158 res = cm->match(Name("/a/b/c"), 0, 0);
159 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700160 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800161
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700162 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800163 res = cm->match(Name("/a/b/c"), 0, 1);
164 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700165 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700166 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800167
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700168 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800169 res = cm->match(Name("/a/b/c"), 0, 2);
170 BOOST_CHECK_EQUAL(res, false);
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 backRef = make_shared<RegexBackrefManager>();
174 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{3}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800175 res = cm->match(Name("/a/b/a/d/"), 0, 2);
176 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700177 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800178
179 res = cm->match(Name("/a/b/a/d/"), 0, 3);
180 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700181 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700182 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
183 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
184 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800185
186 res = cm->match(Name("/a/b/a/d/"), 0, 4);
187 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700188 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800189
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700190 backRef = make_shared<RegexBackrefManager>();
191 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,3}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800192 res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
193 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700194 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
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"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800197
198 res = cm->match(Name("/a/b/a/d/e/"), 0, 3);
199 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700200 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700201 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
202 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
203 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800204
205 res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
206 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700207 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800208
209 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
210 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700211 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800212
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700213 backRef = make_shared<RegexBackrefManager>();
214 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800215 res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
216 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700217 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700218 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
219 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800220
221 res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
222 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700223 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700224 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
225 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
226 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
227 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800228
229 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
230 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700231 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800232
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700233 backRef = make_shared<RegexBackrefManager>();
234 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{,2}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800235 res = cm->match(Name("/a/b/a/b/e/"), 0, 3);
236 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700237 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800238
239 res = cm->match(Name("/a/b/a/b/e/"), 0, 2);
240 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700241 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700242 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
243 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].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, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700247 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700248 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800249
250 res = cm->match(Name("/a/b/a/d/e/"), 0, 0);
251 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700252 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800253}
254
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700255BOOST_AUTO_TEST_CASE(BackRefMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800256{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700257 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
258 shared_ptr<RegexBackrefMatcher> cm = make_shared<RegexBackrefMatcher>("(<a><b>)", backRef);
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700259 backRef->pushRef(static_pointer_cast<RegexMatcher>(cm));
Yingdi Yu5e974202014-01-29 16:59:06 -0800260 cm->lateCompile();
261 bool res = cm->match(Name("/a/b/c"), 0, 2);
262 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700263 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700264 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
265 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800266 BOOST_CHECK_EQUAL(backRef->size(), 1);
267
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700268 backRef = make_shared<RegexBackrefManager>();
269 cm = make_shared<RegexBackrefMatcher>("(<a>(<b>))", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800270 backRef->pushRef(cm);
271 cm->lateCompile();
272 res = cm->match(Name("/a/b/c"), 0, 2);
273 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700274 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700275 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
276 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800277 BOOST_CHECK_EQUAL(backRef->size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700278 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("a"));
279 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[1].toUri(), string("b"));
280 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800281}
282
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700283BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800284{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700285 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
286 shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("([<a><b>])+", backRef, 10);
Yingdi Yu5e974202014-01-29 16:59:06 -0800287 bool 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(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700293 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800294}
295
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700296BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced2)
Yingdi Yu5e974202014-01-29 16:59:06 -0800297{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700298 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
299 shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("(<a>(<b>))<c>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800300 bool res = cm->match(Name("/a/b/c"), 0, 3);
301 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700302 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700303 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
304 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
305 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800306 BOOST_CHECK_EQUAL(backRef->size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700307 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("a"));
308 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[1].toUri(), string("b"));
309 BOOST_CHECK_EQUAL(backRef->getBackref(1)->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(PatternListMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800313{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700314 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
315 shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("<a>[<a><b>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800316 bool res = cm->match(Name("/a/b/c"), 0, 2);
317 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700318 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700319 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
320 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800321
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700322 backRef = make_shared<RegexBackrefManager>();
323 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800324 res = cm->match(Name("/a/b/c"), 0, 1);
325 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700326 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700327 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800328
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700329 backRef = make_shared<RegexBackrefManager>();
330 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800331 res = cm->match(Name("/a/b/c"), 0, 2);
332 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700333 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800334
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700335 backRef = make_shared<RegexBackrefManager>();
336 cm = make_shared<RegexPatternListMatcher>("<>*<a><>*", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800337 res = cm->match(Name("/a/b/c"), 0, 3);
338 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700339 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700340 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
341 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
342 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800343}
344
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700345BOOST_AUTO_TEST_CASE(TopMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800346{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700347 shared_ptr<RegexTopMatcher> cm = make_shared<RegexTopMatcher>("^<a><b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800348 bool res = cm->match(Name("/a/b/c/d"));
349 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700350 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700351 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
352 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
353 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
354 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800355
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700356 cm = make_shared<RegexTopMatcher>("<b><c><d>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800357 res = cm->match(Name("/a/b/c/d"));
358 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700359 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
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"));
363 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800364
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700365 cm = make_shared<RegexTopMatcher>("^<a><b><c><d>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800366 res = cm->match(Name("/a/b/c/d"));
367 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700368 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700369 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
370 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
371 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
372 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800373
374 res = cm->match(Name("/a/b/c/d/e"));
375 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700376 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800377
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700378 cm = make_shared<RegexTopMatcher>("<a><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>("<b><c>");
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
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700397BOOST_AUTO_TEST_CASE(TopMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800398{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700399 shared_ptr<Regex> cm = make_shared<Regex>("^(<.*>*)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800400 bool res = cm->match(Name("/n/a/b/c"));
401 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700402 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800403 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
404
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700405 cm = make_shared<Regex>("^(<.*>*)<.*><c>(<.*>)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800406 res = cm->match(Name("/n/a/b/c/d/e/"));
407 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700408 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800409 BOOST_CHECK_EQUAL(cm->expand("\\1\\2"), Name("/n/a/d/"));
410
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700411 cm = make_shared<Regex>("(<.*>*)<.*>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800412 res = cm->match(Name("/n/a/b/c/"));
413 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700414 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800415 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
416
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700417 cm = make_shared<Regex>("<.*>(<.*>*)<.*>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800418 res = cm->match(Name("/n/a/b/c/"));
419 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700420 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800421 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/a/b/"));
422
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700423 cm = make_shared<Regex>("<a>(<>*)<>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800424 res = cm->match(Name("/n/a/b/c/"));
425 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700426 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800427 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/b/"));
428
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700429 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800430 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
431 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700432 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800433 BOOST_CHECK_EQUAL(cm->expand("<ndn>\\2\\1\\3"), Name("/ndn/edu/ucla/yingdi/mac/"));
434
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700435 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>", "<ndn>\\2\\1\\3");
Yingdi Yu5e974202014-01-29 16:59:06 -0800436 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
437 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700438 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800439 BOOST_CHECK_EQUAL(cm->expand(), Name("/ndn/edu/ucla/yingdi/mac/"));
440}
441
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100442BOOST_AUTO_TEST_SUITE_END() // TestRegex
443BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800444
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800445} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800446} // namespace ndn