vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 04 Dec 2012 09:16:53 +0100
changeset 248 0bfcb6585290
parent 206 84920274cb32
child 405 e41809be6106
child 572 e84419744dba
permissions -rw-r--r--
Using the same mangling scheme as JNI
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import javax.script.Invocable;
    21 import org.testng.annotations.Test;
    22 import static org.testng.Assert.*;
    23 import org.testng.annotations.BeforeClass;
    24 
    25 /**
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 public class StringTest {
    30     @Test public void firstChar() throws Exception {
    31         assertExec(
    32             "First char in Hello is H",
    33             StringSample.class, "sayHello__CI",
    34             72, 0
    35         );
    36     }
    37 
    38     @Test public void fromChars() throws Exception {
    39         assertExec(
    40             "First char in Hello is ABC",
    41             StringSample.class, "fromChars__Ljava_lang_String_2CCC",
    42             "ABC", 'A', 'B', 'C'
    43         );
    44     }
    45 
    46     @Test public void concatChars() throws Exception {
    47         assertExec(
    48             "Composing yields ABC",
    49             StringSample.class, "chars__Ljava_lang_String_2CCC",
    50             "ABC", 'A', 'B', 'C'
    51         );
    52     }
    53 
    54     @Test public void concatCharsFromInts() throws Exception {
    55         assertExec(
    56             "Composing yields ABC",
    57             StringSample.class, "charsFromNumbers__Ljava_lang_String_2",
    58             "ABC"
    59         );
    60     }
    61 
    62     @Test public void concatCharsFromChars() throws Exception {
    63         assertExec(
    64             "Composing yields ABC",
    65             StringSample.class, "charsFromChars__Ljava_lang_String_2",
    66             "ABC"
    67         );
    68     }
    69 
    70     @Test public void instanceOfWorks() throws Exception {
    71         assertExec(
    72             "It is string",
    73             StringSample.class, "isStringInstance__Z",
    74             Double.valueOf(1.0)
    75         );
    76     }
    77 
    78     @Test(timeOut=10000) public void toStringConcatenation() throws Exception {
    79         assertExec(
    80             "Five executions should generate 5Hello World!",
    81             StringSample.class, "toStringTest__Ljava_lang_String_2I",
    82             "Hello World!5", 5
    83         );
    84     }
    85     @Test public void toStringConcatenationJava() throws Exception {
    86         assertEquals("Hello World!5", StringSample.toStringTest(5));
    87     }
    88     
    89     @Test(timeOut=10000) public void stringStringConcat() throws Exception {
    90         assertExec(
    91             "Composes strings OK",
    92             StringSample.class, "concatStrings__Ljava_lang_String_2",
    93             "Hello World!1" + "\\\n\r\t"
    94         );
    95     }
    96 
    97     @Test public void equalsAndSubstring() throws Exception {
    98         assertExec(
    99             "Composes are OK",
   100             StringSample.class, "equalToHello__ZII",
   101             true, 0, 5
   102         );
   103     }
   104     @Test public void replaceChars() throws Exception {
   105         assertExec(
   106             "Can replace slashes by underscores",
   107             StringSample.class, "replace__Ljava_lang_String_2Ljava_lang_String_2CC",
   108             "x_y_z", "x/y/z", '/', '_'
   109         );
   110     }
   111     @Test public void replaceIntChars() throws Exception {
   112         assertExec(
   113             "Can replace slashes by underscores",
   114             StringSample.class, "replace__Ljava_lang_String_2Ljava_lang_String_2CC",
   115             "x_y_z", "x/y/z", (int)'/', (int)'_'
   116         );
   117     }
   118 
   119     @Test public void insertBuilder() throws Exception {
   120         assertExec(
   121             "Can insert something into a buffer?",
   122             StringSample.class, "insertBuffer__Ljava_lang_String_2",
   123             "Ahojdo!"
   124         );
   125     }
   126     
   127     @Test public void compareHashCodeHi() throws Exception {
   128         String j = "Hi";
   129         int jh = StringSample.hashCode(j);
   130         assertExec(
   131             "Hashcode is the same " +jh,
   132             StringSample.class, "hashCode__ILjava_lang_String_2",
   133             Double.valueOf(jh), j
   134         );
   135     }
   136     @Test public void compareHashCode1() throws Exception {
   137         String j = "Hello Java!";
   138         int jh = StringSample.hashCode(j);
   139         assertExec(
   140             "Hashcode is the same " + jh,
   141             StringSample.class, "hashCode__ILjava_lang_String_2",
   142             Double.valueOf(jh), j
   143         );
   144     }
   145     @Test public void stringSwitch1() throws Exception {
   146         assertExec(
   147             "Get one",
   148             StringSample.class, "stringSwitch__ILjava_lang_String_2",
   149             Double.valueOf(1), "jedna"
   150         );
   151     }
   152     @Test public void stringSwitch2() throws Exception {
   153         assertExec(
   154             "Get two",
   155             StringSample.class, "stringSwitch__ILjava_lang_String_2",
   156             Double.valueOf(2), "dve"
   157         );
   158     }
   159     @Test public void stringSwitchDefault() throws Exception {
   160         assertExec(
   161             "Get -1",
   162             StringSample.class, "stringSwitch__ILjava_lang_String_2",
   163             Double.valueOf(-1), "none"
   164         );
   165     }
   166     
   167     @Test public void countAB() throws Exception {
   168         assertEquals(StringSample.countAB("Ahoj Bedo!"), 3, "Verify Java code is sane");
   169         assertExec(
   170             "One A and one B adds to 3",
   171             StringSample.class, "countAB__ILjava_lang_String_2",
   172             Double.valueOf(3), "Ahoj Bedo!"
   173         );
   174         
   175     }
   176     
   177     private static CharSequence codeSeq;
   178     private static Invocable code;
   179     
   180     @BeforeClass 
   181     public void compileTheCode() throws Exception {
   182         StringBuilder sb = new StringBuilder();
   183         code = StaticMethodTest.compileClass(sb, 
   184             "org/apidesign/vm4brwsr/StringSample",
   185             "java/lang/String"
   186         );
   187         codeSeq = sb;
   188     }
   189     
   190     private static void assertExec(String msg, 
   191         Class<?> clazz, String method, Object expRes, Object... args
   192     ) throws Exception {
   193         StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
   194     }
   195     
   196 }