blob: d09688e20c44cc79d1024266e1271a891784c0b7 [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 Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040035namespace ndn::tests {
Yingdi Yu5e974202014-01-29 16:59:06 -080036
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080037using std::string;
38
Davide Pesaventoeee3e822016-11-26 19:19:34 +010039BOOST_AUTO_TEST_SUITE(Util)
40BOOST_AUTO_TEST_SUITE(TestRegex)
Yingdi Yu5e974202014-01-29 16:59:06 -080041
42BOOST_AUTO_TEST_CASE(ComponentMatcher)
43{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070044 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
45 shared_ptr<RegexComponentMatcher> cm = make_shared<RegexComponentMatcher>("a", backRef);
Davide Pesavento861e0942021-02-06 01:07:13 -050046 bool res = cm->match(Name("/a/b"), 0, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -080047 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070048 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070049 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -080050
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070051 backRef = make_shared<RegexBackrefManager>();
52 cm = make_shared<RegexComponentMatcher>("a", backRef);
Davide Pesavento861e0942021-02-06 01:07:13 -050053 res = cm->match(Name("/a/b"), 1, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -080054 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070055 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -080056
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070057 backRef = make_shared<RegexBackrefManager>();
58 cm = make_shared<RegexComponentMatcher>("(c+)\\.(cd)", backRef);
Davide Pesavento861e0942021-02-06 01:07:13 -050059 res = cm->match(Name("/ccc.cd/b"), 0, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -080060 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070061 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070062 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("ccc.cd"));
Alexander Afanasyev680e77a2014-08-14 22:39:17 -070063
64 BOOST_REQUIRE_EQUAL(backRef->size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070065 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("ccc"));
66 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("cd"));
Yingdi Yu5e974202014-01-29 16:59:06 -080067}
68
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070069BOOST_AUTO_TEST_CASE(ComponentSetMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -080070{
Davide Pesavento861e0942021-02-06 01:07:13 -050071 auto backRef = make_shared<RegexBackrefManager>();
72 auto cm = make_shared<RegexComponentSetMatcher>("<a>", backRef);
73 bool res = cm->match(Name("/a/b"), 0, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -080074 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070075 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070076 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070077
Davide Pesavento861e0942021-02-06 01:07:13 -050078 res = cm->match(Name("/a/b"), 1, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -080079 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070080 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070081
Davide Pesavento861e0942021-02-06 01:07:13 -050082 res = cm->match(Name("/a/b"), 0, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -080083 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070084 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -080085
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070086 backRef = make_shared<RegexBackrefManager>();
87 cm = make_shared<RegexComponentSetMatcher>("[<a><b><c>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080088 res = cm->match(Name("/a/b/d"), 1, 1);
89 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070090 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -070091 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -080092
93 res = cm->match(Name("/a/b/d"), 2, 1);
94 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070095 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070096
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070097 backRef = make_shared<RegexBackrefManager>();
98 cm = make_shared<RegexComponentSetMatcher>("[^<a><b><c>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080099 res = cm->match(Name("/b/d"), 1, 1);
100 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700101 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700102 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("d"));
Davide Pesavento861e0942021-02-06 01:07:13 -0500103
104 backRef = make_shared<RegexBackrefManager>();
105 BOOST_CHECK_THROW(make_shared<RegexComponentSetMatcher>("", backRef), RegexMatcher::Error);
106 BOOST_CHECK_THROW(make_shared<RegexComponentSetMatcher>("(<a><b>)", backRef), RegexMatcher::Error);
107 BOOST_CHECK_THROW(make_shared<RegexComponentSetMatcher>("[<a><b>", backRef), RegexMatcher::Error);
108 BOOST_CHECK_THROW(make_shared<RegexComponentSetMatcher>("<a", backRef), RegexMatcher::Error);
109 BOOST_CHECK_THROW(make_shared<RegexComponentSetMatcher>("<a>b", backRef), RegexMatcher::Error);
110 BOOST_CHECK_THROW(make_shared<RegexComponentSetMatcher>("<><<>", backRef), RegexMatcher::Error);
111 BOOST_CHECK_THROW(make_shared<RegexComponentSetMatcher>("[abc]", backRef), RegexMatcher::Error);
Yingdi Yu5e974202014-01-29 16:59:06 -0800112}
113
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700114BOOST_AUTO_TEST_CASE(RepeatMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800115{
Davide Pesavento861e0942021-02-06 01:07:13 -0500116 auto backRef = make_shared<RegexBackrefManager>();
117 auto cm = make_shared<RegexRepeatMatcher>("[<a><b>]*", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800118 bool res = cm->match(Name("/a/b/c"), 0, 0);
119 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700120 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800121
Junxiao Shib3d0a8e2017-09-30 13:41:00 +0000122 res = cm->match(Name("/a/b/c"), 0, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800123 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700124 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700125 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
126 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800127
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700128 backRef = make_shared<RegexBackrefManager>();
129 cm = make_shared<RegexRepeatMatcher>("[<a><b>]+", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800130 res = cm->match(Name("/a/b/c"), 0, 0);
131 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700132 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800133
134 res = cm->match(Name("/a/b/c"), 0, 2);
135 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700136 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
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"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800139
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700140 backRef = make_shared<RegexBackrefManager>();
141 cm = make_shared<RegexRepeatMatcher>("<.*>*", backRef, 4);
Davide Pesavento861e0942021-02-06 01:07:13 -0500142 res = cm->match(Name("/a/b/c/d/e/f"), 0, 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800143 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700144 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700145 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
146 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
147 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
148 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
149 BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toUri(), string("e"));
150 BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toUri(), string("f"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800151
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700152 backRef = make_shared<RegexBackrefManager>();
153 cm = make_shared<RegexRepeatMatcher>("<>*", backRef, 2);
Davide Pesavento861e0942021-02-06 01:07:13 -0500154 res = cm->match(Name("/a/b/c/d/e/f"), 0, 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800155 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700156 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700157 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
158 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
159 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
160 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
161 BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toUri(), string("e"));
162 BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toUri(), string("f"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800163
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700164 backRef = make_shared<RegexBackrefManager>();
165 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800166 res = cm->match(Name("/a/b/c"), 0, 0);
167 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700168 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800169
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700170 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800171 res = cm->match(Name("/a/b/c"), 0, 1);
172 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700173 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700174 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800175
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700176 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800177 res = cm->match(Name("/a/b/c"), 0, 2);
178 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700179 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800180
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700181 backRef = make_shared<RegexBackrefManager>();
182 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{3}", backRef, 8);
Davide Pesavento861e0942021-02-06 01:07:13 -0500183 res = cm->match(Name("/a/b/a/d"), 0, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800184 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700185 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800186
Davide Pesavento861e0942021-02-06 01:07:13 -0500187 res = cm->match(Name("/a/b/a/d"), 0, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800188 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700189 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700190 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
191 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
192 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800193
Davide Pesavento861e0942021-02-06 01:07:13 -0500194 res = cm->match(Name("/a/b/a/d"), 0, 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800195 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700196 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800197
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700198 backRef = make_shared<RegexBackrefManager>();
199 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,3}", backRef, 8);
Davide Pesavento861e0942021-02-06 01:07:13 -0500200 res = cm->match(Name("/a/b/a/d/e"), 0, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800201 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700202 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700203 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
204 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800205
Davide Pesavento861e0942021-02-06 01:07:13 -0500206 res = cm->match(Name("/a/b/a/d/e"), 0, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800207 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700208 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700209 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
210 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
211 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800212
Davide Pesavento861e0942021-02-06 01:07:13 -0500213 res = cm->match(Name("/a/b/a/b/e"), 0, 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800214 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700215 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800216
Davide Pesavento861e0942021-02-06 01:07:13 -0500217 res = cm->match(Name("/a/b/a/d/e"), 0, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -0800218 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700219 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800220
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700221 backRef = make_shared<RegexBackrefManager>();
222 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,}", backRef, 8);
Davide Pesavento861e0942021-02-06 01:07:13 -0500223 res = cm->match(Name("/a/b/a/d/e"), 0, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800224 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700225 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700226 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
227 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800228
Davide Pesavento861e0942021-02-06 01:07:13 -0500229 res = cm->match(Name("/a/b/a/b/e"), 0, 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800230 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700231 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700232 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
233 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
234 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
235 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800236
Davide Pesavento861e0942021-02-06 01:07:13 -0500237 res = cm->match(Name("/a/b/a/d/e"), 0, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -0800238 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700239 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800240
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700241 backRef = make_shared<RegexBackrefManager>();
242 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{,2}", backRef, 8);
Davide Pesavento861e0942021-02-06 01:07:13 -0500243 res = cm->match(Name("/a/b/a/b/e"), 0, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800244 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700245 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800246
Davide Pesavento861e0942021-02-06 01:07:13 -0500247 res = cm->match(Name("/a/b/a/b/e"), 0, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800248 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700249 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700250 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
251 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800252
Davide Pesavento861e0942021-02-06 01:07:13 -0500253 res = cm->match(Name("/a/b/a/d/e"), 0, 1);
Yingdi Yu5e974202014-01-29 16:59:06 -0800254 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700255 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700256 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800257
Davide Pesavento861e0942021-02-06 01:07:13 -0500258 res = cm->match(Name("/a/b/a/d/e"), 0, 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800259 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700260 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Davide Pesavento861e0942021-02-06 01:07:13 -0500261
262 backRef = make_shared<RegexBackrefManager>();
263 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("", backRef, 2), RegexMatcher::Error);
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>("<>1,2", backRef, 2), RegexMatcher::Error);
270 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>{foo,bar}", backRef, 2), RegexMatcher::Error);
271 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>{0x12,0x34}", backRef, 2), RegexMatcher::Error);
272 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>{10,5}", backRef, 2), RegexMatcher::Error);
273 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>{99999999999999999999,}", backRef, 2), RegexMatcher::Error);
274 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>{,99999999999999999999}", backRef, 2), RegexMatcher::Error);
275 BOOST_CHECK_THROW(make_shared<RegexRepeatMatcher>("<>{1,2,3}", backRef, 2), RegexMatcher::Error);
Yingdi Yu5e974202014-01-29 16:59:06 -0800276}
277
Davide Pesavento861e0942021-02-06 01:07:13 -0500278BOOST_AUTO_TEST_CASE(BackrefMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800279{
Davide Pesavento861e0942021-02-06 01:07:13 -0500280 auto backRef = make_shared<RegexBackrefManager>();
281 auto cm = make_shared<RegexBackrefMatcher>("(<a><b>)", backRef);
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700282 backRef->pushRef(static_pointer_cast<RegexMatcher>(cm));
Davide Pesaventob08edd82021-02-05 19:41:04 -0500283 cm->compile();
Yingdi Yu5e974202014-01-29 16:59:06 -0800284 bool res = cm->match(Name("/a/b/c"), 0, 2);
285 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700286 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Davide Pesavento861e0942021-02-06 01:07:13 -0500287 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), "a");
288 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), "b");
Yingdi Yu5e974202014-01-29 16:59:06 -0800289 BOOST_CHECK_EQUAL(backRef->size(), 1);
290
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700291 backRef = make_shared<RegexBackrefManager>();
292 cm = make_shared<RegexBackrefMatcher>("(<a>(<b>))", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800293 backRef->pushRef(cm);
Davide Pesaventob08edd82021-02-05 19:41:04 -0500294 cm->compile();
Yingdi Yu5e974202014-01-29 16:59:06 -0800295 res = cm->match(Name("/a/b/c"), 0, 2);
296 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700297 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Davide Pesavento861e0942021-02-06 01:07:13 -0500298 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), "a");
299 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), "b");
Yingdi Yu5e974202014-01-29 16:59:06 -0800300 BOOST_CHECK_EQUAL(backRef->size(), 2);
Davide Pesavento861e0942021-02-06 01:07:13 -0500301 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), "a");
302 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[1].toUri(), "b");
303 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), "b");
304
305 backRef = make_shared<RegexBackrefManager>();
306 cm = make_shared<RegexBackrefMatcher>("", backRef);
307 BOOST_CHECK_THROW(cm->compile(), RegexMatcher::Error);
308 cm = make_shared<RegexBackrefMatcher>("(", backRef);
309 BOOST_CHECK_THROW(cm->compile(), RegexMatcher::Error);
310 cm = make_shared<RegexBackrefMatcher>("(<a><b>", backRef);
311 BOOST_CHECK_THROW(cm->compile(), RegexMatcher::Error);
312 cm = make_shared<RegexBackrefMatcher>("[<a><b>)", backRef);
313 BOOST_CHECK_THROW(cm->compile(), RegexMatcher::Error);
Yingdi Yu5e974202014-01-29 16:59:06 -0800314}
315
Davide Pesavento861e0942021-02-06 01:07:13 -0500316BOOST_AUTO_TEST_CASE(BackrefMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800317{
Davide Pesavento861e0942021-02-06 01:07:13 -0500318 auto backRef = make_shared<RegexBackrefManager>();
319 shared_ptr<RegexMatcher> cm = make_shared<RegexRepeatMatcher>("([<a><b>])+", backRef, 10);
Yingdi Yu5e974202014-01-29 16:59:06 -0800320 bool res = cm->match(Name("/a/b/c"), 0, 2);
321 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700322 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Davide Pesavento861e0942021-02-06 01:07:13 -0500323 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), "a");
324 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), "b");
Yingdi Yu5e974202014-01-29 16:59:06 -0800325 BOOST_CHECK_EQUAL(backRef->size(), 1);
Davide Pesavento861e0942021-02-06 01:07:13 -0500326 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), "b");
Yingdi Yu5e974202014-01-29 16:59:06 -0800327
Davide Pesavento861e0942021-02-06 01:07:13 -0500328 backRef = make_shared<RegexBackrefManager>();
329 cm = make_shared<RegexPatternListMatcher>("(<a>(<b>))<c>", backRef);
330 res = cm->match(Name("/a/b/c"), 0, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800331 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700332 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Davide Pesavento861e0942021-02-06 01:07:13 -0500333 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), "a");
334 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), "b");
335 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), "c");
Yingdi Yu5e974202014-01-29 16:59:06 -0800336 BOOST_CHECK_EQUAL(backRef->size(), 2);
Davide Pesavento861e0942021-02-06 01:07:13 -0500337 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), "a");
338 BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[1].toUri(), "b");
339 BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), "b");
Yingdi Yu5e974202014-01-29 16:59:06 -0800340}
341
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700342BOOST_AUTO_TEST_CASE(PatternListMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800343{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700344 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
345 shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("<a>[<a><b>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800346 bool res = cm->match(Name("/a/b/c"), 0, 2);
347 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700348 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700349 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
350 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800351
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700352 backRef = make_shared<RegexBackrefManager>();
353 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800354 res = cm->match(Name("/a/b/c"), 0, 1);
355 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700356 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700357 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800358
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700359 backRef = make_shared<RegexBackrefManager>();
360 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800361 res = cm->match(Name("/a/b/c"), 0, 2);
362 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700363 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800364
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700365 backRef = make_shared<RegexBackrefManager>();
366 cm = make_shared<RegexPatternListMatcher>("<>*<a><>*", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800367 res = cm->match(Name("/a/b/c"), 0, 3);
368 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700369 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700370 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
371 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
372 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800373}
374
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700375BOOST_AUTO_TEST_CASE(TopMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800376{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700377 shared_ptr<RegexTopMatcher> cm = make_shared<RegexTopMatcher>("^<a><b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800378 bool res = cm->match(Name("/a/b/c/d"));
379 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700380 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700381 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
382 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
383 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
384 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800385
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700386 cm = make_shared<RegexTopMatcher>("<b><c><d>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800387 res = cm->match(Name("/a/b/c/d"));
388 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700389 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700390 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
391 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
392 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
393 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800394
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700395 cm = make_shared<RegexTopMatcher>("^<a><b><c><d>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800396 res = cm->match(Name("/a/b/c/d"));
397 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700398 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700399 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
400 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
401 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
402 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800403
404 res = cm->match(Name("/a/b/c/d/e"));
405 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700406 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800407
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700408 cm = make_shared<RegexTopMatcher>("<a><b><c><d>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800409 res = cm->match(Name("/a/b/c/d"));
410 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700411 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700412 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
413 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
414 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
415 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800416
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700417 cm = make_shared<RegexTopMatcher>("<b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800418 res = cm->match(Name("/a/b/c/d"));
419 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700420 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700421 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
422 BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
423 BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
424 BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800425}
426
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700427BOOST_AUTO_TEST_CASE(TopMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800428{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700429 shared_ptr<Regex> cm = make_shared<Regex>("^(<.*>*)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800430 bool res = cm->match(Name("/n/a/b/c"));
431 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700432 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800433 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
434
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700435 cm = make_shared<Regex>("^(<.*>*)<.*><c>(<.*>)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800436 res = cm->match(Name("/n/a/b/c/d/e/"));
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("\\1\\2"), Name("/n/a/d/"));
440
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700441 cm = make_shared<Regex>("(<.*>*)<.*>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800442 res = cm->match(Name("/n/a/b/c/"));
443 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700444 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800445 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
446
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700447 cm = make_shared<Regex>("<.*>(<.*>*)<.*>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800448 res = cm->match(Name("/n/a/b/c/"));
449 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700450 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800451 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/a/b/"));
452
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700453 cm = make_shared<Regex>("<a>(<>*)<>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800454 res = cm->match(Name("/n/a/b/c/"));
455 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700456 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800457 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/b/"));
458
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700459 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800460 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
461 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700462 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800463 BOOST_CHECK_EQUAL(cm->expand("<ndn>\\2\\1\\3"), Name("/ndn/edu/ucla/yingdi/mac/"));
464
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700465 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>", "<ndn>\\2\\1\\3");
Yingdi Yu5e974202014-01-29 16:59:06 -0800466 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
467 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700468 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800469 BOOST_CHECK_EQUAL(cm->expand(), Name("/ndn/edu/ucla/yingdi/mac/"));
470}
471
Junxiao Shi70f95fa2016-11-25 03:04:39 +0000472BOOST_AUTO_TEST_CASE(RegexBackrefManagerMemoryLeak)
473{
474 auto re = make_unique<Regex>("^(<>)$");
475
476 weak_ptr<RegexPatternListMatcher> m1(re->m_primaryMatcher);
477 weak_ptr<RegexPatternListMatcher> m2(re->m_secondaryMatcher);
478 weak_ptr<RegexBackrefManager> b1(re->m_primaryBackrefManager);
479 weak_ptr<RegexBackrefManager> b2(re->m_secondaryBackrefManager);
480
481 re.reset();
482
483 BOOST_CHECK_EQUAL(m1.use_count(), 0);
484 BOOST_CHECK_EQUAL(m2.use_count(), 0);
485 BOOST_CHECK_EQUAL(b1.use_count(), 0);
486 BOOST_CHECK_EQUAL(b2.use_count(), 0);
487}
488
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100489BOOST_AUTO_TEST_SUITE_END() // TestRegex
490BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800491
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400492} // namespace ndn::tests