001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.emultest.java.lang;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019:
020: /**
021: * This class tests StringBuffer.
022: */
023: public class StringBufferTest extends GWTTestCase {
024:
025: /**
026: * This method gets the module name.
027: *
028: * @return the module name.
029: * @see com.google.gwt.junit.client.GWTTestCase#getModuleName()
030: */
031: public String getModuleName() {
032: return "com.google.gwt.emultest.EmulSuite";
033: }
034:
035: /**
036: * This method tests <code>append</code>.
037: */
038: public void testAppend() {
039: StringBuffer x = new StringBuffer();
040: x.append(C.FLOAT_VALUE);
041: assertTrue(x.toString().startsWith(C.FLOAT_STRING));
042: x = new StringBuffer();
043: x.append(C.INT_VALUE);
044: assertEquals(C.INT_STRING, x.toString());
045: x = new StringBuffer();
046: x.append(C.LONG_VALUE);
047: assertTrue(x.toString().startsWith(C.LONG_STRING));
048: x = new StringBuffer();
049: x.append(C.DOUBLE_VALUE);
050: assertTrue(x.toString().startsWith(C.DOUBLE_STRING));
051: x = new StringBuffer();
052: x.append(C.CHAR_VALUE);
053: assertEquals(C.CHAR_STRING, x.toString());
054: x = new StringBuffer();
055: x.append(C.CHAR_ARRAY_VALUE);
056: assertEquals(C.CHAR_ARRAY_STRING, x.toString());
057: x = new StringBuffer();
058: x.append(C.CHAR_ARRAY_VALUE, 1, 4);
059: assertEquals(C.CHAR_ARRAY_STRING.substring(1, 5), x.toString());
060: x = new StringBuffer();
061: x.append(C.FALSE_VALUE);
062: assertEquals(C.FALSE_STRING, x.toString());
063: x = new StringBuffer();
064: x.append(C.TRUE_VALUE);
065: assertEquals(C.TRUE_STRING, x.toString());
066: x = new StringBuffer();
067: x.append((String) null);
068: assertEquals("null", x.toString());
069: }
070:
071: /**
072: * This method tests <code>charAt</code>.
073: */
074: public void testCharAt() {
075: assertEquals(new StringBuffer("abc").charAt(1), 'b');
076: }
077:
078: /**
079: * This method tests string creation and equality.
080: */
081: public void testContructor() {
082: String constant = "abcdef";
083: assertEquals(new StringBuffer(constant).toString(), constant);
084: assertEquals(new StringBuffer().toString(), "");
085: }
086:
087: /**
088: * This method tests <code>delete</code>.
089: */
090: public void testDelete() {
091: StringBuffer haystack = new StringBuffer("abcdefghi");
092: haystack.delete(2, 4);
093: assertEquals(haystack.toString(), "abefghi");
094: haystack.deleteCharAt(0);
095: assertEquals(haystack.toString(), "befghi");
096: haystack.deleteCharAt(1);
097: assertEquals(haystack.toString(), "bfghi");
098: }
099:
100: /**
101: * Tests toCharArray.
102: */
103: public void testGetChars() {
104: StringBuffer x = new StringBuffer("ABCDEFGHIJ");
105: char[] a1 = "abcdefghij".toCharArray();
106: char[] desired = "abcDEFghij".toCharArray();
107: x.getChars(3, 6, a1, 3);
108: for (int i = 0; i < a1.length; i++) {
109: assertEquals(a1[i], desired[i]);
110: }
111: }
112:
113: /**
114: * This method tests <code>indexOf</code>.
115: */
116: public void testIndexOf() {
117: String haystack = "abcdefghi";
118: assertEquals(haystack.indexOf("q"), -1);
119: assertEquals(haystack.indexOf('q'), -1);
120: assertEquals(haystack.indexOf("a"), 0);
121: assertEquals(haystack.indexOf('a'), 0);
122: assertEquals(haystack.indexOf('a', 1), -1);
123: assertEquals(haystack.indexOf("bc"), 1);
124: assertEquals(haystack.indexOf(""), 0);
125: }
126:
127: /**
128: * This method tests <code>insert</code>.
129: */
130: public void testInsert() {
131: StringBuffer x = new StringBuffer("!");
132: x.insert(1, C.FLOAT_VALUE);
133: assertTrue(x.toString().startsWith("!" + C.FLOAT_STRING));
134: x = new StringBuffer("!");
135: x.insert(1, C.INT_VALUE);
136: assertEquals("!" + C.INT_STRING, x.toString());
137: x = new StringBuffer("!");
138: x.insert(1, C.LONG_VALUE);
139: assertEquals("!" + C.LONG_STRING, x.toString());
140: x = new StringBuffer("!");
141: x.insert(1, C.DOUBLE_VALUE);
142: assertTrue(x.toString().startsWith("!" + C.DOUBLE_STRING));
143: x = new StringBuffer("!");
144: x.insert(1, C.CHAR_VALUE);
145: assertEquals("!" + C.CHAR_STRING, x.toString());
146: x = new StringBuffer("!");
147: x.insert(1, C.CHAR_ARRAY_VALUE);
148: assertEquals("!" + C.CHAR_ARRAY_STRING, x.toString());
149: x = new StringBuffer("!");
150: x.insert(1, C.CHAR_ARRAY_VALUE, 1, 4);
151: assertEquals("!" + C.CHAR_ARRAY_STRING.substring(1, 5), x
152: .toString());
153: x = new StringBuffer("!");
154: x.insert(1, C.FALSE_VALUE);
155: assertEquals("!" + C.FALSE_STRING, x.toString());
156: x = new StringBuffer("!");
157: x.insert(1, C.TRUE_VALUE);
158: assertEquals("!" + C.TRUE_STRING, x.toString());
159: }
160:
161: /**
162: * This method does interleaved inserts and deletes.
163: */
164: public void testInterleavedInsertAndDelete() {
165: StringBuffer x = new StringBuffer();
166: for (int i = 0; i < 9; i++) {
167: x.append("1234567890");
168: }
169: for (int i = 0; i < 10; i++) {
170: x.delete(5, 15);
171: }
172: assertEquals(x.toString(), "12345");
173: }
174:
175: /**
176: * This method tests <code>lastIndexOf</code>.
177: */
178: public void testLastIndexOf() {
179: StringBuffer x = new StringBuffer("abcdeabcdef");
180: assertEquals(9, x.lastIndexOf("e"));
181: assertEquals(10, x.lastIndexOf("f"));
182: assertEquals(-1, x.lastIndexOf("f", 1));
183: }
184:
185: /**
186: * This method tests <code>length</code>, and tests moderately long
187: * StringBuffers.
188: */
189: public void testLength() {
190: assertEquals(3, new StringBuffer("abc").length());
191: StringBuffer str = new StringBuffer("x");
192: for (int i = 0; i < 16; i++) {
193: str.append(str);
194: }
195: assertEquals(1 << 16, str.length());
196: }
197:
198: /**
199: * This method tests <code>toLowerCase</code>.
200: */
201: public void testLowerCase() {
202: assertEquals("abc", "AbC".toLowerCase());
203: assertEquals("abc", "abc".toLowerCase());
204: assertEquals("", "".toLowerCase());
205: }
206:
207: /**
208: * Tests correctness under repeated insertion and append.
209: */
210: public void testRepeatedAppendsAndInserts() {
211: StringBuffer x = new StringBuffer();
212: final int size = 1000;
213: for (int i = 0; i < size; i++) {
214: x.append("" + i % 10);
215: }
216: assertTrue("endwith1", x.toString().endsWith("0123456789"));
217: assertTrue("startswith1", x.toString().startsWith("0123456789"));
218: assertEquals(x.length(), size);
219: x = new StringBuffer();
220: for (int i = 0; i < size * 4; i++) {
221: x.append("" + i % 10);
222: }
223: assertTrue("endswith2", x.toString().endsWith("0123456789"));
224: assertTrue("startswith2", x.toString().startsWith("0123456789"));
225: assertEquals("length2", x.length(), size * 4);
226: x = new StringBuffer();
227: for (int i = 0; i < size; i++) {
228: x.insert(0, "" + i % 10);
229: }
230: assertTrue("endswith3", x.toString().endsWith("9876543210"));
231: assertTrue("startswith3", x.toString().startsWith("9876543210"));
232: assertEquals("length3", x.length(), size);
233: x = new StringBuffer();
234: for (int i = 0; i < size * 4; i++) {
235: x.insert(0, "" + i % 10);
236: }
237: assertTrue("endswith4", x.toString().endsWith("9876543210"));
238: assertTrue("startswith4", x.toString().startsWith("9876543210"));
239: assertEquals("size4", x.length(), size * 4);
240: }
241:
242: /**
243: * This method tests <code>replace</code>.
244: */
245: public void testReplace() {
246: StringBuffer x = new StringBuffer("xxyyxx");
247: x.replace(2, 4, "YY");
248: assertEquals("xxYYxx", x.toString());
249: }
250:
251: /**
252: * This method tests <code>setLength</code>.
253: */
254: public void testSetLength() {
255: StringBuffer x = new StringBuffer("abcdefghi");
256: x.setLength(20);
257: assertEquals(x.length(), 20);
258: assertEquals(x.toString(), x.toString(),
259: "abcdefghi\0\0\0\0\0\0\0\0\0\0\0");
260: x.setLength(5);
261: assertEquals(x.length(), 5);
262: assertEquals(x.toString(), "abcde");
263: }
264:
265: /**
266: * This method tests <code>startsWith</code>.
267: */
268: public void testStartsWith() {
269: String haystack = "abcdefghi";
270: assertTrue(haystack.startsWith("abc"));
271: assertTrue(haystack.startsWith("bc", 1));
272: assertTrue(haystack.startsWith(haystack));
273: assertFalse(haystack.startsWith(haystack + "j"));
274: }
275:
276: /**
277: * This method tests <code>ubstring</code>.
278: */
279: public void testSubstring() {
280: StringBuffer haystack = new StringBuffer("abcdefghi");
281: assertEquals("cd", haystack.substring(2, 4));
282: assertEquals("bc", "abcdef".substring(1, 3));
283: assertEquals("bcdef", "abcdef".substring(1));
284: }
285: }
|