This project has retired. For details please refer to its Attic page.
ConnectedComponentsComputationTest 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.combiner.MinimumIntMessageCombiner;
22  import org.apache.giraph.conf.GiraphConfiguration;
23  import org.apache.giraph.edge.ByteArrayEdges;
24  import org.apache.giraph.io.formats.IdWithValueTextOutputFormat;
25  import org.apache.giraph.io.formats.IntIntNullTextInputFormat;
26  import org.apache.giraph.utils.InternalVertexRunner;
27  import org.junit.Test;
28  
29  import com.google.common.base.Splitter;
30  import com.google.common.collect.HashMultimap;
31  import com.google.common.collect.Iterables;
32  import com.google.common.collect.SetMultimap;
33  
34  import java.util.Set;
35  
36  import static org.junit.Assert.assertEquals;
37  import static org.junit.Assert.assertTrue;
38  
39  /**
40   *  Tests for {@link ConnectedComponentsComputation}
41   */
42  public class ConnectedComponentsComputationTest {
43  
44      /**
45       * A local integration test on toy data
46       */
47      @Test
48      public void testToyData() throws Exception {
49  
50          // a small graph with three components
51          String[] graph = new String[] {
52                  "1 2 3",
53                  "2 1 4 5",
54                  "3 1 4",
55                  "4 2 3 5 13",
56                  "5 2 4 12 13",
57                  "12 5 13",
58                  "13 4 5 12",
59  
60                  "6 7 8",
61                  "7 6 10 11",
62                  "8 6 10",
63                  "10 7 8 11",
64                  "11 7 10",
65  
66                  "9" };
67  
68          GiraphConfiguration conf = new GiraphConfiguration();
69          conf.setComputationClass(ConnectedComponentsComputation.class);
70          conf.setOutEdgesClass(ByteArrayEdges.class);
71          conf.setMessageCombinerClass(MinimumIntMessageCombiner.class);
72          conf.setVertexInputFormatClass(IntIntNullTextInputFormat.class);
73          conf.setVertexOutputFormatClass(IdWithValueTextOutputFormat.class);
74  
75          // run internally
76          Iterable<String> results = InternalVertexRunner.run(conf, graph);
77  
78          SetMultimap<Integer,Integer> components = parseResults(results);
79  
80          Set<Integer> componentIDs = components.keySet();
81          assertEquals(3, componentIDs.size());
82          assertTrue(componentIDs.contains(1));
83          assertTrue(componentIDs.contains(6));
84          assertTrue(componentIDs.contains(9));
85  
86          Set<Integer> componentOne = components.get(1);
87          assertEquals(7, componentOne.size());
88          assertTrue(componentOne.contains(1));
89          assertTrue(componentOne.contains(2));
90          assertTrue(componentOne.contains(3));
91          assertTrue(componentOne.contains(4));
92          assertTrue(componentOne.contains(5));
93          assertTrue(componentOne.contains(12));
94          assertTrue(componentOne.contains(13));
95  
96          Set<Integer> componentTwo = components.get(6);
97          assertEquals(5, componentTwo.size());
98          assertTrue(componentTwo.contains(6));
99          assertTrue(componentTwo.contains(7));
100         assertTrue(componentTwo.contains(8));
101         assertTrue(componentTwo.contains(10));
102         assertTrue(componentTwo.contains(11));
103 
104         Set<Integer> componentThree = components.get(9);
105         assertEquals(1, componentThree.size());
106         assertTrue(componentThree.contains(9));
107     }
108 
109     private SetMultimap<Integer,Integer> parseResults(
110             Iterable<String> results) {
111         SetMultimap<Integer,Integer> components = HashMultimap.create();
112         for (String result : results) {
113             Iterable<String> parts = Splitter.on('\t').split(result);
114             int vertex = Integer.parseInt(Iterables.get(parts, 0));
115             int component = Integer.parseInt(Iterables.get(parts, 1));
116             components.put(component, vertex);
117         }
118         return components;
119     }
120 }