|
| 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 FragmentedLinearDistributionTest { |
| 11 | + |
| 12 | + private lateinit var distribution: FragmentedLinearDistribution |
| 13 | + private val accuracy = 0.0001f |
| 14 | + |
| 15 | + @Before |
| 16 | + fun setUp() { |
| 17 | + distribution = FragmentedLinearDistribution.Builder() |
| 18 | + .sliceAt(2f, 0f) |
| 19 | + .sliceAt(1f, 0.5f) |
| 20 | + .sliceAt(2f, 0.8f) |
| 21 | + .build() |
| 22 | + } |
| 23 | + |
| 24 | + @After |
| 25 | + fun tearDown() { |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + fun `range overlap exception`() { |
| 30 | + assertThrows(FragmentedLinearDistribution.OverlappingRangeException::class.java) { |
| 31 | + FragmentedLinearDistribution.Builder() |
| 32 | + .sliceAt(0.2f, 0f) |
| 33 | + .sliceAt(1f, 0.75f) |
| 34 | + .sliceAt(3f, 0.55f) |
| 35 | + .build() |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun `range starts with zero exception`() { |
| 41 | + assertThrows(FragmentedLinearDistribution.FirstValueNotZeroException::class.java) { |
| 42 | + FragmentedLinearDistribution.Builder() |
| 43 | + .sliceAt(0.2f, 0.3f) |
| 44 | + .sliceAt(1f, 0.75f) |
| 45 | + .build() |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun `out of range value exception`() { |
| 51 | + assertThrows(FragmentedLinearDistribution.OutOfRangeException::class.java) { |
| 52 | + FragmentedLinearDistribution.Builder() |
| 53 | + .sliceAt(0.2f, 0f) |
| 54 | + .sliceAt(1f, 1.75f) |
| 55 | + .build() |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `create distribution with no exception`() { |
| 61 | + val distribution = FragmentedLinearDistribution.Builder() |
| 62 | + .sliceAt(0.2f, 0f) |
| 63 | + .sliceAt(1f, 0.5f) |
| 64 | + .sliceAt(3f, 0.8f) |
| 65 | + .build() |
| 66 | + assertNotEquals(distribution.interpolate(0.5f), null) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun `test value from offset distribution on ranges`() { |
| 71 | + val value0 = distribution.interpolate(0f) ?: 0f // 0 |
| 72 | + val value1 = distribution.interpolate(0.3f) ?: 0f // 0.35294117 |
| 73 | + val value2 = distribution.interpolate(0.7f) ?: 0f // 0.7058823 |
| 74 | + val value3 = distribution.interpolate(0.9f) ?: 0f // 0.88235294 |
| 75 | + val value4 = distribution.interpolate(1f) ?: 0f // 1 |
| 76 | + |
| 77 | + assertEquals(0f, value0, accuracy) |
| 78 | + assertEquals(0.3529411f, value1, accuracy) |
| 79 | + assertEquals(0.7058823f, value2, accuracy) |
| 80 | + assertEquals(0.8823529f, value3, accuracy) |
| 81 | + assertEquals(1f, 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.inverse(0f) ?: 0f // 0 |
| 91 | + val value1 = distribution.inverse(0.35294f) ?: 0f // 0.3 |
| 92 | + val value2 = distribution.inverse(0.70588f) ?: 0f // 0.7 |
| 93 | + val value3 = distribution.inverse(0.88235f) ?: 0f // 0.9 |
| 94 | + val value4 = distribution.inverse(1f) ?: 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