/
AssertionExtensions.cs
93 lines (80 loc) · 3.87 KB
/
AssertionExtensions.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright Lamont Granquist (lamont@scriptkiddie.org)
* Dual licensed under the MIT (MIT-LICENSE) license
* and GPLv2 (GPLv2-LICENSE) license or any later version.
*/
using System;
using System.Globalization;
using MechJebLib.Primitives;
using Xunit.Sdk;
using static MechJebLib.Utils.Statics;
using static System.Math;
namespace MechJebLibTest
{
/// <summary>
/// Xunit Assertion Extensions
/// </summary>
public static class AssertionExtensions
{
// A proper relative tolerance comparison comparsion between float values.
public static void ShouldEqual(this double actual, double expected, double epsilon = EPS)
{
if (double.IsNaN(epsilon) || double.IsNegativeInfinity(epsilon) || epsilon < 0.0)
throw new ArgumentException("Epsilon must be greater than or equal to zero", nameof(epsilon));
if (!NearlyEqual(actual, expected, epsilon))
throw new EqualException(
string.Format(CultureInfo.CurrentCulture, "{0:G17}", expected),
string.Format(CultureInfo.CurrentCulture, "{0:G17}", actual)
);
}
public static void ShouldEqual(this V3 actual, V3 expected, double epsilon = EPS)
{
if (double.IsNaN(epsilon) || double.IsNegativeInfinity(epsilon) || epsilon < 0.0)
throw new ArgumentException("Epsilon must be greater than or equal to zero", nameof(epsilon));
if (!NearlyEqual(actual, expected, epsilon))
throw new EqualException(
string.Format(CultureInfo.CurrentCulture, "{0:G17}", expected),
string.Format(CultureInfo.CurrentCulture, "{0:G17}", actual)
);
}
public static void ShouldEqual(this M3 actual, M3 expected, double epsilon = EPS)
{
if (double.IsNaN(epsilon) || double.IsNegativeInfinity(epsilon) || epsilon < 0.0)
throw new ArgumentException("Epsilon must be greater than or equal to zero", nameof(epsilon));
if (!NearlyEqual(actual, expected, epsilon))
throw new EqualException(
string.Format(CultureInfo.CurrentCulture, "{0:G17}", expected),
string.Format(CultureInfo.CurrentCulture, "{0:G17}", actual)
);
}
// Comparison to zero within a tolerance
public static void ShouldBeZero(this double actual, double epsilon = EPS)
{
if (double.IsNaN(epsilon) || double.IsNegativeInfinity(epsilon) || epsilon < 0.0)
throw new ArgumentException("Epsilon must be greater than or equal to zero", nameof(epsilon));
if (Abs(actual) > epsilon)
throw new EqualException(
string.Format(CultureInfo.CurrentCulture, "{0:G17}", 0.0),
string.Format(CultureInfo.CurrentCulture, "{0:G17}", actual)
);
}
// Comparison to zero within a tolerance
public static void ShouldBeZero(this V3 actual, double epsilon = EPS)
{
if (double.IsNaN(epsilon) || double.IsNegativeInfinity(epsilon) || epsilon < 0.0)
throw new ArgumentException("Epsilon must be greater than or equal to zero", nameof(epsilon));
if (Abs(actual.x) > epsilon || Abs(actual.y) > epsilon || Abs(actual.z) > epsilon)
throw new EqualException(
string.Format(CultureInfo.CurrentCulture, "{0:G17}", 0.0),
string.Format(CultureInfo.CurrentCulture, "{0:G17}", actual)
);
}
public static void ShouldBePositive(this double actual)
{
if (!IsFinite(actual))
throw new XunitException($"{actual} must be finite");
if (actual <= 0)
throw new XunitException($"{actual} must be positive");
}
}
}