This project has retired. For details please refer to its Attic page.
SimpleTriangleClosingComputationTest 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.examples;
20  
21  import org.apache.giraph.edge.EdgeFactory;
22  import org.apache.giraph.graph.DefaultVertex;
23  import org.apache.giraph.graph.Vertex;
24  import org.apache.giraph.utils.MockUtils;
25  import org.apache.hadoop.io.IntWritable;
26  import org.apache.hadoop.io.NullWritable;
27  import org.junit.Test;
28  
29  import com.google.common.collect.Lists;
30  
31  import static org.apache.giraph.examples.SimpleTriangleClosingComputation.IntArrayListWritable;
32  import static org.junit.Assert.assertEquals;
33  
34  /**
35   * Contains a simple unit test for {@link SimpleTriangleClosingComputation}
36   */
37  public class SimpleTriangleClosingComputationTest {
38  
39    /**
40     * Test the behavior of the triangle closing algorithm:
41     * does it send all its out edge values to all neighbors?
42     */
43    @Test
44    public void testSuperstepZero() throws Exception {
45      // this guy should end up with an array value of 4
46      Vertex<IntWritable, IntArrayListWritable, NullWritable> vertex =
47          new DefaultVertex<IntWritable, IntArrayListWritable, NullWritable>();
48  
49      IntArrayListWritable alw = new IntArrayListWritable();
50  
51      SimpleTriangleClosingComputation computation =
52          new SimpleTriangleClosingComputation();
53      MockUtils.MockedEnvironment env = MockUtils.prepareVertexAndComputation(
54          vertex, new IntWritable(1), alw, false, computation, 0L);
55  
56      vertex.addEdge(EdgeFactory.create(new IntWritable(5)));
57      vertex.addEdge(EdgeFactory.create(new IntWritable(7)));
58  
59      computation.compute(vertex, Lists.<IntWritable>newArrayList(
60        new IntWritable(83), new IntWritable(42)));
61  
62      env.verifyMessageSentToAllEdges(vertex, new IntWritable(5));
63      env.verifyMessageSentToAllEdges(vertex, new IntWritable(7));
64    }
65  
66    /** Test behavior of compute() with incoming messages (superstep 1) */
67    @Test
68    public void testSuperstepOne() throws Exception {
69      // see if the vertex interprets its incoming
70      // messages properly to verify the algorithm
71      Vertex<IntWritable, IntArrayListWritable, NullWritable> vertex =
72          new DefaultVertex<IntWritable, IntArrayListWritable, NullWritable>();
73      SimpleTriangleClosingComputation computation =
74          new SimpleTriangleClosingComputation();
75      MockUtils.MockedEnvironment env = MockUtils.prepareVertexAndComputation(
76          vertex, new IntWritable(1), null, false, computation, 1L);
77  
78        // superstep 1: can the vertex process these correctly?
79        computation.compute(vertex, Lists.<IntWritable>newArrayList(
80          new IntWritable(7),
81          new IntWritable(3),
82          new IntWritable(4),
83          new IntWritable(7),
84          new IntWritable(4),
85          new IntWritable(2),
86          new IntWritable(4)));
87        final String pairCheck = "[4, 7]";
88        assertEquals(pairCheck, vertex.getValue().toString());
89    }
90   }