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.BeforeClass;
26 * @author Jaroslav Tulach <jtulach@netbeans.org>
28 public class StringTest {
29 @Test public void firstChar() throws Exception {
31 "First char in Hello is H",
32 StringSample.class, "sayHello__CI",
37 @Test public void fromChars() throws Exception {
39 "First char in Hello is ABC",
40 StringSample.class, "fromChars__Ljava_lang_String_2CCC",
45 @Test public void concatChars() throws Exception {
47 "Composing yields ABC",
48 StringSample.class, "chars__Ljava_lang_String_2CCC",
53 @Test public void concatCharsFromInts() throws Exception {
55 "Composing yields ABC",
56 StringSample.class, "charsFromNumbers__Ljava_lang_String_2",
61 @Test public void concatCharsFromChars() throws Exception {
63 "Composing yields ABC",
64 StringSample.class, "charsFromChars__Ljava_lang_String_2",
69 @Test public void instanceOfWorks() throws Exception {
72 StringSample.class, "isStringInstance__Z",
77 @Test public void getBytes() throws Exception {
78 final String horse = "\u017dlu\u0165ou\u010dk\u00fd k\u016f\u0148";
79 final String expected = StringSample.getBytes(horse);
82 StringSample.class, "getBytes__Ljava_lang_String_2Ljava_lang_String_2",
87 @Test(timeOut=10000) public void toStringConcatenation() throws Exception {
89 "Five executions should generate 5Hello World!",
90 StringSample.class, "toStringTest__Ljava_lang_String_2I",
94 @Test public void toStringConcatenationJava() throws Exception {
95 assertEquals("Hello World!5", StringSample.toStringTest(5));
98 @Test(timeOut=10000) public void stringStringConcat() throws Exception {
100 "Composes strings OK",
101 StringSample.class, "concatStrings__Ljava_lang_String_2",
102 "Hello World!1" + "\\\n\r\t"
106 @Test public void equalsAndSubstring() throws Exception {
109 StringSample.class, "equalToHello__ZII",
113 @Test public void replaceChars() throws Exception {
115 "Can replace slashes by underscores",
116 StringSample.class, "replace__Ljava_lang_String_2Ljava_lang_String_2CC",
117 "x_y_z", "x/y/z", '/', '_'
120 @Test public void replaceIntChars() throws Exception {
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)'_'
128 @Test public void insertBuilder() throws Exception {
130 "Can insert something into a buffer?",
131 StringSample.class, "insertBuffer__Ljava_lang_String_2",
136 @Test public void compareHashCodeHi() throws Exception {
138 int jh = StringSample.hashCode(j);
140 "Hashcode is the same " +jh,
141 StringSample.class, "hashCode__ILjava_lang_String_2",
142 Double.valueOf(jh), j
145 @Test public void compareHashCode1() throws Exception {
146 String j = "Hello Java!";
147 int jh = StringSample.hashCode(j);
149 "Hashcode is the same " + jh,
150 StringSample.class, "hashCode__ILjava_lang_String_2",
151 Double.valueOf(jh), j
154 @Test public void stringSwitch1() throws Exception {
157 StringSample.class, "stringSwitch__ILjava_lang_String_2",
158 Double.valueOf(1), "jedna"
161 @Test public void stringSwitch2() throws Exception {
164 StringSample.class, "stringSwitch__ILjava_lang_String_2",
165 Double.valueOf(2), "dve"
168 @Test public void stringSwitchDefault() throws Exception {
171 StringSample.class, "stringSwitch__ILjava_lang_String_2",
172 Double.valueOf(-1), "none"
176 @Test public void countAB() throws Exception {
177 assertEquals(StringSample.countAB("Ahoj Bedo!"), 3, "Verify Java code is sane");
179 "One A and one B adds to 3",
180 StringSample.class, "countAB__ILjava_lang_String_2",
181 Double.valueOf(3), "Ahoj Bedo!"
186 @Test public void compareStrings() throws Exception {
187 int res = StringSample.compare("Saab", "Volvo");
189 "Saab finished sooner than Volvo",
190 StringSample.class, "compare__ILjava_lang_String_2Ljava_lang_String_2",
191 Double.valueOf(res), "Saab", "Volvo"
196 private static TestVM code;
199 public void compileTheCode() throws Exception {
200 code = TestVM.compileClass(
201 "org/apidesign/vm4brwsr/StringSample",
206 private static void assertExec(String msg,
207 Class<?> clazz, String method, Object expRes, Object... args
209 code.assertExec(msg, clazz, method, expRes, args);