|
| 1 | +namespace TUnit.Assertions.Tests; |
| 2 | + |
| 3 | +public class NumericEqualityToleranceAssertionTests |
| 4 | +{ |
| 5 | + [Test] |
| 6 | + public async Task Double_RelativeTolerance_Passes_When_Within_Percentage() |
| 7 | + { |
| 8 | + const double actual = 104.9; |
| 9 | + const double expected = 100.0; |
| 10 | + |
| 11 | + await Assert.That(actual) |
| 12 | + .IsEqualTo(expected) |
| 13 | + .WithinRelativeTolerance(5); |
| 14 | + } |
| 15 | + |
| 16 | + [Test] |
| 17 | + public async Task Double_RelativeTolerance_Fails_When_Outside_Percentage() |
| 18 | + { |
| 19 | + const double actual = 105.1; |
| 20 | + const double expected = 100.0; |
| 21 | + |
| 22 | + var sut = async () => await Assert.That(actual) |
| 23 | + .IsEqualTo(expected) |
| 24 | + .WithinRelativeTolerance(5); |
| 25 | + |
| 26 | + await Assert.That(sut).ThrowsException(); |
| 27 | + } |
| 28 | + |
| 29 | + [Test] |
| 30 | + public async Task Double_RelativeTolerance_Includes_Exact_Boundary() |
| 31 | + { |
| 32 | + const double actual = 105.0; |
| 33 | + const double expected = 100.0; |
| 34 | + |
| 35 | + await Assert.That(actual) |
| 36 | + .IsEqualTo(expected) |
| 37 | + .WithinRelativeTolerance(5); |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public async Task Double_RelativeTolerance_Works_With_Negative_Expected_Value() |
| 42 | + { |
| 43 | + const double actual = -104.9; |
| 44 | + const double expected = -100.0; |
| 45 | + |
| 46 | + await Assert.That(actual) |
| 47 | + .IsEqualTo(expected) |
| 48 | + .WithinRelativeTolerance(5); |
| 49 | + } |
| 50 | + |
| 51 | + [Test] |
| 52 | + public async Task Double_RelativeTolerance_Fails_With_Negative_Expected_Value_When_Outside_Boundary() |
| 53 | + { |
| 54 | + const double actual = -106.0; |
| 55 | + const double expected = -100.0; |
| 56 | + |
| 57 | + var sut = async () => await Assert.That(actual) |
| 58 | + .IsEqualTo(expected) |
| 59 | + .WithinRelativeTolerance(5); |
| 60 | + |
| 61 | + await Assert.That(sut).ThrowsException(); |
| 62 | + } |
| 63 | + |
| 64 | + [Test] |
| 65 | + public async Task Double_RelativeTolerance_ZeroExpected_Passes_Only_For_Exact_Zero() |
| 66 | + { |
| 67 | + const double actual = 0.0; |
| 68 | + const double expected = 0.0; |
| 69 | + |
| 70 | + await Assert.That(actual) |
| 71 | + .IsEqualTo(expected) |
| 72 | + .WithinRelativeTolerance(5); |
| 73 | + } |
| 74 | + |
| 75 | + [Test] |
| 76 | + public async Task Double_RelativeTolerance_ZeroExpected_Fails_For_NonZero_Actual() |
| 77 | + { |
| 78 | + const double actual = 0.0001; |
| 79 | + const double expected = 0.0; |
| 80 | + |
| 81 | + var sut = async () => await Assert.That(actual) |
| 82 | + .IsEqualTo(expected) |
| 83 | + .WithinRelativeTolerance(5); |
| 84 | + |
| 85 | + await Assert.That(sut).ThrowsException(); |
| 86 | + } |
| 87 | + |
| 88 | + [Test] |
| 89 | + public async Task Double_RelativeTolerance_Treats_NaN_As_Equal_To_NaN() |
| 90 | + { |
| 91 | + const double actual = double.NaN; |
| 92 | + const double expected = double.NaN; |
| 93 | + |
| 94 | + await Assert.That(actual) |
| 95 | + .IsEqualTo(expected) |
| 96 | + .WithinRelativeTolerance(5); |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public async Task Double_RelativeTolerance_Fails_When_Only_One_Side_Is_NaN() |
| 101 | + { |
| 102 | + const double actual = double.NaN; |
| 103 | + const double expected = 100.0; |
| 104 | + |
| 105 | + var sut = async () => await Assert.That(actual) |
| 106 | + .IsEqualTo(expected) |
| 107 | + .WithinRelativeTolerance(5); |
| 108 | + |
| 109 | + await Assert.That(sut).ThrowsException(); |
| 110 | + } |
| 111 | + |
| 112 | + [Test] |
| 113 | + public async Task Double_RelativeTolerance_Treats_Matching_Positive_Infinity_As_Equal() |
| 114 | + { |
| 115 | + const double actual = double.PositiveInfinity; |
| 116 | + const double expected = double.PositiveInfinity; |
| 117 | + |
| 118 | + await Assert.That(actual) |
| 119 | + .IsEqualTo(expected) |
| 120 | + .WithinRelativeTolerance(5); |
| 121 | + } |
| 122 | + |
| 123 | + [Test] |
| 124 | + public async Task Double_RelativeTolerance_Fails_For_Mismatched_Infinities() |
| 125 | + { |
| 126 | + const double actual = double.PositiveInfinity; |
| 127 | + const double expected = double.NegativeInfinity; |
| 128 | + |
| 129 | + var sut = async () => await Assert.That(actual) |
| 130 | + .IsEqualTo(expected) |
| 131 | + .WithinRelativeTolerance(5); |
| 132 | + |
| 133 | + await Assert.That(sut).ThrowsException(); |
| 134 | + } |
| 135 | + |
| 136 | + [Test] |
| 137 | + public async Task Float_RelativeTolerance_Passes_When_Within_Percentage() |
| 138 | + { |
| 139 | + const float actual = 104.9f; |
| 140 | + const float expected = 100f; |
| 141 | + |
| 142 | + await Assert.That(actual) |
| 143 | + .IsEqualTo(expected) |
| 144 | + .WithinRelativeTolerance(5); |
| 145 | + } |
| 146 | + |
| 147 | + [Test] |
| 148 | + public async Task Float_RelativeTolerance_Treats_NaN_As_Equal_To_NaN() |
| 149 | + { |
| 150 | + const float actual = float.NaN; |
| 151 | + const float expected = float.NaN; |
| 152 | + |
| 153 | + await Assert.That(actual) |
| 154 | + .IsEqualTo(expected) |
| 155 | + .WithinRelativeTolerance(5); |
| 156 | + } |
| 157 | + |
| 158 | + [Test] |
| 159 | + public async Task Float_RelativeTolerance_Treats_Matching_Infinity_As_Equal() |
| 160 | + { |
| 161 | + const float actual = float.PositiveInfinity; |
| 162 | + const float expected = float.PositiveInfinity; |
| 163 | + |
| 164 | + await Assert.That(actual) |
| 165 | + .IsEqualTo(expected) |
| 166 | + .WithinRelativeTolerance(5); |
| 167 | + } |
| 168 | + |
| 169 | + [Test] |
| 170 | + public async Task Decimal_RelativeTolerance_Passes_When_Within_Percentage() |
| 171 | + { |
| 172 | + const decimal actual = 104.9m; |
| 173 | + const decimal expected = 100m; |
| 174 | + |
| 175 | + await Assert.That(actual) |
| 176 | + .IsEqualTo(expected) |
| 177 | + .WithinRelativeTolerance(5); |
| 178 | + } |
| 179 | + |
| 180 | + [Test] |
| 181 | + public async Task Decimal_RelativeTolerance_Fails_When_Outside_Percentage() |
| 182 | + { |
| 183 | + const decimal actual = 105.1m; |
| 184 | + const decimal expected = 100m; |
| 185 | + |
| 186 | + var sut = async () => await Assert.That(actual) |
| 187 | + .IsEqualTo(expected) |
| 188 | + .WithinRelativeTolerance(5); |
| 189 | + |
| 190 | + await Assert.That(sut).ThrowsException(); |
| 191 | + } |
| 192 | + |
| 193 | + [Test] |
| 194 | + public async Task RelativeTolerance_Exception_Message_Contains_Expectation_Text() |
| 195 | + { |
| 196 | + const double actual = 106.0; |
| 197 | + const double expected = 100.0; |
| 198 | + |
| 199 | + var sut = async () => await Assert.That(actual) |
| 200 | + .IsEqualTo(expected) |
| 201 | + .WithinRelativeTolerance(5); |
| 202 | + |
| 203 | + var exception = await Assert.That(sut).ThrowsException(); |
| 204 | + |
| 205 | + await Assert.That(exception.Message.NormalizeLineEndings()) |
| 206 | + .Contains("Expected to be within 5% of 100") |
| 207 | + .And.Contains("but found 106") |
| 208 | + .And.Contains("Assert.That(actual).IsEqualTo(expected).WithinRelativeTolerance(5)"); |
| 209 | + } |
| 210 | + |
| 211 | + [Test] |
| 212 | + public async Task RelativeTolerance_Exception_Message_Contains_Difference_Details() |
| 213 | + { |
| 214 | + const double actual = 106.0; |
| 215 | + const double expected = 100.0; |
| 216 | + |
| 217 | + var sut = async () => await Assert.That(actual) |
| 218 | + .IsEqualTo(expected) |
| 219 | + .WithinRelativeTolerance(5); |
| 220 | + |
| 221 | + var exception = await Assert.That(sut).ThrowsException(); |
| 222 | + |
| 223 | + await Assert.That(exception.Message.NormalizeLineEndings()) |
| 224 | + .Contains("differs by 6") |
| 225 | + .And.Contains("relative tolerance of 5%"); |
| 226 | + } |
| 227 | +} |
0 commit comments