rt/vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985 cd1cc103a03c
parent 1354 43f89d9f7238
permissions -rw-r--r--
Implementation of ClassValue for bck2brwsr
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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.AfterClass;
    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 public void getBytes() throws Exception {
    79         final String horse = "\u017dlu\u0165ou\u010dk\u00fd k\u016f\u0148";
    80         final String expected = StringSample.getBytes(horse);
    81         assertExec(
    82             "Bytes look simplar",
    83             StringSample.class, "getBytes__Ljava_lang_String_2Ljava_lang_String_2",
    84             expected, horse
    85         );
    86     }
    87 
    88     @Test(timeOut=10000) public void toStringConcatenation() throws Exception {
    89         assertExec(
    90             "Five executions should generate 5Hello World!",
    91             StringSample.class, "toStringTest__Ljava_lang_String_2I",
    92             "Hello World!5", 5
    93         );
    94     }
    95     @Test public void toStringConcatenationJava() throws Exception {
    96         assertEquals("Hello World!5", StringSample.toStringTest(5));
    97     }
    98     
    99     @Test(timeOut=10000) public void stringStringConcat() throws Exception {
   100         assertExec(
   101             "Composes strings OK",
   102             StringSample.class, "concatStrings__Ljava_lang_String_2",
   103             "Hello World!1" + "\\\n\r\t"
   104         );
   105     }
   106 
   107     @Test public void equalsAndSubstring() throws Exception {
   108         assertExec(
   109             "Composes are OK",
   110             StringSample.class, "equalToHello__ZII",
   111             true, 0, 5
   112         );
   113     }
   114     @Test public void replaceChars() throws Exception {
   115         assertExec(
   116             "Can replace slashes by underscores",
   117             StringSample.class, "replace__Ljava_lang_String_2Ljava_lang_String_2CC",
   118             "x_y_z", "x/y/z", '/', '_'
   119         );
   120     }
   121     @Test public void replaceIntChars() throws Exception {
   122         assertExec(
   123             "Can replace slashes by underscores",
   124             StringSample.class, "replace__Ljava_lang_String_2Ljava_lang_String_2CC",
   125             "x_y_z", "x/y/z", (int)'/', (int)'_'
   126         );
   127     }
   128 
   129     @Test public void insertBuilder() throws Exception {
   130         assertExec(
   131             "Can insert something into a buffer?",
   132             StringSample.class, "insertBuffer__Ljava_lang_String_2",
   133             "Ahojdo!"
   134         );
   135     }
   136     
   137     @Test public void compareHashCodeHi() throws Exception {
   138         String j = "Hi";
   139         int jh = StringSample.hashCode(j);
   140         assertExec(
   141             "Hashcode is the same " +jh,
   142             StringSample.class, "hashCode__ILjava_lang_String_2",
   143             Double.valueOf(jh), j
   144         );
   145     }
   146     @Test public void compareHashCode1() throws Exception {
   147         String j = "Hello Java!";
   148         int jh = StringSample.hashCode(j);
   149         assertExec(
   150             "Hashcode is the same " + jh,
   151             StringSample.class, "hashCode__ILjava_lang_String_2",
   152             Double.valueOf(jh), j
   153         );
   154     }
   155     @Test public void stringSwitch1() throws Exception {
   156         assertExec(
   157             "Get one",
   158             StringSample.class, "stringSwitch__ILjava_lang_String_2",
   159             Double.valueOf(1), "jedna"
   160         );
   161     }
   162     @Test public void stringSwitch2() throws Exception {
   163         assertExec(
   164             "Get two",
   165             StringSample.class, "stringSwitch__ILjava_lang_String_2",
   166             Double.valueOf(2), "dve"
   167         );
   168     }
   169     @Test public void stringSwitchDefault() throws Exception {
   170         assertExec(
   171             "Get -1",
   172             StringSample.class, "stringSwitch__ILjava_lang_String_2",
   173             Double.valueOf(-1), "none"
   174         );
   175     }
   176     
   177     @Test public void countAB() throws Exception {
   178         assertEquals(StringSample.countAB("Ahoj Bedo!"), 3, "Verify Java code is sane");
   179         assertExec(
   180             "One A and one B adds to 3",
   181             StringSample.class, "countAB__ILjava_lang_String_2",
   182             Double.valueOf(3), "Ahoj Bedo!"
   183         );
   184         
   185     }
   186 
   187     @Test public void compareStrings() throws Exception {
   188         int res = StringSample.compare("Saab", "Volvo");
   189         assertExec(
   190             "Saab finished sooner than Volvo",
   191             StringSample.class, "compare__ILjava_lang_String_2Ljava_lang_String_2",
   192             Double.valueOf(res), "Saab", "Volvo"
   193         );
   194         
   195     }
   196     
   197     @Test public void toStringOnJSArray() throws Exception {
   198         String exp = StringSample.toStringArray(false, true);
   199         
   200         assertExec(
   201             "Treated as Java Object array",
   202             StringSample.class, "toStringArray__Ljava_lang_String_2ZZ",
   203             exp, true, true
   204         );
   205     }
   206 
   207     @Test public void toStringOnRealArray() throws Exception {
   208         String exp = StringSample.toStringArray(false, true);
   209         
   210         assertExec(
   211             "Is Java Object array",
   212             StringSample.class, "toStringArray__Ljava_lang_String_2ZZ",
   213             exp, false, true
   214         );
   215     }
   216     
   217     @Test public void weirdUnicodeCharacters() throws Exception {
   218         String exp = StringSample.unicode();
   219         
   220         assertExec(
   221             "Unicode is OK",
   222             StringSample.class, "unicode__Ljava_lang_String_2",
   223             exp
   224         );
   225     }
   226 
   227     @Test public void valueOfOnJSArray() throws Exception {
   228         assertExec(
   229             "Treated as classical JavaScript array",
   230             StringSample.class, "toStringArray__Ljava_lang_String_2ZZ",
   231             "1,2", true, false
   232         );
   233     }
   234     
   235     private static TestVM code;
   236     
   237     @BeforeClass 
   238     public static void compileTheCode() throws Exception {
   239         code = TestVM.compileClass(
   240             "org/apidesign/vm4brwsr/StringSample",
   241             "java/lang/String"
   242         );
   243     }
   244     @AfterClass
   245     public static void releaseTheCode() {
   246         code = null;
   247     }
   248     
   249     private static void assertExec(String msg, 
   250         Class<?> clazz, String method, Object expRes, Object... args
   251     ) throws Exception {
   252         code.assertExec(msg, clazz, method, expRes, args);
   253     }
   254     
   255 }