This project has retired. For details please refer to its Attic page.
TestTestGraph xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Test TestGraph
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     * Vertex value combiner that sums up long vertex values
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  }