@@ -294,7 +294,7 @@ end subroutine teardown_alarm_fixture
294294 !> Timeline:
295295 !> A ---------------- B ----- C ---------------- D --------- E
296296 !>
297- !> The test covers 18 cases, including behavior:
297+ !> The test covers 19 cases, including behavior:
298298 !> - Before anchor (A - > B)
299299 !> - Between anchor and window start (B - > C)
300300 !> - At window boundaries (C, D)
@@ -305,6 +305,7 @@ end subroutine teardown_alarm_fixture
305305 !> - When the clock direction changes (forward/ backward)
306306 !> - When the window start time is after the stop time (invalid)
307307 !> - When the window start time equals the stop time (valid)
308+ !> - When the window start time is before the clock start time (valid)
308309 !>
309310 !> Each case checks whether the alarm rings at the correct times and
310311 !> uses mpas_log_write to log PASS/ FAIL outcomes.
@@ -456,7 +457,7 @@ subroutine test_window_alarm(case_idx, ierr)
456457 !-----------------------------------------------------------------------
457458 case(13 )
458459 call mpas_remove_clock_alarm(f%clock, f%alarm_id)
459- call mpas_set_time(f%alarm_time, YYYY= 2000 , MM= 01 , DD= 01 , H= 9 , M= 0 , S= 0 , S_n= 0 , S_d= 0 , ierr = ierr )
460+ call mpas_set_time(f%alarm_time, YYYY= 2000 , MM= 01 , DD= 01 , H= 9 , M= 0 , S= 0 , S_n= 0 , S_d= 0 )
460461 call mpas_add_clock_alarm(f%clock, f%alarm_id, f%alarm_time, &
461462 alarmTimeInterval = f%alarm_interval, &
462463 alarmStartTime = f%window_start_time, &
@@ -552,6 +553,39 @@ subroutine test_window_alarm(case_idx, ierr)
552553 alarmStopTime = f%window_start_time, ierr= local_ierr)
553554 call assert_false(local_ierr /= 0 , ' Alarm with equal start/stop times incorrectly returned error' , local_ierr)
554555 ierr = ierr + local_ierr
556+
557+ !-----------------------------------------------------------------------
558+ ! Case 19 : Alarm starting before clock start test
559+ !>
560+ !> This test verifies that mpas_add_clock_alarm allows alarms whose
561+ !> active window begins before the clock start time:
562+ !> 1 . It must not return an error when the activeStartTime occurs
563+ !> before the clock start time.
564+ !> 2 . The alarm should ring immediately when the clock begins,
565+ !> since its anchor time equals the clock start time.
566+ !-----------------------------------------------------------------------
567+ case(19 )
568+ call mpas_remove_clock_alarm(f%clock, f%alarm_id)
569+
570+ ! Set the alarm anchor time equal to the clock start time so it can ring immediately
571+ f%alarm_time = f%clock_start_time
572+
573+ ! Add an alarm whose active window begins before the clock start time
574+ call mpas_add_clock_alarm(f%clock, f%alarm_id, f%alarm_time, &
575+ alarmTimeInterval = f%alarm_interval, &
576+ alarmStartTime = f%clock_start_time - mul_ti_n(f%alarm_interval, 4 ), &
577+ alarmStopTime = f%window_stop_time, ierr= local_ierr)
578+
579+ ! Verify that adding the alarm does not return an error
580+ call assert_false(local_ierr /= 0 , &
581+ ' Alarm with start time before clock start time incorrectly returned error' , local_ierr)
582+
583+ ! Check whether the alarm rings immediately at clock start
584+ ringing = mpas_is_alarm_ringing(f%clock, f%alarm_id)
585+ call assert_true(ringing, &
586+ ' Alarm is not ringing inside window with start time before clock start' , local_ierr)
587+ ierr = ierr + local_ierr
588+
555589 end select
556590
557591 call teardown_alarm_fixture(f)
@@ -572,8 +606,8 @@ integer function mpas_window_alarm_tests() result(ierr)
572606 integer :: i, ierr_local
573607
574608 ierr = 0
575- call mpas_log_write(' Running 18 window alarm tests' )
576- do i = 1 , 18
609+ call mpas_log_write(' Running 19 window alarm tests' )
610+ do i = 1 , 19
577611 ierr_local = 0
578612 call test_window_alarm(i, ierr_local)
579613 ierr = ierr + ierr_local
0 commit comments