Skip to content

Commit 1258a6a

Browse files
#85: Added test unit for testing ,math.calcNewBotSpeed()
1 parent 5d3f76f commit 1258a6a

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

server/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dependencies {
3434
implementation(libs.gson)
3535

3636
testImplementation(libs.kotest.junit5)
37+
testImplementation(libs.kotest.datatest)
3738
testImplementation(libs.mockk)
3839
}
3940

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package rules
2+
3+
import dev.robocode.tankroyale.server.rules.calcNewBotSpeed
4+
import io.kotest.core.spec.style.FunSpec
5+
import io.kotest.datatest.withData
6+
import io.kotest.matchers.shouldBe
7+
8+
9+
class mathTest : FunSpec({
10+
11+
context("calcNewBotSpeed") {
12+
withData(
13+
mapOf(
14+
// When starting speed is 0 with various positive target speeds, the new speed should not exceed the
15+
// positive acceleration of 1
16+
"current speed = 0, target speed = 0 => new speed = 0" to arrayOf(0.0, 0.0, 0.0),
17+
"current speed = 0, target speed = 0.5 => new speed = 0.5" to arrayOf(0.0, 0.5, 0.5),
18+
"current speed = 0, target speed = 1 => new speed = 1" to arrayOf(0.0, 1.0, 1.0),
19+
// New speed cannot pass the acceleration of 1
20+
"current speed = 0, target speed = 2 => new speed = 1" to arrayOf(0.0, 2.0, 1.0),
21+
"current speed = 0, target speed = 8 => new speed = 1" to arrayOf(0.0, 8.0, 1.0),
22+
"current speed = 0, target speed = 9 => new speed = 1" to arrayOf(0.0, 9.0, 1.0),
23+
24+
// When starting speed is 0 with various negative target speeds, the new speed should not exceed the
25+
// negative acceleration of 1
26+
"current speed = 0, target speed = -0.5 => new speed = -0.5" to arrayOf(0.0, -0.5, -0.5),
27+
"current speed = 0, target speed = -1 => new speed = -1" to arrayOf(0.0, -1.0, -1.0),
28+
"current speed = 0, target speed = -2 => new speed = -1" to arrayOf(0.0, -2.0, -1.0),
29+
// New speed cannot pass the acceleration of -1
30+
"current speed = 0, target speed = -8 => new speed = -1" to arrayOf(0.0, -8.0, -1.0),
31+
"current speed = 0, target speed = -9 => new speed = -1" to arrayOf(0.0, -9.0, -1.0),
32+
33+
// Decelerating from max speed should allow deceleration of down to -2
34+
"current speed = 8, target speed = 7 => new speed = 7" to arrayOf(8.0, 7.0, 7.0),
35+
"current speed = 8, target speed = 6 => new speed = 6" to arrayOf(8.0, 6.0, 6.0),
36+
// new speed cannot pass the deceleration of -2
37+
"current speed = 8, target speed = 5 => new speed = 6" to arrayOf(8.0, 5.0, 6.0),
38+
39+
// Decelerating from min speed should allow deceleration of up to 2
40+
"current speed = -8, target speed = -7 => new speed = -7" to arrayOf(-8.0, -7.0, -7.0),
41+
"current speed = -8, target speed = -6 => new speed = -6" to arrayOf(-8.0, -6.0, -6.0),
42+
// new speed cannot pass the deceleration of -2
43+
"current speed = -8, target speed = -5 => new speed = -6" to arrayOf(-8.0, -5.0, -6.0),
44+
45+
// Speed crossing a positive to negative speed (original Robocode)
46+
"current speed = 1, target speed = -8 => new speed = -0.5" to arrayOf(1.0, -8.0, -0.5),
47+
"current speed = 0.5, target speed = -8 => new speed = -0.75" to arrayOf(0.5, -8.0, -0.75),
48+
49+
// Speed crossing a negative to positive speed (original Robocode)
50+
"current speed = -1, target speed = 8 => new speed = 0.5" to arrayOf(-1.0, 8.0, 0.5),
51+
"current speed = -0.5, target speed = 8 => new speed = 0.75" to arrayOf(-0.5, 8.0, 0.75),
52+
)
53+
)
54+
{ (currentSpeed, targetSpeed, newSpeed) ->
55+
calcNewBotSpeed(currentSpeed, targetSpeed) shouldBe newSpeed
56+
}
57+
}
58+
})

settings.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ val tankroyaleVersion: String = providers.gradleProperty("version").get()
2727

2828
val kotlinVersion = "1.9.21"
2929
val junitVersion = "5.10.2"
30+
val kotestVersion = "5.8.1"
3031

3132
// Check dependencies with this command: gradle dependencyUpdates
3233

@@ -55,7 +56,8 @@ dependencyResolutionManagement {
5556
library("system-stubs", "uk.org.webcompere:system-stubs-jupiter:2.1.6")
5657

5758
// Kotlin testing
58-
library("kotest-junit5", "io.kotest:kotest-runner-junit5-jvm:5.8.1")
59+
library("kotest-junit5", "io.kotest:kotest-runner-junit5-jvm:$kotestVersion")
60+
library("kotest-datatest", "io.kotest:kotest-framework-datatest:$kotestVersion")
5961
library("mockk", "io.mockk:mockk:1.13.10")
6062

6163
// plugins

0 commit comments

Comments
 (0)