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