/* * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @summary Basic test for Reducers SAM utils * @author Mike Duigou */ import java.io.*; import java.util.Objects; import java.util.functions.*; public class ReducersTest { private static void testNop() { String base = "base value"; String result = Reducers.nop().reduce(base,Integer.valueOf(1)); check(base == result); } private static void testMin() { check(0 == Reducers.min().reduce(0,Reducers.min().reduce(1,Reducers.min().reduce(Integer.valueOf(2),Integer.valueOf(3))))); } private static void testMax() { check(3 == Reducers.max().reduce(0,Reducers.max().reduce(1,Reducers.max().reduce(Integer.valueOf(2),Integer.valueOf(3))))); } private static void testComposeMapper() { Mapper truthMapper = #{ Integer x -> x != 0 ? "true" : "false" }; Reducer truth = #{ String b, String s -> b + " " + s}; check("base false".equals(Reducers.compose(truth,truthMapper).reduce("base",Integer.valueOf(0)))); check("base true".equals(Reducers.compose(truth,truthMapper).reduce("base",Integer.valueOf(1)))); } public static void testComposePredicate() { Predicate truthteller = #{ String s -> "true".equals(s) }; Reducer truth = #{ String b, String s -> if("true".equals(s)) return "truth"; else throw new IllegalArgumentException("only compatible with the truth");}; check("base".equals(Reducers.compose(truth,truthteller).reduce("base","false"))); check("truth".equals(Reducers.compose(truth,truthteller).reduce("base","true"))); } public static void RealMain(String ... args) { testNop(); testMin(); testMax(); testComposeMapper(); testComposePredicate(); } //--------------------- Infrastructure --------------------------- static volatile int passed = 0, failed = 0; static void pass() { passed++; } static void fail() { failed++; (new Exception("stack dump")).printStackTrace(System.err);} static void fail(String msg) { System.out.println(msg); fail(); } static void unexpected(Throwable t) { failed++; t.printStackTrace(System.err); } static void check(boolean cond) { if (cond) pass(); else fail(); } static void equal(Object x, Object y) { if (Objects.equals(x,y)) pass(); else {System.out.println(x + " not equal to " + y); fail();}} static void equal2(Object x, Object y) {equal(x, y); equal(y, x);} public static void main(String[] args) throws Throwable { try { RealMain(args); } catch (Throwable t) { unexpected(t); } System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); if (failed > 0) throw new Exception("Some tests failed"); } private interface Fun {void f() throws Throwable;} static void THROWS(Class k, Fun... fs) { for (Fun f : fs) try { f.f(); fail("Expected " + k.getName() + " not thrown"); } catch (Throwable t) { if (k.isAssignableFrom(t.getClass())) pass(); else unexpected(t);}} static byte[] serializedForm(Object obj) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); new ObjectOutputStream(baos).writeObject(obj); return baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); }} static Object readObject(byte[] bytes) throws IOException, ClassNotFoundException { InputStream is = new ByteArrayInputStream(bytes); return new ObjectInputStream(is).readObject();} @SuppressWarnings("unchecked") static T serialClone(T obj) { try { return (T) readObject(serializedForm(obj)); } catch (Exception e) { throw new RuntimeException(e); }} }