File: RegexTrial.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (108 lines) | stat: -rw-r--r-- 2,246 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Text.RegularExpressions;

using NUnit.Framework;

namespace MonoTests.System.Text.RegularExpressions {

	class RegexTrial {
		public string pattern;
		public RegexOptions options;
		public string input;

		public string expected;
		public string error = "";

		public RegexTrial (string pattern, RegexOptions options, string input, string expected)
		{
			this.pattern = pattern;
			this.options = options;
			this.input = input;
			this.expected = expected;
		}

		public string Expected {
			get { return expected; }
		}
		
		public string Error {
			get { return this.error; }
		}

		public void Execute ()
		{
			string result;

			for (int compiled = 0; compiled < 2; ++compiled) {
				RegexOptions real_options = (compiled == 1) ? (options | RegexOptions.Compiled) : options;
				try {
					Regex re = new Regex (pattern, real_options);
					int [] group_nums = re.GetGroupNumbers ();
					Match m = re.Match (input);

					if (m.Success) {
						result = "Pass.";

						for (int i = 0; i < m.Groups.Count; ++ i) {
							int gid = group_nums [i];
							Group group = m.Groups [gid];

							result += " Group[" + gid + "]=";
							foreach (Capture cap in group.Captures)
								result += "(" + cap.Index + "," + cap.Length + ")";
						}
					} else {
						result = "Fail.";
					}
				}
				catch (Exception e) {
					error = e.Message + "\n" + e.StackTrace + "\n\n";

					result = "Error.";
				}

				Assert.AreEqual (expected, result,
								 "Matching input '{0}' against pattern '{1}' with options '{2}'", input, pattern, real_options);
			}
		}
	}

	class Checksum {
		public Checksum () {
			this.sum = 0;
		}

		public uint Value {
			get { return sum; }
		}

		public void Add (string str) {
			for (int i = 0; i < str.Length; ++ i)
				Add (str[i], 16);
		}

		public void Add (uint n) {
			Add (n, 32);
		}

		public void Add (ulong n, int bits) {
			ulong mask = 1ul << (bits - 1);
			for (int i = 0; i < bits; ++ i) {
				Add ((n & mask) != 0);
				mask >>= 1;
			}
		}

		public void Add (bool bit) {
			bool top = (sum & 0x80000000) != 0;
			sum <<= 1;
			sum ^= bit ? (uint)1 : (uint)0;

			if (top)
				sum ^= key;
		}

		private uint sum;
		private readonly uint key = 0x04c11db7;
	}
}