Skip to content

Commit d6e9939

Browse files
Duy PhạmDuy Phạm
authored andcommitted
Add unit test for W3WRectangle id generator
1 parent 5c6377b commit d6e9939

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
package com.what3words.map.components.compose.extensions
2+
3+
import com.what3words.components.compose.maps.extensions.id
4+
import com.what3words.core.types.geometry.W3WCoordinates
5+
import com.what3words.core.types.geometry.W3WRectangle
6+
import org.junit.Assert.assertEquals
7+
import org.junit.Assert.assertNotEquals
8+
import org.junit.Test
9+
10+
class W3WRectangleIdTest {
11+
12+
@Test
13+
fun `should generate unique IDs for different rectangles`() {
14+
val rect1 = W3WRectangle(
15+
southwest = W3WCoordinates(10.780078, 106.705424),
16+
northeast = W3WCoordinates(10.780105, 106.705451)
17+
)
18+
val rect2 = W3WRectangle(
19+
southwest = W3WCoordinates(10.780590, 106.705424),
20+
northeast = W3WCoordinates(10.780617, 106.705451)
21+
)
22+
23+
val id1 = rect1.id
24+
val id2 = rect2.id
25+
26+
assertNotEquals("Different rectangles should have different IDs", id1, id2)
27+
}
28+
29+
@Test
30+
fun `should generate same ID for identical rectangles`() {
31+
val rect1 = W3WRectangle(
32+
southwest = W3WCoordinates(10.780078, 106.705424),
33+
northeast = W3WCoordinates(10.780105, 106.705451)
34+
)
35+
val rect2 = W3WRectangle(
36+
southwest = W3WCoordinates(10.780078, 106.705424),
37+
northeast = W3WCoordinates(10.780105, 106.705451)
38+
)
39+
40+
val id1 = rect1.id
41+
val id2 = rect2.id
42+
43+
assertEquals("Identical rectangles should have the same ID", id1, id2)
44+
}
45+
46+
@Test
47+
fun `should generate different IDs for previously colliding rectangles - case 1`() {
48+
// These two rectangles previously generated the same ID: 9222864837722059307
49+
val rect1 = W3WRectangle(
50+
southwest = W3WCoordinates(10.780078, 106.705424),
51+
northeast = W3WCoordinates(10.780105, 106.705451)
52+
)
53+
val rect2 = W3WRectangle(
54+
southwest = W3WCoordinates(10.780590, 106.705424),
55+
northeast = W3WCoordinates(10.780617, 106.705451)
56+
)
57+
58+
val id1 = rect1.id
59+
val id2 = rect2.id
60+
61+
assertNotEquals("Previously colliding rectangles (case 1) should now have different IDs", id1, id2)
62+
}
63+
64+
@Test
65+
fun `should generate different IDs for previously colliding rectangles - case 2`() {
66+
// These two rectangles previously generated the same ID: 9214139332489523810
67+
val rect1 = W3WRectangle(
68+
southwest = W3WCoordinates(10.780051, 106.705479),
69+
northeast = W3WCoordinates(10.780078, 106.705506)
70+
)
71+
val rect2 = W3WRectangle(
72+
southwest = W3WCoordinates(10.780563, 106.705479),
73+
northeast = W3WCoordinates(10.780590, 106.705506)
74+
)
75+
76+
val id1 = rect1.id
77+
val id2 = rect2.id
78+
79+
assertNotEquals("Previously colliding rectangles (case 2) should now have different IDs", id1, id2)
80+
}
81+
82+
@Test
83+
fun `should generate different IDs for rectangles with very small coordinate differences`() {
84+
val rect1 = W3WRectangle(
85+
southwest = W3WCoordinates(54.672341, 89.467876),
86+
northeast = W3WCoordinates(54.672351, 89.467886)
87+
)
88+
val rect2 = W3WRectangle(
89+
southwest = W3WCoordinates(54.672342, 89.467875),
90+
northeast = W3WCoordinates(54.672352, 89.467885)
91+
)
92+
93+
val id1 = rect1.id
94+
val id2 = rect2.id
95+
96+
assertNotEquals("Rectangles with micro-degree differences should have different IDs", id1, id2)
97+
}
98+
99+
@Test
100+
fun `should generate different IDs when sw and ne are swapped`() {
101+
val rect1 = W3WRectangle(
102+
southwest = W3WCoordinates(10.0, 100.0),
103+
northeast = W3WCoordinates(10.001, 100.001)
104+
)
105+
val rect2 = W3WRectangle(
106+
southwest = W3WCoordinates(10.001, 100.001),
107+
northeast = W3WCoordinates(10.0, 100.0)
108+
)
109+
110+
val id1 = rect1.id
111+
val id2 = rect2.id
112+
113+
assertNotEquals("Swapped coordinates should produce different IDs", id1, id2)
114+
}
115+
116+
@Test
117+
fun `should generate different IDs for adjacent grid squares`() {
118+
// Simulating adjacent w3w grid squares (3m x 3m approximately)
119+
val rect1 = W3WRectangle(
120+
southwest = W3WCoordinates(51.520847, -0.195521),
121+
northeast = W3WCoordinates(51.520870, -0.195497)
122+
)
123+
val rect2 = W3WRectangle(
124+
southwest = W3WCoordinates(51.520870, -0.195521),
125+
northeast = W3WCoordinates(51.520893, -0.195497)
126+
)
127+
128+
val id1 = rect1.id
129+
val id2 = rect2.id
130+
131+
assertNotEquals("Adjacent grid squares should have different IDs", id1, id2)
132+
}
133+
134+
@Test(expected = IllegalArgumentException::class)
135+
fun `should throw exception for invalid latitude above range`() {
136+
val rect = W3WRectangle(
137+
southwest = W3WCoordinates(91.0, 100.0),
138+
northeast = W3WCoordinates(10.0, 100.0)
139+
)
140+
rect.id
141+
}
142+
143+
@Test(expected = IllegalArgumentException::class)
144+
fun `should throw exception for invalid latitude below range`() {
145+
val rect = W3WRectangle(
146+
southwest = W3WCoordinates(-91.0, 100.0),
147+
northeast = W3WCoordinates(10.0, 100.0)
148+
)
149+
rect.id
150+
}
151+
152+
@Test(expected = IllegalArgumentException::class)
153+
fun `should throw exception for invalid longitude above range`() {
154+
val rect = W3WRectangle(
155+
southwest = W3WCoordinates(10.0, 181.0),
156+
northeast = W3WCoordinates(10.0, 100.0)
157+
)
158+
rect.id
159+
}
160+
161+
@Test(expected = IllegalArgumentException::class)
162+
fun `should throw exception for invalid longitude below range`() {
163+
val rect = W3WRectangle(
164+
southwest = W3WCoordinates(10.0, -181.0),
165+
northeast = W3WCoordinates(10.0, 100.0)
166+
)
167+
rect.id
168+
}
169+
170+
@Test
171+
fun `should generate consistent IDs across multiple calls`() {
172+
val rect = W3WRectangle(
173+
southwest = W3WCoordinates(10.780078, 106.705424),
174+
northeast = W3WCoordinates(10.780105, 106.705451)
175+
)
176+
177+
val ids = (1..100).map { rect.id }
178+
val uniqueIds = ids.toSet()
179+
180+
assertEquals("ID should be consistent across multiple calls", 1, uniqueIds.size)
181+
}
182+
183+
@Test
184+
fun `should generate unique IDs for large batch of random rectangles`() {
185+
val rectangles = (1..1000).map { index ->
186+
val lat = -90.0 + (index % 180) + (index * 0.000001)
187+
val lng = -180.0 + (index % 360) + (index * 0.000001)
188+
W3WRectangle(
189+
southwest = W3WCoordinates(lat, lng),
190+
northeast = W3WCoordinates(lat + 0.00003, lng + 0.00003)
191+
)
192+
}
193+
194+
val ids = rectangles.map { it.id }
195+
val uniqueIds = ids.toSet()
196+
197+
assertEquals("All ${rectangles.size} rectangles should have unique IDs", rectangles.size, uniqueIds.size)
198+
}
199+
}

0 commit comments

Comments
 (0)