Skip to content

Commit f586e6f

Browse files
committed
added test cases for clip function
1 parent 2608434 commit f586e6f

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

src/stdlib_math.fypp

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
#:set INT_KINDS_TYPES = [("int8", "integer"), ("int16", "integer"), ("int32", "integer"), ("int64", "integer")]
44
#:set REAL_KINDS_TYPES = [("sp", "real"), ("dp", "real"), ("qp", "real")]
5-
65
#:set IR_KINDS_TYPES = INT_KINDS_TYPES + REAL_KINDS_TYPES
6+
77
module stdlib_math
88
use stdlib_kinds, only: int8, int16, int32, int64, sp, dp, qp
99

@@ -20,6 +20,7 @@ module stdlib_math
2020

2121

2222
contains
23+
2324
#:for k1, t1 in IR_KINDS_TYPES
2425
elemental function clip_${t1}$_${k1}$(x, xmin, xmax) result(res)
2526
${t1}$(${k1}$), intent(in) :: x
@@ -28,6 +29,7 @@ module stdlib_math
2829
${t1}$(${k1}$) :: res
2930
res = max(min(x, xmax), xmin)
3031
end function clip_${t1}$_${k1}$
32+
3133
#:endfor
3234

3335
end module stdlib_math

src/tests/math/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ADDTEST(math)

src/tests/math/Makefile.manual

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PROGS_SRC = test_math.f90
2+
3+
4+
include ../Makefile.manual.test.mk

src/tests/math/test_math.f90

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

Comments
 (0)