Skip to content

Commit 3da684e

Browse files
committed
Add unit tests to the test core for mpas_adjust_alarm_to_reference_time
This commit adds unit tests for the mpas_adjust_alarm_to_reference_time routine to the test_core_timekeeping_tests module in the test core. Implied in these tests is the requirement that, after a call to mpas_adjust_alarm_to_reference_time, the prevRingTime of the alarm is set to a value that ensures that an immediately subsequent call to mpas_is_alarm_ringing will show that the alarm is ringing. In order to get more verbose printout, including the internal state of alarms, the MPAS_ADJUST_ALARM_VERBOSE macro can be modfied near the top of the test_core_timekeeping_tests module so that its argument is not turned into a comment.
1 parent 1d6ec7c commit 3da684e

File tree

2 files changed

+297
-1
lines changed

2 files changed

+297
-1
lines changed

src/core_test/mpas_test_core.F

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ function test_core_run(domain) result(iErr)!{{{
180180

181181
call mpas_stream_mgr_write(domain % streamManager, forceWriteNow=.true.)
182182

183+
!
184+
! Test functionality of adjustments to alarm reference time
185+
!
186+
call mpas_log_write('')
187+
call mpas_log_write('Testing mpas_adjust_alarm_to_reference_time:')
188+
call mpas_adjust_alarm_tests(domain, iErr)
189+
if (iErr == 0) then
190+
call mpas_log_write('* mpas_adjust_alarm_tests tests - all tests passed: SUCCESS')
191+
else
192+
call mpas_log_write('* mpas_adjust_alarm_tests tests - $i failed tests: FAILURE', intArgs=[iErr])
193+
end if
194+
call mpas_log_write('')
195+
183196
deallocate(threadErrs)
184197

185198
end function test_core_run!}}}

src/core_test/mpas_test_core_timekeeping_tests.F

Lines changed: 284 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
! Additional copyright and license information can be found in the LICENSE file
66
! distributed with this code, or at http://mpas-dev.github.com/license.html
77
!
8+
9+
#define MPAS_ADJUST_ALARM_VERBOSE( M ) ! M
10+
811
module test_core_timekeeping_tests
912

1013
use mpas_derived_types
@@ -19,7 +22,8 @@ module test_core_timekeeping_tests
1922
implicit none
2023
private
2124

22-
public :: test_core_test_intervals
25+
public :: test_core_test_intervals, &
26+
mpas_adjust_alarm_tests
2327

2428
contains
2529

@@ -178,4 +182,283 @@ subroutine test_core_interval_test(ref_str, int1_str, int2_str, expected_divs, e
178182

179183
end subroutine test_core_interval_test!}}}
180184

185+
!***********************************************************************
186+
!
187+
! routine mpas_adjust_alarm_tests
188+
!
189+
!> \brief Tests functionality of mpas_adjust_alarm_to_reference_time
190+
!> \author Michael Duda
191+
!> \date 25 Feb 2025
192+
!> \details
193+
!> This routine tests the functionality of the
194+
!> mpas_adjust_alarm_to_reference_time routine for combinations of the
195+
!> following possibilities:
196+
!>
197+
!> - The current time is aligned with the new alarm time grid
198+
!> - The current time is not aligned with the new alarm time grid
199+
!>
200+
!> - The reference time is before the current time on the clock
201+
!> - The reference time is the same as the current time on the clock
202+
!> - The reference time is after the current time on the clock
203+
!>
204+
!> - The clock is running forwards
205+
!> - The clock is running backwards
206+
!>
207+
!> Upon return, the ierr arugment is set to the number of failed tests.
208+
!
209+
!-----------------------------------------------------------------------
210+
subroutine mpas_adjust_alarm_tests(domain, ierr)
211+
212+
use mpas_derived_types, only : domain_type, MPAS_Clock_type, MPAS_Time_type, MPAS_TimeInterval_type
213+
use mpas_kind_types, only : StrKIND
214+
use mpas_log, only : mpas_log_write
215+
use mpas_timekeeping, only : mpas_set_time, mpas_set_timeInterval, mpas_create_clock, &
216+
mpas_add_clock_alarm, mpas_is_alarm_ringing, mpas_reset_clock_alarm
217+
218+
implicit none
219+
220+
type (domain_type), intent(inout) :: domain
221+
integer, intent(out) :: ierr
222+
223+
integer :: istep
224+
integer :: ierr_local
225+
character(len=StrKIND) :: test_mesg
226+
type (MPAS_Clock_type) :: test_clock
227+
type (MPAS_Time_type) :: test_startTime
228+
type (MPAS_Time_type) :: test_stopTime
229+
type (MPAS_Time_type) :: test_currTime
230+
type (MPAS_Time_type) :: test_alarmTime
231+
type (MPAS_Time_type) :: test_refTime
232+
type (MPAS_TimeInterval_type) :: test_timeStep
233+
type (MPAS_TimeInterval_type) :: test_alarmTimeInterval
234+
MPAS_ADJUST_ALARM_VERBOSE( character(len=StrKIND) :: timeStamp )
235+
236+
ierr = 0
237+
238+
!
239+
! Create a clock with an initial time of 2000-01-01_00 and with a 1-hour 'tick' length
240+
! (The stopping time is set to 2100-01-01_00.)
241+
!
242+
call mpas_set_time(test_startTime, YYYY=2000, MM=01, DD=01, H=0, M=0, S=0, S_n=0, S_d=0, ierr=ierr_local)
243+
call mpas_set_time(test_stopTime, YYYY=2100, MM=01, DD=01, H=0, M=0, S=0, S_n=0, S_d=0, ierr=ierr_local)
244+
call mpas_set_timeInterval(test_timeStep, dt=3600.0_RKIND, ierr=ierr_local)
245+
246+
call mpas_create_clock(test_clock, test_startTime, test_timeStep, test_stopTime, ierr=ierr_local)
247+
248+
!
249+
! Add a recurring alarm to the clock with an initial reference time of 2000-01-01_00 and
250+
! a ringing interval of 1 day.
251+
!
252+
call mpas_set_time(test_alarmTime, YYYY=2000, MM=01, DD=01, H=0, M=0, S=0, S_n=0, S_d=0, ierr=ierr_local)
253+
call mpas_set_timeInterval(test_alarmTimeInterval, dt=86400.0_RKIND, ierr=ierr_local)
254+
255+
call mpas_add_clock_alarm(test_clock, 'foobar', test_alarmTime, test_alarmTimeInterval, ierr_local)
256+
257+
#ifdef MPAS_ADVANCE_TEST_CLOCK
258+
do istep = 1, 24*365
259+
if (mpas_is_alarm_ringing(test_clock, 'foobar', ierr=ierr_local)) then
260+
call mpas_reset_clock_alarm(test_clock, 'foobar', ierr=ierr_local)
261+
test_currTime = mpas_get_clock_time(test_clock, MPAS_NOW, iErr)
262+
call mpas_get_time(test_currTime, dateTimeString=timeStamp)
263+
call mpas_log_write('**ALARM** '//trim(timeStamp))
264+
end if
265+
call mpas_advance_clock(test_clock, ierr=ierr_local)
266+
end do
267+
#endif
268+
269+
MPAS_ADJUST_ALARM_VERBOSE( test_currTime = mpas_get_clock_time(test_clock, MPAS_NOW, iErr) )
270+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_get_time(test_currTime, dateTimeString=timeStamp) )
271+
272+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('') )
273+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('Now it is '//trim(timeStamp)) )
274+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('') )
275+
276+
277+
write(test_mesg, '(a)') ' forward clock, ref_time < now, now is on new alarm time grid: '
278+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('=================================') )
279+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('Setting ref time to 1999-06-15_00') )
280+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('---------------------------------') )
281+
call mpas_set_time(test_refTime, YYYY=1999, MM=6, DD=15, H=0, M=0, S=0, S_n=0, S_d=0, ierr=ierr_local)
282+
call mpas_adjust_alarm_to_reference_time(test_clock, 'foobar', test_refTime, ierr_local)
283+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_print_alarm(test_clock, 'foobar', ierr_local) )
284+
if (mpas_is_alarm_ringing(test_clock, 'foobar', ierr=ierr_local)) then
285+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is RINGING') )
286+
test_mesg = trim(test_mesg)//' SUCCESS'
287+
else
288+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is NOT ringing') )
289+
test_mesg = trim(test_mesg)//' FAILURE'
290+
ierr = ierr + 1
291+
end if
292+
call mpas_log_write(trim(test_mesg))
293+
call mpas_reset_clock_alarm(test_clock, 'foobar', ierr=ierr_local)
294+
295+
write(test_mesg, '(a)') ' forward clock, ref_time > now, now is on new alarm time grid: '
296+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('=================================') )
297+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('Setting ref time to 2010-02-01_00') )
298+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('---------------------------------') )
299+
call mpas_set_time(test_refTime, YYYY=2010, MM=2, DD=1, H=0, M=0, S=0, S_n=0, S_d=0, ierr=ierr_local)
300+
call mpas_adjust_alarm_to_reference_time(test_clock, 'foobar', test_refTime, ierr_local)
301+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_print_alarm(test_clock, 'foobar', ierr_local) )
302+
if (mpas_is_alarm_ringing(test_clock, 'foobar', ierr=ierr_local)) then
303+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is RINGING') )
304+
test_mesg = trim(test_mesg)//' SUCCESS'
305+
else
306+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is NOT ringing') )
307+
test_mesg = trim(test_mesg)//' FAILURE'
308+
ierr = ierr + 1
309+
end if
310+
call mpas_log_write(trim(test_mesg))
311+
call mpas_reset_clock_alarm(test_clock, 'foobar', ierr=ierr_local)
312+
313+
write(test_mesg, '(a)') ' forward clock, ref_time = now, now is on new alarm time grid: '
314+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('=================================') )
315+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('Setting ref time to 2000-01-01_00') )
316+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('---------------------------------') )
317+
call mpas_set_time(test_refTime, YYYY=2000, MM=1, DD=1, H=0, M=0, S=0, S_n=0, S_d=0, ierr=ierr_local)
318+
call mpas_adjust_alarm_to_reference_time(test_clock, 'foobar', test_refTime, ierr_local)
319+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_print_alarm(test_clock, 'foobar', ierr_local) )
320+
if (mpas_is_alarm_ringing(test_clock, 'foobar', ierr=ierr_local)) then
321+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is RINGING') )
322+
test_mesg = trim(test_mesg)//' SUCCESS'
323+
else
324+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is NOT ringing') )
325+
test_mesg = trim(test_mesg)//' FAILURE'
326+
ierr = ierr + 1
327+
end if
328+
call mpas_log_write(trim(test_mesg))
329+
call mpas_reset_clock_alarm(test_clock, 'foobar', ierr=ierr_local)
330+
331+
write(test_mesg, '(a)') ' forward clock, ref_time < now, now is NOT on new alarm time grid: '
332+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('=================================') )
333+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('Setting ref time to 1999-06-15_08') )
334+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('---------------------------------') )
335+
call mpas_set_time(test_refTime, YYYY=1999, MM=6, DD=15, H=8, M=0, S=0, S_n=0, S_d=0, ierr=ierr_local)
336+
call mpas_adjust_alarm_to_reference_time(test_clock, 'foobar', test_refTime, ierr_local)
337+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_print_alarm(test_clock, 'foobar', ierr_local) )
338+
if (mpas_is_alarm_ringing(test_clock, 'foobar', ierr=ierr_local)) then
339+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is RINGING') )
340+
test_mesg = trim(test_mesg)//' SUCCESS'
341+
else
342+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is NOT ringing') )
343+
test_mesg = trim(test_mesg)//' FAILURE'
344+
ierr = ierr + 1
345+
end if
346+
call mpas_log_write(trim(test_mesg))
347+
call mpas_reset_clock_alarm(test_clock, 'foobar', ierr=ierr_local)
348+
349+
write(test_mesg, '(a)') ' forward clock, ref_time > now, now is NOT on new alarm time grid: '
350+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('=================================') )
351+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('Setting ref time to 2010-02-01_18') )
352+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('---------------------------------') )
353+
call mpas_set_time(test_refTime, YYYY=2010, MM=2, DD=1, H=18, M=0, S=0, S_n=0, S_d=0, ierr=ierr_local)
354+
call mpas_adjust_alarm_to_reference_time(test_clock, 'foobar', test_refTime, ierr_local)
355+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_print_alarm(test_clock, 'foobar', ierr_local) )
356+
if (mpas_is_alarm_ringing(test_clock, 'foobar', ierr=ierr_local)) then
357+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is RINGING') )
358+
test_mesg = trim(test_mesg)//' SUCCESS'
359+
else
360+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is NOT ringing') )
361+
test_mesg = trim(test_mesg)//' FAILURE'
362+
ierr = ierr + 1
363+
end if
364+
call mpas_log_write(trim(test_mesg))
365+
call mpas_reset_clock_alarm(test_clock, 'foobar', ierr=ierr_local)
366+
367+
!
368+
! Set clock to run backwards in time
369+
!
370+
call mpas_set_clock_direction(test_clock, MPAS_BACKWARD, ierr_local)
371+
372+
write(test_mesg, '(a)') ' backward clock, ref_time < now, now is on new alarm time grid: '
373+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('=================================') )
374+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('Setting ref time to 1999-06-15_00') )
375+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('---------------------------------') )
376+
call mpas_set_time(test_refTime, YYYY=1999, MM=6, DD=15, H=0, M=0, S=0, S_n=0, S_d=0, ierr=ierr_local)
377+
call mpas_adjust_alarm_to_reference_time(test_clock, 'foobar', test_refTime, ierr_local)
378+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_print_alarm(test_clock, 'foobar', ierr_local) )
379+
if (mpas_is_alarm_ringing(test_clock, 'foobar', ierr=ierr_local)) then
380+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is RINGING') )
381+
test_mesg = trim(test_mesg)//' SUCCESS'
382+
else
383+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is NOT ringing') )
384+
test_mesg = trim(test_mesg)//' FAILURE'
385+
ierr = ierr + 1
386+
end if
387+
call mpas_log_write(trim(test_mesg))
388+
call mpas_reset_clock_alarm(test_clock, 'foobar', ierr=ierr_local)
389+
390+
write(test_mesg, '(a)') ' backward clock, ref_time > now, now is on new alarm time grid: '
391+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('=================================') )
392+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('Setting ref time to 2010-02-01_00') )
393+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('---------------------------------') )
394+
call mpas_set_time(test_refTime, YYYY=2010, MM=2, DD=1, H=0, M=0, S=0, S_n=0, S_d=0, ierr=ierr_local)
395+
call mpas_adjust_alarm_to_reference_time(test_clock, 'foobar', test_refTime, ierr_local)
396+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_print_alarm(test_clock, 'foobar', ierr_local) )
397+
if (mpas_is_alarm_ringing(test_clock, 'foobar', ierr=ierr_local)) then
398+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is RINGING') )
399+
test_mesg = trim(test_mesg)//' SUCCESS'
400+
else
401+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is NOT ringing') )
402+
test_mesg = trim(test_mesg)//' FAILURE'
403+
ierr = ierr + 1
404+
end if
405+
call mpas_log_write(trim(test_mesg))
406+
call mpas_reset_clock_alarm(test_clock, 'foobar', ierr=ierr_local)
407+
408+
write(test_mesg, '(a)') ' backward clock, ref_time = now, now is on new alarm time grid: '
409+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('=================================') )
410+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('Setting ref time to 2000-01-01_00') )
411+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('---------------------------------') )
412+
call mpas_set_time(test_refTime, YYYY=2000, MM=1, DD=1, H=0, M=0, S=0, S_n=0, S_d=0, ierr=ierr_local)
413+
call mpas_adjust_alarm_to_reference_time(test_clock, 'foobar', test_refTime, ierr_local)
414+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_print_alarm(test_clock, 'foobar', ierr_local) )
415+
if (mpas_is_alarm_ringing(test_clock, 'foobar', ierr=ierr_local)) then
416+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is RINGING') )
417+
test_mesg = trim(test_mesg)//' SUCCESS'
418+
else
419+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is NOT ringing') )
420+
test_mesg = trim(test_mesg)//' FAILURE'
421+
ierr = ierr + 1
422+
end if
423+
call mpas_log_write(trim(test_mesg))
424+
call mpas_reset_clock_alarm(test_clock, 'foobar', ierr=ierr_local)
425+
426+
write(test_mesg, '(a)') ' backward clock, ref_time < now, now is NOT on new alarm time grid: '
427+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('=================================') )
428+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('Setting ref time to 1999-06-15_08') )
429+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('---------------------------------') )
430+
call mpas_set_time(test_refTime, YYYY=1999, MM=6, DD=15, H=8, M=0, S=0, S_n=0, S_d=0, ierr=ierr_local)
431+
call mpas_adjust_alarm_to_reference_time(test_clock, 'foobar', test_refTime, ierr_local)
432+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_print_alarm(test_clock, 'foobar', ierr_local) )
433+
if (mpas_is_alarm_ringing(test_clock, 'foobar', ierr=ierr_local)) then
434+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is RINGING') )
435+
test_mesg = trim(test_mesg)//' SUCCESS'
436+
else
437+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is NOT ringing') )
438+
test_mesg = trim(test_mesg)//' FAILURE'
439+
ierr = ierr + 1
440+
end if
441+
call mpas_log_write(trim(test_mesg))
442+
call mpas_reset_clock_alarm(test_clock, 'foobar', ierr=ierr_local)
443+
444+
write(test_mesg, '(a)') ' backward clock, ref_time > now, now is NOT on new alarm time grid: '
445+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('=================================') )
446+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('Setting ref time to 2010-02-01_18') )
447+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('---------------------------------') )
448+
call mpas_set_time(test_refTime, YYYY=2010, MM=2, DD=1, H=18, M=0, S=0, S_n=0, S_d=0, ierr=ierr_local)
449+
call mpas_adjust_alarm_to_reference_time(test_clock, 'foobar', test_refTime, ierr_local)
450+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_print_alarm(test_clock, 'foobar', ierr_local) )
451+
if (mpas_is_alarm_ringing(test_clock, 'foobar', ierr=ierr_local)) then
452+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is RINGING') )
453+
test_mesg = trim(test_mesg)//' SUCCESS'
454+
else
455+
MPAS_ADJUST_ALARM_VERBOSE( call mpas_log_write('-> Alarm is NOT ringing') )
456+
test_mesg = trim(test_mesg)//' FAILURE'
457+
ierr = ierr + 1
458+
end if
459+
call mpas_log_write(trim(test_mesg))
460+
call mpas_reset_clock_alarm(test_clock, 'foobar', ierr=ierr_local)
461+
462+
end subroutine mpas_adjust_alarm_tests
463+
181464
end module test_core_timekeeping_tests

0 commit comments

Comments
 (0)