Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | b3d0a8e | 2017-09-30 13:41:00 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 32 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 33 | #include "tests/boost-test.hpp" |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 34 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 35 | namespace ndn::tests { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 36 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 37 | using std::string; |
| 38 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 39 | BOOST_AUTO_TEST_SUITE(Util) |
| 40 | BOOST_AUTO_TEST_SUITE(TestRegex) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 41 | |
| 42 | BOOST_AUTO_TEST_CASE(ComponentMatcher) |
| 43 | { |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 44 | shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>(); |
| 45 | shared_ptr<RegexComponentMatcher> cm = make_shared<RegexComponentMatcher>("a", backRef); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 46 | bool res = cm->match(Name("/a/b"), 0, 1); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 49 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 51 | backRef = make_shared<RegexBackrefManager>(); |
| 52 | cm = make_shared<RegexComponentMatcher>("a", backRef); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 53 | res = cm->match(Name("/a/b"), 1, 1); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 54 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 57 | backRef = make_shared<RegexBackrefManager>(); |
| 58 | cm = make_shared<RegexComponentMatcher>("(c+)\\.(cd)", backRef); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 59 | res = cm->match(Name("/ccc.cd/b"), 0, 1); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 60 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 61 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 62 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("ccc.cd")); |
Alexander Afanasyev | 680e77a | 2014-08-14 22:39:17 -0700 | [diff] [blame] | 63 | |
| 64 | BOOST_REQUIRE_EQUAL(backRef->size(), 2); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("ccc")); |
| 66 | BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("cd")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 69 | BOOST_AUTO_TEST_CASE(ComponentSetMatcher) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 70 | { |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 71 | auto backRef = make_shared<RegexBackrefManager>(); |
| 72 | auto cm = make_shared<RegexComponentSetMatcher>("<a>", backRef); |
| 73 | bool res = cm->match(Name("/a/b"), 0, 1); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a")); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 77 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 78 | res = cm->match(Name("/a/b"), 1, 1); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 79 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 80 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 81 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 82 | res = cm->match(Name("/a/b"), 0, 2); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 85 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 86 | backRef = make_shared<RegexBackrefManager>(); |
| 87 | cm = make_shared<RegexComponentSetMatcher>("[<a><b><c>]", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 88 | res = cm->match(Name("/a/b/d"), 1, 1); |
| 89 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 90 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 91 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 92 | |
| 93 | res = cm->match(Name("/a/b/d"), 2, 1); |
| 94 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 96 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 97 | backRef = make_shared<RegexBackrefManager>(); |
| 98 | cm = make_shared<RegexComponentSetMatcher>("[^<a><b><c>]", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 99 | res = cm->match(Name("/b/d"), 1, 1); |
| 100 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("d")); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 103 | |
| 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 114 | BOOST_AUTO_TEST_CASE(RepeatMatcher) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 115 | { |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 116 | auto backRef = make_shared<RegexBackrefManager>(); |
| 117 | auto cm = make_shared<RegexRepeatMatcher>("[<a><b>]*", backRef, 8); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 118 | bool res = cm->match(Name("/a/b/c"), 0, 0); |
| 119 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 121 | |
Junxiao Shi | b3d0a8e | 2017-09-30 13:41:00 +0000 | [diff] [blame] | 122 | res = cm->match(Name("/a/b/c"), 0, 2); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 125 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a")); |
| 126 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 127 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 128 | backRef = make_shared<RegexBackrefManager>(); |
| 129 | cm = make_shared<RegexRepeatMatcher>("[<a><b>]+", backRef, 8); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 130 | res = cm->match(Name("/a/b/c"), 0, 0); |
| 131 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 132 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 133 | |
| 134 | res = cm->match(Name("/a/b/c"), 0, 2); |
| 135 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a")); |
| 138 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 140 | backRef = make_shared<RegexBackrefManager>(); |
| 141 | cm = make_shared<RegexRepeatMatcher>("<.*>*", backRef, 4); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 142 | res = cm->match(Name("/a/b/c/d/e/f"), 0, 6); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 144 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 145 | 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 151 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 152 | backRef = make_shared<RegexBackrefManager>(); |
| 153 | cm = make_shared<RegexRepeatMatcher>("<>*", backRef, 2); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 154 | res = cm->match(Name("/a/b/c/d/e/f"), 0, 6); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 155 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 157 | 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 163 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 164 | backRef = make_shared<RegexBackrefManager>(); |
| 165 | cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 166 | res = cm->match(Name("/a/b/c"), 0, 0); |
| 167 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 168 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 169 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 170 | cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 171 | res = cm->match(Name("/a/b/c"), 0, 1); |
| 172 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 173 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 174 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 175 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 176 | cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 177 | res = cm->match(Name("/a/b/c"), 0, 2); |
| 178 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 179 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 180 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 181 | backRef = make_shared<RegexBackrefManager>(); |
| 182 | cm = make_shared<RegexRepeatMatcher>("[<a><b>]{3}", backRef, 8); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 183 | res = cm->match(Name("/a/b/a/d"), 0, 2); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 184 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 185 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 186 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 187 | res = cm->match(Name("/a/b/a/d"), 0, 3); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 188 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 189 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 190 | 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 193 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 194 | res = cm->match(Name("/a/b/a/d"), 0, 4); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 195 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 196 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 197 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 198 | backRef = make_shared<RegexBackrefManager>(); |
| 199 | cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,3}", backRef, 8); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 200 | res = cm->match(Name("/a/b/a/d/e"), 0, 2); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 201 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 202 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 203 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a")); |
| 204 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 205 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 206 | res = cm->match(Name("/a/b/a/d/e"), 0, 3); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 207 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 208 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 209 | 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 212 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 213 | res = cm->match(Name("/a/b/a/b/e"), 0, 4); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 214 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 215 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 216 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 217 | res = cm->match(Name("/a/b/a/d/e"), 0, 1); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 218 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 219 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 220 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 221 | backRef = make_shared<RegexBackrefManager>(); |
| 222 | cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,}", backRef, 8); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 223 | res = cm->match(Name("/a/b/a/d/e"), 0, 2); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 224 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 225 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 226 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a")); |
| 227 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 228 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 229 | res = cm->match(Name("/a/b/a/b/e"), 0, 4); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 230 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 231 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 232 | 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 236 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 237 | res = cm->match(Name("/a/b/a/d/e"), 0, 1); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 238 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 239 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 240 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 241 | backRef = make_shared<RegexBackrefManager>(); |
| 242 | cm = make_shared<RegexRepeatMatcher>("[<a><b>]{,2}", backRef, 8); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 243 | res = cm->match(Name("/a/b/a/b/e"), 0, 3); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 244 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 245 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 246 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 247 | res = cm->match(Name("/a/b/a/b/e"), 0, 2); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 248 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 249 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 250 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a")); |
| 251 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 252 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 253 | res = cm->match(Name("/a/b/a/d/e"), 0, 1); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 254 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 255 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 256 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 257 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 258 | res = cm->match(Name("/a/b/a/d/e"), 0, 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 259 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 260 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 261 | |
| 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 278 | BOOST_AUTO_TEST_CASE(BackrefMatcher) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 279 | { |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 280 | auto backRef = make_shared<RegexBackrefManager>(); |
| 281 | auto cm = make_shared<RegexBackrefMatcher>("(<a><b>)", backRef); |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 282 | backRef->pushRef(static_pointer_cast<RegexMatcher>(cm)); |
Davide Pesavento | b08edd8 | 2021-02-05 19:41:04 -0500 | [diff] [blame] | 283 | cm->compile(); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 284 | bool res = cm->match(Name("/a/b/c"), 0, 2); |
| 285 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 286 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 287 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), "a"); |
| 288 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), "b"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 289 | BOOST_CHECK_EQUAL(backRef->size(), 1); |
| 290 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 291 | backRef = make_shared<RegexBackrefManager>(); |
| 292 | cm = make_shared<RegexBackrefMatcher>("(<a>(<b>))", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 293 | backRef->pushRef(cm); |
Davide Pesavento | b08edd8 | 2021-02-05 19:41:04 -0500 | [diff] [blame] | 294 | cm->compile(); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 295 | res = cm->match(Name("/a/b/c"), 0, 2); |
| 296 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 297 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 298 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), "a"); |
| 299 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), "b"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 300 | BOOST_CHECK_EQUAL(backRef->size(), 2); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 301 | 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 316 | BOOST_AUTO_TEST_CASE(BackrefMatcherAdvanced) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 317 | { |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 318 | auto backRef = make_shared<RegexBackrefManager>(); |
| 319 | shared_ptr<RegexMatcher> cm = make_shared<RegexRepeatMatcher>("([<a><b>])+", backRef, 10); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 320 | bool res = cm->match(Name("/a/b/c"), 0, 2); |
| 321 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 322 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 323 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), "a"); |
| 324 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), "b"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 325 | BOOST_CHECK_EQUAL(backRef->size(), 1); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 326 | BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), "b"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 327 | |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 328 | backRef = make_shared<RegexBackrefManager>(); |
| 329 | cm = make_shared<RegexPatternListMatcher>("(<a>(<b>))<c>", backRef); |
| 330 | res = cm->match(Name("/a/b/c"), 0, 3); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 331 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 332 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 333 | 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 336 | BOOST_CHECK_EQUAL(backRef->size(), 2); |
Davide Pesavento | 861e094 | 2021-02-06 01:07:13 -0500 | [diff] [blame] | 337 | 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 342 | BOOST_AUTO_TEST_CASE(PatternListMatcher) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 343 | { |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 344 | shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>(); |
| 345 | shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("<a>[<a><b>]", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 346 | bool res = cm->match(Name("/a/b/c"), 0, 2); |
| 347 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 348 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 349 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a")); |
| 350 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 351 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 352 | backRef = make_shared<RegexBackrefManager>(); |
| 353 | cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 354 | res = cm->match(Name("/a/b/c"), 0, 1); |
| 355 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 356 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 357 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 358 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 359 | backRef = make_shared<RegexBackrefManager>(); |
| 360 | cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 361 | res = cm->match(Name("/a/b/c"), 0, 2); |
| 362 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 363 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 364 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 365 | backRef = make_shared<RegexBackrefManager>(); |
| 366 | cm = make_shared<RegexPatternListMatcher>("<>*<a><>*", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 367 | res = cm->match(Name("/a/b/c"), 0, 3); |
| 368 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 369 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 370 | 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 375 | BOOST_AUTO_TEST_CASE(TopMatcher) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 376 | { |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 377 | shared_ptr<RegexTopMatcher> cm = make_shared<RegexTopMatcher>("^<a><b><c>"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 378 | bool res = cm->match(Name("/a/b/c/d")); |
| 379 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 380 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 381 | 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 385 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 386 | cm = make_shared<RegexTopMatcher>("<b><c><d>$"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 387 | res = cm->match(Name("/a/b/c/d")); |
| 388 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 389 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 390 | 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 394 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 395 | cm = make_shared<RegexTopMatcher>("^<a><b><c><d>$"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 396 | res = cm->match(Name("/a/b/c/d")); |
| 397 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 398 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 399 | 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 403 | |
| 404 | res = cm->match(Name("/a/b/c/d/e")); |
| 405 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 406 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 407 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 408 | cm = make_shared<RegexTopMatcher>("<a><b><c><d>"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 409 | res = cm->match(Name("/a/b/c/d")); |
| 410 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 411 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 412 | 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 416 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 417 | cm = make_shared<RegexTopMatcher>("<b><c>"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 418 | res = cm->match(Name("/a/b/c/d")); |
| 419 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 420 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 421 | 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 Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 427 | BOOST_AUTO_TEST_CASE(TopMatcherAdvanced) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 428 | { |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 429 | shared_ptr<Regex> cm = make_shared<Regex>("^(<.*>*)<.*>"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 430 | bool res = cm->match(Name("/n/a/b/c")); |
| 431 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 432 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 433 | BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/")); |
| 434 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 435 | cm = make_shared<Regex>("^(<.*>*)<.*><c>(<.*>)<.*>"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 436 | res = cm->match(Name("/n/a/b/c/d/e/")); |
| 437 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 438 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 439 | BOOST_CHECK_EQUAL(cm->expand("\\1\\2"), Name("/n/a/d/")); |
| 440 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 441 | cm = make_shared<Regex>("(<.*>*)<.*>$"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 442 | res = cm->match(Name("/n/a/b/c/")); |
| 443 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 444 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 445 | BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/")); |
| 446 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 447 | cm = make_shared<Regex>("<.*>(<.*>*)<.*>$"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 448 | res = cm->match(Name("/n/a/b/c/")); |
| 449 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 450 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 451 | BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/a/b/")); |
| 452 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 453 | cm = make_shared<Regex>("<a>(<>*)<>$"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 454 | res = cm->match(Name("/n/a/b/c/")); |
| 455 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 456 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 457 | BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/b/")); |
| 458 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 459 | cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 460 | res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/")); |
| 461 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 462 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 463 | BOOST_CHECK_EQUAL(cm->expand("<ndn>\\2\\1\\3"), Name("/ndn/edu/ucla/yingdi/mac/")); |
| 464 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 465 | cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>", "<ndn>\\2\\1\\3"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 466 | res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/")); |
| 467 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 468 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 469 | BOOST_CHECK_EQUAL(cm->expand(), Name("/ndn/edu/ucla/yingdi/mac/")); |
| 470 | } |
| 471 | |
Junxiao Shi | 70f95fa | 2016-11-25 03:04:39 +0000 | [diff] [blame] | 472 | BOOST_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 Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 489 | BOOST_AUTO_TEST_SUITE_END() // TestRegex |
| 490 | BOOST_AUTO_TEST_SUITE_END() // Util |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 491 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 492 | } // namespace ndn::tests |