This repository has been archived by the owner on Jan 14, 2021. It is now read-only.
forked from alexanderkyte/NUnitLite
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Numerics.cs
428 lines (361 loc) · 17.5 KB
/
Numerics.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// ***********************************************************************
// Copyright (c) 2008 Charlie Poole
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************
using System;
namespace NUnit.Framework.Constraints
{
/// <summary>
/// The Numerics class contains common operations on numeric values.
/// </summary>
public class Numerics
{
#region Numeric Type Recognition
/// <summary>
/// Checks the type of the object, returning true if
/// the object is a numeric type.
/// </summary>
/// <param name="obj">The object to check</param>
/// <returns>true if the object is a numeric type</returns>
public static bool IsNumericType(Object obj)
{
return IsFloatingPointNumeric(obj) || IsFixedPointNumeric(obj);
}
/// <summary>
/// Checks the type of the object, returning true if
/// the object is a floating point numeric type.
/// </summary>
/// <param name="obj">The object to check</param>
/// <returns>true if the object is a floating point numeric type</returns>
public static bool IsFloatingPointNumeric(Object obj)
{
if (null != obj)
{
if (obj is System.Double) return true;
if (obj is System.Single) return true;
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (obj is System.nfloat) return true;
#endif
}
return false;
}
/// <summary>
/// Checks the type of the object, returning true if
/// the object is a fixed point numeric type.
/// </summary>
/// <param name="obj">The object to check</param>
/// <returns>true if the object is a fixed point numeric type</returns>
public static bool IsFixedPointNumeric(Object obj)
{
if (null != obj)
{
if (obj is System.Byte) return true;
if (obj is System.SByte) return true;
if (obj is System.Decimal) return true;
if (obj is System.Int32) return true;
if (obj is System.UInt32) return true;
if (obj is System.Int64) return true;
if (obj is System.UInt64) return true;
if (obj is System.Int16) return true;
if (obj is System.UInt16) return true;
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (obj is System.nint) return true;
if (obj is System.nuint) return true;
#endif
}
return false;
}
#endregion
#region Numeric Equality
/// <summary>
/// Test two numeric values for equality, performing the usual numeric
/// conversions and using a provided or default tolerance. If the tolerance
/// provided is Empty, this method may set it to a default tolerance.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The actual value</param>
/// <param name="tolerance">A reference to the tolerance in effect</param>
/// <returns>True if the values are equal</returns>
public static bool AreEqual(object expected, object actual, ref Tolerance tolerance)
{
bool _double = (expected is double || actual is double);
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (!_double && (IntPtr.Size == 8))
_double = (expected is nfloat || actual is nfloat);
#endif
if (_double)
return AreEqual(Convert.ToDouble(expected), Convert.ToDouble(actual), ref tolerance);
bool _float = (expected is float || actual is float);
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (!_float && (IntPtr.Size == 4))
_float = (expected is nfloat || actual is nfloat);
#endif
if (_float)
return AreEqual(Convert.ToSingle(expected), Convert.ToSingle(actual), ref tolerance);
if (tolerance.Mode == ToleranceMode.Ulps)
throw new InvalidOperationException("Ulps may only be specified for floating point arguments");
if (expected is decimal || actual is decimal)
return AreEqual(Convert.ToDecimal(expected), Convert.ToDecimal(actual), tolerance);
bool _ulong = (expected is ulong || actual is ulong);
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (!_ulong && (IntPtr.Size == 8))
_ulong = (expected is nuint || actual is nuint);
#endif
if (_ulong)
return AreEqual(Convert.ToUInt64(expected), Convert.ToUInt64(actual), tolerance);
bool _long = (expected is long || actual is long);
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (!_long && (IntPtr.Size == 8))
_long = (expected is nint || actual is nint);
#endif
if (_long)
return AreEqual(Convert.ToInt64(expected), Convert.ToInt64(actual), tolerance);
bool _uint = (expected is uint || actual is uint);
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (!_uint && (IntPtr.Size == 4))
_uint = (expected is nuint || actual is nuint);
#endif
if (_uint)
return AreEqual(Convert.ToUInt32(expected), Convert.ToUInt32(actual), tolerance);
// int or nint on 32bits archs
return AreEqual(Convert.ToInt32(expected), Convert.ToInt32(actual), tolerance);
}
private static bool AreEqual(double expected, double actual, ref Tolerance tolerance)
{
if (double.IsNaN(expected) && double.IsNaN(actual))
return true;
// Handle infinity specially since subtracting two infinite values gives
// NaN and the following test fails. mono also needs NaN to be handled
// specially although ms.net could use either method. Also, handle
// situation where no tolerance is used.
if (double.IsInfinity(expected) || double.IsNaN(expected) || double.IsNaN(actual))
{
return expected.Equals(actual);
}
if (tolerance.IsEmpty && GlobalSettings.DefaultFloatingPointTolerance > 0.0d)
tolerance = new Tolerance(GlobalSettings.DefaultFloatingPointTolerance);
switch (tolerance.Mode)
{
case ToleranceMode.None:
return expected.Equals(actual);
case ToleranceMode.Linear:
return Math.Abs(expected - actual) <= Convert.ToDouble(tolerance.Value);
case ToleranceMode.Percent:
if (expected == 0.0)
return expected.Equals(actual);
double relativeError = Math.Abs((expected - actual) / expected);
return (relativeError <= Convert.ToDouble(tolerance.Value) / 100.0);
case ToleranceMode.Ulps:
return FloatingPointNumerics.AreAlmostEqualUlps(
expected, actual, Convert.ToInt64(tolerance.Value));
default:
throw new ArgumentException("Unknown tolerance mode specified", "mode");
}
}
private static bool AreEqual(float expected, float actual, ref Tolerance tolerance)
{
if (float.IsNaN(expected) && float.IsNaN(actual))
return true;
// handle infinity specially since subtracting two infinite values gives
// NaN and the following test fails. mono also needs NaN to be handled
// specially although ms.net could use either method.
if (float.IsInfinity(expected) || float.IsNaN(expected) || float.IsNaN(actual))
{
return expected.Equals(actual);
}
if (tolerance.IsEmpty && GlobalSettings.DefaultFloatingPointTolerance > 0.0d)
tolerance = new Tolerance(GlobalSettings.DefaultFloatingPointTolerance);
switch (tolerance.Mode)
{
case ToleranceMode.None:
return expected.Equals(actual);
case ToleranceMode.Linear:
return Math.Abs(expected - actual) <= Convert.ToDouble(tolerance.Value);
case ToleranceMode.Percent:
if (expected == 0.0f)
return expected.Equals(actual);
float relativeError = Math.Abs((expected - actual) / expected);
return (relativeError <= Convert.ToSingle(tolerance.Value) / 100.0f);
case ToleranceMode.Ulps:
return FloatingPointNumerics.AreAlmostEqualUlps(
expected, actual, Convert.ToInt32(tolerance.Value));
default:
throw new ArgumentException("Unknown tolerance mode specified", "mode");
}
}
private static bool AreEqual(decimal expected, decimal actual, Tolerance tolerance)
{
switch (tolerance.Mode)
{
case ToleranceMode.None:
return expected.Equals(actual);
case ToleranceMode.Linear:
decimal decimalTolerance = Convert.ToDecimal(tolerance.Value);
if (decimalTolerance > 0m)
return Math.Abs(expected - actual) <= decimalTolerance;
return expected.Equals(actual);
case ToleranceMode.Percent:
if (expected == 0m)
return expected.Equals(actual);
double relativeError = Math.Abs(
(double)(expected - actual) / (double)expected);
return (relativeError <= Convert.ToDouble(tolerance.Value) / 100.0);
default:
throw new ArgumentException("Unknown tolerance mode specified", "mode");
}
}
private static bool AreEqual(ulong expected, ulong actual, Tolerance tolerance)
{
switch (tolerance.Mode)
{
case ToleranceMode.None:
return expected.Equals(actual);
case ToleranceMode.Linear:
ulong ulongTolerance = Convert.ToUInt64(tolerance.Value);
if (ulongTolerance > 0ul)
{
ulong diff = expected >= actual ? expected - actual : actual - expected;
return diff <= ulongTolerance;
}
return expected.Equals(actual);
case ToleranceMode.Percent:
if (expected == 0ul)
return expected.Equals(actual);
// Can't do a simple Math.Abs() here since it's unsigned
ulong difference = Math.Max(expected, actual) - Math.Min(expected, actual);
double relativeError = Math.Abs((double)difference / (double)expected);
return (relativeError <= Convert.ToDouble(tolerance.Value) / 100.0);
default:
throw new ArgumentException("Unknown tolerance mode specified", "mode");
}
}
private static bool AreEqual(long expected, long actual, Tolerance tolerance)
{
switch (tolerance.Mode)
{
case ToleranceMode.None:
return expected.Equals(actual);
case ToleranceMode.Linear:
long longTolerance = Convert.ToInt64(tolerance.Value);
if (longTolerance > 0L)
return Math.Abs(expected - actual) <= longTolerance;
return expected.Equals(actual);
case ToleranceMode.Percent:
if (expected == 0L)
return expected.Equals(actual);
double relativeError = Math.Abs(
(double)(expected - actual) / (double)expected);
return (relativeError <= Convert.ToDouble(tolerance.Value) / 100.0);
default:
throw new ArgumentException("Unknown tolerance mode specified", "mode");
}
}
private static bool AreEqual(uint expected, uint actual, Tolerance tolerance)
{
switch (tolerance.Mode)
{
case ToleranceMode.None:
return expected.Equals(actual);
case ToleranceMode.Linear:
uint uintTolerance = Convert.ToUInt32(tolerance.Value);
if (uintTolerance > 0)
{
uint diff = expected >= actual ? expected - actual : actual - expected;
return diff <= uintTolerance;
}
return expected.Equals(actual);
case ToleranceMode.Percent:
if (expected == 0u)
return expected.Equals(actual);
// Can't do a simple Math.Abs() here since it's unsigned
uint difference = Math.Max(expected, actual) - Math.Min(expected, actual);
double relativeError = Math.Abs((double)difference / (double)expected);
return (relativeError <= Convert.ToDouble(tolerance.Value) / 100.0);
default:
throw new ArgumentException("Unknown tolerance mode specified", "mode");
}
}
private static bool AreEqual(int expected, int actual, Tolerance tolerance)
{
switch (tolerance.Mode)
{
case ToleranceMode.None:
return expected.Equals(actual);
case ToleranceMode.Linear:
int intTolerance = Convert.ToInt32(tolerance.Value);
if (intTolerance > 0)
return Math.Abs(expected - actual) <= intTolerance;
return expected.Equals(actual);
case ToleranceMode.Percent:
if (expected == 0)
return expected.Equals(actual);
double relativeError = Math.Abs(
(double)(expected - actual) / (double)expected);
return (relativeError <= Convert.ToDouble(tolerance.Value) / 100.0);
default:
throw new ArgumentException("Unknown tolerance mode specified", "mode");
}
}
#endregion
#region Numeric Comparisons
/// <summary>
/// Compare two numeric values, performing the usual numeric conversions.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The actual value</param>
/// <returns>The relationship of the values to each other</returns>
public static int Compare(object expected, object actual)
{
if (!IsNumericType(expected) || !IsNumericType(actual))
throw new ArgumentException("Both arguments must be numeric");
if (IsFloatingPointNumeric(expected) || IsFloatingPointNumeric(actual))
return Convert.ToDouble(expected).CompareTo(Convert.ToDouble(actual));
if (expected is decimal || actual is decimal)
return Convert.ToDecimal(expected).CompareTo(Convert.ToDecimal(actual));
bool _ulong = (expected is ulong || actual is ulong);
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (!_ulong && (IntPtr.Size == 8))
_ulong = (expected is nuint || actual is nuint);
#endif
if (_ulong)
return Convert.ToUInt64(expected).CompareTo(Convert.ToUInt64(actual));
bool _long = (expected is long || actual is long);
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (!_long && (IntPtr.Size == 8))
_long = (expected is nint || actual is nint);
#endif
if (_long)
return Convert.ToInt64(expected).CompareTo(Convert.ToInt64(actual));
bool _uint = (expected is uint || actual is uint);
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (!_uint && (IntPtr.Size == 4))
_uint = (expected is nuint || actual is nuint);
#endif
if (_uint)
return Convert.ToUInt32(expected).CompareTo(Convert.ToUInt32(actual));
return Convert.ToInt32(expected).CompareTo(Convert.ToInt32(actual));
}
#endregion
private Numerics()
{
}
}
}