This project has retired. For details please refer to its Attic page.
TestRepeatUntilBlock 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.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.apache.giraph.function.primitive.PrimitiveRefs.IntRef;
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  import com.google.common.collect.Iterables;
34  
35  /**
36   * Tests repeatUntilBlock's correctness
37   */
38  public class TestRepeatUntilBlock {
39  
40    public static final int REPEAT_TIMES = 5;
41  
42    private static final Supplier<Boolean> falseSupplier = new Supplier<Boolean>() {
43        @Override
44        public Boolean get() {
45          return false;
46        }
47    };
48  
49    @Test
50    public void testRepeatUntilBlockBasic() throws Exception {
51      Piece piece1 = new Piece();
52      Piece piece2 = new Piece();
53      Block innerBlock = new SequenceBlock(piece1, piece2);
54      Block repeatBlock = new RepeatUntilBlock(
55        REPEAT_TIMES,
56        innerBlock,
57        falseSupplier
58      );
59      BlockTestingUtils.testIndependence(
60        Iterables.concat(Collections.nCopies(REPEAT_TIMES, Arrays.asList(piece1, piece2))),
61        repeatBlock);
62      Assert.assertEquals(2, innerBlock.getPieceCount().getCount());
63      Assert.assertFalse(repeatBlock.getPieceCount().isKnown());
64    }
65  
66    @Test
67    public void testRepeatUntilBlockBasicExit() throws Exception {
68      testRepeatUntilBlockBasicExit(1, 1);
69      testRepeatUntilBlockBasicExit(1, 3);
70      testRepeatUntilBlockBasicExit(3, 1);
71      testRepeatUntilBlockBasicExit(3, 2);
72      testRepeatUntilBlockBasicExit(4, 7);
73    }
74  
75    private void testRepeatUntilBlockBasicExit(int inner, int outer) {
76      final IntRef counter = new IntRef(outer + 1);
77      Supplier<Boolean> countDown = new Supplier<Boolean>() {
78        @Override
79        public Boolean get() {
80          counter.value--;
81          return counter.value == 0;
82        }
83      };
84      Piece piece = new Piece();
85      Block repeatBlock = new RepeatUntilBlock(
86        outer + 4,
87        new RepeatBlock(inner, piece),
88        countDown
89      );
90      BlockTestingUtils.testSequential(
91        Iterables.concat(Collections.<Piece>nCopies(outer * inner, piece)),
92        repeatBlock);
93    }
94  
95    @Test
96    public void testNestedRepeatUntilBlock() throws Exception {
97      Piece piece1 = new Piece();
98      Piece piece2 = new Piece();
99      Block innerBlock = new SequenceBlock(piece1, piece2);
100     Block repeatBlock = new RepeatUntilBlock(
101       REPEAT_TIMES,
102       innerBlock,
103       falseSupplier
104     );
105     BlockTestingUtils.testNestedRepeatBlock(
106       Iterables.concat(Collections.nCopies(REPEAT_TIMES, Arrays.asList(piece1, piece2))),
107       repeatBlock);
108   }
109 
110   @Test
111   public void testRepeatUntilBlockUnlimited() throws Exception {
112     Block innerBlock = new SequenceBlock(new Piece());
113     // Can't test with testIndependence - spin up our own test inline
114     Supplier<Boolean> countingSupplier = new Supplier<Boolean>() {
115       private int i = 0;
116 
117       @Override
118       public Boolean get() {
119         i++;
120         return i > REPEAT_TIMES;
121       }
122     };
123     Block repeatBlock = RepeatUntilBlock.unlimited(
124       innerBlock,
125       countingSupplier
126     );
127     int count = 0;
128     Iterator<AbstractPiece> it = repeatBlock.iterator();
129     while (it.hasNext()) {
130       it.next();
131       count++;
132     }
133     assertEquals("Count must be equal to REPEAT_TIMES", REPEAT_TIMES, count);
134   }
135 
136 }