Author: edwardyoon
Date: Tue Jan 3 11:46:20 2012
New Revision: 1226746
URL: http://svn.apache.org/viewvc?rev=1226746&view=rev
Log:
Add unit test for SSSP algorithm
Modified:
incubator/hama/trunk/CHANGES.txt
incubator/hama/trunk/examples/src/test/java/org/apache/hama/examples/ShortestPathsTest.java
Modified: incubator/hama/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/incubator/hama/trunk/CHANGES.txt?rev=1226746&r1=1226745&r2=1226746&view=diff
==============================================================================
--- incubator/hama/trunk/CHANGES.txt (original)
+++ incubator/hama/trunk/CHANGES.txt Tue Jan 3 11:46:20 2012
@@ -31,6 +31,7 @@ Release 0.4 - Unreleased
IMPROVEMENTS
+ HAMA-475: Add unit test for SSSP example (edwardyoon)
HAMA-481: Add reopen to peer (tjungblut)
HAMA-471: Peer names should be sorted by task id (tjungblut)
HAMA-461: Extract a message service from BSPPeer (tjungblut)
Modified:
incubator/hama/trunk/examples/src/test/java/org/apache/hama/examples/ShortestPathsTest.java
URL:
http://svn.apache.org/viewvc/incubator/hama/trunk/examples/src/test/java/org/apache/hama/examples/ShortestPathsTest.java?rev=1226746&r1=1226745&r2=1226746&view=diff
==============================================================================
---
incubator/hama/trunk/examples/src/test/java/org/apache/hama/examples/ShortestPathsTest.java
(original)
+++
incubator/hama/trunk/examples/src/test/java/org/apache/hama/examples/ShortestPathsTest.java
Tue Jan 3 11:46:20 2012
@@ -17,23 +17,155 @@
*/
package org.apache.hama.examples;
-import org.junit.Test;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
-import static org.junit.Assert.fail;
+import junit.framework.TestCase;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.SequenceFile;
+import org.apache.hadoop.io.Text;
+import org.apache.hama.HamaConfiguration;
/**
* Testcase for {@link ShortestPaths}
*/
-public class ShortestPathsTest {
+public class ShortestPathsTest extends TestCase {
+ private static String INPUT = "/tmp/sssp-tmp.seq";
+ private static String OUTPUT = "/tmp/sssp-out";
+ private Configuration conf;
+ private FileSystem fs;
+
+ public void testShortestPathsWithWrongArgs() throws IOException,
+ InterruptedException, ClassNotFoundException, InstantiationException,
+ IllegalAccessException {
+ conf = new HamaConfiguration();
+ fs = FileSystem.get(conf);
+
+ generateTestData();
+ ShortestPaths.main(new String[] { "Frankfurt", OUTPUT, INPUT });
+
+ verifyResult();
+ fs.delete(new Path(INPUT), true);
+ fs.delete(new Path(OUTPUT), true);
+ }
+
+ private void verifyResult() throws IOException {
+ Map<String, Integer> rs = new HashMap<String, Integer>();
+ rs.put("Erfurt", 385);
+ rs.put("Mannheim", 85);
+ rs.put("Stuttgart", 503);
+ rs.put("Kassel", 173);
+ rs.put("Nuernberg", 320);
+ rs.put("Augsburg", 415);
+ rs.put("Frankfurt", 0);
+ rs.put("Muenchen", 487);
+ rs.put("Wuerzburg", 217);
+ rs.put("Karlsruhe", 165);
+
+ SequenceFile.Reader reader = new SequenceFile.Reader(fs, new Path(OUTPUT
+ + "/part-00000"), conf);
+ Text key = new Text();
+ IntWritable value = new IntWritable();
+ while (reader.next(key, value)) {
+ assertEquals(value.get(), (int) rs.get(key.toString()));
+ }
+ }
+
+ private void generateTestData() throws IOException {
+ Map<ShortestPathVertex, ShortestPathVertexArrayWritable> tmp = new
HashMap<ShortestPathVertex, ShortestPathVertexArrayWritable>();
+ String[] cities = new String[] { "Frankfurt", "Mannheim", "Wuerzburg",
+ "Stuttgart", "Kassel", "Karlsruhe", "Erfurt", "Nuernberg", "Augsburg",
+ "Muenchen" };
+
+ for (String city : cities) {
+ if (city.equals("Frankfurt")) {
+ ShortestPathVertex[] textArr = new ShortestPathVertex[3];
+ textArr[0] = new ShortestPathVertex(85, "Mannheim");
+ textArr[1] = new ShortestPathVertex(173, "Kassel");
+ textArr[2] = new ShortestPathVertex(217, "Wuerzburg");
+ ShortestPathVertexArrayWritable arr = new
ShortestPathVertexArrayWritable();
+ arr.set(textArr);
+ tmp.put(new ShortestPathVertex(0, city), arr);
+ } else if (city.equals("Stuttgart")) {
+ ShortestPathVertex[] textArr = new ShortestPathVertex[1];
+ textArr[0] = new ShortestPathVertex(183, "Nuernberg");
+ ShortestPathVertexArrayWritable arr = new
ShortestPathVertexArrayWritable();
+ arr.set(textArr);
+ tmp.put(new ShortestPathVertex(0, city), arr);
+ } else if (city.equals("Kassel")) {
+ ShortestPathVertex[] textArr = new ShortestPathVertex[2];
+ textArr[0] = new ShortestPathVertex(502, "Muenchen");
+ textArr[1] = new ShortestPathVertex(173, "Frankfurt");
+ ShortestPathVertexArrayWritable arr = new
ShortestPathVertexArrayWritable();
+ arr.set(textArr);
+ tmp.put(new ShortestPathVertex(0, city), arr);
+ } else if (city.equals("Erfurt")) {
+ ShortestPathVertex[] textArr = new ShortestPathVertex[1];
+ textArr[0] = new ShortestPathVertex(186, "Wuerzburg");
+ ShortestPathVertexArrayWritable arr = new
ShortestPathVertexArrayWritable();
+ arr.set(textArr);
+ tmp.put(new ShortestPathVertex(0, city), arr);
+ } else if (city.equals("Wuerzburg")) {
+ ShortestPathVertex[] textArr = new ShortestPathVertex[3];
+ textArr[0] = new ShortestPathVertex(217, "Frankfurt");
+ textArr[1] = new ShortestPathVertex(168, "Erfurt");
+ textArr[2] = new ShortestPathVertex(103, "Nuernberg");
+ ShortestPathVertexArrayWritable arr = new
ShortestPathVertexArrayWritable();
+ arr.set(textArr);
+ tmp.put(new ShortestPathVertex(0, city), arr);
+ } else if (city.equals("Mannheim")) {
+ ShortestPathVertex[] textArr = new ShortestPathVertex[2];
+ textArr[0] = new ShortestPathVertex(80, "Karlsruhe");
+ textArr[1] = new ShortestPathVertex(85, "Frankfurt");
+ ShortestPathVertexArrayWritable arr = new
ShortestPathVertexArrayWritable();
+ arr.set(textArr);
+ tmp.put(new ShortestPathVertex(0, city), arr);
+ } else if (city.equals("Karlsruhe")) {
+ ShortestPathVertex[] textArr = new ShortestPathVertex[2];
+ textArr[0] = new ShortestPathVertex(250, "Augsburg");
+ textArr[1] = new ShortestPathVertex(80, "Mannheim");
+ ShortestPathVertexArrayWritable arr = new
ShortestPathVertexArrayWritable();
+ arr.set(textArr);
+ tmp.put(new ShortestPathVertex(0, city), arr);
+ } else if (city.equals("Augsburg")) {
+ ShortestPathVertex[] textArr = new ShortestPathVertex[2];
+ textArr[0] = new ShortestPathVertex(250, "Karlsruhe");
+ textArr[1] = new ShortestPathVertex(84, "Muenchen");
+ ShortestPathVertexArrayWritable arr = new
ShortestPathVertexArrayWritable();
+ arr.set(textArr);
+ tmp.put(new ShortestPathVertex(0, city), arr);
+ } else if (city.equals("Nuernberg")) {
+ ShortestPathVertex[] textArr = new ShortestPathVertex[3];
+ textArr[0] = new ShortestPathVertex(183, "Stuttgart");
+ textArr[1] = new ShortestPathVertex(167, "Muenchen");
+ textArr[2] = new ShortestPathVertex(103, "Wuerzburg");
+ ShortestPathVertexArrayWritable arr = new
ShortestPathVertexArrayWritable();
+ arr.set(textArr);
+ tmp.put(new ShortestPathVertex(0, city), arr);
+ } else if (city.equals("Muenchen")) {
+ ShortestPathVertex[] textArr = new ShortestPathVertex[3];
+ textArr[0] = new ShortestPathVertex(167, "Nuernberg");
+ textArr[1] = new ShortestPathVertex(173, "Kassel");
+ textArr[2] = new ShortestPathVertex(84, "Augsburg");
+ ShortestPathVertexArrayWritable arr = new
ShortestPathVertexArrayWritable();
+ arr.set(textArr);
+ tmp.put(new ShortestPathVertex(0, city), arr);
+ }
+ }
- @Test
- public void testShortestPathsWithWrongArgs() {
- try {
- ShortestPaths.main(new String[]{"1", ".", "."});
- fail("ShortestPaths should fail if the arguments list contains wrong
items");
- } catch (Exception e) {
- // everything ok
+ SequenceFile.Writer writer = SequenceFile
+ .createWriter(fs, conf, new Path(INPUT), ShortestPathVertex.class,
+ ShortestPathVertexArrayWritable.class);
+ for (Map.Entry<ShortestPathVertex, ShortestPathVertexArrayWritable> e : tmp
+ .entrySet()) {
+ writer.append(e.getKey(), e.getValue());
}
+ writer.close();
}
}