1 33 34 package edu.rice.cs.util; 35 36 import edu.rice.cs.drjava.DrJavaTestCase; 37 38 import edu.rice.cs.plt.tuple.Pair; 39 import java.io.PrintWriter ; 40 41 44 public class StringOpsTest extends DrJavaTestCase { 45 48 public void testReplace() { 49 String test = "aabbccdd"; 50 assertEquals("testReplace:", "aab12cdd", StringOps.replace(test, "bc", "12")); 51 test = "cabcabc"; 52 assertEquals("testReplace:", "cabc", StringOps.replace(test, "cabc", "c")); 53 } 54 55 58 public void testGetOffsetAndLength() { 59 String test = "123456789\n123456789\n123456789\n"; 60 61 Pair<Integer ,Integer > oAndL = StringOps.getOffsetAndLength(test, 1, 1, 1, 9); 64 assertEquals("testGetOffsetAndLength- offSet:", new Integer (0), oAndL.first()); 65 assertEquals("testGetOffsetAndLength- length:", new Integer (9), oAndL.second()); 66 67 oAndL = StringOps.getOffsetAndLength(test, 1, 1, 2, 3); 68 assertEquals("testGetOffsetAndLength- offSet:", new Integer (0), oAndL.first()); 69 assertEquals("testGetOffsetAndLength- length:", new Integer (13), oAndL.second()); 70 71 oAndL = StringOps.getOffsetAndLength(test, 1, 5, 2, 3); 72 assertEquals("testGetOffsetAndLength- offSet:", new Integer (4), oAndL.first()); 73 assertEquals("testGetOffsetAndLength- length:", new Integer (9), oAndL.second()); 74 75 oAndL = StringOps.getOffsetAndLength(test, 1, 1, 1, 1); 76 assertEquals("testGetOffsetAndLength- offSet:", new Integer (0), oAndL.first()); 77 assertEquals("testGetOffsetAndLength- length:", new Integer (1), oAndL.second()); 78 79 oAndL = StringOps.getOffsetAndLength(test, 3, 5, 3, 5); 80 assertEquals("testGetOffsetAndLength- offSet:", new Integer (24), oAndL.first()); 81 assertEquals("testGetOffsetAndLength- length:", new Integer (1), oAndL.second()); 82 83 oAndL = StringOps.getOffsetAndLength(test, 2, 3, 3, 6); 84 assertEquals("testGetOffsetAndLength- offSet:", new Integer (12), oAndL.first()); 85 assertEquals("testGetOffsetAndLength- length:", new Integer (14), oAndL.second()); 86 87 try { 88 StringOps.getOffsetAndLength(test, 3, 2, 2, 3); 89 fail("Should not have been able to compute offset where startRow > endRow"); 90 } 91 catch (IllegalArgumentException ex) { 92 } 94 95 try { 96 StringOps.getOffsetAndLength(test, 2, 4, 2, 3); 97 fail("Should not have been able to compute offset where start > end"); 98 } 99 catch (IllegalArgumentException ex) { 100 } 102 103 try { 104 StringOps.getOffsetAndLength(test, 4, 4, 5, 5); 105 fail("Should not have been able to compute offset where the\n" + 106 "given coordinates are not contained within the string"); 107 } 108 catch (IllegalArgumentException ex) { 109 } 111 112 try { 113 StringOps.getOffsetAndLength(test, 3, 4, 3, 12); 114 fail("Should not have been able to compute offset where the\n" + 115 "given coordinates are not contained within the string"); 116 } 117 catch (IllegalArgumentException ex) { 118 } 120 121 try { 122 StringOps.getOffsetAndLength(test, 2, 15, 3, 1); 123 fail("Should not have been able to compute offset where the\n" + 124 "given coordinates are not contained within the string"); 125 } 126 catch (IllegalArgumentException ex) { 127 } 129 } 130 131 134 public void testGetStackTrace() { 135 final String trace = "hello"; 136 Throwable t = new Throwable () { 137 public void printStackTrace(PrintWriter w) { 138 w.print(trace); 139 } 140 }; 141 assertEquals("Should have returned the correct stack trace!", trace, StringOps.getStackTrace(t)); 142 } 143 144 147 public void testConvertToLiteral() { 148 String toConvert = " a b c d"; 149 String expResult = "\" a b c d\""; 150 String actResult = StringOps.convertToLiteral(toConvert); 151 assertEquals("converting "+toConvert+" should yield "+ expResult, expResult, actResult); 152 153 toConvert = "\\ hello world \\"; 154 expResult = "\"\\\\ hello world \\\\\""; 155 actResult = StringOps.convertToLiteral(toConvert); 156 assertEquals("converting "+toConvert+" should yield "+ expResult, expResult, actResult); 157 158 toConvert = "\\\n\\n"; 159 expResult = "\"\\\\\\n\\\\n\""; 160 actResult = StringOps.convertToLiteral(toConvert); 161 assertEquals("converting "+toConvert+" should yield "+ expResult, expResult, actResult); 162 163 toConvert = "\\\"\t\\t"; 164 expResult = "\"\\\\\\\"\\t\\\\t\""; 165 actResult = StringOps.convertToLiteral(toConvert); 166 assertEquals("converting "+toConvert+" should yield "+ expResult, expResult, actResult); 167 168 toConvert = "\"\\\"\t\\n\n\\\n\""; 169 expResult = "\"\\\"\\\\\\\"\\t\\\\n\\n\\\\\\n\\\"\""; 170 actResult = StringOps.convertToLiteral(toConvert); 171 assertEquals("converting "+toConvert+" should yield "+ expResult, expResult, actResult); 172 173 toConvert = " "; 174 expResult = "\" \""; 175 actResult = StringOps.convertToLiteral(toConvert); 176 assertEquals("converting "+toConvert+" should yield "+ expResult, expResult, actResult); 177 } 178 179 private static class TestGetSimpleNameInner { 180 public static class Nested { 181 public static Class anonClass() { 182 java.awt.event.ActionListener l = new java.awt.event.ActionListener () { 183 public void actionPerformed(java.awt.event.ActionEvent e) { } 184 }; 185 return l.getClass(); 186 } 187 } 188 public class Inner { 189 public Class anonClass() { 190 java.awt.event.ActionListener l = new java.awt.event.ActionListener () { 191 public void actionPerformed(java.awt.event.ActionEvent e) { } 192 }; 193 return l.getClass(); 194 } 195 } 196 public Inner getInner() { 197 return new Inner(); 198 } 199 public static Class anonClass() { 200 java.awt.event.ActionListener l = new java.awt.event.ActionListener () { 201 public void actionPerformed(java.awt.event.ActionEvent e) { } 202 }; 203 return l.getClass(); 204 } 205 public static Lambda<Class , Object > getLambda() { 206 return new Lambda<Class , Object >() { 207 public Class apply(Object param) { 208 java.awt.event.ActionListener l = new java.awt.event.ActionListener () { 209 public void actionPerformed(java.awt.event.ActionEvent e) { } 210 }; 211 return l.getClass(); 212 } 213 }; 214 } 215 } 216 217 220 public void testGetSimpleName() { 221 String exp = "Integer"; 222 String act = StringOps.getSimpleName(java.lang.Integer .class); 223 assertEquals("Wrong simple name for java.lang.Integer, exp="+exp+", act="+act, 224 exp, 225 act); 226 227 exp = "TestGetSimpleNameInner"; 228 act = StringOps.getSimpleName(TestGetSimpleNameInner.class); 229 assertEquals("Wrong simple name for TestGetSimpleNameInner, exp="+exp+", act="+act, 230 exp, 231 act); 232 233 exp = "Nested"; 234 act = StringOps.getSimpleName(TestGetSimpleNameInner.Nested.class); 235 assertEquals("Wrong simple name for TestGetSimpleNameInner.Nested, exp="+exp+", act="+act, 236 exp, 237 act); 238 239 exp = "Inner"; 240 act = StringOps.getSimpleName(TestGetSimpleNameInner.Inner.class); 241 assertEquals("Wrong simple name for TestGetSimpleNameInner.Inner, exp="+exp+", act="+act, 242 exp, 243 act); 244 245 java.awt.event.ActionListener l = new java.awt.event.ActionListener () { 246 public void actionPerformed(java.awt.event.ActionEvent e) { } 247 }; 248 249 exp = ""; 250 act = StringOps.getSimpleName(l.getClass()); 251 assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act, 252 exp, 253 act); 254 255 exp = ""; 256 act = StringOps.getSimpleName(TestGetSimpleNameInner.anonClass()); 257 assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act, 258 exp, 259 act); 260 261 exp = ""; 262 act = StringOps.getSimpleName(TestGetSimpleNameInner.Nested.anonClass()); 263 assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act, 264 exp, 265 act); 266 267 exp = ""; 268 act = StringOps.getSimpleName((new TestGetSimpleNameInner()).getInner().anonClass()); 269 assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act, 270 exp, 271 act); 272 273 exp = ""; 274 act = StringOps.getSimpleName(TestGetSimpleNameInner.getLambda().apply(null)); 275 assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act, 276 exp, 277 act); 278 } 279 280 283 343 public void testToStringLong() { 344 long[] a = null; 345 assertEquals("null", StringOps.toString(a)); 346 assertEquals("[]", StringOps.toString(new long[] {})); 347 assertEquals("[1]", StringOps.toString(new long[] {1})); 348 assertEquals("[1, 2]", StringOps.toString(new long[] {1, 2})); 349 } 350 351 public void testToStringInt() { 352 int[] a = null; 353 assertEquals("null", StringOps.toString(a)); 354 assertEquals("[]", StringOps.toString(new int[] {})); 355 assertEquals("[1]", StringOps.toString(new int[] {1})); 356 assertEquals("[1, 2]", StringOps.toString(new int[] {1, 2})); 357 } 358 359 public void testToStringShort() { 360 short[] a = null; 361 assertEquals("null", StringOps.toString(a)); 362 assertEquals("[]", StringOps.toString(new short[] {})); 363 assertEquals("[1]", StringOps.toString(new short[] {1})); 364 assertEquals("[1, 2]", StringOps.toString(new short[] {1, 2})); 365 } 366 367 public void testToStringChar() { 368 char[] a = null; 369 assertEquals("null", StringOps.toString(a)); 370 assertEquals("[]", StringOps.toString(new char[] {})); 371 assertEquals("[a]", StringOps.toString(new char[] {'a'})); 372 assertEquals("[a, b]", StringOps.toString(new char[] {'a', 'b'})); 373 } 374 375 public void testToStringByte() { 376 byte[] a = null; 377 assertEquals("null", StringOps.toString(a)); 378 assertEquals("[]", StringOps.toString(new byte[] {})); 379 assertEquals("[1]", StringOps.toString(new byte[] {1})); 380 assertEquals("[1, 2]", StringOps.toString(new byte[] {1, 2})); 381 } 382 383 public void testToStringBoolean() { 384 boolean[] a = null; 385 assertEquals("null", StringOps.toString(a)); 386 assertEquals("[]", StringOps.toString(new boolean[] {})); 387 assertEquals("[true]", StringOps.toString(new boolean[] {true})); 388 assertEquals("[true, false]", StringOps.toString(new boolean[] {true, false})); 389 } 390 391 public void testToStringFloat() { 392 float[] a = null; 393 assertEquals("null", StringOps.toString(a)); 394 assertEquals("[]", StringOps.toString(new float[] {})); 395 assertEquals("[1.23]", StringOps.toString(new float[] {1.23f})); 396 assertEquals("[1.23, 4.56]", StringOps.toString(new float[] {1.23f, 4.56f})); 397 } 398 399 public void testToStringDouble() { 400 double[] a = null; 401 assertEquals("null", StringOps.toString(a)); 402 assertEquals("[]", StringOps.toString(new double[] {})); 403 assertEquals("[1.23]", StringOps.toString(new double[] {1.23})); 404 assertEquals("[1.23, 4.56]", StringOps.toString(new double[] {1.23, 4.56})); 405 } 406 407 public void testToStringObject() { 408 Object [] a = null; 409 assertEquals("null", StringOps.toString(a)); 410 assertEquals("[]", StringOps.toString(new Object [] {})); 411 assertEquals("[123]", StringOps.toString(new Object [] {"123"})); 412 assertEquals("[123, 123]", StringOps.toString(new Object [] {"123", new Integer (123)})); 413 } 414 415 public void testMemSizeToString() { 416 assertEquals("<1024", "500 bytes", StringOps.memSizeToString(500)); 417 assertEquals("1KB", "1 kilobyte", StringOps.memSizeToString(1024)); 418 assertEquals("2KB", "2 kilobytes", StringOps.memSizeToString(1024*2)); 419 assertEquals("1.5KB", "1.50 kilobytes", StringOps.memSizeToString((long)(1024*1.5))); 420 assertEquals("1MB", "1 megabyte", StringOps.memSizeToString((1024*1024))); 421 assertEquals("2MB", "2 megabytes", StringOps.memSizeToString((1024*1024*2))); 422 assertEquals("1.1MB", "1.10 megabytes", StringOps.memSizeToString((long)(1024*1024*1.1))); 423 assertEquals("1GB", "1 gigabyte", StringOps.memSizeToString((1024*1024*1024))); 424 assertEquals("1.25GB", "1.25 gigabytes", StringOps.memSizeToString((long)(1024*1024*1024*1.25))); 425 } 426 } 427 | Popular Tags |