From e0bf850b12c1bab60030240f3fde8ee7084f12e3 Mon Sep 17 00:00:00 2001 From: James Brown <64858662+james-d-brown@users.noreply.github.com> Date: Thu, 26 Dec 2024 16:44:26 +0000 Subject: [PATCH] Add unit tests for the intersection of time windows, #130 --- .../datamodel/time/TimeWindowSlicerTest.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/wres-datamodel/test/wres/datamodel/time/TimeWindowSlicerTest.java b/wres-datamodel/test/wres/datamodel/time/TimeWindowSlicerTest.java index 5a8d20f45..29b4a554d 100644 --- a/wres-datamodel/test/wres/datamodel/time/TimeWindowSlicerTest.java +++ b/wres-datamodel/test/wres/datamodel/time/TimeWindowSlicerTest.java @@ -180,6 +180,103 @@ void testIntersectionOnLeadDurationsOnly() assertEquals( expected, actual ); } + @Test + void testIntersectionOnValidTimeOnly() + { + TimeWindow one = MessageFactory.getTimeWindow( Instant.parse( INSTANT_ONE ), + Instant.parse( INSTANT_TWO ) ); + TimeWindow two = MessageFactory.getTimeWindow( Instant.parse( INSTANT_FOUR ), + Instant.parse( INSTANT_THREE ) ); + TimeWindow three = MessageFactory.getTimeWindow( Instant.parse( INSTANT_ONE ), + Instant.parse( INSTANT_THREE ) ); + TimeWindow four = MessageFactory.getTimeWindow( Instant.parse( INSTANT_ELEVEN ), + Instant.parse( INSTANT_TEN ) ); + Set first = Set.of( TimeWindowOuter.of( one ), + TimeWindowOuter.of( two ) ); + Set second = Set.of( TimeWindowOuter.of( three ), + TimeWindowOuter.of( four ) ); + + Set actual = TimeWindowSlicer.intersection( first, second ); + Set expected = Set.of( TimeWindowOuter.of( one ), + TimeWindowOuter.of( two ), + TimeWindowOuter.of( three ) ); + + assertEquals( expected, actual ); + } + + @Test + void testIntersectionOnReferenceTimeOnly() + { + TimeWindow one = MessageFactory.getTimeWindow( Instant.parse( INSTANT_ONE ), + Instant.parse( INSTANT_TWO ), + Instant.MIN, + Instant.MAX ); + TimeWindow two = MessageFactory.getTimeWindow( Instant.parse( INSTANT_FOUR ), + Instant.parse( INSTANT_THREE ), + Instant.MIN, + Instant.MAX ); + TimeWindow three = MessageFactory.getTimeWindow( Instant.parse( INSTANT_ONE ), + Instant.parse( INSTANT_THREE ), + Instant.MIN, + Instant.MAX ); + TimeWindow four = MessageFactory.getTimeWindow( Instant.parse( INSTANT_ELEVEN ), + Instant.parse( INSTANT_TEN ), + Instant.MIN, + Instant.MAX ); + Set first = Set.of( TimeWindowOuter.of( one ), + TimeWindowOuter.of( two ) ); + Set second = Set.of( TimeWindowOuter.of( three ), + TimeWindowOuter.of( four ) ); + + Set actual = TimeWindowSlicer.intersection( first, second ); + Set expected = Set.of( TimeWindowOuter.of( one ), + TimeWindowOuter.of( two ), + TimeWindowOuter.of( three ) ); + + assertEquals( expected, actual ); + } + + @Test + void testIntersectionOnLeadDurationValidTimeAndReferenceTime() + { + TimeWindow one = MessageFactory.getTimeWindow( Instant.parse( "1934-01-01T00:00:00Z" ), + Instant.parse( "1934-01-02T00:00:00Z" ), + Instant.parse( "1935-01-01T00:00:00Z" ), + Instant.parse( "1935-01-02T00:00:00Z" ), + Duration.ofHours( 1 ), + Duration.ofHours( 2 ) ); + TimeWindow two = MessageFactory.getTimeWindow( Instant.parse( "1933-01-01T00:00:00Z" ), + Instant.parse( "1934-01-02T00:00:00Z" ), + Instant.parse( "1934-01-01T00:00:00Z" ), + Instant.parse( "1935-01-02T00:00:00Z" ), + Duration.ofHours( 1 ), + Duration.ofHours( 2 ) ); + TimeWindow three = MessageFactory.getTimeWindow( Instant.parse( "1934-01-01T00:00:00Z" ), + Instant.parse( "1934-01-04T00:00:00Z" ), + Instant.parse( "1935-01-01T00:00:00Z" ), + Instant.parse( "1935-01-03T00:00:00Z" ), + Duration.ofHours( 2 ), + Duration.ofHours( 5 ) ); + TimeWindow four = MessageFactory.getTimeWindow( Instant.parse( "1934-01-01T00:00:00Z" ), + Instant.parse( "1934-01-02T00:00:00Z" ), + Instant.parse( "1935-01-01T00:00:00Z" ), + Instant.parse( "1935-01-02T00:00:00Z" ), + Duration.ofHours( 3 ), + Duration.ofHours( 4 ) ); + + Set first = Set.of( TimeWindowOuter.of( one ), + TimeWindowOuter.of( two ) ); + Set second = Set.of( TimeWindowOuter.of( three ), + TimeWindowOuter.of( four ) ); + + Set actual = TimeWindowSlicer.intersection( first, second ); + Set expected = Set.of( TimeWindowOuter.of( one ), + TimeWindowOuter.of( two ), + TimeWindowOuter.of( three ) ); + + assertEquals( expected, actual ); + } + @Test void testAdjustByTimeScalePeriodWhenTimeScaleIsInstantaneous() {