This project has retired. For details please refer to its Attic page.
TestMultiGraphEdges 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.edge;
20  
21  import com.google.common.collect.Lists;
22  import org.apache.hadoop.io.DoubleWritable;
23  import org.apache.hadoop.io.LongWritable;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import java.util.Collection;
28  import java.util.List;
29  
30  import static org.apache.giraph.graph.TestVertexAndEdges.instantiateOutEdges;
31  import static org.junit.Assert.assertEquals;
32  
33  /**
34   * Tests {@link OutEdges} implementations that allow parallel edges.
35   */
36  public class TestMultiGraphEdges {
37    /** {@link OutEdges} classes to be tested. */
38    private Collection<Class<? extends OutEdges>> edgesClasses =
39        Lists.newArrayList();
40  
41    @Before
42    public void setUp() {
43      edgesClasses.add(ByteArrayEdges.class);
44      edgesClasses.add(ArrayListEdges.class);
45      edgesClasses.add(HashMultimapEdges.class);
46      edgesClasses.add(LongDoubleArrayEdges.class);
47    }
48  
49    /**
50     * Ensures that all multigraph {@link OutEdges} implementations allow
51     * parallel edges.
52     */
53    @Test
54    public void testParallelEdges() {
55      for (Class<? extends OutEdges> edgesClass : edgesClasses) {
56        testParallelEdgesClass(edgesClass);
57      }
58    }
59  
60    private void testParallelEdgesClass(
61        Class<? extends OutEdges> edgesClass) {
62      OutEdges<LongWritable, DoubleWritable> edges =
63          instantiateOutEdges(edgesClass);
64  
65      // Initial edges list contains parallel edges.
66      List<Edge<LongWritable, DoubleWritable>> initialEdges = Lists.newArrayList(
67          EdgeFactory.create(new LongWritable(1), new DoubleWritable(1)),
68          EdgeFactory.create(new LongWritable(2), new DoubleWritable(2)),
69          EdgeFactory.create(new LongWritable(3), new DoubleWritable(3)),
70          EdgeFactory.create(new LongWritable(2), new DoubleWritable(20)));
71  
72      edges.initialize(initialEdges);
73  
74      // The parallel edges should still be there.
75      assertEquals(4, edges.size());
76  
77      // Adding a parallel edge should increase the number of edges.
78      edges.add(EdgeFactory.create(new LongWritable(3), new DoubleWritable(30)));
79      assertEquals(5, edges.size());
80  
81      // Removing edges pointing to a given vertex should remove all parallel
82      // edges.
83      edges.remove(new LongWritable(2));
84      assertEquals(3, edges.size());
85    }
86  }