forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssert.cs
More file actions
104 lines (87 loc) · 2.09 KB
/
Assert.cs
File metadata and controls
104 lines (87 loc) · 2.09 KB
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
using System;
using System.Runtime.CompilerServices;
using System.Text;
namespace JavaScriptEngineSwitcher.Benchmarks
{
internal static class Assert
{
private static readonly char[] _lineBreakChars = ['\r', '\n'];
public static void Equal(string expected, string actual, bool ignoreLineBreaks = false)
{
if (!EqualInternal(expected, actual, ignoreLineBreaks))
{
var messageBuilder = new StringBuilder();
messageBuilder.AppendLine("Assert.Equal() Failure");
messageBuilder.AppendLine();
messageBuilder.AppendLine($"Expected: {expected}");
messageBuilder.Append($"Actual: {actual}");
string errorMessage = messageBuilder.ToString();
throw new InvalidOperationException(errorMessage);
}
}
private static bool EqualInternal(string a, string b, bool ignoreLineBreaks)
{
if (!ignoreLineBreaks)
{
return a == b;
}
if (ReferenceEquals(a, b))
{
return true;
}
if (a is null || b is null)
{
return false;
}
if (a.IndexOfAny(_lineBreakChars) == -1 && b.IndexOfAny(_lineBreakChars) == -1)
{
return a.Equals(b);
}
int aIndex = 0;
int aLength = a.Length;
int bIndex = 0;
int bLength = b.Length;
while (true)
{
if (aIndex >= aLength)
{
return bIndex >= bLength;
}
if (bIndex >= bLength)
{
return false;
}
char aChar = a[aIndex];
char bChar = b[bIndex];
if (aChar != bChar)
{
if (Array.IndexOf(_lineBreakChars, aChar) != -1 && Array.IndexOf(_lineBreakChars, bChar) != -1)
{
ProcessLineBreaks(a, aChar, ref aIndex, aLength);
ProcessLineBreaks(b, bChar, ref bIndex, bLength);
continue;
}
else
{
return false;
}
}
aIndex++;
bIndex++;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ProcessLineBreaks(string value, char charValue, ref int charIndex, int charCount)
{
if (charValue == '\r')
{
int nextCharIndex = charIndex + 1;
if (nextCharIndex < charCount && value[nextCharIndex] == '\n')
{
charIndex++;
}
}
charIndex++;
}
}
}