-
Notifications
You must be signed in to change notification settings - Fork 444
28099 feat unittest assertclose #28144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jmag722
wants to merge
15
commits into
chapel-lang:main
Choose a base branch
from
jmag722:28099-feat-unittest-assertclose
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
edf3aed
WIP JMM[#28099]: Rough draft of assertClose for scalar and array type…
jmag722 6c49ed4
WIP JMM[#28099]: assertClose still not working, still same error, but…
jmag722 78faff9
WIP JMM[#28099]: assertClose working with array and floating point! m…
jmag722 acfd1f8
WIP JMM[28099]: simplified error message, arrays must be same shape b…
jmag722 662268d
Update assertClose and withinTol documentation. add asserts to within…
jmag722 110b911
add tests to check signs of tolerances. withinTol now throws and add …
jmag722 8b99a13
[#28099]: using more appropriate error types
jmag722 7ec3c74
Nan equality checks work for scalar comparisons
jmag722 e52f4b2
assert nan array elements are equal, add tests
jmag722 317e352
add compareNan documentation, where clause
jmag722 91723fb
add default args to testAssert, use named arguments for clearer tests
jmag722 2e064b6
assertClose now compares partial complex NaN consistently, updated te…
jmag722 d7f1efd
reorganize tests: passing first, fail last. added 2D array test, more…
jmag722 638fa5a
remove compareNanTrue with array inputs, b/c procedure with scalar ar…
jmag722 ade21b0
removing separate if block for complex arrays, compareComplex is prom…
jmag722 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
test/library/packages/UnitTest/AssertClose/AssertCloseTest.chpl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| use UnitTest; | ||
|
|
||
| proc testAssert(actual, expected, absTol=0.0, relTol=1e-15, | ||
| equalNan=true, reason="") { | ||
| var sep = "=="*40; | ||
| var test = new Test(); | ||
| try { | ||
| test.assertClose(actual=actual, expected=expected, absTol=absTol, | ||
| relTol=relTol, equalNan=equalNan); | ||
| } catch e { | ||
| writeln("Error Caught in "+reason); | ||
| writeln(e); | ||
| writeln(sep); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| proc main() { | ||
| equalReal(); | ||
| equalRealArray(); | ||
| equalImag(); | ||
| equalComplex(); | ||
| equalComplexArray(); | ||
|
|
||
| equalNanReal(); | ||
| equalNanImag(); | ||
| equalNanComplex(); | ||
|
|
||
| negativeRelTol(); | ||
| negativeAbsTol(); | ||
| mismatchedArraySize(); | ||
|
|
||
| unequalReal(); | ||
| unequalComplex(); | ||
| unequalComplexArray(); | ||
|
|
||
| unequalNanReal(); | ||
| unequalNanImag(); | ||
| unequalNanComplex(); | ||
| } | ||
|
|
||
| proc equalReal() { | ||
| testAssert(1.8, 2.0, relTol=0.1, reason="Equal real numbers"); | ||
| } | ||
|
|
||
| proc equalRealArray() { | ||
| var a: [0..4] real = [.3, .31, .35, -.1, -1e-9]; | ||
| var e: [1..5] real = [.3, .32, .6, -.2, -2e-9]; | ||
| testAssert(a, e, absTol=0.3, relTol=0.0, reason="Equal real 1D arrays"); | ||
| } | ||
|
|
||
| proc equalImag() { | ||
| testAssert(1.8i, 2.0i, relTol=0.1, reason="Equal imag numbers"); | ||
| } | ||
| proc equalComplex() { | ||
| testAssert(-13+2.5i, -13+2.4i, relTol=8e-3, reason="Equal complex numbers"); | ||
| } | ||
| proc equalComplexArray() { | ||
| var a: [0..2, 0..1] complex = [2+0.99i, 1+1i; -3+1i, 5+1i; 6+1i, 7+1i]; | ||
| var e: [1..3, 0..1] complex = [2+1i, 1+1i; -2.99+1i, 5+1i; 6+1i, 7+1i]; | ||
| testAssert(a, e, relTol=0.01, reason="Equal complex 2D arrays"); | ||
| } | ||
|
|
||
| proc equalNanReal() { | ||
| testAssert(nan, nan, reason="Equal real NaN"); | ||
| testAssert([3.1, nan, 6.5], [4.1, nan, 6.5], absTol=1.0, relTol=0.0, | ||
| reason="Equal real NaN array"); | ||
| } | ||
| proc equalNanImag() { | ||
| testAssert(nan*1i, nan*1i, reason="Equal imag NaN"); | ||
| testAssert([nan*1i, 3.5i], [nan*1i, 3.9i], absTol=0.5, relTol=0.0, | ||
| reason="Equal imag NaN array"); | ||
| } | ||
| proc equalNanComplex() { | ||
| testAssert(nan+nan*1i, nan+nan*1i, reason="Equal complex NaN"); | ||
| testAssert(1.0+nan*1i, 1.0+nan*1i, reason="Equal complex partial NaN array"); | ||
| testAssert([nan+nan*1i, 14+9i], [nan+nan*1i, 16+9i], | ||
| absTol=2.0, relTol=0.0, reason="Equal complex NaN array"); | ||
| } | ||
|
|
||
| proc negativeRelTol() { | ||
| testAssert(1.5, 1.6, absTol=-0.1, reason="Negative relative tolerance"); | ||
| } | ||
|
|
||
| proc negativeAbsTol() { | ||
| testAssert(1.5, 1.6, absTol=0.1, relTol=-1e-15, | ||
| reason="Negative absolute tolerance"); | ||
| } | ||
|
|
||
| proc mismatchedArraySize() { | ||
| var a = [.3, .31, .35, -.1, -1e-9]; | ||
| var e = [.3, .32, .35, -.1]; | ||
| testAssert(a, e, reason="Mismatched array sizes"); | ||
| } | ||
|
|
||
| proc unequalReal() { | ||
| testAssert(0.0, 1e-6, absTol=9.9e-7, reason="Unequal real numbers"); | ||
| } | ||
| proc unequalComplex() { | ||
| testAssert(1.8+3i, 1.8+3.000001i, reason="Unequal complex numbers"); | ||
| } | ||
|
|
||
| proc unequalComplexArray() { | ||
| var a = [.3+1i, 2-3i]; | ||
| var e = [.3+1i, 3-2i]; | ||
| testAssert(a, e, absTol=1.0, relTol=0.0, reason="Unequal complex 1D arrays"); | ||
| } | ||
|
|
||
| proc unequalNanReal() { | ||
| testAssert(nan, 2.5, reason="Unequal real NaN, equalNan true"); | ||
| testAssert(nan, 2.5, equalNan=false, | ||
| reason="Unequal real NaN, equalNan false"); | ||
| testAssert([3.1, nan, 6.5], [3.1, nan, 6.5], equalNan=false, | ||
| reason="Unequal real array NaN"); | ||
| } | ||
|
|
||
| proc unequalNanImag() { | ||
| testAssert(nan*1i, nan*1i, equalNan=false, | ||
| reason="Unequal imag NaN"); | ||
| testAssert([3.1i, 14i, 6.5i], [4.1i, nan*1i, 6.5i], | ||
| absTol=1.0, relTol=0.0, reason="Unequal imag array NaN"); | ||
| } | ||
|
|
||
| proc unequalNanComplex() { | ||
| testAssert(nan+2.0i, 2.5+2.0i, reason="Unequal complex partial NaN, real"); | ||
| testAssert(1.0+nan*1i, 1.0+2.5i, reason="Unequal complex partial NaN, imag"); | ||
| testAssert(nan+nan*1i, nan+nan*1i, equalNan=false, | ||
| reason="Unequal complex NaN"); | ||
| } |
64 changes: 64 additions & 0 deletions
64
test/library/packages/UnitTest/AssertClose/AssertCloseTest.good
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| Error Caught in Negative relative tolerance | ||
| IllegalArgumentError: relTol and absTol must both be nonnegative. | ||
| ================================================================================ | ||
| Error Caught in Negative absolute tolerance | ||
| IllegalArgumentError: relTol and absTol must both be nonnegative. | ||
| ================================================================================ | ||
| Error Caught in Mismatched array sizes | ||
| IllegalArgumentError: Actual array shape (5,) does not match expected: (4,) | ||
| ================================================================================ | ||
| Error Caught in Unequal real numbers | ||
| AssertionError: in AssertCloseTest.chpl:8 - assert failed for actual=0, expected=1e-06 | ||
| Rel. Error: 100 % | ||
| Abs. Error: 1e-06 | ||
| ================================================================================ | ||
| Error Caught in Unequal complex numbers | ||
| AssertionError: in AssertCloseTest.chpl:8 - assert failed for actual=1.8 + 3i, expected=1.8 + 3i | ||
| Rel. Error: 2.85831e-05 % | ||
| Abs. Error: 1e-06 | ||
| ================================================================================ | ||
| Error Caught in Unequal complex 1D arrays | ||
| AssertionError: in AssertCloseTest.chpl:8 - assert failed for 1/2 elements (50%) | ||
| Max Rel. Error: 39.2232% | ||
| Max Abs. Error: 1.41421 | ||
| ================================================================================ | ||
| Error Caught in Unequal real NaN, equalNan true | ||
| AssertionError: in AssertCloseTest.chpl:8 - assert failed for actual=nan, expected=2.5 | ||
| Rel. Error: nan % | ||
| Abs. Error: nan | ||
| ================================================================================ | ||
| Error Caught in Unequal real NaN, equalNan false | ||
| AssertionError: in AssertCloseTest.chpl:8 - assert failed for actual=nan, expected=2.5 | ||
| Rel. Error: nan % | ||
| Abs. Error: nan | ||
| ================================================================================ | ||
| Error Caught in Unequal real array NaN | ||
| AssertionError: in AssertCloseTest.chpl:8 - assert failed for 1/3 elements (33.3333%) | ||
| Max Rel. Error: nan% | ||
| Max Abs. Error: nan | ||
| ================================================================================ | ||
| Error Caught in Unequal imag NaN | ||
| AssertionError: in AssertCloseTest.chpl:8 - assert failed for actual=nani, expected=nani | ||
| Rel. Error: nan % | ||
| Abs. Error: nan | ||
| ================================================================================ | ||
| Error Caught in Unequal imag array NaN | ||
| AssertionError: in AssertCloseTest.chpl:8 - assert failed for 1/3 elements (33.3333%) | ||
| Max Rel. Error: nan% | ||
| Max Abs. Error: nan | ||
| ================================================================================ | ||
| Error Caught in Unequal complex partial NaN, real | ||
| AssertionError: in AssertCloseTest.chpl:8 - assert failed for actual=nan, expected=2.5 + 2i | ||
| Rel. Error: nan % | ||
| Abs. Error: nan | ||
| ================================================================================ | ||
| Error Caught in Unequal complex partial NaN, imag | ||
| AssertionError: in AssertCloseTest.chpl:8 - assert failed for actual=nan, expected=1 + 2.5i | ||
| Rel. Error: nan % | ||
| Abs. Error: nan | ||
| ================================================================================ | ||
| Error Caught in Unequal complex NaN | ||
| AssertionError: in AssertCloseTest.chpl:8 - assert failed for actual=nan, expected=nan | ||
| Rel. Error: nan % | ||
| Abs. Error: nan | ||
| ================================================================================ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.