Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | * |
| 12 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 15 | #include "util/regex/regex-backref-manager.hpp" |
| 16 | #include "util/regex/regex-component-matcher.hpp" |
| 17 | #include "util/regex/regex-component-set-matcher.hpp" |
| 18 | #include "util/regex/regex-pattern-list-matcher.hpp" |
| 19 | #include "util/regex/regex-repeat-matcher.hpp" |
| 20 | #include "util/regex/regex-backref-matcher.hpp" |
| 21 | #include "util/regex/regex-top-matcher.hpp" |
| 22 | #include "util/regex.hpp" |
| 23 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 24 | #include "boost-test.hpp" |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 25 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 26 | using namespace std; |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 27 | namespace ndn { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 29 | BOOST_AUTO_TEST_SUITE(UtilTestRegex) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 30 | |
| 31 | BOOST_AUTO_TEST_CASE(ComponentMatcher) |
| 32 | { |
| 33 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 34 | shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>(); |
| 35 | shared_ptr<RegexComponentMatcher> cm = make_shared<RegexComponentMatcher>("a", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 36 | bool res = cm->match(Name("/a/b/"), 0, 1); |
| 37 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 38 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
| 39 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 40 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 41 | backRef = make_shared<RegexBackrefManager>(); |
| 42 | cm = make_shared<RegexComponentMatcher>("a", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 43 | res = cm->match(Name("/a/b/"), 1, 1); |
| 44 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 45 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 47 | backRef = make_shared<RegexBackrefManager>(); |
| 48 | cm = make_shared<RegexComponentMatcher>("(c+)\\.(cd)", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 49 | res = cm->match(Name("/ccc.cd/b/"), 0, 1); |
| 50 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 51 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
| 52 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("ccc.cd")); |
| 53 | BOOST_CHECK_EQUAL(backRef->getBackRef(0)->getMatchResult()[0].toEscapedString(), string("ccc")); |
| 54 | BOOST_CHECK_EQUAL(backRef->getBackRef(1)->getMatchResult()[0].toEscapedString(), string("cd")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 57 | BOOST_AUTO_TEST_CASE(ComponentSetMatcher) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 58 | { |
| 59 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 60 | shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>(); |
| 61 | shared_ptr<RegexComponentSetMatcher> cm = make_shared<RegexComponentSetMatcher>("<a>", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 62 | bool res = cm->match(Name("/a/b/"), 0, 1); |
| 63 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
| 65 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 66 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 67 | res = cm->match(Name("/a/b/"), 1, 1); |
| 68 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 69 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 70 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 71 | res = cm->match(Name("/a/b/"), 0, 2); |
| 72 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 75 | backRef = make_shared<RegexBackrefManager>(); |
| 76 | cm = make_shared<RegexComponentSetMatcher>("[<a><b><c>]", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 77 | res = cm->match(Name("/a/b/d"), 1, 1); |
| 78 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 79 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
| 80 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 81 | |
| 82 | res = cm->match(Name("/a/b/d"), 2, 1); |
| 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); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [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("/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); |
| 91 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("d")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 92 | |
| 93 | } |
| 94 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 95 | BOOST_AUTO_TEST_CASE(RepeatMatcher) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 96 | { |
| 97 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 98 | shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>(); |
| 99 | shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("[<a><b>]*", backRef, 8); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 100 | bool res = cm->match(Name("/a/b/c"), 0, 0); |
| 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(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 103 | |
| 104 | cm->match(Name("/a/b/c"), 0, 2); |
| 105 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 106 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
| 107 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 108 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 109 | |
| 110 | |
| 111 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 112 | backRef = make_shared<RegexBackrefManager>(); |
| 113 | cm = make_shared<RegexRepeatMatcher>("[<a><b>]+", backRef, 8); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 114 | res = cm->match(Name("/a/b/c"), 0, 0); |
| 115 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 116 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 117 | |
| 118 | res = cm->match(Name("/a/b/c"), 0, 2); |
| 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(), 2); |
| 121 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 122 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 123 | |
| 124 | |
| 125 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 126 | backRef = make_shared<RegexBackrefManager>(); |
| 127 | cm = make_shared<RegexRepeatMatcher>("<.*>*", backRef, 4); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 128 | res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6); |
| 129 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6); |
| 131 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 132 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
| 133 | BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c")); |
| 134 | BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("d")); |
| 135 | BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toEscapedString(), string("e")); |
| 136 | BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toEscapedString(), string("f")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 137 | |
| 138 | |
| 139 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 140 | backRef = make_shared<RegexBackrefManager>(); |
| 141 | cm = make_shared<RegexRepeatMatcher>("<>*", backRef, 2); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 142 | res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6); |
| 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); |
| 145 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 146 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
| 147 | BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c")); |
| 148 | BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("d")); |
| 149 | BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toEscapedString(), string("e")); |
| 150 | BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toEscapedString(), string("f")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 151 | |
| 152 | |
| 153 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 154 | backRef = make_shared<RegexBackrefManager>(); |
| 155 | cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 156 | res = cm->match(Name("/a/b/c"), 0, 0); |
| 157 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 158 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 159 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 160 | cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 161 | res = cm->match(Name("/a/b/c"), 0, 1); |
| 162 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 163 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
| 164 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 165 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 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, 2); |
| 168 | BOOST_CHECK_EQUAL(res, false); |
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 | |
| 171 | |
| 172 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 173 | backRef = make_shared<RegexBackrefManager>(); |
| 174 | cm = make_shared<RegexRepeatMatcher>("[<a><b>]{3}", backRef, 8); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 175 | res = cm->match(Name("/a/b/a/d/"), 0, 2); |
| 176 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 177 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 178 | |
| 179 | res = cm->match(Name("/a/b/a/d/"), 0, 3); |
| 180 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 181 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3); |
| 182 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 183 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
| 184 | BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("a")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 185 | |
| 186 | res = cm->match(Name("/a/b/a/d/"), 0, 4); |
| 187 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 188 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 189 | |
| 190 | |
| 191 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 192 | backRef = make_shared<RegexBackrefManager>(); |
| 193 | cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,3}", backRef, 8); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 194 | res = cm->match(Name("/a/b/a/d/e/"), 0, 2); |
| 195 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 196 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
| 197 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 198 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 199 | |
| 200 | res = cm->match(Name("/a/b/a/d/e/"), 0, 3); |
| 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(), 3); |
| 203 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 204 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
| 205 | BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("a")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 206 | |
| 207 | res = cm->match(Name("/a/b/a/b/e/"), 0, 4); |
| 208 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 209 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 210 | |
| 211 | res = cm->match(Name("/a/b/a/d/e/"), 0, 1); |
| 212 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 213 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 214 | |
| 215 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 216 | backRef = make_shared<RegexBackrefManager>(); |
| 217 | cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,}", backRef, 8); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 218 | res = cm->match(Name("/a/b/a/d/e/"), 0, 2); |
| 219 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 220 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
| 221 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 222 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 223 | |
| 224 | res = cm->match(Name("/a/b/a/b/e/"), 0, 4); |
| 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(), 4); |
| 227 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 228 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
| 229 | BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("a")); |
| 230 | BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 231 | |
| 232 | res = cm->match(Name("/a/b/a/d/e/"), 0, 1); |
| 233 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 234 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 235 | |
| 236 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 237 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 238 | backRef = make_shared<RegexBackrefManager>(); |
| 239 | cm = make_shared<RegexRepeatMatcher>("[<a><b>]{,2}", backRef, 8); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 240 | res = cm->match(Name("/a/b/a/b/e/"), 0, 3); |
| 241 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 242 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 243 | |
| 244 | res = cm->match(Name("/a/b/a/b/e/"), 0, 2); |
| 245 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 246 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
| 247 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 248 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 249 | |
| 250 | res = cm->match(Name("/a/b/a/d/e/"), 0, 1); |
| 251 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 252 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
| 253 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 254 | |
| 255 | res = cm->match(Name("/a/b/a/d/e/"), 0, 0); |
| 256 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 257 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 260 | BOOST_AUTO_TEST_CASE(BackRefMatcher) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 261 | { |
| 262 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 263 | shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>(); |
| 264 | shared_ptr<RegexBackrefMatcher> cm = make_shared<RegexBackrefMatcher>("(<a><b>)", backRef); |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 265 | backRef->pushRef(static_pointer_cast<RegexMatcher>(cm)); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 266 | cm->lateCompile(); |
| 267 | bool res = cm->match(Name("/a/b/c"), 0, 2); |
| 268 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 269 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
| 270 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 271 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 272 | BOOST_CHECK_EQUAL(backRef->size(), 1); |
| 273 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 274 | backRef = make_shared<RegexBackrefManager>(); |
| 275 | cm = make_shared<RegexBackrefMatcher>("(<a>(<b>))", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 276 | backRef->pushRef(cm); |
| 277 | cm->lateCompile(); |
| 278 | res = cm->match(Name("/a/b/c"), 0, 2); |
| 279 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 280 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
| 281 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 282 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 283 | BOOST_CHECK_EQUAL(backRef->size(), 2); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 284 | BOOST_CHECK_EQUAL(backRef->getBackRef(0)->getMatchResult()[0].toEscapedString(), string("a")); |
| 285 | BOOST_CHECK_EQUAL(backRef->getBackRef(0)->getMatchResult()[1].toEscapedString(), string("b")); |
| 286 | BOOST_CHECK_EQUAL(backRef->getBackRef(1)->getMatchResult()[0].toEscapedString(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 289 | BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 290 | { |
| 291 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 292 | shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>(); |
| 293 | shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("([<a><b>])+", backRef, 10); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 294 | bool res = cm->match(Name("/a/b/c"), 0, 2); |
| 295 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 296 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
| 297 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 298 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 299 | BOOST_CHECK_EQUAL(backRef->size(), 1); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 300 | BOOST_CHECK_EQUAL(backRef->getBackRef(0)->getMatchResult()[0].toEscapedString(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 303 | BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced2) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 304 | { |
| 305 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 306 | shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>(); |
| 307 | shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("(<a>(<b>))<c>", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 308 | bool res = cm->match(Name("/a/b/c"), 0, 3); |
| 309 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 310 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3); |
| 311 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 312 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
| 313 | BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 314 | BOOST_CHECK_EQUAL(backRef->size(), 2); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 315 | BOOST_CHECK_EQUAL(backRef->getBackRef(0)->getMatchResult()[0].toEscapedString(), string("a")); |
| 316 | BOOST_CHECK_EQUAL(backRef->getBackRef(0)->getMatchResult()[1].toEscapedString(), string("b")); |
| 317 | BOOST_CHECK_EQUAL(backRef->getBackRef(1)->getMatchResult()[0].toEscapedString(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 320 | BOOST_AUTO_TEST_CASE(PatternListMatcher) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 321 | { |
| 322 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 323 | shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>(); |
| 324 | shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("<a>[<a><b>]", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 325 | bool res = cm->match(Name("/a/b/c"), 0, 2); |
| 326 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 327 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2); |
| 328 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 329 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 330 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 331 | backRef = make_shared<RegexBackrefManager>(); |
| 332 | cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 333 | res = cm->match(Name("/a/b/c"), 0, 1); |
| 334 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 335 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1); |
| 336 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 337 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 338 | backRef = make_shared<RegexBackrefManager>(); |
| 339 | cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 340 | res = cm->match(Name("/a/b/c"), 0, 2); |
| 341 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 342 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
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 | backRef = make_shared<RegexBackrefManager>(); |
| 345 | cm = make_shared<RegexPatternListMatcher>("<>*<a><>*", backRef); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 346 | res = cm->match(Name("/a/b/c"), 0, 3); |
| 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(), 3); |
| 349 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 350 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
| 351 | BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 352 | |
| 353 | } |
| 354 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 355 | BOOST_AUTO_TEST_CASE(TopMatcher) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 356 | { |
| 357 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 358 | shared_ptr<RegexTopMatcher> cm = make_shared<RegexTopMatcher>("^<a><b><c>"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 359 | bool res = cm->match(Name("/a/b/c/d")); |
| 360 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 361 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
| 362 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 363 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
| 364 | BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c")); |
| 365 | BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("d")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 366 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 367 | cm = make_shared<RegexTopMatcher>("<b><c><d>$"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 368 | res = cm->match(Name("/a/b/c/d")); |
| 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(), 4); |
| 371 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 372 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
| 373 | BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c")); |
| 374 | BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("d")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 375 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 376 | cm = make_shared<RegexTopMatcher>("^<a><b><c><d>$"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 377 | res = cm->match(Name("/a/b/c/d")); |
| 378 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 379 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
| 380 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 381 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
| 382 | BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c")); |
| 383 | BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("d")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 384 | |
| 385 | res = cm->match(Name("/a/b/c/d/e")); |
| 386 | BOOST_CHECK_EQUAL(res, false); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 387 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 388 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 389 | cm = make_shared<RegexTopMatcher>("<a><b><c><d>"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 390 | res = cm->match(Name("/a/b/c/d")); |
| 391 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 392 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
| 393 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 394 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
| 395 | BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c")); |
| 396 | BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("d")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 397 | |
| 398 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 399 | cm = make_shared<RegexTopMatcher>("<b><c>"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 400 | res = cm->match(Name("/a/b/c/d")); |
| 401 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 402 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
| 403 | BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a")); |
| 404 | BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toEscapedString(), string("b")); |
| 405 | BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toEscapedString(), string("c")); |
| 406 | BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toEscapedString(), string("d")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 409 | BOOST_AUTO_TEST_CASE(TopMatcherAdvanced) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 410 | { |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 411 | shared_ptr<Regex> cm = make_shared<Regex>("^(<.*>*)<.*>"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 412 | bool res = cm->match(Name("/n/a/b/c")); |
| 413 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 414 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 415 | BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/")); |
| 416 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 417 | cm = make_shared<Regex>("^(<.*>*)<.*><c>(<.*>)<.*>"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 418 | res = cm->match(Name("/n/a/b/c/d/e/")); |
| 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(), 6); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 421 | BOOST_CHECK_EQUAL(cm->expand("\\1\\2"), Name("/n/a/d/")); |
| 422 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 423 | cm = make_shared<Regex>("(<.*>*)<.*>$"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 424 | res = cm->match(Name("/n/a/b/c/")); |
| 425 | BOOST_CHECK_EQUAL(res, true); |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 426 | BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 427 | BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/")); |
| 428 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 429 | cm = make_shared<Regex>("<.*>(<.*>*)<.*>$"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 430 | 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("/a/b/")); |
| 434 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 435 | cm = make_shared<Regex>("<a>(<>*)<>$"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 436 | res = cm->match(Name("/n/a/b/c/")); |
| 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(), 4); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 439 | BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/b/")); |
| 440 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 441 | cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 442 | res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/")); |
| 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(), 6); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 445 | BOOST_CHECK_EQUAL(cm->expand("<ndn>\\2\\1\\3"), Name("/ndn/edu/ucla/yingdi/mac/")); |
| 446 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 447 | cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>", "<ndn>\\2\\1\\3"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 448 | res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/")); |
| 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(), 6); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 451 | BOOST_CHECK_EQUAL(cm->expand(), Name("/ndn/edu/ucla/yingdi/mac/")); |
| 452 | } |
| 453 | |
| 454 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 455 | |
| 456 | } // namespace ndn |