FLASH-X
Doxygen Generated Documentation From Interface Source Code
ut_testDriverMod.F90
Go to the documentation of this file.
1!! NOTICE
2!! Copyright 2022 UChicago Argonne, LLC and contributors
3!!
4!! Licensed under the Apache License, Version 2.0 (the "License");
5!! you may not use this file except in compliance with the License.
6!!
7!! Unless required by applicable law or agreed to in writing, software
8!! distributed under the License is distributed on an "AS IS" BASIS,
9!! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10!! See the License for the specific language governing permissions and
11!! limitations under the License.
12!!
35
36#include "constants.h"
37
39 implicit none
40 private
41
42 integer, save :: my_n_tests = 0
43 integer, save :: my_n_failed = 0
44 real, save :: my_t_start = 0.0d0
45 logical, save :: is_testing = .FALSE.
46
47 interface assertEqual
48 procedure :: assertEqualInt
49 procedure :: assertEqualReal
50 end interface assertEqual
51
53 procedure :: assertSetEqual2dIntArray
54 end interface assertSetEqual
55
56 public :: start_test_run
57 public :: finish_test_run
58
59 public :: assertTrue
60 public :: assertFalse
61 public :: assertEqual
62 public :: assertSetEqual
63 public :: assertAlmostEqual
64
65contains
66
67 subroutine start_test_run()
68 use Driver_Interface, ONLY : Driver_abort
69
70 if (is_testing) then
71 call Driver_abort("[start_test_run] Already testing")
72 end if
73
74 is_testing = .TRUE.
75
76 my_t_start = 0.0d0
77 call cpu_time(my_t_start)
78 end subroutine start_test_run
79
80 subroutine finish_test_run
81 use Driver_data, ONLY : dr_globalMe
82 use Driver_Interface, ONLY : Driver_abort
83
84 real :: my_t_end
85 real :: my_walltime
86
87 integer :: file_unit
88 integer :: ut_getFreeFileUnit
89
90 character(4) :: rank_str
91 character(MAX_STRING_LENGTH) :: fileName
92
93 if (.NOT. is_testing) then
94 call Driver_abort("[finish_test_run] Not testing yet")
95 end if
96
97 is_testing = .FALSE.
98
99 call cpu_time(my_t_end)
100 my_walltime = my_t_end - my_t_start
101
102 ! DEV: TODO reduction to collect number of tests/fails/max walltime?
103 if (dr_globalMe == MASTER_PE) then
104 ! Print result to standard out
105 write(*,*)
106 if (my_n_failed == 0) then
107 write(*,*) "SUCCESS - ", &
108 (my_n_tests - my_n_failed), "/", my_n_tests, ' passed'
109 else
110 write(*,*) "FAILURE - ", &
111 (my_n_tests - my_n_failed), "/", my_n_tests, ' passed'
112 end if
113 write(*,*)
114 write(*,*) 'Walltime = ', my_walltime, ' s'
115 write(*,*)
116
117 ! Create log file for automatic testing on server
118 write(rank_str,"(I4.4)") dr_globalMe
119 filename = "unitTest_" // rank_str
120 file_unit = ut_getFreeFileUnit()
121 OPEN(file_unit, file=filename)
122 if (my_n_failed == 0) then
123 write(file_unit,'(A)') 'SUCCESS all results conformed with expected values.'
124 else
125 write(file_unit,'(A)') 'FAILURE'
126 end if
127 CLOSE(file_unit)
128 end if
129 end subroutine finish_test_run
130
131 subroutine assertTrue(a, msg)
132 logical, intent(IN) :: a
133 character(*), intent(IN) :: msg
134
135 character(256) :: buffer = ""
136
137 if (.NOT. a) then
138 write(buffer,'(A)') msg
139 write(*,*) TRIM(ADJUSTL(buffer))
141 end if
143 end subroutine assertTrue
144
145 subroutine assertFalse(a, msg)
146 logical, intent(IN) :: a
147 character(*), intent(IN) :: msg
148
149 character(256) :: buffer = ""
150
151 if (a) then
152 write(buffer,'(A)') msg
153 write(*,*) TRIM(ADJUSTL(buffer))
155 end if
157 end subroutine assertFalse
158
159 subroutine assertEqualInt(a, b, msg)
160 integer, intent(IN) :: a
161 integer, intent(IN) :: b
162 character(*), intent(IN) :: msg
163
164 character(256) :: buffer = ""
165
166 if (a /= b) then
167 write(buffer,'(A,I5,A,I5)') msg, a, " != ", b
168 write(*,*) TRIM(ADJUSTL(buffer))
170 end if
172 end subroutine assertEqualInt
173
174 subroutine assertEqualReal(a, b, msg)
175 real, intent(IN) :: a
176 real, intent(IN) :: b
177 character(*), intent(IN) :: msg
178
179 character(256) :: buffer = ""
180
181 if (a /= b) then
182 write(buffer,'(A,F15.8,A,F15.8)') msg, a, " != ", b
183 write(*,*) TRIM(ADJUSTL(buffer))
185 end if
187 end subroutine assertEqualReal
188
189 subroutine assertAlmostEqual(a, b, prec, msg)
190 real, intent(IN) :: a
191 real, intent(IN) :: b
192 real, intent(IN) :: prec
193 character(*), intent(IN) :: msg
194
195 character(256) :: buffer = ""
196
197 if (ABS(b - a) > prec) then
198 write(buffer,'(A,F15.8,A,F15.8)') msg, a, " != ", b
199 write(*,*) TRIM(ADJUSTL(buffer))
201 end if
203 end subroutine assertAlmostEqual
204
205 subroutine assertSetEqual2dIntArray(A, B, msg)
206 integer, intent(IN) :: A(:, :)
207 integer, intent(IN) :: B(:, :)
208 character(*), intent(IN) :: msg
209
210 logical :: in_set
211 logical :: failed
212 integer :: j, k
213
215
216 ! Confirm A subset of B
217 failed = .FALSE.
218 do j = 1, SIZE(A, 1)
219 in_set = .FALSE.
220 do k = 1, SIZE(B, 1)
221 if (ALL(A(j, :) == B(k, :))) then
222 in_set = .TRUE.
223 exit
224 end if
225 end do
226
227 if (.NOT. in_set) then
228 write(*,*) msg, " - ", A(j, :), " of A not in B"
229 failed = .TRUE.
230 end if
231 end do
232
233 ! Confirm B subset of A
234 do j = 1, SIZE(B, 1)
235 in_set = .FALSE.
236 do k = 1, SIZE(A, 1)
237 if (ALL(B(j, :) == A(k, :))) then
238 in_set = .TRUE.
239 exit
240 end if
241 end do
242
243 if (.NOT. in_set) then
244 write(*,*) msg, " - ", B(j, :), " of B not in A"
245 failed = .TRUE.
246 end if
247 end do
248
249 if (failed) then
251 end if
252 end subroutine assertSetEqual2dIntArray
253
254end module ut_testDriverMod
255
subroutine Driver_abort(errorMessage)
#define MASTER_PE
Definition: constants.h:6
integer, save my_n_failed
subroutine finish_test_run
subroutine assertEqualInt(a, b, msg)
subroutine start_test_run()
subroutine assertEqualReal(a, b, msg)
logical, save is_testing
integer, save my_n_tests
subroutine assertSetEqual2dIntArray(A, B, msg)
subroutine assertTrue(a, msg)
subroutine assertFalse(a, msg)
subroutine assertAlmostEqual(a, b, prec, msg)
integer function ut_getFreeFileUnit()