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.framework.block;
1920importstatic org.junit.Assert.assertEquals;
2122import java.util.ArrayList;
23import java.util.Collections;
24import java.util.Iterator;
25import java.util.Random;
2627import org.apache.giraph.block_app.framework.piece.AbstractPiece;
2829import com.google.common.base.Preconditions;
30import com.google.common.collect.Iterables;
3132import it.unimi.dsi.fastutil.ints.IntArrayList;
3334 @SuppressWarnings({"unchecked", "rawtypes"})
35publicclassBlockTestingUtils {
3637BlockTestingUtils() { }
3839privatestaticfinalint NUM_TRIALS = 10;
40privatestaticfinalint REPEAT_TIMES = 10;
4142publicstaticint testSequential(
43 Iterable<? extends AbstractPiece> referenceImpl,
44 Iterable<? extends AbstractPiece> testImpl) {
45int length = 0;
4647CheckIterator checkIterator = newCheckIterator(
48 referenceImpl.iterator(), testImpl.iterator());
49while (checkIterator.hasNext()) {
50 checkIterator.next();
51 length++;
52 }
5354 System.out.println("Length is : " + length);
55return length;
56 }
5758privatestaticboolean anyHasNext(ArrayList<? extends Iterator> arr) {
59for (Iterator t : arr) {
60if (t.hasNext()) {
61returntrue;
62 }
63 }
64return false;
65 }
6667privatestaticvoid testRandom(int length,
68 Iterable<? extends AbstractPiece> referenceImpl,
69 Iterable<? extends AbstractPiece> testImpl) {
70 Random rand = new Random();
7172 ArrayList<CheckIterator<AbstractPiece>> arr = new ArrayList<>();
73 IntArrayList lengths = new IntArrayList(NUM_TRIALS);
74for (int i = 0; i < NUM_TRIALS; i++) {
75 lengths.add(0);
76 }
77for (int i = 0; i < NUM_TRIALS; i++) {
78 arr.add(newCheckIterator(referenceImpl.iterator(), testImpl.iterator()));
79 }
8081int totalCount = 0;
82while (anyHasNext(arr)) {
83int index = rand.nextInt(NUM_TRIALS);
84while (!arr.get(index).hasNext()) {
85 index = rand.nextInt(NUM_TRIALS);
86 }
87CheckIterator it = arr.get(index);
88 it.next();
89int itLength = lengths.getInt(index);
90 lengths.set(index, itLength + 1);
91 totalCount++;
92 }
93 assertEquals("TotalCount should be length * NUM_TRIALS", length * NUM_TRIALS, totalCount);
94 System.out.println("Final count is : " + totalCount);
95 }
9697/**98 * Tests both the length of the iterator returned by the block, as-well as the deterministic behavior99 * expected by calling .iterator() against the referenceImpl.100 * @param referenceImpl : A list of pieces in the expected order101 * @param testImpl : A list of pieces to test against (the Block)102 */103publicstaticvoid testIndependence(Iterable<? extends AbstractPiece> referenceImpl,
104 Iterable<? extends AbstractPiece> testImpl) {
105int length = testSequential(referenceImpl, testImpl);
106 testRandom(length, referenceImpl, testImpl);
107 }
108109/**110 * Test how the block interacts with a repeatBlock. The expected result is to111 * see the pieces in referenceImpl show up REPEAT_TIMES many times.112 * @param referenceImpl : A list of pieces in the expected order113 * @param block : The block to test114 */115publicstaticvoid testNestedRepeatBlock(Iterable<? extends AbstractPiece> referenceImpl, Block block) {
116 Block repeatBlock = new RepeatBlock(
117 REPEAT_TIMES,
118 block
119 );
120 testIndependence(
121 Iterables.concat(Collections.nCopies(REPEAT_TIMES, referenceImpl)),
122 repeatBlock
123 );
124 }
125126publicstaticclass CheckIterator<T> implements Iterator {
127128privatefinal Iterator<T> fst;
129privatefinal Iterator<T> snd;
130131publicCheckIterator(Iterator<T> fst, Iterator<T> snd) {
132this.fst = fst;
133this.snd = snd;
134 }
135136 @Override
137publicboolean hasNext() {
138boolean fstHasNxt = fst.hasNext();
139boolean sndHasNxt = snd.hasNext();
140 Preconditions.checkArgument(fstHasNxt == sndHasNxt, "Expect hasNext() on " +
141"both iterators to be identical. Got: " + fst.hasNext() + " and " + snd.hasNext());
142return fstHasNxt;
143 }
144145 @Override
146public Object next() {
147 T fstNxt = fst.next();
148 T sndNxt = snd.next();
149 Preconditions.checkArgument(fstNxt == sndNxt, "Expect objs returned by " +
150"both iterators to be identical. Got: " + fstNxt + " and " + sndNxt);
151return fstNxt;
152 }
153154 @Override
155publicvoid remove() {
156thrownew RuntimeException("Not implemented");
157 }
158159 }
160161 }