Skip to content

Commit 81ac612

Browse files
committed
write tests for SensitivityDistribution
1 parent 747ff73 commit 81ac612

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package io.monstarlab.mosaic.slider
2+
3+
import org.junit.After
4+
import org.junit.Assert.assertEquals
5+
import org.junit.Assert.assertNotEquals
6+
import org.junit.Assert.assertThrows
7+
import org.junit.Before
8+
import org.junit.Test
9+
10+
class SensitivityDistributionTest {
11+
12+
private lateinit var distribution: SensitivityDistribution
13+
private val accuracy = 0.0001f
14+
15+
@Before
16+
fun setUp() {
17+
distribution = SensitivityDistribution.Builder()
18+
.add(2f, 0f)
19+
.add(1f, 0.5f)
20+
.add(2f, 0.8f)
21+
.build()
22+
}
23+
24+
@After
25+
fun tearDown() {
26+
}
27+
28+
@Test
29+
fun `range overlap exception`() {
30+
assertThrows(SensitivityDistribution.OverlappingRangeException::class.java) {
31+
SensitivityDistribution.Builder()
32+
.add(0.2f, 0f)
33+
.add(1f, 0.75f)
34+
.add(3f, 0.55f)
35+
.build()
36+
}
37+
}
38+
39+
@Test
40+
fun `range starts with zero exception`() {
41+
assertThrows(SensitivityDistribution.FirstValueNotZeroException::class.java) {
42+
SensitivityDistribution.Builder()
43+
.add(0.2f, 0.3f)
44+
.add(1f, 0.75f)
45+
.build()
46+
}
47+
}
48+
49+
@Test
50+
fun `out of range value exception`() {
51+
assertThrows(SensitivityDistribution.OutOfRangeException::class.java) {
52+
SensitivityDistribution.Builder()
53+
.add(0.2f, 0f)
54+
.add(1f, 1.75f)
55+
.build()
56+
}
57+
}
58+
59+
@Test
60+
fun `create distribution with no exception`() {
61+
val distribution = SensitivityDistribution.Builder()
62+
.add(0.2f, 0f)
63+
.add(1f, 0.5f)
64+
.add(3f, 0.8f)
65+
.build()
66+
assertNotEquals(distribution.valueFromOffset(0.5f), null)
67+
}
68+
69+
@Test
70+
fun `test value from offset distribution on ranges`() {
71+
val value0 = distribution.valueFromOffset(0f) ?: 0f // 0
72+
val value1 = distribution.valueFromOffset(0.3f) ?: 0f // 0.6
73+
val value2 = distribution.valueFromOffset(0.7f) ?: 0f // 1.2
74+
val value3 = distribution.valueFromOffset(0.9f) ?: 0f // 1.5
75+
val value4 = distribution.valueFromOffset(1f) ?: 0f // 1.7
76+
77+
assertEquals(0f, value0, accuracy)
78+
assertEquals(0.6f, value1, accuracy)
79+
assertEquals(1.2f, value2, accuracy)
80+
assertEquals(1.5f, value3, accuracy)
81+
assertEquals(1.7f, value4, accuracy)
82+
}
83+
84+
@Test
85+
fun `test offset from value distribution on ranges`() {
86+
println("---------------------------------------------")
87+
distribution.equationList.forEach {
88+
println(it)
89+
}
90+
val value0 = distribution.offsetFromValue(0f) ?: 0f // 0
91+
val value1 = distribution.offsetFromValue(0.6f) ?: 0f // 0.3
92+
val value2 = distribution.offsetFromValue(1.2f) ?: 0f // 0.7
93+
val value3 = distribution.offsetFromValue(1.5f) ?: 0f // 0.9
94+
val value4 = distribution.offsetFromValue(1.69999f) ?: 0f // 1
95+
assertEquals(0f, value0, accuracy)
96+
assertEquals(0.3f, value1, accuracy)
97+
assertEquals(0.7f, value2, accuracy)
98+
assertEquals(0.9f, value3, accuracy)
99+
assertEquals(1f, value4, accuracy)
100+
}
101+
}

0 commit comments

Comments
 (0)