You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2010/09/03 14:28:41 UTC
svn commit: r992277 [4/9] - in /mahout/trunk:
core/src/main/java/org/apache/mahout/ep/
core/src/main/java/org/apache/mahout/fpm/pfpgrowth/
core/src/test/java/org/apache/mahout/cf/taste/common/
core/src/test/java/org/apache/mahout/cf/taste/hadoop/ core/...
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java Fri Sep 3 12:28:34 2010
@@ -17,9 +17,8 @@
package org.apache.mahout.clustering.fuzzykmeans;
-import java.io.File;
-import java.io.IOException;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -32,6 +31,7 @@ import org.apache.hadoop.io.DataOutputBu
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
@@ -48,31 +48,18 @@ import org.apache.mahout.common.distance
import org.apache.mahout.math.DenseVector;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;
+import org.junit.Before;
+import org.junit.Test;
-public class TestFuzzyKmeansClustering extends MahoutTestCase {
+public final class TestFuzzyKmeansClustering extends MahoutTestCase {
private FileSystem fs;
-
- private DistanceMeasure measure = new EuclideanDistanceMeasure();
-
- private static void rmr(String path) {
- File f = new File(path);
- if (f.exists()) {
- if (f.isDirectory()) {
- String[] contents = f.list();
- for (String content : contents) {
- rmr(f.toString() + File.separator + content);
- }
- }
- f.delete();
- }
- }
+ private final DistanceMeasure measure = new EuclideanDistanceMeasure();
@Override
- protected void setUp() throws Exception {
+ @Before
+ public void setUp() throws Exception {
super.setUp();
- rmr("output");
- rmr("testdata");
Configuration conf = new Configuration();
fs = FileSystem.get(conf);
}
@@ -94,10 +81,9 @@ public class TestFuzzyKmeansClustering e
private static Vector tweakValue(Vector point) {
return point.plus(0.1);
-
}
- private static void computeCluster(List<Vector> points,
+ private static void computeCluster(Iterable<Vector> points,
List<SoftCluster> clusterList,
FuzzyKMeansClusterer clusterer,
Map<Integer, List<WeightedVectorWritable>> pointClusterInfo) {
@@ -151,9 +137,10 @@ public class TestFuzzyKmeansClustering e
}
}
+ @Test
public void testReferenceImplementation() throws Exception {
- List<Vector> points = TestKmeansClustering.getPoints(TestKmeansClustering.reference);
- EuclideanDistanceMeasure measure = new EuclideanDistanceMeasure();
+ List<Vector> points = TestKmeansClustering.getPoints(TestKmeansClustering.REFERENCE);
+ EuclideanDistanceMeasure measure = new EuclideanDistanceMeasure();
for (int k = 0; k < points.size(); k++) {
System.out.println("test k= " + k);
@@ -188,8 +175,9 @@ public class TestFuzzyKmeansClustering e
}
}
+ @Test
public void testFuzzyKMeansSeqJob() throws Exception {
- List<VectorWritable> points = TestKmeansClustering.getPointsWritable(TestKmeansClustering.reference);
+ List<VectorWritable> points = TestKmeansClustering.getPointsWritable(TestKmeansClustering.REFERENCE);
Path pointsPath = getTestTempDirPath("points");
Path clustersPath = getTestTempDirPath("clusters");
@@ -232,17 +220,30 @@ public class TestFuzzyKmeansClustering e
true,
0);
*/
- String[] args = { optKey(DefaultOptionCreator.INPUT_OPTION), pointsPath.toString(),
- optKey(DefaultOptionCreator.CLUSTERS_IN_OPTION), clustersPath.toString(), optKey(DefaultOptionCreator.OUTPUT_OPTION),
- output.toString(), optKey(DefaultOptionCreator.DISTANCE_MEASURE_OPTION), EuclideanDistanceMeasure.class.getName(),
- optKey(DefaultOptionCreator.CONVERGENCE_DELTA_OPTION), "0.001", optKey(DefaultOptionCreator.MAX_ITERATIONS_OPTION), "2",
- optKey(FuzzyKMeansDriver.M_OPTION), "2.0", optKey(DefaultOptionCreator.CLUSTERING_OPTION),
- optKey(DefaultOptionCreator.EMIT_MOST_LIKELY_OPTION), optKey(DefaultOptionCreator.OVERWRITE_OPTION),
- optKey(DefaultOptionCreator.METHOD_OPTION), DefaultOptionCreator.SEQUENTIAL_METHOD };
+ String[] args = {
+ optKey(DefaultOptionCreator.INPUT_OPTION), pointsPath.toString(),
+ optKey(DefaultOptionCreator.CLUSTERS_IN_OPTION),
+ clustersPath.toString(),
+ optKey(DefaultOptionCreator.OUTPUT_OPTION),
+ output.toString(),
+ optKey(DefaultOptionCreator.DISTANCE_MEASURE_OPTION),
+ EuclideanDistanceMeasure.class.getName(),
+ optKey(DefaultOptionCreator.CONVERGENCE_DELTA_OPTION),
+ "0.001",
+ optKey(DefaultOptionCreator.MAX_ITERATIONS_OPTION),
+ "2",
+ optKey(FuzzyKMeansDriver.M_OPTION),
+ "2.0",
+ optKey(DefaultOptionCreator.CLUSTERING_OPTION),
+ optKey(DefaultOptionCreator.EMIT_MOST_LIKELY_OPTION),
+ optKey(DefaultOptionCreator.OVERWRITE_OPTION),
+ optKey(DefaultOptionCreator.METHOD_OPTION),
+ DefaultOptionCreator.SEQUENTIAL_METHOD
+ };
new FuzzyKMeansDriver().run(args);
SequenceFile.Reader reader = new SequenceFile.Reader(fs, new Path(output, "clusteredPoints/part-m-0"), conf);
- IntWritable key = new IntWritable();
- WeightedVectorWritable out = new WeightedVectorWritable();
+ Writable key = new IntWritable();
+ Writable out = new WeightedVectorWritable();
while (reader.next(key, out)) {
// make sure we can read all the clusters
}
@@ -251,8 +252,9 @@ public class TestFuzzyKmeansClustering e
}
+ @Test
public void testFuzzyKMeansMRJob() throws Exception {
- List<VectorWritable> points = TestKmeansClustering.getPointsWritable(TestKmeansClustering.reference);
+ List<VectorWritable> points = TestKmeansClustering.getPointsWritable(TestKmeansClustering.REFERENCE);
Path pointsPath = getTestTempDirPath("points");
Path clustersPath = getTestTempDirPath("clusters");
@@ -295,16 +297,29 @@ public class TestFuzzyKmeansClustering e
true,
0);
*/
- String[] args = { optKey(DefaultOptionCreator.INPUT_OPTION), pointsPath.toString(),
- optKey(DefaultOptionCreator.CLUSTERS_IN_OPTION), clustersPath.toString(), optKey(DefaultOptionCreator.OUTPUT_OPTION),
- output.toString(), optKey(DefaultOptionCreator.DISTANCE_MEASURE_OPTION), EuclideanDistanceMeasure.class.getName(),
- optKey(DefaultOptionCreator.CONVERGENCE_DELTA_OPTION), "0.001", optKey(DefaultOptionCreator.MAX_ITERATIONS_OPTION), "2",
- optKey(FuzzyKMeansDriver.M_OPTION), "2.0", optKey(DefaultOptionCreator.CLUSTERING_OPTION),
- optKey(DefaultOptionCreator.EMIT_MOST_LIKELY_OPTION), optKey(DefaultOptionCreator.OVERWRITE_OPTION) };
+ String[] args = {
+ optKey(DefaultOptionCreator.INPUT_OPTION),
+ pointsPath.toString(),
+ optKey(DefaultOptionCreator.CLUSTERS_IN_OPTION),
+ clustersPath.toString(),
+ optKey(DefaultOptionCreator.OUTPUT_OPTION),
+ output.toString(),
+ optKey(DefaultOptionCreator.DISTANCE_MEASURE_OPTION),
+ EuclideanDistanceMeasure.class.getName(),
+ optKey(DefaultOptionCreator.CONVERGENCE_DELTA_OPTION),
+ "0.001",
+ optKey(DefaultOptionCreator.MAX_ITERATIONS_OPTION),
+ "2",
+ optKey(FuzzyKMeansDriver.M_OPTION),
+ "2.0",
+ optKey(DefaultOptionCreator.CLUSTERING_OPTION),
+ optKey(DefaultOptionCreator.EMIT_MOST_LIKELY_OPTION),
+ optKey(DefaultOptionCreator.OVERWRITE_OPTION)
+ };
new FuzzyKMeansDriver().run(args);
SequenceFile.Reader reader = new SequenceFile.Reader(fs, new Path(output, "clusteredPoints/part-m-00000"), conf);
- IntWritable key = new IntWritable();
- WeightedVectorWritable out = new WeightedVectorWritable();
+ Writable key = new IntWritable();
+ Writable out = new WeightedVectorWritable();
while (reader.next(key, out)) {
// make sure we can read all the clusters
}
@@ -313,8 +328,9 @@ public class TestFuzzyKmeansClustering e
}
+ @Test
public void testFuzzyKMeansMapper() throws Exception {
- List<VectorWritable> points = TestKmeansClustering.getPointsWritable(TestKmeansClustering.reference);
+ List<VectorWritable> points = TestKmeansClustering.getPointsWritable(TestKmeansClustering.REFERENCE);
for (int k = 0; k < points.size(); k++) {
System.out.println("testKFuzzyKMeansMRJob k= " + k);
@@ -370,13 +386,14 @@ public class TestFuzzyKmeansClustering e
Vector key = entry.getKey();
double value = round(entry.getValue(), 1);
- assertEquals("total Prob for Point:" + key, 1.0, value);
+ assertEquals("total Prob for Point:" + key, 1.0, value, EPSILON);
}
}
}
+ @Test
public void testFuzzyKMeansCombiner() throws Exception {
- List<VectorWritable> points = TestKmeansClustering.getPointsWritable(TestKmeansClustering.reference);
+ List<VectorWritable> points = TestKmeansClustering.getPointsWritable(TestKmeansClustering.REFERENCE);
for (int k = 0; k < points.size(); k++) {
System.out.println("testKFuzzyKMeansMRJob k= " + k);
@@ -396,15 +413,16 @@ public class TestFuzzyKmeansClustering e
mapper.config(clusterList);
Configuration conf = new Configuration();
- conf.set(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY, "org.apache.mahout.common.distance.EuclideanDistanceMeasure");
+ conf.set(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY,
+ "org.apache.mahout.common.distance.EuclideanDistanceMeasure");
conf.set(FuzzyKMeansConfigKeys.CLUSTER_CONVERGENCE_KEY, "0.001");
conf.set(FuzzyKMeansConfigKeys.M_KEY, "2");
conf.set(FuzzyKMeansConfigKeys.EMIT_MOST_LIKELY_KEY, "true");
conf.set(FuzzyKMeansConfigKeys.THRESHOLD_KEY, "0");
DummyRecordWriter<Text, ClusterObservations> mapWriter = new DummyRecordWriter<Text, ClusterObservations>();
- Mapper<WritableComparable<?>, VectorWritable, Text, ClusterObservations>.Context mapContext = DummyRecordWriter
- .build(mapper, conf, mapWriter);
+ Mapper<WritableComparable<?>, VectorWritable, Text, ClusterObservations>.Context mapContext =
+ DummyRecordWriter.build(mapper, conf, mapWriter);
mapper.setup(mapContext);
for (VectorWritable point : points) {
mapper.map(new Text(), point, mapContext);
@@ -413,8 +431,8 @@ public class TestFuzzyKmeansClustering e
// run combiner
FuzzyKMeansCombiner combiner = new FuzzyKMeansCombiner();
DummyRecordWriter<Text, ClusterObservations> combinerWriter = new DummyRecordWriter<Text, ClusterObservations>();
- Reducer<Text, ClusterObservations, Text, ClusterObservations>.Context combinerContext = DummyRecordWriter
- .build(combiner, conf, combinerWriter, Text.class, ClusterObservations.class);
+ Reducer<Text, ClusterObservations, Text, ClusterObservations>.Context combinerContext =
+ DummyRecordWriter.build(combiner, conf, combinerWriter, Text.class, ClusterObservations.class);
combiner.setup(combinerContext);
for (Text key : mapWriter.getKeys()) {
List<ClusterObservations> values = mapWriter.getValue(key);
@@ -431,8 +449,9 @@ public class TestFuzzyKmeansClustering e
}
}
+ @Test
public void testFuzzyKMeansReducer() throws Exception {
- List<VectorWritable> points = TestKmeansClustering.getPointsWritable(TestKmeansClustering.reference);
+ List<VectorWritable> points = TestKmeansClustering.getPointsWritable(TestKmeansClustering.REFERENCE);
for (int k = 0; k < points.size(); k++) {
System.out.println("testKFuzzyKMeansMRJob k= " + k);
@@ -459,8 +478,8 @@ public class TestFuzzyKmeansClustering e
conf.set(FuzzyKMeansConfigKeys.THRESHOLD_KEY, "0");
DummyRecordWriter<Text, ClusterObservations> mapWriter = new DummyRecordWriter<Text, ClusterObservations>();
- Mapper<WritableComparable<?>, VectorWritable, Text, ClusterObservations>.Context mapContext = DummyRecordWriter
- .build(mapper, conf, mapWriter);
+ Mapper<WritableComparable<?>, VectorWritable, Text, ClusterObservations>.Context mapContext =
+ DummyRecordWriter.build(mapper, conf, mapWriter);
mapper.setup(mapContext);
for (VectorWritable point : points) {
mapper.map(new Text(), point, mapContext);
@@ -469,8 +488,8 @@ public class TestFuzzyKmeansClustering e
// run combiner
FuzzyKMeansCombiner combiner = new FuzzyKMeansCombiner();
DummyRecordWriter<Text, ClusterObservations> combinerWriter = new DummyRecordWriter<Text, ClusterObservations>();
- Reducer<Text, ClusterObservations, Text, ClusterObservations>.Context combinerContext = DummyRecordWriter
- .build(combiner, conf, combinerWriter, Text.class, ClusterObservations.class);
+ Reducer<Text, ClusterObservations, Text, ClusterObservations>.Context combinerContext =
+ DummyRecordWriter.build(combiner, conf, combinerWriter, Text.class, ClusterObservations.class);
combiner.setup(combinerContext);
for (Text key : mapWriter.getKeys()) {
List<ClusterObservations> values = mapWriter.getValue(key);
@@ -480,8 +499,8 @@ public class TestFuzzyKmeansClustering e
// run reducer
FuzzyKMeansReducer reducer = new FuzzyKMeansReducer();
DummyRecordWriter<Text, SoftCluster> reducerWriter = new DummyRecordWriter<Text, SoftCluster>();
- Reducer<Text, ClusterObservations, Text, SoftCluster>.Context reducerContext = DummyRecordWriter
- .build(reducer, conf, reducerWriter, Text.class, ClusterObservations.class);
+ Reducer<Text, ClusterObservations, Text, SoftCluster>.Context reducerContext =
+ DummyRecordWriter.build(reducer, conf, reducerWriter, Text.class, ClusterObservations.class);
reducer.setup(clusterList, conf);
for (Text key : combinerWriter.getKeys()) {
@@ -498,7 +517,7 @@ public class TestFuzzyKmeansClustering e
Vector vec = tweakValue(points.get(i).get());
reference.add(new SoftCluster(vec, i, measure));
}
- List<Vector> pointsVectors = new ArrayList<Vector>();
+ Collection<Vector> pointsVectors = new ArrayList<Vector>();
for (VectorWritable point : points) {
pointsVectors.add(point.get());
}
@@ -518,8 +537,9 @@ public class TestFuzzyKmeansClustering e
}
}
+ @Test
public void testFuzzyKMeansClusterMapper() throws Exception {
- List<VectorWritable> points = TestKmeansClustering.getPointsWritable(TestKmeansClustering.reference);
+ List<VectorWritable> points = TestKmeansClustering.getPointsWritable(TestKmeansClustering.REFERENCE);
for (int k = 0; k < points.size(); k++) {
System.out.println("testKFuzzyKMeansMRJob k= " + k);
@@ -550,8 +570,8 @@ public class TestFuzzyKmeansClustering e
conf.set(FuzzyKMeansConfigKeys.THRESHOLD_KEY, "0");
DummyRecordWriter<Text, ClusterObservations> mapWriter = new DummyRecordWriter<Text, ClusterObservations>();
- Mapper<WritableComparable<?>, VectorWritable, Text, ClusterObservations>.Context mapContext = DummyRecordWriter
- .build(mapper, conf, mapWriter);
+ Mapper<WritableComparable<?>, VectorWritable, Text, ClusterObservations>.Context mapContext =
+ DummyRecordWriter.build(mapper, conf, mapWriter);
mapper.setup(mapContext);
for (VectorWritable point : points) {
mapper.map(new Text(), point, mapContext);
@@ -560,8 +580,8 @@ public class TestFuzzyKmeansClustering e
// run combiner
FuzzyKMeansCombiner combiner = new FuzzyKMeansCombiner();
DummyRecordWriter<Text, ClusterObservations> combinerWriter = new DummyRecordWriter<Text, ClusterObservations>();
- Reducer<Text, ClusterObservations, Text, ClusterObservations>.Context combinerContext = DummyRecordWriter
- .build(combiner, conf, combinerWriter, Text.class, ClusterObservations.class);
+ Reducer<Text, ClusterObservations, Text, ClusterObservations>.Context combinerContext =
+ DummyRecordWriter.build(combiner, conf, combinerWriter, Text.class, ClusterObservations.class);
combiner.setup(combinerContext);
for (Text key : mapWriter.getKeys()) {
List<ClusterObservations> values = mapWriter.getValue(key);
@@ -571,8 +591,8 @@ public class TestFuzzyKmeansClustering e
// run reducer
FuzzyKMeansReducer reducer = new FuzzyKMeansReducer();
DummyRecordWriter<Text, SoftCluster> reducerWriter = new DummyRecordWriter<Text, SoftCluster>();
- Reducer<Text, ClusterObservations, Text, SoftCluster>.Context reducerContext = DummyRecordWriter
- .build(reducer, conf, reducerWriter, Text.class, ClusterObservations.class);
+ Reducer<Text, ClusterObservations, Text, SoftCluster>.Context reducerContext =
+ DummyRecordWriter.build(reducer, conf, reducerWriter, Text.class, ClusterObservations.class);
reducer.setup(clusterList, conf);
for (Text key : combinerWriter.getKeys()) {
@@ -592,8 +612,8 @@ public class TestFuzzyKmeansClustering e
FuzzyKMeansClusterMapper clusterMapper = new FuzzyKMeansClusterMapper();
DummyRecordWriter<IntWritable, WeightedVectorWritable> clusterWriter = new DummyRecordWriter<IntWritable, WeightedVectorWritable>();
- Mapper<WritableComparable<?>, VectorWritable, IntWritable, WeightedVectorWritable>.Context clusterContext = DummyRecordWriter
- .build(clusterMapper, conf, clusterWriter);
+ Mapper<WritableComparable<?>, VectorWritable, IntWritable, WeightedVectorWritable>.Context clusterContext =
+ DummyRecordWriter.build(clusterMapper, conf, clusterWriter);
clusterMapper.setup(reducerClusters, conf);
for (VectorWritable point : points) {
@@ -607,7 +627,7 @@ public class TestFuzzyKmeansClustering e
reference.add(new SoftCluster(vec, i, measure));
}
Map<Integer, List<WeightedVectorWritable>> refClusters = new HashMap<Integer, List<WeightedVectorWritable>>();
- List<Vector> pointsVectors = new ArrayList<Vector>();
+ Collection<Vector> pointsVectors = new ArrayList<Vector>();
for (VectorWritable point : points) {
pointsVectors.add((Vector) point.get());
}
@@ -640,7 +660,8 @@ public class TestFuzzyKmeansClustering e
}
}
- public void testClusterObservationsSerialization() throws IOException {
+ @Test
+ public void testClusterObservationsSerialization() throws Exception {
double[] data = { 1.1, 2.2, 3.3 };
Vector vector = new DenseVector(data);
ClusterObservations reference = new ClusterObservations(1, 2.0, vector, vector);
@@ -650,8 +671,8 @@ public class TestFuzzyKmeansClustering e
DataInputBuffer in = new DataInputBuffer();
in.reset(out.getData(), out.getLength());
info.readFields(in);
- assertEquals("probability", reference.getS0(), info.getS0());
- assertTrue("point total", reference.getS1().equals(info.getS1()));
+ assertEquals("probability", reference.getS0(), info.getS0(), EPSILON);
+ assertEquals("point total", reference.getS1(), info.getS1());
assertEquals("combiner", reference.getCombinerState(), info.getCombinerState());
}
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestKmeansClustering.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestKmeansClustering.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestKmeansClustering.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestKmeansClustering.java Fri Sep 3 12:28:34 2010
@@ -19,6 +19,7 @@ package org.apache.mahout.clustering.kme
import java.io.File;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -49,35 +50,24 @@ import org.apache.mahout.math.RandomAcce
import org.apache.mahout.math.SequentialAccessSparseVector;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;
+import org.junit.Before;
+import org.junit.Test;
-public class TestKmeansClustering extends MahoutTestCase {
+public final class TestKmeansClustering extends MahoutTestCase {
- public static final double[][] reference = { { 1, 1 }, { 2, 1 }, { 1, 2 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 4 }, { 4, 5 },
- { 5, 5 } };
+ public static final double[][] REFERENCE = {
+ { 1, 1 }, { 2, 1 }, { 1, 2 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 4 }, { 4, 5 }, { 5, 5 } };
- private static final int[][] expectedNumPoints = { { 9 }, { 4, 5 }, { 4, 4, 1 }, { 1, 2, 1, 5 }, { 1, 1, 1, 2, 4 },
+ private static final int[][] EXPECTED_NUM_POINTS =
+ { { 9 }, { 4, 5 }, { 4, 4, 1 }, { 1, 2, 1, 5 }, { 1, 1, 1, 2, 4 },
{ 1, 1, 1, 1, 1, 4 }, { 1, 1, 1, 1, 1, 2, 2 }, { 1, 1, 1, 1, 1, 1, 2, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
private FileSystem fs;
- private static void rmr(String path) {
- File f = new File(path);
- if (f.exists()) {
- if (f.isDirectory()) {
- String[] contents = f.list();
- for (String content : contents) {
- rmr(f.toString() + File.separator + content);
- }
- }
- f.delete();
- }
- }
-
@Override
- protected void setUp() throws Exception {
+ @Before
+ public void setUp() throws Exception {
super.setUp();
- rmr("output");
- rmr("testdata");
Configuration conf = new Configuration();
fs = FileSystem.get(conf);
}
@@ -103,8 +93,9 @@ public class TestKmeansClustering extend
}
/** Story: Test the reference implementation */
+ @Test
public void testReferenceImplementation() throws Exception {
- List<Vector> points = getPoints(reference);
+ List<Vector> points = getPoints(REFERENCE);
DistanceMeasure measure = new EuclideanDistanceMeasure();
// try all possible values of k
for (int k = 0; k < points.size(); k++) {
@@ -122,12 +113,12 @@ public class TestKmeansClustering extend
for (int c = 0; c < clusters.size(); c++) {
AbstractCluster cluster = clusters.get(c);
System.out.println(cluster.asFormatString(null));
- assertEquals("Cluster " + c + " test " + (k + 1), expectedNumPoints[k][c], cluster.getNumPoints());
+ assertEquals("Cluster " + c + " test " + (k + 1), EXPECTED_NUM_POINTS[k][c], cluster.getNumPoints());
}
}
}
- private static Map<String, Cluster> loadClusterMap(List<Cluster> clusters) {
+ private static Map<String, Cluster> loadClusterMap(Iterable<Cluster> clusters) {
Map<String, Cluster> clusterMap = new HashMap<String, Cluster>();
for (Cluster cluster : clusters) {
@@ -137,6 +128,7 @@ public class TestKmeansClustering extend
}
/** Story: test that the mapper will map input points to the nearest cluster */
+ @Test
public void testKMeansMapper() throws Exception {
KMeansMapper mapper = new KMeansMapper();
EuclideanDistanceMeasure measure = new EuclideanDistanceMeasure();
@@ -144,7 +136,7 @@ public class TestKmeansClustering extend
conf.set(KMeansConfigKeys.DISTANCE_MEASURE_KEY, measure.getClass().getName());
conf.set(KMeansConfigKeys.CLUSTER_CONVERGENCE_KEY, "0.001");
conf.set(KMeansConfigKeys.CLUSTER_PATH_KEY, "");
- List<VectorWritable> points = getPointsWritable(reference);
+ List<VectorWritable> points = getPointsWritable(REFERENCE);
for (int k = 0; k < points.size(); k++) {
// pick k initial cluster centers at random
DummyRecordWriter<Text, ClusterObservations> mapWriter = new DummyRecordWriter<Text, ClusterObservations>();
@@ -183,6 +175,7 @@ public class TestKmeansClustering extend
* Story: test that the combiner will produce partial cluster totals for all of the clusters and points that
* it sees
*/
+ @Test
public void testKMeansCombiner() throws Exception {
KMeansMapper mapper = new KMeansMapper();
EuclideanDistanceMeasure measure = new EuclideanDistanceMeasure();
@@ -190,7 +183,7 @@ public class TestKmeansClustering extend
conf.set(KMeansConfigKeys.DISTANCE_MEASURE_KEY, measure.getClass().getName());
conf.set(KMeansConfigKeys.CLUSTER_CONVERGENCE_KEY, "0.001");
conf.set(KMeansConfigKeys.CLUSTER_PATH_KEY, "");
- List<VectorWritable> points = getPointsWritable(reference);
+ List<VectorWritable> points = getPointsWritable(REFERENCE);
for (int k = 0; k < points.size(); k++) {
// pick k initial cluster centers at random
DummyRecordWriter<Text, ClusterObservations> mapWriter = new DummyRecordWriter<Text, ClusterObservations>();
@@ -241,6 +234,7 @@ public class TestKmeansClustering extend
* Story: test that the reducer will sum the partial cluster totals for all of the clusters and points that
* it sees
*/
+ @Test
public void testKMeansReducer() throws Exception {
KMeansMapper mapper = new KMeansMapper();
EuclideanDistanceMeasure measure = new EuclideanDistanceMeasure();
@@ -248,7 +242,7 @@ public class TestKmeansClustering extend
conf.set(KMeansConfigKeys.DISTANCE_MEASURE_KEY, measure.getClass().getName());
conf.set(KMeansConfigKeys.CLUSTER_CONVERGENCE_KEY, "0.001");
conf.set(KMeansConfigKeys.CLUSTER_PATH_KEY, "");
- List<VectorWritable> points = getPointsWritable(reference);
+ List<VectorWritable> points = getPointsWritable(REFERENCE);
for (int k = 0; k < points.size(); k++) {
System.out.println("K = " + k);
// pick k initial cluster centers at random
@@ -281,11 +275,8 @@ public class TestKmeansClustering extend
KMeansReducer reducer = new KMeansReducer();
reducer.setup(clusters, measure);
DummyRecordWriter<Text, Cluster> reducerWriter = new DummyRecordWriter<Text, Cluster>();
- Reducer<Text, ClusterObservations, Text, Cluster>.Context reducerContext = DummyRecordWriter.build(reducer,
- conf,
- reducerWriter,
- Text.class,
- ClusterObservations.class);
+ Reducer<Text, ClusterObservations, Text, Cluster>.Context reducerContext =
+ DummyRecordWriter.build(reducer, conf, reducerWriter, Text.class, ClusterObservations.class);
for (Text key : combinerWriter.getKeys()) {
reducer.reduce(new Text(key), combinerWriter.getValue(key), reducerContext);
}
@@ -293,12 +284,12 @@ public class TestKmeansClustering extend
assertEquals("Number of map results", k + 1, reducerWriter.getData().size());
// compute the reference result after one iteration and compare
- List<Cluster> reference = new ArrayList<Cluster>();
+ Collection<Cluster> reference = new ArrayList<Cluster>();
for (int i = 0; i < k + 1; i++) {
Vector vec = points.get(i).get();
reference.add(new Cluster(vec, i, measure));
}
- List<Vector> pointsVectors = new ArrayList<Vector>();
+ Collection<Vector> pointsVectors = new ArrayList<Vector>();
for (VectorWritable point : points) {
pointsVectors.add(point.get());
}
@@ -330,9 +321,10 @@ public class TestKmeansClustering extend
}
/** Story: User wishes to run kmeans job on reference data */
+ @Test
public void testKMeansSeqJob() throws Exception {
DistanceMeasure measure = new EuclideanDistanceMeasure();
- List<VectorWritable> points = getPointsWritable(reference);
+ List<VectorWritable> points = getPointsWritable(REFERENCE);
Path pointsPath = getTestTempDirPath("points");
Path clustersPath = getTestTempDirPath("clusters");
@@ -369,7 +361,7 @@ public class TestKmeansClustering extend
// now compare the expected clusters with actual
Path clusteredPointsPath = new Path(outputPath, "clusteredPoints");
SequenceFile.Reader reader = new SequenceFile.Reader(fs, new Path(clusteredPointsPath, "part-m-0"), conf);
- int[] expect = expectedNumPoints[k];
+ int[] expect = EXPECTED_NUM_POINTS[k];
DummyOutputCollector<IntWritable, WeightedVectorWritable> collector = new DummyOutputCollector<IntWritable, WeightedVectorWritable>();
// The key is the clusterId
IntWritable clusterId = new IntWritable(0);
@@ -386,9 +378,10 @@ public class TestKmeansClustering extend
}
/** Story: User wishes to run kmeans job on reference data */
+ @Test
public void testKMeansMRJob() throws Exception {
DistanceMeasure measure = new EuclideanDistanceMeasure();
- List<VectorWritable> points = getPointsWritable(reference);
+ List<VectorWritable> points = getPointsWritable(REFERENCE);
Path pointsPath = getTestTempDirPath("points");
Path clustersPath = getTestTempDirPath("clusters");
@@ -425,7 +418,7 @@ public class TestKmeansClustering extend
Path clusteredPointsPath = new Path(outputPath, "clusteredPoints");
// assertEquals("output dir files?", 4, outFiles.length);
SequenceFile.Reader reader = new SequenceFile.Reader(fs, new Path(clusteredPointsPath, "part-m-00000"), conf);
- int[] expect = expectedNumPoints[k];
+ int[] expect = EXPECTED_NUM_POINTS[k];
DummyOutputCollector<IntWritable, WeightedVectorWritable> collector = new DummyOutputCollector<IntWritable, WeightedVectorWritable>();
// The key is the clusterId
IntWritable clusterId = new IntWritable(0);
@@ -447,8 +440,9 @@ public class TestKmeansClustering extend
}
/** Story: User wants to use canopy clustering to input the initial clusters for kmeans job. */
+ @Test
public void testKMeansWithCanopyClusterInput() throws Exception {
- List<VectorWritable> points = getPointsWritable(reference);
+ List<VectorWritable> points = getPointsWritable(REFERENCE);
Path pointsPath = getTestTempDirPath("points");
Configuration conf = new Configuration();
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestRandomSeedGenerator.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestRandomSeedGenerator.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestRandomSeedGenerator.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestRandomSeedGenerator.java Fri Sep 3 12:28:34 2010
@@ -19,6 +19,7 @@ package org.apache.mahout.clustering.kme
import java.io.File;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -36,46 +37,36 @@ import org.apache.mahout.common.distance
import org.apache.mahout.math.RandomAccessSparseVector;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;
+import org.junit.Before;
+import org.junit.Test;
-public class TestRandomSeedGenerator extends MahoutTestCase {
+public final class TestRandomSeedGenerator extends MahoutTestCase {
- private static final double[][] raw = {{1, 1}, {2, 1}, {1, 2}, {2, 2},
+ private static final double[][] RAW = {{1, 1}, {2, 1}, {1, 2}, {2, 2},
{3, 3}, {4, 4}, {5, 4}, {4, 5}, {5, 5}};
private FileSystem fs;
private static List<VectorWritable> getPoints() {
List<VectorWritable> points = new ArrayList<VectorWritable>();
- for (double[] fr : raw) {
+ for (double[] fr : RAW) {
Vector vec = new RandomAccessSparseVector(fr.length);
vec.assign(fr);
points.add(new VectorWritable(vec));
}
return points;
}
-
- private static void rmr(String path) throws Exception {
- File f = new File(path);
- if (f.exists()) {
- if (f.isDirectory()) {
- String[] contents = f.list();
- for (String content : contents) {
- rmr(f.toString() + File.separator + content);
- }
- }
- f.delete();
- }
- }
-
+
@Override
+ @Before
public void setUp() throws Exception {
super.setUp();
- rmr("testdata");
Configuration conf = new Configuration();
fs = FileSystem.get(conf);
}
/** Story: test random seed generation generates 4 clusters with proper ids and data */
+ @Test
public void testRandomSeedGenerator() throws Exception {
List<VectorWritable> points = getPoints();
Job job = new Job();
@@ -92,14 +83,14 @@ public class TestRandomSeedGenerator ext
AbstractCluster value = (AbstractCluster) reader.getValueClass().newInstance();
int clusterCount = 0;
- Set<Integer> set = new HashSet<Integer>();
+ Collection<Integer> set = new HashSet<Integer>();
while (reader.next(key, value)) {
clusterCount++;
int id = value.getId();
assertTrue(set.add(id)); // validate unique id's
Vector v = value.getCenter();
- assertVectorEquals(raw[id], v); // validate values match
+ assertVectorEquals(RAW[id], v); // validate values match
}
assertEquals(4, clusterCount); // validate sample count
@@ -108,7 +99,7 @@ public class TestRandomSeedGenerator ext
private static void assertVectorEquals(double[] raw, Vector v) {
assertEquals(raw.length, v.size());
for (int i=0; i < raw.length; i++) {
- assertEquals(raw[i], v.getQuick(i));
+ assertEquals(raw[i], v.getQuick(i), EPSILON);
}
}
}
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/lda/TestLDAInference.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/lda/TestLDAInference.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/lda/TestLDAInference.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/lda/TestLDAInference.java Fri Sep 3 12:28:34 2010
@@ -20,7 +20,7 @@ package org.apache.mahout.clustering.lda
import java.util.Iterator;
import java.util.Random;
-import org.apache.commons.math.distribution.PoissonDistribution;
+import org.apache.commons.math.distribution.IntegerDistribution;
import org.apache.commons.math.distribution.PoissonDistributionImpl;
import org.apache.commons.math.MathException;
@@ -30,15 +30,18 @@ import org.apache.mahout.math.DenseVecto
import org.apache.mahout.math.Matrix;
import org.apache.mahout.math.Vector;
import org.apache.mahout.common.RandomUtils;
+import org.junit.Before;
+import org.junit.Test;
-public class TestLDAInference extends MahoutTestCase {
+public final class TestLDAInference extends MahoutTestCase {
private static final int NUM_TOPICS = 20;
private Random random;
@Override
- protected void setUp() throws Exception {
+ @Before
+ public void setUp() throws Exception {
super.setUp();
random = RandomUtils.getRandom();
}
@@ -53,7 +56,7 @@ public class TestLDAInference extends Ma
*/
private Vector generateRandomDoc(int numWords, double sparsity) throws MathException {
Vector v = new DenseVector(numWords);
- PoissonDistribution dist = new PoissonDistributionImpl(sparsity);
+ IntegerDistribution dist = new PoissonDistributionImpl(sparsity);
for (int i = 0; i < numWords; i++) {
// random integer
v.setQuick(i, dist.inverseCumulativeProbability(random.nextDouble()) + 1);
@@ -101,16 +104,19 @@ public class TestLDAInference extends Ma
assertTrue("log likelihood", doc.getLogLikelihood() <= 1.0E-10);
}
}
-
- public void testLDAEasy() throws MathException {
+
+ @Test
+ public void testLDAEasy() throws Exception {
runTest(10, 1.0, 5); // 1 word per doc in expectation
}
-
- public void testLDASparse() throws MathException {
+
+ @Test
+ public void testLDASparse() throws Exception {
runTest(100, 0.4, 5); // 40 words per doc in expectation
}
-
- public void testLDADense() throws MathException {
+
+ @Test
+ public void testLDADense() throws Exception {
runTest(100, 3.0, 5); // 300 words per doc in expectation
}
}
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/lda/TestMapReduce.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/lda/TestMapReduce.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/lda/TestMapReduce.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/lda/TestMapReduce.java Fri Sep 3 12:28:34 2010
@@ -16,13 +16,14 @@
*/
package org.apache.mahout.clustering.lda;
+import org.apache.commons.math.distribution.IntegerDistribution;
import org.easymock.classextension.EasyMock;
import java.util.Iterator;
import java.util.Random;
import org.apache.commons.math.MathException;
-import org.apache.commons.math.distribution.PoissonDistribution;
+
import org.apache.commons.math.distribution.PoissonDistributionImpl;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
@@ -34,8 +35,10 @@ import org.apache.mahout.math.Matrix;
import org.apache.mahout.math.RandomAccessSparseVector;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;
+import org.junit.Before;
+import org.junit.Test;
-public class TestMapReduce extends MahoutTestCase {
+public final class TestMapReduce extends MahoutTestCase {
private static final int NUM_TESTS = 10;
private static final int NUM_TOPICS = 10;
@@ -49,7 +52,7 @@ public class TestMapReduce extends Mahou
*/
private RandomAccessSparseVector generateRandomDoc(int numWords, double sparsity) throws MathException {
RandomAccessSparseVector v = new RandomAccessSparseVector(numWords,(int)(numWords * sparsity));
- PoissonDistribution dist = new PoissonDistributionImpl(sparsity);
+ IntegerDistribution dist = new PoissonDistributionImpl(sparsity);
for (int i = 0; i < numWords; i++) {
// random integer
v.set(i,dist.inverseCumulativeProbability(random.nextDouble()) + 1);
@@ -78,16 +81,16 @@ public class TestMapReduce extends Mahou
}
@Override
- protected void setUp() throws Exception {
+ @Before
+ public void setUp() throws Exception {
super.setUp();
random = RandomUtils.getRandom();
}
/**
* Test the basic Mapper
- *
- * @throws Exception
*/
+ @Test
public void testMapper() throws Exception {
LDAState state = generateRandomState(100,NUM_TOPICS);
LDAMapper mapper = new LDAMapper();
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/meanshift/TestMeanShift.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/meanshift/TestMeanShift.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/meanshift/TestMeanShift.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/meanshift/TestMeanShift.java Fri Sep 3 12:28:34 2010
@@ -18,6 +18,8 @@
package org.apache.mahout.clustering.meanshift;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -27,6 +29,7 @@ import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
@@ -39,8 +42,10 @@ import org.apache.mahout.common.distance
import org.apache.mahout.math.DenseVector;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;
+import org.junit.Before;
+import org.junit.Test;
-public class TestMeanShift extends MahoutTestCase {
+public final class TestMeanShift extends MahoutTestCase {
private Vector[] raw = null;
@@ -54,14 +59,14 @@ public class TestMeanShift extends Mahou
* @param canopies
* a List<Canopy>
*/
- private static void printCanopies(List<MeanShiftCanopy> canopies) {
+ private static void printCanopies(Iterable<MeanShiftCanopy> canopies) {
for (MeanShiftCanopy canopy : canopies) {
System.out.println(canopy.asFormatString(null));
}
}
/** Print a graphical representation of the clustered image points as a 10x10 character mask */
- private void printImage(List<MeanShiftCanopy> canopies) {
+ private void printImage(Iterable<MeanShiftCanopy> canopies) {
char[][] out = new char[10][10];
for (int i = 0; i < out.length; i++) {
for (int j = 0; j < out[0].length; j++) {
@@ -90,7 +95,8 @@ public class TestMeanShift extends Mahou
}
@Override
- protected void setUp() throws Exception {
+ @Before
+ public void setUp() throws Exception {
super.setUp();
raw = new Vector[100];
for (int i = 0; i < 10; i++) {
@@ -113,6 +119,7 @@ public class TestMeanShift extends Mahou
* Story: User can exercise the reference implementation to verify that the test datapoints are clustered in
* a reasonable manner.
*/
+ @Test
public void testReferenceImplementation() {
MeanShiftCanopyClusterer clusterer = new MeanShiftCanopyClusterer(new EuclideanDistanceMeasure(), 4.0, 1.0, 0.5);
List<MeanShiftCanopy> canopies = new ArrayList<MeanShiftCanopy>();
@@ -141,11 +148,11 @@ public class TestMeanShift extends Mahou
/**
* Test the MeanShiftCanopyClusterer's reference implementation. Should produce the same final output as above.
*/
+ @Test
public void testClustererReferenceImplementation() {
- List<Vector> points = new ArrayList<Vector>();
- for (Vector v : raw)
- points.add(v);
- List<MeanShiftCanopy> canopies = MeanShiftCanopyClusterer.clusterPoints(points, euclideanDistanceMeasure, 0.5, 4, 1, 10);
+ Iterable<Vector> points = new ArrayList<Vector>(Arrays.asList(raw));
+ List<MeanShiftCanopy> canopies =
+ MeanShiftCanopyClusterer.clusterPoints(points, euclideanDistanceMeasure, 0.5, 4, 1, 10);
printCanopies(canopies);
printImage(canopies);
}
@@ -154,12 +161,13 @@ public class TestMeanShift extends Mahou
* Story: User can produce initial canopy centers using a EuclideanDistanceMeasure and a
* CanopyMapper/Combiner which clusters input points to produce an output set of canopies.
*/
+ @Test
public void testCanopyMapperEuclidean() throws Exception {
MeanShiftCanopyClusterer clusterer = new MeanShiftCanopyClusterer(euclideanDistanceMeasure, 4, 1, 0.5);
// get the initial canopies
List<MeanShiftCanopy> canopies = getInitialCanopies();
// build the reference set
- List<MeanShiftCanopy> refCanopies = new ArrayList<MeanShiftCanopy>();
+ Collection<MeanShiftCanopy> refCanopies = new ArrayList<MeanShiftCanopy>();
int nextCanopyId = 0;
for (Vector aRaw : raw) {
clusterer.mergeCanopy(new MeanShiftCanopy(aRaw, nextCanopyId++, euclideanDistanceMeasure), refCanopies);
@@ -174,9 +182,8 @@ public class TestMeanShift extends Mahou
// map the data
MeanShiftCanopyMapper mapper = new MeanShiftCanopyMapper();
DummyRecordWriter<Text, MeanShiftCanopy> mapWriter = new DummyRecordWriter<Text, MeanShiftCanopy>();
- Mapper<WritableComparable<?>, MeanShiftCanopy, Text, MeanShiftCanopy>.Context mapContext = DummyRecordWriter.build(mapper,
- conf,
- mapWriter);
+ Mapper<WritableComparable<?>, MeanShiftCanopy, Text, MeanShiftCanopy>.Context mapContext =
+ DummyRecordWriter.build(mapper, conf, mapWriter);
mapper.setup(mapContext);
for (MeanShiftCanopy canopy : canopies) {
mapper.map(new Text(), canopy, mapContext);
@@ -205,7 +212,8 @@ public class TestMeanShift extends Mahou
MeanShiftCanopy canopy = canopyMap.get((ref.isConverged() ? "MSV-" : "MSC-") + ref.getId());
assertEquals("ids", ref.getId(), canopy.getId());
- assertEquals("centers(" + ref.getIdentifier() + ')', ref.getCenter().asFormatString(), canopy.getCenter().asFormatString());
+ assertEquals("centers(" + ref.getIdentifier() + ')',
+ ref.getCenter().asFormatString(), canopy.getCenter().asFormatString());
assertEquals("bound points", ref.getBoundPoints().toList().size(), canopy.getBoundPoints().toList().size());
}
}
@@ -214,12 +222,13 @@ public class TestMeanShift extends Mahou
* Story: User can produce final canopy centers using a EuclideanDistanceMeasure and a CanopyReducer which
* clusters input centroid points to produce an output set of final canopy centroid points.
*/
+ @Test
public void testCanopyReducerEuclidean() throws Exception {
MeanShiftCanopyClusterer clusterer = new MeanShiftCanopyClusterer(euclideanDistanceMeasure, 4, 1, 0.5);
// get the initial canopies
List<MeanShiftCanopy> canopies = getInitialCanopies();
// build the mapper output reference set
- List<MeanShiftCanopy> mapperReference = new ArrayList<MeanShiftCanopy>();
+ Collection<MeanShiftCanopy> mapperReference = new ArrayList<MeanShiftCanopy>();
int nextCanopyId = 0;
for (Vector aRaw : raw) {
clusterer.mergeCanopy(new MeanShiftCanopy(aRaw, nextCanopyId++, euclideanDistanceMeasure), mapperReference);
@@ -228,7 +237,7 @@ public class TestMeanShift extends Mahou
clusterer.shiftToMean(canopy);
}
// build the reducer reference output set
- List<MeanShiftCanopy> reducerReference = new ArrayList<MeanShiftCanopy>();
+ Collection<MeanShiftCanopy> reducerReference = new ArrayList<MeanShiftCanopy>();
for (MeanShiftCanopy canopy : mapperReference) {
clusterer.mergeCanopy(canopy, reducerReference);
}
@@ -245,9 +254,8 @@ public class TestMeanShift extends Mahou
MeanShiftCanopyMapper mapper = new MeanShiftCanopyMapper();
DummyRecordWriter<Text, MeanShiftCanopy> mapWriter = new DummyRecordWriter<Text, MeanShiftCanopy>();
- Mapper<WritableComparable<?>, MeanShiftCanopy, Text, MeanShiftCanopy>.Context mapContext = DummyRecordWriter.build(mapper,
- conf,
- mapWriter);
+ Mapper<WritableComparable<?>, MeanShiftCanopy, Text, MeanShiftCanopy>.Context mapContext =
+ DummyRecordWriter.build(mapper, conf, mapWriter);
mapper.setup(mapContext);
// map the data
@@ -260,11 +268,8 @@ public class TestMeanShift extends Mahou
// now reduce the mapper output
MeanShiftCanopyReducer reducer = new MeanShiftCanopyReducer();
DummyRecordWriter<Text, MeanShiftCanopy> reduceWriter = new DummyRecordWriter<Text, MeanShiftCanopy>();
- Reducer<Text, MeanShiftCanopy, Text, MeanShiftCanopy>.Context reduceContext = DummyRecordWriter.build(reducer,
- conf,
- reduceWriter,
- Text.class,
- MeanShiftCanopy.class);
+ Reducer<Text, MeanShiftCanopy, Text, MeanShiftCanopy>.Context reduceContext =
+ DummyRecordWriter.build(reducer, conf, reduceWriter, Text.class, MeanShiftCanopy.class);
reducer.setup(reduceContext);
reducer.reduce(new Text("canopy"), mapWriter.getValue(new Text("canopy")), reduceContext);
reducer.cleanup(reduceContext);
@@ -281,7 +286,8 @@ public class TestMeanShift extends Mahou
for (Map.Entry<String, MeanShiftCanopy> mapEntry : reducerReferenceMap.entrySet()) {
MeanShiftCanopy refCanopy = mapEntry.getValue();
- List<MeanShiftCanopy> values = reduceWriter.getValue(new Text((refCanopy.isConverged() ? "MSV-" : "MSC-") + refCanopy.getId()));
+ List<MeanShiftCanopy> values =
+ reduceWriter.getValue(new Text((refCanopy.isConverged() ? "MSV-" : "MSC-") + refCanopy.getId()));
assertEquals("values", 1, values.size());
MeanShiftCanopy reducerCanopy = values.get(0);
assertEquals("ids", refCanopy.getId(), reducerCanopy.getId());
@@ -291,7 +297,9 @@ public class TestMeanShift extends Mahou
String refCenter = refCanopy.getCenter().asFormatString();
String reducerCenter = reducerCanopy.getCenter().asFormatString();
assertEquals("centers(" + mapEntry.getKey() + ')', refCenter, reducerCenter);
- assertEquals("bound points", refCanopy.getBoundPoints().toList().size(), reducerCanopy.getBoundPoints().toList().size());
+ assertEquals("bound points",
+ refCanopy.getBoundPoints().toList().size(),
+ reducerCanopy.getBoundPoints().toList().size());
}
}
@@ -299,11 +307,12 @@ public class TestMeanShift extends Mahou
* Story: User can produce final point clustering using a Hadoop map/reduce job and a
* EuclideanDistanceMeasure.
*/
+ @Test
public void testCanopyEuclideanMRJob() throws Exception {
Path input = getTestTempDirPath("testdata");
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(input.toUri(), conf);
- List<VectorWritable> points = new ArrayList<VectorWritable>();
+ Collection<VectorWritable> points = new ArrayList<VectorWritable>();
for (Vector v : raw) {
points.add(new VectorWritable(v));
}
@@ -321,8 +330,8 @@ public class TestMeanShift extends Mahou
new MeanShiftCanopyDriver().run(args);
Path outPart = new Path(output, "clusters-3/part-r-00000");
SequenceFile.Reader reader = new SequenceFile.Reader(fs, outPart, conf);
- Text key = new Text();
- MeanShiftCanopy value = new MeanShiftCanopy();
+ Writable key = new Text();
+ Writable value = new MeanShiftCanopy();
int count = 0;
while (reader.next(key, value)) {
count++;
@@ -335,11 +344,12 @@ public class TestMeanShift extends Mahou
* Story: User can produce final point clustering using a Hadoop map/reduce job and a
* EuclideanDistanceMeasure.
*/
+ @Test
public void testCanopyEuclideanSeqJob() throws Exception {
Path input = getTestTempDirPath("testdata");
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(input.toUri(), conf);
- List<VectorWritable> points = new ArrayList<VectorWritable>();
+ Collection<VectorWritable> points = new ArrayList<VectorWritable>();
for (Vector v : raw) {
points.add(new VectorWritable(v));
}
@@ -359,7 +369,7 @@ public class TestMeanShift extends Mahou
new MeanShiftCanopyDriver().run(args);
Path outPart = new Path(output, "clusters-4/part-r-00000");
SequenceFile.Reader reader = new SequenceFile.Reader(fs, outPart, conf);
- Text key = new Text();
+ Writable key = new Text();
MeanShiftCanopy value = new MeanShiftCanopy();
int count = 0;
while (reader.next(key, value)) {
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/common/AbstractJobTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/common/AbstractJobTest.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/common/AbstractJobTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/common/AbstractJobTest.java Fri Sep 3 12:28:34 2010
@@ -22,13 +22,12 @@ package org.apache.mahout.common;
import java.util.HashMap;
import java.util.Map;
-import junit.framework.Assert;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.util.ToolRunner;
import org.apache.mahout.common.commandline.DefaultOptionCreator;
import org.junit.Test;
-public class AbstractJobTest {
+public final class AbstractJobTest extends MahoutTestCase {
interface AbstractJobFactory {
AbstractJob getJob();
@@ -58,13 +57,11 @@ public class AbstractJobTest {
// testFlag will only be present if speciied on the command-line
ToolRunner.run(fact.getJob(), new String[0]);
- Assert.assertFalse("test map for absent flag",
- testMap.containsKey("--testFlag"));
+ assertFalse("test map for absent flag", testMap.containsKey("--testFlag"));
String[] withFlag = { "--testFlag" };
ToolRunner.run(fact.getJob(), withFlag);
- Assert.assertTrue("test map for present flag",
- testMap.containsKey("--testFlag"));
+ assertTrue("test map for present flag", testMap.containsKey("--testFlag"));
}
@Test
@@ -99,29 +96,29 @@ public class AbstractJobTest {
};
int ret = ToolRunner.run(fact.getJob(), new String[0]);
- Assert.assertEquals("-1 for missing required options", -1, ret);
+ assertEquals("-1 for missing required options", -1, ret);
ret = ToolRunner.run(fact.getJob(), new String[]{
"--required", "requiredArg"
});
- Assert.assertEquals("0 for no missing required options", 0, ret);
- Assert.assertEquals("requiredArg", testMap.get("--required"));
- Assert.assertEquals("defaultValue", testMap.get("--hasDefault"));
- Assert.assertNull(testMap.get("--option"));
- Assert.assertNull(testMap.get("--notRequired"));
- Assert.assertFalse(testMap.containsKey("--overwrite"));
+ assertEquals("0 for no missing required options", 0, ret);
+ assertEquals("requiredArg", testMap.get("--required"));
+ assertEquals("defaultValue", testMap.get("--hasDefault"));
+ assertNull(testMap.get("--option"));
+ assertNull(testMap.get("--notRequired"));
+ assertFalse(testMap.containsKey("--overwrite"));
ret = ToolRunner.run(fact.getJob(), new String[]{
"--required", "requiredArg",
"--unknownArg"
});
- Assert.assertEquals("-1 for including unknown options", -1, ret);
+ assertEquals("-1 for including unknown options", -1, ret);
ret = ToolRunner.run(fact.getJob(), new String[]{
"--required", "requiredArg",
"--required", "requiredArg2",
});
- Assert.assertEquals("-1 for including duplicate options", -1, ret);
+ assertEquals("-1 for including duplicate options", -1, ret);
ret = ToolRunner.run(fact.getJob(), new String[]{
"--required", "requiredArg",
@@ -130,12 +127,12 @@ public class AbstractJobTest {
"--option", "optionValue",
"--notRequired", "notRequired"
});
- Assert.assertEquals("0 for no missing required options", 0, ret);
- Assert.assertEquals("requiredArg", testMap.get("--required"));
- Assert.assertEquals("nonDefault", testMap.get("--hasDefault"));
- Assert.assertEquals("optionValue", testMap.get("--option"));
- Assert.assertEquals("notRequired", testMap.get("--notRequired"));
- Assert.assertTrue(testMap.containsKey("--overwrite"));
+ assertEquals("0 for no missing required options", 0, ret);
+ assertEquals("requiredArg", testMap.get("--required"));
+ assertEquals("nonDefault", testMap.get("--hasDefault"));
+ assertEquals("optionValue", testMap.get("--option"));
+ assertEquals("notRequired", testMap.get("--notRequired"));
+ assertTrue(testMap.containsKey("--overwrite"));
ret = ToolRunner.run(fact.getJob(), new String[]{
"-r", "requiredArg",
@@ -144,12 +141,12 @@ public class AbstractJobTest {
"-o", "optionValue",
"-nr", "notRequired"
});
- Assert.assertEquals("0 for no missing required options", 0, ret);
- Assert.assertEquals("requiredArg", testMap.get("--required"));
- Assert.assertEquals("nonDefault", testMap.get("--hasDefault"));
- Assert.assertEquals("optionValue", testMap.get("--option"));
- Assert.assertEquals("notRequired", testMap.get("--notRequired"));
- Assert.assertTrue(testMap.containsKey("--overwrite"));
+ assertEquals("0 for no missing required options", 0, ret);
+ assertEquals("requiredArg", testMap.get("--required"));
+ assertEquals("nonDefault", testMap.get("--hasDefault"));
+ assertEquals("optionValue", testMap.get("--option"));
+ assertEquals("notRequired", testMap.get("--notRequired"));
+ assertTrue(testMap.containsKey("--overwrite"));
}
@@ -173,10 +170,10 @@ public class AbstractJobTest {
}
Path inputPath = getInputPath();
- Assert.assertNotNull("getInputPath() returns non-null", inputPath);
+ assertNotNull("getInputPath() returns non-null", inputPath);
Path outputPath = getInputPath();
- Assert.assertNotNull("getOutputPath() returns non-null", outputPath);
+ assertNotNull("getOutputPath() returns non-null", outputPath);
return 0;
}
};
@@ -184,7 +181,7 @@ public class AbstractJobTest {
};
int ret = ToolRunner.run(fact.getJob(), new String[0]);
- Assert.assertEquals("-1 for missing input option", -1, ret);
+ assertEquals("-1 for missing input option", -1, ret);
String testInputPath = "testInputPath";
String testOutputPath = "testOutputPath";
@@ -194,44 +191,36 @@ public class AbstractJobTest {
AbstractJob job = fact.getJob();
ret = ToolRunner.run(job, new String[]{
"--input", testInputPath });
- Assert.assertEquals("-1 for missing output option", -1, ret);
- Assert.assertEquals("input path is correct", testInputPath,
- job.getInputPath().toString());
+ assertEquals("-1 for missing output option", -1, ret);
+ assertEquals("input path is correct", testInputPath, job.getInputPath().toString());
job = fact.getJob();
ret = ToolRunner.run(job, new String[]{
"--output", testOutputPath });
- Assert.assertEquals("-1 for missing input option", -1, ret);
- Assert.assertEquals("output path is correct", testOutputPath,
- job.getOutputPath().toString());
+ assertEquals("-1 for missing input option", -1, ret);
+ assertEquals("output path is correct", testOutputPath, job.getOutputPath().toString());
job = fact.getJob();
ret = ToolRunner.run(job, new String[]{
"--input", testInputPath, "--output", testOutputPath });
- Assert.assertEquals("0 for complete options", 0, ret);
- Assert.assertEquals("input path is correct", testInputPath,
- job.getInputPath().toString());
- Assert.assertEquals("output path is correct", testOutputPath,
- job.getOutputPath().toString());
+ assertEquals("0 for complete options", 0, ret);
+ assertEquals("input path is correct", testInputPath, job.getInputPath().toString());
+ assertEquals("output path is correct", testOutputPath, job.getOutputPath().toString());
job = fact.getJob();
ret = ToolRunner.run(job, new String[]{
"--input", testInputPath, "--output", testOutputPath });
- Assert.assertEquals("0 for complete options", 0, ret);
- Assert.assertEquals("input path is correct", testInputPath,
- job.getInputPath().toString());
- Assert.assertEquals("output path is correct", testOutputPath,
- job.getOutputPath().toString());
+ assertEquals("0 for complete options", 0, ret);
+ assertEquals("input path is correct", testInputPath, job.getInputPath().toString());
+ assertEquals("output path is correct", testOutputPath, job.getOutputPath().toString());
job = fact.getJob();
ret = ToolRunner.run(job, new String[]{
"-Dmapred.input.dir=" + testInputPropertyPath,
"-Dmapred.output.dir=" + testOutputPropertyPath });
- Assert.assertEquals("0 for complete options", 0, ret);
- Assert.assertEquals("input path from property is correct",
- testInputPropertyPath, job.getInputPath().toString());
- Assert.assertEquals("output path from property is correct",
- testOutputPropertyPath, job.getOutputPath().toString());
+ assertEquals("0 for complete options", 0, ret);
+ assertEquals("input path from property is correct", testInputPropertyPath, job.getInputPath().toString());
+ assertEquals("output path from property is correct", testOutputPropertyPath, job.getOutputPath().toString());
job = fact.getJob();
ret = ToolRunner.run(job, new String[]{
@@ -239,9 +228,9 @@ public class AbstractJobTest {
"-Dmapred.output.dir=" + testOutputPropertyPath,
"--input", testInputPath,
"--output", testOutputPath });
- Assert.assertEquals("input command-line option precedes property",
+ assertEquals("input command-line option precedes property",
testInputPath, job.getInputPath().toString());
- Assert.assertEquals("output command-line option precedes property",
+ assertEquals("output command-line option precedes property",
testOutputPath, job.getOutputPath().toString());
}
}
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyCounter.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyCounter.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyCounter.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyCounter.java Fri Sep 3 12:28:34 2010
@@ -1,11 +1,26 @@
+/**
+ * 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.common;
import org.apache.hadoop.mapreduce.Counter;
-class DummyCounter extends Counter {
-
- public DummyCounter() {
- super();
- }
+final class DummyCounter extends Counter {
}
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyOutputCollector.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyOutputCollector.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyOutputCollector.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyOutputCollector.java Fri Sep 3 12:28:34 2010
@@ -21,21 +21,19 @@ import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapred.OutputCollector;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
-public class DummyOutputCollector<K extends WritableComparable, V extends Writable>
+public final class DummyOutputCollector<K extends WritableComparable, V extends Writable>
implements OutputCollector<K,V> {
private final Map<K, List<V>> data = new TreeMap<K,List<V>>();
@Override
- public void collect(K key,V values)
- throws IOException {
+ public void collect(K key,V values) {
List<V> points = data.get(key);
if (points == null) {
points = new ArrayList<V>();
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyRecordWriter.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyRecordWriter.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyRecordWriter.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyRecordWriter.java Fri Sep 3 12:28:34 2010
@@ -63,8 +63,8 @@ public final class DummyRecordWriter<K,
public static <K1, V1, K2, V2> Mapper<K1, V1, K2, V2>.Context build(Mapper<K1, V1, K2, V2> mapper,
Configuration configuration,
- RecordWriter<K2, V2> output) throws IOException,
- InterruptedException {
+ RecordWriter<K2, V2> output)
+ throws IOException, InterruptedException {
return mapper.new Context(configuration, new TaskAttemptID(), null, output, null, new DummyStatusReporter(), null);
}
@@ -72,8 +72,8 @@ public final class DummyRecordWriter<K,
Configuration configuration,
RecordWriter<K2, V2> output,
Class<K1> keyClass,
- Class<V1> valueClass) throws IOException,
- InterruptedException {
+ Class<V1> valueClass)
+ throws IOException, InterruptedException {
return reducer.new Context(configuration,
new TaskAttemptID(),
new MockIterator(),
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyReporter.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyReporter.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyReporter.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyReporter.java Fri Sep 3 12:28:34 2010
@@ -26,7 +26,7 @@ import org.apache.hadoop.mapred.InputSpl
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.Counters.Counter;
-public class DummyReporter implements Reporter {
+public final class DummyReporter implements Reporter {
//private String status = "";
private final Map<Enum<?>,MutableLong> count1 = new HashMap<Enum<?>,MutableLong>();
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyStatusReporter.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyStatusReporter.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyStatusReporter.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/common/DummyStatusReporter.java Fri Sep 3 12:28:34 2010
@@ -1,3 +1,22 @@
+/**
+ * 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.common;
import java.util.HashMap;
@@ -6,34 +25,34 @@ import java.util.Map;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.StatusReporter;
-public class DummyStatusReporter extends StatusReporter {
+public final class DummyStatusReporter extends StatusReporter {
- Map<Enum<?>, Counter> counters = new HashMap<Enum<?>, Counter>();
+ private final Map<Enum<?>, Counter> counters = new HashMap<Enum<?>, Counter>();
+ private final Map<String, Counter> counterGroups = new HashMap<String, Counter>();
@Override
public Counter getCounter(Enum<?> name) {
- if (!counters.containsKey(name))
+ if (!counters.containsKey(name)) {
counters.put(name, new DummyCounter());
+ }
return counters.get(name);
}
- Map<String, Counter> counterGroups = new HashMap<String, Counter>();
@Override
public Counter getCounter(String group, String name) {
- if (!counterGroups.containsKey(group + name))
+ if (!counterGroups.containsKey(group + name)) {
counterGroups.put(group + name, new DummyCounter());
+ }
return counterGroups.get(group+name);
}
@Override
public void progress() {
- // TODO Auto-generated method stub
}
@Override
public void setStatus(String status) {
- // TODO Auto-generated method stub
}
}
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/common/IntPairWritableTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/common/IntPairWritableTest.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/common/IntPairWritableTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/common/IntPairWritableTest.java Fri Sep 3 12:28:34 2010
@@ -19,66 +19,65 @@ package org.apache.mahout.common;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.DataInput;
import java.io.DataInputStream;
+import java.io.DataOutput;
import java.io.DataOutputStream;
-import java.io.IOException;
import java.util.Arrays;
-import junit.framework.Assert;
-
import org.junit.Test;
-public class IntPairWritableTest {
+public final class IntPairWritableTest extends MahoutTestCase {
@Test
public void testGetSet() {
IntPairWritable n = new IntPairWritable();
- Assert.assertEquals(0, n.getFirst());
- Assert.assertEquals(0, n.getSecond());
+ assertEquals(0, n.getFirst());
+ assertEquals(0, n.getSecond());
n.setFirst(5);
n.setSecond(10);
- Assert.assertEquals(5, n.getFirst());
- Assert.assertEquals(10, n.getSecond());
+ assertEquals(5, n.getFirst());
+ assertEquals(10, n.getSecond());
n = new IntPairWritable(2,4);
- Assert.assertEquals(2, n.getFirst());
- Assert.assertEquals(4, n.getSecond());
+ assertEquals(2, n.getFirst());
+ assertEquals(4, n.getSecond());
}
@Test
- public void testWritable() throws IOException {
+ public void testWritable() throws Exception {
IntPairWritable one = new IntPairWritable(1,2);
IntPairWritable two = new IntPairWritable(3,4);
- Assert.assertEquals(1, one.getFirst());
- Assert.assertEquals(2, one.getSecond());
+ assertEquals(1, one.getFirst());
+ assertEquals(2, one.getSecond());
- Assert.assertEquals(3, two.getFirst());
- Assert.assertEquals(4, two.getSecond());
+ assertEquals(3, two.getFirst());
+ assertEquals(4, two.getSecond());
ByteArrayOutputStream bout = new ByteArrayOutputStream();
- DataOutputStream out = new DataOutputStream(bout);
+ DataOutput out = new DataOutputStream(bout);
two.write(out);
byte[] b = bout.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(b);
- DataInputStream din = new DataInputStream(bin);
+ DataInput din = new DataInputStream(bin);
one.readFields(din);
- Assert.assertEquals(two.getFirst(), one.getFirst());
- Assert.assertEquals(two.getSecond(), one.getSecond());
+ assertEquals(two.getFirst(), one.getFirst());
+ assertEquals(two.getSecond(), one.getSecond());
}
@Test
- public void testComparable() throws IOException {
+ public void testComparable() {
IntPairWritable[] input = {
new IntPairWritable(2,3),
new IntPairWritable(2,2),
@@ -108,7 +107,7 @@ public class IntPairWritableTest {
};
for (int i=0; i < input.length; i++) {
- Assert.assertSame(input[expected[i]], sorted[i]);
+ assertSame(input[expected[i]], sorted[i]);
}
}
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/common/MahoutTestCase.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/common/MahoutTestCase.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/common/MahoutTestCase.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/common/MahoutTestCase.java Fri Sep 3 12:28:34 2010
@@ -23,15 +23,20 @@ import java.lang.reflect.Field;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
+import org.junit.After;
+import org.junit.Before;
public abstract class MahoutTestCase extends org.apache.mahout.math.MahoutTestCase {
- private Path testTempDirPath;
+ /** "Close enough" value for floating-point comparisons. */
+ public static final double EPSILON = 0.0000001;
+ private Path testTempDirPath;
private FileSystem fs;
@Override
- protected void setUp() throws Exception {
+ @Before
+ public void setUp() throws Exception {
super.setUp();
RandomUtils.useTestSeed();
testTempDirPath = null;
@@ -39,7 +44,8 @@ public abstract class MahoutTestCase ext
}
@Override
- protected void tearDown() throws Exception {
+ @After
+ public void tearDown() throws Exception {
if (testTempDirPath != null) {
try {
fs.delete(testTempDirPath, true);
@@ -56,7 +62,8 @@ public abstract class MahoutTestCase ext
if (testTempDirPath == null) {
fs = FileSystem.get(new Configuration());
long simpleRandomLong = (long) (Long.MAX_VALUE * Math.random());
- testTempDirPath = fs.makeQualified(new Path("/tmp/mahout-" + getClass().getSimpleName() + '-' + simpleRandomLong));
+ testTempDirPath = fs.makeQualified(
+ new Path("/tmp/mahout-" + getClass().getSimpleName() + '-' + simpleRandomLong));
if (!fs.mkdirs(testTempDirPath)) {
throw new IOException("Could not create " + testTempDirPath);
}
@@ -84,22 +91,17 @@ public abstract class MahoutTestCase ext
}
/**
- * try to directly set a (possibly private) field on an Object
- *
- * @param target
- * @param fieldname
- * @param value
- * @throws NoSuchFieldException
- * @throws IllegalAccessException
+ * Try to directly set a (possibly private) field on an Object
*/
- protected void setField(Object target, String fieldname, Object value) throws NoSuchFieldException, IllegalAccessException {
+ protected static void setField(Object target, String fieldname, Object value)
+ throws NoSuchFieldException, IllegalAccessException {
Field field = findDeclaredField(target.getClass(), fieldname);
field.setAccessible(true);
field.set(target, value);
}
/**
- * find a declared field in a class or one of it's super classes
+ * Find a declared field in a class or one of it's super classes
*/
private static Field findDeclaredField(Class<?> inClass, String fieldname) throws NoSuchFieldException {
if (Object.class.equals(inClass)) {
@@ -116,7 +118,7 @@ public abstract class MahoutTestCase ext
/**
* @return a job option key string (--name) from the given option name
*/
- protected String optKey(String optionName) {
+ protected static String optKey(String optionName) {
return AbstractJob.keyFor(optionName);
}
}
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/common/MockIterator.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/common/MockIterator.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/common/MockIterator.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/common/MockIterator.java Fri Sep 3 12:28:34 2010
@@ -1,19 +1,34 @@
-package org.apache.mahout.common;
+/**
+ * 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.
+ */
-import java.io.IOException;
+package org.apache.mahout.common;
import org.apache.hadoop.io.DataInputBuffer;
import org.apache.hadoop.mapred.RawKeyValueIterator;
import org.apache.hadoop.util.Progress;
-public class MockIterator implements RawKeyValueIterator {
+public final class MockIterator implements RawKeyValueIterator {
@Override
- public void close() throws IOException {
+ public void close() {
}
@Override
- public DataInputBuffer getKey() throws IOException {
+ public DataInputBuffer getKey() {
return null;
}
@@ -23,13 +38,13 @@ public class MockIterator implements Raw
}
@Override
- public DataInputBuffer getValue() throws IOException {
+ public DataInputBuffer getValue() {
return null;
}
@Override
- public boolean next() throws IOException {
+ public boolean next() {
return true;
}
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/common/StringUtilsTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/common/StringUtilsTest.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/common/StringUtilsTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/common/StringUtilsTest.java Fri Sep 3 12:28:34 2010
@@ -17,10 +17,12 @@
package org.apache.mahout.common;
+import org.junit.Test;
+
import java.util.Arrays;
import java.util.List;
-public class StringUtilsTest extends MahoutTestCase {
+public final class StringUtilsTest extends MahoutTestCase {
private static class DummyTest {
private int field;
@@ -48,6 +50,7 @@ public class StringUtilsTest extends Mah
}
}
+ @Test
public void testStringConversion() throws Exception {
List<String> expected = Arrays.asList("A", "B", "C");
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/CosineDistanceMeasureTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/CosineDistanceMeasureTest.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/CosineDistanceMeasureTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/CosineDistanceMeasureTest.java Fri Sep 3 12:28:34 2010
@@ -20,9 +20,11 @@ package org.apache.mahout.common.distanc
import org.apache.mahout.common.MahoutTestCase;
import org.apache.mahout.math.DenseVector;
import org.apache.mahout.math.Vector;
+import org.junit.Test;
-public class CosineDistanceMeasureTest extends MahoutTestCase {
+public final class CosineDistanceMeasureTest extends MahoutTestCase {
+ @Test
public void testMeasure() {
DistanceMeasure distanceMeasure = new CosineDistanceMeasure();
@@ -41,15 +43,15 @@ public class CosineDistanceMeasureTest e
}
}
- assertEquals(0.0, distanceMatrix[0][0]);
+ assertEquals(0.0, distanceMatrix[0][0], EPSILON);
assertTrue(distanceMatrix[0][0] < distanceMatrix[0][1]);
assertTrue(distanceMatrix[0][1] < distanceMatrix[0][2]);
- assertEquals(0.0, distanceMatrix[1][1]);
+ assertEquals(0.0, distanceMatrix[1][1], EPSILON);
assertTrue(distanceMatrix[1][0] > distanceMatrix[1][1]);
assertTrue(distanceMatrix[1][2] < distanceMatrix[1][0]);
- assertEquals(0.0, distanceMatrix[2][2]);
+ assertEquals(0.0, distanceMatrix[2][2], EPSILON);
assertTrue(distanceMatrix[2][0] > distanceMatrix[2][1]);
assertTrue(distanceMatrix[2][1] > distanceMatrix[2][2]);
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/DefaultDistanceMeasureTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/DefaultDistanceMeasureTest.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/DefaultDistanceMeasureTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/DefaultDistanceMeasureTest.java Fri Sep 3 12:28:34 2010
@@ -21,11 +21,13 @@ import org.apache.mahout.common.MahoutTe
import org.apache.mahout.math.DenseVector;
import org.apache.mahout.math.RandomAccessSparseVector;
import org.apache.mahout.math.Vector;
+import org.junit.Test;
public abstract class DefaultDistanceMeasureTest extends MahoutTestCase {
protected abstract DistanceMeasure distanceMeasureFactory();
+ @Test
public void testMeasure() {
DistanceMeasure distanceMeasure = distanceMeasureFactory();
@@ -70,15 +72,15 @@ public abstract class DefaultDistanceMea
}
}
- assertEquals("Distance from first vector to itself is not zero", 0.0, distanceMatrix[0][0]);
+ assertEquals("Distance from first vector to itself is not zero", 0.0, distanceMatrix[0][0], EPSILON);
assertTrue(distanceMatrix[0][0] < distanceMatrix[0][1]);
assertTrue(distanceMatrix[0][1] < distanceMatrix[0][2]);
- assertEquals("Distance from second vector to itself is not zero", 0.0, distanceMatrix[1][1]);
+ assertEquals("Distance from second vector to itself is not zero", 0.0, distanceMatrix[1][1], EPSILON);
assertTrue(distanceMatrix[1][0] > distanceMatrix[1][1]);
assertTrue(distanceMatrix[1][2] > distanceMatrix[1][0]);
- assertEquals("Distance from third vector to itself is not zero", 0.0, distanceMatrix[2][2]);
+ assertEquals("Distance from third vector to itself is not zero", 0.0, distanceMatrix[2][2], EPSILON);
assertTrue(distanceMatrix[2][0] > distanceMatrix[2][1]);
assertTrue(distanceMatrix[2][1] > distanceMatrix[2][2]);
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/DefaultWeightedDistanceMeasureTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/DefaultWeightedDistanceMeasureTest.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/DefaultWeightedDistanceMeasureTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/DefaultWeightedDistanceMeasureTest.java Fri Sep 3 12:28:34 2010
@@ -19,12 +19,14 @@ package org.apache.mahout.common.distanc
import org.apache.mahout.math.DenseVector;
import org.apache.mahout.math.Vector;
+import org.junit.Test;
public abstract class DefaultWeightedDistanceMeasureTest extends DefaultDistanceMeasureTest {
@Override
public abstract WeightedDistanceMeasure distanceMeasureFactory();
+ @Test
public void testMeasureWeighted() {
WeightedDistanceMeasure distanceMeasure = distanceMeasureFactory();
@@ -44,7 +46,7 @@ public abstract class DefaultWeightedDis
}
}
- assertEquals(0.0, distanceMatrix[0][0]);
+ assertEquals(0.0, distanceMatrix[0][0], EPSILON);
assertTrue(distanceMatrix[0][1] < distanceMatrix[0][2]);
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/TestEuclideanDistanceMeasure.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/TestEuclideanDistanceMeasure.java?rev=992277&r1=992276&r2=992277&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/TestEuclideanDistanceMeasure.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/common/distance/TestEuclideanDistanceMeasure.java Fri Sep 3 12:28:34 2010
@@ -17,7 +17,7 @@
package org.apache.mahout.common.distance;
-public class TestEuclideanDistanceMeasure extends DefaultDistanceMeasureTest {
+public final class TestEuclideanDistanceMeasure extends DefaultDistanceMeasureTest {
@Override
public DistanceMeasure distanceMeasureFactory() {