aboutsummaryrefslogtreecommitdiff
path: root/src/test/kotlin/util/math/GChainReconciliationTest.kt
blob: 502bd9eca3ec15a3b8e0c1e9e884c260f270fff8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package moe.nea.firmament.test.util.math

import io.kotest.core.spec.style.AnnotationSpec
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.*
import moe.nea.firmament.util.math.GChainReconciliation
import moe.nea.firmament.util.math.GChainReconciliation.rotated

class GChainReconciliationTest : AnnotationSpec() {

	fun <T> assertEqualCycles(
		expected: List<T>,
		actual: List<T>
	) {
		for (offset in expected.indices) {
			val rotated = expected.rotated(offset)
			val matchesAtRotation = run {
				for ((i, v) in actual.withIndex()) {
					if (rotated[i % rotated.size] != v)
						return@run false
				}
				true
			}
			if (matchesAtRotation)
				return
		}
		assertEquals(expected, actual, "Expected arrays to be cycle equivalent")
	}

	@Test
	fun testUnfixableCycleNotBeingModified() {
		assertEquals(
			listOf(1, 2, 3, 4, 6, 1, 2, 3, 4, 6),
			GChainReconciliation.reconcileCycles(
				listOf(1, 2, 3, 4, 6, 1, 2, 3, 4, 6),
				listOf(2, 3, 4, 5, 1, 2, 3, 4, 5, 1)
			)
		)
	}

	@Test
	fun testMultipleIndependentHoles() {
		assertEqualCycles(
			listOf(1, 2, 3, 4, 5, 6),
			GChainReconciliation.reconcileCycles(
				listOf(1, 3, 4, 5, 6, 1, 3, 4, 5, 6),
				listOf(2, 3, 4, 5, 1, 2, 3, 4, 5, 1)
			)
		)

	}

	@Test
	fun testBigHole() {
		assertEqualCycles(
			listOf(1, 2, 3, 4, 5, 6),
			GChainReconciliation.reconcileCycles(
				listOf(1, 4, 5, 6, 1, 4, 5, 6),
				listOf(2, 3, 4, 5, 1, 2, 3, 4, 5, 1)
			)
		)

	}

	@Test
	fun testOneMissingBeingDetected() {
		assertEqualCycles(
			listOf(1, 2, 3, 4, 5, 6),
			GChainReconciliation.reconcileCycles(
				listOf(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6),
				listOf(2, 3, 4, 5, 1, 2, 3, 4, 5, 1)
			)
		)
	}
}