This project has retired. For details please refer to its
Attic page.
TestTestGraph xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.giraph.utils;
20
21 import org.apache.giraph.conf.GiraphConfiguration;
22 import org.apache.giraph.conf.GiraphConstants;
23 import org.apache.giraph.edge.HashMapEdges;
24 import org.apache.giraph.graph.Vertex;
25 import org.apache.giraph.graph.VertexValueCombiner;
26 import org.apache.hadoop.io.LongWritable;
27 import org.apache.hadoop.io.NullWritable;
28 import org.junit.Assert;
29 import org.junit.Test;
30
31 import java.util.AbstractMap;
32 import java.util.Map;
33
34
35
36
37 public class TestTestGraph {
38 @Test
39 public void testTestGraph() {
40 GiraphConfiguration conf = new GiraphConfiguration();
41 GiraphConstants.VERTEX_ID_CLASS.set(conf, LongWritable.class);
42 GiraphConstants.VERTEX_VALUE_CLASS.set(conf, LongWritable.class);
43 GiraphConstants.EDGE_VALUE_CLASS.set(conf, NullWritable.class);
44 conf.setVertexValueCombinerClass(SumLongVertexValueCombiner.class);
45 conf.setOutEdgesClass(HashMapEdges.class);
46 TestGraph<LongWritable, LongWritable, NullWritable> testGraph =
47 new TestGraph<>(conf);
48 addVertex(testGraph, 1, 10, 2, 3);
49 addVertex(testGraph, 2, 20, 1, 3);
50 addVertex(testGraph, 3, 30, 1, 2);
51 addVertex(testGraph, 1, 100, 3, 4);
52 addVertex(testGraph, 2, 200, 5, 1, 6);
53
54 Vertex<LongWritable, LongWritable, NullWritable> vertex1 =
55 testGraph.getVertex(new LongWritable(1));
56 Assert.assertEquals(110, vertex1.getValue().get());
57 Assert.assertEquals(3, vertex1.getNumEdges());
58
59 Vertex<LongWritable, LongWritable, NullWritable> vertex2 =
60 testGraph.getVertex(new LongWritable(2));
61 Assert.assertEquals(220, vertex2.getValue().get());
62 Assert.assertEquals(4, vertex2.getNumEdges());
63
64 Vertex<LongWritable, LongWritable, NullWritable> vertex3 =
65 testGraph.getVertex(new LongWritable(3));
66 Assert.assertEquals(30, vertex3.getValue().get());
67 Assert.assertEquals(2, vertex3.getNumEdges());
68 }
69
70 public static void addVertex(
71 TestGraph<LongWritable, LongWritable, NullWritable> graph, long id,
72 long value, long... neighbors) {
73 Map.Entry<LongWritable, NullWritable> edges[] =
74 new Map.Entry[neighbors.length];
75 for (int i = 0; i < neighbors.length; i++) {
76 edges[i] = new AbstractMap.SimpleEntry<>(
77 new LongWritable(neighbors[i]), NullWritable.get());
78 }
79 graph.addVertex(new LongWritable(id), new LongWritable(value), edges);
80 }
81
82
83
84
85 public static class SumLongVertexValueCombiner
86 implements VertexValueCombiner<LongWritable> {
87 @Override
88 public void combine(LongWritable originalVertexValue,
89 LongWritable vertexValue) {
90 originalVertexValue.set(originalVertexValue.get() + vertexValue.get());
91 }
92 }
93 }