1 19 20 package org.netbeans.lib.editor.util; 21 22 import java.util.Random ; 23 import junit.framework.TestCase; 24 25 public class CharSequenceUtilitiesTest extends TestCase { 26 27 private static final int CHARS_LENGTH = 1000; 28 private static final int SUBSTR_LENGTH = 100; 29 private static final Random rnd = new Random (0); 30 31 public CharSequenceUtilitiesTest(String testName) { 32 super(testName); 33 } 34 35 public void testCharSequence() { 36 char[] chars = new char[CHARS_LENGTH]; 37 char[] chars_2 = new char[CHARS_LENGTH]; 38 generateChars(chars); 39 generateChars(chars_2); 40 String string = new String (chars); 41 String string_2 = new String (chars_2); 42 43 assertTrue(CharSequenceUtilities.textEquals(string, string)); 45 String s = new String (chars); 46 assertTrue(CharSequenceUtilities.textEquals(string, s)); 47 assertTrue(CharSequenceUtilities.textEquals(string, string_2) == string.equals(string_2)); 48 49 assertTrue(CharSequenceUtilities.toString(string).equals(string)); 51 52 try { 53 CharSequenceUtilities.toString(string, -1, 0); 54 assertTrue(false); 55 } catch (IndexOutOfBoundsException e) { 56 } 57 try { 58 CharSequenceUtilities.toString(string, 0, CHARS_LENGTH + 1); 59 assertTrue(false); 60 } catch (IndexOutOfBoundsException e) { 61 } 62 try { 63 CharSequenceUtilities.toString(string, CHARS_LENGTH, 0); 64 assertTrue(false); 65 } catch (IndexOutOfBoundsException e) { 66 } 67 int start = CHARS_LENGTH / 5; 68 int end = CHARS_LENGTH - start; 69 assertTrue(CharSequenceUtilities.toString(string, start, end).equals(string.substring(start, end))); 70 71 StringBuffer buf = new StringBuffer (); 73 CharSequenceUtilities.append(buf, string); 74 CharSequenceUtilities.append(buf, string_2); 75 StringBuffer buff = new StringBuffer (); 76 buff.append(string); 77 buff.append(string_2); 78 assertTrue(buff.toString().equals(buf.toString())); 79 80 buf = new StringBuffer (); 81 CharSequenceUtilities.append(buf, string, start, end); 82 assertTrue(buf.toString().equals(string.substring(start, end))); 83 84 char ch = string.charAt(start); 86 assertTrue(string.indexOf(ch) == CharSequenceUtilities.indexOf(string, ch)); 87 assertTrue(string.indexOf(ch, 2 * start) == CharSequenceUtilities.indexOf(string, ch, 2 * start)); 88 89 String eta = string.substring(start, start + SUBSTR_LENGTH); 90 assertTrue(string.indexOf(eta) == CharSequenceUtilities.indexOf(string, eta)); 91 eta = string.substring(2 * start, 2 * start + SUBSTR_LENGTH); 92 assertTrue(string.indexOf(eta, start) == CharSequenceUtilities.indexOf(string, eta, start)); 93 94 assertTrue(string.lastIndexOf(ch) == CharSequenceUtilities.lastIndexOf(string, ch)); 96 assertTrue(string.lastIndexOf(ch, 2 * start) == CharSequenceUtilities.lastIndexOf(string, ch, 2 * start)); 97 98 eta = string.substring(start, start + SUBSTR_LENGTH); 99 assertTrue(string.lastIndexOf(eta) == CharSequenceUtilities.lastIndexOf(string, eta)); 100 eta = string.substring(2 * start, 2 * start + SUBSTR_LENGTH); 101 assertTrue(string.lastIndexOf(eta, CHARS_LENGTH) == CharSequenceUtilities.lastIndexOf(string, eta, CHARS_LENGTH)); 102 103 buf = new StringBuffer (); 105 for (int x = 0; x < SUBSTR_LENGTH; x++) { 106 buf.append((char)rnd.nextInt(' ' + 1)); 107 } 108 buf.append(string); 109 for (int x = 0; x < SUBSTR_LENGTH; x++) { 110 buf.append((char)rnd.nextInt(' ' + 1)); 111 } 112 assertTrue(CharSequenceUtilities.textEquals(string, CharSequenceUtilities.trim(buf.toString()))); 113 114 assertTrue(CharSequenceUtilities.startsWith(string, string.substring(0, SUBSTR_LENGTH))); 116 117 assertTrue(CharSequenceUtilities.endsWith(string, string.substring(CHARS_LENGTH - SUBSTR_LENGTH))); 119 } 120 121 public void generateChars(char[] chars) { 122 for (int x = 0; x < chars.length; x++) { 123 chars[x] = (char) rnd.nextInt(); 124 } 125 } 126 127 } 128 | Popular Tags |