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