-
Notifications
You must be signed in to change notification settings - Fork 1
/
10.RegularExpressionMatching.Test.cs
69 lines (66 loc) · 2.1 KB
/
10.RegularExpressionMatching.Test.cs
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
using System;
using System.Text;
using System.Text.RegularExpressions;
using NUnit.Framework;
[TestFixture]
public class TestClass : TestClassBase
{
[TestCase("", "", true)]
[TestCase("", ".*", true)]
[TestCase("", "a*", true)]
[TestCase("", ".", false)]
[TestCase("aa", "a", false)]
[TestCase("aa", "aa", true)]
[TestCase("aaa", "aa", false)]
[TestCase("a", "a*", true)]
[TestCase("aa", "a*", true)]
[TestCase("aaa", "a*", true)]
[TestCase("aaaaaaaa", "a*", true)]
[TestCase("aa", ".*", true)]
[TestCase("ab", ".*", true)]
[TestCase("aab", "c*a*b", true)]
[TestCase("aab", "c*.*b", true)]
public void TestMethod1(string s, string p, bool expectedResult)
{
var result = new Solution().IsMatch(s, p);
Assert.AreEqual(expectedResult, result);
}
[TestCase(10, 10, 100)]
public void TestMethod2(int sMaxLength, int pMaxLength, int testCount)
{
Repeat(testCount, () => {
for (var i = 0; i < testCount; ++i)
{
var s = GenerateString(Random, sMaxLength, false);
var p = GenerateString(Random, pMaxLength, true);
var expectedResult = Regex.Match(s, string.Format("^{0}$", p)).Success;
var result = new Solution().IsMatch(s, p);
Assert.AreEqual(expectedResult, result, string.Format("s={0} p={1}", s, p));
}
});
}
private string GenerateString(Random random, int maxLength, bool isRegex)
{
var length = random.Next(maxLength + 1);
var sb = new StringBuilder(length);
char ch = '*';
for (var i = 0; i < length; ++i)
{
var op = random.Next(isRegex ? 28 : 26);
switch (op)
{
case 26:
ch = '.';
break;
case 27:
ch = ch != '*' ? '*' : '.';
break;
default:
ch = (char)(op + 'a');
break;
}
sb.Append(ch);
}
return sb.ToString();
}
}