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 */18package org.apache.giraph.block_app.reducers.array;
1920importstatic org.junit.Assert.assertEquals;
21importstatic org.junit.Assert.fail;
2223import org.apache.giraph.block_app.reducers.array.HugeArrayUtils.ObjectStriping;
24import org.junit.Test;
2526publicclassObjectStripingTest {
2728privatevoid testStriping(int size, int splits) {
29 ObjectStriping striping = new ObjectStriping(size, splits);
3031int numPerSplit = size / splits;
3233int prevSplitIndex = 0;
34int prevInsideIndex = -1;
3536 assertEquals(0, striping.getSplitStart(0));
3738for (int i = 0; i < size; i++) {
39int splitIndex = striping.getSplitIndex(i);
40int insideIndex = striping.getInsideIndex(i);
414243if (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 }
5152int splitSize = striping.getSplitSize(splitIndex);
53if (splitSize != numPerSplit && splitSize != numPerSplit + 1) {
54 fail(splitSize + " " + numPerSplit);
55 }
56 prevSplitIndex = splitIndex;
57 prevInsideIndex = insideIndex;
58 }
5960 assertEquals(prevSplitIndex + 1, splits);
61 assertEquals(prevInsideIndex + 1, striping.getSplitSize(prevSplitIndex));
62 }
6364 @Test
65publicvoid 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 }