|
4 | 4 |
|
5 | 5 | module test_stdlib_math
|
6 | 6 | use testdrive, only : new_unittest, unittest_type, error_type, check, skip_test
|
7 |
| - use stdlib_math, only: clip, is_close, all_close |
| 7 | + use stdlib_math, only: clip, is_close, all_close, diff |
8 | 8 | use stdlib_kinds, only: int8, int16, int32, int64, sp, dp, xdp, qp
|
9 | 9 | implicit none
|
10 | 10 |
|
@@ -40,6 +40,14 @@ contains
|
40 | 40 | , new_unittest("all_close-real-${k1}$", test_all_close_real_${k1}$) &
|
41 | 41 | , new_unittest("all_close-cmplx-${k1}$", test_all_close_cmplx_${k1}$) &
|
42 | 42 | #:endfor
|
| 43 | + |
| 44 | + !> Tests for `diff` |
| 45 | + #:for k1 in REAL_KINDS |
| 46 | + , new_unittest("diff-real-${k1}$", test_diff_real_${k1}$) & |
| 47 | + #:endfor |
| 48 | + #:for k1 in INT_KINDS |
| 49 | + , new_unittest("diff-int-${k1}$", test_diff_int_${k1}$) & |
| 50 | + #:endfor |
43 | 51 | ]
|
44 | 52 |
|
45 | 53 | end subroutine collect_stdlib_math
|
@@ -294,6 +302,48 @@ contains
|
294 | 302 |
|
295 | 303 | end subroutine test_all_close_cmplx_${k1}$
|
296 | 304 | #:endfor
|
| 305 | + |
| 306 | + #:for k1, t1 in REAL_KINDS_TYPES |
| 307 | + subroutine test_diff_real_${k1}$(error) |
| 308 | + type(error_type), allocatable, intent(out) :: error |
| 309 | + ${t1}$ :: x(6) = [${t1}$ :: 0, 5, 15, 30, 50, 75] |
| 310 | + ${t1}$ :: A(1, 3) = reshape([${t1}$ :: 1, 3, 5], [1, 3]) |
| 311 | + |
| 312 | + call check(error, all_close(diff(x), [${t1}$ :: 5, 10, 15, 20, 25]), & |
| 313 | + "diff(x) in test_diff_real_${k1}$ failed") |
| 314 | + if (allocated(error)) return |
| 315 | + |
| 316 | + call check(error, all_close(diff(x, n=2), [${t1}$ :: 5, 5, 5, 5]), & |
| 317 | + "diff(x, n=2) in test_diff_real_${k1}$ failed") |
| 318 | + if (allocated(error)) return |
| 319 | + |
| 320 | + call check(error, all_close(diff(A, n=1, dim=2), reshape([${t1}$ :: 2, 2], [1, 2])), & |
| 321 | + "diff(x, n=1, dim=2) in test_diff_real_${k1}$ failed") |
| 322 | + if (allocated(error)) return |
| 323 | + |
| 324 | + end subroutine test_diff_real_${k1}$ |
| 325 | + #:endfor |
| 326 | + |
| 327 | + #:for k1, t1 in INT_KINDS_TYPES |
| 328 | + subroutine test_diff_int_${k1}$(error) |
| 329 | + type(error_type), allocatable, intent(out) :: error |
| 330 | + ${t1}$ :: x(6) = [${t1}$ :: 0, 5, 15, 30, 50, 75] |
| 331 | + ${t1}$ :: A(1, 3) = reshape([${t1}$ :: 1, 3, 5], [1, 3]) |
| 332 | + |
| 333 | + call check(error, all(diff(x) == [${t1}$ :: 5, 10, 15, 20, 25]), & |
| 334 | + "diff(x) in test_diff_int_${k1}$ failed") |
| 335 | + if (allocated(error)) return |
| 336 | + |
| 337 | + call check(error, all(diff(x, n=2) == [${t1}$ :: 5, 5, 5, 5]), & |
| 338 | + "diff(x, n=2) in test_diff_int_${k1}$ failed") |
| 339 | + if (allocated(error)) return |
| 340 | + |
| 341 | + call check(error, all(diff(A, n=1, dim=2) == reshape([${t1}$ :: 2, 2], [1, 2])), & |
| 342 | + "diff(A, n=1, dim=2) in test_diff_int_${k1}$ failed") |
| 343 | + if (allocated(error)) return |
| 344 | + |
| 345 | + end subroutine test_diff_int_${k1}$ |
| 346 | + #:endfor |
297 | 347 |
|
298 | 348 | end module test_stdlib_math
|
299 | 349 |
|
|
0 commit comments