Skip to content

Commit b84e107

Browse files
committed
Extend unit testing, handle exceptional values
1 parent 625d785 commit b84e107

File tree

7 files changed

+542
-13
lines changed

7 files changed

+542
-13
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# The simple testing framework
22

3+
[![Apache-2.0](https://img.shields.io/github/license/awvwgk/test-drive)](LICENSE)
4+
[![CI](https://github.com/awvwgk/test-drive/workflows/CI/badge.svg)](https://github.com/awvwgk/test-drive/actions)
5+
[![codecov](https://codecov.io/gh/awvwgk/test-drive/branch/main/graph/badge.svg)](https://codecov.io/gh/awvwgk/test-drive)
6+
37
This project offers a lightweight, procedural unit testing framework
48
based on nothing but standard Fortran.
59

src/testdrive.f90

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
!> For an example setup checkout the ``test/`` directory in this project.
9898
module testdrive
9999
use, intrinsic :: iso_fortran_env, only : error_unit
100+
use, intrinsic :: ieee_arithmetic, only : ieee_is_nan
100101
implicit none
101102
private
102103

@@ -155,6 +156,8 @@ module testdrive
155156
module procedure :: check_logical
156157
module procedure :: check_float_sp
157158
module procedure :: check_float_dp
159+
module procedure :: check_exceptional_sp
160+
module procedure :: check_exceptional_dp
158161
module procedure :: check_int_i1
159162
module procedure :: check_int_i2
160163
module procedure :: check_int_i4
@@ -501,6 +504,9 @@ subroutine check_float_dp(error, actual, expected, message, more, thr, rel)
501504
logical :: relative
502505
real(dp) :: diff, threshold
503506

507+
call check(error, actual, message, more)
508+
if (allocated(error)) return
509+
504510
if (present(thr)) then
505511
threshold = thr
506512
else
@@ -542,6 +548,31 @@ subroutine check_float_dp(error, actual, expected, message, more, thr, rel)
542548
end subroutine check_float_dp
543549

544550

551+
subroutine check_exceptional_dp(error, actual, message, more)
552+
553+
!> Error handling
554+
type(error_type), allocatable, intent(out) :: error
555+
556+
!> Found floating point value
557+
real(dp), intent(in) :: actual
558+
559+
!> A detailed message describing the error
560+
character(len=*), intent(in), optional :: message
561+
562+
!> Another line of error message
563+
character(len=*), intent(in), optional :: more
564+
565+
if (ieee_is_nan(actual)) then
566+
if (present(message)) then
567+
call test_failed(error, message, more)
568+
else
569+
call test_failed(error, "Exceptional value 'not a number' found", more)
570+
end if
571+
end if
572+
573+
end subroutine check_exceptional_dp
574+
575+
545576
subroutine check_float_sp(error, actual, expected, message, more, thr, rel)
546577

547578
!> Error handling
@@ -568,6 +599,9 @@ subroutine check_float_sp(error, actual, expected, message, more, thr, rel)
568599
logical :: relative
569600
real(sp) :: diff, threshold
570601

602+
call check(error, actual, message, more)
603+
if (allocated(error)) return
604+
571605
if (present(thr)) then
572606
threshold = thr
573607
else
@@ -609,6 +643,31 @@ subroutine check_float_sp(error, actual, expected, message, more, thr, rel)
609643
end subroutine check_float_sp
610644

611645

646+
subroutine check_exceptional_sp(error, actual, message, more)
647+
648+
!> Error handling
649+
type(error_type), allocatable, intent(out) :: error
650+
651+
!> Found floating point value
652+
real(sp), intent(in) :: actual
653+
654+
!> A detailed message describing the error
655+
character(len=*), intent(in), optional :: message
656+
657+
!> Another line of error message
658+
character(len=*), intent(in), optional :: more
659+
660+
if (ieee_is_nan(actual)) then
661+
if (present(message)) then
662+
call test_failed(error, message, more)
663+
else
664+
call test_failed(error, "Exceptional value 'not a number' found", more)
665+
end if
666+
end if
667+
668+
end subroutine check_exceptional_sp
669+
670+
612671
subroutine check_int_i1(error, actual, expected, message, more)
613672

614673
!> Error handling

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
set(
1818
tests
1919
"check"
20+
"select"
2021
)
2122
set(
2223
test-srcs

test/main.f90

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ program tester
1919
use testdrive, only : run_testsuite, new_testsuite, testsuite_type, &
2020
& select_suite, run_selected, get_argument
2121
use test_check, only : collect_check
22+
use test_select, only : collect_select
2223
implicit none
2324
integer :: stat, is
2425
character(len=:), allocatable :: suite_name, test_name
@@ -28,7 +29,8 @@ program tester
2829
stat = 0
2930

3031
testsuites = [ &
31-
new_testsuite("check", collect_check) &
32+
new_testsuite("check", collect_check), &
33+
new_testsuite("select", collect_select) &
3234
]
3335

3436
call get_argument(1, suite_name)

test/meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
# limitations under the License.
1515

1616
tests = [
17-
'check'
17+
'check',
18+
'select',
1819
]
1920

2021
test_srcs = files(

0 commit comments

Comments
 (0)