jaroslav@114
|
1 |
/**
|
jaroslav@114
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@114
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@114
|
4 |
*
|
jaroslav@114
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@114
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@114
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@114
|
8 |
*
|
jaroslav@114
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@114
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@114
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@114
|
12 |
* GNU General Public License for more details.
|
jaroslav@114
|
13 |
*
|
jaroslav@114
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@114
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@114
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@114
|
17 |
*/
|
jaroslav@114
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@114
|
19 |
|
jaroslav@114
|
20 |
import javax.script.Invocable;
|
jaroslav@114
|
21 |
import javax.script.ScriptException;
|
jtulach@132
|
22 |
import static org.testng.Assert.*;
|
jaroslav@114
|
23 |
import org.testng.annotations.BeforeClass;
|
jaroslav@114
|
24 |
import org.testng.annotations.Test;
|
jaroslav@114
|
25 |
|
jaroslav@114
|
26 |
/**
|
jaroslav@114
|
27 |
*
|
jaroslav@114
|
28 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@114
|
29 |
*/
|
jaroslav@114
|
30 |
public class NumberTest {
|
jaroslav@114
|
31 |
@Test public void integerFromString() throws Exception {
|
jaroslav@114
|
32 |
assertExec("Can convert string to integer", "java_lang_Integer_parseIntILjava_lang_String",
|
jaroslav@114
|
33 |
Double.valueOf(333), "333"
|
jaroslav@114
|
34 |
);
|
jaroslav@114
|
35 |
}
|
jaroslav@114
|
36 |
|
jaroslav@114
|
37 |
@Test public void doubleFromString() throws Exception {
|
jaroslav@114
|
38 |
assertExec("Can convert string to double", "java_lang_Double_parseDoubleDLjava_lang_String",
|
jaroslav@114
|
39 |
Double.valueOf(33.3), "33.3"
|
jaroslav@114
|
40 |
);
|
jaroslav@114
|
41 |
}
|
jaroslav@114
|
42 |
|
jaroslav@114
|
43 |
@Test public void autoboxDouble() throws Exception {
|
jaroslav@114
|
44 |
assertExec("Autoboxing of doubles is OK", "org_apidesign_vm4brwsr_Numbers_autoboxDblToStringLjava_lang_String",
|
jaroslav@114
|
45 |
"3.3"
|
jaroslav@114
|
46 |
);
|
jaroslav@114
|
47 |
}
|
jtulach@132
|
48 |
|
jtulach@132
|
49 |
@Test public void javalog1000() throws Exception {
|
jtulach@132
|
50 |
assertEquals(3.0, Math.log10(1000.0), 0.00003, "log_10(1000) == 3");
|
jtulach@132
|
51 |
}
|
jtulach@132
|
52 |
|
jtulach@132
|
53 |
@Test public void jslog1000() throws Exception {
|
jtulach@132
|
54 |
assertExec("log_10(1000) == 3", "java_lang_Math_log10DD",
|
jtulach@132
|
55 |
Double.valueOf(3.0), 1000.0
|
jtulach@132
|
56 |
);
|
jtulach@132
|
57 |
}
|
jaroslav@114
|
58 |
|
jaroslav@114
|
59 |
|
jaroslav@114
|
60 |
private static CharSequence codeSeq;
|
jaroslav@114
|
61 |
private static Invocable code;
|
jaroslav@114
|
62 |
|
jaroslav@114
|
63 |
@BeforeClass
|
jaroslav@114
|
64 |
public void compileTheCode() throws Exception {
|
jaroslav@114
|
65 |
if (codeSeq == null) {
|
jaroslav@114
|
66 |
StringBuilder sb = new StringBuilder();
|
jaroslav@114
|
67 |
code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers");
|
jaroslav@114
|
68 |
codeSeq = sb;
|
jaroslav@114
|
69 |
}
|
jaroslav@114
|
70 |
}
|
jaroslav@114
|
71 |
|
jaroslav@114
|
72 |
private static void assertExec(
|
jaroslav@114
|
73 |
String msg, String methodName, Object expRes, Object... args) throws Exception {
|
jaroslav@114
|
74 |
|
jaroslav@114
|
75 |
Object ret = null;
|
jaroslav@114
|
76 |
try {
|
jaroslav@114
|
77 |
ret = code.invokeFunction(methodName, args);
|
jaroslav@114
|
78 |
} catch (ScriptException ex) {
|
jaroslav@114
|
79 |
fail("Execution failed in\n" + codeSeq, ex);
|
jaroslav@114
|
80 |
} catch (NoSuchMethodException ex) {
|
jaroslav@114
|
81 |
fail("Cannot find method in\n" + codeSeq, ex);
|
jaroslav@114
|
82 |
}
|
jaroslav@114
|
83 |
if (ret == null && expRes == null) {
|
jaroslav@114
|
84 |
return;
|
jaroslav@114
|
85 |
}
|
jaroslav@114
|
86 |
if (expRes.equals(ret)) {
|
jaroslav@114
|
87 |
return;
|
jaroslav@114
|
88 |
}
|
jtulach@132
|
89 |
if (expRes instanceof Double && ret instanceof Double) {
|
jtulach@132
|
90 |
double expD = ((Double)expRes).doubleValue();
|
jtulach@132
|
91 |
double retD = ((Double)ret).doubleValue();
|
jtulach@132
|
92 |
assertEquals(retD, expD, 0.000004, msg + " was " + ret + "\n" + codeSeq);
|
jtulach@132
|
93 |
return;
|
jtulach@132
|
94 |
}
|
jaroslav@114
|
95 |
assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
|
jaroslav@114
|
96 |
}
|
jaroslav@114
|
97 |
|
jaroslav@114
|
98 |
}
|