aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/pire/ut/capture_ut.cpp
blob: 3d339c56019852273c2c66f9bf89dc8780a6aa0d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * capture_ut.cpp --
 *
 * Copyright (c) 2007-2010, Dmitry Prokoptsev <dprokoptsev@gmail.com>,
 *                          Alexander Gololobov <agololobov@gmail.com>
 *
 * This file is part of Pire, the Perl Incompatible
 * Regular Expressions library.
 *
 * Pire is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Pire is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 * You should have received a copy of the GNU Lesser Public License
 * along with Pire.  If not, see <http://www.gnu.org/licenses>.
 */


#include <stub/hacks.h>
#include <stub/saveload.h>
#include <stub/utf8.h>
#include <stub/memstreams.h>
#include "stub/cppunit.h"
#include <pire.h>
#include <extra.h>
#include <string.h>

Y_UNIT_TEST_SUITE(TestPireCapture) {

	using Pire::CapturingScanner;
	using Pire::SlowCapturingScanner;
	typedef Pire::CapturingScanner::State State;

	CapturingScanner Compile(const char* regexp, int index)
	{
		Pire::Lexer lexer;

		lexer.Assign(regexp, regexp + strlen(regexp));
		lexer.AddFeature(Pire::Features::CaseInsensitive());
		lexer.AddFeature(Pire::Features::Capture((size_t) index));

		Pire::Fsm fsm = lexer.Parse();

		fsm.Surround();
		fsm.Determine();
		return fsm.Compile<Pire::CapturingScanner>();
	}

	SlowCapturingScanner SlowCompile(const char* regexp, int index, const Pire::Encoding& encoding = Pire::Encodings::Utf8())
	{
		Pire::Lexer lexer;
		lexer.AddFeature(Pire::Features::Capture(static_cast<size_t>(index)));
		lexer.SetEncoding(encoding);
		TVector<wchar32> ucs4;
		encoding.FromLocal(regexp, regexp + strlen(regexp), std::back_inserter(ucs4));
		lexer.Assign(ucs4.begin(), ucs4.end());
		Pire::Fsm fsm = lexer.Parse();
		fsm.Surround();
		return fsm.Compile<Pire::SlowCapturingScanner>();
	}

	State RunRegexp(const CapturingScanner& scanner, const char* str)
	{
		State state;
		scanner.Initialize(state);
		Step(scanner, state, Pire::BeginMark);
		Run(scanner, state, str, str + strlen(str));
		Step(scanner, state, Pire::EndMark);
		return state;
	}

	SlowCapturingScanner::State RunRegexp(const SlowCapturingScanner& scanner, const char* str)
	{
		SlowCapturingScanner::State state;
		scanner.Initialize(state);
		Run(scanner, state, str, str + strlen(str));
		return state;
	}

	ystring Captured(const State& state, const char* str)
	{
		if (state.Captured())
			return ystring(str + state.Begin() - 1, str + state.End() - 1);
		else
			return ystring();
	}

	Y_UNIT_TEST(Trivial)
	{
		CapturingScanner scanner = Compile("google_id\\s*=\\s*[\'\"]([a-z0-9]+)[\'\"]\\s*;", 1);
		State state;
		const char* str;

		str = "google_id = 'abcde';";
		state = RunRegexp(scanner, str);
		UNIT_ASSERT(state.Captured());
		UNIT_ASSERT_EQUAL(Captured(state, str), ystring("abcde"));

		str = "var google_id = 'abcde'; eval(google_id);";
		state = RunRegexp(scanner, str);
		UNIT_ASSERT(state.Captured());
		UNIT_ASSERT_EQUAL(Captured(state, str), ystring("abcde"));

		str = "google_id != 'abcde';";
		state = RunRegexp(scanner, str);
		UNIT_ASSERT(!state.Captured());
	}

	Y_UNIT_TEST(Sequential)
	{
		CapturingScanner scanner = Compile("google_id\\s*=\\s*[\'\"]([a-z0-9]+)[\'\"]\\s*;", 1);
		State state;
		const char* str;

		str = "google_id = 'abcde'; google_id = 'xyz';";
		state = RunRegexp(scanner, str);
		UNIT_ASSERT(state.Captured());
		UNIT_ASSERT_VALUES_EQUAL(Captured(state, str), ystring("abcde"));

		str = "var google_id = 'abc de'; google_id = 'xyz';";
		state = RunRegexp(scanner, str);
		UNIT_ASSERT(state.Captured());
		UNIT_ASSERT_VALUES_EQUAL(Captured(state, str), ystring("xyz"));
	}

	Y_UNIT_TEST(NegatedTerminator)
	{
		CapturingScanner scanner = Compile("=(\\d+)[^\\d]", 1);
		State state;
		const char* str;

		str = "=12345;";
		state = RunRegexp(scanner, str);
		UNIT_ASSERT(state.Captured());
		UNIT_ASSERT_EQUAL(Captured(state, str), ystring("12345"));
	}

	Y_UNIT_TEST(Serialization)
	{
		const char* regex = "google_id\\s*=\\s*[\'\"]([a-z0-9]+)[\'\"]\\s*;";
		CapturingScanner scanner2 = Compile(regex, 1);
		SlowCapturingScanner slowScanner2 = SlowCompile(regex, 1);
		BufferOutput wbuf, wbuf2;
		::Save(&wbuf, scanner2);
		::Save(&wbuf2, slowScanner2);

		MemoryInput rbuf(wbuf.Buffer().Data(), wbuf.Buffer().Size());
		MemoryInput rbuf2(wbuf2.Buffer().Data(), wbuf2.Buffer().Size());
		CapturingScanner scanner;
		SlowCapturingScanner slowScanner;
		::Load(&rbuf, scanner);
		::Load(&rbuf2, slowScanner);

		State state;
		SlowCapturingScanner::State slowState;
		const char* str;

		str = "google_id = 'abcde';";
		state = RunRegexp(scanner, str);
		slowState = RunRegexp(slowScanner, str);
		UNIT_ASSERT(state.Captured());
		UNIT_ASSERT_EQUAL(Captured(state, str), ystring("abcde"));
		SlowCapturingScanner::SingleState final;
		UNIT_ASSERT(slowScanner.GetCapture(slowState, final));
		ystring ans(str, final.GetBegin(), final.GetEnd() - final.GetBegin());
		UNIT_ASSERT_EQUAL(ans, ystring("abcde"));

		str = "google_id != 'abcde';";
		state = RunRegexp(scanner, str);
		slowState = RunRegexp(slowScanner, str);
		UNIT_ASSERT(!state.Captured());
		UNIT_ASSERT(!slowScanner.GetCapture(slowState, final));

		CapturingScanner scanner3;
		const size_t MaxTestOffset = 2 * sizeof(Pire::Impl::MaxSizeWord);
		TVector<char> buf2(wbuf.Buffer().Size() + sizeof(size_t) + MaxTestOffset);
		const void* ptr = Pire::Impl::AlignUp(&buf2[0], sizeof(size_t));
		memcpy((void*) ptr, wbuf.Buffer().Data(), wbuf.Buffer().Size());
		const void* tail = scanner3.Mmap(ptr, wbuf.Buffer().Size());
		UNIT_ASSERT_EQUAL(tail, (const void*) ((size_t)ptr + wbuf.Buffer().Size()));

		str = "google_id = 'abcde';";
		state = RunRegexp(scanner3, str);
		UNIT_ASSERT(state.Captured());
		UNIT_ASSERT_EQUAL(Captured(state, str), ystring("abcde"));

		str = "google_id != 'abcde';";
		state = RunRegexp(scanner3, str);
		UNIT_ASSERT(!state.Captured());

		ptr = (const void*) ((const char*) wbuf.Buffer().Data() + 1);
		try {
			scanner3.Mmap(ptr, wbuf.Buffer().Size());
			UNIT_ASSERT(!"CapturingScanner failed to check for misaligned mmaping");
		}
		catch (Pire::Error&) {}

		for (size_t offset = 1; offset < MaxTestOffset; ++offset) {
			ptr = Pire::Impl::AlignUp(&buf2[0], sizeof(size_t)) + offset;
			memcpy((void*) ptr, wbuf.Buffer().Data(), wbuf.Buffer().Size());
			try {
				scanner3.Mmap(ptr, wbuf.Buffer().Size());
				if (offset % sizeof(size_t) != 0) {
					UNIT_ASSERT(!"CapturingScanner failed to check for misaligned mmaping");
				} else {
					str = "google_id = 'abcde';";
					state = RunRegexp(scanner3, str);
					UNIT_ASSERT(state.Captured());
				}
			}
			catch (Pire::Error&) {}
		}
	}

	Y_UNIT_TEST(Empty)
	{
		Pire::CapturingScanner sc;
		UNIT_ASSERT(sc.Empty());
		
		UNIT_CHECKPOINT(); RunRegexp(sc, "a string"); // Just should not crash

		// Test Save/Load/Mmap
		BufferOutput wbuf;
		::Save(&wbuf, sc);

		MemoryInput rbuf(wbuf.Buffer().Data(), wbuf.Buffer().Size());
		Pire::CapturingScanner sc3;
		::Load(&rbuf, sc3);
		UNIT_CHECKPOINT(); RunRegexp(sc3, "a string");

		const size_t MaxTestOffset = 2 * sizeof(Pire::Impl::MaxSizeWord);
		TVector<char> buf2(wbuf.Buffer().Size() + sizeof(size_t) + MaxTestOffset);
		const void* ptr = Pire::Impl::AlignUp(&buf2[0], sizeof(size_t));
		memcpy((void*) ptr, wbuf.Buffer().Data(), wbuf.Buffer().Size());

		Pire::CapturingScanner sc4;
		const void* tail = sc4.Mmap(ptr, wbuf.Buffer().Size());
		UNIT_ASSERT_EQUAL(tail, (const void*) ((size_t)ptr + wbuf.Buffer().Size()));
		UNIT_CHECKPOINT(); RunRegexp(sc4, "a string");
	}

	void MakeSlowCapturingTest(const char* regexp, const char* text, size_t position, bool ans, const ystring& captured = ystring(""), const Pire::Encoding& encoding = Pire::Encodings::Utf8())
	{
		Pire::SlowCapturingScanner sc = SlowCompile(regexp, position, encoding);
		SlowCapturingScanner::State st = RunRegexp(sc, text);
		SlowCapturingScanner::SingleState fin;
		bool ifCaptured = sc.GetCapture(st, fin);
		if (ans) {
			UNIT_ASSERT(ifCaptured);
			ystring answer(text, fin.GetBegin(), fin.GetEnd() - fin.GetBegin());
			UNIT_ASSERT_EQUAL(answer, captured);
		} else  {
			UNIT_ASSERT(!ifCaptured);
		}
	}

	Y_UNIT_TEST(SlowCapturingNonGreedy)
	{
		const char* regexp = ".*?(pref.*suff)";
		const char* text = "pref ala bla pref cla suff dla";
		MakeSlowCapturingTest(regexp, text, 1, true, ystring("pref ala bla pref cla suff"));
	}

	Y_UNIT_TEST(SlowCaptureGreedy)
	{
		const char* regexp = ".*(pref.*suff)";
		const char* text = "pref ala bla pref cla suff dla";
		MakeSlowCapturingTest(regexp, text, 1, true, ystring("pref cla suff"));
	}

	Y_UNIT_TEST(SlowCaptureInOr)
	{
		const char* regexp = "(A)|A";
		const char* text = "A";
		MakeSlowCapturingTest(regexp, text, 1, true, ystring("A"));
		const char* regexp2 = "A|(A)";
		MakeSlowCapturingTest(regexp2, text, 1, false);
	}

	Y_UNIT_TEST(SlowCapturing)
	{
		const char* regexp = "^http://vk(ontakte[.]ru|[.]com)/id(\\d+)([^0-9]|$)";
		const char* text = "http://vkontakte.ru/id100500";
		MakeSlowCapturingTest(regexp, text, 2, true, ystring("100500"));
	}

	Y_UNIT_TEST(Utf_8)
	{
		const char* regexp = "\xd0\x97\xd0\xb4\xd1\x80\xd0\xb0\xd0\xb2\xd1\x81\xd1\x82\xd0\xb2\xd1\x83\xd0\xb9\xd1\x82\xd0\xb5, ((\\s|\\w|[()]|-)+)!";
		const char* text ="    \xd0\x97\xd0\xb4\xd1\x80\xd0\xb0\xd0\xb2\xd1\x81\xd1\x82\xd0\xb2\xd1\x83\xd0\xb9\xd1\x82\xd0\xb5, \xd0\xa3\xd0\xb2\xd0\xb0\xd0\xb6\xd0\xb0\xd0\xb5\xd0\xbc\xd1\x8b\xd0\xb9 (-\xd0\xb0\xd1\x8f)!   ";
		const char* ans = "\xd0\xa3\xd0\xb2\xd0\xb0\xd0\xb6\xd0\xb0\xd0\xb5\xd0\xbc\xd1\x8b\xd0\xb9 (-\xd0\xb0\xd1\x8f)";
		MakeSlowCapturingTest(regexp, text, 1, true, ystring(ans));
	}
}