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  
21  import java.util.Arrays;
22  
23  import org.apache.giraph.block_app.framework.piece.Piece;
24  import org.apache.giraph.function.Supplier;
25  import org.junit.Test;
26  
27  public class TestIfBlock {
28  
29    private static final Supplier<Boolean> TRUE_SUPPLIER = new Supplier<Boolean>() {
30      @Override
31      public Boolean get() {
32        return true;
33      }
34    };
35  
36    private static final Supplier<Boolean> FALSE_SUPPLIER = new Supplier<Boolean>() {
37      @Override
38      public Boolean get() {
39        return false;
40      }
41    };
42  
43    @Test
44    // Test short-circuiting the if -> then
45    public void testIfBlockThen() throws Exception {
46      Piece piece1 = new Piece();
47      Piece piece2 = new Piece();
48      Block ifBlock = new IfBlock(
49        TRUE_SUPPLIER,
50        new SequenceBlock(piece1, piece2)
51      );
52  
53      BlockTestingUtils.testIndependence(
54          Arrays.asList(piece1, piece2),
55          ifBlock);
56    }
57  
58    @Test
59    // Test short-circuiting the if -> else
60    public void testIfBlockElse() throws Exception {
61      Piece piece1 = new Piece();
62      Piece piece2 = new Piece();
63      Block ifBlock = new IfBlock(
64        FALSE_SUPPLIER,
65        new EmptyBlock(),
66        new SequenceBlock(piece1, piece2)
67      );
68  
69      BlockTestingUtils.testIndependence(
70          Arrays.asList(piece1, piece2),
71          ifBlock);
72    }
73  
74    @Test
75    public void testIfNestedInRepeat() throws Exception {
76      Piece piece1 = new Piece();
77      Piece piece2 = new Piece();
78      Block ifBlock = new IfBlock(
79        TRUE_SUPPLIER,
80        new SequenceBlock(piece1, piece2)
81      );
82  
83      BlockTestingUtils.testNestedRepeatBlock(
84              Arrays.asList(piece1, piece2),
85              ifBlock);
86    }
87  
88  }