briangoetz | ef42f9a | 2013-05-06 11:43:51 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2012, 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 | import org.testng.annotations.Test; |
| 25 | |
| 26 | import java.util.function.IntFunction; |
| 27 | import java.util.function.Supplier; |
| 28 | |
| 29 | import static org.testng.Assert.assertTrue; |
| 30 | |
| 31 | /** |
| 32 | * ArrayCtorRefTest |
| 33 | * |
| 34 | * @author Brian Goetz |
| 35 | */ |
| 36 | @Test |
| 37 | public class ArrayCtorRefTest { |
| 38 | interface ArrayMaker<T> { |
| 39 | public T[] make(int size); |
| 40 | } |
| 41 | |
| 42 | private static<T> Supplier<T[]> emptyArrayFactory(ArrayMaker<T> maker) { |
| 43 | return () -> maker.make(0); |
| 44 | } |
| 45 | |
| 46 | public void testLambda() { |
| 47 | ArrayMaker<String> am = i -> new String[i]; |
| 48 | String[] arr = am.make(3); |
| 49 | arr[0] = "Foo"; |
| 50 | assertTrue(arr instanceof String[]); |
| 51 | assertTrue(arr.length == 3); |
| 52 | } |
| 53 | |
| 54 | public void testIntCtorRef() { |
| 55 | IntFunction<int[]> factory = int[]::new; |
| 56 | int[] arr = factory.apply(6); |
| 57 | assertTrue(arr.length == 6); |
| 58 | } |
| 59 | |
| 60 | public void testLambdaInference() { |
| 61 | Supplier<Object[]> oF = emptyArrayFactory(i -> new Object[i]); |
| 62 | Supplier<String[]> sF = emptyArrayFactory(i -> new String[i]); |
| 63 | assertTrue(oF.get() instanceof Object[]); |
| 64 | assertTrue(sF.get() instanceof String[]); |
| 65 | } |
| 66 | |
| 67 | public void testCtorRef() { |
| 68 | ArrayMaker<String> am = String[]::new; |
| 69 | String[] arr = am.make(3); |
| 70 | arr[0] = "Foo"; |
| 71 | assertTrue(arr instanceof String[]); |
| 72 | assertTrue(arr.length == 3); |
| 73 | } |
| 74 | } |