Skip to content

Commit 08e9a3d

Browse files
committed
test(plugin): add and adjust shape transformation tests
1 parent 51d93d4 commit 08e9a3d

File tree

2 files changed

+72
-14
lines changed

2 files changed

+72
-14
lines changed

plugin/src/shared/sc/plugin2021/util/SetHelpers.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sc.plugin2021.util
22

33
import sc.plugin2021.Coordinates
4+
import sc.plugin2021.FieldContent
45
import sc.plugin2021.Rotation
56
import sc.plugin2021.Vector
67

@@ -54,10 +55,24 @@ fun Set<Coordinates>.align(): Set<Coordinates> {
5455

5556
/** Prints an ascii art of the piece. */
5657
fun Set<Coordinates>.print(dimension: Vector = Vector(4, 5)) {
57-
val array = Array(dimension.area) {'-'}
58-
align().forEach { array[it.x + dimension.dx * it.y] = '#' }
59-
for (x in 0 until dimension.area) {
58+
printShapes(this, dimension = dimension)
59+
}
60+
61+
/** Prints all given shapes next to each other. */
62+
fun printShapes(vararg shapes: Set<Coordinates>, dimension: Vector = Vector(4, 5)) {
63+
val width = shapes.size * (dimension.dx + 1)
64+
val array = Array(dimension.dy * width) {FieldContent.EMPTY.letter}
65+
for (n in array.indices) {
66+
if ((n + 1) % (dimension.dx + 1) == 0) array[n] = ' '
67+
}
68+
for (n in shapes.indices) {
69+
shapes[n].align().forEach {
70+
array[it.x + n * (dimension.dx + 1) + width * it.y] = '#'
71+
}
72+
}
73+
for (x in array.indices) {
6074
print("${array[x]} ")
61-
if (x % dimension.dx == dimension.dx - 1) println()
75+
if ((x + 1) % width == 0 ) println()
6276
}
77+
println()
6378
}

plugin/src/test/sc/plugin2021/PieceTest.kt

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.thoughtworks.xstream.XStream
44
import io.kotlintest.inspectors.forAll
55
import io.kotlintest.shouldBe
66
import io.kotlintest.specs.StringSpec
7+
import org.opentest4j.AssertionFailedError
78
import sc.plugin2021.util.*
89

910

@@ -31,22 +32,64 @@ class PieceTest: StringSpec({
3132
shape.flip(true).flip() shouldBe shape
3233
}
3334
}
34-
"Test Set transformation arithmetic" {
35+
"Test Set transformation arithmetic (PENTO_W)" {
36+
val TID = listOf("NN", "RN", "MN", "LN", "NY", "RY", "MY", "LY")
37+
val shape = PieceShape.PENTO_W
3538
val shapes = listOf(
3639
setOf(Coordinates(0, 0), Coordinates(0, 1), Coordinates(1, 1), Coordinates(1, 2), Coordinates(2, 2)),
3740
setOf(Coordinates(0, 2), Coordinates(0, 1), Coordinates(1, 1), Coordinates(1, 0), Coordinates(2, 0)),
3841
setOf(Coordinates(0, 0), Coordinates(1, 0), Coordinates(1, 1), Coordinates(2, 1), Coordinates(2, 2)),
3942
setOf(Coordinates(0, 2), Coordinates(1, 2), Coordinates(1, 1), Coordinates(2, 1), Coordinates(2, 0))
4043
)
41-
42-
PieceShape.PENTO_W.transform(Rotation.NONE, false) shouldBe shapes[0]
43-
PieceShape.PENTO_W.transform(Rotation.RIGHT, false) shouldBe shapes[1]
44-
PieceShape.PENTO_W.transform(Rotation.MIRROR, false) shouldBe shapes[2]
45-
PieceShape.PENTO_W.transform(Rotation.LEFT, false) shouldBe shapes[3]
46-
PieceShape.PENTO_W.transform(Rotation.NONE, true) shouldBe shapes[3]
47-
PieceShape.PENTO_W.transform(Rotation.RIGHT, true) shouldBe shapes[2]
48-
PieceShape.PENTO_W.transform(Rotation.MIRROR, true) shouldBe shapes[1]
49-
PieceShape.PENTO_W.transform(Rotation.LEFT, true) shouldBe shapes[0]
44+
val SHOULD = (TID zip (shapes + shapes.asReversed())).toMap()
45+
val transformations = (TID zip (
46+
(Rotation.values() zip List(Rotation.values().size) {false}) +
47+
(Rotation.values() zip List(Rotation.values().size) {true})
48+
)).toMap()
49+
val IS = transformations.map {
50+
it.key to shape.transform(it.value.first, it.value.second)
51+
}.toMap()
52+
53+
TID.forEach {
54+
try {
55+
IS[it] shouldBe SHOULD[it]
56+
} catch (e: AssertionFailedError) {
57+
println("Expected: $it Actual:")
58+
printShapes(SHOULD.getValue(it), IS.getValue(it))
59+
throw e
60+
}
61+
}
62+
}
63+
"Test Set transformation arithmetic (PENTO_R)" {
64+
val shape = PieceShape.PENTO_R
65+
val TID = listOf("NN", "RN", "MN", "LN", "NY", "RY", "MY", "LY")
66+
val SHOULD = (TID zip listOf(
67+
setOf(Coordinates(0, 1), Coordinates(1, 2), Coordinates(1, 1), Coordinates(2, 1), Coordinates(2, 0)),
68+
setOf(Coordinates(0, 1), Coordinates(1, 0), Coordinates(1, 1), Coordinates(1, 2), Coordinates(2, 2)),
69+
setOf(Coordinates(0, 2), Coordinates(0, 1), Coordinates(1, 1), Coordinates(1, 0), Coordinates(2, 1)),
70+
setOf(Coordinates(0, 0), Coordinates(1, 0), Coordinates(1, 1), Coordinates(1, 2), Coordinates(2, 1)),
71+
setOf(Coordinates(0, 0), Coordinates(0, 1), Coordinates(1, 1), Coordinates(1, 2), Coordinates(2, 1)),
72+
setOf(Coordinates(0, 2), Coordinates(1, 2), Coordinates(1, 1), Coordinates(1, 0), Coordinates(2, 1)),
73+
setOf(Coordinates(0, 1), Coordinates(1, 0), Coordinates(1, 1), Coordinates(2, 1), Coordinates(2, 2)),
74+
setOf(Coordinates(0, 1), Coordinates(1, 2), Coordinates(1, 1), Coordinates(1, 0), Coordinates(2, 0))
75+
)).toMap()
76+
val transformations = (TID zip (
77+
(Rotation.values() zip List(Rotation.values().size) {false}) +
78+
(Rotation.values() zip List(Rotation.values().size) {true})
79+
)).toMap()
80+
val IS = transformations.map {
81+
it.key to shape.transform(it.value.first, it.value.second)
82+
}.toMap()
83+
84+
TID.forEach {
85+
try {
86+
IS[it] shouldBe SHOULD[it]
87+
} catch (e: AssertionFailedError) {
88+
println("Expected: $it Actual:")
89+
printShapes(SHOULD.getValue(it), IS.getValue(it))
90+
throw e
91+
}
92+
}
5093
}
5194
"Piece coordination calculation" {
5295
val position = Coordinates(2, 2)

0 commit comments

Comments
 (0)