New test.
[mono.git] / mcs / class / System / Test / System.Text.RegularExpressions / RegexTrial.cs
1 using System;
2 using System.Text.RegularExpressions;
3
4 using NUnit.Framework;
5
6 namespace MonoTests.System.Text.RegularExpressions {
7
8         class RegexTrial {
9                 public string pattern;
10                 public RegexOptions options;
11                 public string input;
12
13                 public string expected;
14                 public string error = "";
15
16                 public RegexTrial (string pattern, RegexOptions options, string input, string expected)
17                 {
18                         this.pattern = pattern;
19                         this.options = options;
20                         this.input = input;
21                         this.expected = expected;
22                 }
23
24                 public string Expected {
25                         get { return expected; }
26                 }
27                 
28                 public string Error {
29                         get { return this.error; }
30                 }
31
32                 public void Execute ()
33                 {
34                         string result;
35
36                         try {
37                                 Regex re = new Regex (pattern, options);
38                                 Match m = re.Match (input);
39
40                                 if (m.Success) {
41                                         result = "Pass.";
42
43                                         for (int i = 0; i < m.Groups.Count; ++ i) {
44                                                 Group group = m.Groups [i];
45
46                                                 result += " Group[" + i + "]=";
47                                                 foreach (Capture cap in group.Captures)
48                                                         result += "(" + cap.Index + "," + cap.Length + ")";
49                                         }
50                                 } else {
51                                         result = "Fail.";
52                                 }
53                         }
54                         catch (Exception e) {
55                                 error = e.Message + "\n" + e.StackTrace + "\n\n";
56
57                                 result = "Error.";
58                         }
59
60                         Assert.AreEqual (expected, result,
61                                          "Matching input '{0}' against pattern '{1}' with options '{2}'", input, pattern, options);
62                 }
63         }
64
65         class Checksum {
66                 public Checksum () {
67                         this.sum = 0;
68                 }
69
70                 public uint Value {
71                         get { return sum; }
72                 }
73
74                 public void Add (string str) {
75                         for (int i = 0; i < str.Length; ++ i)
76                                 Add (str[i], 16);
77                 }
78
79                 public void Add (uint n) {
80                         Add (n, 32);
81                 }
82
83                 public void Add (ulong n, int bits) {
84                         ulong mask = 1ul << (bits - 1);
85                         for (int i = 0; i < bits; ++ i) {
86                                 Add ((n & mask) != 0);
87                                 mask >>= 1;
88                         }
89                 }
90
91                 public void Add (bool bit) {
92                         bool top = (sum & 0x80000000) != 0;
93                         sum <<= 1;
94                         sum ^= bit ? (uint)1 : (uint)0;
95
96                         if (top)
97                                 sum ^= key;
98                 }
99
100                 private uint sum;
101                 private readonly uint key = 0x04c11db7;
102         }
103 }