-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleParserTests.cs
More file actions
128 lines (107 loc) · 3.6 KB
/
DoubleParserTests.cs
File metadata and controls
128 lines (107 loc) · 3.6 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#if !NETFRAMEWORK
using System;
#endif
using FluentAssertions;
using Light.SharedCore.Parsing;
using Xunit;
namespace Light.SharedCore.Tests.Parsing;
public static class DoubleParserTests
{
private const double Precision = 0.0000001;
[Theory]
[MemberData(nameof(NumbersWithDecimalPoint))]
public static void ParseFloatingPointNumberWithDecimalPoint(string text, double expectedValue) =>
CheckNumber(text, expectedValue);
#if !NETFRAMEWORK
[Theory]
[MemberData(nameof(NumbersWithDecimalPoint))]
public static void ParseFloatingPointNumberWithDecimalPointAsSpan(string text, double expectedValue) =>
CheckNumberAsSpan(text, expectedValue);
#endif
public static readonly TheoryData<string, double> NumbersWithDecimalPoint =
new ()
{
{ "0.74", 0.74 },
{ "1.34", 1.34 },
{ "391202.9", 391202.9 },
{ "-20.816", -20.816 },
{ "15,019.33", 15_019.33 }
};
[Theory]
[MemberData(nameof(NumbersWithDecimalComma))]
public static void ParseFloatingPointNumberWithDecimalComma(string text, double expectedValue) =>
CheckNumber(text, expectedValue);
#if !NETFRAMEWORK
[Theory]
[MemberData(nameof(NumbersWithDecimalComma))]
public static void ParseFloatingPointNumberWithDecimalCommaAsSpan(string text, double expectedValue) =>
CheckNumberAsSpan(text, expectedValue);
#endif
public static readonly TheoryData<string, double> NumbersWithDecimalComma =
new ()
{
{ "000,7832", 0.7832 },
{ "-0,499", -0.499 },
{ "40593,84", 40593.84 },
{ "1.943.100,84", 1_943_100.84 }
};
[Theory]
[MemberData(nameof(IntegerNumbers))]
public static void ParseInteger(string text, double expectedValue) =>
CheckNumber(text, expectedValue);
#if !NETFRAMEWORK
[Theory]
[MemberData(nameof(IntegerNumbers))]
public static void ParseIntegerAsSpan(string text, double expectedValue) =>
CheckNumberAsSpan(text, expectedValue);
#endif
public static readonly TheoryData<string, double> IntegerNumbers =
new ()
{
{ "15", 15.0 },
{ "-743923", -743923.0 },
{ "239.482.392.923", 239_482_392_923.0 },
{ "21,500,000", 21_500_000.0 }
};
private static void CheckNumber(string text, double expectedValue)
{
var result = DoubleParser.TryParse(text, out var parsedValue);
result.Should().BeTrue();
parsedValue.Should().BeApproximately(expectedValue, Precision);
}
#if !NETFRAMEWORK
private static void CheckNumberAsSpan(ReadOnlySpan<char> text, double expectedValue)
{
var result = DoubleParser.TryParse(text, out var parsedValue);
result.Should().BeTrue();
parsedValue.Should().BeApproximately(expectedValue, Precision);
}
#endif
[Theory]
[MemberData(nameof(InvalidNumbers))]
public static void InvalidNumber(string? text)
{
var result = DoubleParser.TryParse(text, out var actualValue);
result.Should().BeFalse();
actualValue.Should().Be(0);
}
#if !NETFRAMEWORK
[Theory]
[MemberData(nameof(InvalidNumbers))]
public static void InvalidNumberAsSpan(string? text)
{
var result = DoubleParser.TryParse(text.AsSpan(), out var actualValue);
result.Should().BeFalse();
actualValue.Should().Be(0);
}
#endif
public static readonly TheoryData<string?> InvalidNumbers =
new ()
{
"Foo",
"Bar",
"",
null,
"9392gk381"
};
}