|
| 1 | +! SPDX-Identifier: MIT |
| 2 | + |
| 3 | + |
| 4 | +module test_math |
| 5 | + use stdlib_kinds, only: int8, int16, int32, int64, sp, dp, qp |
| 6 | + use stdlib_error, only : check |
| 7 | + use stdlib_math |
| 8 | + |
| 9 | + implicit none |
| 10 | + |
| 11 | +contains |
| 12 | + subroutine test_clip_integer_int8(x, xmin, xmax, compare) |
| 13 | + integer(int8), intent(in) :: x |
| 14 | + integer(int8), intent(in) :: xmin |
| 15 | + integer(int8), intent(in) :: xmax |
| 16 | + integer(int8), intent(in) :: compare |
| 17 | + |
| 18 | + call check(clip(x, xmin, xmax) == compare) |
| 19 | + |
| 20 | + end subroutine test_clip_integer_int8 |
| 21 | + |
| 22 | + subroutine test_clip_integer_int16(x, xmin, xmax, compare) |
| 23 | + integer(int16), intent(in) :: x |
| 24 | + integer(int16), intent(in) :: xmin |
| 25 | + integer(int16), intent(in) :: xmax |
| 26 | + integer(int16), intent(in) :: compare |
| 27 | + |
| 28 | + call check(clip(x, xmin, xmax) == compare) |
| 29 | + |
| 30 | + end subroutine test_clip_integer_int16 |
| 31 | + |
| 32 | + subroutine test_clip_integer_int32(x, xmin, xmax, compare) |
| 33 | + integer(int32), intent(in) :: x |
| 34 | + integer(int32), intent(in) :: xmin |
| 35 | + integer(int32), intent(in) :: xmax |
| 36 | + integer(int32), intent(in) :: compare |
| 37 | + |
| 38 | + call check(clip(x, xmin, xmax) == compare) |
| 39 | + |
| 40 | + end subroutine test_clip_integer_int32 |
| 41 | + |
| 42 | + subroutine test_clip_integer_int64(x, xmin, xmax, compare) |
| 43 | + integer(int64), intent(in) :: x |
| 44 | + integer(int64), intent(in) :: xmin |
| 45 | + integer(int64), intent(in) :: xmax |
| 46 | + integer(int64), intent(in) :: compare |
| 47 | + |
| 48 | + call check(clip(x, xmin, xmax) == compare) |
| 49 | + |
| 50 | + end subroutine test_clip_integer_int64 |
| 51 | + |
| 52 | + |
| 53 | +end module test_math |
| 54 | + |
| 55 | + |
| 56 | +program tester |
| 57 | + use test_math |
| 58 | + implicit none |
| 59 | + |
| 60 | + ! test case format: (x, xmin, xmax, correct answer) |
| 61 | + ! valid case: xmin is not greater than xmax |
| 62 | + ! invalid case: xmin is greater than xmax |
| 63 | + |
| 64 | + |
| 65 | + ! type: integer, kind: int8 |
| 66 | + ! valid test cases |
| 67 | + call test_clip_integer_int8(2, -2, 5, 2) |
| 68 | + call test_clip_integer_int8(127, -128, 0, 0) |
| 69 | + call test_clip_integer_int8(-57, -57, 57, -57) |
| 70 | + |
| 71 | + ! invalid test cases |
| 72 | + call test_clip_integer_int8(2, 5, -2, 5) |
| 73 | + call test_clip_integer_int8(127, 0, -128, 0) |
| 74 | + call test_clip_integer_int8(-57, 57, -57, 57) |
| 75 | + |
| 76 | + ! type: integer, kind: int16 |
| 77 | + ! valid test cases |
| 78 | + call test_clip_integer_int16(2, -2, 5, 2) |
| 79 | + call test_clip_integer_int16(32767, -32768, 0, 0) |
| 80 | + call test_clip_integer_int16(-598, -32, 676, -32) |
| 81 | + |
| 82 | + ! invalid test cases |
| 83 | + call test_clip_integer_int16(2, 5, -2, 5) |
| 84 | + call test_clip_integer_int16(32767, 0, -32768, 0) |
| 85 | + call test_clip_integer_int16(-598, 676, -32, 676) |
| 86 | + |
| 87 | + ! type: integer, kind: int32 |
| 88 | + ! valid test cases |
| 89 | + call test_clip_integer_int32(2, -2, 5, 2) |
| 90 | + call test_clip_integer_int32(-2147483648, 0, 2147483647, 0) |
| 91 | + call test_clip_integer_int32(45732, -385769, 57642, 45732) |
| 92 | + |
| 93 | + ! invalid test cases |
| 94 | + call test_clip_integer_int32(2, 5, -2, 5) |
| 95 | + call test_clip_integer_int32(-2147483648, 2147483647, 0, 2147483647) |
| 96 | + call test_clip_integer_int32(45732, 57642, -385769, 57642) |
| 97 | + |
| 98 | + ! type: integer, kind: int64 |
| 99 | + ! valid test cases |
| 100 | + call test_clip_integer_int64(2, -2, 5, 2) |
| 101 | + call test_clip_integer_int64(-9223372036854775808, -10, 25, -10) |
| 102 | + call test_clip_integer_int64(97683, -200, 5137883248, 97683) |
| 103 | + |
| 104 | + ! invalid test cases |
| 105 | + call test_clip_integer_int64(2, 5, -2, 5) |
| 106 | + call test_clip_integer_int64(-9223372036854775808, 25, -10, 25) |
| 107 | + call test_clip_integer_int64(97683, 5137883248, -200, 5137883248) |
| 108 | + |
| 109 | + |
| 110 | +end program tester |
0 commit comments