rt/vm/src/test/java/org/apidesign/vm4brwsr/LibUse.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 10 Jun 2016 08:24:36 +0200
branchLibraries
changeset 1974 a5fc49f9ef40
parent 1972 a83ae75ebf89
permissions -rw-r--r--
Including failing arrayForEach test
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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.vm4brwsr;
    19 
    20 import net.java.html.js.JavaScriptBody;
    21 import net.java.html.lib.Date;
    22 import static net.java.html.lib.Exports.NaN;
    23 import static net.java.html.lib.Exports.eval;
    24 import static net.java.html.lib.Exports.isNaN;
    25 import static net.java.html.lib.Exports.parseInt;
    26 import net.java.html.lib.Objs;
    27 
    28 public class LibUse {
    29     public static int fourtyTwo() {
    30         Number n = (Number) eval("6 * 7");
    31         return n.intValue();
    32     }
    33 
    34     public static String hiProperty(String set) {
    35         final Object obj = eval("var x = {}; x.x = 'Hi'; x");
    36         if (obj == null) {
    37             throw new IllegalStateException("Some Value returned " + obj);
    38         }
    39         if (!(obj instanceof Objs)) {
    40             throw new IllegalStateException("The result is Objs: " + obj.getClass());
    41         }
    42         Objs js = (Objs) obj;
    43         if (set != null) {
    44             js.$set("x", set);
    45         }
    46         return (String) js.$get("x");
    47     }
    48 
    49     public static double parse(String str) throws Exception {
    50         return parseInt(str);
    51     }
    52 
    53     public static double parse(String str, int radix) throws Exception {
    54         return parseInt(str, radix);
    55     }
    56 
    57     public boolean checkNaN() throws Exception {
    58         double nan = NaN;
    59         return isNaN(nan);
    60     }
    61 
    62     public double dateOperation() throws Exception {
    63         Date eleven = new Date(2016, 1, 10);
    64         return eleven.getFullYear() + eleven.getMonth();
    65     }
    66 
    67     public static double arrayForEach() throws Exception {
    68         net.java.html.lib.Array<java.lang.Number> arr = new net.java.html.lib.Array<>();
    69         assertNumber(arr.length.get(), 0.0, "Empty at first");
    70         arr.push(1.1, 2.2, 3);
    71         assertNumber(arr.$get(0), 1.1, "1.1 is on position 0");
    72         assertNumber(arr.$get(1), 2.2, "2.2 is on position 1");
    73         assertNumber(arr.$get(2), 3.0, "3 is on position 2");
    74         final boolean[] called = { false };
    75         final double[] sum = { 0.0 };
    76         arr.forEach(new net.java.html.lib.Function.A1<java.lang.Number,Void>() {
    77             @Override
    78             public Void call(java.lang.Number p1) {
    79                 called[0] = true;
    80                 sum[0] += p1.doubleValue();
    81                 return null;
    82             }
    83 
    84             @Override
    85             public Void call(Number p1, Object p2) {
    86                 return call(p1);
    87             }
    88 
    89             @Override
    90             public Void call(Number p1, Object p2, Object p3) {
    91                 return call(p1);
    92             }
    93 
    94             @Override
    95             public Void call(Number p1, Object p2, Object p3, Object p4) {
    96                 return call(p1);
    97             }
    98 
    99             @Override
   100             public Void call(Number p1, Object p2, Object p3, Object p4, Object p5) {
   101                 return call(p1);
   102             }
   103         });
   104         assertTrue(called[0], "forEach invoked");
   105         assertEquals(sum[0], 6.3, 0.1, "Sum computed");
   106 
   107         return sum[0];
   108     }
   109 
   110     private static void assertEquals(double real, double expected, double delta, String msg) {
   111         if (expected - delta < real && real < expected + delta) {
   112             return;
   113         }
   114         raise(msg + " exp: " + expected + " but was: " + real);
   115     }
   116 
   117     @JavaScriptBody(args = { "msg" }, body = "throw msg;")
   118     private static native void raise(String msg);
   119 
   120     private static void assertNumber(Object value, double expected, String msg) {
   121         if (value instanceof Number) {
   122             assertEquals(((Number)value).doubleValue(), expected, 0.1, msg);
   123         } else {
   124             if (value == null) {
   125                 raise("null is not a number");
   126                 return;
   127             }
   128             raise("Not a number: " + value + " type: " + value.getClass());
   129         }
   130     }
   131 
   132     private static void assertTrue(boolean b, String msg) {
   133         if (!b) {
   134             raise(msg);
   135         }
   136     }
   137 
   138 }