vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 11 Feb 2013 12:46:43 +0100
branchemul
changeset 708 59d5596a9c6c
parent 595 784aaf9ee179
child 747 ae352b763959
permissions -rw-r--r--
Encapsulation. Moving shared code into TestVM instance.
     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 org.testng.annotations.Test;
    21 import static org.testng.Assert.*;
    22 import org.testng.annotations.BeforeClass;
    23 
    24 /**
    25  *
    26  * @author Jaroslav Tulach <jtulach@netbeans.org>
    27  */
    28 public class StringTest {
    29     @Test public void firstChar() throws Exception {
    30         assertExec(
    31             "First char in Hello is H",
    32             StringSample.class, "sayHello__CI",
    33             72, 0
    34         );
    35     }
    36 
    37     @Test public void fromChars() throws Exception {
    38         assertExec(
    39             "First char in Hello is ABC",
    40             StringSample.class, "fromChars__Ljava_lang_String_2CCC",
    41             "ABC", 'A', 'B', 'C'
    42         );
    43     }
    44 
    45     @Test public void concatChars() throws Exception {
    46         assertExec(
    47             "Composing yields ABC",
    48             StringSample.class, "chars__Ljava_lang_String_2CCC",
    49             "ABC", 'A', 'B', 'C'
    50         );
    51     }
    52 
    53     @Test public void concatCharsFromInts() throws Exception {
    54         assertExec(
    55             "Composing yields ABC",
    56             StringSample.class, "charsFromNumbers__Ljava_lang_String_2",
    57             "ABC"
    58         );
    59     }
    60 
    61     @Test public void concatCharsFromChars() throws Exception {
    62         assertExec(
    63             "Composing yields ABC",
    64             StringSample.class, "charsFromChars__Ljava_lang_String_2",
    65             "ABC"
    66         );
    67     }
    68 
    69     @Test public void instanceOfWorks() throws Exception {
    70         assertExec(
    71             "It is string",
    72             StringSample.class, "isStringInstance__Z",
    73             Double.valueOf(1.0)
    74         );
    75     }
    76 
    77     @Test public void getBytes() throws Exception {
    78         final String horse = "Žluťoučký kůň";
    79         final String expected = StringSample.getBytes(horse);
    80         assertExec(
    81             "Bytes look simplar",
    82             StringSample.class, "getBytes__Ljava_lang_String_2Ljava_lang_String_2",
    83             expected, horse
    84         );
    85     }
    86 
    87     @Test(timeOut=10000) public void toStringConcatenation() throws Exception {
    88         assertExec(
    89             "Five executions should generate 5Hello World!",
    90             StringSample.class, "toStringTest__Ljava_lang_String_2I",
    91             "Hello World!5", 5
    92         );
    93     }
    94     @Test public void toStringConcatenationJava() throws Exception {
    95         assertEquals("Hello World!5", StringSample.toStringTest(5));
    96     }
    97     
    98     @Test(timeOut=10000) public void stringStringConcat() throws Exception {
    99         assertExec(
   100             "Composes strings OK",
   101             StringSample.class, "concatStrings__Ljava_lang_String_2",
   102             "Hello World!1" + "\\\n\r\t"
   103         );
   104     }
   105 
   106     @Test public void equalsAndSubstring() throws Exception {
   107         assertExec(
   108             "Composes are OK",
   109             StringSample.class, "equalToHello__ZII",
   110             true, 0, 5
   111         );
   112     }
   113     @Test public void replaceChars() throws Exception {
   114         assertExec(
   115             "Can replace slashes by underscores",
   116             StringSample.class, "replace__Ljava_lang_String_2Ljava_lang_String_2CC",
   117             "x_y_z", "x/y/z", '/', '_'
   118         );
   119     }
   120     @Test public void replaceIntChars() throws Exception {
   121         assertExec(
   122             "Can replace slashes by underscores",
   123             StringSample.class, "replace__Ljava_lang_String_2Ljava_lang_String_2CC",
   124             "x_y_z", "x/y/z", (int)'/', (int)'_'
   125         );
   126     }
   127 
   128     @Test public void insertBuilder() throws Exception {
   129         assertExec(
   130             "Can insert something into a buffer?",
   131             StringSample.class, "insertBuffer__Ljava_lang_String_2",
   132             "Ahojdo!"
   133         );
   134     }
   135     
   136     @Test public void compareHashCodeHi() throws Exception {
   137         String j = "Hi";
   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 compareHashCode1() throws Exception {
   146         String j = "Hello Java!";
   147         int jh = StringSample.hashCode(j);
   148         assertExec(
   149             "Hashcode is the same " + jh,
   150             StringSample.class, "hashCode__ILjava_lang_String_2",
   151             Double.valueOf(jh), j
   152         );
   153     }
   154     @Test public void stringSwitch1() throws Exception {
   155         assertExec(
   156             "Get one",
   157             StringSample.class, "stringSwitch__ILjava_lang_String_2",
   158             Double.valueOf(1), "jedna"
   159         );
   160     }
   161     @Test public void stringSwitch2() throws Exception {
   162         assertExec(
   163             "Get two",
   164             StringSample.class, "stringSwitch__ILjava_lang_String_2",
   165             Double.valueOf(2), "dve"
   166         );
   167     }
   168     @Test public void stringSwitchDefault() throws Exception {
   169         assertExec(
   170             "Get -1",
   171             StringSample.class, "stringSwitch__ILjava_lang_String_2",
   172             Double.valueOf(-1), "none"
   173         );
   174     }
   175     
   176     @Test public void countAB() throws Exception {
   177         assertEquals(StringSample.countAB("Ahoj Bedo!"), 3, "Verify Java code is sane");
   178         assertExec(
   179             "One A and one B adds to 3",
   180             StringSample.class, "countAB__ILjava_lang_String_2",
   181             Double.valueOf(3), "Ahoj Bedo!"
   182         );
   183         
   184     }
   185 
   186     @Test public void compareStrings() throws Exception {
   187         int res = StringSample.compare("Saab", "Volvo");
   188         assertExec(
   189             "Saab finished sooner than Volvo",
   190             StringSample.class, "compare__ILjava_lang_String_2Ljava_lang_String_2",
   191             Double.valueOf(res), "Saab", "Volvo"
   192         );
   193         
   194     }
   195     
   196     private static TestVM code;
   197     
   198     @BeforeClass 
   199     public void compileTheCode() throws Exception {
   200         code = TestVM.compileClass(
   201             "org/apidesign/vm4brwsr/StringSample",
   202             "java/lang/String"
   203         );
   204     }
   205     
   206     private static void assertExec(String msg, 
   207         Class<?> clazz, String method, Object expRes, Object... args
   208     ) throws Exception {
   209         code.assertExec(msg, clazz, method, expRes, args);
   210     }
   211     
   212 }