File: RegexBugs.cs

package info (click to toggle)
mono 1.2.2.1-1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 142,720 kB
  • ctags: 256,408
  • sloc: cs: 1,495,736; ansic: 249,442; sh: 18,327; xml: 12,463; makefile: 5,046; perl: 1,248; asm: 635; yacc: 285; sql: 7
file content (423 lines) | stat: -rw-r--r-- 14,263 bytes parent folder | download | duplicates (2)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//
// MonoTests.System.Text.RegularExpressions misc. test cases
//
// Authors:
// 	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (c) Copyright 2003,2004 Novell, Inc. (http://www.novell.com)
//

using NUnit.Framework;
using System;
using System.Text;
using System.Text.RegularExpressions;

namespace MonoTests.System.Text.RegularExpressions
{
	[TestFixture]
	public class RegexBugs : Assertion
	{
		[Test]
		public void SplitGroup () // bug51146
		{
		        string [] splitResult = new Regex ("-").Split ("a-bcd-e-fg");
			string [] expected = new string [] {"a", "bcd", "e", "fg"};
			int length = expected.Length;
			int i;
			AssertEquals ("#01", length, splitResult.Length);
			for (i = 0; i < length; i++)
				AssertEquals ("#02 " + i, expected [i], splitResult [i]);
			
			splitResult = new Regex ("(-)").Split ("a-bcd-e-fg");
			expected = new string [] {"a", "-", "bcd", "-", "e", "-", "fg"};
			length = expected.Length;
			AssertEquals ("#03", length, splitResult.Length);
			for (i = 0; i < length; i++)
				AssertEquals ("#04 " + i, expected [i], splitResult [i]);

			splitResult = new Regex ("(-)b(c)").Split ("a-bcd-e-fg");
			expected = new string [] {"a", "-", "c", "d-e-fg" };
			length = expected.Length;
			AssertEquals ("#04", length, splitResult.Length);
			for (i = 0; i < length; i++)
				AssertEquals ("#05 " + i, expected [i], splitResult [i]);
				
			splitResult = new Regex ("-").Split ("a-bcd-e-fg-");
			expected = new string [] {"a", "bcd", "e", "fg", ""};
			length = expected.Length;
			AssertEquals ("#06", length, splitResult.Length);
			for (i = 0; i < length; i++)
				AssertEquals ("#07 " + i, expected [i], splitResult [i]);
		}

		[Test]
		public void MathEmptyGroup () // bug 42529
		{
			string str = "Match something from here.";

			AssertEquals ("MEG #01", false, Regex.IsMatch(str, @"(something|dog)$"));
			AssertEquals ("MEG #02", true, Regex.IsMatch(str, @"(|something|dog)$"));
			AssertEquals ("MEG #03", true, Regex.IsMatch(str, @"(something||dog)$"));
			AssertEquals ("MEG #04", true, Regex.IsMatch(str, @"(something|dog|)$"));

			AssertEquals ("MEG #05", true, Regex.IsMatch(str, @"(something|dog)*"));
			AssertEquals ("MEG #06", true, Regex.IsMatch(str, @"(|something|dog)*"));
			AssertEquals ("MEG #07", true, Regex.IsMatch(str, @"(something||dog)*"));
			AssertEquals ("MEG #08", true, Regex.IsMatch(str, @"(something|dog|)*"));

			AssertEquals ("MEG #09", true, Regex.IsMatch(str, @"(something|dog)*$"));
			AssertEquals ("MEG #10", true, Regex.IsMatch(str, @"(|something|dog)*$"));
			AssertEquals ("MEG #11", true, Regex.IsMatch(str, @"(something||dog)*$"));
			AssertEquals ("MEG #12", true, Regex.IsMatch(str, @"(something|dog|)*$"));

		}

		[Test]
		public void Braces () // bug 52924
		{
			// Before the fix, the next line throws an exception
			Regex regVar = new Regex(@"{\w+}");
			Match m = regVar.Match ("{   }");
			AssertEquals ("BR #01", false, m.Success);
		}

		[Test]
		public void WhiteSpaceGroupped () // bug 71077
		{
			string s = "\n";
			string p = @"[\s\S]";	// =Category.Any

			AssertEquals ("WSG#1", true, Regex.IsMatch (s, p));
		}

                [Test]
                public void RangeIgnoreCase() // bug 45976
                {
                        string str = "AAABBBBAAA" ;
                        AssertEquals("RIC #01", true, Regex.IsMatch(str, @"[A-F]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #02", true, Regex.IsMatch(str, @"[a-f]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #03", true, Regex.IsMatch(str, @"[A-Fa-f]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #04", true, Regex.IsMatch(str, @"[AB]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #05", true, Regex.IsMatch(str, @"[A-B]+", RegexOptions.IgnoreCase));

                        str = "AaaBBBaAa" ;
                        AssertEquals("RIC #06", true, Regex.IsMatch(str, @"[A-F]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #07", true, Regex.IsMatch(str, @"[a-f]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #08", true, Regex.IsMatch(str, @"[A-Fa-f]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #09", true, Regex.IsMatch(str, @"[AB]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #10", true, Regex.IsMatch(str, @"[A-B]+", RegexOptions.IgnoreCase));

			str = "Aaa[";
			AssertEquals("RIC #11", true, Regex.IsMatch(str, @"[A-a]+", RegexOptions.IgnoreCase));
			
			str = "Ae";
			Assert("RIC #12", Regex.IsMatch(str, @"[A-a]+", RegexOptions.IgnoreCase));

                }

		[Test]
		public void Escape0 () // bug54797
		{
            		Regex r = new Regex(@"^[\s\0]*$");
			AssertEquals ("E0-1", true, r.Match(" \0").Success);
		}

        	[Test()]
        	public void MultipleMatches()
        	{
            		Regex regex = new Regex (@"^(?'path'.*(\\|/)|(/|\\))(?'file'.*)$");
            		Match match = regex.Match (@"d:\Temp\SomeDir\SomeDir\bla.xml");
                                                                                           
            		AssertEquals ("MM #01", 5, match.Groups.Count);
                                                                                           
            		AssertEquals ("MM #02", "1", regex.GroupNameFromNumber(1));
            		AssertEquals ("MM #03", "2", regex.GroupNameFromNumber(2));
            		AssertEquals ("MM #04", "path", regex.GroupNameFromNumber(3));
            		AssertEquals ("MM #05", "file", regex.GroupNameFromNumber(4));
                                                                                           
            		AssertEquals ("MM #06", "\\", match.Groups[1].Value);
            		AssertEquals ("MM #07", "", match.Groups[2].Value);
            		AssertEquals ("MM #08", @"d:\Temp\SomeDir\SomeDir\", // fool emacs: "
				      match.Groups[3].Value);
            		AssertEquals ("MM #09", "bla.xml", match.Groups[4].Value);
        	}

		[Test] 
		public void SameNameGroups () // First problem in fixing bug #56000
		{
			string rex = "link\\s*rel\\s*=\\s*[\"']?alternate[\"']?\\s*";
			rex += "type\\s*=\\s*[\"']?text/xml[\"']?\\s*href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^']*)'|(?<1>\\S+))";
			Regex rob = new Regex (rex, RegexOptions.IgnoreCase);
		}

		[Test]
		public void UndefinedGroup () // bug 52890
		{
			Regex regex = new Regex( "[A-Za-z_0-9]" );
			Match m = regex.Match( "123456789abc" );
			Group g = m.Groups["not_defined"];
			AssertNotNull ("#0", g);
			AssertEquals ("#1", 0, g.Index);
			AssertEquals ("#2", 0, g.Length);
			AssertEquals ("#3", "", g.Value);
			Assert ("#4", !g.Success);
			AssertNotNull ("#5", g.Captures);
			AssertEquals ("#6", 0, g.Captures.Count);
		}

		[Test]
		public void Quantifiers1 ()
		{
			Regex re = new Regex ("[\\w\\W]{8,32}");
			Match m = re.Match (new string ('1', 7));
			AssertEquals ("#01", false, m.Success);
		}

		[Test]
		public void Quantifiers2 ()
		{
			Regex re = new Regex ("[\\w\\W]{8,32}");
			Match m = re.Match (new string ('1', 8));
			AssertEquals ("#01", true, m.Success);
		}

		[Test]
		public void Quantifiers3 ()
		{
			Regex re = new Regex ("[\\w\\W]{8,32}");
			Match m = re.Match (new string ('1', 16));
			AssertEquals ("#01", true, m.Success);
		}

		[Test]
		public void Quantifiers4 ()
		{
			Regex re = new Regex ("[\\w\\W]{8,32}");
			Match m = re.Match (new string ('1', 32));
			AssertEquals ("#01", true, m.Success);
		}

		[Test]
		public void Quantifiers5 ()
		{
			Regex re = new Regex ("[\\w\\W]{8,32}");
			Match m = re.Match (new string ('1', 33));
			AssertEquals ("#01", true, m.Success);
		}

		[Test]
		public void CategoryAndNegated () // Was a regression after first attemp to fix 59150.
		{
			string text = "<?xml version=\"1.0\"?>";
			Regex re = new Regex ("<\\s*(\\/?)\\s*([\\s\\S]*?)\\s*(\\/?)\\s*>");
			text = re.Replace (text, "{blue:&lt;$1}{maroon:$2}{blue:$3&gt;}");
			AssertEquals ("#01", "{blue:&lt;}{maroon:?xml version=\"1.0\"?}{blue:&gt;}", text);
		}
	
		[Test]
		public void BackSpace ()
		{
			string text = "Go, \bNo\bGo" ;
			Regex re = new Regex(@"\b[\b]");
			text = re.Replace(text, " ");
			AssertEquals("#01", "Go, \bNo Go", text);
		}

		[Test]
		public void ReplaceNegOneAndStartat ()
		{
			string text = "abcdeeee";
			Regex re = new Regex("e+");
			text = re.Replace(text, "e", -1, 4);
			AssertEquals("#01", "abcde", text);
		}

		[Test]
		//[Ignore] You may want to ignore this if the bugs gets back
		public void SplitInfiniteLoop () // bug 57274
		{
			string ss = "a b c d e";
			string [] words = Regex.Split (ss, "[ \t\n\r]*");
			AssertEquals ("#01Length", 11, words.Length);
			AssertEquals ("#00", "", words [0]);
			AssertEquals ("#01", "a", words [1]);
			AssertEquals ("#02", "", words [2]);
			AssertEquals ("#03", "b", words [3]);
			AssertEquals ("#04", "", words [4]);
			AssertEquals ("#05", "c", words [5]);
			AssertEquals ("#06", "", words [6]);
			AssertEquals ("#07", "d", words [7]);
			AssertEquals ("#08", "", words [8]);
			AssertEquals ("#09", "e", words [9]);
			AssertEquals ("#10", "", words [10]);

		}

		[Test]
		public void CaseAndSearch () // bug 69065
		{
			string test1 =  @"!E   ZWEITBAD :REGLER-PARAMETER 20.10.2004  SEITE   1";
			string test2 =  @" REGLER-PARAMETER ";
			string test3 =  @"REGLER-PARAMETER ";
			Regex x = new Regex ("REGLER-PARAMETER",RegexOptions.IgnoreCase|RegexOptions.Compiled);

			Match m = x.Match (test1);
			AssertEquals ("#01", true, m.Success);

			m = x.Match (test2);
			AssertEquals ("#02", true, m.Success);

			m = x.Match (test3);
			AssertEquals ("#03", true, m.Success);
		}

		[Test]
		public void QuantifiersParseError () // bug 69193
		{
			Regex x = new Regex ("{1,a}");
			x = new Regex ("{a,1}");
			x = new Regex ("{a}");
			x = new Regex ("{,a}");
		}

		[Test]
		public void NameLookupInEmptyMatch () // bug 74753
		{
			Regex regTime = new Regex (
					@"(?<hour>[0-9]{1,2})([\:](?<minute>[0-9]{1,2})){0,1}([\:](?<second>[0-9]{1,2})){0,1}\s*(?<ampm>(?i:(am|pm)){0,1})");

			Match mTime = regTime.Match("");
			AssertEquals ("#01", "", mTime.Groups["hour"].Value);
			AssertEquals ("#02", "", mTime.Groups["minute"].Value);
			AssertEquals ("#03", "", mTime.Groups["second"].Value);
			AssertEquals ("#04", "", mTime.Groups["ampm"].Value);

			mTime = regTime.Match("12:00 pm");
			AssertEquals ("#05", "12", mTime.Groups["hour"].Value);
			AssertEquals ("#06", "00", mTime.Groups["minute"].Value);
			AssertEquals ("#07", "", mTime.Groups["second"].Value);
			AssertEquals ("#08", "pm", mTime.Groups["ampm"].Value);
		}

		[Test]
		public void HangingHyphens ()
		{
			// bug 77626
			Assert ("#01", Regex.IsMatch ("mT1[", @"m[0-9A-Za-z_-]+\["));
			Assert ("#02", Regex.IsMatch ("mT1[", @"m[-0-9A-Za-z_]+\["));

			Assert ("#03", Regex.IsMatch ("-a;", @"[--a]{3}"));
			Assert ("#04", Regex.IsMatch ("-&,", @"[&--]{3}"));

			Assert ("#05", Regex.IsMatch ("abcz-", @"[a-c-z]{5}"));
			Assert ("#05b", !Regex.IsMatch ("defghijklmnopqrstuvwxy", @"[a-c-z]"));

			Assert ("#06", Regex.IsMatch ("abcxyz-", @"[a-c-x-z]{7}"));
			Assert ("#06b", !Regex.IsMatch ("defghijklmnopqrstuvw", @"[a-c-x-z]"));

			Assert ("#07", Regex.IsMatch (" \tz-", @"[\s-z]{4}"));
			Assert ("#07b", !Regex.IsMatch ("abcdefghijklmnopqrstuvwxy", @"[\s-z]"));
		}

		[Test, ExpectedException (typeof (ArgumentException))]
		public void HangingHyphen1 ()
		{
			bool b = Regex.IsMatch ("foobar", @"[a-\s]");
		}

		[Test]
		public void Bug77487 ()
		{
			Assert ("#01", Regex.IsMatch ("a a", "^(a[^a]*)*a$"));
			Assert ("#02", Regex.IsMatch ("a a", "^(a *)*a$"));
			Assert ("#03", Regex.IsMatch ("a a", "(a[^a]*)+a"));
			Assert ("#04", Regex.IsMatch ("a a", "(a *)+a"));
		}

		[Test]
		public void Bug69269 ()
		{
			string s = "CREATE aa\faa; CREATE bb\nbb; CREATE cc\rcc; CREATE dd\tdd; CREATE ee\vee;";
			AssertEquals ("#01", 5, Regex.Matches(s, @"CREATE[\s\S]+?;").Count);
			AssertEquals ("#02", 5, Regex.Matches(s, @"CREATE[ \f\n\r\t\v\S]+?;").Count);
		}

		[Test]
		public void Bug76345 ()
		{
			Match m;
			string s1 = "'asdf'";
			string s2 = "'as,'df'";

			m = new Regex("'.*?'").Match(s1);     Assert ("#01", m.Success); AssertEquals ("#01v", s1, m.Value);
			m = new Regex("'[^,].*?'").Match(s1); Assert ("#02", m.Success); AssertEquals ("#02v", s1, m.Value);
			m = new Regex("'.*?[^,]'").Match(s1); Assert ("#03", m.Success); AssertEquals ("#03v", s1, m.Value);
			m = new Regex("'.*?[^,]'").Match(s2); Assert ("#04", m.Success); AssertEquals ("#04v", s2, m.Value);
		}

		[Test]
		public void Bug78007 ()
		{
			string test = "head&gt;<html>";
			string pattern = @"\Ahead&gt;\<html\>";
			Regex r = new Regex (pattern);
			Match m = r.Match (test);
			Assert ("#01", m.Success);
			AssertEquals ("#01i", 0, m.Index);
			AssertEquals ("#01l", 14, m.Length);

			m = m.NextMatch ();
			Assert ("#02", !m.Success);
		}

		[Test]
		public void CharClassWithIgnoreCase ()
		{
			string str = "Foobar qux";
			Regex re = new Regex (@"[a-z\s]*", RegexOptions.IgnoreCase);
			Match m = re.Match (str);
			AssertEquals ("#01", str, m.Value);
                }

		void Kill65535_1 (int length)
		{
			StringBuilder sb = new StringBuilder ("x");
			sb.Append ('a', length);
			sb.Append ('y');
			string teststring = sb.ToString ();
			Regex regex = new Regex (@"xa*y");
			Match m = regex.Match (teststring);
			Assert ("#01 " + length, m.Success);
			AssertEquals ("#02 " + length, m.Index, 0);
			AssertEquals ("#03 " + length, m.Length, teststring.Length);
		}

		void Kill65535_2 (int length)
		{
			StringBuilder sb = new StringBuilder ("xaaaax");
			sb.Append ('a', length);
			sb.Append ('y');
			string teststring = sb.ToString ();
			Regex regex = new Regex (@"x.*y");
			Match m = regex.Match(teststring);
			Assert ("#01 " + length, m.Success);
			AssertEquals ("#02 " + length, m.Index, 0);
			AssertEquals ("#03 " + length, m.Length, teststring.Length);
		}
		

		[Test] // Based on bug #78278
		public void No65535Limit ()
		{
			Kill65535_1 (65535);
			Kill65535_1 (65536);
			Kill65535_1 (131071);
			Kill65535_1 (131072);

			Kill65535_2 (65530);
			Kill65535_2 (65531);
			Kill65535_2 (131066);
			Kill65535_2 (131067);
		} 
	}
}