forked from fortran-lang/test-drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestdrive.F90
1988 lines (1481 loc) · 51 KB
/
testdrive.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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
! This file is part of test-drive.
! SPDX-Identifier: Apache-2.0 OR MIT
!
! Licensed under either of Apache License, Version 2.0 or MIT license
! at your option; you may not use this file except in compliance with
! the License.
!
! Unless required by applicable law or agreed to in writing, software
! distributed under the License is distributed on an "AS IS" BASIS,
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! See the License for the specific language governing permissions and
! limitations under the License.
!# Enable support for quadruple precision
#ifndef WITH_QP
#define WITH_QP 0
#endif
!# Enable support for extended double precision
#ifndef WITH_XDP
#define WITH_XDP 0
#endif
!> Provides a light-weight procedural testing framework for Fortran projects.
!>
!> Testsuites are defined by a [[collect_interface]] returning a set of
!> [[unittest_type]] objects. To create a new test use the [[new_unittest]]
!> constructor, which requires a test identifier and a procedure with a
!> [[test_interface]] compatible signature. The error status is communicated
!> by the allocation status of an [[error_type]].
!>
!> The necessary boilerplate code to setup the test entry point is just
!>
!>```fortran
!>program tester
!> use, intrinsic :: iso_fortran_env, only : error_unit
!> use testdrive, only : run_testsuite, new_testsuite, testsuite_type
!> use test_suite1, only : collect_suite1
!> use test_suite2, only : collect_suite2
!> implicit none
!> integer :: stat, is
!> type(testsuite_type), allocatable :: testsuites(:)
!> character(len=*), parameter :: fmt = '("#", *(1x, a))'
!>
!> stat = 0
!>
!> testsuites = [ &
!> new_testsuite("suite1", collect_suite1), &
!> new_testsuite("suite2", collect_suite2) &
!> ]
!>
!> 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 tester
!>```
!>
!> Every test is defined in a separate module using a ``collect`` function, which
!> is exported and added to the ``testsuites`` array in the test runner.
!> All test have a simple interface with just an allocatable [[error_type]] as
!> output to provide the test results.
!>
!>```fortran
!>module test_suite1
!> use testdrive, only : new_unittest, unittest_type, error_type, check
!> implicit none
!> private
!>
!> public :: collect_suite1
!>
!>contains
!>
!>!> Collect all exported unit tests
!>subroutine collect_suite1(testsuite)
!> !> Collection of tests
!> type(unittest_type), allocatable, intent(out) :: testsuite(:)
!>
!> testsuite = [ &
!> new_unittest("valid", test_valid), &
!> new_unittest("invalid", test_invalid, should_fail=.true.) &
!> ]
!>
!>end subroutine collect_suite1
!>
!>subroutine test_valid(error)
!> type(error_type), allocatable, intent(out) :: error
!> ! ...
!>end subroutine test_valid
!>
!>subroutine test_invalid(error)
!> type(error_type), allocatable, intent(out) :: error
!> ! ...
!>end subroutine test_invalid
!>
!>end module test_suite1
!>```
!>
!> For an example setup checkout the ``test/`` directory in this project.
module testdrive
use, intrinsic :: iso_fortran_env, only : error_unit
implicit none
private
public :: run_testsuite, run_selected, new_unittest, new_testsuite
public :: select_test, select_suite
public :: unittest_type, testsuite_type, error_type
public :: check, test_failed, skip_test
public :: test_interface, collect_interface
public :: get_argument, get_variable, to_string
!> Single precision real numbers
integer, parameter :: sp = selected_real_kind(6)
!> Double precision real numbers
integer, parameter :: dp = selected_real_kind(15)
#if WITH_XDP
!> Extended double precision real numbers
integer, parameter :: xdp = selected_real_kind(18)
#endif
#if WITH_QP
!> Quadruple precision real numbers
integer, parameter :: qp = selected_real_kind(33)
#endif
!> Char length for integers
integer, parameter :: i1 = selected_int_kind(2)
!> Short length for integers
integer, parameter :: i2 = selected_int_kind(4)
!> Length of default integers
integer, parameter :: i4 = selected_int_kind(9)
!> Long length for integers
integer, parameter :: i8 = selected_int_kind(18)
!> Error code for success
integer, parameter :: success = 0
!> Error code for failure
integer, parameter :: fatal = 1
!> Error code for skipped test
integer, parameter :: skipped = 77
!> Error message
type :: error_type
!> Error code
integer :: stat = success
!> Payload of the error
character(len=:), allocatable :: message
contains
!> Escalate uncaught errors
final :: escalate_error
end type error_type
interface check
module procedure :: check_stat
module procedure :: check_logical
module procedure :: check_float_sp
module procedure :: check_float_dp
#if WITH_XDP
module procedure :: check_float_xdp
#endif
#if WITH_QP
module procedure :: check_float_qp
#endif
module procedure :: check_float_exceptional_sp
module procedure :: check_float_exceptional_dp
#if WITH_XDP
module procedure :: check_float_exceptional_xdp
#endif
#if WITH_QP
module procedure :: check_float_exceptional_qp
#endif
module procedure :: check_complex_sp
module procedure :: check_complex_dp
#if WITH_XDP
module procedure :: check_complex_xdp
#endif
#if WITH_QP
module procedure :: check_complex_qp
#endif
module procedure :: check_complex_exceptional_sp
module procedure :: check_complex_exceptional_dp
#if WITH_XDP
module procedure :: check_complex_exceptional_xdp
#endif
#if WITH_QP
module procedure :: check_complex_exceptional_qp
#endif
module procedure :: check_int_i1
module procedure :: check_int_i2
module procedure :: check_int_i4
module procedure :: check_int_i8
module procedure :: check_bool
module procedure :: check_string
end interface check
interface to_string
module procedure :: integer_i1_to_string
module procedure :: integer_i2_to_string
module procedure :: integer_i4_to_string
module procedure :: integer_i8_to_string
module procedure :: real_sp_to_string
module procedure :: real_dp_to_string
#if WITH_XDP
module procedure :: real_xdp_to_string
#endif
#if WITH_QP
module procedure :: real_qp_to_string
#endif
module procedure :: complex_sp_to_string
module procedure :: complex_dp_to_string
#if WITH_XDP
module procedure :: complex_xdp_to_string
#endif
#if WITH_QP
module procedure :: complex_qp_to_string
#endif
end interface to_string
!> Implementation of check for not a number value, in case a compiler does not
!> provide the IEEE intrinsic ``ieee_is_nan`` (currently this is Intel oneAPI on MacOS)
interface is_nan
module procedure :: is_nan_sp
module procedure :: is_nan_dp
#if WITH_XDP
module procedure :: is_nan_xdp
#endif
#if WITH_QP
module procedure :: is_nan_qp
#endif
end interface is_nan
abstract interface
!> Entry point for tests
subroutine test_interface(error)
import :: error_type
!> Error handling
type(error_type), allocatable, intent(out) :: error
end subroutine test_interface
end interface
!> Declaration of a unit test
type :: unittest_type
!> Name of the test
character(len=:), allocatable :: name
!> Entry point of the test
procedure(test_interface), pointer, nopass :: test => null()
!> Whether test is supposed to fail
logical :: should_fail = .false.
end type unittest_type
abstract interface
!> Collect all tests
subroutine collect_interface(testsuite)
import :: unittest_type
!> Collection of tests
type(unittest_type), allocatable, intent(out) :: testsuite(:)
end subroutine collect_interface
end interface
!> Collection of unit tests
type :: testsuite_type
!> Name of the testsuite
character(len=:), allocatable :: name
!> Entry point of the test
procedure(collect_interface), pointer, nopass :: collect => null()
end type testsuite_type
character(len=*), parameter :: fmt = '(1x, *(1x, a))'
contains
!> Driver for testsuite
recursive subroutine run_testsuite(collect, unit, stat, parallel)
!> Collect tests
procedure(collect_interface) :: collect
!> Unit for IO
integer, intent(in) :: unit
!> Number of failed tests
integer, intent(inout) :: stat
!> Run the tests in parallel
logical, intent(in), optional :: parallel
type(unittest_type), allocatable :: testsuite(:)
integer :: it
logical :: parallel_
parallel_ = .true.
if(present(parallel)) parallel_ = parallel
call collect(testsuite)
!$omp parallel do schedule(dynamic) shared(testsuite, unit) reduction(+:stat) &
!$omp if (parallel_)
do it = 1, size(testsuite)
!$omp critical(testdrive_testsuite)
write(unit, '(1x, 3(1x, a), 1x, "(", i0, "/", i0, ")")') &
& "Starting", testsuite(it)%name, "...", it, size(testsuite)
!$omp end critical(testdrive_testsuite)
call run_unittest(testsuite(it), unit, stat)
end do
end subroutine run_testsuite
!> Driver for selective testing
recursive subroutine run_selected(collect, name, unit, stat)
!> Collect tests
procedure(collect_interface) :: collect
!> Name of the selected test
character(len=*), intent(in) :: name
!> Unit for IO
integer, intent(in) :: unit
!> Number of failed tests
integer, intent(inout) :: stat
type(unittest_type), allocatable :: testsuite(:)
integer :: it
call collect(testsuite)
it = select_test(testsuite, name)
if (it > 0 .and. it <= size(testsuite)) then
call run_unittest(testsuite(it), unit, stat)
else
write(unit, fmt) "Available tests:"
do it = 1, size(testsuite)
write(unit, fmt) "-", testsuite(it)%name
end do
stat = -huge(it)
end if
end subroutine run_selected
!> Run a selected unit test
recursive subroutine run_unittest(test, unit, stat)
!> Unit test
type(unittest_type), intent(in) :: test
!> Unit for IO
integer, intent(in) :: unit
!> Number of failed tests
integer, intent(inout) :: stat
type(error_type), allocatable :: error
character(len=:), allocatable :: message
call test%test(error)
if (.not.test_skipped(error)) then
if (allocated(error) .neqv. test%should_fail) stat = stat + 1
end if
call make_output(message, test, error)
!$omp critical(testdrive_testsuite)
write(unit, '(a)') message
!$omp end critical(testdrive_testsuite)
if (allocated(error)) then
call clear_error(error)
end if
end subroutine run_unittest
pure function test_skipped(error) result(is_skipped)
!> Error handling
type(error_type), intent(in), optional :: error
!> Test was skipped
logical :: is_skipped
is_skipped = .false.
if (present(error)) then
is_skipped = error%stat == skipped
end if
end function test_skipped
!> Create output message for test (this procedure is pure and therefore cannot launch tests)
pure subroutine make_output(output, test, error)
!> Output message for display
character(len=:), allocatable, intent(out) :: output
!> Unit test
type(unittest_type), intent(in) :: test
!> Error handling
type(error_type), intent(in), optional :: error
character(len=:), allocatable :: label
character(len=*), parameter :: indent = repeat(" ", 7) // repeat(".", 3) // " "
if (test_skipped(error)) then
output = indent // test%name // " [SKIPPED]" &
& // new_line("a") // " Message: " // error%message
return
end if
if (present(error) .neqv. test%should_fail) then
if (test%should_fail) then
label = " [UNEXPECTED PASS]"
else
label = " [FAILED]"
end if
else
if (test%should_fail) then
label = " [EXPECTED FAIL]"
else
label = " [PASSED]"
end if
end if
output = indent // test%name // label
if (present(error)) then
output = output // new_line("a") // " Message: " // error%message
end if
end subroutine make_output
!> Select a unit test from all available tests
function select_test(tests, name) result(pos)
!> Name identifying the test suite
character(len=*), intent(in) :: name
!> Available unit tests
type(unittest_type) :: tests(:)
!> Selected test suite
integer :: pos
integer :: it
pos = 0
do it = 1, size(tests)
if (name == tests(it)%name) then
pos = it
exit
end if
end do
end function select_test
!> Select a test suite from all available suites
function select_suite(suites, name) result(pos)
!> Name identifying the test suite
character(len=*), intent(in) :: name
!> Available test suites
type(testsuite_type) :: suites(:)
!> Selected test suite
integer :: pos
integer :: it
pos = 0
do it = 1, size(suites)
if (name == suites(it)%name) then
pos = it
exit
end if
end do
end function select_suite
!> Register a new unit test
function new_unittest(name, test, should_fail) result(self)
!> Name of the test
character(len=*), intent(in) :: name
!> Entry point for the test
procedure(test_interface) :: test
!> Whether test is supposed to error or not
logical, intent(in), optional :: should_fail
!> Newly registered test
type(unittest_type) :: self
self%name = name
self%test => test
if (present(should_fail)) self%should_fail = should_fail
end function new_unittest
!> Register a new testsuite
function new_testsuite(name, collect) result(self)
!> Name of the testsuite
character(len=*), intent(in) :: name
!> Entry point to collect tests
procedure(collect_interface) :: collect
!> Newly registered testsuite
type(testsuite_type) :: self
self%name = name
self%collect => collect
end function new_testsuite
subroutine check_stat(error, stat, message, more)
!> Error handling
type(error_type), allocatable, intent(out) :: error
!> Status of operation
integer, intent(in) :: stat
!> A detailed message describing the error
character(len=*), intent(in), optional :: message
!> Another line of error message
character(len=*), intent(in), optional :: more
if (stat /= success) then
if (present(message)) then
call test_failed(error, message, more)
else
call test_failed(error, "Non-zero exit code encountered", more)
end if
end if
end subroutine check_stat
subroutine check_logical(error, expression, message, more)
!> Error handling
type(error_type), allocatable, intent(out) :: error
!> Result of logical operator
logical, intent(in) :: expression
!> A detailed message describing the error
character(len=*), intent(in), optional :: message
!> Another line of error message
character(len=*), intent(in), optional :: more
if (.not.expression) then
if (present(message)) then
call test_failed(error, message, more)
else
call test_failed(error, "Condition not fullfilled", more)
end if
end if
end subroutine check_logical
subroutine check_float_dp(error, actual, expected, message, more, thr, rel)
!> Error handling
type(error_type), allocatable, intent(out) :: error
!> Found floating point value
real(dp), intent(in) :: actual
!> Expected floating point value
real(dp), intent(in) :: expected
!> A detailed message describing the error
character(len=*), intent(in), optional :: message
!> Another line of error message
character(len=*), intent(in), optional :: more
!> Allowed threshold for matching floating point values
real(dp), intent(in), optional :: thr
!> Check for relative errors instead
logical, intent(in), optional :: rel
logical :: relative
real(dp) :: diff, threshold
call check(error, actual, message, more)
if (allocated(error)) return
if (present(thr)) then
threshold = thr
else
threshold = epsilon(expected)
end if
if (present(rel)) then
relative = rel
else
relative = .false.
end if
if (relative) then
diff = abs(actual - expected) / abs(expected)
else
diff = abs(actual - expected)
end if
if (diff > threshold) then
if (present(message)) then
call test_failed(error, message, more)
else
if (relative) then
call test_failed(error, &
"Floating point value missmatch", &
"expected "//to_string(expected)//" but got "//to_string(actual)//" "//&
"(difference: "//to_string(int(diff*100))//"%)", &
more)
else
call test_failed(error, &
"Floating point value missmatch", &
"expected "//to_string(expected)//" but got "//to_string(actual)//" "//&
"(difference: "//to_string(diff)//")", &
more)
end if
end if
end if
end subroutine check_float_dp
subroutine check_float_exceptional_dp(error, actual, message, more)
!> Error handling
type(error_type), allocatable, intent(out) :: error
!> Found floating point value
real(dp), intent(in) :: actual
!> A detailed message describing the error
character(len=*), intent(in), optional :: message
!> Another line of error message
character(len=*), intent(in), optional :: more
if (is_nan(actual)) then
if (present(message)) then
call test_failed(error, message, more)
else
call test_failed(error, "Exceptional value 'not a number' found", more)
end if
end if
end subroutine check_float_exceptional_dp
subroutine check_float_sp(error, actual, expected, message, more, thr, rel)
!> Error handling
type(error_type), allocatable, intent(out) :: error
!> Found floating point value
real(sp), intent(in) :: actual
!> Expected floating point value
real(sp), intent(in) :: expected
!> A detailed message describing the error
character(len=*), intent(in), optional :: message
!> Another line of error message
character(len=*), intent(in), optional :: more
!> Allowed threshold for matching floating point values
real(sp), intent(in), optional :: thr
!> Check for relative errors instead
logical, intent(in), optional :: rel
logical :: relative
real(sp) :: diff, threshold
call check(error, actual, message, more)
if (allocated(error)) return
if (present(thr)) then
threshold = thr
else
threshold = epsilon(expected)
end if
if (present(rel)) then
relative = rel
else
relative = .false.
end if
if (relative) then
diff = abs(actual - expected) / abs(expected)
else
diff = abs(actual - expected)
end if
if (diff > threshold) then
if (present(message)) then
call test_failed(error, message, more)
else
if (relative) then
call test_failed(error, &
"Floating point value missmatch", &
"expected "//to_string(expected)//" but got "//to_string(actual)//" "//&
"(difference: "//to_string(int(diff*100))//"%)", &
more)
else
call test_failed(error, &
"Floating point value missmatch", &
"expected "//to_string(expected)//" but got "//to_string(actual)//" "//&
"(difference: "//to_string(diff)//")", &
more)
end if
end if
end if
end subroutine check_float_sp
subroutine check_float_exceptional_sp(error, actual, message, more)
!> Error handling
type(error_type), allocatable, intent(out) :: error
!> Found floating point value
real(sp), intent(in) :: actual
!> A detailed message describing the error
character(len=*), intent(in), optional :: message
!> Another line of error message
character(len=*), intent(in), optional :: more
if (is_nan(actual)) then
if (present(message)) then
call test_failed(error, message, more)
else
call test_failed(error, "Exceptional value 'not a number' found", more)
end if
end if
end subroutine check_float_exceptional_sp
#if WITH_XDP
subroutine check_float_xdp(error, actual, expected, message, more, thr, rel)
!> Error handling
type(error_type), allocatable, intent(out) :: error
!> Found floating point value
real(xdp), intent(in) :: actual
!> Expected floating point value
real(xdp), intent(in) :: expected
!> A detailed message describing the error
character(len=*), intent(in), optional :: message
!> Another line of error message
character(len=*), intent(in), optional :: more
!> Allowed threshold for matching floating point values
real(xdp), intent(in), optional :: thr
!> Check for relative errors instead
logical, intent(in), optional :: rel
logical :: relative
real(xdp) :: diff, threshold
call check(error, actual, message, more)
if (allocated(error)) return
if (present(thr)) then
threshold = thr
else
threshold = epsilon(expected)
end if
if (present(rel)) then
relative = rel
else
relative = .false.
end if
if (relative) then
diff = abs(actual - expected) / abs(expected)
else
diff = abs(actual - expected)
end if
if (diff > threshold) then
if (present(message)) then
call test_failed(error, message, more)
else
if (relative) then
call test_failed(error, &
"Floating point value missmatch", &
"expected "//to_string(expected)//" but got "//to_string(actual)//" "//&
"(difference: "//to_string(int(diff*100))//"%)", &
more)
else
call test_failed(error, &
"Floating point value missmatch", &
"expected "//to_string(expected)//" but got "//to_string(actual)//" "//&
"(difference: "//to_string(diff)//")", &
more)
end if
end if
end if
end subroutine check_float_xdp
subroutine check_float_exceptional_xdp(error, actual, message, more)
!> Error handling
type(error_type), allocatable, intent(out) :: error
!> Found floating point value
real(xdp), intent(in) :: actual
!> A detailed message describing the error
character(len=*), intent(in), optional :: message
!> Another line of error message
character(len=*), intent(in), optional :: more
if (is_nan(actual)) then
if (present(message)) then
call test_failed(error, message, more)
else
call test_failed(error, "Exceptional value 'not a number' found", more)
end if
end if
end subroutine check_float_exceptional_xdp
#endif
#if WITH_QP
subroutine check_float_qp(error, actual, expected, message, more, thr, rel)
!> Error handling
type(error_type), allocatable, intent(out) :: error
!> Found floating point value
real(qp), intent(in) :: actual
!> Expected floating point value
real(qp), intent(in) :: expected
!> A detailed message describing the error
character(len=*), intent(in), optional :: message
!> Another line of error message
character(len=*), intent(in), optional :: more
!> Allowed threshold for matching floating point values
real(qp), intent(in), optional :: thr
!> Check for relative errors instead
logical, intent(in), optional :: rel
logical :: relative
real(qp) :: diff, threshold
call check(error, actual, message, more)
if (allocated(error)) return
if (present(thr)) then
threshold = thr
else
threshold = epsilon(expected)
end if
if (present(rel)) then
relative = rel
else
relative = .false.
end if
if (relative) then
diff = abs(actual - expected) / abs(expected)
else
diff = abs(actual - expected)
end if
if (diff > threshold) then
if (present(message)) then
call test_failed(error, message, more)
else
if (relative) then
call test_failed(error, &
"Floating point value missmatch", &
"expected "//to_string(expected)//" but got "//to_string(actual)//" "//&
"(difference: "//to_string(int(diff*100))//"%)", &
more)
else
call test_failed(error, &
"Floating point value missmatch", &
"expected "//to_string(expected)//" but got "//to_string(actual)//" "//&
"(difference: "//to_string(diff)//")", &
more)
end if
end if
end if
end subroutine check_float_qp
subroutine check_float_exceptional_qp(error, actual, message, more)
!> Error handling
type(error_type), allocatable, intent(out) :: error
!> Found floating point value
real(qp), intent(in) :: actual
!> A detailed message describing the error
character(len=*), intent(in), optional :: message
!> Another line of error message
character(len=*), intent(in), optional :: more
if (is_nan(actual)) then
if (present(message)) then
call test_failed(error, message, more)
else
call test_failed(error, "Exceptional value 'not a number' found", more)
end if
end if
end subroutine check_float_exceptional_qp
#endif
subroutine check_complex_dp(error, actual, expected, message, more, thr, rel)
!> Error handling
type(error_type), allocatable, intent(out) :: error