Skip to content

Commit 9d0d3aa

Browse files
committed
Add tests for loadtxt and savetxt
1 parent 7a7ca5f commit 9d0d3aa

File tree

10 files changed

+135
-0
lines changed

10 files changed

+135
-0
lines changed

src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
set(SRC
22
stdlib_types.f90
33
stdlib_io.f90
4+
stdlib_error.f90
45
)
56

67
add_library(fortran_stdlib ${SRC})
78

9+
add_subdirectory(tests)
10+
811
install(TARGETS fortran_stdlib
912
RUNTIME DESTINATION bin
1013
ARCHIVE DESTINATION lib

src/stdlib_error.f90

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module stdlib_error
2+
implicit none
3+
private
4+
public assert, error_stop
5+
6+
contains
7+
8+
subroutine assert(condition)
9+
! If condition == .false., it aborts the program.
10+
!
11+
! Arguments
12+
! ---------
13+
!
14+
logical, intent(in) :: condition
15+
!
16+
! Example
17+
! -------
18+
!
19+
! call assert(a == 5)
20+
21+
if (.not. condition) call error_stop("Assert failed.")
22+
end subroutine
23+
24+
subroutine error_stop(msg)
25+
! Aborts the program with nonzero exit code
26+
!
27+
! The statement "stop msg" will return 0 exit code when compiled using
28+
! gfortran. error_stop() uses the statement "stop 1" which returns an exit code
29+
! 1 and a print statement to print the message.
30+
!
31+
! Example
32+
! -------
33+
!
34+
! call error_stop("Invalid argument")
35+
36+
character(len=*) :: msg ! Message to print on stdout
37+
print *, msg
38+
stop 1
39+
end subroutine
40+
41+
end module

src/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(loadtxt)

src/tests/loadtxt/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
include_directories(${PROJECT_BINARY_DIR}/src)
2+
3+
project(loadtxt)
4+
5+
add_executable(test_loadtxt test_loadtxt.f90)
6+
target_link_libraries(test_loadtxt fortran_stdlib)
7+
8+
add_executable(test_savetxt test_savetxt.f90)
9+
target_link_libraries(test_savetxt fortran_stdlib)
10+
11+
add_test(test_loadtxt ${PROJECT_BINARY_DIR}/test_loadtxt)
12+
add_test(test_savetxt ${PROJECT_BINARY_DIR}/test_savetxt)

src/tests/loadtxt/array1.dat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1 2
2+
3 4
3+
5 6
4+
7 8

src/tests/loadtxt/array2.dat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1 2 9
2+
3 4 10
3+
5 6 11
4+
7 8 12

src/tests/loadtxt/array3.dat

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
1.000000000000000021e-08 9.199998759392489944e+01
2+
1.024113254885563425e-08 9.199998731474968849e+01
3+
1.048233721895820948e-08 9.199998703587728244e+01
4+
1.072361403187881949e-08 9.199998675729767683e+01
5+
1.096496300919481796e-08 9.199998647900135040e+01
6+
1.120638417249036630e-08 9.199998620097916557e+01
7+
1.144787754335570897e-08 9.199998592322251056e+01
8+
1.168944314338753750e-08 9.199998564572304360e+01
9+
1.193108099418952317e-08 9.199998536847290609e+01
10+
1.217279111737088596e-08 9.199998509146449521e+01
11+
1.241457353454836993e-08 9.199998481469057765e+01
12+
1.265642826734443823e-08 9.199998453814424693e+01
13+
1.289835533738818635e-08 9.199998426181879552e+01
14+
1.314035476631514857e-08 9.199998398570787117e+01
15+
1.338242657576766519e-08 9.199998370980536322e+01
16+
1.362457078739434161e-08 9.199998343410533153e+01

src/tests/loadtxt/array4.dat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1.56367173122998851E-010 4.51568171776229776E-007 4.96568621780730290E-006 5.01068666781180638E-005 5.01518671281225327E-004 5.01763629287519872E-003 5.58487648776459511E-002 0.32618374746711520 1.7639051761733842 9.4101331514118236
2+
8.23481961129666271E-010 4.58239319656296504E-007 5.03239769660796763E-006 5.07739814661247314E-005 5.08189819161291786E-004 5.09287863145356859E-003 5.62489258981838380E-002 0.32831192218075922 1.7752234390209392 9.4703270222745211
3+
2.02201163784892633E-009 4.70224616423489051E-007 5.15225066427989480E-006 5.19725111428439625E-005 5.20175115928484585E-004 5.22805802989171828E-003 5.69678499382489378E-002 0.33213537295325257 1.7955576815764616 9.5784705410250410

src/tests/loadtxt/test_loadtxt.f90

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
program test_loadtxt
2+
use stdlib_types, only: dp
3+
use stdlib_io, only: loadtxt
4+
implicit none
5+
6+
real(dp), allocatable :: d(:, :)
7+
call loadtxt("array1.dat", d)
8+
call print_array(d)
9+
10+
call loadtxt("array2.dat", d)
11+
call print_array(d)
12+
13+
call loadtxt("array3.dat", d)
14+
call print_array(d)
15+
16+
call loadtxt("array4.dat", d)
17+
call print_array(d)
18+
19+
contains
20+
21+
subroutine print_array(a)
22+
real(dp) :: a(:, :)
23+
integer :: i
24+
print *, "Array, shape=(", size(a, 1), ",", size(a, 2), ")"
25+
do i = 1, size(a, 1)
26+
print *, a(i, :)
27+
end do
28+
end subroutine
29+
30+
end program

src/tests/loadtxt/test_savetxt.f90

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
program test_loadtxt
2+
use stdlib_types, only: dp
3+
use stdlib_io, only: loadtxt, savetxt
4+
use stdlib_error, only: assert
5+
implicit none
6+
7+
real(dp) :: d(3, 2), e(2, 3)
8+
real(dp), allocatable :: d2(:, :)
9+
d = reshape([1, 2, 3, 4, 5, 6], [3, 2])
10+
call savetxt("tmp.dat", d)
11+
call loadtxt("tmp.dat", d2)
12+
call assert(all(shape(d2) == [3, 2]))
13+
call assert(all(abs(d-d2) < epsilon(1._dp)))
14+
15+
e = reshape([1, 2, 3, 4, 5, 6], [2, 3])
16+
call savetxt("tmp.dat", e)
17+
call loadtxt("tmp.dat", d2)
18+
call assert(all(shape(d2) == [2, 3]))
19+
call assert(all(abs(e-d2) < epsilon(1._dp)))
20+
21+
end program

0 commit comments

Comments
 (0)