1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,212 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.vm4brwsr;
1.22 +
1.23 +import org.testng.annotations.Test;
1.24 +import static org.testng.Assert.*;
1.25 +import org.testng.annotations.BeforeClass;
1.26 +
1.27 +/**
1.28 + *
1.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.30 + */
1.31 +public class StringTest {
1.32 + @Test public void firstChar() throws Exception {
1.33 + assertExec(
1.34 + "First char in Hello is H",
1.35 + StringSample.class, "sayHello__CI",
1.36 + 72, 0
1.37 + );
1.38 + }
1.39 +
1.40 + @Test public void fromChars() throws Exception {
1.41 + assertExec(
1.42 + "First char in Hello is ABC",
1.43 + StringSample.class, "fromChars__Ljava_lang_String_2CCC",
1.44 + "ABC", 'A', 'B', 'C'
1.45 + );
1.46 + }
1.47 +
1.48 + @Test public void concatChars() throws Exception {
1.49 + assertExec(
1.50 + "Composing yields ABC",
1.51 + StringSample.class, "chars__Ljava_lang_String_2CCC",
1.52 + "ABC", 'A', 'B', 'C'
1.53 + );
1.54 + }
1.55 +
1.56 + @Test public void concatCharsFromInts() throws Exception {
1.57 + assertExec(
1.58 + "Composing yields ABC",
1.59 + StringSample.class, "charsFromNumbers__Ljava_lang_String_2",
1.60 + "ABC"
1.61 + );
1.62 + }
1.63 +
1.64 + @Test public void concatCharsFromChars() throws Exception {
1.65 + assertExec(
1.66 + "Composing yields ABC",
1.67 + StringSample.class, "charsFromChars__Ljava_lang_String_2",
1.68 + "ABC"
1.69 + );
1.70 + }
1.71 +
1.72 + @Test public void instanceOfWorks() throws Exception {
1.73 + assertExec(
1.74 + "It is string",
1.75 + StringSample.class, "isStringInstance__Z",
1.76 + Double.valueOf(1.0)
1.77 + );
1.78 + }
1.79 +
1.80 + @Test public void getBytes() throws Exception {
1.81 + final String horse = "\u017dlu\u0165ou\u010dk\u00fd k\u016f\u0148";
1.82 + final String expected = StringSample.getBytes(horse);
1.83 + assertExec(
1.84 + "Bytes look simplar",
1.85 + StringSample.class, "getBytes__Ljava_lang_String_2Ljava_lang_String_2",
1.86 + expected, horse
1.87 + );
1.88 + }
1.89 +
1.90 + @Test(timeOut=10000) public void toStringConcatenation() throws Exception {
1.91 + assertExec(
1.92 + "Five executions should generate 5Hello World!",
1.93 + StringSample.class, "toStringTest__Ljava_lang_String_2I",
1.94 + "Hello World!5", 5
1.95 + );
1.96 + }
1.97 + @Test public void toStringConcatenationJava() throws Exception {
1.98 + assertEquals("Hello World!5", StringSample.toStringTest(5));
1.99 + }
1.100 +
1.101 + @Test(timeOut=10000) public void stringStringConcat() throws Exception {
1.102 + assertExec(
1.103 + "Composes strings OK",
1.104 + StringSample.class, "concatStrings__Ljava_lang_String_2",
1.105 + "Hello World!1" + "\\\n\r\t"
1.106 + );
1.107 + }
1.108 +
1.109 + @Test public void equalsAndSubstring() throws Exception {
1.110 + assertExec(
1.111 + "Composes are OK",
1.112 + StringSample.class, "equalToHello__ZII",
1.113 + true, 0, 5
1.114 + );
1.115 + }
1.116 + @Test public void replaceChars() throws Exception {
1.117 + assertExec(
1.118 + "Can replace slashes by underscores",
1.119 + StringSample.class, "replace__Ljava_lang_String_2Ljava_lang_String_2CC",
1.120 + "x_y_z", "x/y/z", '/', '_'
1.121 + );
1.122 + }
1.123 + @Test public void replaceIntChars() throws Exception {
1.124 + assertExec(
1.125 + "Can replace slashes by underscores",
1.126 + StringSample.class, "replace__Ljava_lang_String_2Ljava_lang_String_2CC",
1.127 + "x_y_z", "x/y/z", (int)'/', (int)'_'
1.128 + );
1.129 + }
1.130 +
1.131 + @Test public void insertBuilder() throws Exception {
1.132 + assertExec(
1.133 + "Can insert something into a buffer?",
1.134 + StringSample.class, "insertBuffer__Ljava_lang_String_2",
1.135 + "Ahojdo!"
1.136 + );
1.137 + }
1.138 +
1.139 + @Test public void compareHashCodeHi() throws Exception {
1.140 + String j = "Hi";
1.141 + int jh = StringSample.hashCode(j);
1.142 + assertExec(
1.143 + "Hashcode is the same " +jh,
1.144 + StringSample.class, "hashCode__ILjava_lang_String_2",
1.145 + Double.valueOf(jh), j
1.146 + );
1.147 + }
1.148 + @Test public void compareHashCode1() throws Exception {
1.149 + String j = "Hello Java!";
1.150 + int jh = StringSample.hashCode(j);
1.151 + assertExec(
1.152 + "Hashcode is the same " + jh,
1.153 + StringSample.class, "hashCode__ILjava_lang_String_2",
1.154 + Double.valueOf(jh), j
1.155 + );
1.156 + }
1.157 + @Test public void stringSwitch1() throws Exception {
1.158 + assertExec(
1.159 + "Get one",
1.160 + StringSample.class, "stringSwitch__ILjava_lang_String_2",
1.161 + Double.valueOf(1), "jedna"
1.162 + );
1.163 + }
1.164 + @Test public void stringSwitch2() throws Exception {
1.165 + assertExec(
1.166 + "Get two",
1.167 + StringSample.class, "stringSwitch__ILjava_lang_String_2",
1.168 + Double.valueOf(2), "dve"
1.169 + );
1.170 + }
1.171 + @Test public void stringSwitchDefault() throws Exception {
1.172 + assertExec(
1.173 + "Get -1",
1.174 + StringSample.class, "stringSwitch__ILjava_lang_String_2",
1.175 + Double.valueOf(-1), "none"
1.176 + );
1.177 + }
1.178 +
1.179 + @Test public void countAB() throws Exception {
1.180 + assertEquals(StringSample.countAB("Ahoj Bedo!"), 3, "Verify Java code is sane");
1.181 + assertExec(
1.182 + "One A and one B adds to 3",
1.183 + StringSample.class, "countAB__ILjava_lang_String_2",
1.184 + Double.valueOf(3), "Ahoj Bedo!"
1.185 + );
1.186 +
1.187 + }
1.188 +
1.189 + @Test public void compareStrings() throws Exception {
1.190 + int res = StringSample.compare("Saab", "Volvo");
1.191 + assertExec(
1.192 + "Saab finished sooner than Volvo",
1.193 + StringSample.class, "compare__ILjava_lang_String_2Ljava_lang_String_2",
1.194 + Double.valueOf(res), "Saab", "Volvo"
1.195 + );
1.196 +
1.197 + }
1.198 +
1.199 + private static TestVM code;
1.200 +
1.201 + @BeforeClass
1.202 + public void compileTheCode() throws Exception {
1.203 + code = TestVM.compileClass(
1.204 + "org/apidesign/vm4brwsr/StringSample",
1.205 + "java/lang/String"
1.206 + );
1.207 + }
1.208 +
1.209 + private static void assertExec(String msg,
1.210 + Class<?> clazz, String method, Object expRes, Object... args
1.211 + ) throws Exception {
1.212 + code.assertExec(msg, clazz, method, expRes, args);
1.213 + }
1.214 +
1.215 +}