1 /* 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @summary Basic test for Predicates SAM utils 27 * @author Mike Duigou 28 */ 29 30 import java.io.*; 31 import java.util.Arrays; 32 import java.util.List; 33 import java.util.Objects; 34 import java.util.functions.*; 35 36 public class PredicatesTest { 37 38 private static void testIsNull() { 39 check(Predicates.isNull().eval(null)); 40 check(!Predicates.isNull().eval(1)); 41 check(!Predicates.isNull().eval("true")); 42 check(!Predicates.isNull().eval(PredicatesTest.class)); 43 } 44 45 private static void testIsNonNull() { 46 check(!Predicates.nonNull().eval(null)); 47 check(Predicates.nonNull().eval(1)); 48 check(Predicates.nonNull().eval("true")); 49 check(Predicates.nonNull().eval(PredicatesTest.class)); 50 } 51 52 private static void testAlwaysFalse() { 53 check(!Predicates.alwaysFalse().eval(null)); 54 check(!Predicates.alwaysFalse().eval(1)); 55 check(!Predicates.alwaysFalse().eval("true")); 56 check(!Predicates.alwaysFalse().eval(PredicatesTest.class)); 57 } 58 59 private static void testAlwaysTrue() { 60 check(Predicates.alwaysTrue().eval(null)); 61 check(Predicates.alwaysTrue().eval(1)); 62 check(Predicates.alwaysTrue().eval("true")); 63 check(Predicates.alwaysTrue().eval(PredicatesTest.class)); 64 } 65 66 private static void testNegate() { 67 Predicate<Integer> predicate = #{Integer x -> x != 0 }; 68 69 check(predicate.eval(0) != Predicates.negate(predicate).eval(0)); 70 check(predicate.eval(1) != Predicates.negate(predicate).eval(1)); 71 } 72 73 private static void testAnd() { 74 Predicate<Integer> first = #{Integer x -> x != 0 }; 75 Predicate<Integer> second = #{Integer x -> x == 0 }; 76 77 check(!Predicates.and(first,first).eval(0)); 78 check(Predicates.and(first,first).eval(1)); 79 check(!Predicates.and(first,second).eval(0)); 80 check(!Predicates.and(first,second).eval(1)); 81 } 82 83 private static void testAndVarArgs() { 84 Predicate<Integer> first = #{Integer x -> x != 0 }; 85 Predicate<Integer> second = #{Integer x -> x == 0 }; 86 87 Predicate<Integer> predicates1[] = (Predicate<Integer>[]) new Predicate[] { 88 first, first 89 }; 90 91 Predicate<Integer> predicates2[] = (Predicate<Integer>[]) new Predicate[] { 92 first, second 93 }; 94 95 THROWS(NullPointerException.class, #{ Predicates.and(new Predicate[] {null});}); 96 check(!Predicates.and(predicates1).eval(0)); 97 check(Predicates.and(predicates1).eval(1)); 98 check(!Predicates.and(predicates2).eval(0)); 99 check(!Predicates.and(predicates2).eval(1)); 100 } 101 102 private static void testOr() { 103 Predicate<Integer> first = #{Integer x -> x != 0 }; 104 Predicate<Integer> second = #{Integer x -> x == 0 }; 105 106 check(!Predicates.or(first,first).eval(0)); 107 check(Predicates.or(first,first).eval(1)); 108 check(Predicates.or(first,second).eval(0)); 109 check(Predicates.or(first,second).eval(1)); 110 } 111 112 113 private static void testOrVarArgs() { 114 Predicate<Integer> first = #{Integer x -> x != 0 }; 115 Predicate<Integer> second = #{Integer x -> x == 0 }; 116 117 Predicate<Integer> predicates1[] = (Predicate<Integer>[]) new Predicate[] { 118 first, first 119 }; 120 121 Predicate<Integer> predicates2[] = (Predicate<Integer>[]) new Predicate[] { 122 first, second 123 }; 124 125 THROWS(NullPointerException.class, #{ Predicates.or(new Predicate[] {null});}); 126 check(!Predicates.or(predicates1).eval(0)); 127 check(Predicates.or(predicates1).eval(1)); 128 check(Predicates.or(predicates2).eval(0)); 129 check(Predicates.or(predicates2).eval(1)); 130 } 131 132 private static void testXor() { 133 Predicate<Integer> first = #{Integer x -> x != 0 }; 134 Predicate<Integer> second = #{Integer x -> x == 0 }; 135 136 check(!Predicates.xor(first,first).eval(0)); 137 check(!Predicates.xor(first,first).eval(1)); 138 check(Predicates.xor(first,second).eval(0)); 139 check(Predicates.xor(first,second).eval(1)); 140 } 141 142 private static void testXorVarArgs() { 143 Predicate<Integer> first = #{Integer x -> x != 0 }; 144 Predicate<Integer> second = #{Integer x -> x == 0 }; 145 146 Predicate<Integer> predicates1[] = (Predicate<Integer>[]) new Predicate[] { 147 first, first 148 }; 149 150 Predicate<Integer> predicates2[] = (Predicate<Integer>[]) new Predicate[] { 151 first, second 152 }; 153 154 THROWS(NullPointerException.class, #{ Predicates.xor(new Predicate[] {null});}); 155 check(!Predicates.xor(predicates1).eval(0)); 156 check(!Predicates.xor(predicates1).eval(1)); 157 check(Predicates.xor(predicates2).eval(0)); 158 check(Predicates.xor(predicates2).eval(1)); 159 } 160 161 private static void RealMain(String ... args) { 162 testIsNonNull(); 163 testIsNull(); 164 testAlwaysFalse(); 165 testAlwaysTrue(); 166 testNegate(); 167 testAnd(); 168 testAndVarArgs(); 169 testOr(); 170 testOrVarArgs(); 171 testXor(); 172 } 173 174 //--------------------- Infrastructure --------------------------- 175 static volatile int passed = 0, failed = 0; 176 static void pass() { passed++; } 177 static void fail() { failed++; (new Exception("stack dump")).printStackTrace(System.err);} 178 static void fail(String msg) { System.out.println(msg); fail(); } 179 static void unexpected(Throwable t) { failed++; t.printStackTrace(System.err); } 180 static void check(boolean cond) { if (cond) pass(); else fail(); } 181 static void equal(Object x, Object y) { 182 if (Objects.equals(x,y)) pass(); 183 else {System.out.println(x + " not equal to " + y); fail();}} 184 static void equal2(Object x, Object y) {equal(x, y); equal(y, x);} 185 public static void main(String[] args) throws Throwable { 186 try { RealMain(args); } catch (Throwable t) { unexpected(t); } 187 188 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 189 if (failed > 0) throw new Exception("Some tests failed"); 190 } 191 private interface Fun {void f() throws Throwable;} 192 static void THROWS(Class<? extends Throwable> k, Fun... fs) { 193 for (Fun f : fs) 194 try { f.f(); fail("Expected " + k.getName() + " not thrown"); } 195 catch (Throwable t) { 196 if (k.isAssignableFrom(t.getClass())) pass(); 197 else unexpected(t);}} 198 static byte[] serializedForm(Object obj) { 199 try { 200 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 201 new ObjectOutputStream(baos).writeObject(obj); 202 return baos.toByteArray(); 203 } catch (IOException e) { throw new RuntimeException(e); }} 204 static Object readObject(byte[] bytes) 205 throws IOException, ClassNotFoundException { 206 InputStream is = new ByteArrayInputStream(bytes); 207 return new ObjectInputStream(is).readObject();} 208 @SuppressWarnings("unchecked") 209 static <T> T serialClone(T obj) { 210 try { return (T) readObject(serializedForm(obj)); } 211 catch (Exception e) { throw new RuntimeException(e); }} 212 }