rt/vm/src/test/java/org/apidesign/vm4brwsr/SizeOfAMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Feb 2014 17:41:00 +0100
branchReducedStack
changeset 1472 0a1b4f1bf4d0
parent 1469 f57fa856ffc4
child 1544 f75ee66cfc2f
permissions -rw-r--r--
No need to generate primitive final fields as their values get inlined in the classfiles anyway
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@1453
    28
import static org.testng.Assert.assertEquals;
jaroslav@1453
    29
import static org.testng.Assert.assertTrue;
jaroslav@1453
    30
import org.testng.annotations.AfterClass;
jaroslav@1453
    31
import org.testng.annotations.BeforeClass;
jaroslav@1453
    32
import org.testng.annotations.Test;
jaroslav@1453
    33
jaroslav@1453
    34
/**
jaroslav@1453
    35
 *
jaroslav@1453
    36
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@1453
    37
 */
jaroslav@1453
    38
public class SizeOfAMethodTest {
jaroslav@1457
    39
    private static String code;
jaroslav@1453
    40
jaroslav@1453
    41
    @Test public void sumXYShouldBeSmall() {
jaroslav@1457
    42
        String s = code;
jaroslav@1453
    43
        int beg = s.indexOf("c.sum__III");
jaroslav@1453
    44
        int end = s.indexOf("c.sum__III.access");
jaroslav@1453
    45
        
jaroslav@1457
    46
        assertTrue(beg > 0, "Found sum method in " + code);
jaroslav@1457
    47
        assertTrue(beg < end, "Found end of sum method in " + code);
jaroslav@1453
    48
        
jaroslav@1453
    49
        String method = s.substring(beg, end);
jaroslav@1453
    50
        
jaroslav@1457
    51
        
jaroslav@1457
    52
        assertEquals(method.indexOf("st"), -1, "There should be no stack operations:\n" + method);
jaroslav@1457
    53
    }
jaroslav@1457
    54
jaroslav@1457
    55
    @Test public void emptyConstructorRequiresNoStack() {
jaroslav@1457
    56
        String s = code;
jaroslav@1457
    57
        int beg = s.indexOf("CLS.cons__V");
jaroslav@1457
    58
        int end = s.indexOf("CLS.cons__V.access");
jaroslav@1457
    59
        
jaroslav@1457
    60
        assertTrue(beg > 0, "Found constructor in " + code);
jaroslav@1457
    61
        assertTrue(beg < end, "Found end of constructor in " + code);
jaroslav@1457
    62
        
jaroslav@1457
    63
        String method = s.substring(beg, end);
jaroslav@1457
    64
        method = method.replace("constructor", "CNSTR");
jaroslav@1457
    65
        
jaroslav@1453
    66
        assertEquals(method.indexOf("st"), -1, "There should be no stack operations:\n" + method);
jaroslav@1469
    67
        assertEquals(method.indexOf("for"), -1, "There should be no for blocks:\n" + method);
jaroslav@1453
    68
    }
jaroslav@1453
    69
    
jaroslav@1472
    70
    @Test public void dontGeneratePrimitiveFinalConstants() {
jaroslav@1472
    71
        assertEquals(code.indexOf("MISSING_CONSTANT"), -1, "MISSING_CONSTANT field should not be generated");
jaroslav@1472
    72
    }
jaroslav@1453
    73
    
jaroslav@1453
    74
    @BeforeClass 
jaroslav@1453
    75
    public static void compileTheCode() throws Exception {
jaroslav@1457
    76
        final String res = "org/apidesign/vm4brwsr/StaticMethod";
jaroslav@1453
    77
        StringBuilder sb = new StringBuilder();
jaroslav@1457
    78
        class JustStaticMethod implements Bck2Brwsr.Resources {
jaroslav@1457
    79
            @Override
jaroslav@1457
    80
            public InputStream get(String resource) throws IOException {
jaroslav@1457
    81
                final String cn = res + ".class";
jaroslav@1457
    82
                if (resource.equals(cn)) {
jaroslav@1457
    83
                    return getClass().getClassLoader().getResourceAsStream(cn);
jaroslav@1457
    84
                }
jaroslav@1457
    85
                return null;
jaroslav@1457
    86
            }
jaroslav@1457
    87
        }
jaroslav@1457
    88
        Bck2Brwsr.generate(sb, new JustStaticMethod(), res);
jaroslav@1457
    89
        code = sb.toString();
jaroslav@1453
    90
    }
jaroslav@1453
    91
    @AfterClass
jaroslav@1453
    92
    public static void releaseTheCode() {
jaroslav@1453
    93
        code = null;
jaroslav@1453
    94
    }
jaroslav@1453
    95
    
jaroslav@1453
    96
}