processed with astyle
[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 {
26             return expected;
27         }
28     }
29
30     public string Error {
31         get {
32             return this.error;
33         }
34     }
35
36     public void Execute ()
37     {
38         string result;
39
40         for (int compiled = 0; compiled < 2; ++compiled) {
41             RegexOptions real_options = (compiled == 1) ? (options | RegexOptions.Compiled) : options;
42             try {
43                 Regex re = new Regex (pattern, real_options);
44                 int [] group_nums = re.GetGroupNumbers ();
45                 Match m = re.Match (input);
46
47                 if (m.Success) {
48                     result = "Pass.";
49
50                     for (int i = 0; i < m.Groups.Count; ++ i) {
51                         int gid = group_nums [i];
52                         Group group = m.Groups [gid];
53
54                         result += " Group[" + gid + "]=";
55                         foreach (Capture cap in group.Captures)
56                             result += "(" + cap.Index + "," + cap.Length + ")";
57                     }
58                 } else {
59                     result = "Fail.";
60                 }
61             }
62             catch (Exception e) {
63                 error = e.Message + "\n" + e.StackTrace + "\n\n";
64
65                 result = "Error.";
66             }
67
68             Assert.AreEqual (expected, result,
69                              "Matching input '{0}' against pattern '{1}' with options '{2}'", input, pattern, real_options);
70         }
71     }
72 }
73
74 class Checksum {
75     public Checksum () {
76         this.sum = 0;
77     }
78
79     public uint Value {
80         get {
81             return sum;
82         }
83     }
84
85     public void Add (string str) {
86         for (int i = 0; i < str.Length; ++ i)
87             Add (str[i], 16);
88     }
89
90     public void Add (uint n) {
91         Add (n, 32);
92     }
93
94     public void Add (ulong n, int bits) {
95         ulong mask = 1ul << (bits - 1);
96         for (int i = 0; i < bits; ++ i) {
97             Add ((n & mask) != 0);
98             mask >>= 1;
99         }
100     }
101
102     public void Add (bool bit) {
103         bool top = (sum & 0x80000000) != 0;
104         sum <<= 1;
105         sum ^= bit ? (uint)1 : (uint)0;
106
107         if (top)
108             sum ^= key;
109     }
110
111     private uint sum;
112     private readonly uint key = 0x04c11db7;
113 }
114 }