rt/vm/src/test/java/org/apidesign/vm4brwsr/SizeOfAMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 07 May 2014 10:45:42 +0200
branchclosure
changeset 1544 f75ee66cfc2f
parent 1472 0a1b4f1bf4d0
child 1545 50991eae72d4
permissions -rw-r--r--
Test to verify constructors don't need special register
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@1457
    67
    @Test public void emptyConstructorRequiresNoStack() {
jaroslav@1457
    68
        String s = code;
jaroslav@1457
    69
        int beg = s.indexOf("CLS.cons__V");
jaroslav@1457
    70
        int end = s.indexOf("CLS.cons__V.access");
jaroslav@1457
    71
        
jaroslav@1457
    72
        assertTrue(beg > 0, "Found constructor in " + code);
jaroslav@1457
    73
        assertTrue(beg < end, "Found end of constructor in " + code);
jaroslav@1457
    74
        
jaroslav@1457
    75
        String method = s.substring(beg, end);
jaroslav@1457
    76
        method = method.replace("constructor", "CNSTR");
jaroslav@1457
    77
        
jaroslav@1453
    78
        assertEquals(method.indexOf("st"), -1, "There should be no stack operations:\n" + method);
jaroslav@1469
    79
        assertEquals(method.indexOf("for"), -1, "There should be no for blocks:\n" + method);
jaroslav@1453
    80
    }
jaroslav@1453
    81
    
jaroslav@1472
    82
    @Test public void dontGeneratePrimitiveFinalConstants() {
jaroslav@1472
    83
        assertEquals(code.indexOf("MISSING_CONSTANT"), -1, "MISSING_CONSTANT field should not be generated");
jaroslav@1472
    84
    }
jaroslav@1453
    85
    
jaroslav@1453
    86
    @BeforeClass 
jaroslav@1453
    87
    public static void compileTheCode() throws Exception {
jaroslav@1457
    88
        final String res = "org/apidesign/vm4brwsr/StaticMethod";
jaroslav@1453
    89
        StringBuilder sb = new StringBuilder();
jaroslav@1457
    90
        class JustStaticMethod implements Bck2Brwsr.Resources {
jaroslav@1457
    91
            @Override
jaroslav@1457
    92
            public InputStream get(String resource) throws IOException {
jaroslav@1457
    93
                final String cn = res + ".class";
jaroslav@1457
    94
                if (resource.equals(cn)) {
jaroslav@1457
    95
                    return getClass().getClassLoader().getResourceAsStream(cn);
jaroslav@1457
    96
                }
jaroslav@1457
    97
                return null;
jaroslav@1457
    98
            }
jaroslav@1457
    99
        }
jaroslav@1457
   100
        Bck2Brwsr.generate(sb, new JustStaticMethod(), res);
jaroslav@1457
   101
        code = sb.toString();
jaroslav@1453
   102
    }
jaroslav@1453
   103
    @AfterClass
jaroslav@1453
   104
    public static void releaseTheCode() {
jaroslav@1453
   105
        code = null;
jaroslav@1453
   106
    }
jaroslav@1453
   107
    
jaroslav@1453
   108
}