@@ -8,11 +8,11 @@ import kotlin.test.*
88
99/* *
1010 * Tests for [Interval] which creates intervals for testing using [a], which should be smaller than [b],
11- * which should be smaller than [c].
12- * For evenly-spaced types of [T], the distance between [a] and [b], and [b] and [c], should be greater than the spacing
13- * between any two subsequent values in the set.
14- * For non-evenly-spaced types, the distance between [a] and [b] should be small enough so that there isn't sufficient
15- * precision to represent them as individual values when shifted close to the max possible values represented by [T].
11+ * which should be smaller than [c]. The distance between [a] and [b], and [b] and [c], should be equal.
12+ * In addition, for evenly-spaced types of [T], this distance should be greater than the spacing between any two
13+ * subsequent values in the set.
14+ * And, for non-evenly-spaced types, this distance should be small enough so that there isn't sufficient precision to
15+ * represent them as individual values when shifted close to the max possible values represented by [T].
1616 */
1717@Suppress( " FunctionName" )
1818abstract class IntervalTest <T : Comparable <T >, TSize : Comparable <TSize >>(
@@ -59,14 +59,14 @@ abstract class IntervalTest<T : Comparable<T>, TSize : Comparable<TSize>>(
5959 assertTrue( a < b && b < c )
6060 assertTrue( abSize > sizeOperations.additiveIdentity )
6161
62- // For evenly-spaced types, the distance between a-b, and b-c, should be greater than the spacing.
62+ // The distance between a-b and b-c should be equal.
63+ val abSize = valueOperations.unsafeSubtract( b, a )
64+ val bcSize = valueOperations.unsafeSubtract( c, b )
65+ assertTrue( abSize == bcSize )
66+
67+ // For evenly-spaced types, the distance should be e greater than the spacing.
6368 val spacing = valueOperations.spacing
64- if ( spacing != null )
65- {
66- val abSize = valueOperations.unsafeSubtract( b, a )
67- val bcSize = valueOperations.unsafeSubtract( c, b )
68- assertTrue( abSize > spacing && bcSize > spacing )
69- }
69+ if ( spacing != null ) assertTrue( abSize > spacing )
7070 }
7171
7272 @Test
@@ -179,6 +179,82 @@ abstract class IntervalTest<T : Comparable<T>, TSize : Comparable<TSize>>(
179179 openIntervals.forEach { assertTrue( a !in it && b !in it ) }
180180 }
181181
182+ @Test
183+ fun getValueAt_inside_interval ()
184+ {
185+ val acIntervals = createAllInclusionTypeIntervals( a, c )
186+
187+ for ( ac in acIntervals )
188+ {
189+ assertEquals( a,ac.getValueAt( 0.0 ) )
190+ assertEquals( b, ac.getValueAt( 0.5 ) )
191+ assertEquals( c, ac.getValueAt( 1.0 ) )
192+ }
193+ }
194+
195+ @Test
196+ fun getValueAt_outside_interval ()
197+ {
198+ val abIntervals = createAllInclusionTypeIntervals( a, b )
199+ for ( ab in abIntervals )
200+ {
201+ assertEquals( c, ab.getValueAt( 2.0 ) )
202+ }
203+
204+ val bcIntervals = createAllInclusionTypeIntervals( b, c )
205+ for ( bc in bcIntervals )
206+ {
207+ assertEquals( a, bc.getValueAt( - 1.0 ) )
208+ }
209+ }
210+
211+ @Test
212+ fun getValueAt_reverse_intervals ()
213+ {
214+ val adIntervals = createAllInclusionTypeIntervals( a, d )
215+
216+ for ( ad in adIntervals )
217+ {
218+ val nonReversed = ad.getValueAt( 0.2 )
219+ val reversed = ad.reverse().getValueAt( 0.8 )
220+ assertEquals(
221+ valueOperations.toDouble( nonReversed ),
222+ valueOperations.toDouble( reversed ),
223+ absoluteTolerance = 0.000000001
224+ )
225+ }
226+ }
227+
228+ @Test
229+ fun getValueAt_returned_value_overflows ()
230+ {
231+ val maxIntervals = createAllInclusionTypeIntervals( operations.minValue, operations.maxValue )
232+
233+ for ( max in maxIntervals )
234+ {
235+ assertEquals( max.start, max.getValueAt( 0.0 ) )
236+ // Loss of precision for large values as part of double conversion is expected.
237+ // Therefore, compare double-converted values which have the same loss of precision.
238+ assertEquals(
239+ valueOperations.toDouble(max.end ),
240+ valueOperations.toDouble( max.getValueAt( 1.0 ) )
241+ )
242+ assertFailsWith<ArithmeticException > { max.getValueAt( - 0.1 ) }
243+ assertFailsWith<ArithmeticException > { max.getValueAt( 1.1 ) }
244+ }
245+ }
246+
247+ @Test
248+ fun getValueAt_percentage_overflows ()
249+ {
250+ val ab = createClosedInterval( a, b )
251+
252+ val maxPercentage = sizeOperations.toDouble( sizeOperations.maxValue ) / sizeOperations.toDouble( ab.size )
253+ val tooBigPercentage = maxPercentage * 2
254+
255+ assertFailsWith<ArithmeticException > { ab.getValueAt( tooBigPercentage ) }
256+ }
257+
182258 @Test
183259 fun minus_for_interval_lying_within ()
184260 {
0 commit comments