1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  }