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);
68 public void Unescape ()
70 string inString = @"\a\b\t\r\v\f\n\e\02400\x231\cC\ufffff\*";
71 char [] c = { (char)7, (char)8, (char)9, (char)13,
72 (char)11, (char)12, (char)10, (char)27, (char) 20,
73 (char)48, (char)48, (char)35, (char)49,
74 (char)3, (char)65535, (char)102, (char)42
76 string expectedString = new String(c);
77 string outString = Regex.Unescape(inString);
79 Assert.AreEqual (outString, expectedString, "unescape");
85 Regex email = new Regex ("(?<user>[^@]+)@(?<domain>.+)");
88 m = email.Match ("mono@go-mono.com");
90 Assert.IsTrue (m.Success, "#m01");
91 Assert.AreEqual ("mono", m.Groups ["user"].Value, "#m02");
92 Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m03");
94 m = email.Match ("mono.bugs@go-mono.com");
95 Assert.IsTrue (m.Success, "m04");
96 Assert.AreEqual ("mono.bugs", m.Groups ["user"].Value, "#m05");
97 Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m06");
101 public void Match2 ()
103 Regex regex = new Regex(@"(?<tab>\t)|(?<text>[^\t]*)");
104 MatchCollection col = regex.Matches("\tjust a text");
105 Assert.AreEqual(3, col.Count);
106 Assert.AreEqual (col [0].Value, "\t");
107 Assert.AreEqual (col [1].Value, "just a text");
108 Assert.AreEqual(col[2].Value, string.Empty);
111 [Test, ExpectedException (typeof (ArgumentNullException))]
112 public void Match_Null1 ()
114 new Regex (@"foo").Match (null);
117 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
118 public void Match_BadStart1 ()
120 new Regex (@"foo").Match ("foobar", -1);
123 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
124 public void Match_BadStart2 ()
126 new Regex (@"foo").Match ("foobar", -1, 0);
129 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
130 public void Match_BadStart3 ()
132 new Regex (@"foo").Match ("foobar", 7);
135 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
136 public void Match_BadStart4 ()
138 new Regex (@"foo").Match ("foobar", 7, 0);
141 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
142 public void Match_BadLength1 ()
144 new Regex (@"foo").Match ("foobar", 5, -1);
147 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
148 public void Match_BadLength2 ()
150 new Regex (@"foo").Match ("foobar", 5, 3);
153 [Test, ExpectedException (typeof (ArgumentNullException))]
154 public void Matches_Null1 ()
156 new Regex (@"foo").Matches (null);
159 [Test, ExpectedException (typeof (ArgumentNullException))]
160 public void Matches_Null2 ()
162 new Regex (@"foo").Matches (null, 0);
165 [Test, ExpectedException (typeof (ArgumentNullException))]
166 public void Matches_Null3 ()
168 new Regex (@"foo", RegexOptions.RightToLeft).Matches (null);
171 [Test, ExpectedException (typeof (ArgumentNullException))]
172 public void Replace_InputNull ()
174 Regex r = new Regex ("^.*$");
175 MatchEvaluator m = delegate (Match match) {return null;};
176 r.Replace (null, m, 0, 0);
179 [Test, ExpectedException (typeof (ArgumentNullException))]
180 public void Replace_InputNull2 ()
182 Regex r = new Regex ("^.*$");
183 r.Replace (null, "abc", 0, 0);
186 [Test, ExpectedException (typeof (ArgumentNullException))]
187 public void Replace_InputNull3 ()
189 Regex r = new Regex ("^.*$", RegexOptions.RightToLeft);
190 MatchEvaluator m = delegate (Match match) {return null;};
194 [Test, ExpectedException (typeof (ArgumentNullException))]
195 public void Replace_InputNull4 ()
197 Regex r = new Regex ("^.*$", RegexOptions.RightToLeft);
198 r.Replace (null, "abc");
201 [Test, ExpectedException (typeof (ArgumentNullException))]
202 public void Replace_ReplacementNull ()
204 Regex r = new Regex ("^.*$");
205 r.Replace ("string", (string) null, 0, 0);
208 [Test, ExpectedException (typeof (ArgumentNullException))]
209 public void Replace_EvaluatorNull ()
211 Regex r = new Regex ("^.*$");
212 MatchEvaluator m = null;
213 r.Replace ("string", m, 0, 0);
216 [Test, ExpectedException (typeof (ArgumentNullException))]
217 public void Split_InputNull1 ()
219 Regex.Split (null, "^.*$");
222 [Test, ExpectedException (typeof (ArgumentNullException))]
223 public void Split_InputNull2 ()
225 Regex.Split (null, "^.*$", RegexOptions.RightToLeft);
228 static string story =
229 "Two little dragons lived in the forest\n" +
230 "They spent their days collecting honey suckle,\n" +
231 "And eating curds and whey\n" +
232 "Until an evil sorcer came along\n" +
233 "And chased my dragon friends away";
235 struct MatchCollectionTrial {
236 public readonly string name;
237 public readonly string text;
238 public readonly string regex;
239 public readonly string [] matches;
240 public MatchCollectionTrial (string name, string text, string regex, string [] matches)
245 this.matches = matches;
249 static readonly MatchCollectionTrial [] trials = {
250 new MatchCollectionTrial ("word", "the fat cat ate the rat", "(?<word>\\w+)",
251 new string [] { "the", "fat", "cat", "ate", "the", "rat" }),
252 new MatchCollectionTrial ("digit", "0 1 2 3 4 5 6a7b8c9d10", "(?<digit>\\d+)",
253 new string [] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }),
254 new MatchCollectionTrial ("line", story, "(?<line>.+)",
255 new string [] { "Two little dragons lived in the forest",
256 "They spent their days collecting honey suckle,",
257 "And eating curds and whey",
258 "Until an evil sorcer came along",
259 "And chased my dragon friends away" }),
260 new MatchCollectionTrial ("nonwhite", "ab 12 cde 456 fghi .,\niou", "(?<nonwhite>\\S+)",
261 new string [] { "ab", "12", "cde", "456", "fghi", ".,", "iou" }),
262 new MatchCollectionTrial ("nondigit", "ab0cd1ef2", "(?<nondigit>\\D+)",
263 new string [] { "ab", "cd", "ef" })
266 static void runTrial (MatchCollectionTrial t)
272 static void runTrial (MatchCollectionTrial t, bool rtl)
277 string name = t.name;
281 int len = t.matches.Length;
282 Regex r = new Regex (t.regex, rtl ? RegexOptions.RightToLeft : RegexOptions.None);
284 // Incremental mode -- this access
285 mc = r.Matches (t.text);
286 for (i = 0; i < len; ++i)
287 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:this:{1}", name, i);
288 Assert.AreEqual (i, mc.Count, "{0}:this:count", name);
290 // Incremental mode -- enumerator
291 mc = r.Matches (t.text);
293 foreach (Match m in mc) {
294 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:enum:{1}", name, i);
297 Assert.AreEqual (i, len, "{0}:enum:count", name);
300 Random rng = new Random ();
301 for (int j = 0; j < len * 5; ++j) {
303 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:random{1}:{2}", name, j, i);
306 // Non-incremental mode
307 mc = r.Matches (t.text);
308 Assert.AreEqual (mc.Count, len);
310 foreach (Match m in mc) {
311 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:nienum:{1}", name, i);
314 for (i = 0; i < len; ++i)
315 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:nithis:{1}", name, i);
319 public void Matches ()
321 foreach (MatchCollectionTrial t in trials)
326 public void CacheSize ()
328 Assert.AreEqual (15, Regex.CacheSize, "CacheSize");
330 Regex.CacheSize = Int32.MaxValue;
334 [ExpectedException (typeof (ArgumentOutOfRangeException))]
335 public void CacheSize_Negative ()
337 Regex.CacheSize = -1;
341 [ExpectedException (typeof (ArgumentOutOfRangeException))]
342 public void CacheSize_Min ()
344 Regex.CacheSize = Int32.MinValue;
347 static IEnumerable<uint> Primes (uint m)
354 Dictionary<uint, uint> w = new Dictionary<uint, uint> ();
357 for (uint n = 3; n < m; n += 2) {
358 if (w.TryGetValue (n, out p2)) {
366 // if there's an overflow, don't bother
367 if (n1 / n != n || n1 >= m)
371 while (w.ContainsKey (n1))
378 public void PrimeRegex ()
380 // Perl regex oneliner by: abigail@fnx.com (Abigail)
381 // from: http://www.mit.edu:8008/bloom-picayune.mit.edu/perl/10138
382 // perl -wle 'print "Prime" if (1 x shift) !~ /^1?$|^(11+?)\1+$/'
384 // This is a backtracking torture test
386 Regex composite = new Regex (@"^1?$|^(11+?)\1+$");
391 foreach (uint p in Primes (3333)) {
393 Assert.IsTrue (composite.IsMatch (x));
398 Assert.IsFalse (composite.IsMatch (x));