Skip to content

Commit 0fa4450

Browse files
committed
add tests, change string swap
1 parent fe02d4b commit 0fa4450

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

src/stdlib_math.fypp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -545,10 +545,10 @@ contains
545545
temp = lhs; lhs = rhs; rhs = temp
546546
end subroutine
547547

548-
elemental subroutine swap_str(lhs, rhs)
549-
character(1), intent(inout) :: lhs, rhs
550-
character(1) :: temp
551-
temp = lhs; lhs = rhs; rhs = temp
548+
elemental subroutine swap_str(lhs,rhs)
549+
character(*), intent(inout) :: lhs, rhs
550+
character(len=max(len(lhs),len(rhs))) :: temp
551+
temp = lhs ; lhs = rhs ; rhs = temp
552552
end subroutine
553553

554554
end module stdlib_math

test/math/test_stdlib_math.fypp

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module test_stdlib_math
66
use testdrive, only : new_unittest, unittest_type, error_type, check, skip_test
7-
use stdlib_math, only: clip, arg, argd, argpi, arange, is_close, all_close, diff, &
7+
use stdlib_math, only: clip, swap, arg, argd, argpi, arange, is_close, all_close, diff, &
88
arange, deg2rad, rad2deg
99
use stdlib_kinds, only: int8, int16, int32, int64, sp, dp, xdp, qp
1010
implicit none
@@ -38,6 +38,15 @@ contains
3838
new_unittest("clip-real-quad", test_clip_rqp), &
3939
new_unittest("clip-real-quad-bounds", test_clip_rqp_bounds) &
4040

41+
!> Tests swap
42+
#:for k1, t1 in INT_KINDS_TYPES + REAL_KINDS_TYPES
43+
, new_unittest("swap_${k1}$", test_swap_${k1}$) &
44+
#:endfor
45+
#:for k1, t1 in CMPLX_KINDS_TYPES
46+
, new_unittest("swap_c${k1}$", test_swap_c${k1}$) &
47+
#:endfor
48+
, new_unittest("swap_str", test_swap_str) &
49+
4150
!> Tests for arg/argd/argpi
4251
#:for k1 in CMPLX_KINDS
4352
, new_unittest("arg-cmplx-${k1}$", test_arg_${k1}$) &
@@ -246,6 +255,55 @@ contains
246255

247256
end subroutine test_clip_rqp_bounds
248257

258+
#:for k1, t1 in INT_KINDS_TYPES + REAL_KINDS_TYPES
259+
subroutine test_swap_${k1}$(error)
260+
type(error_type), allocatable, intent(out) :: error
261+
${t1}$ :: x(3), y(3)
262+
263+
x = [${t1}$ :: 1, 2, 3]
264+
y = [${t1}$ :: 4, 5, 6]
265+
266+
call swap(x,y)
267+
268+
call check(error, all( x == [${t1}$ :: 4, 5, 6] ) )
269+
if (allocated(error)) return
270+
call check(error, all( y == [${t1}$ :: 1, 2, 3] ) )
271+
if (allocated(error)) return
272+
end subroutine test_swap_${k1}$
273+
#:endfor
274+
275+
#:for k1, t1 in CMPLX_KINDS_TYPES
276+
subroutine test_swap_c${k1}$(error)
277+
type(error_type), allocatable, intent(out) :: error
278+
${t1}$ :: x(3), y(3)
279+
280+
x = cmplx( [1, 2, 3] , [4, 5, 6] )
281+
y = cmplx( [4, 5, 6] , [1, 2, 3] )
282+
283+
call swap(x,y)
284+
285+
call check(error, all( x == cmplx( [4, 5, 6] , [1, 2, 3] ) ) )
286+
if (allocated(error)) return
287+
call check(error, all( y == cmplx( [1, 2, 3] , [4, 5, 6] ) ) )
288+
if (allocated(error)) return
289+
end subroutine test_swap_c${k1}$
290+
#:endfor
291+
292+
subroutine test_swap_str(error)
293+
type(error_type), allocatable, intent(out) :: error
294+
character(5) :: x(2), y(2)
295+
296+
x = ['abcde','fghij']
297+
y = ['fghij','abcde']
298+
299+
call swap(x,y)
300+
301+
call check(error, all( x == ['fghij','abcde'] ) )
302+
if (allocated(error)) return
303+
call check(error, all( y == ['abcde','fghij'] ) )
304+
if (allocated(error)) return
305+
end subroutine test_swap_str
306+
249307
#:for k1 in CMPLX_KINDS
250308
subroutine test_arg_${k1}$(error)
251309
type(error_type), allocatable, intent(out) :: error

0 commit comments

Comments
 (0)