Clean up formatting
[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                 [Test]
22                 public void Simple ()
23                 {
24                         char[] c = { (char)32, (char)8212, (char)32 };
25                         string s = new String(c);                       
26                         Assert.IsTrue (Regex.IsMatch(s, s), "char");
27                 }
28                 
29                 [Test]
30                 public void Unescape ()
31                 {
32                         string inString = @"\a\b\t\r\v\f\n\e\02400\x231\cC\ufffff\*";
33                         char [] c = { (char)7, (char)8, (char)9, (char)13, 
34                                       (char)11, (char)12, (char)10, (char)27, (char) 20,
35                                       (char)48, (char)48, (char)35, (char)49, 
36                                       (char)3, (char)65535, (char)102, (char)42
37                         };
38                         string expectedString = new String(c);
39                         string outString = Regex.Unescape(inString);
40
41                         Assert.AreEqual (outString, expectedString, "unescape");
42                 }
43
44                 [Test]
45                 public void Match1 ()
46                 {
47                         Regex email = new Regex ("(?<user>[^@]+)@(?<domain>.+)");
48                         Match m;
49
50                         m = email.Match ("mono@go-mono.com");
51
52                         Assert.IsTrue (m.Success, "#m01");
53                         Assert.AreEqual ("mono", m.Groups ["user"].Value, "#m02");
54                         Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m03");
55
56                         m = email.Match ("mono.bugs@go-mono.com");
57                         Assert.IsTrue (m.Success, "m04");
58                         Assert.AreEqual ("mono.bugs", m.Groups ["user"].Value, "#m05");
59                         Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m06");
60                 }
61
62                 static string story =   
63                         "Two little dragons lived in the forest\n" +
64                         "They spent their days collecting honey suckle,\n" +
65                         "And eating curds and whey\n" +
66                         "Until an evil sorcer came along\n" +
67                         "And chased my dragon friends away";
68
69                 struct MatchCollectionTrial {
70                         public readonly string name;
71                         public readonly string text;
72                         public readonly string regex;
73                         public readonly string [] matches;
74                         public MatchCollectionTrial (string name, string text, string regex, string [] matches)
75                         {
76                                 this.name = name;
77                                 this.text = text;
78                                 this.regex = regex;
79                                 this.matches = matches;
80                         }
81                 }
82
83                 static readonly MatchCollectionTrial [] trials = {
84                         new MatchCollectionTrial ("word", "the fat cat ate the rat", "(?<word>\\w+)", 
85                                 new string [] { "the", "fat", "cat", "ate", "the", "rat" }),
86                         new MatchCollectionTrial ("digit", "0 1 2 3 4 5 6a7b8c9d10", "(?<digit>\\d+)", 
87                                 new string [] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }),
88                         new MatchCollectionTrial ("line", story, "(?<line>.+)", 
89                                 new string [] { "Two little dragons lived in the forest",
90                                                 "They spent their days collecting honey suckle,",
91                                                 "And eating curds and whey",
92                                                 "Until an evil sorcer came along",
93                                                 "And chased my dragon friends away" }),
94                         new MatchCollectionTrial ("nonwhite", "ab 12 cde 456 fghi .,\niou", "(?<nonwhite>\\S+)",
95                                 new string [] { "ab", "12", "cde", "456", "fghi", ".,", "iou" }),
96                         new MatchCollectionTrial ("nondigit", "ab0cd1ef2", "(?<nondigit>\\D+)",
97                                 new string [] { "ab", "cd", "ef" })
98                 };
99
100                 static void runTrial (MatchCollectionTrial t)
101                 {
102                         runTrial (t, false);
103                         runTrial (t, true);
104                 }
105
106                 static void runTrial (MatchCollectionTrial t, bool rtl)
107                 {
108                         int i;
109                         MatchCollection mc;
110
111                         string name = t.name;
112                         if (rtl)
113                                 name += "-rtl";
114
115                         int len = t.matches.Length;
116                         Regex r = new Regex (t.regex, rtl ? RegexOptions.RightToLeft : RegexOptions.None);
117
118                         // Incremental mode -- this access
119                         mc = r.Matches (t.text);
120                         for (i = 0; i < len; ++i)
121                                 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:this:{1}", name, i);
122                         Assert.AreEqual (i, mc.Count, "{0}:this:count", name);
123
124                         // Incremental mode -- enumerator
125                         mc = r.Matches (t.text);
126                         i = 0;
127                         foreach (Match m in mc) {
128                                 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:enum:{1}", name, i);
129                                 ++i;
130                         }
131                         Assert.AreEqual (i, len, "{0}:enum:count", name);
132
133                         // random mode
134                         Random rng = new Random ();
135                         for (int j = 0; j < len * 5; ++j) {
136                                 i = rng.Next (len);
137                                 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:random{1}:{2}", name, j, i);
138                         }
139
140                         // Non-incremental mode
141                         mc = r.Matches (t.text);
142                         Assert.AreEqual (mc.Count, len);
143                         i = 0;
144                         foreach (Match m in mc) {
145                                 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:nienum:{1}", name, i);
146                                 ++i;
147                         }
148                         for (i = 0; i < len; ++i)
149                                 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:nithis:{1}", name, i);
150                 }
151
152                 [Test]
153                 public void Matches ()
154                 {
155                         int i;
156                         MatchCollection mc;
157                         Regex r;
158                         foreach (MatchCollectionTrial t in trials)
159                                 runTrial (t);
160                 }
161         }
162 }