blob: d286732472d4a012fd7a7077f4ff70f686cca70c [file] [log] [blame]
Yingdi Yu5e974202014-01-29 16:59:06 -08001/* -*- 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 Yu5e974202014-01-29 16:59:06 -08008#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 Afanasyevb1db7c62014-04-03 14:57:25 -070017#include "boost-test.hpp"
Yingdi Yu5e974202014-01-29 16:59:06 -080018
Yingdi Yu5e974202014-01-29 16:59:06 -080019using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080020namespace ndn {
Yingdi Yu5e974202014-01-29 16:59:06 -080021
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070022BOOST_AUTO_TEST_SUITE(UtilTestRegex)
Yingdi Yu5e974202014-01-29 16:59:06 -080023
24BOOST_AUTO_TEST_CASE(ComponentMatcher)
25{
26
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070027 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
28 shared_ptr<RegexComponentMatcher> cm = make_shared<RegexComponentMatcher>("a", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080029 bool res = cm->match(Name("/a/b/"), 0, 1);
30 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070031 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
32 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -080033
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070034 backRef = make_shared<RegexBackrefManager>();
35 cm = make_shared<RegexComponentMatcher>("a", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080036 res = cm->match(Name("/a/b/"), 1, 1);
37 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070038 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -080039
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070040 backRef = make_shared<RegexBackrefManager>();
41 cm = make_shared<RegexComponentMatcher>("(c+)\\.(cd)", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080042 res = cm->match(Name("/ccc.cd/b/"), 0, 1);
43 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070044 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 Yu5e974202014-01-29 16:59:06 -080048}
49
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070050BOOST_AUTO_TEST_CASE(ComponentSetMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -080051{
52
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070053 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
54 shared_ptr<RegexComponentSetMatcher> cm = make_shared<RegexComponentSetMatcher>("<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080055 bool res = cm->match(Name("/a/b/"), 0, 1);
56 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070057 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
58 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070059
Yingdi Yu5e974202014-01-29 16:59:06 -080060 res = cm->match(Name("/a/b/"), 1, 1);
61 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070062 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070063
Yingdi Yu5e974202014-01-29 16:59:06 -080064 res = cm->match(Name("/a/b/"), 0, 2);
65 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070066 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -080067
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070068 backRef = make_shared<RegexBackrefManager>();
69 cm = make_shared<RegexComponentSetMatcher>("[<a><b><c>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080070 res = cm->match(Name("/a/b/d"), 1, 1);
71 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070072 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
73 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -080074
75 res = cm->match(Name("/a/b/d"), 2, 1);
76 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070077 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070078
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070079 backRef = make_shared<RegexBackrefManager>();
80 cm = make_shared<RegexComponentSetMatcher>("[^<a><b><c>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -080081 res = cm->match(Name("/b/d"), 1, 1);
82 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070083 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
84 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("d"));
Yingdi Yu5e974202014-01-29 16:59:06 -080085
86}
87
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070088BOOST_AUTO_TEST_CASE(RepeatMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -080089{
90
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070091 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
92 shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("[<a><b>]*", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -080093 bool res = cm->match(Name("/a/b/c"), 0, 0);
94 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070095 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -080096
97 cm->match(Name("/a/b/c"), 0, 2);
98 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070099 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 Yu5e974202014-01-29 16:59:06 -0800102
103
104
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700105 backRef = make_shared<RegexBackrefManager>();
106 cm = make_shared<RegexRepeatMatcher>("[<a><b>]+", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800107 res = cm->match(Name("/a/b/c"), 0, 0);
108 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700109 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800110
111 res = cm->match(Name("/a/b/c"), 0, 2);
112 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700113 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 Yu5e974202014-01-29 16:59:06 -0800116
117
118
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700119 backRef = make_shared<RegexBackrefManager>();
120 cm = make_shared<RegexRepeatMatcher>("<.*>*", backRef, 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800121 res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
122 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700123 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 Yu5e974202014-01-29 16:59:06 -0800130
131
132
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700133 backRef = make_shared<RegexBackrefManager>();
134 cm = make_shared<RegexRepeatMatcher>("<>*", backRef, 2);
Yingdi Yu5e974202014-01-29 16:59:06 -0800135 res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
136 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700137 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 Yu5e974202014-01-29 16:59:06 -0800144
145
146
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700147 backRef = make_shared<RegexBackrefManager>();
148 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800149 res = cm->match(Name("/a/b/c"), 0, 0);
150 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700151 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800152
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700153 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800154 res = cm->match(Name("/a/b/c"), 0, 1);
155 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700156 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
157 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800158
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700159 cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
Yingdi Yu5e974202014-01-29 16:59:06 -0800160 res = cm->match(Name("/a/b/c"), 0, 2);
161 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700162 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800163
164
165
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700166 backRef = make_shared<RegexBackrefManager>();
167 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{3}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800168 res = cm->match(Name("/a/b/a/d/"), 0, 2);
169 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700170 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800171
172 res = cm->match(Name("/a/b/a/d/"), 0, 3);
173 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700174 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 Yu5e974202014-01-29 16:59:06 -0800178
179 res = cm->match(Name("/a/b/a/d/"), 0, 4);
180 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700181 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800182
183
184
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700185 backRef = make_shared<RegexBackrefManager>();
186 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,3}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800187 res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
188 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700189 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 Yu5e974202014-01-29 16:59:06 -0800192
193 res = cm->match(Name("/a/b/a/d/e/"), 0, 3);
194 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700195 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 Yu5e974202014-01-29 16:59:06 -0800199
200 res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
201 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700202 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800203
204 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
205 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700206 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800207
208
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700209 backRef = make_shared<RegexBackrefManager>();
210 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800211 res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
212 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700213 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 Yu5e974202014-01-29 16:59:06 -0800216
217 res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
218 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700219 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 Yu5e974202014-01-29 16:59:06 -0800224
225 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
226 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700227 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800228
229
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700230
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700231 backRef = make_shared<RegexBackrefManager>();
232 cm = make_shared<RegexRepeatMatcher>("[<a><b>]{,2}", backRef, 8);
Yingdi Yu5e974202014-01-29 16:59:06 -0800233 res = cm->match(Name("/a/b/a/b/e/"), 0, 3);
234 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700235 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800236
237 res = cm->match(Name("/a/b/a/b/e/"), 0, 2);
238 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700239 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 Yu5e974202014-01-29 16:59:06 -0800242
243 res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
244 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700245 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
246 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800247
248 res = cm->match(Name("/a/b/a/d/e/"), 0, 0);
249 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700250 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800251}
252
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700253BOOST_AUTO_TEST_CASE(BackRefMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800254{
255
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700256 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
257 shared_ptr<RegexBackrefMatcher> cm = make_shared<RegexBackrefMatcher>("(<a><b>)", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800258 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 Afanasyevd1b5c412014-03-27 15:03:51 -0700262 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 Yu5e974202014-01-29 16:59:06 -0800265 BOOST_CHECK_EQUAL(backRef->size(), 1);
266
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700267 backRef = make_shared<RegexBackrefManager>();
268 cm = make_shared<RegexBackrefMatcher>("(<a>(<b>))", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800269 backRef->pushRef(cm);
270 cm->lateCompile();
271 res = cm->match(Name("/a/b/c"), 0, 2);
272 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700273 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 Yu5e974202014-01-29 16:59:06 -0800276 BOOST_CHECK_EQUAL(backRef->size(), 2);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700277 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 Yu5e974202014-01-29 16:59:06 -0800280}
281
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700282BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800283{
284
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700285 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
286 shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("([<a><b>])+", backRef, 10);
Yingdi Yu5e974202014-01-29 16:59:06 -0800287 bool res = cm->match(Name("/a/b/c"), 0, 2);
288 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700289 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 Yu5e974202014-01-29 16:59:06 -0800292 BOOST_CHECK_EQUAL(backRef->size(), 1);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700293 BOOST_CHECK_EQUAL(backRef->getBackRef(0)->getMatchResult()[0].toEscapedString(), string("b"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800294}
295
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700296BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced2)
Yingdi Yu5e974202014-01-29 16:59:06 -0800297{
298
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700299 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
300 shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("(<a>(<b>))<c>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800301 bool res = cm->match(Name("/a/b/c"), 0, 3);
302 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700303 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 Yu5e974202014-01-29 16:59:06 -0800307 BOOST_CHECK_EQUAL(backRef->size(), 2);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700308 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 Yu5e974202014-01-29 16:59:06 -0800311}
312
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700313BOOST_AUTO_TEST_CASE(PatternListMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800314{
315
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700316 shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
317 shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("<a>[<a><b>]", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800318 bool res = cm->match(Name("/a/b/c"), 0, 2);
319 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700320 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 Yu5e974202014-01-29 16:59:06 -0800323
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700324 backRef = make_shared<RegexBackrefManager>();
325 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800326 res = cm->match(Name("/a/b/c"), 0, 1);
327 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700328 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
329 BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toEscapedString(), string("a"));
Yingdi Yu5e974202014-01-29 16:59:06 -0800330
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700331 backRef = make_shared<RegexBackrefManager>();
332 cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800333 res = cm->match(Name("/a/b/c"), 0, 2);
334 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700335 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800336
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700337 backRef = make_shared<RegexBackrefManager>();
338 cm = make_shared<RegexPatternListMatcher>("<>*<a><>*", backRef);
Yingdi Yu5e974202014-01-29 16:59:06 -0800339 res = cm->match(Name("/a/b/c"), 0, 3);
340 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700341 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 Yu5e974202014-01-29 16:59:06 -0800345
346}
347
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700348BOOST_AUTO_TEST_CASE(TopMatcher)
Yingdi Yu5e974202014-01-29 16:59:06 -0800349{
350
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700351 shared_ptr<RegexTopMatcher> cm = make_shared<RegexTopMatcher>("^<a><b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800352 bool res = cm->match(Name("/a/b/c/d"));
353 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700354 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 Yu5e974202014-01-29 16:59:06 -0800359
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700360 cm = make_shared<RegexTopMatcher>("<b><c><d>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800361 res = cm->match(Name("/a/b/c/d"));
362 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700363 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 Yu5e974202014-01-29 16:59:06 -0800368
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700369 cm = make_shared<RegexTopMatcher>("^<a><b><c><d>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800370 res = cm->match(Name("/a/b/c/d"));
371 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700372 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 Yu5e974202014-01-29 16:59:06 -0800377
378 res = cm->match(Name("/a/b/c/d/e"));
379 BOOST_CHECK_EQUAL(res, false);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700380 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
Yingdi Yu5e974202014-01-29 16:59:06 -0800381
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700382 cm = make_shared<RegexTopMatcher>("<a><b><c><d>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800383 res = cm->match(Name("/a/b/c/d"));
384 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700385 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 Yu5e974202014-01-29 16:59:06 -0800390
391
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700392 cm = make_shared<RegexTopMatcher>("<b><c>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800393 res = cm->match(Name("/a/b/c/d"));
394 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700395 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 Yu5e974202014-01-29 16:59:06 -0800400}
401
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700402BOOST_AUTO_TEST_CASE(TopMatcherAdvanced)
Yingdi Yu5e974202014-01-29 16:59:06 -0800403{
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700404 shared_ptr<Regex> cm = make_shared<Regex>("^(<.*>*)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800405 bool res = cm->match(Name("/n/a/b/c"));
406 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700407 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800408 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
409
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700410 cm = make_shared<Regex>("^(<.*>*)<.*><c>(<.*>)<.*>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800411 res = cm->match(Name("/n/a/b/c/d/e/"));
412 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700413 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800414 BOOST_CHECK_EQUAL(cm->expand("\\1\\2"), Name("/n/a/d/"));
415
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700416 cm = make_shared<Regex>("(<.*>*)<.*>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800417 res = cm->match(Name("/n/a/b/c/"));
418 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700419 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800420 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
421
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700422 cm = make_shared<Regex>("<.*>(<.*>*)<.*>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800423 res = cm->match(Name("/n/a/b/c/"));
424 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700425 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800426 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/a/b/"));
427
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700428 cm = make_shared<Regex>("<a>(<>*)<>$");
Yingdi Yu5e974202014-01-29 16:59:06 -0800429 res = cm->match(Name("/n/a/b/c/"));
430 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700431 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
Yingdi Yu5e974202014-01-29 16:59:06 -0800432 BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/b/"));
433
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700434 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>");
Yingdi Yu5e974202014-01-29 16:59:06 -0800435 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
436 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700437 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800438 BOOST_CHECK_EQUAL(cm->expand("<ndn>\\2\\1\\3"), Name("/ndn/edu/ucla/yingdi/mac/"));
439
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700440 cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>", "<ndn>\\2\\1\\3");
Yingdi Yu5e974202014-01-29 16:59:06 -0800441 res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
442 BOOST_CHECK_EQUAL(res, true);
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700443 BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
Yingdi Yu5e974202014-01-29 16:59:06 -0800444 BOOST_CHECK_EQUAL(cm->expand(), Name("/ndn/edu/ucla/yingdi/mac/"));
445}
446
447BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800448
449} // namespace ndn