|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
4 | 4 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
5 | 6 | import org.junit.jupiter.api.Test; |
6 | 7 |
|
7 | 8 | class KMeansTest { |
8 | 9 |
|
9 | 10 | @Test |
10 | 11 | void testSimpleClustering() { |
11 | 12 |
|
12 | | - double[][] points = { |
13 | | - {1.0, 1.0}, |
14 | | - {1.2, 1.1}, |
15 | | - {8.0, 8.0}, |
16 | | - {8.2, 8.1} |
17 | | - }; |
| 13 | + double[][] points = {{1.0, 1.0}, {1.2, 1.1}, {8.0, 8.0}, {8.2, 8.1}}; |
18 | 14 |
|
19 | | - double[][] centroids = { |
20 | | - {1.0, 1.0}, |
21 | | - {8.0, 8.0} |
22 | | - }; |
| 15 | + double[][] centroids = {{1.0, 1.0}, {8.0, 8.0}}; |
23 | 16 |
|
24 | 17 | int[] expected = {0, 0, 1, 1}; |
25 | 18 |
|
26 | | - assertArrayEquals( |
27 | | - expected, |
28 | | - KMeans.cluster(points, centroids, 100, 0.0001) |
29 | | - ); |
| 19 | + assertArrayEquals(expected, KMeans.cluster(points, centroids, 100, 0.0001)); |
30 | 20 | } |
31 | 21 |
|
32 | 22 | @Test |
33 | 23 | void testNullCentroids() { |
34 | | - double[][] points = { |
35 | | - {1.0, 1.0} |
36 | | - }; |
37 | | - |
38 | | - assertThrows( |
39 | | - IllegalArgumentException.class, |
40 | | - () -> KMeans.cluster(points, null, 100, 0.0001) |
41 | | - ); |
| 24 | + double[][] points = {{1.0, 1.0}}; |
42 | 25 |
|
| 26 | + assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, null, 100, 0.0001)); |
43 | 27 | } |
44 | 28 |
|
45 | 29 | @Test |
46 | 30 | void testEmptyDataset() { |
47 | 31 | double[][] points = {}; |
48 | | - double[][] centroids = { |
49 | | - {1.0, 1.0} |
50 | | - }; |
51 | | - |
52 | | - assertThrows( |
53 | | - IllegalArgumentException.class, |
54 | | - () -> KMeans.cluster(points, centroids, 100, 0.0001) |
55 | | - ); |
| 32 | + double[][] centroids = {{1.0, 1.0}}; |
| 33 | + |
| 34 | + assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, 0.0001)); |
56 | 35 | } |
57 | 36 |
|
58 | 37 | @Test |
59 | 38 | void testEmptyPoints() { |
60 | 39 | double[][] points = {}; |
61 | | - double[][] centroids = { |
62 | | - {1.0, 1.0} |
63 | | - }; |
64 | | - |
65 | | - assertThrows( |
66 | | - IllegalArgumentException.class, |
67 | | - () -> KMeans.cluster(points, centroids, 100, 0.0001) |
68 | | - ); |
| 40 | + double[][] centroids = {{1.0, 1.0}}; |
| 41 | + |
| 42 | + assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, 0.0001)); |
69 | 43 | } |
70 | 44 |
|
71 | 45 | @Test |
72 | 46 | void testNoCentroids() { |
73 | | - double[][] points = { |
74 | | - {1.0, 1.0} |
75 | | - }; |
| 47 | + double[][] points = {{1.0, 1.0}}; |
76 | 48 |
|
77 | 49 | double[][] centroids = {}; |
78 | 50 |
|
79 | | - assertThrows( |
80 | | - IllegalArgumentException.class, |
81 | | - () -> KMeans.cluster(points, centroids, 100, 0.0001) |
82 | | - ); |
| 51 | + assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, 0.0001)); |
83 | 52 | } |
84 | 53 |
|
85 | 54 | @Test |
86 | 55 | void testNonPositiveMaxIterations() { |
87 | | - double[][] points = { |
88 | | - {1.0, 1.0} |
89 | | - }; |
90 | | - |
91 | | - double[][] centroids = { |
92 | | - {1.0, 1.0} |
93 | | - }; |
94 | | - |
95 | | - assertThrows( |
96 | | - IllegalArgumentException.class, |
97 | | - () -> KMeans.cluster(points, centroids, 0, 0.0001) |
98 | | - ); |
| 56 | + double[][] points = {{1.0, 1.0}}; |
| 57 | + |
| 58 | + double[][] centroids = {{1.0, 1.0}}; |
| 59 | + |
| 60 | + assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 0, 0.0001)); |
99 | 61 | } |
100 | 62 |
|
101 | 63 | @Test |
102 | 64 | void testNegativeTolerance() { |
103 | | - double[][] points = { |
104 | | - {1.0, 1.0} |
105 | | - }; |
106 | | - double[][] centroids = { |
107 | | - {1.0, 1.0} |
108 | | - }; |
109 | | - |
110 | | - assertThrows( |
111 | | - IllegalArgumentException.class, |
112 | | - () -> KMeans.cluster(points, centroids, 100, -1.0) |
113 | | - ); |
| 65 | + double[][] points = {{1.0, 1.0}}; |
| 66 | + double[][] centroids = {{1.0, 1.0}}; |
| 67 | + |
| 68 | + assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, -1.0)); |
114 | 69 | } |
115 | 70 |
|
116 | 71 | @Test |
117 | 72 | void testTooManyCentroids() { |
118 | | - double[][] points = { |
119 | | - {1.0, 1.0} |
120 | | - }; |
121 | | - |
122 | | - double[][] centroids = { |
123 | | - {1.0, 1.0}, |
124 | | - {2.0, 2.0} |
125 | | - }; |
126 | | - |
127 | | - assertThrows( |
128 | | - IllegalArgumentException.class, |
129 | | - () -> KMeans.cluster(points, centroids, 100, 0.0001) |
130 | | - ); |
| 73 | + double[][] points = {{1.0, 1.0}}; |
| 74 | + |
| 75 | + double[][] centroids = {{1.0, 1.0}, {2.0, 2.0}}; |
| 76 | + |
| 77 | + assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, 0.0001)); |
131 | 78 | } |
132 | 79 |
|
133 | 80 | @Test |
134 | 81 | void testDimensionMismatchInPoints() { |
135 | | - double[][] points = { |
136 | | - {1.0, 1.0}, |
137 | | - {2.0} |
138 | | - }; |
139 | | - |
140 | | - double[][] centroids = { |
141 | | - {1.0, 1.0} |
142 | | - }; |
143 | | - |
144 | | - assertThrows( |
145 | | - IllegalArgumentException.class, |
146 | | - () -> KMeans.cluster(points, centroids, 100, 0.0001) |
147 | | - ); |
| 82 | + double[][] points = {{1.0, 1.0}, {2.0}}; |
| 83 | + |
| 84 | + double[][] centroids = {{1.0, 1.0}}; |
| 85 | + |
| 86 | + assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, 0.0001)); |
148 | 87 | } |
149 | 88 |
|
150 | 89 | @Test |
151 | 90 | void testDimensionMismatchInCentroids() { |
152 | | - double[][] points = { |
153 | | - {1.0, 1.0} |
154 | | - }; |
155 | | - |
156 | | - double[][] centroids = { |
157 | | - {1.0} |
158 | | - }; |
159 | | - |
160 | | - assertThrows( |
161 | | - IllegalArgumentException.class, |
162 | | - () -> KMeans.cluster(points, centroids, 100, 0.0001) |
163 | | - ); |
| 91 | + double[][] points = {{1.0, 1.0}}; |
| 92 | + |
| 93 | + double[][] centroids = {{1.0}}; |
| 94 | + |
| 95 | + assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, 0.0001)); |
164 | 96 | } |
165 | 97 |
|
166 | 98 | @Test |
167 | 99 | void testZeroDimensionPoints() { |
168 | | - double[][] points = { |
169 | | - {} |
170 | | - }; |
171 | | - |
172 | | - double[][] centroids = { |
173 | | - {} |
174 | | - }; |
175 | | - |
176 | | - assertThrows( |
177 | | - IllegalArgumentException.class, |
178 | | - () -> KMeans.cluster(points, centroids, 100, 0.0001) |
179 | | - ); |
| 100 | + double[][] points = {{}}; |
| 101 | + |
| 102 | + double[][] centroids = {{}}; |
| 103 | + |
| 104 | + assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, 0.0001)); |
180 | 105 | } |
181 | 106 |
|
182 | 107 | @Test |
183 | 108 | void testSingleCluster() { |
184 | | - double[][] points = { |
185 | | - {1.0, 1.0}, |
186 | | - {2.0, 2.0}, |
187 | | - {3.0, 3.0} |
188 | | - }; |
189 | | - |
190 | | - double[][] centroids = { |
191 | | - {2.0, 2.0} |
192 | | - }; |
193 | | - |
194 | | - int[] expected = { |
195 | | - 0, 0, 0 |
196 | | - }; |
197 | | - |
198 | | - assertArrayEquals( |
199 | | - expected, |
200 | | - KMeans.cluster(points, centroids, 100, 0.0001) |
201 | | - ); |
| 109 | + double[][] points = {{1.0, 1.0}, {2.0, 2.0}, {3.0, 3.0}}; |
| 110 | + |
| 111 | + double[][] centroids = {{2.0, 2.0}}; |
| 112 | + |
| 113 | + int[] expected = {0, 0, 0}; |
| 114 | + |
| 115 | + assertArrayEquals(expected, KMeans.cluster(points, centroids, 100, 0.0001)); |
202 | 116 | } |
203 | 117 |
|
204 | 118 | @Test |
205 | 119 | void testEmptyClusterHandling() { |
206 | | - double[][] points = { |
207 | | - {0.0, 0.0}, |
208 | | - {0.1, 0.1}, |
209 | | - {10.0, 10.0} |
210 | | - }; |
| 120 | + double[][] points = {{0.0, 0.0}, {0.1, 0.1}, {10.0, 10.0}}; |
211 | 121 |
|
212 | | - double[][] centroids = { |
213 | | - {0.0, 0.0}, |
214 | | - {100.0, 100.0} |
215 | | - }; |
| 122 | + double[][] centroids = {{0.0, 0.0}, {100.0, 100.0}}; |
216 | 123 |
|
217 | 124 | int[] result = KMeans.cluster(points, centroids, 100, 0.0001); |
218 | 125 |
|
219 | | - assertArrayEquals( |
220 | | - new int[]{0, 0, 0}, |
221 | | - result |
222 | | - ); |
| 126 | + assertArrayEquals(new int[] {0, 0, 0}, result); |
223 | 127 | } |
224 | 128 |
|
225 | 129 | @Test |
226 | 130 | void testImmediateConvergence() { |
227 | | - double[][] points = { |
228 | | - {1.0, 1.0}, |
229 | | - {9.0, 9.0} |
230 | | - }; |
231 | | - |
232 | | - double[][] centroids = { |
233 | | - {1.0, 1.0}, |
234 | | - {9.0, 9.0} |
235 | | - }; |
236 | | - |
237 | | - int[] expected = { |
238 | | - 0, 1 |
239 | | - }; |
240 | | - |
241 | | - assertArrayEquals( |
242 | | - expected, |
243 | | - KMeans.cluster(points, centroids, 100, 0.000001) |
244 | | - ); |
| 131 | + double[][] points = {{1.0, 1.0}, {9.0, 9.0}}; |
| 132 | + |
| 133 | + double[][] centroids = {{1.0, 1.0}, {9.0, 9.0}}; |
| 134 | + |
| 135 | + int[] expected = {0, 1}; |
| 136 | + |
| 137 | + assertArrayEquals(expected, KMeans.cluster(points, centroids, 100, 0.000001)); |
245 | 138 | } |
246 | 139 | } |
0 commit comments