test-drive icon indicating copy to clipboard operation
test-drive copied to clipboard

Support array checking for the `check` subroutine

Open zoziha opened this issue 3 years ago • 2 comments
trafficstars

At present, test-drive can check scalars of Fortran built-in types, such as logical scalars, integer scalars, and real scalars; however, it does not support arrays, so users need to write loops to check the contents of arrays:

do i = 1, size(array)
    call check(error, array(i), expected(i), ...)
    if (allocated(error)) return
end do

It would be a good feature to support array checking. Since the check subroutine usually only needs to read the array without changing the contents of the array, I think using reshape to convert it into a rank-1 array for checking would be an acceptable solution.

subroutine check_array(error, actual, expected, ...)
    type(error_type), allocatable, intent(out) :: error
    class(*), intent(in) :: actual(:)
    class(*), intent(in) :: expected(:)
    ...
end subroutine

! The `array` can be of any shape, and is finally determined to be a rank-1 array by `reshape`
call check(error, reshape(array, shape=[size(array)], expected, ...)
if (allocated(error)) return

zoziha avatar Jun 18 '22 08:06 zoziha

One can also use an array expression like

call check(error, all(array == expected))

ivan-pi avatar Jun 28 '22 07:06 ivan-pi

Using all, for real numbers, we need to construct the following judgments,

call check(error, all((abs(array - expected) < abs_tol)), ...)

And when dealing with relative differences, it becomes more complicated.

zoziha avatar Jul 03 '22 03:07 zoziha