External Issue: [Feature Request]: assertClose() method for UnitTest

28099, "jmag722", "[Feature Request]: assertClose() method for UnitTest", "2025-11-22T16:46:11Z"

assertClose Method for UnitTest

Description:
It would be nice to have an assert method within UnitTest to check if scalars and arrays were within an expected tolerance. The solution I'm imagining would be similar in behavior to np.testing.assert_allclose().

Checking behavior would differ from Math.isClose because it would not be commutative (per below implementation, and numpy documentation). So it could be good if both methods had their respective criterion documented explicitly (which I think is being discussed in [Documentation]: isClose() · Issue #26604 · chapel-lang/chapel · GitHub). If a different name would help avoid confusion, rather than assertClose we could use assertWithinTol or something.

In addition to throwing an AssertionError, it'd be awesome if it also reported the max absolute and relative errors in the comparison, and the percentage of elements violating the check (again, similar to the numpy based call).

Overloaded functions could handle array vs scalar functions if needed.

I would be happy to submit a pull request for this :slight_smile:

Is this issue currently blocking your progress?
No

Code Sample

module UnitTest {
// ...

  class Test {
    inline proc assertClose(const ref actual: ?T, const ref expected: T, const in relTol: real = 1e-5, const in absTol: real = 0.0): bool throws {
      // any runtime checks, check for nan?
      if abs(actual - expected) > absTol + abs(relTol * expected) {
        // compute max abs and rel errors
        // log these max deviations and number/percentage of points violating constraint
        // throw AssertionError
      }
    }
  }
}