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