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.framework.block;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.Iterator;
25  
26  import org.apache.giraph.block_app.framework.piece.AbstractPiece;
27  import org.apache.giraph.block_app.framework.piece.Piece;
28  import org.apache.giraph.function.Supplier;
29  import org.junit.Test;
30  
31  import com.google.common.collect.Iterables;
32  
33  /**
34   * Tests repeatUntilBlock's correctness
35   */
36  public class TestRepeatUntilBlock {
37  
38    public static final int REPEAT_TIMES = 5;
39  
40    private static final Supplier<Boolean> falseSupplier = new Supplier<Boolean>() {
41        @Override
42        public Boolean get() {
43          return false;
44        }
45    };
46  
47    @Test
48    public void testRepeatUntilBlockBasic() throws Exception {
49      Piece piece1 = new Piece();
50      Piece piece2 = new Piece();
51      Block innerBlock = new SequenceBlock(piece1, piece2);
52      Block repeatBlock = new RepeatUntilBlock(
53        REPEAT_TIMES,
54        innerBlock,
55        falseSupplier
56      );
57      BlockTestingUtils.testIndependence(
58        Iterables.concat(Collections.nCopies(REPEAT_TIMES, Arrays.asList(piece1, piece2))),
59        repeatBlock);
60    }
61  
62    @Test
63    public void testNestedRepeatUntilBlock() throws Exception {
64      Piece piece1 = new Piece();
65      Piece piece2 = new Piece();
66      Block innerBlock = new SequenceBlock(piece1, piece2);
67      Block repeatBlock = new RepeatUntilBlock(
68        REPEAT_TIMES,
69        innerBlock,
70        falseSupplier
71      );
72      BlockTestingUtils.testNestedRepeatBlock(
73        Iterables.concat(Collections.nCopies(REPEAT_TIMES, Arrays.asList(piece1, piece2))),
74        repeatBlock);
75    }
76  
77    @Test
78    public void testRepeatUntilBlockUnlimited() throws Exception {
79      Block innerBlock = new SequenceBlock(new Piece());
80      // Can't test with testIndependence - spin up our own test inline
81      Supplier<Boolean> countingSupplier = new Supplier<Boolean>() {
82        private int i = 0;
83  
84        @Override
85        public Boolean get() {
86          i++;
87          return i > REPEAT_TIMES;
88        }
89      };
90      Block repeatBlock = RepeatUntilBlock.unlimited(
91        innerBlock,
92        countingSupplier
93      );
94      int count = 0;
95      Iterator<AbstractPiece> it = repeatBlock.iterator();
96      while (it.hasNext()) {
97        it.next();
98        count++;
99      }
100     assertEquals("Count must be equal to REPEAT_TIMES", REPEAT_TIMES, count);
101   }
102 
103 }