forked from fortran-lang/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_npy.f90
680 lines (521 loc) · 23.9 KB
/
test_npy.f90
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
module test_npy
use stdlib_kinds, only : int8, int16, int32, int64, sp, dp
use stdlib_io_npy, only : save_npy, load_npy
use testdrive, only : new_unittest, unittest_type, error_type, check
implicit none
private
public :: collect_npy
contains
!> Collect all exported unit tests
subroutine collect_npy(testsuite)
!> Collection of tests
type(unittest_type), allocatable, intent(out) :: testsuite(:)
testsuite = [ &
new_unittest("read-rdp-r2", test_read_rdp_rank2), &
new_unittest("read-rdp-r3", test_read_rdp_rank3), &
new_unittest("read-rsp-r1", test_read_rsp_rank1), &
new_unittest("read-rsp-r2", test_read_rsp_rank2), &
new_unittest("write-rdp-r2", test_write_rdp_rank2), &
new_unittest("write-rsp-r2", test_write_rsp_rank2), &
new_unittest("write-i2-r4", test_write_int16_rank4), &
new_unittest("invalid-magic-number", test_invalid_magic_number, should_fail=.true.), &
new_unittest("invalid-magic-string", test_invalid_magic_string, should_fail=.true.), &
new_unittest("invalid-major-version", test_invalid_major_version, should_fail=.true.), &
new_unittest("invalid-minor-version", test_invalid_minor_version, should_fail=.true.), &
new_unittest("invalid-header-len", test_invalid_header_len, should_fail=.true.), &
new_unittest("invalid-nul-byte", test_invalid_nul_byte, should_fail=.true.), &
new_unittest("invalid-key", test_invalid_key, should_fail=.true.), &
new_unittest("invalid-comma", test_invalid_comma, should_fail=.true.), &
new_unittest("invalid-string", test_invalid_string, should_fail=.true.), &
new_unittest("duplicate-descr", test_duplicate_descr, should_fail=.true.), &
new_unittest("missing-descr", test_missing_descr, should_fail=.true.), &
new_unittest("missing-fortran_order", test_missing_fortran_order, should_fail=.true.), &
new_unittest("missing-shape", test_missing_shape, should_fail=.true.), &
new_unittest("iomsg-deallocated", test_iomsg_deallocated) &
]
end subroutine collect_npy
subroutine test_read_rdp_rank2(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'descr': '<f8', 'fortran_order': True, 'shape': (10, 4, ), } " // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(1) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=*), parameter :: filename = ".test-rdp-r2.npy"
real(dp), allocatable :: input(:, :), output(:, :)
allocate(input(10, 4))
call random_number(input)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) input
close(io)
call load_npy(filename, output, stat)
call delete_file(filename)
call check(error, stat, "Reading of npy file failed")
if (allocated(error)) return
call check(error, size(output), size(input))
if (allocated(error)) return
call check(error, any(abs(output - input) <= epsilon(1.0_dp)), &
"Precision loss when rereading array")
end subroutine test_read_rdp_rank2
subroutine test_read_rsp_rank2(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'descr': '<f4', 'fortran_order': False, 'shape': (12, 5, ), } " // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(1) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=*), parameter :: filename = ".test-rsp-r2.npy"
real(sp), allocatable :: input(:, :), output(:, :)
allocate(input(5, 12))
call random_number(input)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) input
close(io)
call load_npy(filename, output, stat)
call delete_file(filename)
call check(error, stat, "Reading of npy file failed")
if (allocated(error)) return
call check(error, size(output), size(input))
if (allocated(error)) return
call check(error, any(abs(output - input) <= epsilon(1.0_dp)), &
"Precision loss when rereading array")
end subroutine test_read_rsp_rank2
subroutine test_read_rdp_rank3(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'descr': '<f8', 'fortran_order': True, 'shape': (10, 2, 2), } " // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(1) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=*), parameter :: filename = ".test-rdp-r3.npy"
real(dp), allocatable :: input(:, :, :), output(:, :, :)
allocate(input(10, 2, 2))
call random_number(input)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) input
close(io)
call load_npy(filename, output, stat)
call delete_file(filename)
call check(error, stat, "Reading of npy file failed")
if (allocated(error)) return
call check(error, size(output), size(input))
if (allocated(error)) return
call check(error, any(abs(output - input) <= epsilon(1.0_dp)), &
"Precision loss when rereading array")
end subroutine test_read_rdp_rank3
subroutine test_read_rsp_rank1(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'descr':'<f4', 'shape' : (37 , ) , 'fortran_order': False} " // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(1) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=*), parameter :: filename = ".test-rsp-r1.npy"
real(sp), allocatable :: input(:), output(:)
allocate(input(37))
call random_number(input)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) input
close(io)
call load_npy(filename, output, stat)
call delete_file(filename)
call check(error, stat, "Reading of npy file failed")
if (allocated(error)) return
call check(error, size(output), 37)
if (allocated(error)) return
call check(error, any(abs(output - input) <= epsilon(1.0_dp)), &
"Precision loss when rereading array")
end subroutine test_read_rsp_rank1
subroutine test_write_rdp_rank2(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
integer :: stat
character(len=*), parameter :: filename = ".test-rdp-r2-rt.npy"
real(dp), allocatable :: input(:, :), output(:, :)
allocate(input(10, 4))
call random_number(input)
call save_npy(filename, input, stat)
call check(error, stat, "Writing of npy file failed")
if (allocated(error)) return
call load_npy(filename, output, stat)
call delete_file(filename)
call check(error, stat, "Reading of npy file failed")
if (allocated(error)) return
call check(error, size(output), size(input))
if (allocated(error)) return
call check(error, any(abs(output - input) <= epsilon(1.0_dp)), &
"Precision loss when rereading array")
end subroutine test_write_rdp_rank2
subroutine test_write_rsp_rank2(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
integer :: stat
character(len=*), parameter :: filename = ".test-rsp-r2-rt.npy"
real(sp), allocatable :: input(:, :), output(:, :)
allocate(input(12, 5))
call random_number(input)
call save_npy(filename, input, stat)
call check(error, stat, "Writing of npy file failed")
if (allocated(error)) return
call load_npy(filename, output, stat)
call delete_file(filename)
call check(error, stat, "Reading of npy file failed")
if (allocated(error)) return
call check(error, size(output), size(input))
if (allocated(error)) return
call check(error, any(abs(output - input) <= epsilon(1.0_dp)), &
"Precision loss when rereading array")
end subroutine test_write_rsp_rank2
subroutine test_write_int16_rank4(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
integer :: stat, i
character(len=*), parameter :: filename = ".test-i2-r4-rt.npy"
integer(int16), allocatable :: input(:, :, :, :), output(:, :, :, :)
input = reshape([(i*(i+1)/2, i = 1, 40)], [2, 5, 2, 2])
call save_npy(filename, input, stat)
call check(error, stat, "Writing of npy file failed")
if (allocated(error)) return
call load_npy(filename, output, stat)
call delete_file(filename)
call check(error, stat, "Reading of npy file failed")
if (allocated(error)) return
call check(error, size(output), size(input))
if (allocated(error)) return
call check(error, all(abs(output - input) == 0), &
"Precision loss when rereading array")
end subroutine test_write_int16_rank4
subroutine test_invalid_magic_number(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'descr': '<f8', 'fortran_order': True, 'shape': (10, 4, ), } " // &
char(10)
character(len=*), parameter :: header = &
char(50) // "NUMPY" // char(1) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=:), allocatable :: msg
character(len=*), parameter :: filename = ".test-invalid-magic-num.npy"
real(dp), allocatable :: array(:, :)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) spread(0.0_dp, 1, 40)
close(io)
call load_npy(filename, array, stat, msg)
call delete_file(filename)
call check(error, stat, msg)
end subroutine test_invalid_magic_number
subroutine test_invalid_magic_string(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'descr': '<f8', 'fortran_order': True, 'shape': (10, 4, ), } " // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "numpy" // char(1) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=:), allocatable :: msg
character(len=*), parameter :: filename = ".test-invalid-magic-str.npy"
real(dp), allocatable :: array(:, :)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) spread(0.0_dp, 1, 40)
close(io)
call load_npy(filename, array, stat, msg)
call delete_file(filename)
call check(error, stat, msg)
end subroutine test_invalid_magic_string
subroutine test_invalid_major_version(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'descr': '<f8', 'fortran_order': True, 'shape': (10, 4, ), } " // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(0) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=:), allocatable :: msg
character(len=*), parameter :: filename = ".test-invalid-major-version.npy"
real(dp), allocatable :: array(:, :)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) spread(0.0_dp, 1, 40)
close(io)
call load_npy(filename, array, stat, msg)
call delete_file(filename)
call check(error, stat, msg)
end subroutine test_invalid_major_version
subroutine test_invalid_minor_version(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'descr': '<f8', 'fortran_order': True, 'shape': (10, 4, ), } " // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(1) // char(9) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=:), allocatable :: msg
character(len=*), parameter :: filename = ".test-invalid-minor-version.npy"
real(dp), allocatable :: array(:, :)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) spread(0.0_dp, 1, 40)
close(io)
call load_npy(filename, array, stat, msg)
call delete_file(filename)
call check(error, stat, msg)
end subroutine test_invalid_minor_version
subroutine test_invalid_header_len(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'descr': '<f8', 'fortran_order': True, 'shape': (10, 4, ), } " // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(1) // char(0) // &
char(len(dict)-1) // char(0) // dict
integer :: io, stat
character(len=:), allocatable :: msg
character(len=*), parameter :: filename = ".test-invalid-header-len.npy"
real(dp), allocatable :: array(:, :)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) spread(0.0_dp, 1, 40)
close(io)
call load_npy(filename, array, stat, msg)
call delete_file(filename)
call check(error, stat, msg)
end subroutine test_invalid_header_len
subroutine test_invalid_nul_byte(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'descr': '<f8', 'fortran_order': True, 'shape': (10, 4, ), } " // &
char(0) // char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(1) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=:), allocatable :: msg
character(len=*), parameter :: filename = ".test-invalid-nul-byte.npy"
real(dp), allocatable :: array(:, :)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) spread(0.0_dp, 1, 40)
close(io)
call load_npy(filename, array, stat, msg)
call delete_file(filename)
call check(error, stat, msg)
end subroutine test_invalid_nul_byte
subroutine test_invalid_key(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'fortran_order': True, 'shape': (10, 4, ), 'descr': '<f8', 'x': 1, }" // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(1) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=:), allocatable :: msg
character(len=*), parameter :: filename = ".test-invalid-key.npy"
real(dp), allocatable :: array(:, :)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) spread(0.0_dp, 1, 40)
close(io)
call load_npy(filename, array, stat, msg)
call delete_file(filename)
call check(error, stat, msg)
end subroutine test_invalid_key
subroutine test_invalid_comma(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'fortran_order': True,, 'shape': (10, 4, ), 'descr': '<f8', } " // &
" " // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(1) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=:), allocatable :: msg
character(len=*), parameter :: filename = ".test-invalid-comma.npy"
real(dp), allocatable :: array(:)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) spread(0.0_dp, 1, 40)
close(io)
call load_npy(filename, array, stat, msg)
call delete_file(filename)
call check(error, stat, msg)
end subroutine test_invalid_comma
subroutine test_invalid_string(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'fortran_order': True, 'shape': (10, 4, ), 'descr': '<f8' '<f4', } " // &
" " // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(1) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=:), allocatable :: msg
character(len=*), parameter :: filename = ".test-invalid-string.npy"
real(dp), allocatable :: array(:)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) spread(0.0_dp, 1, 40)
close(io)
call load_npy(filename, array, stat, msg)
call delete_file(filename)
call check(error, stat, msg)
end subroutine test_invalid_string
subroutine test_duplicate_descr(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'descr': '<f8', 'descr': '<f8', 'fortran_order': True, 'shape': (40, ), }"//&
" " // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(1) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=:), allocatable :: msg
character(len=*), parameter :: filename = ".test-invalid-descr.npy"
real(dp), allocatable :: array(:)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) spread(0.0_dp, 1, 40)
close(io)
call load_npy(filename, array, stat, msg)
call delete_file(filename)
call check(error, stat, msg)
end subroutine test_duplicate_descr
subroutine test_missing_descr(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'fortran_order': True, 'shape': (10, 4, ), } " // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(1) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=:), allocatable :: msg
character(len=*), parameter :: filename = ".test-missing-descr.npy"
real(dp), allocatable :: array(:, :)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) spread(0.0_dp, 1, 40)
close(io)
call load_npy(filename, array, stat, msg)
call delete_file(filename)
call check(error, stat, msg)
end subroutine test_missing_descr
subroutine test_missing_fortran_order(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'descr': '<f8', 'shape': (10, 4, ), } " // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(1) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=:), allocatable :: msg
character(len=*), parameter :: filename = ".test-missing-fortran_order.npy"
real(dp), allocatable :: array(:, :)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) spread(0.0_dp, 1, 40)
close(io)
call load_npy(filename, array, stat, msg)
call delete_file(filename)
call check(error, stat, msg)
end subroutine test_missing_fortran_order
subroutine test_missing_shape(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=*), parameter :: dict = &
"{'fortran_order': True, 'descr': '<f8'} " // &
char(10)
character(len=*), parameter :: header = &
char(int(z"93")) // "NUMPY" // char(1) // char(0) // &
char(len(dict)) // char(0) // dict
integer :: io, stat
character(len=:), allocatable :: msg
character(len=*), parameter :: filename = ".test-missing-shape.npy"
real(dp), allocatable :: array(:, :)
open(newunit=io, file=filename, form="unformatted", access="stream")
write(io) header
write(io) spread(0.0_dp, 1, 40)
close(io)
call load_npy(filename, array, stat, msg)
call delete_file(filename)
call check(error, stat, msg)
end subroutine test_missing_shape
subroutine test_iomsg_deallocated(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
integer :: stat
character(len=:), allocatable :: msg
character(len=*), parameter :: filename = ".test-iomsg-deallocated.npy"
real(sp), allocatable :: input(:, :), output(:, :)
msg = "This message should be deallocated."
allocate(input(12, 5))
call random_number(input)
call save_npy(filename, input, stat, msg)
call delete_file(filename)
call check(error,.not. allocated(msg), "Message wrongly allocated.")
end subroutine
subroutine delete_file(filename)
character(len=*), intent(in) :: filename
integer :: io
open(newunit=io, file=filename)
close(io, status="delete")
end subroutine delete_file
end module test_npy
program tester
use, intrinsic :: iso_fortran_env, only : error_unit
use testdrive, only : run_testsuite, new_testsuite, testsuite_type
use test_npy, only : collect_npy
implicit none
integer :: stat, is
type(testsuite_type), allocatable :: testsuites(:)
character(len=*), parameter :: fmt = '("#", *(1x, a))'
stat = 0
testsuites = [ &
new_testsuite("npy", collect_npy) &
]
do is = 1, size(testsuites)
write(error_unit, fmt) "Testing:", testsuites(is)%name
call run_testsuite(testsuites(is)%collect, error_unit, stat)
end do
if (stat > 0) then
write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!"
error stop
end if
end program