main
parent
fa654e4ce8
commit
1444be1ee4
@ -1,6 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
<file type="web" url="file://$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="temurin-11" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
|
@ -0,0 +1,49 @@
|
||||
package technology.zim.util
|
||||
|
||||
import kotlin.random.Random
|
||||
|
||||
class TrimmableArrayList<E> {
|
||||
val data = ArrayList<E>()
|
||||
|
||||
fun addAll(c: Collection<E>) {
|
||||
data.addAll(c)
|
||||
}
|
||||
|
||||
fun isNotEmpty(): Boolean {
|
||||
return data.isNotEmpty()
|
||||
}
|
||||
|
||||
fun isEmpty(): Boolean {
|
||||
return data.isEmpty()
|
||||
}
|
||||
|
||||
fun add(item: E) {
|
||||
data.add(item)
|
||||
|
||||
}
|
||||
|
||||
fun size():Int {
|
||||
return data.size
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
data.clear()
|
||||
}
|
||||
|
||||
fun removeAt(index: Int) {
|
||||
data[index] = data[data.size - 1]
|
||||
data.removeLast()
|
||||
}
|
||||
|
||||
//O(n) remove operation
|
||||
fun remove(item: E) {
|
||||
data[data.indexOf(item)] = data[data.size - 1]
|
||||
data.removeLast()
|
||||
}
|
||||
|
||||
|
||||
fun random(): E {
|
||||
return data[Random.nextInt(data.size)]
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
import technology.zim.MazeFinder
|
||||
import technology.zim.World
|
||||
import technology.zim.data.Tile
|
||||
import kotlin.test.BeforeTest
|
||||
|
||||
class MazeFinderTest {
|
||||
|
||||
@BeforeTest
|
||||
fun setup() {
|
||||
World.setSize(10, 10)
|
||||
MazeFinder.primsAlgorithm()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun topRowConnectedSouth() {
|
||||
var southExists = false
|
||||
World.tiles.value.forEach {
|
||||
col ->
|
||||
if(col[0].isSouth())
|
||||
southExists = true
|
||||
}
|
||||
assert(southExists)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun bottomRowConnectedSouth() {
|
||||
var southNotExists = true
|
||||
World.tiles.value.forEach {
|
||||
col ->
|
||||
if(col[9].isSouth())
|
||||
southNotExists = false
|
||||
}
|
||||
assert(southNotExists)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun allTilesVisited() {
|
||||
World.tiles.value.forEach {
|
||||
col ->
|
||||
col.forEach {
|
||||
tile ->
|
||||
assert(tile.visited)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun allTilesHaveConnections() {
|
||||
World.tiles.value.forEachIndexed {
|
||||
x,
|
||||
col ->
|
||||
col.forEachIndexed { y,
|
||||
tileprop ->
|
||||
if(tileprop.connections.isEmpty())
|
||||
println("Empty Connections at: " + Tile(x, y).toString())
|
||||
assert(tileprop.connections.isNotEmpty())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isCleanedUp() {
|
||||
MazeFinder.cleanUp()
|
||||
|
||||
assert(MazeFinder.frontier.isEmpty())
|
||||
}
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.BeforeTest
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
import org.junit.jupiter.api.TestFactory
|
||||
import technology.zim.MazeFinder
|
||||
import technology.zim.World
|
||||
import technology.zim.data.*
|
||||
import technology.zim.data.Directions.*
|
||||
|
||||
class TileTest {
|
||||
@BeforeTest
|
||||
fun setup() {
|
||||
World.setSize(10, 10)
|
||||
MazeFinder.cleanUp()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun connectBottomToSouthInvalid() {
|
||||
val bottom = Tile(1, 9)
|
||||
bottom.connect(SOUTH)
|
||||
assertFalse(bottom.getConnections().contains(bottom+SOUTH))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun outOfBoundsTest() {
|
||||
val tooNorth = Tile(1, -1)
|
||||
val tooWest = Tile(-1, 1)
|
||||
val tooEast = Tile(10, 1)
|
||||
val tooSouth = Tile(1, 10)
|
||||
|
||||
val northIn = Tile(0, 0)
|
||||
val southIn = Tile(9, 9)
|
||||
|
||||
assert(northIn.isInBounds())
|
||||
assert(southIn.isInBounds())
|
||||
|
||||
assertFalse(tooNorth.isInBounds())
|
||||
assertFalse(tooWest.isInBounds())
|
||||
assertFalse(tooEast.isInBounds())
|
||||
assertFalse(tooSouth.isInBounds())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun allDirectionsTest() {
|
||||
val dirs = NORTH.all()
|
||||
assert(dirs.containsAll(listOf(NORTH, EAST, SOUTH, WEST)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun connectNorthSouthTest() {
|
||||
//Assumes the world is at least 10, 10 in size as set up by this test class
|
||||
val someTile = Tile(5, 5)
|
||||
someTile.connect(NORTH)
|
||||
val northTile = someTile + NORTH
|
||||
|
||||
assert(someTile.getProperties().isNorth())
|
||||
assertFalse(someTile.getProperties().isSouth())
|
||||
assertFalse(someTile.getProperties().isWest())
|
||||
assertFalse(someTile.getProperties().isEast())
|
||||
|
||||
assert(northTile.getProperties().isSouth())
|
||||
assertFalse(northTile.getProperties().isNorth())
|
||||
assertFalse(northTile.getProperties().isWest())
|
||||
assertFalse(northTile.getProperties().isEast())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun adjacentTest() {
|
||||
val someTile = Tile(1, 1)
|
||||
val northTile = someTile + NORTH
|
||||
val southTile = someTile + SOUTH
|
||||
val westTile = someTile + WEST
|
||||
val eastTile = someTile + EAST
|
||||
|
||||
//Generating raw tiles without built-in bounds checking, do it here
|
||||
if(northTile.isInBounds())
|
||||
northTile.getProperties().visited = true
|
||||
if(southTile.isInBounds())
|
||||
southTile.getProperties().visited = true
|
||||
|
||||
|
||||
val adjacent = someTile.getAdjacentTiles(false)
|
||||
assertFalse(adjacent.contains(northTile))
|
||||
assertFalse(adjacent.contains(southTile))
|
||||
assert(adjacent.contains(eastTile))
|
||||
assert(adjacent.contains(westTile))
|
||||
|
||||
val explored = someTile.getAdjacentTiles(true)
|
||||
assert(explored.elementAt(0) == northTile)
|
||||
|
||||
assert(explored.contains(northTile))
|
||||
assert(explored.contains(southTile))
|
||||
assertFalse(explored.contains(eastTile))
|
||||
assertFalse(explored.contains(westTile))
|
||||
|
||||
adjacent.forEach {
|
||||
it ->
|
||||
println(it.toString())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
import technology.zim.data.Tile
|
||||
import technology.zim.util.TrimmableArrayList
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
|
||||
class TrimmableArrayListTest {
|
||||
|
||||
@Test
|
||||
fun emptyTest() {
|
||||
val empty = TrimmableArrayList<Tile>()
|
||||
assert(empty.isEmpty())
|
||||
|
||||
empty.add(Tile(0, 0))
|
||||
empty.remove(Tile(0, 0))
|
||||
assert(empty.isEmpty())
|
||||
|
||||
empty.addAll(listOf(Tile(0, 0), Tile(1, 1), Tile(2, 2)))
|
||||
assert(empty.isNotEmpty())
|
||||
|
||||
empty.remove(Tile(0, 0))
|
||||
println("realSize: " + empty.size() + " Size: " + empty.data.size)
|
||||
assert(empty.size() == 2)
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in new issue