Skip to content

Commit 87d6dc3

Browse files
authored
Port stdlib_io tests to test-drive
* Port test_open.f90 to test-drive * Port test_parse_mode.f90 to test-drive * Port test_savetxt.f90 to test-drive * Port test_savetxt_qp.f90 to test-drive * Port test_loadtxt.f90 to test-drive * Add complex data test to test_loadtxt.f90 * Port test_loadtxt_qp.f90 to test-drive * Clean all test artifacts in Makefile.manual * Fix error handling in test_loadtxt * Don't reuse output file names between tests
1 parent 83d321c commit 87d6dc3

File tree

8 files changed

+803
-504
lines changed

8 files changed

+803
-504
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ You can pass additional options to CMake to customize the build.
125125
Important options are
126126

127127
- `-G Ninja` to use the Ninja backend instead of the default Make backend. Other build backends are available with a similar syntax.
128-
- `-DCMAKE_INSTALL_PREFIX` is used to provide the install location for the library.
128+
- `-DCMAKE_INSTALL_PREFIX` is used to provide the install location for the library. If not provided the defaults will depend on your operating system, [see here](https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html).
129129
- `-DCMAKE_MAXIMUM_RANK` the maximum array rank procedures should be generated for.
130130
The default value is chosen as 4.
131131
The maximum is 15 for Fortran 2003 compliant compilers, otherwise 7 for compilers not supporting Fortran 2003 completely yet.
@@ -170,7 +170,7 @@ Alternatively, you can build using provided Makefiles:
170170
make -f Makefile.manual
171171
```
172172

173-
You can limit the maximum rank by setting ``-DMAXRANK=<num>`` in the ``FYPPFLAGS`` environment variable:
173+
You can limit the maximum rank by setting ``-DMAXRANK=<num>`` in the ``FYPPFLAGS`` environment variable (which can reduce the compilation time):
174174

175175
```sh
176176
make -f Makefile.manual FYPPFLAGS=-DMAXRANK=4
@@ -196,7 +196,7 @@ target_link_libraries(
196196
```
197197

198198
To make the installed stdlib project discoverable add the stdlib directory to the ``CMAKE_PREFIX_PATH``.
199-
The usual install localtion of the package files is ``$PREFIX/lib/cmake/fortran_stdlib``.
199+
The usual install location of the package files is ``$PREFIX/lib/cmake/fortran_stdlib``.
200200

201201
For non-CMake build systems (like make) you can use the exported pkg-config file by setting ``PKG_CONFIG_PATH`` to include the directory containing the exported pc-file.
202202
The usual install location of the pc-file is ``$PREFIX/lib/pkgconfig``.

src/tests/io/Makefile.manual

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
PROGS_SRC = test_loadtxt.f90 \
2-
test_savetxt.f90 \
3-
test_loadtxt_qp.f90 \
4-
test_savetxt_qp.f90 \
5-
test_parse_mode.f90 \
6-
test_open.f90
2+
test_savetxt.f90 \
3+
test_loadtxt_qp.f90 \
4+
test_savetxt_qp.f90 \
5+
test_parse_mode.f90 \
6+
test_open.f90
77

8-
CLEAN_FILES = tmp.dat tmp_qp.dat io_open.dat io_open.stream
8+
CLEAN_FILES = tmp*.dat array*_new.dat io_open.dat io_open.stream
99

1010

1111
include ../Makefile.manual.test.mk

src/tests/io/test_loadtxt.f90

Lines changed: 114 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,127 @@
1-
program test_loadtxt
2-
use stdlib_kinds, only: int32, sp, dp
3-
use stdlib_io, only: loadtxt
4-
use stdlib_error, only: error_stop
5-
implicit none
1+
module test_loadtxt
2+
use stdlib_kinds, only: int32, sp, dp
3+
use stdlib_io, only: loadtxt, savetxt
4+
use stdlib_test, only: new_unittest, unittest_type, error_type, check
5+
implicit none
66

7-
integer(int32), allocatable :: i(:, :)
8-
real(sp), allocatable :: s(:, :)
9-
real(dp), allocatable :: d(:, :)
10-
complex(dp), allocatable :: z(:, :)
7+
private
8+
public :: collect_loadtxt
9+
contains
1110

12-
call loadtxt("array1.dat", i)
13-
call print_array(i)
11+
!> Collect all exported unit tests
12+
subroutine collect_loadtxt(testsuite)
13+
!> Collection of tests
14+
type(unittest_type), allocatable, intent(out) :: testsuite(:)
1415

15-
call loadtxt("array1.dat", s)
16-
call print_array(s)
16+
testsuite = [ &
17+
new_unittest("loadtxt_int32", test_loadtxt_int32), &
18+
new_unittest("loadtxt_sp", test_loadtxt_sp), &
19+
new_unittest("loadtxt_dp", test_loadtxt_dp), &
20+
new_unittest("loadtxt_complex", test_loadtxt_complex) &
21+
]
1722

18-
call loadtxt("array1.dat", d)
19-
call print_array(d)
23+
end subroutine collect_loadtxt
2024

21-
call loadtxt("array2.dat", d)
22-
call print_array(d)
2325

24-
call loadtxt("array3.dat", d)
25-
call print_array(d)
26+
subroutine test_loadtxt_int32(error)
27+
!> Error handling
28+
type(error_type), allocatable, intent(out) :: error
29+
integer(int32), allocatable :: input(:,:), expected(:,:)
2630

27-
call loadtxt("array4.dat", d)
28-
call print_array(d)
31+
call loadtxt("array1.dat", input)
32+
call savetxt("array1_new.dat", input)
33+
call loadtxt("array1_new.dat", expected)
34+
call check(error, all(input == expected))
35+
if (allocated(error)) return
2936

30-
call loadtxt("array5.dat", z)
31-
call print_array(z)
37+
call loadtxt("array2.dat", input)
38+
call savetxt("array2_new.dat", input)
39+
call loadtxt("array2_new.dat", expected)
40+
call check(error, all(input == expected))
41+
if (allocated(error)) return
3242

33-
contains
43+
end subroutine test_loadtxt_int32
44+
45+
46+
subroutine test_loadtxt_sp(error)
47+
!> Error handling
48+
type(error_type), allocatable, intent(out) :: error
49+
real(sp), allocatable :: input(:,:), expected(:,:)
50+
51+
call loadtxt("array3.dat", input)
52+
call savetxt("array3_sp.dat", input)
53+
call loadtxt("array3_sp.dat", expected)
54+
call check(error, all(input == expected))
55+
if (allocated(error)) return
56+
57+
call loadtxt("array4.dat", input)
58+
call savetxt("array4_sp.dat", input)
59+
call loadtxt("array4_sp.dat", expected)
60+
call check(error, all(input == expected))
61+
if (allocated(error)) return
62+
63+
end subroutine test_loadtxt_sp
64+
65+
66+
subroutine test_loadtxt_dp(error)
67+
!> Error handling
68+
type(error_type), allocatable, intent(out) :: error
69+
real(dp), allocatable :: input(:,:), expected(:,:)
70+
71+
call loadtxt("array3.dat", input)
72+
call savetxt("array3_dp.dat", input)
73+
call loadtxt("array3_dp.dat", expected)
74+
call check(error, all(input == expected))
75+
if (allocated(error)) return
76+
77+
call loadtxt("array4.dat", input)
78+
call savetxt("array4_dp.dat", input)
79+
call loadtxt("array4_dp.dat", expected)
80+
call check(error, all(input == expected))
81+
if (allocated(error)) return
82+
83+
end subroutine test_loadtxt_dp
84+
85+
86+
subroutine test_loadtxt_complex(error)
87+
!> Error handling
88+
type(error_type), allocatable, intent(out) :: error
89+
complex(dp), allocatable :: input(:,:), expected(:,:)
90+
91+
call loadtxt("array5.dat", input)
92+
call savetxt("array5_new.dat", input)
93+
call loadtxt("array5_new.dat", expected)
94+
call check(error, all(input == expected))
95+
if (allocated(error)) return
96+
97+
end subroutine test_loadtxt_complex
98+
99+
end module test_loadtxt
100+
101+
102+
program tester
103+
use, intrinsic :: iso_fortran_env, only : error_unit
104+
use stdlib_test, only : run_testsuite, new_testsuite, testsuite_type
105+
use test_loadtxt, only : collect_loadtxt
106+
implicit none
107+
integer :: stat, is
108+
type(testsuite_type), allocatable :: testsuites(:)
109+
character(len=*), parameter :: fmt = '("#", *(1x, a))'
110+
111+
stat = 0
112+
113+
testsuites = [ &
114+
new_testsuite("loadtxt", collect_loadtxt) &
115+
]
34116

35-
subroutine print_array(a)
36-
class(*),intent(in) :: a(:, :)
37-
integer :: i
38-
print *, "Array, shape=(", size(a, 1), ",", size(a, 2), ")"
39-
40-
select type(a)
41-
type is(integer(int32))
42-
do i = 1, size(a, 1)
43-
print *, a(i, :)
44-
end do
45-
type is(real(sp))
46-
do i = 1, size(a, 1)
47-
print *, a(i, :)
48-
end do
49-
type is(real(dp))
50-
do i = 1, size(a, 1)
51-
print *, a(i, :)
52-
end do
53-
type is(complex(dp))
54-
do i = 1, size(a, 1)
55-
print *, a(i, :)
117+
do is = 1, size(testsuites)
118+
write(error_unit, fmt) "Testing:", testsuites(is)%name
119+
call run_testsuite(testsuites(is)%collect, error_unit, stat)
56120
end do
57-
class default
58-
call error_stop('The proposed type is not supported')
59-
end select
60121

61-
end subroutine
122+
if (stat > 0) then
123+
write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!"
124+
error stop
125+
end if
62126

63-
end program
127+
end program tester

src/tests/io/test_loadtxt_qp.f90

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,64 @@
1-
program test_loadtxt_qp
2-
use stdlib_kinds, only: qp
3-
use stdlib_io, only: loadtxt
4-
implicit none
1+
module test_loadtxt_qp
2+
use stdlib_kinds, only: qp
3+
use stdlib_io, only: loadtxt, savetxt
4+
use stdlib_test, only: new_unittest, unittest_type, error_type, check
5+
implicit none
56

6-
real(qp), allocatable :: q(:, :)
7+
private
8+
public :: collect_loadtxt_qp
9+
contains
710

8-
call loadtxt("array4.dat", q)
9-
call print_array(q)
11+
!> Collect all exported unit tests
12+
subroutine collect_loadtxt_qp(testsuite)
13+
!> Collection of tests
14+
type(unittest_type), allocatable, intent(out) :: testsuite(:)
1015

11-
contains
16+
testsuite = [ &
17+
new_unittest("loadtxt_qp", test_loadtxt_qp_) &
18+
]
19+
20+
end subroutine collect_loadtxt_qp
21+
22+
23+
subroutine test_loadtxt_qp_(error)
24+
!> Error handling
25+
type(error_type), allocatable, intent(out) :: error
26+
real(qp), allocatable :: input(:,:), expected(:,:)
27+
28+
call loadtxt("array4.dat", input)
29+
call savetxt("array4_new.dat", input)
30+
call loadtxt("array4_new.dat", expected)
31+
call check(error, all(input == expected))
32+
if (allocated(error)) return
33+
34+
end subroutine test_loadtxt_qp_
35+
36+
end module test_loadtxt_qp
37+
38+
39+
program tester
40+
use, intrinsic :: iso_fortran_env, only : error_unit
41+
use stdlib_test, only : run_testsuite, new_testsuite, testsuite_type
42+
use test_loadtxt_qp, only : collect_loadtxt_qp
43+
implicit none
44+
integer :: stat, is
45+
type(testsuite_type), allocatable :: testsuites(:)
46+
character(len=*), parameter :: fmt = '("#", *(1x, a))'
47+
48+
stat = 0
1249

13-
subroutine print_array(a)
14-
class(*),intent(in) :: a(:, :)
15-
integer :: i
16-
print *, "Array, shape=(", size(a, 1), ",", size(a, 2), ")"
50+
testsuites = [ &
51+
new_testsuite("loadtxt_qp", collect_loadtxt_qp) &
52+
]
1753

18-
select type(a)
19-
type is(real(qp))
20-
do i = 1, size(a, 1)
21-
print *, a(i, :)
22-
end do
23-
class default
24-
write(*,'(a)')'The proposed type is not supported'
25-
error stop
26-
end select
54+
do is = 1, size(testsuites)
55+
write(error_unit, fmt) "Testing:", testsuites(is)%name
56+
call run_testsuite(testsuites(is)%collect, error_unit, stat)
57+
end do
2758

28-
end subroutine
59+
if (stat > 0) then
60+
write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!"
61+
error stop
62+
end if
2963

30-
end program
64+
end program tester

0 commit comments

Comments
 (0)