|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace System.Text.RegularExpressions.Tests |
| 10 | +{ |
| 11 | + public class RegexCountTests |
| 12 | + { |
| 13 | + [Theory] |
| 14 | + [MemberData(nameof(Count_ReturnsExpectedCount_TestData))] |
| 15 | + public async Task Count_ReturnsExpectedCount(RegexEngine engine, string pattern, string input, RegexOptions options, int expectedCount) |
| 16 | + { |
| 17 | + Regex r = await RegexHelpers.GetRegexAsync(engine, pattern, options); |
| 18 | + Assert.Equal(expectedCount, r.Count(input)); |
| 19 | + Assert.Equal(r.Count(input), r.Matches(input).Count); |
| 20 | + |
| 21 | + if (options == RegexOptions.None && engine == RegexEngine.Interpreter) |
| 22 | + { |
| 23 | + Assert.Equal(expectedCount, Regex.Count(input, pattern)); |
| 24 | + } |
| 25 | + |
| 26 | + switch (engine) |
| 27 | + { |
| 28 | + case RegexEngine.Interpreter: |
| 29 | + case RegexEngine.Compiled: |
| 30 | + case RegexEngine.NonBacktracking: |
| 31 | + RegexOptions engineOptions = RegexHelpers.OptionsFromEngine(engine); |
| 32 | + Assert.Equal(expectedCount, Regex.Count(input, pattern, options | engineOptions)); |
| 33 | + Assert.Equal(expectedCount, Regex.Count(input, pattern, options | engineOptions, Regex.InfiniteMatchTimeout)); |
| 34 | + break; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static IEnumerable<object[]> Count_ReturnsExpectedCount_TestData() |
| 39 | + { |
| 40 | + foreach (RegexEngine engine in RegexHelpers.AvailableEngines) |
| 41 | + { |
| 42 | + yield return new object[] { engine, @"", "", RegexOptions.None, 1 }; |
| 43 | + yield return new object[] { engine, @"", "a", RegexOptions.None, 2 }; |
| 44 | + yield return new object[] { engine, @"", "ab", RegexOptions.None, 3 }; |
| 45 | + |
| 46 | + yield return new object[] { engine, @"\w", "", RegexOptions.None, 0 }; |
| 47 | + yield return new object[] { engine, @"\w", "a", RegexOptions.None, 1 }; |
| 48 | + yield return new object[] { engine, @"\w", "ab", RegexOptions.None, 2 }; |
| 49 | + |
| 50 | + yield return new object[] { engine, @"\b\w+\b", "abc def ghi jkl", RegexOptions.None, 4 }; |
| 51 | + |
| 52 | + yield return new object[] { engine, @"A", "", RegexOptions.IgnoreCase, 0 }; |
| 53 | + yield return new object[] { engine, @"A", "a", RegexOptions.IgnoreCase, 1 }; |
| 54 | + yield return new object[] { engine, @"A", "aAaA", RegexOptions.IgnoreCase, 4 }; |
| 55 | + |
| 56 | + yield return new object[] { engine, @".", "\n\n\n", RegexOptions.None, 0 }; |
| 57 | + yield return new object[] { engine, @".", "\n\n\n", RegexOptions.Singleline, 3 }; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void Count_InvalidArguments_Throws() |
| 63 | + { |
| 64 | + // input is null |
| 65 | + AssertExtensions.Throws<ArgumentNullException>("input", () => new Regex("pattern").Count(null)); |
| 66 | + AssertExtensions.Throws<ArgumentNullException>("input", () => Regex.Count(null, @"pattern")); |
| 67 | + AssertExtensions.Throws<ArgumentNullException>("input", () => Regex.Count(null, @"pattern", RegexOptions.None)); |
| 68 | + AssertExtensions.Throws<ArgumentNullException>("input", () => Regex.Count(null, @"pattern", RegexOptions.None, TimeSpan.FromMilliseconds(1))); |
| 69 | + |
| 70 | + // pattern is null |
| 71 | + AssertExtensions.Throws<ArgumentNullException>("pattern", () => Regex.Count("input", null)); |
| 72 | + AssertExtensions.Throws<ArgumentNullException>("pattern", () => Regex.Count("input", null, RegexOptions.None)); |
| 73 | + AssertExtensions.Throws<ArgumentNullException>("pattern", () => Regex.Count("input", null, RegexOptions.None, TimeSpan.FromMilliseconds(1))); |
| 74 | + |
| 75 | + // pattern is invalid |
| 76 | +#pragma warning disable RE0001 // invalid regex pattern |
| 77 | + AssertExtensions.Throws<RegexParseException>(() => Regex.Count("input", @"[abc")); |
| 78 | + AssertExtensions.Throws<RegexParseException>(() => Regex.Count("input", @"[abc", RegexOptions.None)); |
| 79 | + AssertExtensions.Throws<RegexParseException>(() => Regex.Count("input", @"[abc", RegexOptions.None, TimeSpan.FromMilliseconds(1))); |
| 80 | +#pragma warning restore RE0001 |
| 81 | + |
| 82 | + // options is invalid |
| 83 | + AssertExtensions.Throws<ArgumentOutOfRangeException>("options", () => Regex.Count("input", @"[abc]", (RegexOptions)(-1))); |
| 84 | + AssertExtensions.Throws<ArgumentOutOfRangeException>("options", () => Regex.Count("input", @"[abc]", (RegexOptions)(-1), TimeSpan.FromMilliseconds(1))); |
| 85 | + |
| 86 | + // matchTimeout is invalid |
| 87 | + AssertExtensions.Throws<ArgumentOutOfRangeException>("matchTimeout", () => Regex.Count("input", @"[abc]", RegexOptions.None, TimeSpan.FromMilliseconds(-2))); |
| 88 | + } |
| 89 | + |
| 90 | + [Theory] |
| 91 | + [MemberData(nameof(RegexHelpers.AvailableEngines_MemberData), MemberType = typeof(RegexHelpers))] |
| 92 | + public async Task Count_Timeout_ThrowsAfterTooLongExecution(RegexEngine engine) |
| 93 | + { |
| 94 | + if (RegexHelpers.IsNonBacktracking(engine)) |
| 95 | + { |
| 96 | + // Test relies on backtracking taking a long time |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + const string Pattern = @"^(\w+\s?)*$"; |
| 101 | + const string Input = "An input string that takes a very very very very very very very very very very very long time!"; |
| 102 | + |
| 103 | + Regex r = await RegexHelpers.GetRegexAsync(engine, Pattern, RegexOptions.None, TimeSpan.FromMilliseconds(1)); |
| 104 | + |
| 105 | + Stopwatch sw = Stopwatch.StartNew(); |
| 106 | + Assert.Throws<RegexMatchTimeoutException>(() => r.Count(Input)); |
| 107 | + Assert.InRange(sw.Elapsed.TotalSeconds, 0, 10); // arbitrary upper bound that should be well above what's needed with a 1ms timeout |
| 108 | + |
| 109 | + switch (engine) |
| 110 | + { |
| 111 | + case RegexEngine.Interpreter: |
| 112 | + case RegexEngine.Compiled: |
| 113 | + sw = Stopwatch.StartNew(); |
| 114 | + Assert.Throws<RegexMatchTimeoutException>(() => Regex.Count(Input, Pattern, RegexHelpers.OptionsFromEngine(engine), TimeSpan.FromMilliseconds(1))); |
| 115 | + Assert.InRange(sw.Elapsed.TotalSeconds, 0, 10); // arbitrary upper bound that should be well above what's needed with a 1ms timeout |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments