2 // assembly: System_test
3 // namespace: MonoTests.System.Text.RegularExpressions
7 // Juraj Skripsky (juraj@hotfeet.ch)
9 // (c) 2003 Juraj Skripsky
12 using System.Text.RegularExpressions;
15 using System.Collections.Generic;
18 using NUnit.Framework;
20 namespace MonoTests.System.Text.RegularExpressions
23 public class RegexTest
26 private int cache_initial_value;
29 public void FixtureSetUp ()
31 cache_initial_value = Regex.CacheSize;
35 public void TearDown ()
37 Regex.CacheSize = cache_initial_value;
44 char[] c = { (char)32, (char)8212, (char)32 };
45 string s = new String(c);
46 Assert.IsTrue (Regex.IsMatch(s, s), "char");
49 [Test, ExpectedException (typeof (ArgumentNullException))]
50 public void NullPattern1 ()
55 [Test, ExpectedException (typeof (ArgumentNullException))]
56 public void NullPattern2 ()
58 new Regex (null, RegexOptions.None);
61 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
62 public void InvalidOptions1 ()
64 new Regex ("foo", (RegexOptions) Int32.MaxValue);
67 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
68 public void InvalidOptions2 ()
70 new Regex ("foo", RegexOptions.ECMAScript | RegexOptions.RightToLeft);
74 public void Unescape ()
76 string inString = @"\a\b\t\r\v\f\n\e\02400\x231\cC\ufffff\*";
77 char [] c = { (char)7, (char)8, (char)9, (char)13,
78 (char)11, (char)12, (char)10, (char)27, (char) 20,
79 (char)48, (char)48, (char)35, (char)49,
80 (char)3, (char)65535, (char)102, (char)42
82 string expectedString = new String(c);
83 string outString = Regex.Unescape(inString);
85 Assert.AreEqual (outString, expectedString, "unescape");
91 Regex email = new Regex ("(?<user>[^@]+)@(?<domain>.+)");
94 m = email.Match ("mono@go-mono.com");
96 Assert.IsTrue (m.Success, "#m01");
97 Assert.AreEqual ("mono", m.Groups ["user"].Value, "#m02");
98 Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m03");
100 m = email.Match ("mono.bugs@go-mono.com");
101 Assert.IsTrue (m.Success, "m04");
102 Assert.AreEqual ("mono.bugs", m.Groups ["user"].Value, "#m05");
103 Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m06");
107 public void Match2 ()
109 Regex regex = new Regex(@"(?<tab>\t)|(?<text>[^\t]*)");
110 MatchCollection col = regex.Matches("\tjust a text");
111 Assert.AreEqual(3, col.Count);
112 Assert.AreEqual (col [0].Value, "\t");
113 Assert.AreEqual (col [1].Value, "just a text");
114 Assert.AreEqual(col[2].Value, string.Empty);
117 [Test, ExpectedException (typeof (ArgumentNullException))]
118 public void Match_Null1 ()
120 new Regex (@"foo").Match (null);
123 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
124 public void Match_BadStart1 ()
126 new Regex (@"foo").Match ("foobar", -1);
129 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
130 public void Match_BadStart2 ()
132 new Regex (@"foo").Match ("foobar", -1, 0);
135 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
136 public void Match_BadStart3 ()
138 new Regex (@"foo").Match ("foobar", 7);
141 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
142 public void Match_BadStart4 ()
144 new Regex (@"foo").Match ("foobar", 7, 0);
147 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
148 public void Match_BadLength1 ()
150 new Regex (@"foo").Match ("foobar", 5, -1);
153 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
154 public void Match_BadLength2 ()
156 new Regex (@"foo").Match ("foobar", 5, 3);
159 [Test, ExpectedException (typeof (ArgumentNullException))]
160 public void Matches_Null1 ()
162 new Regex (@"foo").Matches (null);
165 [Test, ExpectedException (typeof (ArgumentNullException))]
166 public void Matches_Null2 ()
168 new Regex (@"foo").Matches (null, 0);
171 [Test, ExpectedException (typeof (ArgumentNullException))]
172 public void Matches_Null3 ()
174 new Regex (@"foo", RegexOptions.RightToLeft).Matches (null);
177 [Test, ExpectedException (typeof (ArgumentNullException))]
178 public void Replace_InputNull ()
180 Regex r = new Regex ("^.*$");
181 MatchEvaluator m = delegate (Match match) {return null;};
182 r.Replace (null, m, 0, 0);
185 [Test, ExpectedException (typeof (ArgumentNullException))]
186 public void Replace_InputNull2 ()
188 Regex r = new Regex ("^.*$");
189 r.Replace (null, "abc", 0, 0);
192 [Test, ExpectedException (typeof (ArgumentNullException))]
193 public void Replace_InputNull3 ()
195 Regex r = new Regex ("^.*$", RegexOptions.RightToLeft);
196 MatchEvaluator m = delegate (Match match) {return null;};
200 [Test, ExpectedException (typeof (ArgumentNullException))]
201 public void Replace_InputNull4 ()
203 Regex r = new Regex ("^.*$", RegexOptions.RightToLeft);
204 r.Replace (null, "abc");
207 [Test, ExpectedException (typeof (ArgumentNullException))]
208 public void Replace_ReplacementNull ()
210 Regex r = new Regex ("^.*$");
211 r.Replace ("string", (string) null, 0, 0);
214 [Test, ExpectedException (typeof (ArgumentNullException))]
215 public void Replace_EvaluatorNull ()
217 Regex r = new Regex ("^.*$");
218 MatchEvaluator m = null;
219 r.Replace ("string", m, 0, 0);
222 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
223 public void Replace_InvalidCount ()
225 Regex r = new Regex ("foo|bar");
226 r.Replace ("foo", "baz", -4);
229 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
230 public void Replace_InvalidStart ()
232 Regex r = new Regex ("foo|bar");
233 r.Replace ("foo", "baz", 1, -4);
236 [Test, ExpectedException (typeof (ArgumentNullException))]
237 public void Split_InputNull1 ()
239 Regex.Split (null, "^.*$");
242 [Test, ExpectedException (typeof (ArgumentNullException))]
243 public void Split_InputNull2 ()
245 Regex.Split (null, "^.*$", RegexOptions.RightToLeft);
248 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
249 public void Split_InvalidCount ()
251 Regex r = new Regex ("^.*$");
255 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
256 public void Split_InvalidCount2 ()
258 Regex r = new Regex ("^.*$");
259 r.Split ("foo", 1, -4);
262 [Test, ExpectedException (typeof (ArgumentNullException))]
263 public void Escape_Null ()
268 [Test, ExpectedException (typeof (ArgumentNullException))]
269 public void Unescape_Null ()
271 Regex.Unescape (null);
274 static string story =
275 "Two little dragons lived in the forest\n" +
276 "They spent their days collecting honey suckle,\n" +
277 "And eating curds and whey\n" +
278 "Until an evil sorcer came along\n" +
279 "And chased my dragon friends away";
281 struct MatchCollectionTrial {
282 public readonly string name;
283 public readonly string text;
284 public readonly string regex;
285 public readonly string [] matches;
286 public MatchCollectionTrial (string name, string text, string regex, string [] matches)
291 this.matches = matches;
295 static readonly MatchCollectionTrial [] trials = {
296 new MatchCollectionTrial ("word", "the fat cat ate the rat", "(?<word>\\w+)",
297 new string [] { "the", "fat", "cat", "ate", "the", "rat" }),
298 new MatchCollectionTrial ("digit", "0 1 2 3 4 5 6a7b8c9d10", "(?<digit>\\d+)",
299 new string [] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }),
300 new MatchCollectionTrial ("line", story, "(?<line>.+)",
301 new string [] { "Two little dragons lived in the forest",
302 "They spent their days collecting honey suckle,",
303 "And eating curds and whey",
304 "Until an evil sorcer came along",
305 "And chased my dragon friends away" }),
306 new MatchCollectionTrial ("nonwhite", "ab 12 cde 456 fghi .,\niou", "(?<nonwhite>\\S+)",
307 new string [] { "ab", "12", "cde", "456", "fghi", ".,", "iou" }),
308 new MatchCollectionTrial ("nondigit", "ab0cd1ef2", "(?<nondigit>\\D+)",
309 new string [] { "ab", "cd", "ef" })
312 static void runTrial (MatchCollectionTrial t)
318 static void runTrial (MatchCollectionTrial t, bool rtl)
323 string name = t.name;
327 int len = t.matches.Length;
328 Regex r = new Regex (t.regex, rtl ? RegexOptions.RightToLeft : RegexOptions.None);
330 // Incremental mode -- this access
331 mc = r.Matches (t.text);
332 for (i = 0; i < len; ++i)
333 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:this:{1}", name, i);
334 Assert.AreEqual (i, mc.Count, "{0}:this:count", name);
336 // Incremental mode -- enumerator
337 mc = r.Matches (t.text);
339 foreach (Match m in mc) {
340 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:enum:{1}", name, i);
343 Assert.AreEqual (i, len, "{0}:enum:count", name);
346 Random rng = new Random ();
347 for (int j = 0; j < len * 5; ++j) {
349 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:random{1}:{2}", name, j, i);
352 // Non-incremental mode
353 mc = r.Matches (t.text);
354 Assert.AreEqual (mc.Count, len);
356 foreach (Match m in mc) {
357 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:nienum:{1}", name, i);
360 for (i = 0; i < len; ++i)
361 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:nithis:{1}", name, i);
365 public void Matches ()
367 foreach (MatchCollectionTrial t in trials)
372 public void CacheSize ()
374 Assert.AreEqual (15, Regex.CacheSize, "CacheSize");
376 Regex.CacheSize = Int32.MaxValue;
380 [ExpectedException (typeof (ArgumentOutOfRangeException))]
381 public void CacheSize_Negative ()
383 Regex.CacheSize = -1;
387 [ExpectedException (typeof (ArgumentOutOfRangeException))]
388 public void CacheSize_Min ()
390 Regex.CacheSize = Int32.MinValue;
393 static IEnumerable<uint> Primes (uint m)
400 Dictionary<uint, uint> w = new Dictionary<uint, uint> ();
403 for (uint n = 3; n < m; n += 2) {
404 if (w.TryGetValue (n, out p2)) {
412 // if there's an overflow, don't bother
413 if (n1 / n != n || n1 >= m)
417 while (w.ContainsKey (n1))
424 public void PrimeRegex ()
426 // Perl regex oneliner by: abigail@fnx.com (Abigail)
427 // from: http://www.mit.edu:8008/bloom-picayune.mit.edu/perl/10138
428 // perl -wle 'print "Prime" if (1 x shift) !~ /^1?$|^(11+?)\1+$/'
430 // This is a backtracking torture test
432 Regex composite = new Regex (@"^1?$|^(11+?)\1+$");
437 foreach (uint p in Primes (3333)) {
439 Assert.IsTrue (composite.IsMatch (x));
444 Assert.IsFalse (composite.IsMatch (x));