test-drive
test-drive copied to clipboard
Support array checking for the `check` subroutine
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
One can also use an array expression like
call check(error, all(array == expected))
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.