@@ -26,6 +26,11 @@ abstract class IntervalTest<T : Comparable<T>, TSize : Comparable<TSize>>(
2626 private val valueOperations = operations.valueOperations
2727 private val sizeOperations = operations.sizeOperations
2828
29+ /* *
30+ * Value which lies as far beyond [c] as [b] lies beyond [a].
31+ */
32+ private val d = valueOperations.unsafeAdd( c, valueOperations.unsafeSubtract( b, a ) )
33+
2934 private fun createInterval ( start : T , isStartIncluded : Boolean , end : T , isEndIncluded : Boolean ) =
3035 Interval ( start, isStartIncluded, end, isEndIncluded, operations )
3136 private fun createClosedInterval ( start : T , end : T ): Interval <T , TSize > = createInterval( start, true , end, true )
@@ -138,4 +143,104 @@ abstract class IntervalTest<T : Comparable<T>, TSize : Comparable<TSize>>(
138143 val openIntervals = listOf ( createOpenInterval( a, b ), createOpenInterval( b, a ) )
139144 openIntervals.forEach { assertTrue( a !in it && b !in it ) }
140145 }
146+
147+ @Test
148+ fun intersects_for_fully_contained_intervals ()
149+ {
150+ val ad = createClosedInterval( a, d )
151+
152+ val withinIntervals = createAllInclusionTypeIntervals( b, c )
153+ val onEndPointIntervals = createAllInclusionTypeIntervals( a, b ) + createAllInclusionTypeIntervals( c, d )
154+ (withinIntervals + onEndPointIntervals).forEach { assertIntersects( ad, it, true ) }
155+ }
156+
157+ @Test
158+ fun intersects_for_partial_overlapping_interval ()
159+ {
160+ val acIntervals = createAllInclusionTypeIntervals( a, c )
161+ val bdIntervals = createAllInclusionTypeIntervals( b, d )
162+
163+ for ( ac in acIntervals ) for ( bd in bdIntervals )
164+ assertIntersects( ac, bd, true )
165+ }
166+
167+ @Test
168+ fun intersects_for_nonoverlapping_intervals ()
169+ {
170+ val abIntervals = createAllInclusionTypeIntervals( a, b )
171+ val cdIntervals = createAllInclusionTypeIntervals( c, d )
172+
173+ for ( ab in abIntervals ) for ( cd in cdIntervals )
174+ assertIntersects( ab, cd, false )
175+ }
176+
177+ @Test
178+ fun intersects_for_touching_endpoints ()
179+ {
180+ val abWithB = createClosedInterval( a, b )
181+ val bcWithB = createClosedInterval( b, c )
182+ assertIntersects( abWithB, bcWithB, true )
183+
184+ val abWithoutB = createOpenInterval( a, b )
185+ val bcWithoutB = createOpenInterval( b, c )
186+ assertIntersects( abWithoutB, bcWithoutB, false )
187+
188+ assertIntersects( abWithB, bcWithoutB, false )
189+ assertIntersects( abWithoutB, bcWithB, false )
190+ }
191+
192+ @Test
193+ fun nonReversed_reversed_when_isReversed ()
194+ {
195+ val reversed = createAllInclusionTypeIntervals( b, a )
196+ for ( original in reversed )
197+ {
198+ val normal = original.nonReversed()
199+ assertEquals( original.reverse(), normal )
200+ }
201+ }
202+
203+ @Test
204+ fun nonReversed_unchanged_when_not_isReversed ()
205+ {
206+ val normal = createAllInclusionTypeIntervals( a, b )
207+ for ( original in normal )
208+ {
209+ val unchanged = original.nonReversed()
210+ assertEquals( original, unchanged )
211+ }
212+ }
213+
214+ @Test
215+ fun reverse_succeeds ()
216+ {
217+ val toReverse = createAllInclusionTypeIntervals( a, b )
218+ for ( original in toReverse )
219+ {
220+ val reversed = original.reverse()
221+ assertEquals( original.start, reversed.end )
222+ assertEquals( original.isStartIncluded, reversed.isEndIncluded )
223+ assertEquals( original.end, reversed.start )
224+ assertEquals( original.isEndIncluded, reversed.isStartIncluded )
225+ }
226+ }
227+
228+ private fun assertIntersects ( interval1 : Interval <T , TSize >, interval2 : Interval <T , TSize >, intersects : Boolean )
229+ {
230+ assertEquals( intersects, interval1.intersects( interval2 ) )
231+ assertEquals( intersects, interval2.intersects( interval1 ) )
232+
233+ // Reversing intervals should have no effect on whether they intersect or not.
234+ val interval1Reversed = interval1.reverse()
235+ val interval2Reversed = interval2.reverse()
236+
237+ assertEquals( intersects, interval1.intersects( interval2Reversed ) )
238+ assertEquals( intersects, interval2Reversed.intersects( interval1 ) )
239+
240+ assertEquals( intersects, interval1Reversed.intersects( interval2 ) )
241+ assertEquals( intersects, interval2.intersects( interval1Reversed ) )
242+
243+ assertEquals( intersects, interval1Reversed.intersects( interval2Reversed ) )
244+ assertEquals( intersects, interval2Reversed.intersects( interval1Reversed ) )
245+ }
141246}
0 commit comments