feat: add IsCloseTo and IsWithinPercentOf numeric assertions#4940
feat: add IsCloseTo and IsWithinPercentOf numeric assertions#4940
Conversation
Code ReviewOverall this is a well-structured addition with good test coverage and proper NaN/infinity handling. Two bugs need addressing before merge. Bug 1 (High): Integer overflow in
|
00f9e5a to
49924bb
Compare
49924bb to
e8e6bbf
Compare
e8e6bbf to
9187312
Compare
9187312 to
bfe1e2e
Compare
Add relative and absolute tolerance assertions for numeric types (double, float, int, long, decimal): - IsCloseTo(expected, tolerance): asserts |actual - expected| <= tolerance - IsWithinPercentOf(expected, percent): asserts |actual - expected| <= |expected * percent / 100| Both assertions follow the existing pattern using [AssertionExtension] for source-generated extension methods. Includes proper handling of NaN, infinity, and edge cases for floating-point types. Closes #4870
…o double Cast `value` to `double` before subtraction and compare against `(double)_tolerance` so that extreme long values (e.g., MaxValue vs MinValue) no longer silently wrap around.
4c70fbe to
80e92bc
Compare
Seek to address thomhurst#4870
Seek to address thomhurst#4870
Seek to address thomhurst#4870
Seek to address thomhurst#4870
Seek to address thomhurst#4870
Seek to address thomhurst#4870
Seek to address thomhurst#4870
Seek to address thomhurst#4870
Seek to address thomhurst#4870
|
This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days. |
* feat: add IsCloseTo and IsWithinPercentOf numeric assertions Add relative and absolute tolerance assertions for numeric types (double, float, int, long, decimal): - IsCloseTo(expected, tolerance): asserts |actual - expected| <= tolerance - IsWithinPercentOf(expected, percent): asserts |actual - expected| <= |expected * percent / 100| Both assertions follow the existing pattern using [AssertionExtension] for source-generated extension methods. Includes proper handling of NaN, infinity, and edge cases for floating-point types. Closes #4870 * fix: prevent integer overflow in LongIsCloseToAssertion by widening to double Cast `value` to `double` before subtraction and compare against `(double)_tolerance` so that extreme long values (e.g., MaxValue vs MinValue) no longer silently wrap around. * Build on top of #4940 Seek to address #4870 * Address issues 3 through 7 * Update public API snapshots for numeric tolerance assertions --------- Co-authored-by: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Co-authored-by: Andrew Gray <118102+agray@users.noreply.github.com>
Summary
IsCloseTo(expected, tolerance)assertion for absolute tolerance comparison:|actual - expected| <= toleranceIsWithinPercentOf(expected, percent)assertion for relative tolerance comparison:|actual - expected| <= |expected * percent / 100|double,float,int,long, anddecimaltypes[AssertionExtension]for source-generated extension methodsUsage
Test plan
Closes #4870