Skip to content

Commit

Permalink
Add vararg handling to method references, refactored java8 jre_emul t…
Browse files Browse the repository at this point in the history
…est suite to rely on shared interfaces.

	Change on 2015/07/29 by kirbs <kirbs@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=99394700
  • Loading branch information
sjkirby authored and kstanger committed Jul 30, 2015
1 parent 7145f3b commit aa363de
Show file tree
Hide file tree
Showing 12 changed files with 533 additions and 224 deletions.
48 changes: 32 additions & 16 deletions jre_emul/Tests/com/google/j2objc/java8/CreationReferenceTest.java
Expand Up @@ -15,18 +15,6 @@

import junit.framework.TestCase;

interface FunInt<T> {
T apply(int x);
}

interface FunInt4<T> {
T apply(int x, I j, String s, Object o);
}

interface Call<T> {
T call();
}

class I {
String s = "...";
I() { }
Expand All @@ -45,6 +33,19 @@ String world() {
String getS() {
return s;
}

I(Object a1, Object... rest) {
s = a1 + p(rest);
}

static String p(Object... ls) {
String out = " [ ";
for (Object x : ls) {
out += x;
out += ' ';
}
return out + ']';
}
}

final class J {
Expand Down Expand Up @@ -75,26 +76,41 @@ int getX() {
public class CreationReferenceTest extends TestCase {
public CreationReferenceTest() {}

interface FunInt<T> {
T apply(int x);
}

interface FunInt4<T> {
T apply(int x, I j, String s, Object o);
}

public void testBasicReferences() throws Exception {
Call<I> iInit = I::new;
Lambdas.Zero<I> iInit = I::new;
FunInt<I> iInit2 = I::new;
FunInt4<I> iInit3 = I::new;
I myI = iInit.call();
I myI = iInit.apply();
I myI2 = iInit2.apply(42);
I myI3 = iInit3.apply(0, myI, "43", "");
assertEquals("World", myI.world());
assertEquals("...", myI.getS());
assertEquals("42", myI2.getS());
assertEquals("43", myI3.getS());
Call<J> jInit = J::new;
Lambdas.Zero<J> jInit = J::new;
FunInt<J> jInit2 = J::new;
FunInt4<J> jInit3 = J::new;
J myJ = jInit.call();
J myJ = jInit.apply();
J myJ2 = jInit2.apply(42);
J myJ3 = jInit3.apply(43, myI, "", "");
assertEquals("World", myJ.world());
assertEquals(41, myJ.getX());
assertEquals(42, myJ2.getX());
assertEquals(43, myJ3.getX());
}

public void testVarargs() throws Exception {
Lambdas.Three f = I::new;
Lambdas.Four<Object, Object, Object, Object, I> f2 = I::new;
assertEquals("12 [ 22 42 ]", ((I) f.apply(12, 22, "42")).getS());
assertEquals("10 [ 20 20 10 ]", f2.apply(10, 20, "20", "10").s);
}
}
Expand Up @@ -11,6 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.j2objc.java8;

import junit.framework.TestCase;
Expand All @@ -25,15 +26,12 @@ static Object o(String x) {
return "Foo";
}
}

String o(String x) {
return "Baz";
}
}

interface Funct<T, R> {
R f(T t);
}

class Q {
static Object o(Object x) {
return x;
Expand All @@ -44,22 +42,55 @@ Object o2(Object x) {
}
}

class Y {
static String m(Number a1, Object... rest) {
return a1 + p(rest);
}

static String p(Object... ls) {
String out = " [ ";
for (Object x : ls) {
out += x;
out += ' ';
}
return out + ']';
}
}

/**
* Command-line tests for expression method references.
*
* @author Seth Kirby
*/
public class ExpressionMethodReferenceTest extends TestCase {
public ExpressionMethodReferenceTest() { }
public ExpressionMethodReferenceTest() {
}

interface I {
String foo(Integer a1, Integer a2, String a3);
}

interface J {
public String m(Integer a1, Integer a2, String a3, String a4);
}

public void testBasicReferences() throws Exception {
Funct f = Z.ZZ::o;
Funct<String, Object> f2 = Z.ZZ::o;
Funct<String, String> f3 = new Z()::o;
assertEquals("Bar", f.f(""));
assertEquals("Foo", f2.f(""));
assertEquals("Baz", f3.f(""));
Funct ff = Q::o;
Funct ff1 = new Q()::o2;
Lambdas.One f = Z.ZZ::o;
Lambdas.One<String, Object> f2 = Z.ZZ::o;
Lambdas.One<String, String> f3 = new Z()::o;
assertEquals("Bar", f.apply(""));
assertEquals("Foo", f2.apply(""));
assertEquals("Baz", f3.apply(""));
Lambdas.One f4 = Q::o;
Lambdas.One f5 = new Q()::o2;
assertEquals("Foo", f4.apply("Foo"));
assertEquals("Bar", f5.apply("Bar"));
}

public void testVarArgs() throws Exception {
I i = Y::m;
J j = Y::m;
assertEquals("12 [ 22 42 ]", i.foo(12, 22, "42"));
assertEquals("10 [ 20 20 10 ]", j.m(10, 20, "20", "10"));
}
}
106 changes: 43 additions & 63 deletions jre_emul/Tests/com/google/j2objc/java8/LambdaTest.java
Expand Up @@ -18,26 +18,7 @@

import java.util.ArrayList;
import java.util.List;

interface Function<F, T> {
T apply(F input);
}

interface Callable<T> {
T call();
}

interface Supplier<T> {
T get();
}

interface Consumer<R> {
void accept(R input);
}

interface FourToOne<F, G, H, I, R> {
R apply(F f, G g, H h, I i);
}
import java.util.concurrent.Callable;

interface UnaryOperator<T> {
public T apply(T t);
Expand All @@ -57,9 +38,9 @@ public class LambdaTest extends TestCase {
public LambdaTest() {}

public void testBasicReflection() {
assertEquals(false, ((Function) (x) -> 1).equals((Function) (x) -> 1));
Function f = x -> 1;
Function g = f;
assertEquals(false, ((Lambdas.One) (x) -> 1).equals((Lambdas.One) (x) -> 1));
Lambdas.One f = x -> 1;
Lambdas.One g = f;
assertEquals(f, g);
assertEquals(f.toString(), "" + g);
String lambdaClassName = f.getClass().getName();
Expand All @@ -85,8 +66,8 @@ String findMe() {
return "Bar";
}

Supplier fooS = () -> outer();
Supplier barS = () -> findMe();
Lambdas.Zero fooS = () -> outer();
Lambdas.Zero barS = () -> findMe();
}

Bar getBar() {
Expand All @@ -95,39 +76,35 @@ Bar getBar() {
}

public void testOuterMethodCalls() {
Supplier s = () -> outerCall();
assertEquals("Foo", s.get());
Supplier s2 = () -> privateOuterCall();
assertEquals("Bar", s2.get());
assertEquals("Foo", Lambdas.get(() -> outerCall()).apply());
assertEquals("Bar", Lambdas.get(() -> privateOuterCall()).apply());
Foo foo = new Foo();
Supplier s3 = () -> foo.outer();
assertEquals("Foo", s3.get());
Supplier s4 = () -> foo.getBar().barS.get();
assertEquals("Bar", s4.get());
assertEquals("Foo", Lambdas.get(() -> foo.outer()).apply());
assertEquals("Bar", Lambdas.get(() -> foo.getBar().barS.apply()).apply());
}

Integer outerX = 0;
int outerY = 0;

public void testOuterVarCapture() {
Supplier<Integer> s = () -> outerX;
Supplier<Integer> s2 = () -> outerY;
assertEquals((Integer) 0, s.get());
assertEquals((Integer) 0, s2.get());
Lambdas.Zero s = () -> outerX;
Lambdas.Zero s2 = () -> outerY;
assertEquals((Integer) 0, s.apply());
assertEquals((Integer) 0, s2.apply());
outerX += 42;
outerY += 42;
assertEquals((Integer) 42, s.get());
assertEquals((Integer) 42, s2.get());
assertEquals((Integer) 42, s.apply());
assertEquals((Integer) 42, s2.apply());
outerX++;
outerY++;
assertEquals((Integer) 43, s.get());
assertEquals((Integer) 43, s2.get());
assertEquals((Integer) 43, s.apply());
assertEquals((Integer) 43, s2.apply());
outerX++;
outerY++;
}

public void testFunctionArray() throws Exception {
List<Function> fs = new ArrayList<Function>();
List<Lambdas.One> fs = new ArrayList<>();
int[] nums = { 3, 2, 1, 0 };
for (int i : nums) {
fs.add((x) -> i);
Expand Down Expand Up @@ -167,55 +144,57 @@ public void testYCombinator() throws Exception {
assertEquals((Integer) 55, yComb(fibonacci).apply(10));
}

Function outerF = (x) -> x;
Lambdas.One outerF = (x) -> x;

public void testOuterFunctions() throws Exception {
assertEquals(42, outerF.apply(42));
}

static Function staticF = (x) -> x;
static Lambdas.One staticF = (x) -> x;

public void testStaticFunctions() throws Exception {
assertEquals(42, staticF.apply(42));
}

public void testNestedLambdas() throws Exception {
Function<String, Function<String, String>> f = (x) -> (y) -> x;
Lambdas.One<String, Lambdas.One<String, String>> f = (x) -> (y) -> x;
assertEquals(f.apply("Foo").apply("Bar"), "Foo");
Function<Integer, Function<Integer, Integer>> f2 = (x) -> (y) -> x;
Lambdas.One<Integer, Lambdas.One<Integer, Integer>> f2 = (x) -> (y) -> x;
assertEquals(f2.apply(42).apply(43), Integer.valueOf(42));
Function<Integer, Function<Integer, Integer>> f3 = (y) -> (x) -> x;
Lambdas.One<Integer, Lambdas.One<Integer, Integer>> f3 = (y) -> (x) -> x;
assertEquals(f3.apply(43).apply(42), Integer.valueOf(42));
Function<String, Function<String, Integer>> f4 = (x) -> (y) -> Integer.parseInt(x);
Lambdas.One<String, Lambdas.One<String, Integer>> f4 = (x) -> (y) -> Integer.parseInt(
x);
assertEquals(f4.apply("42").apply("43"), Integer.valueOf(42));
Function<String, Function<Integer, Integer>> f5 = (x) -> (y) -> Integer.parseInt(x);
Lambdas.One<String, Lambdas.One<Integer, Integer>> f5 = (x) -> (y) -> Integer.parseInt(
x);
assertEquals(f5.apply("42").apply(43), Integer.valueOf(42));
Callable<Callable> c2 = () -> () -> 42;
assertEquals(c2.call().call(), 42);
Supplier<Supplier> s2 = () -> () -> 42;
assertEquals(s2.get().get(), 42);
Lambdas.Zero<Lambdas.Zero> s2 = () -> () -> 42;
assertEquals(s2.apply().apply(), 42);
}

// Tests outer reference resolution, and that inner fields are being correctly resolved for
// lambdas with implicit blocks.
public void testAdditionInLambda() throws Exception {
Function f = new Function<Integer, Integer>() {
Lambdas.One f = new Lambdas.One<Integer, Integer>() {
@Override
public Integer apply(Integer x) {
return x + 20;
}
};
assertEquals(42, f.apply(22));
Function<Integer, Integer> g = x -> {
Lambdas.One<Integer, Integer> g = x -> {
return x + 20;
};
assertEquals((Integer) 42, g.apply(22));
Function<Integer, Integer> h = x -> x + 20;
Lambdas.One<Integer, Integer> h = x -> x + 20;
assertEquals((Integer) 42, h.apply(22));
}

public void testBasicAnonymousClass() throws Exception {
Function h = new Function() {
Lambdas.One h = new Lambdas.One() {
@Override
public Object apply(Object x) {
return x;
Expand All @@ -225,28 +204,29 @@ public Object apply(Object x) {
}

public void testBasicFunction() throws Exception {
Function f = x -> x;
Lambdas.One f = x -> x;
assertEquals(42, f.apply(42));
Function<Integer, Integer> f2 = x -> x;
Lambdas.One<Integer, Integer> f2 = x -> x;
assertEquals((Integer) 42, f2.apply(42));
Function f3 = x -> {
Lambdas.One f3 = x -> {
int y = 42;
return y;
};
assertEquals(42, f3.apply(null));
FourToOne<String, Double, Integer, Boolean, String> appendFour = (a, b, c, d) -> a + b + c + d;
Lambdas.Four<String, Double, Integer, Boolean, String> appendFour = (a, b, c, d) -> a + b + c
+ d;
assertEquals("Foo4.214true", appendFour.apply("Foo", 4.2, 14, true));
}

public void testBasicSupplier() throws Exception {
Supplier s = () -> 42;
assertEquals(42, s.get());
Lambdas.Zero s = () -> 42;
assertEquals(42, s.apply());
}

public void testBasicConsumer() throws Exception {
Object[] ls = new Object[1];
Consumer c = (x) -> ls[0] = x;
c.accept(42);
Lambdas.VoidOne c = (x) -> ls[0] = x;
c.apply(42);
assertEquals(42, ls[0]);
}

Expand Down

0 comments on commit aa363de

Please sign in to comment.