jaroslav@0
|
1 |
/*
|
jaroslav@0
|
2 |
Java 4 Browser Bytecode Translator
|
jaroslav@0
|
3 |
Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@0
|
4 |
|
jaroslav@0
|
5 |
This program is free software: you can redistribute it and/or modify
|
jaroslav@0
|
6 |
it under the terms of the GNU General Public License as published by
|
jaroslav@0
|
7 |
the Free Software Foundation, version 2 of the License.
|
jaroslav@0
|
8 |
|
jaroslav@0
|
9 |
This program is distributed in the hope that it will be useful,
|
jaroslav@0
|
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@0
|
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@0
|
12 |
GNU General Public License for more details.
|
jaroslav@0
|
13 |
|
jaroslav@0
|
14 |
You should have received a copy of the GNU General Public License
|
jaroslav@0
|
15 |
along with this program. Look for COPYING file in the top folder.
|
jaroslav@0
|
16 |
If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@0
|
17 |
*/
|
jaroslav@22
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@0
|
19 |
|
jaroslav@0
|
20 |
import java.io.IOException;
|
jaroslav@0
|
21 |
import javax.script.Invocable;
|
jaroslav@0
|
22 |
import javax.script.ScriptEngine;
|
jaroslav@0
|
23 |
import javax.script.ScriptEngineManager;
|
jaroslav@0
|
24 |
import javax.script.ScriptException;
|
jaroslav@0
|
25 |
import static org.testng.Assert.*;
|
jaroslav@0
|
26 |
import org.testng.annotations.Test;
|
jaroslav@0
|
27 |
|
jaroslav@0
|
28 |
/** Checks the basic behavior of the translator.
|
jaroslav@0
|
29 |
*
|
jaroslav@0
|
30 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@0
|
31 |
*/
|
jaroslav@0
|
32 |
public class StaticMethodTest {
|
jaroslav@0
|
33 |
@Test public void threePlusFour() throws Exception {
|
jaroslav@2
|
34 |
assertExec(
|
jaroslav@2
|
35 |
"Should be seven",
|
jaroslav@22
|
36 |
"org_apidesign_vm4brwsr_StaticMethod_sumIII",
|
jaroslav@2
|
37 |
Double.valueOf(7),
|
jaroslav@2
|
38 |
3, 4
|
jaroslav@2
|
39 |
);
|
jaroslav@0
|
40 |
}
|
jaroslav@0
|
41 |
|
jaroslav@1
|
42 |
@Test public void powerOfThree() throws Exception {
|
jaroslav@2
|
43 |
assertExec(
|
jaroslav@2
|
44 |
"Should be nine",
|
jaroslav@22
|
45 |
"org_apidesign_vm4brwsr_StaticMethod_powerFF",
|
jaroslav@2
|
46 |
Double.valueOf(9),
|
jaroslav@2
|
47 |
3.0f
|
jaroslav@2
|
48 |
);
|
jaroslav@1
|
49 |
}
|
jaroslav@1
|
50 |
|
jaroslav@48
|
51 |
@Test public void minusOne() throws Exception {
|
jaroslav@48
|
52 |
assertExec(
|
jaroslav@48
|
53 |
"Should be minus one",
|
jaroslav@48
|
54 |
"org_apidesign_vm4brwsr_StaticMethod_minusOneI",
|
jaroslav@48
|
55 |
Double.valueOf(-1)
|
jaroslav@48
|
56 |
);
|
jaroslav@48
|
57 |
}
|
jaroslav@48
|
58 |
|
jaroslav@2
|
59 |
@Test public void doubleWithoutLong() throws Exception {
|
jaroslav@2
|
60 |
assertExec(
|
jaroslav@2
|
61 |
"Should be two",
|
jaroslav@22
|
62 |
"org_apidesign_vm4brwsr_StaticMethod_minusDDJ",
|
jaroslav@2
|
63 |
Double.valueOf(2),
|
jaroslav@2
|
64 |
3.0d, 1l
|
jaroslav@2
|
65 |
);
|
jaroslav@2
|
66 |
}
|
jaroslav@3
|
67 |
|
jaroslav@3
|
68 |
@Test public void divAndRound() throws Exception {
|
jaroslav@3
|
69 |
assertExec(
|
jaroslav@3
|
70 |
"Should be rounded to one",
|
jaroslav@22
|
71 |
"org_apidesign_vm4brwsr_StaticMethod_divIBD",
|
jaroslav@3
|
72 |
Double.valueOf(1),
|
jaroslav@3
|
73 |
3, 3.75
|
jaroslav@3
|
74 |
);
|
jaroslav@3
|
75 |
}
|
jaroslav@3
|
76 |
@Test public void mixedMethodFourParams() throws Exception {
|
jaroslav@3
|
77 |
assertExec(
|
jaroslav@3
|
78 |
"Should be two",
|
jaroslav@22
|
79 |
"org_apidesign_vm4brwsr_StaticMethod_mixIIJBD",
|
jaroslav@3
|
80 |
Double.valueOf(20),
|
jaroslav@3
|
81 |
2, 10l, 5, 2.0
|
jaroslav@3
|
82 |
);
|
jaroslav@3
|
83 |
}
|
jaroslav@4
|
84 |
@Test public void factRec() throws Exception {
|
jaroslav@4
|
85 |
assertExec(
|
jaroslav@4
|
86 |
"Factorial of 5 is 120",
|
jaroslav@22
|
87 |
"org_apidesign_vm4brwsr_StaticMethod_factRecJI",
|
jaroslav@4
|
88 |
Double.valueOf(120),
|
jaroslav@4
|
89 |
5
|
jaroslav@4
|
90 |
);
|
jaroslav@4
|
91 |
}
|
jaroslav@5
|
92 |
@Test public void factIter() throws Exception {
|
jaroslav@5
|
93 |
assertExec(
|
jaroslav@5
|
94 |
"Factorial of 5 is 120",
|
jaroslav@22
|
95 |
"org_apidesign_vm4brwsr_StaticMethod_factIterJI",
|
jaroslav@5
|
96 |
Double.valueOf(120),
|
jaroslav@5
|
97 |
5
|
jaroslav@5
|
98 |
);
|
jaroslav@5
|
99 |
}
|
jaroslav@2
|
100 |
|
jaroslav@6
|
101 |
@Test public void xor() throws Exception {
|
jaroslav@6
|
102 |
assertExec(
|
jaroslav@6
|
103 |
"Xor is 4",
|
jaroslav@22
|
104 |
"org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
|
jaroslav@6
|
105 |
Double.valueOf(4),
|
jaroslav@6
|
106 |
7,
|
jaroslav@6
|
107 |
3
|
jaroslav@6
|
108 |
);
|
jaroslav@6
|
109 |
}
|
jaroslav@6
|
110 |
|
jaroslav@7
|
111 |
@Test public void or() throws Exception {
|
jaroslav@7
|
112 |
assertExec(
|
jaroslav@7
|
113 |
"Or will be 7",
|
jaroslav@22
|
114 |
"org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
|
jaroslav@7
|
115 |
Double.valueOf(7),
|
jaroslav@7
|
116 |
true,
|
jaroslav@7
|
117 |
4,
|
jaroslav@7
|
118 |
3
|
jaroslav@7
|
119 |
);
|
jaroslav@7
|
120 |
}
|
jaroslav@46
|
121 |
@Test public void nullCheck() throws Exception {
|
jaroslav@46
|
122 |
assertExec(
|
jaroslav@46
|
123 |
"Returns nothing",
|
jaroslav@46
|
124 |
"org_apidesign_vm4brwsr_StaticMethod_noneLjava_lang_ObjectII",
|
jaroslav@46
|
125 |
null, 1, 3
|
jaroslav@46
|
126 |
);
|
jaroslav@46
|
127 |
}
|
jaroslav@7
|
128 |
@Test public void and() throws Exception {
|
jaroslav@7
|
129 |
assertExec(
|
jaroslav@7
|
130 |
"And will be 3",
|
jaroslav@22
|
131 |
"org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
|
jaroslav@7
|
132 |
Double.valueOf(3),
|
jaroslav@7
|
133 |
false,
|
jaroslav@7
|
134 |
7,
|
jaroslav@7
|
135 |
3
|
jaroslav@7
|
136 |
);
|
jaroslav@7
|
137 |
}
|
jaroslav@9
|
138 |
@Test public void inc4() throws Exception {
|
jaroslav@9
|
139 |
assertExec(
|
jaroslav@9
|
140 |
"It will be 4",
|
jaroslav@22
|
141 |
"org_apidesign_vm4brwsr_StaticMethod_inc4I",
|
jaroslav@9
|
142 |
Double.valueOf(4)
|
jaroslav@9
|
143 |
);
|
jaroslav@9
|
144 |
}
|
jaroslav@7
|
145 |
|
jaroslav@93
|
146 |
@Test public void shiftLeftInJava() throws Exception {
|
jaroslav@93
|
147 |
int res = StaticMethod.shiftLeft(1, 8);
|
jaroslav@93
|
148 |
assertEquals(res, 256);
|
jaroslav@93
|
149 |
}
|
jaroslav@93
|
150 |
|
jaroslav@93
|
151 |
@Test public void shiftLeftInJS() throws Exception {
|
jaroslav@93
|
152 |
assertExec(
|
jaroslav@93
|
153 |
"Setting 9th bit",
|
jaroslav@93
|
154 |
"org_apidesign_vm4brwsr_StaticMethod_shiftLeftIII",
|
jaroslav@93
|
155 |
Double.valueOf(256),
|
jaroslav@93
|
156 |
1, 8
|
jaroslav@93
|
157 |
);
|
jaroslav@93
|
158 |
}
|
jaroslav@93
|
159 |
|
jaroslav@93
|
160 |
@Test public void shiftRightInJava() throws Exception {
|
jaroslav@93
|
161 |
int res = StaticMethod.shiftArithmRight(-8, 3, true);
|
jaroslav@93
|
162 |
assertEquals(res, -1);
|
jaroslav@93
|
163 |
}
|
jaroslav@93
|
164 |
|
jaroslav@93
|
165 |
@Test public void shiftRightInJS() throws Exception {
|
jaroslav@93
|
166 |
assertExec(
|
jaroslav@93
|
167 |
"Get -1",
|
jaroslav@93
|
168 |
"org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
|
jaroslav@93
|
169 |
Double.valueOf(-1),
|
jaroslav@93
|
170 |
-8, 3, true
|
jaroslav@93
|
171 |
);
|
jaroslav@93
|
172 |
}
|
jaroslav@93
|
173 |
@Test public void unsignedShiftRightInJava() throws Exception {
|
jaroslav@93
|
174 |
int res = StaticMethod.shiftArithmRight(8, 3, false);
|
jaroslav@93
|
175 |
assertEquals(res, 1);
|
jaroslav@93
|
176 |
}
|
jaroslav@93
|
177 |
|
jaroslav@93
|
178 |
@Test public void unsignedShiftRightInJS() throws Exception {
|
jaroslav@93
|
179 |
assertExec(
|
jaroslav@93
|
180 |
"Get -1",
|
jaroslav@93
|
181 |
"org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
|
jaroslav@93
|
182 |
Double.valueOf(1),
|
jaroslav@93
|
183 |
8, 3, false
|
jaroslav@93
|
184 |
);
|
jaroslav@93
|
185 |
}
|
jaroslav@93
|
186 |
|
jaroslav@2
|
187 |
private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
|
jaroslav@2
|
188 |
StringBuilder sb = new StringBuilder();
|
jaroslav@22
|
189 |
Invocable i = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
|
jaroslav@2
|
190 |
|
jaroslav@2
|
191 |
Object ret = null;
|
jaroslav@2
|
192 |
try {
|
jaroslav@2
|
193 |
ret = i.invokeFunction(methodName, args);
|
jaroslav@5
|
194 |
} catch (ScriptException ex) {
|
jaroslav@5
|
195 |
fail("Execution failed in " + sb, ex);
|
jaroslav@2
|
196 |
} catch (NoSuchMethodException ex) {
|
jaroslav@2
|
197 |
fail("Cannot find method in " + sb, ex);
|
jaroslav@2
|
198 |
}
|
jaroslav@2
|
199 |
if (ret == null && expRes == null) {
|
jaroslav@2
|
200 |
return;
|
jaroslav@2
|
201 |
}
|
jaroslav@46
|
202 |
if (expRes != null && expRes.equals(ret)) {
|
jaroslav@2
|
203 |
return;
|
jaroslav@2
|
204 |
}
|
jaroslav@3
|
205 |
assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
|
jaroslav@2
|
206 |
|
jaroslav@2
|
207 |
}
|
jaroslav@2
|
208 |
|
jaroslav@13
|
209 |
static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
|
jaroslav@19
|
210 |
if (sb == null) {
|
jaroslav@19
|
211 |
sb = new StringBuilder();
|
jaroslav@19
|
212 |
}
|
jaroslav@29
|
213 |
GenJS.compile(sb, names);
|
jaroslav@0
|
214 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@0
|
215 |
ScriptEngine js = sem.getEngineByExtension("js");
|
jaroslav@0
|
216 |
try {
|
jaroslav@0
|
217 |
Object res = js.eval(sb.toString());
|
jaroslav@0
|
218 |
assertTrue(js instanceof Invocable, "It is invocable object: " + res);
|
jaroslav@0
|
219 |
return (Invocable)js;
|
jaroslav@0
|
220 |
} catch (ScriptException ex) {
|
jaroslav@0
|
221 |
fail("Could not compile:\n" + sb, ex);
|
jaroslav@0
|
222 |
return null;
|
jaroslav@0
|
223 |
}
|
jaroslav@0
|
224 |
}
|
jaroslav@0
|
225 |
}
|