View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.bloomfilter;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.function.BiPredicate;
26  
27  import org.apache.commons.lang3.tuple.Pair;
28  import org.junit.jupiter.api.Test;
29  
30  public class CountingPredicateTest {
31  
32      private Integer[] ary = {Integer.valueOf(1), Integer.valueOf(2)};
33  
34      private BiPredicate<Integer, Integer> makeFunc(BiPredicate<Integer, Integer> inner, List<Pair<Integer, Integer>> result) {
35          return (x, y) -> {
36              if (inner.test(x, y)) {
37                  result.add(Pair.of(x, y));
38                  return true;
39              }
40              return false;
41          };
42      }
43  
44      /**
45       * Test when the predicate array is longer than other array as determined by the number
46       * of times cp.test() is called and all other values result in a true statement.
47       */
48      @Test
49      public void testPredicateLonger() {
50          List<Pair<Integer, Integer>> expected = new ArrayList<>();
51          List<Pair<Integer, Integer>> result = new ArrayList<>();
52          expected.add(Pair.of(1, 3));
53  
54          CountingPredicate<Integer> cp = new CountingPredicate<>(ary, makeFunc((x, y) -> x!=null, result));
55          assertTrue(cp.test(Integer.valueOf(3)));
56          assertEquals(expected, result);
57          expected.add(Pair.of(2, null));
58          assertTrue(cp.forEachRemaining());
59          assertEquals(expected, result);
60  
61          // if the other array is zero length then cp.test() will not be called so
62          // we can just call cp.forEachRemaining() here.
63          expected.clear();
64          expected.add(Pair.of(1, null));
65          expected.add(Pair.of(2, null));
66          result.clear();
67          cp = new CountingPredicate<>(ary, makeFunc((x, y) -> x!=null, result));
68          assertTrue(cp.forEachRemaining());
69          assertEquals( expected, result);
70  
71          // If a test fails then the result should be false and the rest of the list should
72          // not be processed.
73          expected.clear();
74          expected.add(Pair.of(1, null));
75          result.clear();
76          cp = new CountingPredicate<>(ary,  makeFunc((x, y) -> x == Integer.valueOf(1), result));
77          assertFalse(cp.forEachRemaining());
78          assertEquals(expected, result);
79      }
80  
81      /**
82       * Test when the predicate array is shorter than other array as determined by the number
83       * of times cp.test() is called and all other values result in a true statement.
84       */
85      @Test
86      public void testPredicateSameLength() {
87          List<Pair<Integer, Integer>> expected = new ArrayList<>();
88          List<Pair<Integer, Integer>> result = new ArrayList<>();
89          expected.add( Pair.of(1, 3));
90          expected.add( Pair.of(2, 3));
91          CountingPredicate<Integer> cp = new CountingPredicate<>(ary, makeFunc((x, y) -> true, result));
92          assertTrue(cp.test(3));
93          assertTrue(cp.test(3));
94          assertEquals(expected, result);
95          assertTrue(cp.forEachRemaining());
96          assertEquals(expected, result);
97      }
98  
99      /**
100      * Test when the predicate array is shorter than other array as determined by the number
101      * of times cp.test() is called and all other values result in a true statement.
102      */
103     @Test
104     public void testPredicateShorter() {
105         List<Pair<Integer, Integer>> expected = new ArrayList<>();
106         List<Pair<Integer, Integer>> result = new ArrayList<>();
107         Integer[] shortAry = {Integer.valueOf(3)};
108         expected.add(Pair.of(3, 1));
109         expected.add(Pair.of(null, 2));
110         CountingPredicate<Integer> cp = new CountingPredicate<>(shortAry, makeFunc((x, y) -> true, result));
111         for (Integer i : ary) {
112             assertTrue(cp.test(i));
113         }
114         assertEquals(expected, result);
115         assertTrue(cp.forEachRemaining());
116         assertEquals(expected, result);
117     }
118 }