This project has retired. For details please refer to its Attic page.
TestNullValueEdges 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.LongWritable;
23  import org.apache.hadoop.io.NullWritable;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import java.util.Collection;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  import static org.apache.giraph.graph.TestVertexAndEdges.instantiateOutEdges;
32  import static org.junit.Assert.assertEquals;
33  
34  /**
35   * Tests {@link OutEdges} implementations with null edge values.
36   */
37  public class TestNullValueEdges {
38    /** {@link OutEdges} classes to be tested. */
39    private Collection<Class<? extends MutableOutEdges>>
40        edgesClasses = Lists.newArrayList();
41  
42    @Before
43    public void setUp() {
44      edgesClasses.add(LongNullArrayEdges.class);
45      edgesClasses.add(LongNullHashSetEdges.class);
46    }
47  
48    @Test
49    public void testEdges() {
50      for (Class<? extends OutEdges> edgesClass : edgesClasses) {
51        testEdgesClass(edgesClass);
52      }
53    }
54  
55    private void testEdgesClass(
56        Class<? extends OutEdges> edgesClass) {
57      OutEdges<LongWritable, NullWritable> edges =
58          (OutEdges<LongWritable, NullWritable>)
59              instantiateOutEdges(edgesClass);
60  
61      List<Edge<LongWritable, NullWritable>> initialEdges = Lists.newArrayList(
62          EdgeFactory.create(new LongWritable(1)),
63          EdgeFactory.create(new LongWritable(2)),
64          EdgeFactory.create(new LongWritable(3)));
65  
66      edges.initialize(initialEdges);
67      assertEquals(3, edges.size());
68  
69      edges.add(EdgeFactory.createReusable(new LongWritable(4)));
70      assertEquals(4, edges.size());
71  
72      edges.remove(new LongWritable(2));
73      assertEquals(3, edges.size());
74    }
75  
76    /**
77     * Test in-place edge mutations via the iterable returned by {@link
78     * org.apache.giraph.graph.Vertex#getMutableEdges()}.
79     */
80    @Test
81    public void testMutateEdges() {
82      for (Class<? extends MutableOutEdges> edgesClass : edgesClasses) {
83        testMutateEdgesClass(edgesClass);
84      }
85    }
86  
87    private void testMutateEdgesClass(
88        Class<? extends MutableOutEdges> edgesClass) {
89      MutableOutEdges<LongWritable, NullWritable> edges =
90         (MutableOutEdges<LongWritable, NullWritable>)
91             instantiateOutEdges(edgesClass);
92  
93      edges.initialize();
94  
95      // Add 10 edges with id i, for i = 0..9
96      for (int i = 0; i < 10; ++i) {
97        edges.add(EdgeFactory.create(new LongWritable(i)));
98      }
99  
100     // Use the mutable iterator to remove edges with even id
101     Iterator<MutableEdge<LongWritable, NullWritable>> edgeIt =
102         edges.mutableIterator();
103     while (edgeIt.hasNext()) {
104       if (edgeIt.next().getTargetVertexId().get() % 2 == 0) {
105         edgeIt.remove();
106       }
107     }
108 
109     // We should now have 5 edges
110     assertEquals(5, edges.size());
111     // The edge ids should be all odd
112     for (Edge<LongWritable, NullWritable> edge : edges) {
113       assertEquals(1, edge.getTargetVertexId().get() % 2);
114     }
115   }
116 }