Skip to content

Commit 5c7cd6a

Browse files
committed
Created math test units
1 parent 5166dec commit 5c7cd6a

File tree

3 files changed

+513
-0
lines changed

3 files changed

+513
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import com.lambda.util.math.Vec2d
2+
import com.lambda.util.math.coerceIn
3+
import com.lambda.util.math.inv
4+
import com.lambda.util.math.lerp
5+
import com.lambda.util.math.normalize
6+
import com.lambda.util.math.random
7+
import com.lambda.util.math.step
8+
import com.lambda.util.math.transform
9+
import net.minecraft.util.math.Vec3d
10+
import java.awt.Color
11+
import kotlin.test.Test
12+
import kotlin.test.assertEquals
13+
import kotlin.test.assertTrue
14+
15+
/*
16+
* Copyright 2025 Lambda
17+
*
18+
* This program is free software: you can redistribute it and/or modify
19+
* it under the terms of the GNU General Public License as published by
20+
* the Free Software Foundation, either version 3 of the License, or
21+
* (at your option) any later version.
22+
*
23+
* This program is distributed in the hope that it will be useful,
24+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
25+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26+
* GNU General Public License for more details.
27+
*
28+
* You should have received a copy of the GNU General Public License
29+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
30+
*/
31+
32+
class RangeTest {
33+
34+
@Test
35+
fun `test step over double range`() {
36+
val range = 0.0..10.0
37+
val iterator = range.step(2.0)
38+
39+
val result = iterator.asSequence().toList()
40+
assertEquals(listOf(0.0, 2.0, 4.0, 6.0, 8.0, 10.0), result)
41+
}
42+
43+
@Test
44+
fun `test step over float range`() {
45+
val range = 0.0f..10.0f
46+
val iterator = range.step(2.0f)
47+
48+
val result = iterator.asSequence().toList()
49+
assertEquals(listOf(0.0f, 2.0f, 4.0f, 6.0f, 8.0f, 10.0f), result)
50+
}
51+
52+
@Test
53+
fun `test random within range`() {
54+
val range = 0.0..10.0
55+
val randomValue = range.random()
56+
57+
assertTrue(randomValue in range) // The value must be in the range [0.0, 10.0]
58+
}
59+
60+
@Test
61+
fun `test normalize double value`() {
62+
val range = 0.0..100.0
63+
val normalized = range.normalize(50.0)
64+
65+
assertEquals(0.5, normalized) // 50.0 should be normalized to 0.5 in the range [0.0, 100.0]
66+
}
67+
68+
@Test
69+
fun `test normalize float value`() {
70+
val range = 0f..100f
71+
val normalized = range.normalize(50f)
72+
73+
assertEquals(0.5f, normalized) // 50f should be normalized to 0.5f in the range [0f, 100f]
74+
}
75+
76+
@Test
77+
fun `test invert float range`() {
78+
val range = 0f..100f
79+
val inverted = range.inv()
80+
81+
assertEquals(100f to 0f, inverted) // Inverting the range [0f, 100f] gives (100f, 0f)
82+
}
83+
84+
@Test
85+
fun `test transform double range`() {
86+
val range = 0.0..10.0
87+
val transformed = range.transform(5.0, 0.0, 100.0)
88+
89+
assertEquals(50.0, transformed) // 5.0 in range [0.0, 10.0] maps to 50.0 in range [0.0, 100.0]
90+
}
91+
92+
@Test
93+
fun `test transform float range`() {
94+
val range = 0f..10f
95+
val transformed = range.transform(5f, 0f, 100f)
96+
97+
assertEquals(50f, transformed) // 5f in range [0f, 10f] maps to 50f in range [0f, 100f]
98+
}
99+
100+
@Test
101+
fun `test lerp double values`() {
102+
val lerpedValue = lerp(0.5, 0.0, 10.0)
103+
104+
assertEquals(5.0, lerpedValue) // Linear interpolation between 0.0 and 10.0 at 0.5 results in 5.0
105+
}
106+
107+
@Test
108+
fun `test lerp float values`() {
109+
val lerpedValue = lerp(0.5f, 0f, 10f)
110+
111+
assertEquals(5.0f, lerpedValue) // Linear interpolation between 0f and 10f at 0.5 results in 5.0f
112+
}
113+
114+
@Test
115+
fun `test lerp Vec2d`() {
116+
val start = Vec2d(0.0, 0.0)
117+
val end = Vec2d(10.0, 10.0)
118+
val lerpedValue = lerp(0.5, start, end)
119+
120+
assertEquals(Vec2d(5.0, 5.0), lerpedValue) // Interpolated 50% between (0, 0) and (10, 10)
121+
}
122+
123+
@Test
124+
fun `test lerp Vec3d`() {
125+
val start = Vec3d(0.0, 0.0, 0.0)
126+
val end = Vec3d(10.0, 10.0, 10.0)
127+
val lerpedValue = lerp(0.5, start, end)
128+
129+
assertEquals(Vec3d(5.0, 5.0, 5.0), lerpedValue) // Interpolated 50% between (0, 0, 0) and (10, 10, 10)
130+
}
131+
132+
@Test
133+
fun `test lerp Color`() {
134+
val start = Color(255, 0, 0) // Red
135+
val end = Color(0, 0, 255) // Blue
136+
val lerpedValue = lerp(0.5, start, end)
137+
138+
assertEquals(Color(128, 0, 128), lerpedValue) // Interpolated color should be purple
139+
}
140+
141+
@Test
142+
fun `test coercing value in double range`() {
143+
val range = 0.0..10.0
144+
val coercedValue = range.coerceIn(15.0)
145+
146+
assertEquals(10.0, coercedValue) // Coerced value should be within the range [0.0, 10.0]
147+
}
148+
149+
@Test
150+
fun `test coercing value in float range`() {
151+
val range = 0f..10f
152+
val coercedValue = range.coerceIn(15f)
153+
154+
assertEquals(10f, coercedValue) // Coerced value should be within the range [0f, 10f]
155+
}
156+
157+
@Test
158+
fun `test coercing value in 2d vector`() {
159+
val vec = Vec2d(5.0, 5.0)
160+
val coercedVec = vec.coerceIn(0.0, 10.0, 0.0, 10.0)
161+
162+
assertEquals(Vec2d(5.0, 5.0), coercedVec) // Vec should stay the same
163+
}
164+
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import com.lambda.util.math.Vec2d
2+
import kotlin.test.Test
3+
import kotlin.test.assertEquals
4+
5+
/*
6+
* Copyright 2025 Lambda
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
class Vec2dTest {
23+
@Test
24+
fun `test unary minus`() {
25+
val vector = Vec2d(1.0, -2.0)
26+
val result = -vector
27+
28+
assertEquals(Vec2d(-1.0, 2.0), result)
29+
}
30+
31+
@Test
32+
fun `test addition with another Vec2d`() {
33+
val vector1 = Vec2d(1.0, 2.0)
34+
val vector2 = Vec2d(3.0, 4.0)
35+
val result = vector1 + vector2
36+
37+
assertEquals(Vec2d(4.0, 6.0), result)
38+
}
39+
40+
@Test
41+
fun `test addition with scalar (Double)`() {
42+
val vector = Vec2d(1.0, 2.0)
43+
val result = vector + 3.0
44+
45+
assertEquals(Vec2d(4.0, 5.0), result)
46+
}
47+
48+
@Test
49+
fun `test addition with scalar (Float)`() {
50+
val vector = Vec2d(1.0, 2.0)
51+
val result = vector + 3.0f
52+
53+
assertEquals(Vec2d(4.0, 5.0), result)
54+
}
55+
56+
@Test
57+
fun `test addition with scalar (Int)`() {
58+
val vector = Vec2d(1.0, 2.0)
59+
val result = vector + 3
60+
61+
assertEquals(Vec2d(4.0, 5.0), result)
62+
}
63+
64+
@Test
65+
fun `test subtraction with another Vec2d`() {
66+
val vector1 = Vec2d(5.0, 6.0)
67+
val vector2 = Vec2d(3.0, 4.0)
68+
val result = vector1 - vector2
69+
70+
assertEquals(Vec2d(2.0, 2.0), result)
71+
}
72+
73+
@Test
74+
fun `test subtraction with scalar (Double)`() {
75+
val vector = Vec2d(5.0, 6.0)
76+
val result = vector - 2.0
77+
78+
assertEquals(Vec2d(3.0, 4.0), result)
79+
}
80+
81+
@Test
82+
fun `test subtraction with scalar (Float)`() {
83+
val vector = Vec2d(5.0, 6.0)
84+
val result = vector - 2.0f
85+
86+
assertEquals(Vec2d(3.0, 4.0), result)
87+
}
88+
89+
@Test
90+
fun `test subtraction with scalar (Int)`() {
91+
val vector = Vec2d(5.0, 6.0)
92+
val result = vector - 2
93+
94+
assertEquals(Vec2d(3.0, 4.0), result)
95+
}
96+
97+
@Test
98+
fun `test multiplication with scalar (Double)`() {
99+
val vector = Vec2d(2.0, 3.0)
100+
val result = vector * 2.0
101+
102+
assertEquals(Vec2d(4.0, 6.0), result)
103+
}
104+
105+
@Test
106+
fun `test multiplication with scalar (Float)`() {
107+
val vector = Vec2d(2.0, 3.0)
108+
val result = vector * 2.0f
109+
110+
assertEquals(Vec2d(4.0, 6.0), result)
111+
}
112+
113+
@Test
114+
fun `test multiplication with scalar (Int)`() {
115+
val vector = Vec2d(2.0, 3.0)
116+
val result = vector * 2
117+
118+
assertEquals(Vec2d(4.0, 6.0), result)
119+
}
120+
121+
@Test
122+
fun `test multiplication with another Vec2d`() {
123+
val vector1 = Vec2d(2.0, 3.0)
124+
val vector2 = Vec2d(4.0, 5.0)
125+
val result = vector1 * vector2
126+
127+
assertEquals(Vec2d(8.0, 15.0), result)
128+
}
129+
130+
@Test
131+
fun `test division with scalar (Double)`() {
132+
val vector = Vec2d(6.0, 8.0)
133+
val result = vector / 2.0
134+
135+
assertEquals(Vec2d(3.0, 4.0), result)
136+
}
137+
138+
@Test
139+
fun `test division with scalar (Float)`() {
140+
val vector = Vec2d(6.0, 8.0)
141+
val result = vector / 2.0f
142+
143+
assertEquals(Vec2d(3.0, 4.0), result)
144+
}
145+
146+
@Test
147+
fun `test division with scalar (Int)`() {
148+
val vector = Vec2d(6.0, 8.0)
149+
val result = vector / 2
150+
151+
assertEquals(Vec2d(3.0, 4.0), result)
152+
}
153+
154+
@Test
155+
fun `test division with another Vec2d`() {
156+
val vector1 = Vec2d(6.0, 8.0)
157+
val vector2 = Vec2d(2.0, 4.0)
158+
val result = vector1 / vector2
159+
160+
assertEquals(Vec2d(3.0, 2.0), result)
161+
}
162+
163+
@Test
164+
fun `test round to Int`() {
165+
val vector = Vec2d(3.5, 4.5)
166+
val result = vector.roundToInt()
167+
168+
assertEquals(Vec2d(4.0, 5.0), result)
169+
}
170+
171+
@Test
172+
fun `test Vec2d constants`() {
173+
assertEquals(Vec2d.ZERO, Vec2d(0.0, 0.0))
174+
assertEquals(Vec2d.ONE, Vec2d(1.0, 1.0))
175+
assertEquals(Vec2d.LEFT, Vec2d(-1.0, 0.0))
176+
assertEquals(Vec2d.RIGHT, Vec2d(1.0, 0.0))
177+
assertEquals(Vec2d.TOP, Vec2d(0.0, -1.0))
178+
assertEquals(Vec2d.BOTTOM, Vec2d(0.0, 1.0))
179+
}
180+
}

0 commit comments

Comments
 (0)