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");
50 public void Unescape ()
52 string inString = @"\a\b\t\r\v\f\n\e\02400\x231\cC\ufffff\*";
53 char [] c = { (char)7, (char)8, (char)9, (char)13,
54 (char)11, (char)12, (char)10, (char)27, (char) 20,
55 (char)48, (char)48, (char)35, (char)49,
56 (char)3, (char)65535, (char)102, (char)42
58 string expectedString = new String(c);
59 string outString = Regex.Unescape(inString);
61 Assert.AreEqual (outString, expectedString, "unescape");
67 Regex email = new Regex ("(?<user>[^@]+)@(?<domain>.+)");
70 m = email.Match ("mono@go-mono.com");
72 Assert.IsTrue (m.Success, "#m01");
73 Assert.AreEqual ("mono", m.Groups ["user"].Value, "#m02");
74 Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m03");
76 m = email.Match ("mono.bugs@go-mono.com");
77 Assert.IsTrue (m.Success, "m04");
78 Assert.AreEqual ("mono.bugs", m.Groups ["user"].Value, "#m05");
79 Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m06");
83 "Two little dragons lived in the forest\n" +
84 "They spent their days collecting honey suckle,\n" +
85 "And eating curds and whey\n" +
86 "Until an evil sorcer came along\n" +
87 "And chased my dragon friends away";
89 struct MatchCollectionTrial {
90 public readonly string name;
91 public readonly string text;
92 public readonly string regex;
93 public readonly string [] matches;
94 public MatchCollectionTrial (string name, string text, string regex, string [] matches)
99 this.matches = matches;
103 static readonly MatchCollectionTrial [] trials = {
104 new MatchCollectionTrial ("word", "the fat cat ate the rat", "(?<word>\\w+)",
105 new string [] { "the", "fat", "cat", "ate", "the", "rat" }),
106 new MatchCollectionTrial ("digit", "0 1 2 3 4 5 6a7b8c9d10", "(?<digit>\\d+)",
107 new string [] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }),
108 new MatchCollectionTrial ("line", story, "(?<line>.+)",
109 new string [] { "Two little dragons lived in the forest",
110 "They spent their days collecting honey suckle,",
111 "And eating curds and whey",
112 "Until an evil sorcer came along",
113 "And chased my dragon friends away" }),
114 new MatchCollectionTrial ("nonwhite", "ab 12 cde 456 fghi .,\niou", "(?<nonwhite>\\S+)",
115 new string [] { "ab", "12", "cde", "456", "fghi", ".,", "iou" }),
116 new MatchCollectionTrial ("nondigit", "ab0cd1ef2", "(?<nondigit>\\D+)",
117 new string [] { "ab", "cd", "ef" })
120 static void runTrial (MatchCollectionTrial t)
126 static void runTrial (MatchCollectionTrial t, bool rtl)
131 string name = t.name;
135 int len = t.matches.Length;
136 Regex r = new Regex (t.regex, rtl ? RegexOptions.RightToLeft : RegexOptions.None);
138 // Incremental mode -- this access
139 mc = r.Matches (t.text);
140 for (i = 0; i < len; ++i)
141 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:this:{1}", name, i);
142 Assert.AreEqual (i, mc.Count, "{0}:this:count", name);
144 // Incremental mode -- enumerator
145 mc = r.Matches (t.text);
147 foreach (Match m in mc) {
148 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:enum:{1}", name, i);
151 Assert.AreEqual (i, len, "{0}:enum:count", name);
154 Random rng = new Random ();
155 for (int j = 0; j < len * 5; ++j) {
157 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:random{1}:{2}", name, j, i);
160 // Non-incremental mode
161 mc = r.Matches (t.text);
162 Assert.AreEqual (mc.Count, len);
164 foreach (Match m in mc) {
165 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:nienum:{1}", name, i);
168 for (i = 0; i < len; ++i)
169 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:nithis:{1}", name, i);
173 public void Matches ()
175 foreach (MatchCollectionTrial t in trials)
180 public void CacheSize ()
182 Assert.AreEqual (15, Regex.CacheSize, "CacheSize");
184 Regex.CacheSize = Int32.MaxValue;
188 [ExpectedException (typeof (ArgumentOutOfRangeException))]
189 public void CacheSize_Negative ()
191 Regex.CacheSize = -1;
195 [ExpectedException (typeof (ArgumentOutOfRangeException))]
196 public void CacheSize_Min ()
198 Regex.CacheSize = Int32.MinValue;
201 static IEnumerable<uint> Primes (uint m)
208 Dictionary<uint, uint> w = new Dictionary<uint, uint> ();
211 for (uint n = 3; n < m; n += 2) {
212 if (w.TryGetValue (n, out p2)) {
220 // if there's an overflow, don't bother
221 if (n1 / n != n || n1 >= m)
225 while (w.ContainsKey (n1))
232 public void PrimeRegex ()
234 // Perl regex oneliner by: abigail@fnx.com (Abigail)
235 // from: http://www.mit.edu:8008/bloom-picayune.mit.edu/perl/10138
236 // perl -wle 'print "Prime" if (1 x shift) !~ /^1?$|^(11+?)\1+$/'
238 // This is a backtracking torture test
240 Regex composite = new Regex (@"^1?$|^(11+?)\1+$");
245 foreach (uint p in Primes (3333)) {
247 Assert.IsTrue (composite.IsMatch (x));
252 Assert.IsFalse (composite.IsMatch (x));