New test.
[mono.git] / mcs / class / System / Test / System.Text.RegularExpressions / RegexTest.cs
1 //
2 // assembly:    System_test
3 // namespace:   MonoTests.System.Text.RegularExpressions
4 // file:        RegexTest.cs
5 //
6 // Authors:     
7 //   Juraj Skripsky (juraj@hotfeet.ch)
8 //
9 // (c) 2003 Juraj Skripsky
10
11 using System;
12 using System.Text.RegularExpressions;
13
14 using NUnit.Framework;
15
16 namespace MonoTests.System.Text.RegularExpressions {
17         
18         [TestFixture]
19         public class RegexTest {
20
21 #if NET_2_0
22                 private int cache_initial_value;
23
24                 [TestFixtureSetUp]
25                 public void FixtureSetUp ()
26                 {
27                         cache_initial_value = Regex.CacheSize;
28                 }
29
30                 [TearDown]
31                 public void TearDown ()
32                 {
33                         Regex.CacheSize = cache_initial_value;
34                 }
35 #endif
36                 [Test]
37                 public void Simple ()
38                 {
39                         char[] c = { (char)32, (char)8212, (char)32 };
40                         string s = new String(c);                       
41                         Assert.IsTrue (Regex.IsMatch(s, s), "char");
42                 }
43                 
44                 [Test]
45                 public void Unescape ()
46                 {
47                         string inString = @"\a\b\t\r\v\f\n\e\02400\x231\cC\ufffff\*";
48                         char [] c = { (char)7, (char)8, (char)9, (char)13, 
49                                       (char)11, (char)12, (char)10, (char)27, (char) 20,
50                                       (char)48, (char)48, (char)35, (char)49, 
51                                       (char)3, (char)65535, (char)102, (char)42
52                         };
53                         string expectedString = new String(c);
54                         string outString = Regex.Unescape(inString);
55
56                         Assert.AreEqual (outString, expectedString, "unescape");
57                 }
58
59                 [Test]
60                 public void Match1 ()
61                 {
62                         Regex email = new Regex ("(?<user>[^@]+)@(?<domain>.+)");
63                         Match m;
64
65                         m = email.Match ("mono@go-mono.com");
66
67                         Assert.IsTrue (m.Success, "#m01");
68                         Assert.AreEqual ("mono", m.Groups ["user"].Value, "#m02");
69                         Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m03");
70
71                         m = email.Match ("mono.bugs@go-mono.com");
72                         Assert.IsTrue (m.Success, "m04");
73                         Assert.AreEqual ("mono.bugs", m.Groups ["user"].Value, "#m05");
74                         Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m06");
75                 }
76
77                 static string story =   
78                         "Two little dragons lived in the forest\n" +
79                         "They spent their days collecting honey suckle,\n" +
80                         "And eating curds and whey\n" +
81                         "Until an evil sorcer came along\n" +
82                         "And chased my dragon friends away";
83
84                 struct MatchCollectionTrial {
85                         public readonly string name;
86                         public readonly string text;
87                         public readonly string regex;
88                         public readonly string [] matches;
89                         public MatchCollectionTrial (string name, string text, string regex, string [] matches)
90                         {
91                                 this.name = name;
92                                 this.text = text;
93                                 this.regex = regex;
94                                 this.matches = matches;
95                         }
96                 }
97
98                 static readonly MatchCollectionTrial [] trials = {
99                         new MatchCollectionTrial ("word", "the fat cat ate the rat", "(?<word>\\w+)", 
100                                 new string [] { "the", "fat", "cat", "ate", "the", "rat" }),
101                         new MatchCollectionTrial ("digit", "0 1 2 3 4 5 6a7b8c9d10", "(?<digit>\\d+)", 
102                                 new string [] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }),
103                         new MatchCollectionTrial ("line", story, "(?<line>.+)", 
104                                 new string [] { "Two little dragons lived in the forest",
105                                                 "They spent their days collecting honey suckle,",
106                                                 "And eating curds and whey",
107                                                 "Until an evil sorcer came along",
108                                                 "And chased my dragon friends away" }),
109                         new MatchCollectionTrial ("nonwhite", "ab 12 cde 456 fghi .,\niou", "(?<nonwhite>\\S+)",
110                                 new string [] { "ab", "12", "cde", "456", "fghi", ".,", "iou" }),
111                         new MatchCollectionTrial ("nondigit", "ab0cd1ef2", "(?<nondigit>\\D+)",
112                                 new string [] { "ab", "cd", "ef" })
113                 };
114
115                 static void runTrial (MatchCollectionTrial t)
116                 {
117                         runTrial (t, false);
118                         runTrial (t, true);
119                 }
120
121                 static void runTrial (MatchCollectionTrial t, bool rtl)
122                 {
123                         int i;
124                         MatchCollection mc;
125
126                         string name = t.name;
127                         if (rtl)
128                                 name += "-rtl";
129
130                         int len = t.matches.Length;
131                         Regex r = new Regex (t.regex, rtl ? RegexOptions.RightToLeft : RegexOptions.None);
132
133                         // Incremental mode -- this access
134                         mc = r.Matches (t.text);
135                         for (i = 0; i < len; ++i)
136                                 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:this:{1}", name, i);
137                         Assert.AreEqual (i, mc.Count, "{0}:this:count", name);
138
139                         // Incremental mode -- enumerator
140                         mc = r.Matches (t.text);
141                         i = 0;
142                         foreach (Match m in mc) {
143                                 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:enum:{1}", name, i);
144                                 ++i;
145                         }
146                         Assert.AreEqual (i, len, "{0}:enum:count", name);
147
148                         // random mode
149                         Random rng = new Random ();
150                         for (int j = 0; j < len * 5; ++j) {
151                                 i = rng.Next (len);
152                                 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:random{1}:{2}", name, j, i);
153                         }
154
155                         // Non-incremental mode
156                         mc = r.Matches (t.text);
157                         Assert.AreEqual (mc.Count, len);
158                         i = 0;
159                         foreach (Match m in mc) {
160                                 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:nienum:{1}", name, i);
161                                 ++i;
162                         }
163                         for (i = 0; i < len; ++i)
164                                 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:nithis:{1}", name, i);
165                 }
166
167                 [Test]
168                 public void Matches ()
169                 {
170                         int i;
171                         MatchCollection mc;
172                         Regex r;
173                         foreach (MatchCollectionTrial t in trials)
174                                 runTrial (t);
175                 }
176 #if NET_2_0
177                 [Test]
178                 public void CacheSize ()
179                 {
180                         Assert.AreEqual (15, Regex.CacheSize, "CacheSize");
181                         Regex.CacheSize = 0;
182                         Regex.CacheSize = Int32.MaxValue;
183                 }
184
185                 [Test]
186                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
187                 public void CacheSize_Negative ()
188                 {
189                         Regex.CacheSize = -1;
190                 }
191
192                 [Test]
193                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
194                 public void CacheSize_Min ()
195                 {
196                         Regex.CacheSize = Int32.MinValue;
197                 }
198 #endif
199         }
200 }