2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.vm4brwsr;
20 import org.testng.annotations.Test;
21 import static org.testng.Assert.*;
22 import org.testng.annotations.AfterClass;
23 import org.testng.annotations.BeforeClass;
27 * @author Jaroslav Tulach <jtulach@netbeans.org>
29 public class StringTest {
30 @Test public void firstChar() throws Exception {
32 "First char in Hello is H",
33 StringSample.class, "sayHello__CI",
38 @Test public void fromChars() throws Exception {
40 "First char in Hello is ABC",
41 StringSample.class, "fromChars__Ljava_lang_String_2CCC",
46 @Test public void concatChars() throws Exception {
48 "Composing yields ABC",
49 StringSample.class, "chars__Ljava_lang_String_2CCC",
54 @Test public void concatCharsFromInts() throws Exception {
56 "Composing yields ABC",
57 StringSample.class, "charsFromNumbers__Ljava_lang_String_2",
62 @Test public void concatCharsFromChars() throws Exception {
64 "Composing yields ABC",
65 StringSample.class, "charsFromChars__Ljava_lang_String_2",
70 @Test public void instanceOfWorks() throws Exception {
73 StringSample.class, "isStringInstance__Z",
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);
83 StringSample.class, "getBytes__Ljava_lang_String_2Ljava_lang_String_2",
88 @Test(timeOut=10000) public void toStringConcatenation() throws Exception {
90 "Five executions should generate 5Hello World!",
91 StringSample.class, "toStringTest__Ljava_lang_String_2I",
95 @Test public void toStringConcatenationJava() throws Exception {
96 assertEquals("Hello World!5", StringSample.toStringTest(5));
99 @Test(timeOut=10000) public void stringStringConcat() throws Exception {
101 "Composes strings OK",
102 StringSample.class, "concatStrings__Ljava_lang_String_2",
103 "Hello World!1" + "\\\n\r\t"
107 @Test public void equalsAndSubstring() throws Exception {
110 StringSample.class, "equalToHello__ZII",
114 @Test public void replaceChars() throws Exception {
116 "Can replace slashes by underscores",
117 StringSample.class, "replace__Ljava_lang_String_2Ljava_lang_String_2CC",
118 "x_y_z", "x/y/z", '/', '_'
121 @Test public void replaceIntChars() throws Exception {
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)'_'
129 @Test public void insertBuilder() throws Exception {
131 "Can insert something into a buffer?",
132 StringSample.class, "insertBuffer__Ljava_lang_String_2",
137 @Test public void compareHashCodeHi() throws Exception {
139 int jh = StringSample.hashCode(j);
141 "Hashcode is the same " +jh,
142 StringSample.class, "hashCode__ILjava_lang_String_2",
143 Double.valueOf(jh), j
146 @Test public void compareHashCode1() throws Exception {
147 String j = "Hello Java!";
148 int jh = StringSample.hashCode(j);
150 "Hashcode is the same " + jh,
151 StringSample.class, "hashCode__ILjava_lang_String_2",
152 Double.valueOf(jh), j
155 @Test public void stringSwitch1() throws Exception {
158 StringSample.class, "stringSwitch__ILjava_lang_String_2",
159 Double.valueOf(1), "jedna"
162 @Test public void stringSwitch2() throws Exception {
165 StringSample.class, "stringSwitch__ILjava_lang_String_2",
166 Double.valueOf(2), "dve"
169 @Test public void stringSwitchDefault() throws Exception {
172 StringSample.class, "stringSwitch__ILjava_lang_String_2",
173 Double.valueOf(-1), "none"
177 @Test public void countAB() throws Exception {
178 assertEquals(StringSample.countAB("Ahoj Bedo!"), 3, "Verify Java code is sane");
180 "One A and one B adds to 3",
181 StringSample.class, "countAB__ILjava_lang_String_2",
182 Double.valueOf(3), "Ahoj Bedo!"
187 @Test public void compareStrings() throws Exception {
188 int res = StringSample.compare("Saab", "Volvo");
190 "Saab finished sooner than Volvo",
191 StringSample.class, "compare__ILjava_lang_String_2Ljava_lang_String_2",
192 Double.valueOf(res), "Saab", "Volvo"
197 @Test public void toStringOnJSArray() throws Exception {
198 String exp = StringSample.toStringArray(false, true);
201 "Treated as Java Object array",
202 StringSample.class, "toStringArray__Ljava_lang_String_2ZZ",
207 @Test public void toStringOnRealArray() throws Exception {
208 String exp = StringSample.toStringArray(false, true);
211 "Is Java Object array",
212 StringSample.class, "toStringArray__Ljava_lang_String_2ZZ",
217 @Test public void weirdUnicodeCharacters() throws Exception {
218 String exp = StringSample.unicode();
222 StringSample.class, "unicode__Ljava_lang_String_2",
227 @Test public void valueOfOnJSArray() throws Exception {
229 "Treated as classical JavaScript array",
230 StringSample.class, "toStringArray__Ljava_lang_String_2ZZ",
235 private static TestVM code;
238 public static void compileTheCode() throws Exception {
239 code = TestVM.compileClass(
240 "org/apidesign/vm4brwsr/StringSample",
245 public static void releaseTheCode() {
249 private static void assertExec(String msg,
250 Class<?> clazz, String method, Object expRes, Object... args
252 code.assertExec(msg, clazz, method, expRes, args);