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.edge;
2021import com.google.common.collect.Lists;
22import org.apache.hadoop.io.LongWritable;
23import org.apache.hadoop.io.NullWritable;
24import org.junit.Before;
25import org.junit.Test;
2627import java.util.Collection;
28import java.util.Iterator;
29import java.util.List;
3031importstatic org.apache.giraph.graph.TestVertexAndEdges.instantiateOutEdges;
32importstatic org.junit.Assert.assertEquals;
3334/**35 * Tests {@link OutEdges} implementations with null edge values.36 */37publicclassTestNullValueEdges {
38/** {@link OutEdges} classes to be tested. */39private Collection<Class<? extends MutableOutEdges>>
40 edgesClasses = Lists.newArrayList();
4142 @Before
43publicvoid setUp() {
44 edgesClasses.add(LongNullArrayEdges.class);
45 edgesClasses.add(LongNullHashSetEdges.class);
46 }
4748 @Test
49publicvoid testEdges() {
50for (Class<? extends OutEdges> edgesClass : edgesClasses) {
51 testEdgesClass(edgesClass);
52 }
53 }
5455privatevoid testEdgesClass(
56 Class<? extends OutEdges> edgesClass) {
57 OutEdges<LongWritable, NullWritable> edges =
58 (OutEdges<LongWritable, NullWritable>)
59 instantiateOutEdges(edgesClass);
6061 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)));
6566 edges.initialize(initialEdges);
67 assertEquals(3, edges.size());
6869 edges.add(EdgeFactory.createReusable(new LongWritable(4)));
70 assertEquals(4, edges.size());
7172 edges.remove(new LongWritable(2));
73 assertEquals(3, edges.size());
74 }
7576/**77 * Test in-place edge mutations via the iterable returned by {@link78 * org.apache.giraph.graph.Vertex#getMutableEdges()}.79 */80 @Test
81publicvoid testMutateEdges() {
82for (Class<? extends MutableOutEdges> edgesClass : edgesClasses) {
83 testMutateEdgesClass(edgesClass);
84 }
85 }
8687privatevoid testMutateEdgesClass(
88 Class<? extends MutableOutEdges> edgesClass) {
89 MutableOutEdges<LongWritable, NullWritable> edges =
90 (MutableOutEdges<LongWritable, NullWritable>)
91 instantiateOutEdges(edgesClass);
9293 edges.initialize();
9495// Add 10 edges with id i, for i = 0..996for (int i = 0; i < 10; ++i) {
97 edges.add(EdgeFactory.create(new LongWritable(i)));
98 }
99100// Use the mutable iterator to remove edges with even id101 Iterator<MutableEdge<LongWritable, NullWritable>> edgeIt =
102 edges.mutableIterator();
103while (edgeIt.hasNext()) {
104if (edgeIt.next().getTargetVertexId().get() % 2 == 0) {
105 edgeIt.remove();
106 }
107 }
108109// We should now have 5 edges110 assertEquals(5, edges.size());
111// The edge ids should be all odd112for (Edge<LongWritable, NullWritable> edge : edges) {
113 assertEquals(1, edge.getTargetVertexId().get() % 2);
114 }
115 }
116 }