/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2013 Regents of the University of Minnesota and contributors
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.lenskit.vectors.similarity;


import org.grouplens.lenskit.vectors.MutableSparseVector;
import org.grouplens.lenskit.vectors.SparseVector;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class TestDistanceSimilarity {
    private static final double EPSILON = 1.0e-6;
    private DistanceVectorSimilarity similarity;

    @Before
    public void setUp() throws Exception {
        similarity = new DistanceVectorSimilarity();
    }

    private SparseVector emptyVector() {
        long[] keys = {};
        double[] values = {};
        return MutableSparseVector.wrap(keys, values);
    }

    @Test
    public void testEmpty() {
        assertEquals(Double.NaN, similarity.similarity(emptyVector(), emptyVector()), EPSILON);
    }

    @Test
    public void testDisjoint() {
        long[] k1 = {2, 5, 6};
        double[] val1 = {1, 3, 2};
        long[] k2 = {3, 4, 7};
        double[] val2 = {1, 3, 2};
        SparseVector v1, v2;
        v1 = MutableSparseVector.wrap(k1, val1).freeze();
        v2 = MutableSparseVector.wrap(k2, val2).freeze();
        assertEquals(1, similarity.similarity(v1, v1), EPSILON);
        assertEquals(-0.414213562, similarity.similarity(v1, v2), EPSILON);
    }

    @Test
    public void testEqualKeys() {
        long[] keys = {2, 5, 6};
        double[] val1 = {1, 2, 1};
        double[] val2 = {1, 2, 5};
        SparseVector v1 = MutableSparseVector.wrap(keys, val1).freeze();
        SparseVector v2 = MutableSparseVector.wrap(keys, val2).freeze();
        assertEquals(1, similarity.similarity(v1, v1), EPSILON);
        assertEquals(0.28635582, similarity.similarity(v1, v2), EPSILON);
    }

    @Test
    public void testOverlap() {
        long[] k1 = {1, 2, 5, 6};
        double[] val1 = {3, 1, 2, 1};
        long[] k2 = {2, 3, 5, 6, 7};
        double[] val2 = {1, 7, 2, 5, 0};
        SparseVector v1 = MutableSparseVector.wrap(k1, val1).freeze();
        SparseVector v2 = MutableSparseVector.wrap(k2, val2).freeze();
        assertEquals(1, similarity.similarity(v1, v1), EPSILON);
        assertEquals(1, similarity.similarity(v2, v2), EPSILON);
        assertEquals(-0.191220847, similarity.similarity(v1, v2), EPSILON);
    }
}

@@ -1208,16 +1208,114 @@
 .Test;%0A%0A
+import static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.Matchers.closeTo;%0A
 import s
@@ -1351,16 +1351,16 @@
 Equals;%0A
-
 %0Apublic 
@@ -2237,34 +2237,29 @@
       assert
-Equals(1, 
+That(
 similarity.s
@@ -2268,32 +2268,43 @@
 ilarity(v1, v1),
+ closeTo(1,
  EPSILON);%0A     
@@ -2288,32 +2288,33 @@
 seTo(1, EPSILON)
+)
 ;%0A        assert
@@ -2317,29 +2317,13 @@
 sert
-Equals(-0.414213562, 
+That(
 simi
@@ -2340,32 +2340,54 @@
 ilarity(v1, v2),
+ closeTo(-0.414213562,
  EPSILON);%0A    %7D
@@ -2371,32 +2371,33 @@
 213562, EPSILON)
+)
 ;%0A    %7D%0A%0A    @Te
@@ -2692,34 +2692,29 @@
       assert
-Equals(1, 
+That(
 similarity.s
@@ -2723,32 +2723,43 @@
 ilarity(v1, v1),
+ closeTo(1,
  EPSILON);%0A     
@@ -2743,32 +2743,33 @@
 seTo(1, EPSILON)
+)
 ;%0A        assert
@@ -2772,27 +2772,13 @@
 sert
-Equals(0.28635582, 
+That(
 simi
@@ -2795,32 +2795,52 @@
 ilarity(v1, v2),
+ closeTo(0.28635582,
  EPSILON);%0A    %7D
@@ -2824,32 +2824,33 @@
 635582, EPSILON)
+)
 ;%0A    %7D%0A%0A    @Te
@@ -3186,34 +3186,29 @@
       assert
-Equals(1, 
+That(
 similarity.s
@@ -3225,16 +3225,27 @@
 v1, v1),
+ closeTo(1,
  EPSILON
@@ -3237,32 +3237,33 @@
 seTo(1, EPSILON)
+)
 ;%0A        assert
@@ -3266,18 +3266,13 @@
 sert
-Equals(1, 
+That(
 simi
@@ -3293,24 +3293,35 @@
 ity(v2, v2),
+ closeTo(1,
  EPSILON);%0A 
@@ -3313,24 +3313,25 @@
 (1, EPSILON)
+)
 ;%0A        as
@@ -3338,29 +3338,13 @@
 sert
-Equals(-0.191220847, 
+That(
 simi
@@ -3369,16 +3369,38 @@
 v1, v2),
+ closeTo(-0.191220847,
  EPSILON
@@ -3392,26 +3392,27 @@
 220847, EPSILON)
+)
 ;%0A    %7D%0A%7D%0A

Change to assertThat