|
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 |
6 | 6 |
|
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 |
11 | 10 |
|
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(:) |
14 | 15 |
|
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 | + ] |
17 | 22 |
|
18 |
| -call loadtxt("array1.dat", d) |
19 |
| -call print_array(d) |
| 23 | + end subroutine collect_loadtxt |
20 | 24 |
|
21 |
| -call loadtxt("array2.dat", d) |
22 |
| -call print_array(d) |
23 | 25 |
|
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(:,:) |
26 | 30 |
|
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 |
29 | 36 |
|
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 |
32 | 42 |
|
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 | + ] |
34 | 116 |
|
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) |
56 | 120 | end do
|
57 |
| - class default |
58 |
| - call error_stop('The proposed type is not supported') |
59 |
| - end select |
60 | 121 |
|
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 |
62 | 126 |
|
63 |
| -end program |
| 127 | +end program tester |
0 commit comments