diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/LambdaTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/LambdaTest.java deleted file mode 100644 index 8bcbd1c19..000000000 --- a/tests/src/test/java/org/teavm/classlib/java/lang/LambdaTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2017 Alexey Andreev. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.teavm.classlib.java.lang; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import java.lang.reflect.Array; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.teavm.junit.TeaVMTestRunner; - -@RunWith(TeaVMTestRunner.class) -public class LambdaTest { - @Test - public void lambdaWorks() { - Integer[] src = { 1, 2, 3 }; - Integer[] array = map(src, (Integer n) -> n * 2 + src[0]); - assertArrayEquals(new Integer[] { 3, 5, 7 }, array); - } - - @Test - public void lambdaWorksWithLiteral() { - A[] src = { new A(1), new A(2), new A(3) }; - A[] array = map(src, A::inc); - assertEquals(3, array.length); - assertEquals(2, array[0].value); - assertEquals(3, array[1].value); - } - - @Test - public void lambdaWorksWithFieldLiteral() { - A[] src = { new A(1), new A(2), new A(3) }; - int[] array = mapToInt(src, a -> a.value); - assertArrayEquals(new int[] { 1, 2, 3 }, array); - } - - static T[] map(T[] array, Function f) { - @SuppressWarnings("unchecked") - T[] result = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length); - for (int i = 0; i < result.length; ++i) { - result[i] = f.apply(array[i]); - } - return result; - } - - static int[] mapToInt(T[] array, IntFunction f) { - int[] result = new int[array.length]; - for (int i = 0; i < result.length; ++i) { - result[i] = f.apply(array[i]); - } - return result; - } - - interface Function { - T apply(T value); - } - - interface IntFunction { - int apply(T value); - } - - static class A { - int value; - - A(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - A inc() { - return new A(value + 1); - } - } -} diff --git a/tests/src/test/java/org/teavm/vm/LambdaTest.java b/tests/src/test/java/org/teavm/vm/LambdaTest.java index 8a40cbc10..8f7968b8b 100644 --- a/tests/src/test/java/org/teavm/vm/LambdaTest.java +++ b/tests/src/test/java/org/teavm/vm/LambdaTest.java @@ -15,9 +15,11 @@ */ package org.teavm.vm; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.io.Serializable; +import java.lang.reflect.Array; import java.util.function.IntPredicate; import java.util.function.Supplier; import org.junit.Test; @@ -47,6 +49,29 @@ public class LambdaTest { assertEquals(".*.*.*.*.*", acceptIntPredicate(this::oddPredicate)); } + @Test + public void lambdaWorks() { + Integer[] src = { 1, 2, 3 }; + Integer[] array = map(src, (Integer n) -> n * 2 + src[0]); + assertArrayEquals(new Integer[] { 3, 5, 7 }, array); + } + + @Test + public void lambdaWorksWithLiteral() { + B[] src = { new B(1), new B(2), new B(3) }; + B[] array = map(src, B::inc); + assertEquals(3, array.length); + assertEquals(2, array[0].value); + assertEquals(3, array[1].value); + } + + @Test + public void lambdaWorksWithFieldLiteral() { + B[] src = { new B(1), new B(2), new B(3) }; + int[] array = mapToInt(src, a -> a.value); + assertArrayEquals(new int[] { 1, 2, 3 }, array); + } + private String acceptIntPredicate(IntPredicate p) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; ++i) { @@ -61,4 +86,46 @@ public class LambdaTest { interface A { } + + + static T[] map(T[] array, Function f) { + @SuppressWarnings("unchecked") + T[] result = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length); + for (int i = 0; i < result.length; ++i) { + result[i] = f.apply(array[i]); + } + return result; + } + + static int[] mapToInt(T[] array, IntFunction f) { + int[] result = new int[array.length]; + for (int i = 0; i < result.length; ++i) { + result[i] = f.apply(array[i]); + } + return result; + } + + interface Function { + T apply(T value); + } + + interface IntFunction { + int apply(T value); + } + + static class B { + int value; + + B(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + B inc() { + return new B(value + 1); + } + } }