jaroslav@1453
|
1 |
/**
|
jaroslav@1453
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1453
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1453
|
4 |
*
|
jaroslav@1453
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1453
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1453
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1453
|
8 |
*
|
jaroslav@1453
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1453
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1453
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1453
|
12 |
* GNU General Public License for more details.
|
jaroslav@1453
|
13 |
*
|
jaroslav@1453
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1453
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1453
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@1453
|
17 |
*/
|
jaroslav@1453
|
18 |
/*
|
jaroslav@1453
|
19 |
* To change this license header, choose License Headers in Project Properties.
|
jaroslav@1453
|
20 |
* To change this template file, choose Tools | Templates
|
jaroslav@1453
|
21 |
* and open the template in the editor.
|
jaroslav@1453
|
22 |
*/
|
jaroslav@1453
|
23 |
|
jaroslav@1453
|
24 |
package org.apidesign.vm4brwsr;
|
jaroslav@1453
|
25 |
|
jaroslav@1457
|
26 |
import java.io.IOException;
|
jaroslav@1457
|
27 |
import java.io.InputStream;
|
jaroslav@1544
|
28 |
import static org.testng.Assert.*;
|
jaroslav@1453
|
29 |
import org.testng.annotations.AfterClass;
|
jaroslav@1453
|
30 |
import org.testng.annotations.BeforeClass;
|
jaroslav@1453
|
31 |
import org.testng.annotations.Test;
|
jaroslav@1453
|
32 |
|
jaroslav@1453
|
33 |
/**
|
jaroslav@1453
|
34 |
*
|
jaroslav@1453
|
35 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@1453
|
36 |
*/
|
jaroslav@1453
|
37 |
public class SizeOfAMethodTest {
|
jaroslav@1457
|
38 |
private static String code;
|
jaroslav@1453
|
39 |
|
jaroslav@1453
|
40 |
@Test public void sumXYShouldBeSmall() {
|
jaroslav@1457
|
41 |
String s = code;
|
jaroslav@1453
|
42 |
int beg = s.indexOf("c.sum__III");
|
jaroslav@1453
|
43 |
int end = s.indexOf("c.sum__III.access");
|
jaroslav@1453
|
44 |
|
jaroslav@1457
|
45 |
assertTrue(beg > 0, "Found sum method in " + code);
|
jaroslav@1457
|
46 |
assertTrue(beg < end, "Found end of sum method in " + code);
|
jaroslav@1453
|
47 |
|
jaroslav@1453
|
48 |
String method = s.substring(beg, end);
|
jaroslav@1453
|
49 |
|
jaroslav@1457
|
50 |
|
jaroslav@1457
|
51 |
assertEquals(method.indexOf("st"), -1, "There should be no stack operations:\n" + method);
|
jaroslav@1457
|
52 |
}
|
jaroslav@1457
|
53 |
|
jaroslav@1544
|
54 |
@Test public void betterConstructor() {
|
jaroslav@1544
|
55 |
String s = code;
|
jaroslav@1544
|
56 |
int beg = s.indexOf("c.initInflater__IIZ");
|
jaroslav@1544
|
57 |
int end = s.indexOf("c.initInflater__IIZ.access");
|
jaroslav@1544
|
58 |
|
jaroslav@1544
|
59 |
assertTrue(beg > 0, "Found initInflater method in " + code);
|
jaroslav@1544
|
60 |
assertTrue(beg < end, "Found end of initInflater method in " + code);
|
jaroslav@1544
|
61 |
|
jaroslav@1544
|
62 |
String method = s.substring(beg, end);
|
jaroslav@1544
|
63 |
|
jaroslav@1544
|
64 |
assertEquals(method.indexOf("stA1"), -1, "No need for stA1 register:\n" + method);
|
jaroslav@1544
|
65 |
}
|
jaroslav@1544
|
66 |
|
jaroslav@1545
|
67 |
@Test public void deepConstructor() {
|
jaroslav@1545
|
68 |
String s = code;
|
jaroslav@1545
|
69 |
int beg = s.indexOf("c.intHolder__I");
|
jaroslav@1545
|
70 |
int end = s.indexOf("c.intHolder__I.access");
|
jaroslav@1545
|
71 |
|
jaroslav@1545
|
72 |
assertTrue(beg > 0, "Found intHolder method in " + code);
|
jaroslav@1545
|
73 |
assertTrue(beg < end, "Found end of intHolder method in " + code);
|
jaroslav@1545
|
74 |
|
jaroslav@1545
|
75 |
String method = s.substring(beg, end);
|
jaroslav@1545
|
76 |
|
jaroslav@1545
|
77 |
assertEquals(method.indexOf("stA3"), -1, "No need for stA3 register on second constructor:\n" + method);
|
jaroslav@1545
|
78 |
}
|
jaroslav@1545
|
79 |
|
jaroslav@1457
|
80 |
@Test public void emptyConstructorRequiresNoStack() {
|
jaroslav@1457
|
81 |
String s = code;
|
jaroslav@1457
|
82 |
int beg = s.indexOf("CLS.cons__V");
|
jaroslav@1457
|
83 |
int end = s.indexOf("CLS.cons__V.access");
|
jaroslav@1457
|
84 |
|
jaroslav@1457
|
85 |
assertTrue(beg > 0, "Found constructor in " + code);
|
jaroslav@1457
|
86 |
assertTrue(beg < end, "Found end of constructor in " + code);
|
jaroslav@1457
|
87 |
|
jaroslav@1457
|
88 |
String method = s.substring(beg, end);
|
jaroslav@1457
|
89 |
method = method.replace("constructor", "CNSTR");
|
jaroslav@1457
|
90 |
|
jaroslav@1453
|
91 |
assertEquals(method.indexOf("st"), -1, "There should be no stack operations:\n" + method);
|
jaroslav@1469
|
92 |
assertEquals(method.indexOf("for"), -1, "There should be no for blocks:\n" + method);
|
jaroslav@1453
|
93 |
}
|
jaroslav@1453
|
94 |
|
jaroslav@1472
|
95 |
@Test public void dontGeneratePrimitiveFinalConstants() {
|
jaroslav@1472
|
96 |
assertEquals(code.indexOf("MISSING_CONSTANT"), -1, "MISSING_CONSTANT field should not be generated");
|
jaroslav@1472
|
97 |
}
|
jaroslav@1453
|
98 |
|
jaroslav@1453
|
99 |
@BeforeClass
|
jaroslav@1453
|
100 |
public static void compileTheCode() throws Exception {
|
jaroslav@1457
|
101 |
final String res = "org/apidesign/vm4brwsr/StaticMethod";
|
jaroslav@1453
|
102 |
StringBuilder sb = new StringBuilder();
|
jaroslav@1457
|
103 |
class JustStaticMethod implements Bck2Brwsr.Resources {
|
jaroslav@1457
|
104 |
@Override
|
jaroslav@1457
|
105 |
public InputStream get(String resource) throws IOException {
|
jaroslav@1457
|
106 |
final String cn = res + ".class";
|
jaroslav@1457
|
107 |
if (resource.equals(cn)) {
|
jaroslav@1457
|
108 |
return getClass().getClassLoader().getResourceAsStream(cn);
|
jaroslav@1457
|
109 |
}
|
jaroslav@1457
|
110 |
return null;
|
jaroslav@1457
|
111 |
}
|
jaroslav@1457
|
112 |
}
|
jaroslav@1457
|
113 |
Bck2Brwsr.generate(sb, new JustStaticMethod(), res);
|
jaroslav@1457
|
114 |
code = sb.toString();
|
jaroslav@1453
|
115 |
}
|
jaroslav@1453
|
116 |
@AfterClass
|
jaroslav@1453
|
117 |
public static void releaseTheCode() {
|
jaroslav@1453
|
118 |
code = null;
|
jaroslav@1453
|
119 |
}
|
jaroslav@1453
|
120 |
|
jaroslav@1453
|
121 |
}
|