forked from fortran-lang/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_optval.fypp
499 lines (375 loc) · 15.4 KB
/
test_optval.fypp
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
#:include "common.fypp"
module test_optval
use, intrinsic :: iso_fortran_env, only: &
sp => real32, dp => real64, qp => real128, &
int8, int16, int32, int64
use testdrive, only : new_unittest, unittest_type, error_type, check, skip_test
use stdlib_optval, only: optval
implicit none
contains
!> Collect all exported unit tests
subroutine collect_optval(testsuite)
!> Collection of tests
type(unittest_type), allocatable, intent(out) :: testsuite(:)
testsuite = [ &
new_unittest("rsp", test_optval_rsp), &
new_unittest("rdp", test_optval_rdp), &
new_unittest("rqp", test_optval_rqp), &
new_unittest("csp", test_optval_csp), &
new_unittest("cdp", test_optval_cdp), &
new_unittest("cqp", test_optval_cqp), &
new_unittest("iint8", test_optval_iint8), &
new_unittest("iint16", test_optval_iint16), &
new_unittest("iint32", test_optval_iint32), &
new_unittest("iint64", test_optval_iint64), &
new_unittest("logical", test_optval_logical), &
new_unittest("character", test_optval_character), &
new_unittest("rsp_arr", test_optval_rsp_arr), &
new_unittest("rdp_arr", test_optval_rdp_arr), &
new_unittest("rqp_arr", test_optval_rqp_arr), &
new_unittest("csp_arr", test_optval_csp_arr), &
new_unittest("cdp_arr", test_optval_cdp_arr), &
new_unittest("cqp_arr", test_optval_cqp_arr), &
new_unittest("iint8_arr", test_optval_iint8_arr), &
new_unittest("iint16_arr", test_optval_iint16_arr), &
new_unittest("iint32_arr", test_optval_iint32_arr), &
new_unittest("iint64_arr", test_optval_iint64_arr) &
]
end subroutine collect_optval
subroutine test_optval_rsp(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, foo_sp(1.0_sp) == 1.0_sp)
if (allocated(error)) return
call check(error, foo_sp() == 2.0_sp)
end subroutine test_optval_rsp
function foo_sp(x) result(z)
real(sp), intent(in), optional :: x
real(sp) :: z
z = optval(x, 2.0_sp)
endfunction foo_sp
subroutine test_optval_rdp(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, foo_dp(1.0_dp) == 1.0_dp)
if (allocated(error)) return
call check(error, foo_dp() == 2.0_dp)
end subroutine test_optval_rdp
function foo_dp(x) result(z)
real(dp), intent(in), optional :: x
real(dp) :: z
z = optval(x, 2.0_dp)
endfunction foo_dp
subroutine test_optval_rqp(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
#:if WITH_QP
call check(error, foo_qp(1.0_qp) == 1.0_qp)
if (allocated(error)) return
call check(error, foo_qp() == 2.0_qp)
#:else
call skip_test(error, "Quadruple precision is not enabled")
#:endif
end subroutine test_optval_rqp
#:if WITH_QP
function foo_qp(x) result(z)
real(qp), intent(in), optional :: x
real(qp) :: z
z = optval(x, 2.0_qp)
endfunction foo_qp
#:endif
subroutine test_optval_csp(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
complex(sp) :: z1
z1 = cmplx(1.0_sp, 2.0_sp, kind=sp)
call check(error, foo_csp(z1) == z1)
if (allocated(error)) return
call check(error, foo_csp() == z1)
end subroutine test_optval_csp
function foo_csp(x) result(z)
complex(sp), intent(in), optional :: x
complex(sp) :: z
z = optval(x, cmplx(1.0_sp, 2.0_sp, kind=sp))
endfunction foo_csp
subroutine test_optval_cdp(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
complex(dp) :: z1
z1 = cmplx(1.0_dp, 2.0_dp,kind=dp)
call check(error, foo_cdp(z1) == z1)
if (allocated(error)) return
call check(error, foo_cdp() == z1)
end subroutine test_optval_cdp
function foo_cdp(x) result(z)
complex(dp), intent(in), optional :: x
complex(dp) :: z
z = optval(x, cmplx(1.0_dp, 2.0_dp, kind=dp))
endfunction foo_cdp
subroutine test_optval_cqp(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
#:if WITH_QP
complex(qp) :: z1
z1 = cmplx(1.0_qp, 2.0_qp, kind=qp)
call check(error, foo_cqp(z1) == z1)
if (allocated(error)) return
call check(error, foo_cqp() == z1)
#:else
call skip_test(error, "Quadruple precision is not enabled")
#:endif
end subroutine test_optval_cqp
#:if WITH_QP
function foo_cqp(x) result(z)
complex(qp), intent(in), optional :: x
complex(qp) :: z
z = optval(x, cmplx(1.0_qp, 2.0_qp, kind=qp))
endfunction foo_cqp
#:endif
subroutine test_optval_iint8(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, foo_int8(1_int8) == 1_int8)
if (allocated(error)) return
call check(error, foo_int8() == 2_int8)
end subroutine test_optval_iint8
function foo_int8(x) result(z)
integer(int8), intent(in), optional :: x
integer(int8) :: z
z = optval(x, 2_int8)
endfunction foo_int8
subroutine test_optval_iint16(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, foo_int16(1_int16) == 1_int16)
if (allocated(error)) return
call check(error, foo_int16() == 2_int16)
end subroutine test_optval_iint16
function foo_int16(x) result(z)
integer(int16), intent(in), optional :: x
integer(int16) :: z
z = optval(x, 2_int16)
endfunction foo_int16
subroutine test_optval_iint32(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, foo_int32(1_int32) == 1_int32)
if (allocated(error)) return
call check(error, foo_int32() == 2_int32)
end subroutine test_optval_iint32
function foo_int32(x) result(z)
integer(int32), intent(in), optional :: x
integer(int32) :: z
z = optval(x, 2_int32)
endfunction foo_int32
subroutine test_optval_iint64(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, foo_int64(1_int64) == 1_int64)
if (allocated(error)) return
call check(error, foo_int64() == 2_int64)
end subroutine test_optval_iint64
function foo_int64(x) result(z)
integer(int64), intent(in), optional :: x
integer(int64) :: z
z = optval(x, 2_int64)
endfunction foo_int64
subroutine test_optval_logical(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, foo_logical(.true.))
if (allocated(error)) return
call check(error, .not.foo_logical())
end subroutine test_optval_logical
function foo_logical(x) result(z)
logical, intent(in), optional :: x
logical :: z
z = optval(x, .false.)
endfunction foo_logical
subroutine test_optval_character(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, foo_character("x") == "x")
if (allocated(error)) return
call check(error, foo_character() == "y")
end subroutine test_optval_character
function foo_character(x) result(z)
character(len=*), intent(in), optional :: x
character(len=:), allocatable :: z
z = optval(x, "y")
endfunction foo_character
subroutine test_optval_rsp_arr(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, all(foo_sp_arr([1.0_sp, -1.0_sp]) == [1.0_sp, -1.0_sp]))
if (allocated(error)) return
call check(error, all(foo_sp_arr() == [2.0_sp, -2.0_sp]))
end subroutine test_optval_rsp_arr
function foo_sp_arr(x) result(z)
real(sp), dimension(2), intent(in), optional :: x
real(sp), dimension(2) :: z
z = optval(x, [2.0_sp, -2.0_sp])
end function foo_sp_arr
subroutine test_optval_rdp_arr(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, all(foo_dp_arr([1.0_dp, -1.0_dp]) == [1.0_dp, -1.0_dp]))
if (allocated(error)) return
call check(error, all(foo_dp_arr() == [2.0_dp, -2.0_dp]))
end subroutine test_optval_rdp_arr
function foo_dp_arr(x) result(z)
real(dp), dimension(2), intent(in), optional :: x
real(dp), dimension(2) :: z
z = optval(x, [2.0_dp, -2.0_dp])
end function foo_dp_arr
subroutine test_optval_rqp_arr(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
#:if WITH_QP
call check(error, all(foo_qp_arr([1.0_qp, -1.0_qp]) == [1.0_qp, -1.0_qp]))
if (allocated(error)) return
call check(error, all(foo_qp_arr() == [2.0_qp, -2.0_qp]))
#:else
call skip_test(error, "Quadruple precision is not enabled")
#:endif
end subroutine test_optval_rqp_arr
#:if WITH_QP
function foo_qp_arr(x) result(z)
real(qp), dimension(2), intent(in), optional :: x
real(qp), dimension(2) :: z
z = optval(x, [2.0_qp, -2.0_qp])
end function foo_qp_arr
#:endif
subroutine test_optval_csp_arr(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
complex(sp), dimension(2) :: z1, z2
z1 = cmplx(1.0_sp, 2.0_sp, kind=sp)*[1.0_sp, -1.0_sp]
z2 = cmplx(2.0_sp, 2.0_sp, kind=sp)*[1.0_sp, -1.0_sp]
call check(error, all(foo_csp_arr(z1) == z1))
if (allocated(error)) return
call check(error, all(foo_csp_arr() == z2))
end subroutine test_optval_csp_arr
function foo_csp_arr(x) result(z)
complex(sp), dimension(2), intent(in), optional :: x
complex(sp), dimension(2) :: z
z = optval(x, cmplx(2.0_sp, 2.0_sp, kind=sp)*[1.0_sp, -1.0_sp])
end function foo_csp_arr
subroutine test_optval_cdp_arr(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
complex(dp), dimension(2) :: z1, z2
z1 = cmplx(1.0_dp, 2.0_dp, kind=dp)*[1.0_dp, -1.0_dp]
z2 = cmplx(2.0_dp, 2.0_dp, kind=dp)*[1.0_dp, -1.0_dp]
call check(error, all(foo_cdp_arr(z1) == z1))
if (allocated(error)) return
call check(error, all(foo_cdp_arr() == z2))
end subroutine test_optval_cdp_arr
function foo_cdp_arr(x) result(z)
complex(dp), dimension(2), intent(in), optional :: x
complex(dp), dimension(2) :: z
z = optval(x, cmplx(2.0_dp, 2.0_dp, kind=dp)*[1.0_dp, -1.0_dp])
end function foo_cdp_arr
subroutine test_optval_cqp_arr(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
#:if WITH_QP
complex(qp), dimension(2) :: z1, z2
z1 = cmplx(1.0_qp, 2.0_qp, kind=qp)*[1.0_qp, -1.0_qp]
z2 = cmplx(2.0_qp, 2.0_qp, kind=qp)*[1.0_qp, -1.0_qp]
call check(error, all(foo_cqp_arr(z1) == z1))
if (allocated(error)) return
call check(error, all(foo_cqp_arr() == z2))
#:else
call skip_test(error, "Quadruple precision is not enabled")
#:endif
end subroutine test_optval_cqp_arr
#:if WITH_QP
function foo_cqp_arr(x) result(z)
complex(qp), dimension(2), intent(in), optional :: x
complex(qp), dimension(2) :: z
z = optval(x, cmplx(2.0_qp, 2.0_qp, kind=qp)*[1.0_qp, -1.0_qp])
end function foo_cqp_arr
#:endif
subroutine test_optval_iint8_arr(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, all(foo_int8_arr([1_int8, -1_int8]) == [1_int8, -1_int8]))
if (allocated(error)) return
call check(error, all(foo_int8_arr() == [2_int8, -2_int8]))
end subroutine test_optval_iint8_arr
function foo_int8_arr(x) result(z)
integer(int8), dimension(2), intent(in), optional :: x
integer(int8), dimension(2) :: z
z = optval(x, [2_int8, -2_int8])
end function foo_int8_arr
subroutine test_optval_iint16_arr(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, all(foo_int16_arr([1_int16, -1_int16]) == [1_int16, -1_int16]))
if (allocated(error)) return
call check(error, all(foo_int16_arr() == [2_int16, -2_int16]))
end subroutine test_optval_iint16_arr
function foo_int16_arr(x) result(z)
integer(int16), dimension(2), intent(in), optional :: x
integer(int16), dimension(2) :: z
z = optval(x, [2_int16, -2_int16])
end function foo_int16_arr
subroutine test_optval_iint32_arr(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, all(foo_int32_arr([1_int32, -1_int32]) == [1_int32, -1_int32]))
if (allocated(error)) return
call check(error, all(foo_int32_arr() == [2_int32, -2_int32]))
end subroutine test_optval_iint32_arr
function foo_int32_arr(x) result(z)
integer(int32), dimension(2), intent(in), optional :: x
integer(int32), dimension(2) :: z
z = optval(x, [2_int32, -2_int32])
end function foo_int32_arr
subroutine test_optval_iint64_arr(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, all(foo_int64_arr([1_int64, -1_int64]) == [1_int64, -1_int64]))
if (allocated(error)) return
call check(error, all(foo_int64_arr() == [2_int64, -2_int64]))
end subroutine test_optval_iint64_arr
function foo_int64_arr(x) result(z)
integer(int64), dimension(2), intent(in), optional :: x
integer(int64), dimension(2) :: z
z = optval(x, [2_int64, -2_int64])
end function foo_int64_arr
subroutine test_optval_logical_arr(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, all(foo_logical_arr()))
if (allocated(error)) return
call check(error, all(.not.foo_logical_arr()))
end subroutine test_optval_logical_arr
function foo_logical_arr(x) result(z)
logical, dimension(2), intent(in), optional :: x
logical, dimension(2) :: z
z = optval(x, [.false., .false.])
end function foo_logical_arr
end module test_optval
program tester
use, intrinsic :: iso_fortran_env, only : error_unit
use testdrive, only : run_testsuite, new_testsuite, testsuite_type
use test_optval, only : collect_optval
implicit none
integer :: stat, is
type(testsuite_type), allocatable :: testsuites(:)
character(len=*), parameter :: fmt = '("#", *(1x, a))'
stat = 0
testsuites = [ &
new_testsuite("optval", collect_optval) &
]
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