http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/stats/OnlineExponentialAverage.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/stats/OnlineExponentialAverage.java b/core/src/main/java/org/apache/mahout/math/stats/OnlineExponentialAverage.java deleted file mode 100644 index 54a0ec7..0000000 --- a/core/src/main/java/org/apache/mahout/math/stats/OnlineExponentialAverage.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math.stats; - -/** - * Computes an online average that is exponentially weighted toward recent time-embedded samples. - */ -public class OnlineExponentialAverage { - - private final double alpha; - private double lastT; - private double s; - private double w; - private double t; - - /** - * Creates an averager that has a specified time constant for discounting old data. The time - * constant, alpha, is the time at which an older sample is discounted to 1/e relative to current - * data. Roughly speaking, data that is more than 3*alpha old doesn't matter any more and data - * that is more recent than alpha/3 is about as important as current data. - * - * See http://tdunning.blogspot.com/2011/03/exponential-weighted-averages-with.html for a - * derivation. See http://tdunning.blogspot.com/2011/03/exponentially-weighted-averaging-for.html - * for the rate method. - * - * @param alpha The time constant for discounting old data and state. - */ - public OnlineExponentialAverage(double alpha) { - this.alpha = alpha; - } - - public void add(double t, double x) { - double pi = Math.exp(-(t - lastT) / alpha); - s = x + pi * s; - w = 1.0 + pi * w; - this.t = t - lastT + pi * this.t; - lastT = t; - } - - public double mean() { - return s / w; - } - - public double meanRate() { - return s / t; - } -}
http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/stats/OnlineSummarizer.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/stats/OnlineSummarizer.java b/core/src/main/java/org/apache/mahout/math/stats/OnlineSummarizer.java deleted file mode 100644 index 297dcd7..0000000 --- a/core/src/main/java/org/apache/mahout/math/stats/OnlineSummarizer.java +++ /dev/null @@ -1,93 +0,0 @@ -///* -// * Licensed to the Apache Software Foundation (ASF) under one or more -// * contributor license agreements. See the NOTICE file distributed with -// * this work for additional information regarding copyright ownership. -// * The ASF licenses this file to You under the Apache License, Version 2.0 -// * (the "License"); you may not use this file except in compliance with -// * the License. You may obtain a copy of the License at -// * -// * http://www.apache.org/licenses/LICENSE-2.0 -// * -// * Unless required by applicable law or agreed to in writing, software -// * distributed under the License is distributed on an "AS IS" BASIS, -// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// * See the License for the specific language governing permissions and -// * limitations under the License. -// */ -// -//package org.apache.mahout.math.stats; -// -//import com.tdunning.math.stats.TDigest; -// -///** -// * Computes on-line estimates of mean, variance and all five quartiles (notably including the -// * median). Since this is done in a completely incremental fashion (that is what is meant by -// * on-line) estimates are available at any time and the amount of memory used is constant. Somewhat -// * surprisingly, the quantile estimates are about as good as you would get if you actually kept all -// * of the samples. -// * <p/> -// * The method used for mean and variance is Welford's method. See -// * <p/> -// * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#On-line_algorithm -// * <p/> -// * The method used for computing the quartiles is a simplified form of the stochastic approximation -// * method described in the article "Incremental Quantile Estimation for Massive Tracking" by Chen, -// * Lambert and Pinheiro -// * <p/> -// * See -// * <p/> -// * http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.105.1580 -// */ -//public class OnlineSummarizer { -// -// private TDigest quantiles = TDigest.createDigest(100.0); -// -// // mean and variance estimates -// private double mean; -// private double variance; -// -// // number of samples seen so far -// private int n; -// -// public void add(double sample) { -// n++; -// double oldMean = mean; -// mean += (sample - mean) / n; -// double diff = (sample - mean) * (sample - oldMean); -// variance += (diff - variance) / n; -// -// quantiles.add(sample); -// } -// -// public int getCount() { -// return n; -// } -// -// public double getMean() { -// return mean; -// } -// -// public double getSD() { -// return Math.sqrt(variance); -// } -// -// public double getMin() { -// return getQuartile(0); -// } -// -// public double getMax() { -// return getQuartile(4); -// } -// -// public double getQuartile(int i) { -// return quantiles.quantile(0.25 * i); -// } -// -// public double quantile(double q) { -// return quantiles.quantile(q); -// } -// -// public double getMedian() { -// return getQuartile(2); -// } -//} http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/test/java-templates/org/apache/mahout/math/list/ValueTypeArrayListTest.java.t ---------------------------------------------------------------------- diff --git a/core/src/test/java-templates/org/apache/mahout/math/list/ValueTypeArrayListTest.java.t b/core/src/test/java-templates/org/apache/mahout/math/list/ValueTypeArrayListTest.java.t new file mode 100644 index 0000000..17204d3 --- /dev/null +++ b/core/src/test/java-templates/org/apache/mahout/math/list/ValueTypeArrayListTest.java.t @@ -0,0 +1,237 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.mahout.math.list; + +import org.apache.mahout.math.function.${valueTypeCap}Procedure; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +#if ($valueTypeFloating == 'true') + #set ($assertEpsilon=', 0001') +#else + #set ($assertEpsilon='') +#end + +public class ${valueTypeCap}ArrayListTest extends Assert { + + private ${valueTypeCap}ArrayList emptyList; + private ${valueTypeCap}ArrayList listOfFive; + + @Before + public void before() { + emptyList = new ${valueTypeCap}ArrayList(); + listOfFive = new ${valueTypeCap}ArrayList(); + for (int x = 0; x < 5; x ++) { + listOfFive.add((${valueType})x); + } + } + + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetEmpty() { + emptyList.get(0); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void setEmpty() { + emptyList.set(1, (${valueType})1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void beforeInsertInvalidRange() { + emptyList.beforeInsert(1, (${valueType})0); + } + + @Test + public void testAdd() { + emptyList.add((${valueType})12); + assertEquals(1, emptyList.size()); + for (int x = 0; x < 1000; x ++) { + emptyList.add((${valueType})(x % ${valueObjectType}.MAX_VALUE)); + } + assertEquals(1001, emptyList.size()); + assertEquals(12, emptyList.get(0) $assertEpsilon); + for (int x = 0; x < 1000; x ++) { + assertEquals((${valueType})(x % ${valueObjectType}.MAX_VALUE), emptyList.get(x+1) $assertEpsilon); + } + } + + @Test + public void testBinarySearch() + { + int x = listOfFive.binarySearchFromTo((${valueType})0, 2, 4); + assertEquals(-3, x); + x = listOfFive.binarySearchFromTo((${valueType})1, 0, 4); + assertEquals(1, x); + } + + @Test + public void testClone() { + ${valueTypeCap}ArrayList l2 = listOfFive.copy(); // copy just calls clone. + assertNotSame(listOfFive, l2); + assertEquals(listOfFive, l2); + } + + @Test + public void testElements() { + ${valueType}[] l = { 12, 24, 36, 48 }; + ${valueTypeCap}ArrayList lar = new ${valueTypeCap}ArrayList(l); + assertEquals(4, lar.size()); + assertSame(l, lar.elements()); + ${valueType}[] l2 = { 3, 6, 9, 12 }; + lar.elements(l2); + assertSame(l2, lar.elements()); + } + + @Test + public void testEquals() { + ${valueType}[] l = { 12, 24, 36, 48 }; + ${valueTypeCap}ArrayList lar = new ${valueTypeCap}ArrayList(l); + ${valueTypeCap}ArrayList lar2 = new ${valueTypeCap}ArrayList(); + for (int x = 0; x < lar.size(); x++) { + lar2.add(lar.get(x)); + } + assertEquals(lar, lar2); + assertFalse(lar.equals(this)); + lar2.add((${valueType})55); + assertFalse(lar.equals(lar2)); + } + + @Test + public void testForEach() { + listOfFive.forEach(new ${valueTypeCap}Procedure() { + int count; + @Override + public boolean apply(${valueType} element) { + assertFalse(count > 2); + count ++; + return element != 1; + }}); + } + + @Test + public void testGetQuick() { + ${valueTypeCap}ArrayList lar = new ${valueTypeCap}ArrayList(10); + lar.getQuick(1); // inside capacity, outside size. + } + + @Test + public void testIndexOfFromTo() { + int x = listOfFive.indexOfFromTo((${valueType})0, 2, 4); + assertEquals(-1, x); + x = listOfFive.indexOfFromTo((${valueType})1, 0, 4); + assertEquals(1, x); + } + + @Test + public void testLastIndexOfFromTo() { + ${valueTypeCap}ArrayList lar = new ${valueTypeCap}ArrayList(10); + lar.add((${valueType})1); + lar.add((${valueType})2); + lar.add((${valueType})3); + lar.add((${valueType})2); + lar.add((${valueType})1); + assertEquals(3, lar.lastIndexOf((${valueType})2)); + assertEquals(3, lar.lastIndexOfFromTo((${valueType})2, 2, 4)); + assertEquals(-1, lar.lastIndexOf((${valueType})111)); + } + + @Test + public void testPartFromTo() { + Abstract${valueTypeCap}List al = listOfFive.partFromTo(1, 2); + assertEquals(2, al.size()); + assertEquals(1, al.get(0) $assertEpsilon); + assertEquals(2, al.get(1) $assertEpsilon); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testPartFromToOOB() { + listOfFive.partFromTo(10, 11); + } + + @Test + public void testRemoveAll() { + ${valueTypeCap}ArrayList lar = new ${valueTypeCap}ArrayList(1000); + for (int x = 0; x < 128; x ++) { + lar.add((${valueType})x); + } + ${valueTypeCap}ArrayList larOdd = new ${valueTypeCap}ArrayList(500); + for (int x = 1; x < 128; x = x + 2) { + larOdd.add((${valueType})x); + } + lar.removeAll(larOdd); + assertEquals(64, lar.size()); + + for (int x = 0; x < lar.size(); x++) { + assertEquals(x*2, lar.get(x) $assertEpsilon); + } + } + + @Test + public void testReplaceFromToWith() { + listOfFive.add((${valueType})5); + ${valueTypeCap}ArrayList lar = new ${valueTypeCap}ArrayList(); + lar.add((${valueType})44); + lar.add((${valueType})55); + listOfFive.replaceFromToWithFromTo(2, 3, lar, 0, 1); + assertEquals(0, listOfFive.get(0) $assertEpsilon); + assertEquals(1, listOfFive.get(1) $assertEpsilon); + assertEquals(44, listOfFive.get(2) $assertEpsilon); + assertEquals(55, listOfFive.get(3) $assertEpsilon); + assertEquals(4, listOfFive.get(4) $assertEpsilon); + assertEquals(5, listOfFive.get(5) $assertEpsilon); + } + + @Test + public void testRetainAllSmall() { + ${valueTypeCap}ArrayList lar = new ${valueTypeCap}ArrayList(); + lar.addAllOf(listOfFive); + lar.addAllOf(listOfFive); + lar.addAllOf(listOfFive); + ${valueTypeCap}ArrayList lar2 = new ${valueTypeCap}ArrayList(); + lar2.add((${valueType})3); + lar2.add((${valueType})4); + assertTrue(lar.retainAll(lar2)); + for(int x = 0; x < lar.size(); x ++) { + ${valueType} l = lar.get(x); + assertTrue(l == 3 || l == 4); + } + assertEquals(6, lar.size()); + } + + @Test + public void testRetainAllSmaller() { + ${valueTypeCap}ArrayList lar = new ${valueTypeCap}ArrayList(); + lar.addAllOf(listOfFive); + ${valueTypeCap}ArrayList lar2 = new ${valueTypeCap}ArrayList(); + // large 'other' arg to take the other code path. + for (int x = 0; x < 1000; x ++) { + lar2.add((${valueType})3); + lar2.add((${valueType})4); + } + assertTrue(lar.retainAll(lar2)); + for(int x = 0; x < lar.size(); x ++) { + ${valueType} l = lar.get(x); + assertTrue(l == 3 || l == 4); + } + } + +} http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/test/java-templates/org/apache/mahout/math/map/OpenKeyTypeObjectHashMapTest.java.t ---------------------------------------------------------------------- diff --git a/core/src/test/java-templates/org/apache/mahout/math/map/OpenKeyTypeObjectHashMapTest.java.t b/core/src/test/java-templates/org/apache/mahout/math/map/OpenKeyTypeObjectHashMapTest.java.t new file mode 100644 index 0000000..a915e4a --- /dev/null +++ b/core/src/test/java-templates/org/apache/mahout/math/map/OpenKeyTypeObjectHashMapTest.java.t @@ -0,0 +1,431 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#if (${keyTypeFloating} == 'true') +#set ($keyEpsilon = ", (${keyType})0.000001") +#else +#set ($keyEpsilon = "") +#end + + package org.apache.mahout.math.map; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.apache.mahout.math.function.${keyTypeCap}ObjectProcedure; +import org.apache.mahout.math.function.${keyTypeCap}Procedure; +import org.apache.mahout.math.list.${keyTypeCap}ArrayList; +import org.apache.mahout.math.set.AbstractSet; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class Open${keyTypeCap}ObjectHashMapTest extends Assert { + + private static class TestClass implements Comparable<TestClass>{ + + TestClass(${keyType} x) { + this.x = x; + } + + @Override + public String toString() { + return "[ts " + x + " ]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ${keyObjectType}.valueOf(x).hashCode(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + TestClass other = (TestClass) obj; + return x == other.x; + } + + ${keyType} x; + + @Override + public int compareTo(TestClass o) { + +#if (${keyTypeFloating} == 'true') + return ${keyObjectType}.compare(x, o.x); +#else + return (int)(x - o.x); +#end + } + } + + private TestClass item; + private TestClass anotherItem; + private TestClass anotherItem2; + private TestClass anotherItem3; + private TestClass anotherItem4; + private TestClass anotherItem5; + + @Before + public void before() { + item = new TestClass((${keyType})101); + anotherItem = new TestClass((${keyType})99); + anotherItem2 = new TestClass((${keyType})2); + anotherItem3 = new TestClass((${keyType})3); + anotherItem4 = new TestClass((${keyType})4); + anotherItem5 = new TestClass((${keyType})5); + + } + + + @Test + public void testConstructors() { + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + int[] capacity = new int[1]; + double[] minLoadFactor = new double[1]; + double[] maxLoadFactor = new double[1]; + + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(AbstractSet.DEFAULT_CAPACITY, capacity[0]); + assertEquals(AbstractSet.DEFAULT_MAX_LOAD_FACTOR, maxLoadFactor[0], 0.001); + assertEquals(AbstractSet.DEFAULT_MIN_LOAD_FACTOR, minLoadFactor[0], 0.001); + int prime = PrimeFinder.nextPrime(907); + map = new Open${keyTypeCap}ObjectHashMap<TestClass>(prime); + + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(prime, capacity[0]); + assertEquals(AbstractSet.DEFAULT_MAX_LOAD_FACTOR, maxLoadFactor[0], 0.001); + assertEquals(AbstractSet.DEFAULT_MIN_LOAD_FACTOR, minLoadFactor[0], 0.001); + + map = new Open${keyTypeCap}ObjectHashMap<TestClass>(prime, 0.4, 0.8); + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(prime, capacity[0]); + assertEquals(0.4, minLoadFactor[0], 0.001); + assertEquals(0.8, maxLoadFactor[0], 0.001); + } + + @Test + public void testEnsureCapacity() { + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + int prime = PrimeFinder.nextPrime(907); + + map.ensureCapacity(prime); + int[] capacity = new int[1]; + double[] minLoadFactor = new double[1]; + double[] maxLoadFactor = new double[1]; + + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(prime, capacity[0]); + } + + @Test + public void testClear() { + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + for (int i = 0; i < 100; i++) { + map.put((${keyType}) i, item); + assertEquals(1, map.size()); + map.clear(); + assertEquals(0, map.size()); + assertFalse("Contains: " + i, map.containsKey((${keyType}) i)); + assertSame(null, map.get((${keyType}) i)); + } + } + + @Test + public void testClone() { + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + map.put((${keyType}) 11, item); + Open${keyTypeCap}ObjectHashMap<TestClass> map2 = (Open${keyTypeCap}ObjectHashMap<TestClass>) map.clone(); + map.clear(); + assertEquals(1, map2.size()); + } + + @Test + public void testContainsKey() { + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + map.put((${keyType}) 11, item); + assertTrue(map.containsKey((${keyType}) 11)); + assertFalse(map.containsKey((${keyType}) 12)); + } + + @Test + public void testContainValue() { + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + map.put((${keyType}) 11, item); + assertTrue(map.containsValue(item)); + assertFalse(map.containsValue(anotherItem)); + } + + @Test + public void testForEachKey() { + final ${keyTypeCap}ArrayList keys = new ${keyTypeCap}ArrayList(); + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + map.put((${keyType}) 11, anotherItem); + map.put((${keyType}) 12, anotherItem2); + map.put((${keyType}) 13, anotherItem3); + map.put((${keyType}) 14, anotherItem4); + map.removeKey((${keyType}) 13); + map.forEachKey(new ${keyTypeCap}Procedure() { + + @Override + public boolean apply(${keyType} element) { + keys.add(element); + return true; + } + }); + + ${keyType}[] keysArray = keys.toArray(new ${keyType}[keys.size()]); + Arrays.sort(keysArray); + + assertArrayEquals(new ${keyType}[] {11, 12, 14}, keysArray ${keyEpsilon}); + } + + private static class Pair implements Comparable<Pair> { + ${keyType} k; + TestClass v; + + Pair(${keyType} k, TestClass v) { + this.k = k; + this.v = v; + } + + @Override + public int compareTo(Pair o) { + if (k < o.k) { + return -1; + } else if (k == o.k) { + return 0; + } else { + return 1; + } + } + } + + @Test + public void testForEachPair() { + final List<Pair> pairs = new ArrayList<Pair>(); + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + map.put((${keyType}) 11, anotherItem); + map.put((${keyType}) 12, anotherItem2); + map.put((${keyType}) 13, anotherItem3); + map.put((${keyType}) 14, anotherItem4); + map.removeKey((${keyType}) 13); + map.forEachPair(new ${keyTypeCap}ObjectProcedure<TestClass>() { + + @Override + public boolean apply(${keyType} first, TestClass second) { + pairs.add(new Pair(first, second)); + return true; + } + }); + + Collections.sort(pairs); + assertEquals(3, pairs.size()); + assertEquals((${keyType}) 11, pairs.get(0).k ${keyEpsilon}); + assertSame(anotherItem, pairs.get(0).v ); + assertEquals((${keyType}) 12, pairs.get(1).k ${keyEpsilon}); + assertSame(anotherItem2, pairs.get(1).v ); + assertEquals((${keyType}) 14, pairs.get(2).k ${keyEpsilon}); + assertSame(anotherItem4, pairs.get(2).v ); + + pairs.clear(); + map.forEachPair(new ${keyTypeCap}ObjectProcedure<TestClass>() { + int count = 0; + + @Override + public boolean apply(${keyType} first, TestClass second) { + pairs.add(new Pair(first, second)); + count++; + return count < 2; + } + }); + + assertEquals(2, pairs.size()); + } + + @Test + public void testGet() { + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + map.put((${keyType}) 11, item); + map.put((${keyType}) 12, anotherItem); + assertSame(item, map.get((${keyType})11) ); + assertSame(null, map.get((${keyType})0) ); + } + + @Test + public void testKeys() { + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + map.put((${keyType}) 11, item); + map.put((${keyType}) 12, item); + ${keyTypeCap}ArrayList keys = new ${keyTypeCap}ArrayList(); + map.keys(keys); + keys.sort(); + assertEquals(11, keys.get(0) ${keyEpsilon}); + assertEquals(12, keys.get(1) ${keyEpsilon}); + ${keyTypeCap}ArrayList k2 = map.keys(); + k2.sort(); + assertEquals(keys, k2); + } + + @Test + public void testPairsMatching() { + ${keyTypeCap}ArrayList keyList = new ${keyTypeCap}ArrayList(); + List<TestClass> valueList = new ArrayList<TestClass>(); + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + map.put((${keyType}) 11, anotherItem2); + map.put((${keyType}) 12, anotherItem3); + map.put((${keyType}) 13, anotherItem4); + map.put((${keyType}) 14, anotherItem5); + map.removeKey((${keyType}) 13); + map.pairsMatching(new ${keyTypeCap}ObjectProcedure<TestClass>() { + + @Override + public boolean apply(${keyType} first, TestClass second) { + return (first % 2) == 0; + }}, + keyList, valueList); + keyList.sort(); + Collections.sort(valueList); + assertEquals(2, keyList.size()); + assertEquals(2, valueList.size()); + assertEquals(12, keyList.get(0) ${keyEpsilon}); + assertEquals(14, keyList.get(1) ${keyEpsilon}); + assertSame(anotherItem3, valueList.get(0) ); + assertSame(anotherItem5, valueList.get(1) ); + } + + @Test + public void testValues() { + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + map.put((${keyType}) 11, anotherItem); + map.put((${keyType}) 12, anotherItem2); + map.put((${keyType}) 13, anotherItem3); + map.put((${keyType}) 14, anotherItem4); + map.removeKey((${keyType}) 13); + List<TestClass> values = new ArrayList<TestClass>(100); + map.values(values); + assertEquals(3, values.size()); + Collections.sort(values); + assertEquals(anotherItem2, values.get(0) ); + assertEquals(anotherItem4, values.get(1) ); + assertEquals(anotherItem, values.get(2) ); + } + + // tests of the code in the abstract class + + @Test + public void testCopy() { + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + map.put((${keyType}) 11, item); + Open${keyTypeCap}ObjectHashMap<TestClass> map2 = (Open${keyTypeCap}ObjectHashMap<TestClass>) map.copy(); + map.clear(); + assertEquals(1, map2.size()); + } + + @Test + public void testEquals() { + // since there are no other subclasses of + // Abstractxxx available, we have to just test the + // obvious. + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + map.put((${keyType}) 11, anotherItem); + map.put((${keyType}) 12, anotherItem2); + map.put((${keyType}) 13, anotherItem3); + map.put((${keyType}) 14, anotherItem4); + map.removeKey((${keyType}) 13); + Open${keyTypeCap}ObjectHashMap<TestClass> map2 = (Open${keyTypeCap}ObjectHashMap<TestClass>) map.copy(); + assertEquals(map, map2); + assertTrue(map2.equals(map)); + assertFalse("Hello Sailor".equals(map)); + assertFalse(map.equals("hello sailor")); + map2.removeKey((${keyType}) 11); + assertFalse(map.equals(map2)); + assertFalse(map2.equals(map)); + } + + // keys() tested in testKeys + + @Test + public void testKeysSortedByValue() { + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + map.put((${keyType}) 11, anotherItem5); + map.put((${keyType}) 12, anotherItem4); + map.put((${keyType}) 13, anotherItem3); + map.put((${keyType}) 14, anotherItem2); + map.removeKey((${keyType}) 13); + ${keyTypeCap}ArrayList keys = new ${keyTypeCap}ArrayList(); + map.keysSortedByValue(keys); + ${keyType}[] keysArray = keys.toArray(new ${keyType}[keys.size()]); + assertArrayEquals(new ${keyType}[] {14, 12, 11}, + keysArray ${keyEpsilon}); + } + + @Test + public void testPairsSortedByKey() { + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + map.put((${keyType}) 11, anotherItem5); + map.put((${keyType}) 12, anotherItem4); + map.put((${keyType}) 13, anotherItem3); + map.put((${keyType}) 14, anotherItem2); + + ${keyTypeCap}ArrayList keys = new ${keyTypeCap}ArrayList(); + List<TestClass> values = new ArrayList<TestClass>(); + map.pairsSortedByKey(keys, values); + + assertEquals(4, keys.size()); + assertEquals(4, values.size()); + assertEquals((${keyType}) 11, keys.get(0) ${keyEpsilon}); + assertSame(anotherItem5, values.get(0) ); + assertEquals((${keyType}) 12, keys.get(1) ${keyEpsilon}); + assertSame(anotherItem4, values.get(1) ); + assertEquals((${keyType}) 13, keys.get(2) ${keyEpsilon}); + assertSame(anotherItem3, values.get(2) ); + assertEquals((${keyType}) 14, keys.get(3) ${keyEpsilon}); + assertSame(anotherItem2, values.get(3) ); + } + + @Test + public void testPairsSortedByValue() { + Open${keyTypeCap}ObjectHashMap<TestClass> map = new Open${keyTypeCap}ObjectHashMap<TestClass>(); + map.put((${keyType}) 11, anotherItem5); + map.put((${keyType}) 12, anotherItem4); + map.put((${keyType}) 13, anotherItem3); + map.put((${keyType}) 14, anotherItem2); + + ${keyTypeCap}ArrayList keys = new ${keyTypeCap}ArrayList(); + List<TestClass> values = new ArrayList<TestClass>(); + map.pairsSortedByValue(keys, values); + assertEquals((${keyType}) 11, keys.get(3) ${keyEpsilon}); + assertEquals(anotherItem5, values.get(3) ); + assertEquals((${keyType}) 12, keys.get(2) ${keyEpsilon}); + assertEquals(anotherItem4, values.get(2) ); + assertEquals((${keyType}) 13, keys.get(1) ${keyEpsilon}); + assertEquals(anotherItem3, values.get(1) ); + assertEquals((${keyType}) 14, keys.get(0) ${keyEpsilon}); + assertEquals(anotherItem2, values.get(0) ); + } + + } http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/test/java-templates/org/apache/mahout/math/map/OpenKeyTypeValueTypeHashMapTest.java.t ---------------------------------------------------------------------- diff --git a/core/src/test/java-templates/org/apache/mahout/math/map/OpenKeyTypeValueTypeHashMapTest.java.t b/core/src/test/java-templates/org/apache/mahout/math/map/OpenKeyTypeValueTypeHashMapTest.java.t new file mode 100644 index 0000000..57893e9 --- /dev/null +++ b/core/src/test/java-templates/org/apache/mahout/math/map/OpenKeyTypeValueTypeHashMapTest.java.t @@ -0,0 +1,379 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#if (${keyTypeFloating} == 'true') +#set ($keyEpsilon = ", (${keyType})0.000001") +#else +#set ($keyEpsilon = "") +#end +#if (${valueTypeFloating} == 'true') +#set ($valueEpsilon = ", (${valueType})0.000001") +#else +#set ($valueEpsilon = "") +#end + + package org.apache.mahout.math.map; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.apache.mahout.math.function.${keyTypeCap}${valueTypeCap}Procedure; +import org.apache.mahout.math.function.${keyTypeCap}Procedure; +import org.apache.mahout.math.list.${keyTypeCap}ArrayList; +#if (${keyType} != ${valueType}) +import org.apache.mahout.math.list.${valueTypeCap}ArrayList; +#end +import org.apache.mahout.math.set.AbstractSet; + +import org.junit.Assert; +import org.junit.Test; + +public class Open${keyTypeCap}${valueTypeCap}HashMapTest extends Assert { + + + @Test + public void testConstructors() { + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + int[] capacity = new int[1]; + double[] minLoadFactor = new double[1]; + double[] maxLoadFactor = new double[1]; + + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(AbstractSet.DEFAULT_CAPACITY, capacity[0]); + assertEquals(AbstractSet.DEFAULT_MAX_LOAD_FACTOR, maxLoadFactor[0], 0.001); + assertEquals(AbstractSet.DEFAULT_MIN_LOAD_FACTOR, minLoadFactor[0], 0.001); + int prime = PrimeFinder.nextPrime(907); + map = new Open${keyTypeCap}${valueTypeCap}HashMap(prime); + + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(prime, capacity[0]); + assertEquals(AbstractSet.DEFAULT_MAX_LOAD_FACTOR, maxLoadFactor[0], 0.001); + assertEquals(AbstractSet.DEFAULT_MIN_LOAD_FACTOR, minLoadFactor[0], 0.001); + + map = new Open${keyTypeCap}${valueTypeCap}HashMap(prime, 0.4, 0.8); + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(prime, capacity[0]); + assertEquals(0.4, minLoadFactor[0], 0.001); + assertEquals(0.8, maxLoadFactor[0], 0.001); + } + + @Test + public void testEnsureCapacity() { + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + int prime = PrimeFinder.nextPrime(907); + + map.ensureCapacity(prime); + int[] capacity = new int[1]; + double[] minLoadFactor = new double[1]; + double[] maxLoadFactor = new double[1]; + + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(prime, capacity[0]); + } + + @Test + public void testClear() { + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put((${keyType}) 11, (${valueType}) 22); + assertEquals(1, map.size()); + map.clear(); + assertEquals(0, map.size()); + assertEquals(0, map.get((${keyType}) 11), 0.0000001); + } + + @Test + public void testClone() { + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put((${keyType}) 11, (${valueType}) 22); + Open${keyTypeCap}${valueTypeCap}HashMap map2 = (Open${keyTypeCap}${valueTypeCap}HashMap) map.clone(); + map.clear(); + assertEquals(1, map2.size()); + } + + @Test + public void testContainsKey() { + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put(($keyType) 11, (${valueType}) 22); + assertTrue(map.containsKey(($keyType) 11)); + assertFalse(map.containsKey(($keyType) 12)); + } + + @Test + public void testContainValue() { + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put(($keyType) 11, (${valueType}) 22); + assertTrue(map.containsValue((${valueType}) 22)); + assertFalse(map.containsValue((${valueType}) 23)); + } + + @Test + public void testForEachKey() { + final ${keyTypeCap}ArrayList keys = new ${keyTypeCap}ArrayList(); + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put(($keyType) 11, (${valueType}) 22); + map.put(($keyType) 12, (${valueType}) 23); + map.put(($keyType) 13, (${valueType}) 24); + map.put(($keyType) 14, (${valueType}) 25); + map.removeKey(($keyType) 13); + map.forEachKey(new ${keyTypeCap}Procedure() { + + @Override + public boolean apply(${keyType} element) { + keys.add(element); + return true; + } + }); + + ${keyType}[] keysArray = keys.toArray(new ${keyType}[keys.size()]); + Arrays.sort(keysArray); + + assertArrayEquals(new ${keyType}[] {11, 12, 14}, keysArray ${keyEpsilon}); + } + + private static class Pair implements Comparable<Pair> { + ${keyType} k; + ${valueType} v; + + Pair(${keyType} k, ${valueType} v) { + this.k = k; + this.v = v; + } + + @Override + public int compareTo(Pair o) { + if (k < o.k) { + return -1; + } else if (k == o.k) { + return 0; + } else { + return 1; + } + } + } + + @Test + public void testForEachPair() { + final List<Pair> pairs = new ArrayList<Pair>(); + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put(($keyType) 11, (${valueType}) 22); + map.put(($keyType) 12, (${valueType}) 23); + map.put(($keyType) 13, (${valueType}) 24); + map.put(($keyType) 14, (${valueType}) 25); + map.removeKey(($keyType) 13); + map.forEachPair(new ${keyTypeCap}${valueTypeCap}Procedure() { + + @Override + public boolean apply(${keyType} first, ${valueType} second) { + pairs.add(new Pair(first, second)); + return true; + } + }); + + Collections.sort(pairs); + assertEquals(3, pairs.size()); + assertEquals(($keyType) 11, pairs.get(0).k ${keyEpsilon}); + assertEquals((${valueType}) 22, pairs.get(0).v ${valueEpsilon}); + assertEquals(($keyType) 12, pairs.get(1).k ${keyEpsilon}); + assertEquals((${valueType}) 23, pairs.get(1).v ${valueEpsilon}); + assertEquals(($keyType) 14, pairs.get(2).k ${keyEpsilon}); + assertEquals((${valueType}) 25, pairs.get(2).v ${valueEpsilon}); + + pairs.clear(); + map.forEachPair(new ${keyTypeCap}${valueTypeCap}Procedure() { + int count = 0; + + @Override + public boolean apply(${keyType} first, ${valueType} second) { + pairs.add(new Pair(first, second)); + count++; + return count < 2; + } + }); + + assertEquals(2, pairs.size()); + } + + @Test + public void testGet() { + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put(($keyType) 11, (${valueType}) 22); + map.put(($keyType) 12, (${valueType}) 23); + assertEquals(22, map.get(($keyType)11) ${valueEpsilon}); + assertEquals(0, map.get(($keyType)0) ${valueEpsilon}); + } + + @Test + public void testAdjustOrPutValue() { + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put(($keyType) 11, (${valueType}) 22); + map.put(($keyType) 12, (${valueType}) 23); + map.put(($keyType) 13, (${valueType}) 24); + map.put(($keyType) 14, (${valueType}) 25); + map.adjustOrPutValue((${keyType})11, (${valueType})1, (${valueType})3); + assertEquals(25, map.get((${keyType})11) ${valueEpsilon}); + map.adjustOrPutValue((${keyType})15, (${valueType})1, (${valueType})3); + assertEquals(1, map.get((${keyType})15) ${valueEpsilon}); + } + + @Test + public void testKeys() { + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put(($keyType) 11, (${valueType}) 22); + map.put(($keyType) 12, (${valueType}) 22); + ${keyTypeCap}ArrayList keys = new ${keyTypeCap}ArrayList(); + map.keys(keys); + keys.sort(); + assertEquals(11, keys.get(0) ${keyEpsilon}); + assertEquals(12, keys.get(1) ${keyEpsilon}); + ${keyTypeCap}ArrayList k2 = map.keys(); + k2.sort(); + assertEquals(keys, k2); + } + + @Test + public void testPairsMatching() { + ${keyTypeCap}ArrayList keyList = new ${keyTypeCap}ArrayList(); + ${valueTypeCap}ArrayList valueList = new ${valueTypeCap}ArrayList(); + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put(($keyType) 11, (${valueType}) 22); + map.put(($keyType) 12, (${valueType}) 23); + map.put(($keyType) 13, (${valueType}) 24); + map.put(($keyType) 14, (${valueType}) 25); + map.removeKey(($keyType) 13); + map.pairsMatching(new ${keyTypeCap}${valueTypeCap}Procedure() { + + @Override + public boolean apply(${keyType} first, ${valueType} second) { + return (first % 2) == 0; + }}, + keyList, valueList); + keyList.sort(); + valueList.sort(); + assertEquals(2, keyList.size()); + assertEquals(2, valueList.size()); + assertEquals(12, keyList.get(0) ${keyEpsilon}); + assertEquals(14, keyList.get(1) ${keyEpsilon}); + assertEquals(23, valueList.get(0) ${valueEpsilon}); + assertEquals(25, valueList.get(1) ${valueEpsilon}); + } + + @Test + public void testValues() { + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put(($keyType) 11, (${valueType}) 22); + map.put(($keyType) 12, (${valueType}) 23); + map.put(($keyType) 13, (${valueType}) 24); + map.put(($keyType) 14, (${valueType}) 25); + map.removeKey(($keyType) 13); + ${valueTypeCap}ArrayList values = new ${valueTypeCap}ArrayList(100); + map.values(values); + assertEquals(3, values.size()); + values.sort(); + assertEquals(22, values.get(0) ${valueEpsilon}); + assertEquals(23, values.get(1) ${valueEpsilon}); + assertEquals(25, values.get(2) ${valueEpsilon}); + } + + // tests of the code in the abstract class + + @Test + public void testCopy() { + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put(($keyType) 11, (${valueType}) 22); + Open${keyTypeCap}${valueTypeCap}HashMap map2 = (Open${keyTypeCap}${valueTypeCap}HashMap) map.copy(); + map.clear(); + assertEquals(1, map2.size()); + } + + @Test + public void testEquals() { + // since there are no other subclasses of + // Abstractxxx available, we have to just test the + // obvious. + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put(($keyType) 11, (${valueType}) 22); + map.put(($keyType) 12, (${valueType}) 23); + map.put(($keyType) 13, (${valueType}) 24); + map.put(($keyType) 14, (${valueType}) 25); + map.removeKey(($keyType) 13); + Open${keyTypeCap}${valueTypeCap}HashMap map2 = (Open${keyTypeCap}${valueTypeCap}HashMap) map.copy(); + assertEquals(map, map2); + assertTrue(map2.equals(map)); + assertFalse("Hello Sailor".equals(map)); + assertFalse(map.equals("hello sailor")); + map2.removeKey(($keyType) 11); + assertFalse(map.equals(map2)); + assertFalse(map2.equals(map)); + } + + // keys() tested in testKeys + + @Test + public void testKeysSortedByValue() { + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put(($keyType) 11, (${valueType}) 22); + map.put(($keyType) 12, (${valueType}) 23); + map.put(($keyType) 13, (${valueType}) 24); + map.put(($keyType) 14, (${valueType}) 25); + map.removeKey(($keyType) 13); + ${keyTypeCap}ArrayList keys = new ${keyTypeCap}ArrayList(); + map.keysSortedByValue(keys); + ${keyType}[] keysArray = keys.toArray(new ${keyType}[keys.size()]); + assertArrayEquals(new ${keyType}[] {11, 12, 14}, + keysArray ${keyEpsilon}); + } + + @Test + public void testPairsSortedByKey() { + Open${keyTypeCap}${valueTypeCap}HashMap map = new Open${keyTypeCap}${valueTypeCap}HashMap(); + map.put(($keyType) 11, (${valueType}) 100); + map.put(($keyType) 12, (${valueType}) 70); + map.put(($keyType) 13, (${valueType}) 30); + map.put(($keyType) 14, (${valueType}) 3); + + ${keyTypeCap}ArrayList keys = new ${keyTypeCap}ArrayList(); + ${valueTypeCap}ArrayList values = new ${valueTypeCap}ArrayList(); + map.pairsSortedByKey(keys, values); + + assertEquals(4, keys.size()); + assertEquals(4, values.size()); + assertEquals(($keyType) 11, keys.get(0) ${keyEpsilon}); + assertEquals((${valueType}) 100, values.get(0) ${valueEpsilon}); + assertEquals(($keyType) 12, keys.get(1) ${keyEpsilon}); + assertEquals((${valueType}) 70, values.get(1) ${valueEpsilon}); + assertEquals(($keyType) 13, keys.get(2) ${keyEpsilon}); + assertEquals((${valueType}) 30, values.get(2) ${valueEpsilon}); + assertEquals(($keyType) 14, keys.get(3) ${keyEpsilon}); + assertEquals((${valueType}) 3, values.get(3) ${valueEpsilon}); + keys.clear(); + values.clear(); + map.pairsSortedByValue(keys, values); + assertEquals(($keyType) 11, keys.get(3) ${keyEpsilon}); + assertEquals((${valueType}) 100, values.get(3) ${valueEpsilon}); + assertEquals(($keyType) 12, keys.get(2) ${keyEpsilon}); + assertEquals((${valueType}) 70, values.get(2) ${valueEpsilon}); + assertEquals(($keyType) 13, keys.get(1) ${keyEpsilon}); + assertEquals((${valueType}) 30, values.get(1) ${valueEpsilon}); + assertEquals(($keyType) 14, keys.get(0) ${keyEpsilon}); + assertEquals(($valueType) 3, values.get(0) ${valueEpsilon}); + } + + } http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/test/java-templates/org/apache/mahout/math/map/OpenObjectValueTypeHashMapTest.java.t ---------------------------------------------------------------------- diff --git a/core/src/test/java-templates/org/apache/mahout/math/map/OpenObjectValueTypeHashMapTest.java.t b/core/src/test/java-templates/org/apache/mahout/math/map/OpenObjectValueTypeHashMapTest.java.t new file mode 100644 index 0000000..2bbbbef --- /dev/null +++ b/core/src/test/java-templates/org/apache/mahout/math/map/OpenObjectValueTypeHashMapTest.java.t @@ -0,0 +1,423 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#if (${valueTypeFloating} == 'true') +#set ($valueEpsilon = ", (${valueType})0.000001") +#else +#set ($valueEpsilon = "") +#end + +package org.apache.mahout.math.map; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.apache.mahout.math.function.Object${valueTypeCap}Procedure; +import org.apache.mahout.math.function.ObjectProcedure; +import org.apache.mahout.math.list.${valueTypeCap}ArrayList; +import org.apache.mahout.math.set.AbstractSet; +import org.junit.Assert; +import org.junit.Test; + +public class OpenObject${valueTypeCap}HashMapTest extends Assert { + + private static class NotComparableKey { + protected int x; + + public NotComparableKey(int x) { + this.x = x; + } + + @Override + public String toString() { + return "[k " + x + " ]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + x; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + NotComparableKey other = (NotComparableKey) obj; + return x == other.x; + } + } + + private final NotComparableKey[] ncKeys = { + new NotComparableKey(101), + new NotComparableKey(99), + new NotComparableKey(2), + new NotComparableKey(3), + new NotComparableKey(4), + new NotComparableKey(5) + }; + + @Test + public void testConstructors() { + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + int[] capacity = new int[1]; + double[] minLoadFactor = new double[1]; + double[] maxLoadFactor = new double[1]; + + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(AbstractSet.DEFAULT_CAPACITY, capacity[0]); + assertEquals(AbstractSet.DEFAULT_MAX_LOAD_FACTOR, maxLoadFactor[0], 0.001); + assertEquals(AbstractSet.DEFAULT_MIN_LOAD_FACTOR, minLoadFactor[0], 0.001); + int prime = PrimeFinder.nextPrime(907); + map = new OpenObject${valueTypeCap}HashMap<String>(prime); + + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(prime, capacity[0]); + assertEquals(AbstractSet.DEFAULT_MAX_LOAD_FACTOR, maxLoadFactor[0], 0.001); + assertEquals(AbstractSet.DEFAULT_MIN_LOAD_FACTOR, minLoadFactor[0], 0.001); + + map = new OpenObject${valueTypeCap}HashMap<String>(prime, 0.4, 0.8); + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(prime, capacity[0]); + assertEquals(0.4, minLoadFactor[0], 0.001); + assertEquals(0.8, maxLoadFactor[0], 0.001); + } + + @Test + public void testEnsureCapacity() { + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + int prime = PrimeFinder.nextPrime(907); + + map.ensureCapacity(prime); + int[] capacity = new int[1]; + double[] minLoadFactor = new double[1]; + double[] maxLoadFactor = new double[1]; + + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(prime, capacity[0]); + } + + @Test + public void testClear() { + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType})11); + assertEquals(1, map.size()); + map.clear(); + assertEquals(0, map.size()); + } + + @Test + @SuppressWarnings("unchecked") + public void testClone() { + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType})11); + OpenObject${valueTypeCap}HashMap<String> map2 = (OpenObject${valueTypeCap}HashMap<String>) map.clone(); + map.clear(); + assertEquals(1, map2.size()); + } + + @Test + public void testContainsKey() { + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType})11); + assertTrue(map.containsKey("Eleven")); + assertTrue(map.containsKey("Eleven")); + assertFalse(map.containsKey("Twelve")); + } + + @Test + public void testContainValue() { + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType})11); + assertTrue(map.containsValue((${valueType})11)); + assertFalse(map.containsValue((${valueType})12)); + } + + @Test + public void testForEachKey() { + final List<String> keys = new ArrayList<String>(); + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType}) 11); + map.put("Twelve", (${valueType}) 12); + map.put("Thirteen", (${valueType}) 13); + map.put("Fourteen", (${valueType}) 14); + map.removeKey("Thirteen"); + map.forEachKey(new ObjectProcedure<String>() { + + @Override + public boolean apply(String element) { + keys.add(element); + return true; + } + }); + + assertEquals(3, keys.size()); + Collections.sort(keys); + assertSame("Fourteen", keys.get(1)); + assertSame("Twelve", keys.get(2)); + assertSame("Eleven", keys.get(0)); + } + + private static class Pair implements Comparable<Pair> { + ${valueType} v; + String k; + + Pair(String k, ${valueType} v) { + this.k = k; + this.v = v; + } + + @Override + public int compareTo(Pair o) { + return k.compareTo(o.k); + } + } + + @Test + public void testForEachPair() { + final List<Pair> pairs = new ArrayList<Pair>(); + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType}) 11); + map.put("Twelve", (${valueType}) 12); + map.put("Thirteen", (${valueType}) 13); + map.put("Fourteen", (${valueType}) 14); + map.removeKey("Thirteen"); + map.forEachPair(new Object${valueTypeCap}Procedure<String>() { + + @Override + public boolean apply(String first, ${valueType} second) { + pairs.add(new Pair(first, second)); + return true; + } + }); + + Collections.sort(pairs); + assertEquals(3, pairs.size()); + assertEquals((${valueType})14, pairs.get(1).v ${valueEpsilon}); + assertSame("Fourteen", pairs.get(1).k); + assertEquals((${valueType}) 12, pairs.get(2).v ${valueEpsilon}); + assertSame("Twelve", pairs.get(2).k); + assertEquals((${valueType}) 11, pairs.get(0).v ${valueEpsilon}); + assertSame("Eleven", pairs.get(0).k); + + pairs.clear(); + map.forEachPair(new Object${valueTypeCap}Procedure<String>() { + int count = 0; + + @Override + public boolean apply(String first, ${valueType} second) { + pairs.add(new Pair(first, second)); + count++; + return count < 2; + } + }); + + assertEquals(2, pairs.size()); + } + + @Test + public void testGet() { + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType}) 11); + map.put("Twelve", (${valueType}) 12); + assertEquals((${valueType})11, map.get("Eleven") ${valueEpsilon}); + } + + @Test + public void testKeys() { + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType}) 11); + map.put("Twelve", (${valueType}) 12); + List<String> keys = new ArrayList<String>(); + map.keys(keys); + Collections.sort(keys); + assertSame("Twelve", keys.get(1)); + assertSame("Eleven", keys.get(0)); + List<String> k2 = map.keys(); + Collections.sort(k2); + assertEquals(keys, k2); + } + + @Test + public void testAdjustOrPutValue() { + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType}) 11); + map.put("Twelve", (${valueType}) 12); + map.put("Thirteen", (${valueType}) 13); + map.put("Fourteen", (${valueType}) 14); + map.adjustOrPutValue("Eleven", (${valueType})1, (${valueType})3); + assertEquals(14, map.get("Eleven") ${valueEpsilon}); + map.adjustOrPutValue("Fifteen", (${valueType})1, (${valueType})3); + assertEquals(1, map.get("Fifteen") ${valueEpsilon}); + } + + @Test + public void testPairsMatching() { + List<String> keyList = new ArrayList<String>(); + ${valueTypeCap}ArrayList valueList = new ${valueTypeCap}ArrayList(); + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType}) 11); + map.put("Twelve", (${valueType}) 12); + map.put("Thirteen", (${valueType}) 13); + map.put("Fourteen", (${valueType}) 14); + map.removeKey("Thirteen"); + map.pairsMatching(new Object${valueTypeCap}Procedure<String>() { + + @Override + public boolean apply(String first, ${valueType} second) { + return (second % 2) == 0; + }}, + keyList, valueList); + Collections.sort(keyList); + valueList.sort(); + assertEquals(2, keyList.size()); + assertEquals(2, valueList.size()); + assertSame("Fourteen", keyList.get(0)); + assertSame("Twelve", keyList.get(1)); + assertEquals((${valueType})14, valueList.get(1) ${valueEpsilon}); + assertEquals((${valueType})12, valueList.get(0) ${valueEpsilon}); + } + + @Test + public void testValues() { + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType}) 11); + map.put("Twelve", (${valueType}) 12); + map.put("Thirteen", (${valueType}) 13); + map.put("Fourteen", (${valueType}) 14); + map.removeKey("Thirteen"); + ${valueTypeCap}ArrayList values = new ${valueTypeCap}ArrayList(100); + map.values(values); + assertEquals(3, values.size()); + values.sort(); + assertEquals(11, values.get(0) ${valueEpsilon}); + assertEquals(12, values.get(1) ${valueEpsilon}); + assertEquals(14, values.get(2) ${valueEpsilon}); + } + + // tests of the code in the abstract class + + @Test + public void testCopy() { + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType})11); + OpenObject${valueTypeCap}HashMap<String> map2 = (OpenObject${valueTypeCap}HashMap<String>) map.copy(); + map.clear(); + assertEquals(1, map2.size()); + } + + @Test + public void testEquals() { + // since there are no other subclasses of + // Abstractxxx available, we have to just test the + // obvious. + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType}) 11); + map.put("Twelve", (${valueType}) 12); + map.put("Thirteen", (${valueType}) 13); + map.put("Fourteen", (${valueType}) 14); + map.removeKey("Thirteen"); + OpenObject${valueTypeCap}HashMap<String> map2 = (OpenObject${valueTypeCap}HashMap<String>) map.copy(); + assertEquals(map, map2); + assertTrue(map2.equals(map)); + assertFalse("Hello Sailor".equals(map)); + assertFalse(map.equals("hello sailor")); + map2.removeKey("Eleven"); + assertFalse(map.equals(map2)); + assertFalse(map2.equals(map)); + } + + // keys() tested in testKeys + + @Test + public void testKeysSortedByValue() { + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType}) 11); + map.put("Twelve", (${valueType}) 12); + map.put("Thirteen", (${valueType}) 13); + map.put("Fourteen", (${valueType}) 14); + map.removeKey("Thirteen"); + List<String> keys = new ArrayList<String>(); + map.keysSortedByValue(keys); + String[] keysArray = keys.toArray(new String[keys.size()]); + assertArrayEquals(new String[] {"Eleven", "Twelve", "Fourteen"}, + keysArray); + } + + @Test + public void testPairsSortedByKey() { + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType}) 11); + map.put("Twelve", (${valueType}) 12); + map.put("Thirteen", (${valueType}) 13); + map.put("Fourteen", (${valueType}) 14); + + ${valueTypeCap}ArrayList values = new ${valueTypeCap}ArrayList(); + List<String> keys = new ArrayList<String>(); + map.pairsSortedByKey(keys, values); + + assertEquals(4, keys.size()); + assertEquals(4, values.size()); + assertEquals((${valueType}) 11, values.get(0) ${valueEpsilon}); + assertSame("Eleven", keys.get(0)); + assertEquals((${valueType}) 14, values.get(1) ${valueEpsilon}); + assertSame("Fourteen", keys.get(1)); + assertEquals((${valueType}) 13, values.get(2) ${valueEpsilon}); + assertSame("Thirteen", keys.get(2)); + assertEquals((${valueType}) 12, values.get(3) ${valueEpsilon}); + assertSame("Twelve", keys.get(3)); + } + + @Test(expected=UnsupportedOperationException.class) + public void testPairsSortedByKeyNotComparable() { + OpenObject${valueTypeCap}HashMap<NotComparableKey> map = new OpenObject${valueTypeCap}HashMap<NotComparableKey>(); + map.put(ncKeys[0], (${valueType}) 11); + map.put(ncKeys[1], (${valueType}) 12); + map.put(ncKeys[2], (${valueType}) 13); + map.put(ncKeys[3], (${valueType}) 14); + ${valueTypeCap}ArrayList values = new ${valueTypeCap}ArrayList(); + List<NotComparableKey> keys = new ArrayList<NotComparableKey>(); + map.pairsSortedByKey(keys, values); + } + + @Test + public void testPairsSortedByValue() { + OpenObject${valueTypeCap}HashMap<String> map = new OpenObject${valueTypeCap}HashMap<String>(); + map.put("Eleven", (${valueType}) 11); + map.put("Twelve", (${valueType}) 12); + map.put("Thirteen", (${valueType}) 13); + map.put("Fourteen", (${valueType}) 14); + + List<String> keys = new ArrayList<String>(); + ${valueTypeCap}ArrayList values = new ${valueTypeCap}ArrayList(); + map.pairsSortedByValue(keys, values); + assertEquals((${valueType}) 11, values.get(0) ${valueEpsilon}); + assertEquals("Eleven", keys.get(0)); + assertEquals((${valueType}) 12, values.get(1) ${valueEpsilon}); + assertEquals("Twelve", keys.get(1)); + assertEquals((${valueType}) 13, values.get(2) ${valueEpsilon}); + assertEquals("Thirteen", keys.get(2)); + assertEquals((${valueType}) 14, values.get(3) ${valueEpsilon}); + assertEquals("Fourteen", keys.get(3)); + } + + } http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/test/java-templates/org/apache/mahout/math/set/OpenKeyTypeHashSetTest.java.t ---------------------------------------------------------------------- diff --git a/core/src/test/java-templates/org/apache/mahout/math/set/OpenKeyTypeHashSetTest.java.t b/core/src/test/java-templates/org/apache/mahout/math/set/OpenKeyTypeHashSetTest.java.t new file mode 100644 index 0000000..875f16b --- /dev/null +++ b/core/src/test/java-templates/org/apache/mahout/math/set/OpenKeyTypeHashSetTest.java.t @@ -0,0 +1,179 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#if (${keyTypeFloating} == 'true') +#set ($keyEpsilon = ", (${keyType})0.000001") +#else +#set ($keyEpsilon = "") +#end + +package org.apache.mahout.math.set; + +import java.util.Arrays; + +import org.apache.mahout.math.function.${keyTypeCap}Procedure; +import org.apache.mahout.math.list.${keyTypeCap}ArrayList; +import org.apache.mahout.math.map.PrimeFinder; + +import org.junit.Assert; +import org.junit.Test; + +public class Open${keyTypeCap}HashSetTest extends Assert { + + + @Test + public void testConstructors() { + Open${keyTypeCap}HashSet map = new Open${keyTypeCap}HashSet(); + int[] capacity = new int[1]; + double[] minLoadFactor = new double[1]; + double[] maxLoadFactor = new double[1]; + + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(AbstractSet.DEFAULT_CAPACITY, capacity[0]); + assertEquals(AbstractSet.DEFAULT_MAX_LOAD_FACTOR, maxLoadFactor[0], 0.001); + assertEquals(AbstractSet.DEFAULT_MIN_LOAD_FACTOR, minLoadFactor[0], 0.001); + int prime = PrimeFinder.nextPrime(907); + map = new Open${keyTypeCap}HashSet(prime); + + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(prime, capacity[0]); + assertEquals(AbstractSet.DEFAULT_MAX_LOAD_FACTOR, maxLoadFactor[0], 0.001); + assertEquals(AbstractSet.DEFAULT_MIN_LOAD_FACTOR, minLoadFactor[0], 0.001); + + map = new Open${keyTypeCap}HashSet(prime, 0.4, 0.8); + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(prime, capacity[0]); + assertEquals(0.4, minLoadFactor[0], 0.001); + assertEquals(0.8, maxLoadFactor[0], 0.001); + } + + @Test + public void testEnsureCapacity() { + Open${keyTypeCap}HashSet map = new Open${keyTypeCap}HashSet(); + int prime = PrimeFinder.nextPrime(907); + + map.ensureCapacity(prime); + int[] capacity = new int[1]; + double[] minLoadFactor = new double[1]; + double[] maxLoadFactor = new double[1]; + + map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); + assertEquals(prime, capacity[0]); + } + + @Test + public void testClear() { + Open${keyTypeCap}HashSet map = new Open${keyTypeCap}HashSet(); + map.add((${keyType}) 11); + assertEquals(1, map.size()); + map.clear(); + assertEquals(0, map.size()); + } + + @Test + public void testClone() { + Open${keyTypeCap}HashSet map = new Open${keyTypeCap}HashSet(); + map.add((${keyType}) 11); + Open${keyTypeCap}HashSet map2 = (Open${keyTypeCap}HashSet) map.clone(); + map.clear(); + assertEquals(1, map2.size()); + } + + @Test + public void testContains() { + Open${keyTypeCap}HashSet map = new Open${keyTypeCap}HashSet(); + map.add(($keyType) 11); + assertTrue(map.contains(($keyType) 11)); + assertFalse(map.contains(($keyType) 12)); + } + + @Test + public void testForEachKey() { + final ${keyTypeCap}ArrayList keys = new ${keyTypeCap}ArrayList(); + Open${keyTypeCap}HashSet map = new Open${keyTypeCap}HashSet(); + map.add(($keyType) 11); + map.add(($keyType) 12); + map.add(($keyType) 13); + map.add(($keyType) 14); + map.remove(($keyType) 13); + map.forEachKey(new ${keyTypeCap}Procedure() { + + @Override + public boolean apply(${keyType} element) { + keys.add(element); + return true; + } + }); + + ${keyType}[] keysArray = keys.toArray(new ${keyType}[keys.size()]); + Arrays.sort(keysArray); + + assertArrayEquals(new ${keyType}[] {11, 12, 14}, keysArray ${keyEpsilon}); + } + + @Test + public void testKeys() { + Open${keyTypeCap}HashSet map = new Open${keyTypeCap}HashSet(); + map.add(($keyType) 11); + map.add(($keyType) 12); + ${keyTypeCap}ArrayList keys = new ${keyTypeCap}ArrayList(); + map.keys(keys); + keys.sort(); + assertEquals(11, keys.get(0) ${keyEpsilon}); + assertEquals(12, keys.get(1) ${keyEpsilon}); + ${keyTypeCap}ArrayList k2 = map.keys(); + k2.sort(); + assertEquals(keys, k2); + } + + // tests of the code in the abstract class + + @Test + public void testCopy() { + Open${keyTypeCap}HashSet map = new Open${keyTypeCap}HashSet(); + map.add(($keyType) 11); + Open${keyTypeCap}HashSet map2 = (Open${keyTypeCap}HashSet) map.copy(); + map.clear(); + assertEquals(1, map2.size()); + } + + @Test + public void testEquals() { + // since there are no other subclasses of + // Abstractxxx available, we have to just test the + // obvious. + Open${keyTypeCap}HashSet map = new Open${keyTypeCap}HashSet(); + map.add(($keyType) 11); + map.add(($keyType) 12); + map.add(($keyType) 13); + map.add(($keyType) 14); + map.remove(($keyType) 13); + Open${keyTypeCap}HashSet map2 = (Open${keyTypeCap}HashSet) map.copy(); + assertTrue(map.equals(map2)); + assertTrue(map.hashCode() == map2.hashCode()); + assertTrue(map2.equals(map)); + assertTrue(map.hashCode() == map2.hashCode()); + assertFalse("Hello Sailor".equals(map)); + assertFalse(map.equals("hello sailor")); + map2.remove(($keyType) 11); + assertFalse(map.equals(map2)); + assertFalse(map2.equals(map)); + assertFalse(map.hashCode() == map2.hashCode()); + } + } http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/test/java/org/apache/mahout/common/RandomUtilsTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/mahout/common/RandomUtilsTest.java b/core/src/test/java/org/apache/mahout/common/RandomUtilsTest.java index d060528..973cee5 100644 --- a/core/src/test/java/org/apache/mahout/common/RandomUtilsTest.java +++ b/core/src/test/java/org/apache/mahout/common/RandomUtilsTest.java @@ -21,7 +21,6 @@ import org.apache.mahout.math.MahoutTestCase; import org.junit.Test; import java.util.Random; -import static org.junit.Assert.*; /** * Tests {@link RandomUtils}. http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/test/java/org/apache/mahout/math/AbstractVectorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/mahout/math/AbstractVectorTest.java b/core/src/test/java/org/apache/mahout/math/AbstractVectorTest.java index 633f35b..0576703 100644 --- a/core/src/test/java/org/apache/mahout/math/AbstractVectorTest.java +++ b/core/src/test/java/org/apache/mahout/math/AbstractVectorTest.java @@ -25,13 +25,10 @@ import org.apache.mahout.common.RandomUtils; import org.apache.mahout.math.Vector.Element; import org.apache.mahout.math.function.Functions; import org.apache.mahout.math.jet.random.Normal; -//import org.apache.mahout.math.random.MultiNormal; +import org.apache.mahout.math.random.MultiNormal; import org.junit.Before; - import org.junit.Test; -import static org.junit.Assert.*; - /** * Makes sure that a vector under test acts the same as a DenseVector or RandomAccessSparseVector * (according to whether it is dense or sparse). Most operations need to be done within a reasonable @@ -47,9 +44,9 @@ public abstract class AbstractVectorTest<T extends Vector> extends MahoutTestCas private static final double[] gold = {0.0, 1.1, 0.0, 2.2, 0.0, 3.3, 0.0}; private Vector test; - private static void checkIterator(Iterator<Element> nzIter, double[] values) { + private static void checkIterator(Iterator<Vector.Element> nzIter, double[] values) { while (nzIter.hasNext()) { - Element elt = nzIter.next(); + Vector.Element elt = nzIter.next(); assertEquals(elt.index() + " Value: " + values[elt.index()] + " does not equal: " + elt.get(), values[elt.index()], elt.get(), 0.0); } @@ -181,7 +178,7 @@ public abstract class AbstractVectorTest<T extends Vector> extends MahoutTestCas // getNumNondefaultElements - for (Element element : v1.all()) { + for (Vector.Element element : v1.all()) { assertEquals(dv1.get(element.index()), element.get(), 0); assertEquals(dv1.get(element.index()), v1.get(element.index()), 0); assertEquals(dv1.get(element.index()), v1.getQuick(element.index()), 0); @@ -213,7 +210,7 @@ public abstract class AbstractVectorTest<T extends Vector> extends MahoutTestCas @Test public void testIterator() { - Iterator<Element> iterator = test.nonZeroes().iterator(); + Iterator<Vector.Element> iterator = test.nonZeroes().iterator(); checkIterator(iterator, gold); iterator = test.all().iterator(); @@ -621,25 +618,25 @@ public abstract class AbstractVectorTest<T extends Vector> extends MahoutTestCas assertEquals(v0.zSum(), sum, 0); } -// @Test -// public void testSmallDistances() { -// for (double fuzz : new double[]{1.0e-5, 1.0e-6, 1.0e-7, 1.0e-8, 1.0e-9, 1.0e-10}) { -// MultiNormal x = new MultiNormal(fuzz, new ConstantVector(0, 20)); -// for (int i = 0; i < 10000; i++) { -// final T v1 = vectorToTest(20); -// Vector v2 = v1.plus(x.sample()); -// if (1 + fuzz * fuzz > 1) { -// String msg = String.format("fuzz = %.1g, >", fuzz); -// assertTrue(msg, v1.getDistanceSquared(v2) > 0); -// assertTrue(msg, v2.getDistanceSquared(v1) > 0); -// } else { -// String msg = String.format("fuzz = %.1g, >=", fuzz); -// assertTrue(msg, v1.getDistanceSquared(v2) >= 0); -// assertTrue(msg, v2.getDistanceSquared(v1) >= 0); -// } -// } -// } -// } + @Test + public void testSmallDistances() { + for (double fuzz : new double[]{1.0e-5, 1.0e-6, 1.0e-7, 1.0e-8, 1.0e-9, 1.0e-10}) { + MultiNormal x = new MultiNormal(fuzz, new ConstantVector(0, 20)); + for (int i = 0; i < 10000; i++) { + final T v1 = vectorToTest(20); + Vector v2 = v1.plus(x.sample()); + if (1 + fuzz * fuzz > 1) { + String msg = String.format("fuzz = %.1g, >", fuzz); + assertTrue(msg, v1.getDistanceSquared(v2) > 0); + assertTrue(msg, v2.getDistanceSquared(v1) > 0); + } else { + String msg = String.format("fuzz = %.1g, >=", fuzz); + assertTrue(msg, v1.getDistanceSquared(v2) >= 0); + assertTrue(msg, v2.getDistanceSquared(v1) >= 0); + } + } + } + } public void testToString() { http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/test/java/org/apache/mahout/math/CentroidTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/mahout/math/CentroidTest.java b/core/src/test/java/org/apache/mahout/math/CentroidTest.java index 734c2d0..07b13dd 100644 --- a/core/src/test/java/org/apache/mahout/math/CentroidTest.java +++ b/core/src/test/java/org/apache/mahout/math/CentroidTest.java @@ -1,72 +1,72 @@ -///* -// * Licensed to the Apache Software Foundation (ASF) under one or more -// * contributor license agreements. See the NOTICE file distributed with -// * this work for additional information regarding copyright ownership. -// * The ASF licenses this file to You under the Apache License, Version 2.0 -// * (the "License"); you may not use this file except in compliance with -// * the License. You may obtain a copy of the License at -// * -// * http://www.apache.org/licenses/LICENSE-2.0 -// * -// * Unless required by applicable law or agreed to in writing, software -// * distributed under the License is distributed on an "AS IS" BASIS, -// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// * See the License for the specific language governing permissions and -// * limitations under the License. -// */ -// -//package org.apache.mahout.math; -// -//import org.apache.mahout.math.function.Functions; -//import org.apache.mahout.math.random.MultiNormal; -//import org.junit.Test; -// -//public class CentroidTest extends AbstractVectorTest<Centroid> { -// @Test -// public void testUpdate() { -// MultiNormal f = new MultiNormal(20); -// -// Vector a = f.sample(); -// Vector b = f.sample(); -// Vector c = f.sample(); -// -// DenseVector x = new DenseVector(a); -// Centroid x1 = new Centroid(1, x); -// -// x1.update(new Centroid(2, new DenseVector(b))); -// Centroid x2 = new Centroid(x1); -// -// x1.update(c); -// -// // check for correct value -// Vector mean = a.plus(b).plus(c).assign(Functions.div(3)); -// assertEquals(0, x1.getVector().minus(mean).norm(1), 1.0e-8); -// assertEquals(3, x1.getWeight(), 0); -// -// assertEquals(0, x2.minus(a.plus(b).divide(2)).norm(1), 1.0e-8); -// assertEquals(2, x2.getWeight(), 0); -// -// assertEquals(0, new Centroid(x1.getIndex(), x1, x1.getWeight()).minus(x1).norm(1), 1.0e-8); -// -// // and verify shared storage -// assertEquals(0, x.minus(x1).norm(1), 0); -// -// assertEquals(3, x1.getWeight(), 1.0e-8); -// assertEquals(1, x1.getIndex()); -// } -// -// @Override -// public Centroid vectorToTest(int size) { -// return new Centroid(new WeightedVector(new DenseVector(size), 3.15, 51)); -// } -// -// @Override -// public void testSize() { -// assertEquals("size", 3, getTestVector().getNumNonZeroElements()); -// } -// -// @Override -// Vector generateTestVector(int cardinality) { -// return new Centroid(new WeightedVector(new DenseVector(cardinality), 3.14, 53)); -// } -//} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.mahout.math; + +import org.apache.mahout.math.function.Functions; +import org.apache.mahout.math.random.MultiNormal; +import org.junit.Test; + +public class CentroidTest extends AbstractVectorTest<Centroid> { + @Test + public void testUpdate() { + MultiNormal f = new MultiNormal(20); + + Vector a = f.sample(); + Vector b = f.sample(); + Vector c = f.sample(); + + DenseVector x = new DenseVector(a); + Centroid x1 = new Centroid(1, x); + + x1.update(new Centroid(2, new DenseVector(b))); + Centroid x2 = new Centroid(x1); + + x1.update(c); + + // check for correct value + Vector mean = a.plus(b).plus(c).assign(Functions.div(3)); + assertEquals(0, x1.getVector().minus(mean).norm(1), 1.0e-8); + assertEquals(3, x1.getWeight(), 0); + + assertEquals(0, x2.minus(a.plus(b).divide(2)).norm(1), 1.0e-8); + assertEquals(2, x2.getWeight(), 0); + + assertEquals(0, new Centroid(x1.getIndex(), x1, x1.getWeight()).minus(x1).norm(1), 1.0e-8); + + // and verify shared storage + assertEquals(0, x.minus(x1).norm(1), 0); + + assertEquals(3, x1.getWeight(), 1.0e-8); + assertEquals(1, x1.getIndex()); + } + + @Override + public Centroid vectorToTest(int size) { + return new Centroid(new WeightedVector(new DenseVector(size), 3.15, 51)); + } + + @Override + public void testSize() { + assertEquals("size", 3, getTestVector().getNumNonZeroElements()); + } + + @Override + Vector generateTestVector(int cardinality) { + return new Centroid(new WeightedVector(new DenseVector(cardinality), 3.14, 53)); + } +}
