1 32 package net.sf.retrotranslator.runtime.java.lang; 33 34 import java.util.Locale ; 35 import junit.framework.TestCase; 36 37 40 public class _StringTestCase extends TestCase { 41 42 public void testConvertConstructorArguments() throws Exception { 43 assertEquals("b\uD834\uDD1Ec", new String (new int[]{'a', 'b', 0x1D11E, 'c', 'd'}, 1, 3)); 44 try { 45 new String (new int[]{-1, 'b'}, 0, 1); 46 fail(); 47 } catch (IllegalArgumentException e) { 48 } 50 try { 51 new String (new int[]{'a', 'b'}, -1, 1); 52 fail(); 53 } catch (IndexOutOfBoundsException e) { 54 } 56 try { 57 new String (new int[]{'a', 'b'}, 1, -1); 58 fail(); 59 } catch (IndexOutOfBoundsException e) { 60 } 62 try { 63 new String (new int[]{'a', 'b'}, 1, 2); 64 fail(); 65 } catch (IndexOutOfBoundsException e) { 66 } 68 } 69 70 public void testCodePointAt() throws Exception { 71 assertEquals('b', "ab".codePointAt(1)); 72 assertEquals(0x1D11E, "b\uD834\uDD1Ec".codePointAt(1)); 73 assertEquals(0xD834, "b\uD834".codePointAt(1)); 74 try { 75 "abc".codePointAt(-1); 76 fail(); 77 } catch (IndexOutOfBoundsException e) { 78 } 80 try { 81 "abc".codePointAt(3); 82 fail(); 83 } catch (IndexOutOfBoundsException e) { 84 } 86 } 87 88 public void testCodePointBefore() throws Exception { 89 assertEquals('a', "ab".codePointBefore(1)); 90 assertEquals(0x1D11E, "b\uD834\uDD1Ec".codePointBefore(3)); 91 assertEquals(0xDD1E, "\uDD1E".codePointBefore(1)); 92 try { 93 "abc".codePointBefore(0); 94 fail(); 95 } catch (IndexOutOfBoundsException e) { 96 } 98 try { 99 "abc".codePointBefore(4); 100 fail(); 101 } catch (IndexOutOfBoundsException e) { 102 } 104 } 105 106 public void testCodePointCount() throws Exception { 107 assertEquals(2, "abcd".codePointCount(1, 3)); 108 assertEquals(3, "b\uD834\uDD1Ec".codePointCount(0, 4)); 109 assertEquals(2, "b\uD834\uDD1Ec".codePointCount(0, 2)); 110 assertEquals(2, "b\uD834".codePointCount(0, 2)); 111 try { 112 "abc".codePointCount(-1, 1); 113 fail(); 114 } catch (IndexOutOfBoundsException e) { 115 } 117 try { 118 "abc".codePointCount(1, 0); 119 fail(); 120 } catch (IndexOutOfBoundsException e) { 121 } 123 try { 124 "abc".codePointCount(5, 5); 125 fail(); 126 } catch (IndexOutOfBoundsException e) { 127 } 129 try { 130 "abc".codePointCount(1, 5); 131 fail(); 132 } catch (IndexOutOfBoundsException e) { 133 } 135 } 136 137 public void testContains() throws Exception { 138 assertTrue("abcd".contains("bc")); 139 assertFalse("abcd".contains("xy")); 140 } 141 142 public void testContentEquals() throws Exception { 143 assertTrue("abcd".contentEquals("abcd")); 144 assertFalse("abcd".contentEquals("xy")); 145 } 146 147 public void testFormat() throws Exception { 148 assertEquals("1234", String.format("%d", 1234)); 149 assertEquals("1.234", String.format(Locale.GERMAN, "%,d", 1234)); 150 } 151 152 public void testOffsetByCodePoints() throws Exception { 153 assertEquals(3, "abc".offsetByCodePoints(1, 2)); 154 assertEquals(1, "abc".offsetByCodePoints(1, 0)); 155 assertEquals(3, "b\uD834\uDD1Ec".offsetByCodePoints(0, 2)); 156 try { 157 "abc".offsetByCodePoints(-1, 1); 158 fail(); 159 } catch (IndexOutOfBoundsException e) { 160 } 162 try { 163 "abc".offsetByCodePoints(10, 0); 164 fail(); 165 } catch (IndexOutOfBoundsException e) { 166 } 168 try { 169 "abc".offsetByCodePoints(0, 5); 170 fail(); 171 } catch (IndexOutOfBoundsException e) { 172 } 174 try { 175 "abc".offsetByCodePoints(2, -5); 176 fail(); 177 } catch (IndexOutOfBoundsException e) { 178 } 180 } 181 182 public void testReplace() throws Exception { 183 assertEquals("axydxy", "abcdbc".replace("bc", "xy")); 184 assertEquals("abc", "abc*".replace("*", "")); 185 assertEquals("XaXbXcX", "abc".replace("", "X")); 186 assertEquals("ac", "abc".replace("b", "")); 187 assertEquals("abc", "abc".replace("", "")); 188 } 189 190 } | Popular Tags |