This project has retired. For details please refer to its Attic page.
TestIfBlock 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  
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.Assert;
26  import org.junit.Test;
27  
28  public class TestIfBlock {
29  
30    private static final Supplier<Boolean> TRUE_SUPPLIER = new Supplier<Boolean>() {
31      @Override
32      public Boolean get() {
33        return true;
34      }
35    };
36  
37    private static final Supplier<Boolean> FALSE_SUPPLIER = new Supplier<Boolean>() {
38      @Override
39      public Boolean get() {
40        return false;
41      }
42    };
43  
44    @Test
45    // Test short-circuiting the if -> then
46    public void testIfBlockThen() throws Exception {
47      Piece piece1 = new Piece();
48      Piece piece2 = new Piece();
49      Block ifBlock = new IfBlock(
50        TRUE_SUPPLIER,
51        new SequenceBlock(piece1, piece2)
52      );
53  
54      BlockTestingUtils.testIndependence(
55          Arrays.asList(piece1, piece2),
56          ifBlock);
57      Assert.assertFalse(ifBlock.getPieceCount().isKnown());
58    }
59  
60    @Test
61    // Test short-circuiting the if -> else
62    public void testIfBlockElse() throws Exception {
63      Piece piece1 = new Piece();
64      Piece piece2 = new Piece();
65      Block ifBlock = new IfBlock(
66        FALSE_SUPPLIER,
67        new EmptyBlock(),
68        new SequenceBlock(piece1, piece2)
69      );
70  
71      BlockTestingUtils.testIndependence(
72          Arrays.asList(piece1, piece2),
73          ifBlock);
74      Assert.assertFalse(ifBlock.getPieceCount().isKnown());
75    }
76  
77    @Test
78    public void testIfNestedInRepeat() throws Exception {
79      Piece piece1 = new Piece();
80      Piece piece2 = new Piece();
81      Block ifBlock = new IfBlock(
82        TRUE_SUPPLIER,
83        new SequenceBlock(piece1, piece2)
84      );
85  
86      BlockTestingUtils.testNestedRepeatBlock(
87              Arrays.asList(piece1, piece2),
88              ifBlock);
89      Assert.assertFalse(ifBlock.getPieceCount().isKnown());
90    }
91  
92    @Test
93    public void testIfThenElsePieceCount() {
94      Piece piece1 = new Piece();
95      Piece piece2 = new Piece();
96      Block ifBlock = new IfBlock(
97          TRUE_SUPPLIER,
98          piece1,
99          piece2
100     );
101     Assert.assertTrue(ifBlock.getPieceCount().isKnown());
102     Assert.assertEquals(1, ifBlock.getPieceCount().getCount());
103   }
104 }