# HG changeset patch # User Jaroslav Tulach # Date 1356440822 -3600 # Node ID 70d15cf323baf0d29cef1cb72dc4c06f0016dd1d # Parent 4ce33ad7d507ba011bf126dda2a31124b0176c8b Can execute the matrix multiplication test using VMTest diff -r 4ce33ad7d507 -r 70d15cf323ba benchmarks/matrix-multiplication/pom.xml --- a/benchmarks/matrix-multiplication/pom.xml Tue Dec 25 13:31:35 2012 +0100 +++ b/benchmarks/matrix-multiplication/pom.xml Tue Dec 25 14:07:02 2012 +0100 @@ -25,20 +25,6 @@ 1.7 - - - org.apidesign.bck2brwsr - mojo - 0.3-SNAPSHOT - - - - j2js - - - - - @@ -88,24 +74,22 @@ 0.3-SNAPSHOT - com.googlecode.jstd-maven-plugin - jstd-maven-plugin - 1.3.2.5 - test + org.testng + testng + 6.5.2 + test + + + junit + junit + + + + + org.apidesign.bck2brwsr + vmtest + 0.3-SNAPSHOT + test - - - - jstd-maven-plugin google code repo - http://jstd-maven-plugin.googlecode.com/svn/maven2 - - - - - jstd-maven-plugin google code repo - http://jstd-maven-plugin.googlecode.com/svn/maven2 - - - diff -r 4ce33ad7d507 -r 70d15cf323ba benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Main.java --- a/benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Main.java Tue Dec 25 13:31:35 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,42 +0,0 @@ -/** - * Back 2 Browser Bytecode Translator - * Copyright (C) 2012 Jaroslav Tulach - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. Look for COPYING file in the top folder. - * If not, see http://opensource.org/licenses/GPL-2.0. - */ -package org.apidesign.benchmark.matrixmul; - -public class Main { - - public static final int ITERATION_COUNT = 100000; - - public static void main(String[] args) { - Matrix m1 = new Matrix(5); - Matrix m2 = new Matrix(5); - - m1.generateData(); - m2.generateData(); - - //m1.printOn(System.out); - //System.out.println("x"); - //m2.printOn(System.out); - - for (int i = 0; i < ITERATION_COUNT; i++) { - m1.multiply(m2); - } - - //System.out.println("="); - //m1.printOn(System.out); - } -} diff -r 4ce33ad7d507 -r 70d15cf323ba benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Matrix.java --- a/benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Matrix.java Tue Dec 25 13:31:35 2012 +0100 +++ b/benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Matrix.java Tue Dec 25 14:07:02 2012 +0100 @@ -17,17 +17,20 @@ */ package org.apidesign.benchmark.matrixmul; -//import java.io.PrintStream; -//import java.util.Random; +import java.io.IOException; +import java.util.Arrays; public class Matrix { - private final int rank; - private float data[][]; + private final float data[][]; public Matrix(int r) { - rank = r; - data = new float[r][r]; + this(r, new float[r][r]); + } + + private Matrix(int r, float[][] data) { + this.rank = r; + this.data = data; } public void setElement(int i, int j, float value) { @@ -62,20 +65,44 @@ res[i][j] = ij; } } - data = res; - - return this; + return new Matrix(rank, res); } - /* - public void printOn(PrintStream s) { + public void printOn(Appendable s) throws IOException { for (int i = 0; i < rank; i++) { + String sep = ""; for (int j = 0; j < rank; j++) { - s.printf("%f ", data[i][j]); + s.append(sep + data[i][j]); + sep = " "; } - s.println(); + s.append("\n"); } } - */ - + + @Override + public boolean equals(Object obj) { + if (obj instanceof Matrix) { + Matrix snd = (Matrix)obj; + if (snd.rank != rank) { + return false; + } + for (int i = 0; i < rank; i++) { + for (int j = 0; j < rank; j++) { + if (data[i][j] != snd.data[i][j]) { + return false; + } + } + } + return true; + } + return false; + } + + @Override + public int hashCode() { + int hash = 3; + hash = 97 * hash + this.rank; + hash = 97 * hash + Arrays.deepHashCode(this.data); + return hash; + } } diff -r 4ce33ad7d507 -r 70d15cf323ba benchmarks/matrix-multiplication/src/test/java/org/apidesign/benchmark/matrixmul/MatrixTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/benchmarks/matrix-multiplication/src/test/java/org/apidesign/benchmark/matrixmul/MatrixTest.java Tue Dec 25 14:07:02 2012 +0100 @@ -0,0 +1,61 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.benchmark.matrixmul; + +import java.io.IOException; +import org.apidesign.bck2brwsr.vmtest.Compare; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +/** + * + * @author Jaroslav Tulach + */ +public class MatrixTest { + public static final int ITERATION_COUNT = 10; + + public MatrixTest() { + } + + @Compare public String tenThousandIterations() throws IOException { + + Matrix m1 = new Matrix(5); + Matrix m2 = new Matrix(5); + + m1.generateData(); + m2.generateData(); + + Matrix res = null; + for (int i = 0; i < ITERATION_COUNT; i++) { + Matrix m = m1.multiply(m2); + if (res != null && !res.equals(m)) { + return "different"; + } + res = m; + } + + StringBuilder sb = new StringBuilder(); + res.printOn(sb); + return sb.toString(); + } + + @Factory + public static Object[] create() { + return VMTest.create(MatrixTest.class); + } +} diff -r 4ce33ad7d507 -r 70d15cf323ba launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Tue Dec 25 13:31:35 2012 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Tue Dec 25 14:07:02 2012 +0100 @@ -149,7 +149,7 @@ if (id != null && value != null) { LOG.log(Level.INFO, "Received result for case {0} = {1}", new Object[]{id, value}); - value = value.replace("%20", " "); + value = decodeURL(value); cases.get(Integer.parseInt(id)).result(value, null); } @@ -253,6 +253,17 @@ } } + private static String decodeURL(String s) { + for (;;) { + int pos = s.indexOf('%'); + if (pos == -1) { + return s; + } + int i = Integer.parseInt(s.substring(pos + 1, pos + 2), 16); + s = s.substring(0, pos) + (char)i + s.substring(pos + 2); + } + } + private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException, InterruptedException { Process process = (Process)brwsr[0]; diff -r 4ce33ad7d507 -r 70d15cf323ba launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java Tue Dec 25 13:31:35 2012 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java Tue Dec 25 14:07:02 2012 +0100 @@ -22,6 +22,8 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; +import java.net.URLDecoder; +import java.net.URLEncoder; import java.util.Enumeration; import org.apidesign.bck2brwsr.core.JavaScriptBody; @@ -45,7 +47,7 @@ "window.document.getElementById(id)[attr] = value;") private static native void setAttr(String id, String attr, Object value); - @JavaScriptBody(args = {}, body = "window.close();") + @JavaScriptBody(args = {}, body = "return; window.close();") private static native void closeWindow(); private static void log(String newText) { @@ -81,6 +83,9 @@ Object result = invokeMethod(c.getClassName(), c.getMethodName()); log("Result: " + result); + + result = encodeURL("" + result); + log("Sending back: " + url + "?request=" + c.getRequestId() + "&result=" + result); u = new URL(url + "?request=" + c.getRequestId() + "&result=" + result); } @@ -91,6 +96,23 @@ } } + private static String encodeURL(String r) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < r.length(); i++) { + int ch = r.charAt(i); + if (ch < 32 || ch == '%' || ch == '+') { + sb.append("%").append(("0" + Integer.toHexString(ch)).substring(0, 2)); + } else { + if (ch == 32) { + sb.append("+"); + } else { + sb.append((char)ch); + } + } + } + return sb.toString(); + } + static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException { final Object r = invokeMethod(clazz, method); return r == null ? "null" : r.toString().toString();