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