-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathassert_mod.f90
190 lines (125 loc) · 4.87 KB
/
assert_mod.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
! DART software - Copyright UCAR. This open source software is provided
! by UCAR, "as is", without charge, subject to all terms of use at
! http://www.image.ucar.edu/DAReS/DART/DART_download
!> Aim: collection of assertions for use in test code
module assert_mod
implicit none
public
interface assert_equal
module procedure assert_equal_real
module procedure assert_equal_double
module procedure assert_equal_int
module procedure assert_equal_int8
module procedure assert_equal_strings
module procedure assert_equal_int_array
module procedure assert_equal_logical
module procedure assert_equal_logical_array
end interface
interface assert_not_equal
module procedure assert_not_equal_real
module procedure assert_not_equal_int
end interface
interface assert_greater
module procedure assert_greater_int
end interface assert_greater
contains
!-------------------------------
! Assert equal
!-------------------------------
subroutine assert_equal_real(a, b, message)
real, intent(in) :: a, b
character(len=*), intent(in) :: message
if (a /= b) print*, 'FAIL: ', trim(message),' assertion ', a, '==', b, 'failed'
end subroutine assert_equal_real
!-------------------------------
subroutine assert_equal_double(a, b, message)
double precision, intent(in) :: a, b
character(len=*), intent(in) :: message
if (a /= b) print*, 'FAIL: ', trim(message),' assertion ', a, '==', b, 'failed'
end subroutine assert_equal_double
!-------------------------------
subroutine assert_equal_int(a, b, message)
integer, intent(in) :: a, b
character(len=*), intent(in) :: message
if (a /= b) print*, 'FAIL: ', trim(message),' assertion ', a, '==', b, 'failed'
end subroutine assert_equal_int
!-------------------------------
subroutine assert_equal_int8(a, b, message)
integer*8, intent(in) :: a, b
character(len=*), intent(in) :: message
if (a /= b) print*, 'FAIL: ', trim(message),' assertion ', a, '==', b, 'failed'
end subroutine assert_equal_int8
!-------------------------------
subroutine assert_equal_strings(a, b, message)
character(len=*), intent(in) :: a, b
character(len=*), intent(in) :: message
if (trim(a) == "" .and. trim(b) == "") return ! both strings blank
if (trim(a) /= trim(b)) print*, 'FAIL: ', trim(message), &
' assertion "', trim(a), '" == "', trim(b), '" failed'
end subroutine assert_equal_strings
!-------------------------------
subroutine assert_equal_int_array(a, b, message)
integer, dimension(:), intent(in) :: a, b
character(len=*), intent(in) :: message
integer :: i
if (size(a) /= size(b)) print*, 'FAIL: ', trim(message), &
' array assertion failed because of unequal lengths'
if (any(a /= b)) then
print*, 'FAIL: ', trim(message), ' array assertion failed.'
if (size(a) < 100) then
do i = 1,size(a)
write(*,'('' element('',i3,'') '',i3,'' ?==? '',i3)')i, a(i), b(i)
enddo
else
print*, 'arrays too long to concisely specify where/how failed.'
endif
endif
end subroutine assert_equal_int_array
!-------------------------------
subroutine assert_equal_logical(a, b, message)
logical, intent(in) :: a, b
character(len=*), intent(in) :: message
if (a .neqv. b) print*, 'FAIL: ', trim(message), &
' assertion ', a, '==', b, ' failed'
end subroutine assert_equal_logical
!-------------------------------
subroutine assert_equal_logical_array(a, b, message)
logical, dimension(:), intent(in) :: a, b
character(len=*), intent(in) :: message
integer :: i
if (size(a) /= size(b)) print*, 'FAIL: ', trim(message), &
' array assertion failed because of unequal lengths'
if (any(a .neqv. b)) then
print*, 'FAIL: ', trim(message), ' array assertion failed.'
if (size(a) < 100) then
do i = 1,size(a)
write(*,'('' element('',i3,'') '',L,'' ?==? '',L)')i, a(i), b(i)
enddo
else
print*, 'arrays too long to concisely specify where/how failed.'
endif
endif
end subroutine assert_equal_logical_array
!-------------------------------
! Assert greater
!-------------------------------
subroutine assert_greater_int(a, b, message)
integer, intent(in) :: a, b
character(len=*), intent(in) :: message
if (a <= b) print*, 'FAIL: ', trim(message),' assertion ', a, ' >', b, 'failed'
end subroutine assert_greater_int
!-------------------------------
! Assert not equal
!-------------------------------
subroutine assert_not_equal_real(a, b, message)
real, intent(in) :: a, b
character(len=*), intent(in) :: message
if (a == b) print*, 'FAIL: ', trim(message),' assertion ', a, '/=', b, 'failed'
end subroutine assert_not_equal_real
!-------------------------------
subroutine assert_not_equal_int(a, b, message)
integer, intent(in) :: a, b
character(len=*), intent(in) :: message
if (a == b) print*, 'FAIL: ', trim(message),' assertion ', a, '/=', b, 'failed'
end subroutine assert_not_equal_int
end module assert_mod