Skip to content

Commit 554f301

Browse files
committed
added tests for clip function in test_math.f90 file
1 parent 2608434 commit 554f301

File tree

4 files changed

+181
-1
lines changed

4 files changed

+181
-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

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
subroutine test_clip_real_sp(x, xmin, xmax, compare)
53+
real(sp), intent(in) :: x
54+
real(sp), intent(in) :: xmin
55+
real(sp), intent(in) :: xmax
56+
real(sp), intent(in) :: compare
57+
58+
call check(clip(x, xmin, xmax) == compare)
59+
60+
end subroutine test_clip_real_sp
61+
62+
subroutine test_clip_real_dp(x, xmin, xmax, compare)
63+
real(dp), intent(in) :: x
64+
real(dp), intent(in) :: xmin
65+
real(dp), intent(in) :: xmax
66+
real(dp), intent(in) :: compare
67+
68+
call check(clip(x, xmin, xmax) == compare)
69+
70+
end subroutine test_clip_real_dp
71+
72+
subroutine test_clip_real_qp(x, xmin, xmax, compare)
73+
real(qp), intent(in) :: x
74+
real(qp), intent(in) :: xmin
75+
real(qp), intent(in) :: xmax
76+
real(qp), intent(in) :: compare
77+
78+
call check(clip(x, xmin, xmax) == compare)
79+
80+
end subroutine test_clip_real_qp
81+
82+
83+
end module test_math
84+
85+
86+
program tester
87+
use test_math
88+
implicit none
89+
90+
! test case format: (x, xmin, xmax, correct answer)
91+
! valid case: xmin is not greater than xmax
92+
! invalid case: xmin is greater than xmax
93+
94+
95+
! type: integer, kind: int8
96+
! valid test cases
97+
call test_clip_integer_int8(2, -2, 5, 2)
98+
call test_clip_integer_int8(127, -128, 0, 0)
99+
call test_clip_integer_int8(-57, -57, 57, -57)
100+
101+
! invalid test cases
102+
call test_clip_integer_int8(2, 5, -2, 5)
103+
call test_clip_integer_int8(127, 0, -128, 0)
104+
call test_clip_integer_int8(-57, 57, -57, 57)
105+
106+
! type: integer, kind: int16
107+
! valid test cases
108+
call test_clip_integer_int16(2, -2, 5, 2)
109+
call test_clip_integer_int16(32767, -32768, 0, 0)
110+
call test_clip_integer_int16(-598, -32, 676, -32)
111+
112+
! invalid test cases
113+
call test_clip_integer_int16(2, 5, -2, 5)
114+
call test_clip_integer_int16(32767, 0, -32768, 0)
115+
call test_clip_integer_int16(-598, 676, -32, 676)
116+
117+
! type: integer, kind: int32
118+
! valid test cases
119+
call test_clip_integer_int32(2, -2, 5, 2)
120+
call test_clip_integer_int32(-2147483648, 0, 2147483647, 0)
121+
call test_clip_integer_int32(45732, -385769, 57642, 45732)
122+
123+
! invalid test cases
124+
call test_clip_integer_int32(2, 5, -2, 5)
125+
call test_clip_integer_int32(-2147483648, 2147483647, 0, 2147483647)
126+
call test_clip_integer_int32(45732, 57642, -385769, 57642)
127+
128+
! type: integer, kind: int64
129+
! valid test cases
130+
call test_clip_integer_int64(2, -2, 5, 2)
131+
call test_clip_integer_int64(-9223372036854775808, -10, 25, -10)
132+
call test_clip_integer_int64(97683, -200, 5137883248, 97683)
133+
134+
! invalid test cases
135+
call test_clip_integer_int64(2, 5, -2, 5)
136+
call test_clip_integer_int64(-9223372036854775808, 25, -10, 25)
137+
call test_clip_integer_int64(97683, 5137883248, -200, 5137883248)
138+
139+
! type: real, kind: sp
140+
! valid test cases
141+
call test_clip_real_sp(3.025, -5.77, 3.025, 3.025)
142+
call test_clip_real_sp(0.0, -1578.025, -59.68, -59.68)
143+
call test_clip_real_sp(5.6, -97854.25, 2.3666, 2.3666)
144+
145+
! invalid test cases
146+
call test_clip_real_sp(3.025, 3.025, -5.77, 3.025)
147+
call test_clip_real_sp(0.0, -59.68, -1578.025, -59.68)
148+
call test_clip_real_sp(5.6, 2.3666, -97854.25, 2.3666)
149+
150+
! type: real, kind: dp
151+
! valid test cases
152+
call test_clip_real_dp(3.025, -5.77, 3.025, 3.025)
153+
call test_clip_real_dp(-7.0, 0.059668, 1.00268, 0.059668)
154+
call test_clip_real_dp(-12.3358, 8.55759, 8.55759, 8.55759)
155+
156+
! invalid test cases
157+
call test_clip_real_dp(3.025, 3.025, -5.77, 3.025)
158+
call test_clip_real_dp(-7.0, 1.00268, 0.059668, 1.00268)
159+
call test_clip_real_dp(-12.3358, 8.55759, 8.55759, 8.55759)
160+
161+
! type: real, kind: qp
162+
! valid test cases
163+
call test_clip_real_qp(3.025, -5.77, 3.025, 3.025)
164+
call test_clip_real_qp(-55891546.2, -8958133457.23, -689712245.23, -689712245.23)
165+
call test_clip_real_qp(58.7, -2352.335, -189.58, -189.58)
166+
167+
! invalid test cases
168+
call test_clip_real_qp(3.025, 3.025, -5.77, 3.025)
169+
call test_clip_real_qp(-55891546.2, -689712245.23, -8958133457.23, -689712245.23)
170+
call test_clip_real_qp(58.7, -189.58, -2352.335, -189.58)
171+
172+
173+
end program tester

0 commit comments

Comments
 (0)