1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.stat.clustering;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.commons.math.TestUtils;
27 import org.junit.Test;
28
29 public class EuclideanIntegerPointTest {
30
31 @Test
32 public void testArrayIsReference() {
33 int[] array = { -3, -2, -1, 0, 1 };
34 assertTrue(array == new EuclideanIntegerPoint(array).getPoint());
35 }
36
37 @Test
38 public void testDistance() {
39 EuclideanIntegerPoint e1 = new EuclideanIntegerPoint(new int[] { -3, -2, -1, 0, 1 });
40 EuclideanIntegerPoint e2 = new EuclideanIntegerPoint(new int[] { 1, 0, -1, 1, 1 });
41 assertEquals(Math.sqrt(21.0), e1.distanceFrom(e2), 1.0e-15);
42 assertEquals(0.0, e1.distanceFrom(e1), 1.0e-15);
43 assertEquals(0.0, e2.distanceFrom(e2), 1.0e-15);
44 }
45
46 @Test
47 public void testCentroid() {
48 List<EuclideanIntegerPoint> list = new ArrayList<EuclideanIntegerPoint>();
49 list.add(new EuclideanIntegerPoint(new int[] { 1, 3 }));
50 list.add(new EuclideanIntegerPoint(new int[] { 2, 2 }));
51 list.add(new EuclideanIntegerPoint(new int[] { 3, 3 }));
52 list.add(new EuclideanIntegerPoint(new int[] { 2, 4 }));
53 EuclideanIntegerPoint c = list.get(0).centroidOf(list);
54 assertEquals(2, c.getPoint()[0]);
55 assertEquals(3, c.getPoint()[1]);
56 }
57
58 @Test
59 public void testSerial() {
60 EuclideanIntegerPoint p = new EuclideanIntegerPoint(new int[] { -3, -2, -1, 0, 1 });
61 assertEquals(p, TestUtils.serializeAndRecover(p));
62 }
63
64 }