source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/util/TestCyclicIteration.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 2.2 KB
Line 
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 */
18package org.apache.hadoop.util;
19
20import java.util.ArrayList;
21import java.util.Arrays;
22import java.util.List;
23import java.util.Map;
24import java.util.NavigableMap;
25import java.util.TreeMap;
26
27public class TestCyclicIteration extends junit.framework.TestCase {
28  public void testCyclicIteration() throws Exception {
29    for(int n = 0; n < 5; n++) {
30      checkCyclicIteration(n);
31    }
32  }
33
34  private static void checkCyclicIteration(int numOfElements) {
35    //create a tree map
36    final NavigableMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
37    final Integer[] integers = new Integer[numOfElements];
38    for(int i = 0; i < integers.length; i++) {
39      integers[i] = 2*i;
40      map.put(integers[i], integers[i]);
41    }
42    System.out.println("\n\nintegers=" + Arrays.asList(integers));
43    System.out.println("map=" + map);
44
45    //try starting everywhere
46    for(int start = -1; start <= 2*integers.length - 1; start++) {
47      //get a cyclic iteration
48      final List<Integer> iteration = new ArrayList<Integer>(); 
49      for(Map.Entry<Integer, Integer> e : new CyclicIteration<Integer, Integer>(map, start)) {
50        iteration.add(e.getKey());
51      }
52      System.out.println("start=" + start + ", iteration=" + iteration);
53     
54      //verify results
55      for(int i = 0; i < integers.length; i++) {
56        final int j = ((start+2)/2 + i)%integers.length;
57        assertEquals("i=" + i + ", j=" + j, iteration.get(i), integers[j]);
58      }
59    }
60  }
61}
Note: See TracBrowser for help on using the repository browser.