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 Blocks 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 BlocksTest { 37 38 private static void testNop() { 39 Blocks.nop().apply(null); 40 Blocks.nop().apply(BlocksTest.class); 41 pass(); 42 } 43 44 private static void testNullCheckSuccess() { 45 Blocks.requireNonNull().apply(BlocksTest.class); 46 pass(); 47 } 48 49 private static void testNullCheckFailure() { 50 Blocks.requireNonNull().apply(null); 51 pass(); 52 } 53 54 private static void testChain2() { 55 final boolean results[] = new boolean[2]; 56 57 Block<Boolean> block1 = #{ Boolean b -> results[0] = b;}; 58 Block<Boolean> block2 = #{ Boolean b -> results[1] = b;}; 59 60 Blocks.chain(block1, block2).apply(true); 61 check(results[0] && results[1]); 62 } 63 64 private static void testChainVarArgs() { 65 final boolean results[] = new boolean[3]; 66 67 Block<Boolean> blocks[] = (Block<Boolean>[]) new Block[]{ 68 (Block<Boolean>) #{ Boolean b -> results[0] = b;}, 69 (Block<Boolean>) #{ Boolean b -> results[1] = b;}, 70 (Block<Boolean>) #{Boolean b -> results[2] = b;} 71 }; 72 73 Blocks.chain( blocks ).apply(true); 74 check(results[0] && results[1] && results[2]); 75 } 76 77 private static void testChain2VarArgs() { 78 final boolean results[] = new boolean[3]; 79 80 Block<Boolean> block0 = #{ Boolean b -> results[0] = b;}; 81 Block<Boolean> block1 = #{ Boolean b -> results[1] = b;}; 82 Block<Boolean> block2 = #{ Boolean b -> results[2] = b;}; 83 84 Block<Boolean> blocks[] = (Block<Boolean>[]) new Block[]{ 85 block1, 86 block2}; 87 88 Blocks.chain(block0, blocks).apply(true); 89 90 check(results[0] && results[1] && results[2]); 91 } 92 93 private static void testChainIterable() { 94 final boolean results[] = new boolean[3]; 95 final Block<Boolean> blocks[] = (Block<Boolean>[]) new Block[] { 96 (Block<Boolean>) #{ Boolean b -> results[0] = b;}, 97 (Block<Boolean>) #{ Boolean b -> results[1] = b;}, 98 (Block<Boolean>) #{ Boolean b -> results[2] = b;}, 99 }; 100 101 Blocks.chain(Arrays.asList(blocks)).apply(true); 102 check(results[0] && results[1] && results[2]); 103 } 104 105 private static void testChainIterable2() { 106 final boolean results[] = new boolean[3]; 107 Block<Boolean> blocks[] = new Block[]{ 108 (Block<Boolean>) #{ Boolean b -> results[1] = b;}, 109 (Block<Boolean>) #{ Boolean b -> results[2] = b;} 110 }; 111 112 List<Block<Boolean>> listed = Arrays.asList(blocks); 113 114 Blocks.chain( 115 (Block<Boolean>) #{ Boolean b -> results[0] = b;}, 116 listed).apply(true); 117 check(results[0] && results[1] && results[2]); 118 } 119 120 private static void testRepeatTimes() { 121 final int results[] = new int[] { 0 }; 122 123 Blocks.repeat( (Block<Integer>) #{ Integer x -> results[0] += x;}, 5).apply(1); 124 check(results[0] == 5); 125 } 126 127 private static void testRepeatPredicate() { 128 final int results[] = new int[] { 0 }; 129 130 Blocks.repeatWhile( (Block<Integer>) #{ Integer x -> results[0] += x;}, 131 (Predicate<Integer>) #{ Integer x -> results[0] < 5 }).apply(1); 132 check(results[0] == 5); 133 134 results[0] = 0; 135 Blocks.repeatUntil( (Block<Integer>) #{ Integer x -> results[0] += x;}, 136 (Predicate<Integer>) #{ Integer x -> results[0] < 5 }).apply(1); 137 check(results[0] == 5); 138 } 139 140 public static void RealMain(String ... args) { 141 testNop(); 142 testNullCheckSuccess(); 143 THROWS( NullPointerException.class, 144 #{ testNullCheckFailure(); fail();} ); 145 testChain2(); 146 testChainVarArgs(); 147 testChain2VarArgs(); 148 testChainIterable(); 149 testChainIterable2(); 150 testRepeatTimes(); 151 testRepeatPredicate(); 152 } 153 154 //--------------------- Infrastructure --------------------------- 155 static volatile int passed = 0, failed = 0; 156 static void pass() { passed++; } 157 static void fail() { failed++; (new Exception("stack dump")).printStackTrace(System.err);} 158 static void fail(String msg) { System.out.println(msg); fail(); } 159 static void unexpected(Throwable t) { failed++; t.printStackTrace(System.err); } 160 static void check(boolean cond) { if (cond) pass(); else fail(); } 161 static void equal(Object x, Object y) { 162 if (Objects.equals(x,y)) pass(); 163 else {System.out.println(x + " not equal to " + y); fail();}} 164 static void equal2(Object x, Object y) {equal(x, y); equal(y, x);} 165 public static void main(String[] args) throws Throwable { 166 try { RealMain(args); } catch (Throwable t) { unexpected(t); } 167 168 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 169 if (failed > 0) throw new Exception("Some tests failed"); 170 } 171 private interface Fun {void f() throws Throwable;} 172 static void THROWS(Class<? extends Throwable> k, Fun... fs) { 173 for (Fun f : fs) 174 try { f.f(); fail("Expected " + k.getName() + " not thrown"); } 175 catch (Throwable t) { 176 if (k.isAssignableFrom(t.getClass())) pass(); 177 else unexpected(t);}} 178 static byte[] serializedForm(Object obj) { 179 try { 180 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 181 new ObjectOutputStream(baos).writeObject(obj); 182 return baos.toByteArray(); 183 } catch (IOException e) { throw new RuntimeException(e); }} 184 static Object readObject(byte[] bytes) 185 throws IOException, ClassNotFoundException { 186 InputStream is = new ByteArrayInputStream(bytes); 187 return new ObjectInputStream(is).readObject();} 188 @SuppressWarnings("unchecked") 189 static <T> T serialClone(T obj) { 190 try { return (T) readObject(serializedForm(obj)); } 191 catch (Exception e) { throw new RuntimeException(e); }} 192 }