rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java
changeset 1723 3a1f262311cf
parent 1402 e896bc687984
child 1787 ea12a3bb4b33
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java	Wed Nov 19 19:32:00 2014 +0100
     1.3 @@ -0,0 +1,209 @@
     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.bck2brwsr.tck;
    1.22 +
    1.23 +import java.io.UnsupportedEncodingException;
    1.24 +import java.net.MalformedURLException;
    1.25 +import java.net.URISyntaxException;
    1.26 +import java.net.URL;
    1.27 +import java.util.Locale;
    1.28 +import org.apidesign.bck2brwsr.vmtest.Compare;
    1.29 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    1.30 +import org.testng.annotations.Factory;
    1.31 +
    1.32 +/**
    1.33 + *
    1.34 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.35 + */
    1.36 +public class CompareStringsTest {
    1.37 +    @Compare public String firstChar() {
    1.38 +        return "" + ("Hello".toCharArray()[0]);
    1.39 +    }
    1.40 +    
    1.41 +    @Compare public String classCast() {
    1.42 +        Object o = firstChar();
    1.43 +        return String.class.cast(o);
    1.44 +    }
    1.45 +
    1.46 +    @Compare public String classCastThrown() {
    1.47 +        Object o = null;
    1.48 +        return String.class.cast(o);
    1.49 +    }
    1.50 +    
    1.51 +    @Compare public boolean equalToNull() {
    1.52 +        return "Ahoj".equals(null);
    1.53 +    }
    1.54 +    
    1.55 +    @Compare public boolean internIsSame() {
    1.56 +        return new String("Ahoj").intern() == another();
    1.57 +    }
    1.58 +    
    1.59 +    private static String another() {
    1.60 +        return new String("Ahoj").intern();
    1.61 +    }
    1.62 +    
    1.63 +    @Compare public int highByteLenght() {
    1.64 +        byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
    1.65 +        return new String(arr, 0).length();
    1.66 +    }
    1.67 +    
    1.68 +    @Compare public String highByte() {
    1.69 +        byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
    1.70 +        StringBuilder sb = new StringBuilder();
    1.71 +        sb.append("pref:");
    1.72 +        sb.append(new String(arr, 0));
    1.73 +        return sb.toString();
    1.74 +    }
    1.75 +    
    1.76 +    @Compare public static Object compareURLs() throws MalformedURLException {
    1.77 +        return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString();
    1.78 +    }
    1.79 +
    1.80 +    @Compare public static Object compareURLsViaURIs() throws Exception {
    1.81 +        return new URL("http://apidesign.org:8080/wiki/").toURI().toString();
    1.82 +    }
    1.83 +    
    1.84 +    @Compare public String deleteLastTwoCharacters() {
    1.85 +        StringBuilder sb = new StringBuilder();
    1.86 +        sb.append("453.0");
    1.87 +        if (sb.toString().endsWith(".0")) {
    1.88 +            final int l = sb.length();
    1.89 +            sb.delete(l - 2, l);
    1.90 +        }
    1.91 +        return sb.toString().toString();
    1.92 +    }
    1.93 +    
    1.94 +    @Compare public String nameOfStringClass() throws Exception {
    1.95 +        return Class.forName("java.lang.String").getName();
    1.96 +    }
    1.97 +    @Compare public String nameOfArrayClass() throws Exception {
    1.98 +        return Class.forName("org.apidesign.bck2brwsr.tck.CompareHashTest").getName();
    1.99 +    }
   1.100 +    
   1.101 +    @Compare public String lowerHello() {
   1.102 +        return "HeLlO".toLowerCase();
   1.103 +    }
   1.104 +    
   1.105 +    @Compare public String lowerA() {
   1.106 +        return String.valueOf(Character.toLowerCase('A')).toString();
   1.107 +    }
   1.108 +    @Compare public String upperHello() {
   1.109 +        return "hello".toUpperCase();
   1.110 +    }
   1.111 +    
   1.112 +    @Compare public String upperA() {
   1.113 +        return String.valueOf(Character.toUpperCase('a')).toString();
   1.114 +    }
   1.115 +    
   1.116 +    @Compare public boolean matchRegExp() throws Exception {
   1.117 +        return "58038503".matches("\\d*");
   1.118 +    }
   1.119 +
   1.120 +    @Compare public boolean doesNotMatchRegExp() throws Exception {
   1.121 +        return "58038503GH".matches("\\d*");
   1.122 +    }
   1.123 +
   1.124 +    @Compare public boolean doesNotMatchRegExpFully() throws Exception {
   1.125 +        return "Hello".matches("Hell");
   1.126 +    }
   1.127 +    
   1.128 +    @Compare public String emptyCharArray() {
   1.129 +        char[] arr = new char[10];
   1.130 +        return new String(arr);
   1.131 +    }
   1.132 +    
   1.133 +    @Compare public String variousCharacterTests() throws Exception {
   1.134 +        StringBuilder sb = new StringBuilder();
   1.135 +        
   1.136 +        sb.append(Character.isUpperCase('a'));
   1.137 +        sb.append(Character.isUpperCase('A'));
   1.138 +        sb.append(Character.isLowerCase('a'));
   1.139 +        sb.append(Character.isLowerCase('A'));
   1.140 +        
   1.141 +        sb.append(Character.isLetter('A'));
   1.142 +        sb.append(Character.isLetterOrDigit('9'));
   1.143 +        sb.append(Character.isLetterOrDigit('A'));
   1.144 +        sb.append(Character.isLetter('0'));
   1.145 +        
   1.146 +        return sb.toString().toString();
   1.147 +    }
   1.148 +        
   1.149 +    @Compare
   1.150 +    public String nullFieldInitialized() {
   1.151 +        NullField nf = new NullField();
   1.152 +        return ("" + nf.name).toString();
   1.153 +    }
   1.154 +    @Compare
   1.155 +    public String toUTFString() throws UnsupportedEncodingException {
   1.156 +        byte[] arr = {
   1.157 +            (byte) -59, (byte) -67, (byte) 108, (byte) 117, (byte) -59, (byte) -91,
   1.158 +            (byte) 111, (byte) 117, (byte) -60, (byte) -115, (byte) 107, (byte) -61,
   1.159 +            (byte) -67, (byte) 32, (byte) 107, (byte) -59, (byte) -81, (byte) -59,
   1.160 +            (byte) -120
   1.161 +        };
   1.162 +        return new String(arr, "utf-8");
   1.163 +    }
   1.164 +
   1.165 +    @Compare
   1.166 +    public int stringToBytesLenght() throws UnsupportedEncodingException {
   1.167 +        return "\u017dlu\u0165ou\u010dk\u00fd k\u016f\u0148".getBytes("utf8").length;
   1.168 +    }
   1.169 +    
   1.170 +    @Compare public String replaceSeq() {
   1.171 +        return "Hello World.".replace(".", "!");
   1.172 +    }
   1.173 +    @Compare public String replaceSeqAll() {
   1.174 +        return "Hello World! Hello World.".replace("World", "Jarda");
   1.175 +    }
   1.176 +    @Compare public String replaceSeqAA() {
   1.177 +        String res = "aaa".replace("aa", "b");
   1.178 +        assert res.equals("ba") : "Expecting ba: " + res;
   1.179 +        return res;
   1.180 +    }
   1.181 +    
   1.182 +    @Compare public String localeUS() {
   1.183 +        return Locale.US.toString();
   1.184 +    }
   1.185 +    
   1.186 +    @Compare public String localeFrench() {
   1.187 +        return Locale.FRENCH.toString();
   1.188 +    }
   1.189 +    
   1.190 +    
   1.191 +    @Compare public String formatSimple() {
   1.192 +        return String.format((Locale)null, "Hello %s!", "World");
   1.193 +    }
   1.194 +
   1.195 +    @Compare public String replaceWithItself() {
   1.196 +        return "org.apidesign.bck2brwsr.core.JavaScriptBody".replace(".", "\\.");
   1.197 +    }
   1.198 +    
   1.199 +    @Compare public boolean matchWithComplicatedRegExp() {
   1.200 +        return "Activates this model instance.".matches("(?sm).*^\\s*@deprecated( |$).*");
   1.201 +    }
   1.202 +    
   1.203 +    @Factory
   1.204 +    public static Object[] create() {
   1.205 +        return VMTest.create(CompareStringsTest.class);
   1.206 +    }
   1.207 +
   1.208 +    private static final class NullField {
   1.209 +
   1.210 +        String name;
   1.211 +    }
   1.212 +}