File: tests_module.f90

package info (click to toggle)
etsf-io 1.0.4-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,580 kB
  • sloc: f90: 26,717; sh: 466; makefile: 452; ansic: 23
file content (415 lines) | stat: -rw-r--r-- 13,890 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
module tests

  use etsf_io_low_level

  implicit none

  interface tests_init_variable
     module procedure allocate_int_0D
     module procedure allocate_int_1D
     module procedure allocate_int_2D
     module procedure allocate_int_3D
     module procedure allocate_int_4D
     module procedure allocate_int_5D
     module procedure allocate_int_6D
     module procedure allocate_int_7D
     module procedure allocate_dbl_0D
     module procedure allocate_dbl_1D
     module procedure allocate_dbl_2D
     module procedure allocate_dbl_3D
     module procedure allocate_dbl_4D
     module procedure allocate_dbl_5D
     module procedure allocate_dbl_6D
     module procedure allocate_dbl_7D
     module procedure allocate_str_0D
     module procedure allocate_str_1D
  end interface

  interface tests_check_variable
     module procedure check_read_0D
     module procedure check_read_nD
  end interface

  interface tests_check_values
     module procedure check_mem_int_0D
     module procedure check_mem_int_nD
     module procedure check_mem_dbl_0D
     module procedure check_mem_dbl_nD
     module procedure check_mem_str_0D
     module procedure check_mem_str_1D
  end interface
  
contains

  subroutine tests_status(name, lstat, error)
    character(len = *), intent(in)      :: name
    logical, intent(in)                 :: lstat
    type(etsf_io_low_error), intent(in) :: error
    
    if (lstat) then
      write(*, "(A,A,A,A)") "== ", name, repeat(" ", 68 - len(name)), "OK     =="
    else
      write(*, "(A,A,A,A)") "== ", name, repeat(" ", 68 - len(name)), "Failed =="
      call etsf_io_low_error_handle(error)
    end if
  end subroutine tests_status
  
  subroutine check_read_0D(ncid, varname, type, lstat, error_data)
    integer, intent(in) :: ncid
    character(len = *), intent(in) :: varname
    character(len = *), intent(in) :: type
    logical, intent(out) :: lstat
    type(etsf_io_low_error), intent(inout) :: error_data

    character(len = *), parameter :: me = "check_read_0D"

    if (type == "integer") then
       call check_read_nD(ncid, varname, type, (/ 1 /), lstat, error_data)
    else if (type == "real double_precision") then
       call check_read_nD(ncid, varname, type, (/ 1 /), lstat, error_data)
    else
       call etsf_io_low_error_set(error_data, ERROR_MODE_SPEC, ERROR_TYPE_ARG, me, &
            & tgtname = "check_read_0D", errmess = "unknown type")
       lstat = .false.
    end if
  end subroutine check_read_0D

  subroutine check_read_nD(ncid, varname, type, dims, lstat, error_data)
    integer, intent(in) :: ncid
    character(len = *), intent(in) :: varname
    character(len = *), intent(in) :: type
    integer, intent(in) :: dims(:)
    logical, intent(out) :: lstat
    type(etsf_io_low_error), intent(inout) :: error_data

    character(len = *), parameter :: me = "check_read_nD"
    integer, allocatable :: int_array(:)
    double precision, allocatable :: dbl_array(:)
    character(len = 256), allocatable :: strings(:)

    ! Allocate temporary read space
    if (type == "integer") then
       allocate(int_array(product(dims)))
    else if (type == "real double_precision") then
       allocate(dbl_array(product(dims)))
    else if (type == "string") then
       if (size(dims) == 1) then
          allocate(strings(1))
       else
          allocate(strings(dims(2)))
       end if
    else
       call etsf_io_low_error_set(error_data, ERROR_MODE_SPEC, ERROR_TYPE_ARG, me, &
            & tgtname = me, errmess = "unknown type")
       lstat = .false.
    end if

    ! Read
    if (type == "integer") then
       call etsf_io_low_read_var(ncid, varname, int_array, &
            & lstat, error_data = error_data)
    else if (type == "real double_precision") then
       call etsf_io_low_read_var(ncid, varname, dbl_array, &
            & lstat, error_data = error_data)
    else if (type == "string") then
       call etsf_io_low_read_var(ncid, varname, strings, dims(1), &
            & lstat, error_data = error_data)
    end if
    call tests_status(" | read '"// varname //"' values", lstat, error_data)
    if (.not. lstat) return

    ! Check values
    if (type == "integer") then
       call check_mem_int_nD(int_array, varname, lstat, error_data)
       deallocate(int_array)
    else if (type == "real double_precision") then
       call check_mem_dbl_nD(dbl_array, varname, lstat, error_data)
       deallocate(dbl_array)
    else if (type == "string") then
       if (size(dims) == 1) then
          call check_mem_str_0D(strings(1), dims(1), varname, lstat, error_data)
       else
          call check_mem_str_1D(strings, dims, varname, lstat, error_data)
       end if
       deallocate(strings)
    end if
  end subroutine check_read_nD

  !------------------!
  ! The integer case !
  !------------------!
  subroutine allocate_int_0D(array)
    integer, pointer :: array

    allocate(array)
    array = 1
  end subroutine allocate_int_0D
  subroutine allocate_int_1D(array, dims)
    integer, pointer :: array(:)
    integer, intent(in) :: dims(:)

    integer :: i

    allocate(array(dims(1)))
    array = (/ (i, i = 1, product(dims)) /)
  end subroutine allocate_int_1D
  subroutine allocate_int_2D(array, dims)
    integer, pointer :: array(:, :)
    integer, intent(in) :: dims(:)

    integer :: i

    allocate(array(dims(1), dims(2)))
    array = reshape( (/ (i, i = 1, product(dims)) /) , dims(1:2) )
  end subroutine allocate_int_2D
  subroutine allocate_int_3D(array, dims)
    integer, pointer :: array(:, :, :)
    integer, intent(in) :: dims(:)

    integer :: i

    allocate(array(dims(1), dims(2), dims(3)))
    array = reshape( (/ (i, i = 1, product(dims)) /) , dims(1:3) )
  end subroutine allocate_int_3D
  subroutine allocate_int_4D(array, dims)
    integer, pointer :: array(:, :, :, :)
    integer, intent(in) :: dims(:)

    integer :: i

    allocate(array(dims(1), dims(2), dims(3), dims(4)))
    array = reshape( (/ (i, i = 1, product(dims)) /) , dims(1:4) )
  end subroutine allocate_int_4D
  subroutine allocate_int_5D(array, dims)
    integer, pointer :: array(:, :, :, :, :)
    integer, intent(in) :: dims(:)

    integer :: i

    allocate(array(dims(1), dims(2), dims(3), dims(4), dims(5)))
    array = reshape( (/ (i, i = 1, product(dims)) /) , dims(1:5) )
  end subroutine allocate_int_5D
  subroutine allocate_int_6D(array, dims)
    integer, pointer :: array(:, :, :, :, :, :)
    integer, intent(in) :: dims(:)

    integer :: i

    allocate(array(dims(1), dims(2), dims(3), dims(4), dims(5), dims(6)))
    array = reshape( (/ (i, i = 1, product(dims)) /) , dims(1:6) )
  end subroutine allocate_int_6D
  subroutine allocate_int_7D(array, dims)
    integer, pointer :: array(:, :, :, :, :, :, :)
    integer, intent(in) :: dims(:)

    integer :: i

    allocate(array(dims(1), dims(2), dims(3), dims(4), dims(5), dims(6), dims(7)))
    array = reshape( (/ (i, i = 1, product(dims)) /) , dims(1:7) )
  end subroutine allocate_int_7D

  subroutine check_mem_int_0D(value, varname, lstat, error_data)
    integer, intent(in) :: value
    character(len = *), intent(in) :: varname
    logical, intent(out) :: lstat
    type(etsf_io_low_error), intent(inout) :: error_data

    call check_mem_int_nD((/ value /), varname, lstat, error_data)
  end subroutine check_mem_int_0D

  subroutine check_mem_int_nD(array, varname, lstat, error_data)
    integer, intent(in) :: array(:)
    character(len = *), intent(in) :: varname
    logical, intent(out) :: lstat
    type(etsf_io_low_error), intent(inout) :: error_data

    character(len = *), parameter :: me = "check_mem_int_nD"
    integer, allocatable :: read_array(:)
    integer :: i

    ! Check values
    lstat = .true.
    do i = 1, size(array), 1
       lstat = (array(i) == i) .and. lstat
    end do
    if (.not. lstat) then
       call etsf_io_low_error_set(error_data, ERROR_MODE_SPEC, ERROR_TYPE_VAR, me, &
            & tgtname = varname, errmess = "wrong values")
       lstat = .false.
    end if
    call tests_status(" | check '"// varname //"' values", lstat, error_data)
  end subroutine check_mem_int_nD

  !-----------------!
  ! The double case !
  !-----------------!
  subroutine allocate_dbl_0D(array)
    double precision, pointer :: array

    allocate(array)
    array = 1.d0
  end subroutine allocate_dbl_0D
  subroutine allocate_dbl_1D(array, dims)
    double precision, pointer :: array(:)
    integer, intent(in) :: dims(:)

    integer :: i

    allocate(array(dims(1)))
    array = (/ (i, i = 1, product(dims)) /)
  end subroutine allocate_dbl_1D
  subroutine allocate_dbl_2D(array, dims)
    double precision, pointer :: array(:, :)
    integer, intent(in) :: dims(:)

    integer :: i

    allocate(array(dims(1), dims(2)))
    array = reshape( (/ (i, i = 1, product(dims)) /) , dims(1:2) )
  end subroutine allocate_dbl_2D
  subroutine allocate_dbl_3D(array, dims)
    double precision, pointer :: array(:, :, :)
    integer, intent(in) :: dims(:)

    integer :: i

    allocate(array(dims(1), dims(2), dims(3)))
    array = reshape( (/ (i, i = 1, product(dims)) /) , dims(1:3) )
  end subroutine allocate_dbl_3D
  subroutine allocate_dbl_4D(array, dims)
    double precision, pointer :: array(:, :, :, :)
    integer, intent(in) :: dims(:)

    integer :: i

    allocate(array(dims(1), dims(2), dims(3), dims(4)))
    array = reshape( (/ (i, i = 1, product(dims)) /) , dims(1:4) )
  end subroutine allocate_dbl_4D
  subroutine allocate_dbl_5D(array, dims)
    double precision, pointer :: array(:, :, :, :, :)
    integer, intent(in) :: dims(:)

    integer :: i

    allocate(array(dims(1), dims(2), dims(3), dims(4), dims(5)))
    array = reshape( (/ (i, i = 1, product(dims)) /) , dims(1:5) )
  end subroutine allocate_dbl_5D
  subroutine allocate_dbl_6D(array, dims)
    double precision, pointer :: array(:, :, :, :, :, :)
    integer, intent(in) :: dims(:)

    integer :: i

    allocate(array(dims(1), dims(2), dims(3), dims(4), dims(5), dims(6)))
    array = reshape( (/ (i, i = 1, product(dims)) /) , dims(1:6) )
  end subroutine allocate_dbl_6D
  subroutine allocate_dbl_7D(array, dims)
    double precision, pointer :: array(:, :, :, :, :, :, :)
    integer, intent(in) :: dims(:)

    integer :: i

    allocate(array(dims(1), dims(2), dims(3), dims(4), dims(5), dims(6), dims(7)))
    array = reshape( (/ (i, i = 1, product(dims)) /) , dims(1:7) )
  end subroutine allocate_dbl_7D

  subroutine check_mem_dbl_0D(value, varname, lstat, error_data)
    double precision, intent(in) :: value
    character(len = *), intent(in) :: varname
    logical, intent(out) :: lstat
    type(etsf_io_low_error), intent(inout) :: error_data

    call check_mem_dbl_nD((/ value /), varname, lstat, error_data)
  end subroutine check_mem_dbl_0D

  subroutine check_mem_dbl_nD(array, varname, lstat, error_data)
    double precision, intent(in) :: array(:)
    character(len = *), intent(in) :: varname
    logical, intent(out) :: lstat
    type(etsf_io_low_error), intent(inout) :: error_data

    character(len = *), parameter :: me = "check_mem_dbl_nD"
    integer :: i

    ! Check values
    lstat = .true.
    do i = 1, size(array), 1
       lstat = (array(i) == i) .and. lstat
    end do
    if (.not. lstat) then
       call etsf_io_low_error_set(error_data, ERROR_MODE_SPEC, ERROR_TYPE_VAR, me, &
            & tgtname = varname, errmess = "wrong values")
       lstat = .false.
    end if
    call tests_status(" | check '"// varname //"' values", lstat, error_data)
  end subroutine check_mem_dbl_nD

  !-----------------!
  ! The string case !
  !-----------------!
  subroutine allocate_str_0D(string, dims)
    integer, intent(in) :: dims(:)
    character(len = dims(1)), pointer :: string

    allocate(string)
    write(string, "(A)") repeat("a", dims(1))
  end subroutine allocate_str_0D

  subroutine allocate_str_1D(string, dims)
    integer, intent(in) :: dims(:)
    character(len = dims(1)), pointer :: string(:)

    integer :: i
    character(len = 1) :: chr

    allocate(string(dims(2)))
    do i = 1, dims(2), 1
       write(chr, "(A1)") char(97 + modulo(i - 1, 26))
       write(string(i), "(A)") repeat(chr, dims(1))
    end do
  end subroutine allocate_str_1D

  subroutine check_mem_str_0D(string, length, varname, lstat, error_data)
    character(len = length), intent(in) :: string
    integer, intent(in) :: length
    character(len = *), intent(in) :: varname
    logical, intent(out) :: lstat
    type(etsf_io_low_error), intent(inout) :: error_data

    character(len = *), parameter :: me = "check_mem_str"

    ! Check values
    lstat = (string == repeat("a", length))
    if (.not. lstat) then
       call etsf_io_low_error_set(error_data, ERROR_MODE_SPEC, ERROR_TYPE_VAR, me, &
            & tgtname = varname, errmess = "wrong values")
       lstat = .false.
    end if
    call tests_status(" | check '"// varname //"' values", lstat, error_data)
  end subroutine check_mem_str_0D
  subroutine check_mem_str_1D(string, dims, varname, lstat, error_data)
    integer, intent(in) :: dims(:)
    character(len = dims(1)), intent(in) :: string(:)
    character(len = *), intent(in) :: varname
    logical, intent(out) :: lstat
    type(etsf_io_low_error), intent(inout) :: error_data

    character(len = *), parameter :: me = "check_mem_str_1D"
    character(len = 1) :: chr
    integer :: i

    ! Check values
    lstat = .true.
    do i = 1, dims(2), 1
       write(chr, "(A1)") char(97 + modulo(i - 1, 26))
       lstat = lstat .and. (string(i) == repeat(chr, dims(1)))
    end do
    if (.not. lstat) then
       call etsf_io_low_error_set(error_data, ERROR_MODE_SPEC, ERROR_TYPE_VAR, me, &
            & tgtname = varname, errmess = "wrong values")
       lstat = .false.
    end if
    call tests_status(" | check '"// varname //"' values", lstat, error_data)
  end subroutine check_mem_str_1D

end module tests