|
| 1 | +program test_errors |
| 2 | +use, intrinsic:: iso_fortran_env, only: int64, int32, real32, real64, stderr=>error_unit |
| 3 | +use nc4fortran, only: netcdf_file, NF90_NOERR |
| 4 | + |
| 5 | +implicit none (type, external) |
| 6 | + |
| 7 | +call test_nonexist_old_file() |
| 8 | +print *, 'OK: non-existing old file' |
| 9 | +call test_nonexist_unknown_file() |
| 10 | +print *, 'OK: non-existing unknown file' |
| 11 | +call test_nonnetcdf_file() |
| 12 | +print *, 'OK: non-NetCDF file' |
| 13 | +call test_nonexist_variable() |
| 14 | +print *, 'OK: non-existing variable' |
| 15 | +call test_wrong_type() |
| 16 | +print *, "OK: wrong type read" |
| 17 | +call test_unknown_write() |
| 18 | +print *, 'OK: unknown write' |
| 19 | +call test_unknown_read() |
| 20 | +print *, 'OK: unknown read' |
| 21 | + |
| 22 | +contains |
| 23 | + |
| 24 | + |
| 25 | +subroutine test_nonexist_old_file() |
| 26 | +integer :: ierr |
| 27 | +type(netcdf_file) :: h |
| 28 | + |
| 29 | +call h%initialize('not-exist.nc', ierr, status='old', action='read') |
| 30 | +if (ierr==NF90_NOERR) error stop 'should have had ierr/=0 on non-existing old file' |
| 31 | +end subroutine test_nonexist_old_file |
| 32 | + |
| 33 | + |
| 34 | +subroutine test_nonexist_unknown_file() |
| 35 | +integer :: ierr |
| 36 | +type(netcdf_file) :: h |
| 37 | + |
| 38 | +call h%initialize('not-exist.nc', ierr, status='unknown', action='read') |
| 39 | +if (ierr==NF90_NOERR) error stop 'should have had ierr/=0 on non-existing unknown read file' |
| 40 | +end subroutine test_nonexist_unknown_file |
| 41 | + |
| 42 | + |
| 43 | +subroutine test_nonnetcdf_file() |
| 44 | +integer :: u,ierr |
| 45 | +type(netcdf_file) :: h |
| 46 | +character(*), parameter :: filename = 'bad.nc' |
| 47 | + |
| 48 | +! create or replace zero-length file, could be any size, just not a valid NetCDF file |
| 49 | +open(newunit=u, file=filename, status='replace', action='write') |
| 50 | +close(u) |
| 51 | + |
| 52 | +call h%initialize(filename, ierr, status='old', action='read') |
| 53 | +if (ierr==NF90_NOERR) error stop 'should have had ierr/=0 on invalid NetCDF file' |
| 54 | +end subroutine test_nonnetcdf_file |
| 55 | + |
| 56 | + |
| 57 | +subroutine test_nonexist_variable() |
| 58 | +integer :: u,ierr |
| 59 | +type(netcdf_file) :: h |
| 60 | +character(*), parameter :: filename = 'bad.nc' |
| 61 | + |
| 62 | +call h%initialize(filename, status='replace') |
| 63 | +call h%read('/not-exist', u, ierr) |
| 64 | +if (ierr==NF90_NOERR) error stop 'test_nonexist_variable: should have ierr/=0 on non-exist variable' |
| 65 | +call h%finalize() |
| 66 | +end subroutine test_nonexist_variable |
| 67 | + |
| 68 | + |
| 69 | +subroutine test_wrong_type() |
| 70 | +integer :: u |
| 71 | +type(netcdf_file) :: h |
| 72 | +character(*), parameter :: filename = 'bad.nc' |
| 73 | + |
| 74 | +call h%initialize(filename, status='replace') |
| 75 | +call h%write('real32', 42.) |
| 76 | +call h%finalize() |
| 77 | + |
| 78 | +call h%initialize(filename, status='old', action='read') |
| 79 | +call h%read('real32', u) |
| 80 | +if (u /= 42) error stop 'test_wrong_type: did not coerce real to integer' |
| 81 | +call h%finalize() |
| 82 | + |
| 83 | +end subroutine test_wrong_type |
| 84 | + |
| 85 | + |
| 86 | +subroutine test_unknown_write() |
| 87 | +integer :: ierr |
| 88 | +type(netcdf_file) :: h |
| 89 | +character(*), parameter :: filename = 'bad.nc' |
| 90 | +complex :: x |
| 91 | + |
| 92 | +x = (1, -1) |
| 93 | + |
| 94 | +call h%initialize(filename, ierr, status='replace') |
| 95 | +call h%write('/complex', x, ierr) |
| 96 | +if(ierr==NF90_NOERR) error stop 'test_unknown_write: writing unknown type variable' |
| 97 | +end subroutine test_unknown_write |
| 98 | + |
| 99 | + |
| 100 | +subroutine test_unknown_read() |
| 101 | +integer :: ierr |
| 102 | +type(netcdf_file) :: h |
| 103 | +character(*), parameter :: filename = 'bad.nc' |
| 104 | +complex :: x |
| 105 | + |
| 106 | +x = (1, -1) |
| 107 | + |
| 108 | +call h%initialize(filename, status='unknown', action='readwrite') |
| 109 | +call h%read('/complex', x, ierr) |
| 110 | +if(ierr==NF90_NOERR) error stop 'test_unknown_read: reading unknown type variable' |
| 111 | +end subroutine test_unknown_read |
| 112 | + |
| 113 | +end program |
0 commit comments