-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmifu_asserts.f90
104 lines (88 loc) · 2.81 KB
/
mifu_asserts.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
!> Minimal module for unit-testing in fortran.
!!
module mifu_asserts
! No implicit types allowed.
implicit none
! Keep private what we can.
private
public :: assert_equal_real8
public :: mifu_running_name
public :: print_error_log
integer, parameter :: line_len = 79
integer :: reg_p = 1
integer :: failed = 0
integer :: passed = 0
character(len=line_len), dimension(1024) :: error_log
character(len=line_len) :: mifu_running_name
! Module content below.
contains
!> Prints error summary and details to screen.
subroutine print_error_log()
implicit none
integer :: i
if (reg_p == 1) then
write (*,*)
write (*,*)
write (*,*) "ALL ", passed, " ASSERTS PASSED"
write (*,*)
else
write (*,*)
write (*,*)
write (*,*) "!!!FAILURES!!!"
write (*,*)
write (*,*)
write (*,*) "Test results:"
write (*,*) "Run: ", failed + passed
write (*,*) "Failures: ", failed
write (*,*)
do i=1,reg_p
write(*,*) error_log(i)
end do
end if
end subroutine print_error_log
!> Asserts the equality of two real*8 numbers within a specified limit.
!! This subroutine is supposed to be called through the
!! MIFU_ASSERT_REAL_EQUAL( actual, expected, eps ) macro.
!! @param[in] a : actual value
!! @param[in] b : expected value
!! @param[in] eps : allowed difference
!! @param[in] file : the name of the file where assertion was made
!! @param[in] line : the line in the file where assertion was made
!!
subroutine assert_equal_real8(a, b, eps, file, line)
implicit none
real*8, intent(in) :: a
real*8, intent(in) :: b
real*4, intent(in) :: eps
character*(*), intent(in) :: file
integer, intent(in) :: line
! ------------------------
! Local variables.
real*8 :: diff
! Function body.
diff = abs(a-b)
if (eps < diff) then
failed = failed + 1
write(error_log(reg_p),*) "----------------------------------------------------------------------------"
reg_p = reg_p + 1
write(error_log(reg_p),*) "FAILURE ", failed, ") ", "test: ", trim(mifu_running_name)
reg_p = reg_p + 1
write(error_log(reg_p),*) "file:", file
reg_p = reg_p + 1
write(error_log(reg_p),*) "line:", line
reg_p = reg_p + 1
write(error_log(reg_p),*) "real*8 equality assertion faild within limit: ", eps
reg_p = reg_p + 1
write(error_log(reg_p),*) "Expected ", a
reg_p = reg_p + 1
write(error_log(reg_p),*) "Actual ", b
reg_p = reg_p + 1
write(error_log(reg_p),*) "Diff ", diff
reg_p = reg_p + 1
write(error_log(reg_p),*)
reg_p = reg_p + 1
else
passed = passed + 1
end if
end subroutine assert_equal_real8
end module mifu_asserts