1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.util.function;
18
19import junit.framework.TestCase;
20
21import java.util.concurrent.atomic.AtomicBoolean;
22import java.util.function.LongPredicate;
23
24public class LongPredicateTest extends TestCase {
25
26  public void testAnd() throws Exception {
27    AtomicBoolean alwaysTrueInvoked = new AtomicBoolean(false);
28    AtomicBoolean alwaysTrue2Invoked = new AtomicBoolean(false);
29    AtomicBoolean alwaysFalseInvoked = new AtomicBoolean(false);
30    AtomicBoolean alwaysFalse2Invoked = new AtomicBoolean(false);
31    AtomicBoolean[] invocationState = {
32        alwaysTrueInvoked, alwaysTrue2Invoked, alwaysFalseInvoked, alwaysFalse2Invoked };
33
34    LongPredicate alwaysTrue = x -> { alwaysTrueInvoked.set(true); return true; };
35    LongPredicate alwaysTrue2 = x -> { alwaysTrue2Invoked.set(true); return true; };
36    LongPredicate alwaysFalse = x -> { alwaysFalseInvoked.set(true); return false; };
37    LongPredicate alwaysFalse2 = x -> { alwaysFalse2Invoked.set(true); return false; };
38
39    // true && true
40    resetToFalse(invocationState);
41    assertTrue(alwaysTrue.and(alwaysTrue2).test(1L));
42    assertTrue(alwaysTrueInvoked.get() && alwaysTrue2Invoked.get());
43
44    // true && false
45    resetToFalse(invocationState);
46    assertFalse(alwaysTrue.and(alwaysFalse).test(1L));
47    assertTrue(alwaysTrueInvoked.get() && alwaysFalseInvoked.get());
48
49    // false && false
50    resetToFalse(invocationState);
51    assertFalse(alwaysFalse.and(alwaysFalse2).test(1L));
52    assertTrue(alwaysFalseInvoked.get() && !alwaysFalse2Invoked.get());
53
54    // false && true
55    resetToFalse(invocationState);
56    assertFalse(alwaysFalse.and(alwaysTrue).test(1L));
57    assertTrue(alwaysFalseInvoked.get() && !alwaysTrueInvoked.get());
58  }
59
60  public void testAnd_null() throws Exception {
61    LongPredicate alwaysTrue = x -> true;
62    try {
63      alwaysTrue.and(null);
64      fail();
65    } catch (NullPointerException expected) {}
66  }
67
68  public void testNegate() throws Exception {
69    long arg = 5L;
70    LongPredicate alwaysTrue = x -> { assertEquals(x, arg); return true; };
71    assertFalse(alwaysTrue.negate().test(arg));
72
73    LongPredicate alwaysFalse = x -> { assertEquals(x, arg); return false; };
74    assertTrue(alwaysFalse.negate().test(arg));
75  }
76
77  public void testOr() throws Exception {
78    AtomicBoolean alwaysTrueInvoked = new AtomicBoolean(false);
79    AtomicBoolean alwaysTrue2Invoked = new AtomicBoolean(false);
80    AtomicBoolean alwaysFalseInvoked = new AtomicBoolean(false);
81    AtomicBoolean alwaysFalse2Invoked = new AtomicBoolean(false);
82    AtomicBoolean[] invocationState = {
83        alwaysTrueInvoked, alwaysTrue2Invoked, alwaysFalseInvoked, alwaysFalse2Invoked };
84
85    LongPredicate alwaysTrue = x -> { alwaysTrueInvoked.set(true); return true; };
86    LongPredicate alwaysTrue2 = x -> { alwaysTrue2Invoked.set(true); return true; };
87    LongPredicate alwaysFalse = x -> { alwaysFalseInvoked.set(true); return false; };
88    LongPredicate alwaysFalse2 = x -> { alwaysFalse2Invoked.set(true); return false; };
89
90    // true || true
91    resetToFalse(invocationState);
92    assertTrue(alwaysTrue.or(alwaysTrue2).test(1L));
93    assertTrue(alwaysTrueInvoked.get() && !alwaysTrue2Invoked.get());
94
95    // true || false
96    resetToFalse(invocationState);
97    assertTrue(alwaysTrue.or(alwaysFalse).test(1L));
98    assertTrue(alwaysTrueInvoked.get() && !alwaysFalseInvoked.get());
99
100    // false || false
101    resetToFalse(invocationState);
102    assertFalse(alwaysFalse.or(alwaysFalse2).test(1L));
103    assertTrue(alwaysFalseInvoked.get() && alwaysFalse2Invoked.get());
104
105    // false || true
106    resetToFalse(invocationState);
107    assertTrue(alwaysFalse.or(alwaysTrue).test(1L));
108    assertTrue(alwaysFalseInvoked.get() && alwaysTrueInvoked.get());
109  }
110
111  public void testOr_null() throws Exception {
112    LongPredicate alwaysTrue = x -> true;
113    try {
114      alwaysTrue.or(null);
115      fail();
116    } catch (NullPointerException expected) {}
117  }
118
119  private static void resetToFalse(AtomicBoolean... toResets) {
120    for (AtomicBoolean toReset : toResets) {
121      toReset.set(false);
122    }
123  }
124}
125