rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/LambdasTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 14 Sep 2014 19:27:44 +0200
changeset 1692 2f800fdc371e
parent 1680 3b553acbd931
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Adding necessary fake classes to allow Javac to compile lamda expressions against our emulation library.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.vm8;
    19 
    20 import java.util.ArrayList;
    21 import java.util.Arrays;
    22 import java.util.List;
    23 import java.util.concurrent.Callable;
    24 import org.apidesign.bck2brwsr.vmtest.Compare;
    25 import org.apidesign.bck2brwsr.vmtest.VMTest;
    26 import org.testng.annotations.Factory;
    27 
    28 /**
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 public class LambdasTest extends LambdasSuper {
    33     @Compare public String StringverifyJSTime() throws Exception {
    34         return Lambdas.compound();
    35     }
    36 
    37     @Compare
    38     public int canCallLambda() {
    39         int[] arr = {0};
    40         Runnable incr = () -> {
    41             arr[0]++;
    42         };
    43         incr.run();
    44         return arr[0];
    45     }
    46 
    47     @Compare
    48     public String lambdaReturnsString() throws Exception {
    49         Callable<String> lambda = () -> "Hello World!";
    50         return lambda.call();
    51     }
    52 
    53     private interface Convertor<P, R> {
    54 
    55         public R convert(P value);
    56     }
    57 
    58     @Compare
    59     public int convertToLength() {
    60         Convertor<String, Integer> lambda = (String s) -> s.getBytes().length;
    61         return lambda.convert("buu");
    62     }
    63 
    64     private int meaningOfWorld = 0;
    65 
    66     @Compare
    67     public int accessToInstanceVar() {
    68         Runnable lambda = () -> {
    69             meaningOfWorld = 42;
    70         };
    71         lambda.run();
    72         return meaningOfWorld;
    73     }
    74 
    75     @Compare
    76     public int localMeaningOfWorld() {
    77         int[] meansOfWorld = new int[1];
    78         Runnable lambda = () -> {
    79             meansOfWorld[0] = 42;
    80         };
    81         lambda.run();
    82         return meansOfWorld[0];
    83     }
    84 
    85     @Compare
    86     public int useLocalVars() throws Exception {
    87         boolean bool = true;
    88         byte b = 2;
    89         short s = 3;
    90         int i = 4;
    91         long l = 5;
    92         float f = 6;
    93         double d = 7;
    94         char c = 8;
    95         Callable<Integer> lambda = () -> (int) ((bool ? 1 : 0) + b + s + i + l + f + d + c);
    96         return lambda.call();
    97     }
    98 
    99     @Compare
   100     public String callVirtualMethod() throws Exception {
   101         String foo = "foo";
   102         Callable<String> ref = foo::toUpperCase;
   103         return ref.call();
   104     }
   105 
   106     @Compare
   107     public int callInterfaceMethod() throws Exception {
   108         List<String> foos = Arrays.asList("foo");
   109         Callable<Integer> ref = foos::size;
   110         return ref.call();
   111     }
   112 
   113     @Compare
   114     public long callStaticMethod() throws Exception {
   115         long expected = System.currentTimeMillis();
   116         Callable<Long> ref = System::currentTimeMillis;
   117         return ref.call() & ~0xffff;
   118     }
   119 
   120     @Compare
   121     public String callConstructor() throws Exception {
   122         Callable<List<String>> ref = ArrayList<String>::new;
   123         return ref.call().toString();
   124     }
   125 
   126     @Compare
   127     public String superMethodOverridenByThis() throws Exception {
   128         Callable<String> ref = super::inheritedMethod;
   129         return ref.call();
   130     }
   131 
   132     @Override
   133     String inheritedMethod() {
   134         return "overridden version";
   135     }
   136 
   137     @Compare
   138     public String referencePrivateClassMethod() throws Exception {
   139         StringBuilder sb = new StringBuilder();
   140 
   141         Callable<String> ref1 = LambdasTest::privateClassMethod;
   142         sb.append(ref1.call());
   143 
   144         Callable<String> ref2 = this::privateInstanceMethod;
   145         sb.append("\n").append(ref2.call());
   146 
   147         // Normal method calls should still work after our magic
   148         // of making them them accessible from the lambda classes.
   149         sb.append("\n").append(privateClassMethod());
   150         sb.append("\n").append(privateInstanceMethod());
   151 
   152         return sb.toString();
   153     }
   154 
   155     private String privateInstanceMethod() {
   156         return "foo";
   157     }
   158 
   159     private static String privateClassMethod() {
   160         return "foo";
   161     }
   162 
   163     private String unrelatedPrivateMethod() {
   164         return "foo";
   165     }
   166     
   167     @Factory public static Object[] create() {
   168         return VMTest.create(LambdasTest.class);
   169     }
   170 }
   171