1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.giraph.examples;
2021import org.apache.giraph.edge.EdgeFactory;
22import org.apache.giraph.graph.DefaultVertex;
23import org.apache.giraph.graph.Vertex;
24import org.apache.giraph.utils.MockUtils;
25import org.apache.hadoop.io.IntWritable;
26import org.apache.hadoop.io.NullWritable;
27import org.junit.Test;
2829import com.google.common.collect.Lists;
3031importstatic org.apache.giraph.examples.SimpleTriangleClosingComputation.IntArrayListWritable;
32importstatic org.junit.Assert.assertEquals;
3334/**35 * Contains a simple unit test for {@link SimpleTriangleClosingComputation}36 */37publicclassSimpleTriangleClosingComputationTest {
3839/**40 * Test the behavior of the triangle closing algorithm:41 * does it send all its out edge values to all neighbors?42 */43 @Test
44publicvoid testSuperstepZero() throws Exception {
45// this guy should end up with an array value of 446 Vertex<IntWritable, IntArrayListWritable, NullWritable> vertex =
47new DefaultVertex<IntWritable, IntArrayListWritable, NullWritable>();
4849 IntArrayListWritable alw = new IntArrayListWritable();
5051 SimpleTriangleClosingComputation computation =
52new SimpleTriangleClosingComputation();
53 MockUtils.MockedEnvironment env = MockUtils.prepareVertexAndComputation(
54 vertex, new IntWritable(1), alw, false, computation, 0L);
5556 vertex.addEdge(EdgeFactory.create(new IntWritable(5)));
57 vertex.addEdge(EdgeFactory.create(new IntWritable(7)));
5859 computation.compute(vertex, Lists.<IntWritable>newArrayList(
60new IntWritable(83), new IntWritable(42)));
6162 env.verifyMessageSentToAllEdges(vertex, new IntWritable(5));
63 env.verifyMessageSentToAllEdges(vertex, new IntWritable(7));
64 }
6566/** Test behavior of compute() with incoming messages (superstep 1) */67 @Test
68publicvoid testSuperstepOne() throws Exception {
69// see if the vertex interprets its incoming70// messages properly to verify the algorithm71 Vertex<IntWritable, IntArrayListWritable, NullWritable> vertex =
72new DefaultVertex<IntWritable, IntArrayListWritable, NullWritable>();
73 SimpleTriangleClosingComputation computation =
74new SimpleTriangleClosingComputation();
75 MockUtils.MockedEnvironment env = MockUtils.prepareVertexAndComputation(
76 vertex, new IntWritable(1), null, false, computation, 1L);
7778// superstep 1: can the vertex process these correctly?79 computation.compute(vertex, Lists.<IntWritable>newArrayList(
80new IntWritable(7),
81new IntWritable(3),
82new IntWritable(4),
83new IntWritable(7),
84new IntWritable(4),
85new IntWritable(2),
86new IntWritable(4)));
87final String pairCheck = "[4, 7]";
88 assertEquals(pairCheck, vertex.getValue().toString());
89 }
90 }