blob: ca15b668dfad131e204d2e80029568c6901092e8 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shib3d0a8e2017-09-30 13:41:00 +00002/*
Davide Pesaventob08edd82021-02-05 19:41:04 -05003 * Copyright (c) 2013-2021 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
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/util/regex.hpp"
25#include "ndn-cxx/util/regex/regex-backref-manager.hpp"
26#include "ndn-cxx/util/regex/regex-backref-matcher.hpp"
27#include "ndn-cxx/util/regex/regex-component-matcher.hpp"
28#include "ndn-cxx/util/regex/regex-component-set-matcher.hpp"
29#include "ndn-cxx/util/regex/regex-pattern-list-matcher.hpp"
30#include "ndn-cxx/util/regex/regex-repeat-matcher.hpp"
31#include "ndn-cxx/util/regex/regex-top-matcher.hpp"
Yingdi Yu5e974202014-01-29 16:59:06 -080032
Davide Pesavento7e780642018-11-24 15:51:34 -050033#include "tests/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);
Davide Pesavento861e0942021-02-06 01:07:13 -050047 bool res = cm->match(Name("/a/b"), 0, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -080048 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);
Davide Pesavento861e0942021-02-06 01:07:13 -050054 res = cm->match(Name("/a/b"), 1, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -080055 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);
Davide Pesavento861e0942021-02-06 01:07:13 -050060 res = cm->match(Name("/ccc.cd/b"), 0, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -080061 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{
Davide Pesavento861e0942021-02-06 01:07:13 -050072 auto backRef = make_shared<RegexBackrefManager>();
73 auto cm = make_shared<RegexComponentSetMatcher>("<a>", backRef);
74 bool res = cm->match(Name("/a/b"), 0, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -080075 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
Davide Pesavento861e0942021-02-06 01:07:13 -050079 res = cm->match(Name("/a/b"), 1, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -080080 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
Davide Pesavento861e0942021-02-06 01:07:13 -050083 res = cm->match(Name("/a/b"), 0, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -080084 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"));
Davide Pesavento861e0942021-02-06 01:07:13 -0500104
105 backRef = make_shared<RegexBackrefManager>();
106 BOOST_CHECK_THROW(make_shared<RegexComponentSetMatcher>("", backRef), RegexMatcher::Error);
107 BOOST_CHECK_THROW(make_shared<RegexComponentSetMatcher>("(<a><b>)", backRef), RegexMatcher::Error);
108 BOOST_CHECK_THROW(make_shared<RegexComponentSetMatcher>("[<a><b>", backRef), RegexMatcher::Error);
109 BOOST_CHECK_THROW(make_shared<RegexComponentSetMatcher>("<a", backRef), RegexMatcher::Error);
110 BOOST_CHECK_THROW(make_shared<RegexComponentSetMatcher>("<a>b", backRef), RegexMatcher::Error);
111 BOOST_CHECK_THROW(make_shared<RegexComponentSetMatcher>("<><<>", backRef), RegexMatcher::Error);
112 BOOST_CHECK_THROW(make_shared<RegexComponentSetMatcher>("[abc]", backRef), RegexMatcher::Error);
Yingdi Yu5e974202014-01-29 16:59:06 -0800113}
114
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700115BOOST_AUTO_TEST_CASE(RepeatMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800116{
Davide Pesavento861e0942021-02-06 01:07:13 -0500117 auto backRef = make_shared<RegexBackrefManager>();
118 auto cm = make_shared<RegexRepeatMatcher>("[<a><b>]*", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800119 bool res = cm->match(Name("/a/b/c"), 0, 0);
120 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700121 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800122
Junxiao Shib3d0a8e2017-09-30 13:41:00 +0000123 res = cm->match(Name("/a/b/c"), 0, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800124 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700125 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700126 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
127 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800128
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700129 backRef = make_shared<RegexBackrefManager>();
130 cm = make_shared<RegexRepeatMatcher>("[<a><b>]+", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800131 res = cm->match(Name("/a/b/c"), 0, 0);
132 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700133 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800134
135 res = cm->match(Name("/a/b/c"), 0, 2);
136 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700137 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700138 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
139 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800140
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700141 backRef = make_shared<RegexBackrefManager>();
142 cm = make_shared<RegexRepeatMatcher>("<.*>*", backRef, 4);
Davide Pesavento861e0942021-02-06 01:07:13 -0500143 res = cm->match(Name("/a/b/c/d/e/f"), 0, 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800144 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700145 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700146 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
147 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
148 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
149 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
150 BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toUri(), string("e"));
151 BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toUri(), string("f"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800152
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700153 backRef = make_shared<RegexBackrefManager>();
154 cm = make_shared<RegexRepeatMatcher>("<>*", backRef, 2);
Davide Pesavento861e0942021-02-06 01:07:13 -0500155 res = cm->match(Name("/a/b/c/d/e/f"), 0, 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800156 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
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
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700182 backRef = make_shared<RegexBackrefManager>();
183 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{3}", backRef, 8);
Davide Pesavento861e0942021-02-06 01:07:13 -0500184 res = cm->match(Name("/a/b/a/d"), 0, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800185 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700186 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800187
Davide Pesavento861e0942021-02-06 01:07:13 -0500188 res = cm->match(Name("/a/b/a/d"), 0, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800189 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700190 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700191 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
192 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
193 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800194
Davide Pesavento861e0942021-02-06 01:07:13 -0500195 res = cm->match(Name("/a/b/a/d"), 0, 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800196 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700197 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800198
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700199 backRef = make_shared<RegexBackrefManager>();
200 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,3}", backRef, 8);
Davide Pesavento861e0942021-02-06 01:07:13 -0500201 res = cm->match(Name("/a/b/a/d/e"), 0, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800202 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700203 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700204 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
205 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800206
Davide Pesavento861e0942021-02-06 01:07:13 -0500207 res = cm->match(Name("/a/b/a/d/e"), 0, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800208 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700209 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
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"));
212 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800213
Davide Pesavento861e0942021-02-06 01:07:13 -0500214 res = cm->match(Name("/a/b/a/b/e"), 0, 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800215 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700216 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800217
Davide Pesavento861e0942021-02-06 01:07:13 -0500218 res = cm->match(Name("/a/b/a/d/e"), 0, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -0800219 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
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700222 backRef = make_shared<RegexBackrefManager>();
223 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,}", backRef, 8);
Davide Pesavento861e0942021-02-06 01:07:13 -0500224 res = cm->match(Name("/a/b/a/d/e"), 0, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800225 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700226 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700227 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
228 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800229
Davide Pesavento861e0942021-02-06 01:07:13 -0500230 res = cm->match(Name("/a/b/a/b/e"), 0, 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800231 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700232 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700233 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
234 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
235 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
236 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800237
Davide Pesavento861e0942021-02-06 01:07:13 -0500238 res = cm->match(Name("/a/b/a/d/e"), 0, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -0800239 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700240 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800241
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700242 backRef = make_shared<RegexBackrefManager>();
243 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{,2}", backRef, 8);
Davide Pesavento861e0942021-02-06 01:07:13 -0500244 res = cm->match(Name("/a/b/a/b/e"), 0, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800245 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700246 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800247
Davide Pesavento861e0942021-02-06 01:07:13 -0500248 res = cm->match(Name("/a/b/a/b/e"), 0, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800249 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700250 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700251 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
252 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800253
Davide Pesavento861e0942021-02-06 01:07:13 -0500254 res = cm->match(Name("/a/b/a/d/e"), 0, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -0800255 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700256 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700257 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800258
Davide Pesavento861e0942021-02-06 01:07:13 -0500259 res = cm->match(Name("/a/b/a/d/e"), 0, 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800260 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700261 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Davide Pesavento861e0942021-02-06 01:07:13 -0500262
263 backRef = make_shared<RegexBackrefManager>();
264 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("", backRef, 2), RegexMatcher::Error);
265 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>!", backRef, 2), RegexMatcher::Error);
266 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>@", backRef, 2), RegexMatcher::Error);
267 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>##", backRef, 2), RegexMatcher::Error);
268 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>{}", backRef, 2), RegexMatcher::Error);
269 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>{,}", backRef, 2), RegexMatcher::Error);
270 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>1,2", backRef, 2), RegexMatcher::Error);
271 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>{foo,bar}", backRef, 2), RegexMatcher::Error);
272 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>{0x12,0x34}", backRef, 2), RegexMatcher::Error);
273 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>{10,5}", backRef, 2), RegexMatcher::Error);
274 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>{99999999999999999999,}", backRef, 2), RegexMatcher::Error);
275 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>{,99999999999999999999}", backRef, 2), RegexMatcher::Error);
276 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>{1,2,3}", backRef, 2), RegexMatcher::Error);
Yingdi Yu5e974202014-01-29 16:59:06 -0800277}
278
Davide Pesavento861e0942021-02-06 01:07:13 -0500279BOOST_AUTO_TEST_CASE(BackrefMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800280{
Davide Pesavento861e0942021-02-06 01:07:13 -0500281 auto backRef = make_shared<RegexBackrefManager>();
282 auto cm = make_shared<RegexBackrefMatcher>("(<a><b>)", backRef);
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700283 backRef->pushRef(static_pointer_cast<RegexMatcher>(cm));
Davide Pesaventob08edd82021-02-05 19:41:04 -0500284 cm->compile();
Yingdi Yu5e974202014-01-29 16:59:06 -0800285 bool res = cm->match(Name("/a/b/c"), 0, 2);
286 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700287 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Davide Pesavento861e0942021-02-06 01:07:13 -0500288 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), "a");
289 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), "b");
Yingdi Yu5e974202014-01-29 16:59:06 -0800290 BOOST_CHECK_EQUAL(backRef->size(), 1);
291
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700292 backRef = make_shared<RegexBackrefManager>();
293 cm = make_shared<RegexBackrefMatcher>("(<a>(<b>))", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800294 backRef->pushRef(cm);
Davide Pesaventob08edd82021-02-05 19:41:04 -0500295 cm->compile();
Yingdi Yu5e974202014-01-29 16:59:06 -0800296 res = cm->match(Name("/a/b/c"), 0, 2);
297 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700298 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Davide Pesavento861e0942021-02-06 01:07:13 -0500299 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), "a");
300 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), "b");
Yingdi Yu5e974202014-01-29 16:59:06 -0800301 BOOST_CHECK_EQUAL(backRef->size(), 2);
Davide Pesavento861e0942021-02-06 01:07:13 -0500302 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), "a");
303 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[1].toUri(), "b");
304 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), "b");
305
306 backRef = make_shared<RegexBackrefManager>();
307 cm = make_shared<RegexBackrefMatcher>("", backRef);
308 BOOST_CHECK_THROW(cm->compile(), RegexMatcher::Error);
309 cm = make_shared<RegexBackrefMatcher>("(", backRef);
310 BOOST_CHECK_THROW(cm->compile(), RegexMatcher::Error);
311 cm = make_shared<RegexBackrefMatcher>("(<a><b>", backRef);
312 BOOST_CHECK_THROW(cm->compile(), RegexMatcher::Error);
313 cm = make_shared<RegexBackrefMatcher>("[<a><b>)", backRef);
314 BOOST_CHECK_THROW(cm->compile(), RegexMatcher::Error);
Yingdi Yu5e974202014-01-29 16:59:06 -0800315}
316
Davide Pesavento861e0942021-02-06 01:07:13 -0500317BOOST_AUTO_TEST_CASE(BackrefMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800318{
Davide Pesavento861e0942021-02-06 01:07:13 -0500319 auto backRef = make_shared<RegexBackrefManager>();
320 shared_ptr<RegexMatcher> cm = make_shared<RegexRepeatMatcher>("([<a><b>])+", backRef, 10);
Yingdi Yu5e974202014-01-29 16:59:06 -0800321 bool res = cm->match(Name("/a/b/c"), 0, 2);
322 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700323 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Davide Pesavento861e0942021-02-06 01:07:13 -0500324 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), "a");
325 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), "b");
Yingdi Yu5e974202014-01-29 16:59:06 -0800326 BOOST_CHECK_EQUAL(backRef->size(), 1);
Davide Pesavento861e0942021-02-06 01:07:13 -0500327 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), "b");
Yingdi Yu5e974202014-01-29 16:59:06 -0800328
Davide Pesavento861e0942021-02-06 01:07:13 -0500329 backRef = make_shared<RegexBackrefManager>();
330 cm = make_shared<RegexPatternListMatcher>("(<a>(<b>))<c>", backRef);
331 res = cm->match(Name("/a/b/c"), 0, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800332 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700333 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Davide Pesavento861e0942021-02-06 01:07:13 -0500334 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), "a");
335 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), "b");
336 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), "c");
Yingdi Yu5e974202014-01-29 16:59:06 -0800337 BOOST_CHECK_EQUAL(backRef->size(), 2);
Davide Pesavento861e0942021-02-06 01:07:13 -0500338 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), "a");
339 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[1].toUri(), "b");
340 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), "b");
Yingdi Yu5e974202014-01-29 16:59:06 -0800341}
342
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700343BOOST_AUTO_TEST_CASE(PatternListMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800344{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700345 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
346 shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("<a>[<a><b>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800347 bool res = cm->match(Name("/a/b/c"), 0, 2);
348 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700349 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700350 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
351 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800352
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700353 backRef = make_shared<RegexBackrefManager>();
354 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800355 res = cm->match(Name("/a/b/c"), 0, 1);
356 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700357 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700358 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800359
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700360 backRef = make_shared<RegexBackrefManager>();
361 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800362 res = cm->match(Name("/a/b/c"), 0, 2);
363 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700364 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800365
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700366 backRef = make_shared<RegexBackrefManager>();
367 cm = make_shared<RegexPatternListMatcher>("<>*<a><>*", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800368 res = cm->match(Name("/a/b/c"), 0, 3);
369 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700370 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700371 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
372 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
373 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800374}
375
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700376BOOST_AUTO_TEST_CASE(TopMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800377{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700378 shared_ptr<RegexTopMatcher> cm = make_shared<RegexTopMatcher>("^<a><b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800379 bool 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><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
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700396 cm = make_shared<RegexTopMatcher>("^<a><b><c><d>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800397 res = cm->match(Name("/a/b/c/d"));
398 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700399 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700400 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
401 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
402 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
403 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800404
405 res = cm->match(Name("/a/b/c/d/e"));
406 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700407 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800408
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700409 cm = make_shared<RegexTopMatcher>("<a><b><c><d>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800410 res = cm->match(Name("/a/b/c/d"));
411 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700412 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700413 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
414 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
415 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
416 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800417
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700418 cm = make_shared<RegexTopMatcher>("<b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800419 res = cm->match(Name("/a/b/c/d"));
420 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700421 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700422 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
423 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
424 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
425 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800426}
427
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700428BOOST_AUTO_TEST_CASE(TopMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800429{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700430 shared_ptr<Regex> cm = make_shared<Regex>("^(<.*>*)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800431 bool res = cm->match(Name("/n/a/b/c"));
432 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700433 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800434 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
435
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700436 cm = make_shared<Regex>("^(<.*>*)<.*><c>(<.*>)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800437 res = cm->match(Name("/n/a/b/c/d/e/"));
438 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700439 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800440 BOOST_CHECK_EQUAL(cm->expand("\\1\\2"), Name("/n/a/d/"));
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("/n/a/b/"));
447
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700448 cm = make_shared<Regex>("<.*>(<.*>*)<.*>$");
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("/a/b/"));
453
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700454 cm = make_shared<Regex>("<a>(<>*)<>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800455 res = cm->match(Name("/n/a/b/c/"));
456 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700457 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800458 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/b/"));
459
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700460 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>");
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("<ndn>\\2\\1\\3"), Name("/ndn/edu/ucla/yingdi/mac/"));
465
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700466 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>", "<ndn>\\2\\1\\3");
Yingdi Yu5e974202014-01-29 16:59:06 -0800467 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
468 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700469 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800470 BOOST_CHECK_EQUAL(cm->expand(), Name("/ndn/edu/ucla/yingdi/mac/"));
471}
472
Junxiao Shi70f95fa2016-11-25 03:04:39 +0000473BOOST_AUTO_TEST_CASE(RegexBackrefManagerMemoryLeak)
474{
475 auto re = make_unique<Regex>("^(<>)$");
476
477 weak_ptr<RegexPatternListMatcher> m1(re->m_primaryMatcher);
478 weak_ptr<RegexPatternListMatcher> m2(re->m_secondaryMatcher);
479 weak_ptr<RegexBackrefManager> b1(re->m_primaryBackrefManager);
480 weak_ptr<RegexBackrefManager> b2(re->m_secondaryBackrefManager);
481
482 re.reset();
483
484 BOOST_CHECK_EQUAL(m1.use_count(), 0);
485 BOOST_CHECK_EQUAL(m2.use_count(), 0);
486 BOOST_CHECK_EQUAL(b1.use_count(), 0);
487 BOOST_CHECK_EQUAL(b2.use_count(), 0);
488}
489
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100490BOOST_AUTO_TEST_SUITE_END() // TestRegex
491BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800492
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800493} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800494} // namespace ndn