Skip to content

Added complex to io #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reverted formatting changes. Complex support for loadtxt and savetxt
  • Loading branch information
fiolj committed Feb 3, 2020
commit fafd22931f9ce229f6e871e93b9df88b133609e1
102 changes: 51 additions & 51 deletions src/tests/io/test_loadtxt.f90
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
program test_loadtxt
use stdlib_experimental_kinds, only: int32, sp, dp
use stdlib_experimental_io, only: loadtxt
use stdlib_experimental_error, only: error_stop
implicit none
use stdlib_experimental_kinds, only: int32, sp, dp
use stdlib_experimental_io, only: loadtxt
use stdlib_experimental_error, only: error_stop
implicit none

integer(int32), allocatable :: i(:, :)
real(sp), allocatable :: s(:, :)
real(dp), allocatable :: d(:, :)
complex(dp), allocatable :: z(:, :)
integer(int32), allocatable :: i(:, :)
real(sp), allocatable :: s(:, :)
real(dp), allocatable :: d(:, :)
complex(dp), allocatable :: z(:, :)

call loadtxt("array1.dat", i)
call print_array(i)
call loadtxt("array1.dat", i)
call print_array(i)

call loadtxt("array1.dat", s)
call print_array(s)
call loadtxt("array1.dat", s)
call print_array(s)

call loadtxt("array1.dat", d)
call print_array(d)
call loadtxt("array1.dat", d)
call print_array(d)

call loadtxt("array2.dat", d)
call print_array(d)
call loadtxt("array2.dat", d)
call print_array(d)

call loadtxt("array3.dat", d)
call print_array(d)
call loadtxt("array3.dat", d)
call print_array(d)

call loadtxt("array4.dat", d)
call print_array(d)
call loadtxt("array4.dat", d)
call print_array(d)

call loadtxt("array5.dat", z)
call print_array(z)
call loadtxt("array5.dat", z)
call print_array(z)

contains

subroutine print_array(a)
class(*),intent(in) :: a(:, :)
integer :: i
print *, "Array, shape=(", size(a, 1), ",", size(a, 2), ")"

select type(a)
type is(integer(int32))
do i = 1, size(a, 1)
print *, a(i, :)
end do
type is(real(sp))
do i = 1, size(a, 1)
print *, a(i, :)
end do
type is(real(dp))
do i = 1, size(a, 1)
print *, a(i, :)
end do
type is(complex(dp))
do i = 1, size(a, 1)
print *, a(i, :)
end do
class default
call error_stop('The proposed type is not supported')
end select

end subroutine print_array

end program test_loadtxt
subroutine print_array(a)
class(*),intent(in) :: a(:, :)
integer :: i
print *, "Array, shape=(", size(a, 1), ",", size(a, 2), ")"

select type(a)
type is(integer(int32))
do i = 1, size(a, 1)
print *, a(i, :)
end do
type is(real(sp))
do i = 1, size(a, 1)
print *, a(i, :)
end do
type is(real(dp))
do i = 1, size(a, 1)
print *, a(i, :)
end do
type is(complex(dp))
do i = 1, size(a, 1)
print *, a(i, :)
end do
class default
call error_stop('The proposed type is not supported')
end select

end subroutine

end program
52 changes: 26 additions & 26 deletions src/tests/io/test_savetxt.f90
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
program test_savetxt
use stdlib_experimental_kinds, only: int32, sp, dp
use stdlib_experimental_io, only: loadtxt, savetxt
use stdlib_experimental_error, only: assert
implicit none
use stdlib_experimental_kinds, only: int32, sp, dp
use stdlib_experimental_io, only: loadtxt, savetxt
use stdlib_experimental_error, only: assert
implicit none

character(:), allocatable :: outpath
character(:), allocatable :: outpath

outpath = get_outpath() // "/tmp.dat"
outpath = get_outpath() // "/tmp.dat"

call test_int32(outpath)
call test_rsp(outpath)
call test_rdp(outpath)
call test_csp(outpath)
call test_cdp(outpath)
call test_int32(outpath)
call test_rsp(outpath)
call test_rdp(outpath)
call test_csp(outpath)
call test_cdp(outpath)

contains

function get_outpath() result(outpath)
function get_outpath() result(outpath)
integer :: ierr
character(256) :: argv
character(:), allocatable :: outpath

call get_command_argument(1, argv, status=ierr)
if (ierr==0) then
outpath = trim(argv)
outpath = trim(argv)
else
outpath = '.'
outpath = '.'
endif
end function get_outpath
end function get_outpath

subroutine test_int32(outpath)
subroutine test_int32(outpath)
character(*), intent(in) :: outpath
integer(int32) :: d(3, 2), e(2, 3)
integer(int32), allocatable :: d2(:, :)
Expand All @@ -44,10 +44,10 @@ subroutine test_int32(outpath)
call loadtxt(outpath, d2)
call assert(all(shape(d2) == [2, 3]))
call assert(all(abs(e-d2) == 0))
end subroutine test_int32
end subroutine


subroutine test_rsp(outpath)
subroutine test_rsp(outpath)
character(*), intent(in) :: outpath
real(sp) :: d(3, 2), e(2, 3)
real(sp), allocatable :: d2(:, :)
Expand All @@ -62,7 +62,7 @@ subroutine test_rsp(outpath)
call loadtxt(outpath, d2)
call assert(all(shape(d2) == [2, 3]))
call assert(all(abs(e-d2) < epsilon(1._sp)))
end subroutine test_rsp
end subroutine


subroutine test_rdp(outpath)
Expand All @@ -80,9 +80,9 @@ subroutine test_rdp(outpath)
call loadtxt(outpath, d2)
call assert(all(shape(d2) == [2, 3]))
call assert(all(abs(e-d2) < epsilon(1._dp)))
end subroutine test_rdp
end subroutine

subroutine test_csp(outpath)
subroutine test_csp(outpath)
character(*), intent(in) :: outpath
complex(sp) :: d(3, 2), e(2, 3)
complex(sp), allocatable :: d2(:, :)
Expand All @@ -92,14 +92,14 @@ subroutine test_csp(outpath)
call assert(all(shape(d2) == [3, 2]))
call assert(all(abs(d-d2) < epsilon(1._sp)))

e = reshape([1, 2, 3, 4, 5, 6], [2, 3])
e = cmplx(1, 1)* reshape([1, 2, 3, 4, 5, 6], [2, 3])
call savetxt(outpath, e)
call loadtxt(outpath, d2)
call assert(all(shape(d2) == [2, 3]))
call assert(all(abs(e-d2) < epsilon(1._sp)))
end subroutine test_csp
end subroutine

subroutine test_cdp(outpath)
subroutine test_cdp(outpath)
character(*), intent(in) :: outpath
complex(dp) :: d(3, 2), e(2, 3)
complex(dp), allocatable :: d2(:, :)
Expand All @@ -109,11 +109,11 @@ subroutine test_cdp(outpath)
call assert(all(shape(d2) == [3, 2]))
call assert(all(abs(d-d2) < epsilon(1._dp)))

e = reshape([1, 2, 3, 4, 5, 6], [2, 3])
e = cmplx(1, 1)* reshape([1, 2, 3, 4, 5, 6], [2, 3])
call savetxt(outpath, e)
call loadtxt(outpath, d2)
call assert(all(shape(d2) == [2, 3]))
call assert(all(abs(e-d2) < epsilon(1._dp)))
end subroutine test_cdp
end subroutine

end program test_savetxt