This project has retired. For details please refer to its Attic page.
MaxComputationTest 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  package org.apache.giraph.examples;
19  
20  import org.apache.giraph.conf.GiraphConfiguration;
21  import org.apache.giraph.io.formats.IdWithValueTextOutputFormat;
22  import org.apache.giraph.io.formats.IntIntNullTextInputFormat;
23  import org.apache.giraph.utils.InternalVertexRunner;
24  import org.junit.Test;
25  
26  import com.google.common.collect.Maps;
27  
28  import java.util.Map;
29  
30  import static org.junit.Assert.assertEquals;
31  
32  /**
33   * Test for max computation
34   */
35  public class MaxComputationTest {
36    @Test
37    public void testMax() throws Exception {
38      String[] graph = {
39          "5 1",
40          "1 5 2",
41          "2 5",
42      };
43  
44      GiraphConfiguration conf = new GiraphConfiguration();
45      conf.setComputationClass(MaxComputation.class);
46      conf.setVertexInputFormatClass(IntIntNullTextInputFormat.class);
47      conf.setVertexOutputFormatClass(IdWithValueTextOutputFormat.class);
48      Iterable<String> results = InternalVertexRunner.run(conf, graph);
49  
50      Map<Integer, Integer> values = parseResults(results);
51      assertEquals(3, values.size());
52      assertEquals(5, (int) values.get(1));
53      assertEquals(5, (int) values.get(2));
54      assertEquals(5, (int) values.get(5));
55    }
56  
57    private static Map<Integer, Integer> parseResults(Iterable<String> results) {
58      Map<Integer, Integer> values = Maps.newHashMap();
59      for (String line : results) {
60        String[] tokens = line.split("\\s+");
61        int id = Integer.valueOf(tokens[0]);
62        int value = Integer.valueOf(tokens[1]);
63        values.put(id, value);
64      }
65      return values;
66    }
67  }