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.Replace ("foo", "foo|bar", "baz", -4);
228 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
229 public void Replace_InvalidStart ()
231 Regex.Replace ("foo", "foo|bar", "baz", 1, -4);
234 [Test, ExpectedException (typeof (ArgumentNullException))]
235 public void Split_InputNull1 ()
237 Regex.Split (null, "^.*$");
240 [Test, ExpectedException (typeof (ArgumentNullException))]
241 public void Split_InputNull2 ()
243 Regex.Split (null, "^.*$", RegexOptions.RightToLeft);
246 [Test, ExpectedException (typeof (ArgumentNullException))]
247 public void Split_InvalidCount ()
249 Regex.Split (null, "^.*$", -4);
252 [Test, ExpectedException (typeof (ArgumentNullException))]
253 public void Split_InvalidCount ()
255 Regex.Split (null, "^.*$", 1, -4);
258 [Test, ExpectedException (typeof (ArgumentNullException))]
259 public void Escape_Null ()
264 [Test, ExpectedException (typeof (ArgumentNullException))]
265 public void Unescape_Null ()
267 Regex.Unescape (null);
270 static string story =
271 "Two little dragons lived in the forest\n" +
272 "They spent their days collecting honey suckle,\n" +
273 "And eating curds and whey\n" +
274 "Until an evil sorcer came along\n" +
275 "And chased my dragon friends away";
277 struct MatchCollectionTrial {
278 public readonly string name;
279 public readonly string text;
280 public readonly string regex;
281 public readonly string [] matches;
282 public MatchCollectionTrial (string name, string text, string regex, string [] matches)
287 this.matches = matches;
291 static readonly MatchCollectionTrial [] trials = {
292 new MatchCollectionTrial ("word", "the fat cat ate the rat", "(?<word>\\w+)",
293 new string [] { "the", "fat", "cat", "ate", "the", "rat" }),
294 new MatchCollectionTrial ("digit", "0 1 2 3 4 5 6a7b8c9d10", "(?<digit>\\d+)",
295 new string [] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }),
296 new MatchCollectionTrial ("line", story, "(?<line>.+)",
297 new string [] { "Two little dragons lived in the forest",
298 "They spent their days collecting honey suckle,",
299 "And eating curds and whey",
300 "Until an evil sorcer came along",
301 "And chased my dragon friends away" }),
302 new MatchCollectionTrial ("nonwhite", "ab 12 cde 456 fghi .,\niou", "(?<nonwhite>\\S+)",
303 new string [] { "ab", "12", "cde", "456", "fghi", ".,", "iou" }),
304 new MatchCollectionTrial ("nondigit", "ab0cd1ef2", "(?<nondigit>\\D+)",
305 new string [] { "ab", "cd", "ef" })
308 static void runTrial (MatchCollectionTrial t)
314 static void runTrial (MatchCollectionTrial t, bool rtl)
319 string name = t.name;
323 int len = t.matches.Length;
324 Regex r = new Regex (t.regex, rtl ? RegexOptions.RightToLeft : RegexOptions.None);
326 // Incremental mode -- this access
327 mc = r.Matches (t.text);
328 for (i = 0; i < len; ++i)
329 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:this:{1}", name, i);
330 Assert.AreEqual (i, mc.Count, "{0}:this:count", name);
332 // Incremental mode -- enumerator
333 mc = r.Matches (t.text);
335 foreach (Match m in mc) {
336 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:enum:{1}", name, i);
339 Assert.AreEqual (i, len, "{0}:enum:count", name);
342 Random rng = new Random ();
343 for (int j = 0; j < len * 5; ++j) {
345 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:random{1}:{2}", name, j, i);
348 // Non-incremental mode
349 mc = r.Matches (t.text);
350 Assert.AreEqual (mc.Count, len);
352 foreach (Match m in mc) {
353 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:nienum:{1}", name, i);
356 for (i = 0; i < len; ++i)
357 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:nithis:{1}", name, i);
361 public void Matches ()
363 foreach (MatchCollectionTrial t in trials)
368 public void CacheSize ()
370 Assert.AreEqual (15, Regex.CacheSize, "CacheSize");
372 Regex.CacheSize = Int32.MaxValue;
376 [ExpectedException (typeof (ArgumentOutOfRangeException))]
377 public void CacheSize_Negative ()
379 Regex.CacheSize = -1;
383 [ExpectedException (typeof (ArgumentOutOfRangeException))]
384 public void CacheSize_Min ()
386 Regex.CacheSize = Int32.MinValue;
389 static IEnumerable<uint> Primes (uint m)
396 Dictionary<uint, uint> w = new Dictionary<uint, uint> ();
399 for (uint n = 3; n < m; n += 2) {
400 if (w.TryGetValue (n, out p2)) {
408 // if there's an overflow, don't bother
409 if (n1 / n != n || n1 >= m)
413 while (w.ContainsKey (n1))
420 public void PrimeRegex ()
422 // Perl regex oneliner by: abigail@fnx.com (Abigail)
423 // from: http://www.mit.edu:8008/bloom-picayune.mit.edu/perl/10138
424 // perl -wle 'print "Prime" if (1 x shift) !~ /^1?$|^(11+?)\1+$/'
426 // This is a backtracking torture test
428 Regex composite = new Regex (@"^1?$|^(11+?)\1+$");
433 foreach (uint p in Primes (3333)) {
435 Assert.IsTrue (composite.IsMatch (x));
440 Assert.IsFalse (composite.IsMatch (x));