1 /*
2  * Copyright (C) 2008 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 
17 package com.android.internal.util;
18 
19 import junit.framework.TestCase;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 
24 public class PredicatesTest extends TestCase {
25 
26     private static final Predicate<Object> TRUE = new Predicate<Object>() {
27         public boolean apply(Object o) {
28             return true;
29         }
30     };
31 
32     private static final Predicate<Object> FALSE = new Predicate<Object>() {
33         public boolean apply(Object o) {
34             return false;
35         }
36     };
37 
testAndPredicate_AllConditionsTrue()38     public void testAndPredicate_AllConditionsTrue() throws Exception {
39         assertTrue(Predicates.and(newArrayList(TRUE)).apply(null));
40         assertTrue(Predicates.and(newArrayList(TRUE, TRUE)).apply(null));
41     }
42 
testAndPredicate_AtLeastOneConditionIsFalse()43     public void testAndPredicate_AtLeastOneConditionIsFalse() throws Exception {
44         assertFalse(Predicates.and(newArrayList(FALSE, TRUE, TRUE)).apply(null));
45         assertFalse(Predicates.and(newArrayList(TRUE, FALSE, TRUE)).apply(null));
46         assertFalse(Predicates.and(newArrayList(TRUE, TRUE, FALSE)).apply(null));
47     }
48 
testOrPredicate_AllConditionsTrue()49     public void testOrPredicate_AllConditionsTrue() throws Exception {
50         assertTrue(Predicates.or(newArrayList(TRUE, TRUE, TRUE)).apply(null));
51     }
52 
testOrPredicate_AllConditionsFalse()53     public void testOrPredicate_AllConditionsFalse() throws Exception {
54         assertFalse(Predicates.or(newArrayList(FALSE, FALSE, FALSE)).apply(null));
55     }
56 
testOrPredicate_AtLeastOneConditionIsTrue()57     public void testOrPredicate_AtLeastOneConditionIsTrue() throws Exception {
58         assertTrue(Predicates.or(newArrayList(TRUE, FALSE, FALSE)).apply(null));
59         assertTrue(Predicates.or(newArrayList(FALSE, TRUE, FALSE)).apply(null));
60         assertTrue(Predicates.or(newArrayList(FALSE, FALSE, TRUE)).apply(null));
61     }
62 
testNotPredicate()63     public void testNotPredicate() throws Exception {
64         assertTrue(Predicates.not(FALSE).apply(null));
65         assertFalse(Predicates.not(TRUE).apply(null));
66     }
67 
newArrayList(E... elements)68     private static <E> ArrayList<E> newArrayList(E... elements) {
69         ArrayList<E> list = new ArrayList<E>();
70         Collections.addAll(list, elements);
71         return list;
72     }
73 
74 }
75