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;
192021import java.util.Arrays;
2223import org.apache.giraph.block_app.framework.piece.Piece;
24import org.apache.giraph.function.Supplier;
25import org.junit.Assert;
26import org.junit.Test;
2728publicclassTestIfBlock {
2930privatestaticfinal Supplier<Boolean> TRUE_SUPPLIER = new Supplier<Boolean>() {
31 @Override
32public Boolean get() {
33returntrue;
34 }
35 };
3637privatestaticfinal Supplier<Boolean> FALSE_SUPPLIER = new Supplier<Boolean>() {
38 @Override
39public Boolean get() {
40return false;
41 }
42 };
4344 @Test
45// Test short-circuiting the if -> then46publicvoid testIfBlockThen() throws Exception {
47 Piece piece1 = new Piece();
48 Piece piece2 = new Piece();
49 Block ifBlock = new IfBlock(
50 TRUE_SUPPLIER,
51new SequenceBlock(piece1, piece2)
52 );
5354 BlockTestingUtils.testIndependence(
55 Arrays.asList(piece1, piece2),
56 ifBlock);
57 Assert.assertFalse(ifBlock.getPieceCount().isKnown());
58 }
5960 @Test
61// Test short-circuiting the if -> else62publicvoid testIfBlockElse() throws Exception {
63 Piece piece1 = new Piece();
64 Piece piece2 = new Piece();
65 Block ifBlock = new IfBlock(
66 FALSE_SUPPLIER,
67new EmptyBlock(),
68new SequenceBlock(piece1, piece2)
69 );
7071 BlockTestingUtils.testIndependence(
72 Arrays.asList(piece1, piece2),
73 ifBlock);
74 Assert.assertFalse(ifBlock.getPieceCount().isKnown());
75 }
7677 @Test
78publicvoid testIfNestedInRepeat() throws Exception {
79 Piece piece1 = new Piece();
80 Piece piece2 = new Piece();
81 Block ifBlock = new IfBlock(
82 TRUE_SUPPLIER,
83new SequenceBlock(piece1, piece2)
84 );
8586 BlockTestingUtils.testNestedRepeatBlock(
87 Arrays.asList(piece1, piece2),
88 ifBlock);
89 Assert.assertFalse(ifBlock.getPieceCount().isKnown());
90 }
9192 @Test
93publicvoid 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 }