001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.math.stat.clustering; 019 020 import static org.junit.Assert.assertEquals; 021 import static org.junit.Assert.assertTrue; 022 023 import java.util.ArrayList; 024 import java.util.List; 025 026 import org.apache.commons.math.TestUtils; 027 import org.junit.Test; 028 029 public class EuclideanIntegerPointTest { 030 031 @Test 032 public void testArrayIsReference() { 033 int[] array = { -3, -2, -1, 0, 1 }; 034 assertTrue(array == new EuclideanIntegerPoint(array).getPoint()); 035 } 036 037 @Test 038 public void testDistance() { 039 EuclideanIntegerPoint e1 = new EuclideanIntegerPoint(new int[] { -3, -2, -1, 0, 1 }); 040 EuclideanIntegerPoint e2 = new EuclideanIntegerPoint(new int[] { 1, 0, -1, 1, 1 }); 041 assertEquals(Math.sqrt(21.0), e1.distanceFrom(e2), 1.0e-15); 042 assertEquals(0.0, e1.distanceFrom(e1), 1.0e-15); 043 assertEquals(0.0, e2.distanceFrom(e2), 1.0e-15); 044 } 045 046 @Test 047 public void testCentroid() { 048 List<EuclideanIntegerPoint> list = new ArrayList<EuclideanIntegerPoint>(); 049 list.add(new EuclideanIntegerPoint(new int[] { 1, 3 })); 050 list.add(new EuclideanIntegerPoint(new int[] { 2, 2 })); 051 list.add(new EuclideanIntegerPoint(new int[] { 3, 3 })); 052 list.add(new EuclideanIntegerPoint(new int[] { 2, 4 })); 053 EuclideanIntegerPoint c = list.get(0).centroidOf(list); 054 assertEquals(2, c.getPoint()[0]); 055 assertEquals(3, c.getPoint()[1]); 056 } 057 058 @Test 059 public void testSerial() { 060 EuclideanIntegerPoint p = new EuclideanIntegerPoint(new int[] { -3, -2, -1, 0, 1 }); 061 assertEquals(p, TestUtils.serializeAndRecover(p)); 062 } 063 064 }