This project has retired. For details please refer to its Attic page.
ObjectStripingTest 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.block_app.reducers.array;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.fail;
22  
23  import org.apache.giraph.block_app.reducers.array.HugeArrayUtils.ObjectStriping;
24  import org.junit.Test;
25  
26  public class ObjectStripingTest {
27  
28    private void testStriping(int size, int splits) {
29      ObjectStriping striping = new ObjectStriping(size, splits);
30  
31      int numPerSplit = size / splits;
32  
33      int prevSplitIndex = 0;
34      int prevInsideIndex = -1;
35  
36      assertEquals(0, striping.getSplitStart(0));
37  
38      for (int i = 0; i < size; i++) {
39        int splitIndex = striping.getSplitIndex(i);
40        int insideIndex = striping.getInsideIndex(i);
41  
42  
43        if (prevInsideIndex + 1 == striping.getSplitSize(prevSplitIndex)) {
44          assertEquals(i, striping.getSplitStart(splitIndex));
45          assertEquals(splitIndex, prevSplitIndex + 1);
46          assertEquals(insideIndex, 0);
47        } else {
48          assertEquals(splitIndex, prevSplitIndex);
49          assertEquals(insideIndex, prevInsideIndex + 1);
50        }
51  
52        int splitSize = striping.getSplitSize(splitIndex);
53        if (splitSize != numPerSplit && splitSize != numPerSplit + 1) {
54          fail(splitSize + " " + numPerSplit);
55        }
56        prevSplitIndex = splitIndex;
57        prevInsideIndex = insideIndex;
58      }
59  
60      assertEquals(prevSplitIndex + 1, splits);
61      assertEquals(prevInsideIndex + 1, striping.getSplitSize(prevSplitIndex));
62    }
63  
64    @Test
65    public void test() {
66      testStriping(5, 5);
67      testStriping(6, 5);
68      testStriping(7, 5);
69      testStriping(9, 5);
70      testStriping(10, 5);
71      testStriping(100, 5);
72      testStriping(101, 5);
73      testStriping(104, 5);
74    }
75  }