1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.core; 18 19import com.ibm.icu4jni.text.RuleBasedNumberFormat; 20import com.ibm.icu4jni.text.RuleBasedNumberFormat.RBNFType; 21 22import junit.framework.TestCase; 23import android.test.suitebuilder.annotation.SmallTest; 24 25/** 26 * Some String tests. 27 */ 28public class StringTest extends TestCase { 29 private String germanSpelloutRule = "%alt-ones:" + 30 " -x: minus >>;" + 31 " x.x: << komma >>;" + 32 " null; eins; =%%main=;" + 33 "%%main:" + 34 " null; ein; zwei; drei; vier; f\u00fcnf; sechs; sieben; acht; neun;\n" + 35 " zehn; elf; zwu00f6lf; >>zehn;" + 36 " 20: [>>und]zwanzig;" + 37 " 30: [>>und]dreiu00dfig;" + 38 " 40: [>>und]vierzig;" + 39 " 50: [>>und]fu00fcnfzig;" + 40 " 60: [>>und]sechzig;" + 41 " 70: [>>und]siebzig;" + 42 " 80: [>>und]achtzig;" + 43 " 90: [>>und]neunzig;" + 44 " 100: hundert[>%alt-ones>];" + 45 " 200: <<hundert[>%alt-ones>];" + 46 " 1000: tausend[>%alt-ones>];" + 47 " 1100: tausendein[>%alt-ones>];" + 48 " 1200: tausend[>%alt-ones>];" + 49 " 2000: <<tausend[>%alt-ones>];"; 50 51 @SmallTest 52 public void testString() throws Exception { 53 String test = "0123456789"; 54 String test1 = new String("0123456789"); // different object 55 String test2 = new String("0123456780"); // different value 56 String offset = new String("xxx0123456789yyy"); 57 String sub = offset.substring(3, 13); 58 59 assertEquals(test, test); 60 assertEquals(test, test1); 61 assertFalse(test.equals(test2)); 62 63 assertEquals(0, test.compareTo(test1)); 64 assertTrue(test1.compareTo(test2) > 0); 65 assertTrue(test2.compareTo(test1) < 0); 66 67 /* compare string with a nonzero offset, in left/right side */ 68 assertEquals(0, test.compareTo(sub)); 69 assertEquals(0, sub.compareTo(test)); 70 assertEquals(test, sub); 71 assertEquals(sub, test); 72 /* same base, one is a substring */ 73 assertFalse(offset.equals(sub)); 74 assertFalse(sub.equals(offset)); 75 /* wrong class */ 76 assertFalse(test.equals(this)); 77 78 /* null ptr - throw */ 79 try { 80 test.compareTo(null); 81 fail("didn't get expected npe"); 82 } catch (NullPointerException npe) { 83 // expected 84 } 85 /* null ptr - ok */ 86 assertFalse(test.equals(null)); 87 88 test = test.substring(1); 89 assertEquals("123456789", test); 90 assertFalse(test.equals(test1)); 91 92 test = test.substring(1); 93 assertEquals("23456789", test); 94 95 test = test.substring(1); 96 assertEquals("3456789", test); 97 98 test = test.substring(1); 99 assertEquals("456789", test); 100 101 test = test.substring(3, 5); 102 assertEquals("78", test); 103 104 test = "this/is/a/path"; 105 String[] strings = test.split("/"); 106 assertEquals(4, strings.length); 107 108 assertEquals("this is a path", test.replaceAll("/", " ")); 109 assertEquals("this is a path", test.replace("/", " ")); 110 111 assertEquals(0, "abc".compareToIgnoreCase("ABC")); 112 assertTrue("abc".compareToIgnoreCase("DEF") < 0); 113 assertTrue("ABC".compareToIgnoreCase("def") < 0); 114 assertTrue("Now".compareTo("Mow") > 0); 115 assertTrue("Now".compareToIgnoreCase("Mow") > 0); 116 117 // RuleBasedNumberFormnat tests 118 RuleBasedNumberFormat format = new RuleBasedNumberFormat(); 119 format.open(RBNFType.SPELLOUT); 120 String result = format.format(15); 121 assertEquals("Expected spellout format: 'fifteen' but was " 122 + result, "fifteen", result); 123 format.close(); 124 125 format.open(RBNFType.DURATION); 126 result = format.format(15); 127 assertEquals("Expected spellout format: '15 sec.' but was " 128 + result, "15 sec.", result); 129 format.close(); 130 131 format.open(RBNFType.ORDINAL); 132 result = format.format(15); 133 assertEquals("Expected spellout format: '15th' but was " 134 + result, "15th", result); 135 format.close(); 136 format.open(germanSpelloutRule); 137 138 result = format.format(1323); 139 assertEquals("Expected spellout format: 'tausenddrei" + 140 "hundertdreiundzwanzig' but was " + result, "tausend" + 141 "dreihundertdreiundzwanzig", result); 142 int res = format.parse("tausenddreihundertdreiundzwanzig") 143 .intValue(); 144 assertEquals("Expected spellout format: 'tausend" + 145 "dreihundertdreiundzwanzig' but was " + res , 1323, res); 146 format.close(); 147 } 148} 149 150