0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
0013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
0014: * License for the specific language governing permissions and limitations under
0015: * the License.
0016: */
0017:
0018: package org.apache.harmony.luni.tests.java.lang;
0019:
0020: import java.io.Serializable;
0021: import java.util.Arrays;
0022:
0023: import junit.framework.TestCase;
0024:
0025: import org.apache.harmony.testframework.serialization.SerializationTest;
0026: import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
0027:
0028: public class StringBuilderTest extends TestCase {
0029:
0030: /**
0031: * @tests java.lang.StringBuilder.StringBuilder()
0032: */
0033: public void test_Constructor() {
0034: StringBuilder sb = new StringBuilder();
0035: assertNotNull(sb);
0036: assertEquals(16, sb.capacity());
0037: }
0038:
0039: /**
0040: * @tests java.lang.StringBuilder.StringBuilder(int)
0041: */
0042: public void test_ConstructorI() {
0043: StringBuilder sb = new StringBuilder(24);
0044: assertNotNull(sb);
0045: assertEquals(24, sb.capacity());
0046:
0047: try {
0048: new StringBuilder(-1);
0049: fail("no exception");
0050: } catch (NegativeArraySizeException e) {
0051: // Expected
0052: }
0053:
0054: assertNotNull(new StringBuilder(0));
0055: }
0056:
0057: /**
0058: * @tests java.lang.StringBuilder.StringBuilder(CharSequence)
0059: */
0060: @SuppressWarnings("cast")
0061: public void test_ConstructorLjava_lang_CharSequence() {
0062: StringBuilder sb = new StringBuilder((CharSequence) "fixture");
0063: assertEquals("fixture", sb.toString());
0064: assertEquals("fixture".length() + 16, sb.capacity());
0065:
0066: sb = new StringBuilder((CharSequence) new StringBuffer(
0067: "fixture"));
0068: assertEquals("fixture", sb.toString());
0069: assertEquals("fixture".length() + 16, sb.capacity());
0070:
0071: try {
0072: new StringBuilder((CharSequence) null);
0073: fail("no NPE");
0074: } catch (NullPointerException e) {
0075: // Expected
0076: }
0077: }
0078:
0079: /**
0080: * @tests java.lang.StringBuilder.StringBuilder(String)
0081: */
0082: public void test_ConstructorLjava_lang_String() {
0083: StringBuilder sb = new StringBuilder("fixture");
0084: assertEquals("fixture", sb.toString());
0085: assertEquals("fixture".length() + 16, sb.capacity());
0086:
0087: try {
0088: new StringBuilder((String) null);
0089: fail("no NPE");
0090: } catch (NullPointerException e) {
0091: }
0092: }
0093:
0094: /**
0095: * @tests java.lang.StringBuilder.append(boolean)
0096: */
0097: public void test_appendZ() {
0098: StringBuilder sb = new StringBuilder();
0099: assertSame(sb, sb.append(true));
0100: assertEquals("true", sb.toString());
0101: sb.setLength(0);
0102: assertSame(sb, sb.append(false));
0103: assertEquals("false", sb.toString());
0104: }
0105:
0106: /**
0107: * @tests java.lang.StringBuilder.append(char)
0108: */
0109: public void test_appendC() {
0110: StringBuilder sb = new StringBuilder();
0111: assertSame(sb, sb.append('a'));
0112: assertEquals("a", sb.toString());
0113: sb.setLength(0);
0114: assertSame(sb, sb.append('b'));
0115: assertEquals("b", sb.toString());
0116: }
0117:
0118: /**
0119: * @tests java.lang.StringBuilder.append(char[])
0120: */
0121: public void test_append$C() {
0122: StringBuilder sb = new StringBuilder();
0123: assertSame(sb, sb.append(new char[] { 'a', 'b' }));
0124: assertEquals("ab", sb.toString());
0125: sb.setLength(0);
0126: assertSame(sb, sb.append(new char[] { 'c', 'd' }));
0127: assertEquals("cd", sb.toString());
0128: try {
0129: sb.append((char[]) null);
0130: fail("no NPE");
0131: } catch (NullPointerException e) {
0132: // Expected
0133: }
0134: }
0135:
0136: /**
0137: * @tests java.lang.StringBuilder.append(char[], int, int)
0138: */
0139: public void test_append$CII() {
0140: StringBuilder sb = new StringBuilder();
0141: assertSame(sb, sb.append(new char[] { 'a', 'b' }, 0, 2));
0142: assertEquals("ab", sb.toString());
0143: sb.setLength(0);
0144: assertSame(sb, sb.append(new char[] { 'c', 'd' }, 0, 2));
0145: assertEquals("cd", sb.toString());
0146:
0147: sb.setLength(0);
0148: assertSame(sb, sb.append(new char[] { 'a', 'b', 'c', 'd' }, 0,
0149: 2));
0150: assertEquals("ab", sb.toString());
0151:
0152: sb.setLength(0);
0153: assertSame(sb, sb.append(new char[] { 'a', 'b', 'c', 'd' }, 2,
0154: 2));
0155: assertEquals("cd", sb.toString());
0156:
0157: sb.setLength(0);
0158: assertSame(sb, sb.append(new char[] { 'a', 'b', 'c', 'd' }, 2,
0159: 0));
0160: assertEquals("", sb.toString());
0161:
0162: try {
0163: sb.append((char[]) null, 0, 2);
0164: fail("no NPE");
0165: } catch (NullPointerException e) {
0166: // Expected
0167: }
0168:
0169: try {
0170: sb.append(new char[] { 'a', 'b', 'c', 'd' }, -1, 2);
0171: fail("no IOOBE, negative offset");
0172: } catch (IndexOutOfBoundsException e) {
0173: // Expected
0174: }
0175:
0176: try {
0177: sb.append(new char[] { 'a', 'b', 'c', 'd' }, 0, -1);
0178: fail("no IOOBE, negative length");
0179: } catch (IndexOutOfBoundsException e) {
0180: // Expected
0181: }
0182:
0183: try {
0184: sb.append(new char[] { 'a', 'b', 'c', 'd' }, 2, 3);
0185: fail("no IOOBE, offset and length overflow");
0186: } catch (IndexOutOfBoundsException e) {
0187: // Expected
0188: }
0189: }
0190:
0191: /**
0192: * @tests java.lang.StringBuilder.append(CharSequence)
0193: */
0194: public void test_appendLjava_lang_CharSequence() {
0195: StringBuilder sb = new StringBuilder();
0196: assertSame(sb, sb.append((CharSequence) "ab"));
0197: assertEquals("ab", sb.toString());
0198: sb.setLength(0);
0199: assertSame(sb, sb.append((CharSequence) "cd"));
0200: assertEquals("cd", sb.toString());
0201: sb.setLength(0);
0202: assertSame(sb, sb.append((CharSequence) null));
0203: assertEquals("null", sb.toString());
0204: }
0205:
0206: /**
0207: * @tests java.lang.StringBuilder.append(CharSequence, int, int)
0208: */
0209: @SuppressWarnings("cast")
0210: public void test_appendLjava_lang_CharSequenceII() {
0211: StringBuilder sb = new StringBuilder();
0212: assertSame(sb, sb.append((CharSequence) "ab", 0, 2));
0213: assertEquals("ab", sb.toString());
0214: sb.setLength(0);
0215: assertSame(sb, sb.append((CharSequence) "cd", 0, 2));
0216: assertEquals("cd", sb.toString());
0217: sb.setLength(0);
0218: assertSame(sb, sb.append((CharSequence) "abcd", 0, 2));
0219: assertEquals("ab", sb.toString());
0220: sb.setLength(0);
0221: assertSame(sb, sb.append((CharSequence) "abcd", 2, 4));
0222: assertEquals("cd", sb.toString());
0223: sb.setLength(0);
0224: assertSame(sb, sb.append((CharSequence) null, 0, 2));
0225: assertEquals("nu", sb.toString());
0226: }
0227:
0228: /**
0229: * @tests java.lang.StringBuilder.append(double)
0230: */
0231: public void test_appendD() {
0232: StringBuilder sb = new StringBuilder();
0233: assertSame(sb, sb.append(1D));
0234: assertEquals(String.valueOf(1D), sb.toString());
0235: sb.setLength(0);
0236: assertSame(sb, sb.append(0D));
0237: assertEquals(String.valueOf(0D), sb.toString());
0238: sb.setLength(0);
0239: assertSame(sb, sb.append(-1D));
0240: assertEquals(String.valueOf(-1D), sb.toString());
0241: sb.setLength(0);
0242: assertSame(sb, sb.append(Double.NaN));
0243: assertEquals(String.valueOf(Double.NaN), sb.toString());
0244: sb.setLength(0);
0245: assertSame(sb, sb.append(Double.NEGATIVE_INFINITY));
0246: assertEquals(String.valueOf(Double.NEGATIVE_INFINITY), sb
0247: .toString());
0248: sb.setLength(0);
0249: assertSame(sb, sb.append(Double.POSITIVE_INFINITY));
0250: assertEquals(String.valueOf(Double.POSITIVE_INFINITY), sb
0251: .toString());
0252: sb.setLength(0);
0253: assertSame(sb, sb.append(Double.MIN_VALUE));
0254: assertEquals(String.valueOf(Double.MIN_VALUE), sb.toString());
0255: sb.setLength(0);
0256: assertSame(sb, sb.append(Double.MAX_VALUE));
0257: assertEquals(String.valueOf(Double.MAX_VALUE), sb.toString());
0258: }
0259:
0260: /**
0261: * @tests java.lang.StringBuilder.append(float)
0262: */
0263: public void test_appendF() {
0264: StringBuilder sb = new StringBuilder();
0265: assertSame(sb, sb.append(1F));
0266: assertEquals(String.valueOf(1F), sb.toString());
0267: sb.setLength(0);
0268: assertSame(sb, sb.append(0F));
0269: assertEquals(String.valueOf(0F), sb.toString());
0270: sb.setLength(0);
0271: assertSame(sb, sb.append(-1F));
0272: assertEquals(String.valueOf(-1F), sb.toString());
0273: sb.setLength(0);
0274: assertSame(sb, sb.append(Float.NaN));
0275: assertEquals(String.valueOf(Float.NaN), sb.toString());
0276: sb.setLength(0);
0277: assertSame(sb, sb.append(Float.NEGATIVE_INFINITY));
0278: assertEquals(String.valueOf(Float.NEGATIVE_INFINITY), sb
0279: .toString());
0280: sb.setLength(0);
0281: assertSame(sb, sb.append(Float.POSITIVE_INFINITY));
0282: assertEquals(String.valueOf(Float.POSITIVE_INFINITY), sb
0283: .toString());
0284: sb.setLength(0);
0285: assertSame(sb, sb.append(Float.MIN_VALUE));
0286: assertEquals(String.valueOf(Float.MIN_VALUE), sb.toString());
0287: sb.setLength(0);
0288: assertSame(sb, sb.append(Float.MAX_VALUE));
0289: assertEquals(String.valueOf(Float.MAX_VALUE), sb.toString());
0290: }
0291:
0292: /**
0293: * @tests java.lang.StringBuilder.append(int)
0294: */
0295: public void test_appendI() {
0296: StringBuilder sb = new StringBuilder();
0297: assertSame(sb, sb.append(1));
0298: assertEquals(String.valueOf(1), sb.toString());
0299: sb.setLength(0);
0300: assertSame(sb, sb.append(0));
0301: assertEquals(String.valueOf(0), sb.toString());
0302: sb.setLength(0);
0303: assertSame(sb, sb.append(-1));
0304: assertEquals(String.valueOf(-1), sb.toString());
0305: sb.setLength(0);
0306: assertSame(sb, sb.append(Integer.MIN_VALUE));
0307: assertEquals(String.valueOf(Integer.MIN_VALUE), sb.toString());
0308: sb.setLength(0);
0309: assertSame(sb, sb.append(Integer.MAX_VALUE));
0310: assertEquals(String.valueOf(Integer.MAX_VALUE), sb.toString());
0311: }
0312:
0313: /**
0314: * @tests java.lang.StringBuilder.append(long)
0315: */
0316: public void test_appendL() {
0317: StringBuilder sb = new StringBuilder();
0318: assertSame(sb, sb.append(1L));
0319: assertEquals(String.valueOf(1L), sb.toString());
0320: sb.setLength(0);
0321: assertSame(sb, sb.append(0L));
0322: assertEquals(String.valueOf(0L), sb.toString());
0323: sb.setLength(0);
0324: assertSame(sb, sb.append(-1L));
0325: assertEquals(String.valueOf(-1L), sb.toString());
0326: sb.setLength(0);
0327: assertSame(sb, sb.append(Integer.MIN_VALUE));
0328: assertEquals(String.valueOf(Integer.MIN_VALUE), sb.toString());
0329: sb.setLength(0);
0330: assertSame(sb, sb.append(Integer.MAX_VALUE));
0331: assertEquals(String.valueOf(Integer.MAX_VALUE), sb.toString());
0332: }
0333:
0334: /**
0335: * @tests java.lang.StringBuilder.append(Object)'
0336: */
0337: public void test_appendLjava_lang_Object() {
0338: StringBuilder sb = new StringBuilder();
0339: assertSame(sb, sb.append(Fixture.INSTANCE));
0340: assertEquals(Fixture.INSTANCE.toString(), sb.toString());
0341:
0342: sb.setLength(0);
0343: assertSame(sb, sb.append((Object) null));
0344: assertEquals("null", sb.toString());
0345: }
0346:
0347: /**
0348: * @tests java.lang.StringBuilder.append(String)
0349: */
0350: public void test_appendLjava_lang_String() {
0351: StringBuilder sb = new StringBuilder();
0352: assertSame(sb, sb.append("ab"));
0353: assertEquals("ab", sb.toString());
0354: sb.setLength(0);
0355: assertSame(sb, sb.append("cd"));
0356: assertEquals("cd", sb.toString());
0357: sb.setLength(0);
0358: assertSame(sb, sb.append((String) null));
0359: assertEquals("null", sb.toString());
0360: }
0361:
0362: /**
0363: * @tests java.lang.StringBuilder.append(StringBuffer)
0364: */
0365: public void test_appendLjava_lang_StringBuffer() {
0366: StringBuilder sb = new StringBuilder();
0367: assertSame(sb, sb.append(new StringBuffer("ab")));
0368: assertEquals("ab", sb.toString());
0369: sb.setLength(0);
0370: assertSame(sb, sb.append(new StringBuffer("cd")));
0371: assertEquals("cd", sb.toString());
0372: sb.setLength(0);
0373: assertSame(sb, sb.append((StringBuffer) null));
0374: assertEquals("null", sb.toString());
0375: }
0376:
0377: /**
0378: * @tests java.lang.StringBuilder.appendCodePoint(int)'
0379: */
0380: public void test_appendCodePointI() {
0381: StringBuilder sb = new StringBuilder();
0382: sb.appendCodePoint(0x10000);
0383: assertEquals("\uD800\uDC00", sb.toString());
0384: sb.append("fixture");
0385: assertEquals("\uD800\uDC00fixture", sb.toString());
0386: sb.appendCodePoint(0x00010FFFF);
0387: assertEquals("\uD800\uDC00fixture\uDBFF\uDFFF", sb.toString());
0388: }
0389:
0390: /**
0391: * @tests java.lang.StringBuilder.capacity()'
0392: */
0393: public void test_capacity() {
0394: StringBuilder sb = new StringBuilder();
0395: assertEquals(16, sb.capacity());
0396: sb.append("0123456789ABCDEF0123456789ABCDEF");
0397: assertTrue(sb.capacity() > 16);
0398: }
0399:
0400: /**
0401: * @tests java.lang.StringBuilder.charAt(int)'
0402: */
0403: public void test_charAtI() {
0404: final String fixture = "0123456789";
0405: StringBuilder sb = new StringBuilder(fixture);
0406: for (int i = 0; i < fixture.length(); i++) {
0407: assertEquals((char) ('0' + i), sb.charAt(i));
0408: }
0409:
0410: try {
0411: sb.charAt(-1);
0412: fail("no IOOBE, negative index");
0413: } catch (IndexOutOfBoundsException e) {
0414: // Expected
0415: }
0416:
0417: try {
0418: sb.charAt(fixture.length());
0419: fail("no IOOBE, equal to length");
0420: } catch (IndexOutOfBoundsException e) {
0421: }
0422:
0423: try {
0424: sb.charAt(fixture.length() + 1);
0425: fail("no IOOBE, greater than length");
0426: } catch (IndexOutOfBoundsException e) {
0427: }
0428: }
0429:
0430: /**
0431: * @tests java.lang.StringBuilder.codePointAt(int)
0432: */
0433: public void test_codePointAtI() {
0434: StringBuilder sb = new StringBuilder("abc");
0435: assertEquals('a', sb.codePointAt(0));
0436: assertEquals('b', sb.codePointAt(1));
0437: assertEquals('c', sb.codePointAt(2));
0438:
0439: sb = new StringBuilder("\uD800\uDC00");
0440: assertEquals(0x10000, sb.codePointAt(0));
0441: assertEquals('\uDC00', sb.codePointAt(1));
0442:
0443: sb = new StringBuilder();
0444: sb.append("abc");
0445: try {
0446: sb.codePointAt(-1);
0447: fail("No IOOBE on negative index.");
0448: } catch (IndexOutOfBoundsException e) {
0449:
0450: }
0451:
0452: try {
0453: sb.codePointAt(sb.length());
0454: fail("No IOOBE on index equal to length.");
0455: } catch (IndexOutOfBoundsException e) {
0456:
0457: }
0458:
0459: try {
0460: sb.codePointAt(sb.length() + 1);
0461: fail("No IOOBE on index greater than length.");
0462: } catch (IndexOutOfBoundsException e) {
0463:
0464: }
0465: }
0466:
0467: /**
0468: * @tests java.lang.StringBuilder.codePointBefore(int)
0469: */
0470: public void test_codePointBeforeI() {
0471: StringBuilder sb = new StringBuilder("abc");
0472: assertEquals('a', sb.codePointBefore(1));
0473: assertEquals('b', sb.codePointBefore(2));
0474: assertEquals('c', sb.codePointBefore(3));
0475:
0476: sb = new StringBuilder("\uD800\uDC00");
0477: assertEquals(0x10000, sb.codePointBefore(2));
0478: assertEquals('\uD800', sb.codePointBefore(1));
0479:
0480: sb = new StringBuilder();
0481: sb.append("abc");
0482:
0483: try {
0484: sb.codePointBefore(0);
0485: fail("No IOOBE on zero index.");
0486: } catch (IndexOutOfBoundsException e) {
0487:
0488: }
0489:
0490: try {
0491: sb.codePointBefore(-1);
0492: fail("No IOOBE on negative index.");
0493: } catch (IndexOutOfBoundsException e) {
0494:
0495: }
0496:
0497: try {
0498: sb.codePointBefore(sb.length() + 1);
0499: fail("No IOOBE on index greater than length.");
0500: } catch (IndexOutOfBoundsException e) {
0501:
0502: }
0503: }
0504:
0505: /**
0506: * @tests java.lang.StringBuilder.codePointCount(int, int)
0507: */
0508: public void test_codePointCountII() {
0509: assertEquals(1, new StringBuilder("\uD800\uDC00")
0510: .codePointCount(0, 2));
0511: assertEquals(1, new StringBuilder("\uD800\uDC01")
0512: .codePointCount(0, 2));
0513: assertEquals(1, new StringBuilder("\uD801\uDC01")
0514: .codePointCount(0, 2));
0515: assertEquals(1, new StringBuilder("\uDBFF\uDFFF")
0516: .codePointCount(0, 2));
0517:
0518: assertEquals(3, new StringBuilder("a\uD800\uDC00b")
0519: .codePointCount(0, 4));
0520: assertEquals(4, new StringBuilder("a\uD800\uDC00b\uD800")
0521: .codePointCount(0, 5));
0522:
0523: StringBuilder sb = new StringBuilder();
0524: sb.append("abc");
0525: try {
0526: sb.codePointCount(-1, 2);
0527: fail("No IOOBE for negative begin index.");
0528: } catch (IndexOutOfBoundsException e) {
0529:
0530: }
0531:
0532: try {
0533: sb.codePointCount(0, 4);
0534: fail("No IOOBE for end index that's too large.");
0535: } catch (IndexOutOfBoundsException e) {
0536:
0537: }
0538:
0539: try {
0540: sb.codePointCount(3, 2);
0541: fail("No IOOBE for begin index larger than end index.");
0542: } catch (IndexOutOfBoundsException e) {
0543:
0544: }
0545: }
0546:
0547: /**
0548: * @tests java.lang.StringBuilder.delete(int, int)
0549: */
0550: public void test_deleteII() {
0551: final String fixture = "0123456789";
0552: StringBuilder sb = new StringBuilder(fixture);
0553: assertSame(sb, sb.delete(0, 0));
0554: assertEquals(fixture, sb.toString());
0555: assertSame(sb, sb.delete(5, 5));
0556: assertEquals(fixture, sb.toString());
0557: assertSame(sb, sb.delete(0, 1));
0558: assertEquals("123456789", sb.toString());
0559: assertEquals(9, sb.length());
0560: assertSame(sb, sb.delete(0, sb.length()));
0561: assertEquals("", sb.toString());
0562: assertEquals(0, sb.length());
0563:
0564: sb = new StringBuilder(fixture);
0565: assertSame(sb, sb.delete(0, 11));
0566: assertEquals("", sb.toString());
0567: assertEquals(0, sb.length());
0568:
0569: try {
0570: new StringBuilder(fixture).delete(-1, 2);
0571: fail("no SIOOBE, negative start");
0572: } catch (StringIndexOutOfBoundsException e) {
0573: // Expected
0574: }
0575:
0576: try {
0577: new StringBuilder(fixture).delete(11, 12);
0578: fail("no SIOOBE, start too far");
0579: } catch (StringIndexOutOfBoundsException e) {
0580: // Expected
0581: }
0582:
0583: try {
0584: new StringBuilder(fixture).delete(13, 12);
0585: fail("no SIOOBE, start larger than end");
0586: } catch (StringIndexOutOfBoundsException e) {
0587: // Expected
0588: }
0589: }
0590:
0591: /**
0592: * @tests java.lang.StringBuilder.deleteCharAt(int)
0593: */
0594: public void test_deleteCharAtI() {
0595: final String fixture = "0123456789";
0596: StringBuilder sb = new StringBuilder(fixture);
0597: assertSame(sb, sb.deleteCharAt(0));
0598: assertEquals("123456789", sb.toString());
0599: assertEquals(9, sb.length());
0600: sb = new StringBuilder(fixture);
0601: assertSame(sb, sb.deleteCharAt(5));
0602: assertEquals("012346789", sb.toString());
0603: assertEquals(9, sb.length());
0604: sb = new StringBuilder(fixture);
0605: assertSame(sb, sb.deleteCharAt(9));
0606: assertEquals("012345678", sb.toString());
0607: assertEquals(9, sb.length());
0608:
0609: try {
0610: new StringBuilder(fixture).deleteCharAt(-1);
0611: fail("no SIOOBE, negative index");
0612: } catch (StringIndexOutOfBoundsException e) {
0613: // Expected
0614: }
0615:
0616: try {
0617: new StringBuilder(fixture).deleteCharAt(fixture.length());
0618: fail("no SIOOBE, index equals length");
0619: } catch (StringIndexOutOfBoundsException e) {
0620: // Expected
0621: }
0622:
0623: try {
0624: new StringBuilder(fixture)
0625: .deleteCharAt(fixture.length() + 1);
0626: fail("no SIOOBE, index exceeds length");
0627: } catch (StringIndexOutOfBoundsException e) {
0628: // Expected
0629: }
0630: }
0631:
0632: /**
0633: * @tests java.lang.StringBuilder.ensureCapacity(int)'
0634: */
0635: public void test_ensureCapacityI() {
0636: StringBuilder sb = new StringBuilder(5);
0637: assertEquals(5, sb.capacity());
0638: sb.ensureCapacity(10);
0639: assertEquals(12, sb.capacity());
0640: sb.ensureCapacity(26);
0641: assertEquals(26, sb.capacity());
0642: sb.ensureCapacity(55);
0643: assertEquals(55, sb.capacity());
0644: }
0645:
0646: /**
0647: * @tests java.lang.StringBuilder.getChars(int, int, char[], int)'
0648: */
0649: public void test_getCharsII$CI() {
0650: final String fixture = "0123456789";
0651: StringBuilder sb = new StringBuilder(fixture);
0652: char[] dst = new char[10];
0653: sb.getChars(0, 10, dst, 0);
0654: assertTrue(Arrays.equals(fixture.toCharArray(), dst));
0655:
0656: Arrays.fill(dst, '\0');
0657: sb.getChars(0, 5, dst, 0);
0658: char[] fixtureChars = new char[10];
0659: fixture.getChars(0, 5, fixtureChars, 0);
0660: assertTrue(Arrays.equals(fixtureChars, dst));
0661:
0662: Arrays.fill(dst, '\0');
0663: Arrays.fill(fixtureChars, '\0');
0664: sb.getChars(0, 5, dst, 5);
0665: fixture.getChars(0, 5, fixtureChars, 5);
0666: assertTrue(Arrays.equals(fixtureChars, dst));
0667:
0668: Arrays.fill(dst, '\0');
0669: Arrays.fill(fixtureChars, '\0');
0670: sb.getChars(5, 10, dst, 1);
0671: fixture.getChars(5, 10, fixtureChars, 1);
0672: assertTrue(Arrays.equals(fixtureChars, dst));
0673:
0674: try {
0675: sb.getChars(0, 10, null, 0);
0676: fail("no NPE");
0677: } catch (NullPointerException e) {
0678: // Expected
0679: }
0680:
0681: try {
0682: sb.getChars(-1, 10, dst, 0);
0683: fail("no IOOBE, srcBegin negative");
0684: } catch (IndexOutOfBoundsException e) {
0685: // Expected
0686: }
0687:
0688: try {
0689: sb.getChars(0, 10, dst, -1);
0690: fail("no IOOBE, dstBegin negative");
0691: } catch (IndexOutOfBoundsException e) {
0692: // Expected
0693: }
0694:
0695: try {
0696: sb.getChars(5, 4, dst, 0);
0697: fail("no IOOBE, srcBegin > srcEnd");
0698: } catch (IndexOutOfBoundsException e) {
0699: // Expected
0700: }
0701:
0702: try {
0703: sb.getChars(0, 11, dst, 0);
0704: fail("no IOOBE, srcEnd > length");
0705: } catch (IndexOutOfBoundsException e) {
0706: // Expected
0707: }
0708:
0709: try {
0710: sb.getChars(0, 10, dst, 5);
0711: fail("no IOOBE, dstBegin and src size too large for what's left in dst");
0712: } catch (IndexOutOfBoundsException e) {
0713: // Expected
0714: }
0715: }
0716:
0717: /**
0718: * @tests java.lang.StringBuilder.indexOf(String)
0719: */
0720: public void test_indexOfLjava_lang_String() {
0721: final String fixture = "0123456789";
0722: StringBuilder sb = new StringBuilder(fixture);
0723: assertEquals(0, sb.indexOf("0"));
0724: assertEquals(0, sb.indexOf("012"));
0725: assertEquals(-1, sb.indexOf("02"));
0726: assertEquals(8, sb.indexOf("89"));
0727:
0728: try {
0729: sb.indexOf(null);
0730: fail("no NPE");
0731: } catch (NullPointerException e) {
0732: // Expected
0733: }
0734: }
0735:
0736: /**
0737: * @tests java.lang.StringBuilder.indexOf(String, int)
0738: */
0739: public void test_IndexOfStringInt() {
0740: final String fixture = "0123456789";
0741: StringBuilder sb = new StringBuilder(fixture);
0742: assertEquals(0, sb.indexOf("0"));
0743: assertEquals(0, sb.indexOf("012"));
0744: assertEquals(-1, sb.indexOf("02"));
0745: assertEquals(8, sb.indexOf("89"));
0746:
0747: assertEquals(0, sb.indexOf("0"), 0);
0748: assertEquals(0, sb.indexOf("012"), 0);
0749: assertEquals(-1, sb.indexOf("02"), 0);
0750: assertEquals(8, sb.indexOf("89"), 0);
0751:
0752: assertEquals(-1, sb.indexOf("0"), 5);
0753: assertEquals(-1, sb.indexOf("012"), 5);
0754: assertEquals(-1, sb.indexOf("02"), 0);
0755: assertEquals(8, sb.indexOf("89"), 5);
0756:
0757: try {
0758: sb.indexOf(null, 0);
0759: fail("no NPE");
0760: } catch (NullPointerException e) {
0761: // Expected
0762: }
0763: }
0764:
0765: /**
0766: * @tests java.lang.StringBuilder.insert(int, boolean)
0767: */
0768: public void test_insertIZ() {
0769: final String fixture = "0000";
0770: StringBuilder sb = new StringBuilder(fixture);
0771: assertSame(sb, sb.insert(0, true));
0772: assertEquals("true0000", sb.toString());
0773: assertEquals(8, sb.length());
0774:
0775: sb = new StringBuilder(fixture);
0776: assertSame(sb, sb.insert(0, false));
0777: assertEquals("false0000", sb.toString());
0778: assertEquals(9, sb.length());
0779:
0780: sb = new StringBuilder(fixture);
0781: assertSame(sb, sb.insert(2, false));
0782: assertEquals("00false00", sb.toString());
0783: assertEquals(9, sb.length());
0784:
0785: sb = new StringBuilder(fixture);
0786: assertSame(sb, sb.insert(4, false));
0787: assertEquals("0000false", sb.toString());
0788: assertEquals(9, sb.length());
0789:
0790: try {
0791: sb = new StringBuilder(fixture);
0792: sb.insert(-1, false);
0793: fail("no SIOOBE, negative index");
0794: } catch (StringIndexOutOfBoundsException e) {
0795: // Expected
0796: }
0797:
0798: try {
0799: sb = new StringBuilder(fixture);
0800: sb.insert(5, false);
0801: fail("no SIOOBE, index too large index");
0802: } catch (StringIndexOutOfBoundsException e) {
0803: // Expected
0804: }
0805: }
0806:
0807: /**
0808: * @tests java.lang.StringBuilder.insert(int, char)
0809: */
0810: public void test_insertIC() {
0811: final String fixture = "0000";
0812: StringBuilder sb = new StringBuilder(fixture);
0813: assertSame(sb, sb.insert(0, 'a'));
0814: assertEquals("a0000", sb.toString());
0815: assertEquals(5, sb.length());
0816:
0817: sb = new StringBuilder(fixture);
0818: assertSame(sb, sb.insert(0, 'b'));
0819: assertEquals("b0000", sb.toString());
0820: assertEquals(5, sb.length());
0821:
0822: sb = new StringBuilder(fixture);
0823: assertSame(sb, sb.insert(2, 'b'));
0824: assertEquals("00b00", sb.toString());
0825: assertEquals(5, sb.length());
0826:
0827: sb = new StringBuilder(fixture);
0828: assertSame(sb, sb.insert(4, 'b'));
0829: assertEquals("0000b", sb.toString());
0830: assertEquals(5, sb.length());
0831:
0832: // FIXME this fails on Sun JRE 5.0_5
0833: // try {
0834: // sb = new StringBuilder(fixture);
0835: // sb.insert(-1, 'a');
0836: // fail("no SIOOBE, negative index");
0837: // } catch (StringIndexOutOfBoundsException e) {
0838: // // Expected
0839: // }
0840:
0841: /*
0842: * FIXME This fails on Sun JRE 5.0_5, but that seems like a bug, since
0843: * the 'insert(int, char[]) behaves this way.
0844: */
0845: // try {
0846: // sb = new StringBuilder(fixture);
0847: // sb.insert(5, 'a');
0848: // fail("no SIOOBE, index too large index");
0849: // } catch (StringIndexOutOfBoundsException e) {
0850: // // Expected
0851: // }
0852: }
0853:
0854: /**
0855: * @tests java.lang.StringBuilder.insert(int, char)
0856: */
0857: public void test_insertIC_2() {
0858: StringBuilder obj = new StringBuilder();
0859: try {
0860: obj.insert(-1, '?');
0861: fail("ArrayIndexOutOfBoundsException expected");
0862: } catch (ArrayIndexOutOfBoundsException e) {
0863: // expected
0864: }
0865: }
0866:
0867: /**
0868: * @tests java.lang.StringBuilder.insert(int, char[])'
0869: */
0870: public void test_insertI$C() {
0871: final String fixture = "0000";
0872: StringBuilder sb = new StringBuilder(fixture);
0873: assertSame(sb, sb.insert(0, new char[] { 'a', 'b' }));
0874: assertEquals("ab0000", sb.toString());
0875: assertEquals(6, sb.length());
0876:
0877: sb = new StringBuilder(fixture);
0878: assertSame(sb, sb.insert(2, new char[] { 'a', 'b' }));
0879: assertEquals("00ab00", sb.toString());
0880: assertEquals(6, sb.length());
0881:
0882: sb = new StringBuilder(fixture);
0883: assertSame(sb, sb.insert(4, new char[] { 'a', 'b' }));
0884: assertEquals("0000ab", sb.toString());
0885: assertEquals(6, sb.length());
0886:
0887: /*
0888: * TODO This NPE is the behavior on Sun's JRE 5.0_5, but it's
0889: * undocumented. The assumption is that this method behaves like
0890: * String.valueOf(char[]), which does throw a NPE too, but that is also
0891: * undocumented.
0892: */
0893:
0894: try {
0895: sb.insert(0, (char[]) null);
0896: fail("no NPE");
0897: } catch (NullPointerException e) {
0898: // Expected
0899: }
0900:
0901: try {
0902: sb = new StringBuilder(fixture);
0903: sb.insert(-1, new char[] { 'a', 'b' });
0904: fail("no SIOOBE, negative index");
0905: } catch (StringIndexOutOfBoundsException e) {
0906: // Expected
0907: }
0908:
0909: try {
0910: sb = new StringBuilder(fixture);
0911: sb.insert(5, new char[] { 'a', 'b' });
0912: fail("no SIOOBE, index too large index");
0913: } catch (StringIndexOutOfBoundsException e) {
0914: // Expected
0915: }
0916: }
0917:
0918: /**
0919: * @tests java.lang.StringBuilder.insert(int, char[], int, int)
0920: */
0921: public void test_insertI$CII() {
0922: final String fixture = "0000";
0923: StringBuilder sb = new StringBuilder(fixture);
0924: assertSame(sb, sb.insert(0, new char[] { 'a', 'b' }, 0, 2));
0925: assertEquals("ab0000", sb.toString());
0926: assertEquals(6, sb.length());
0927:
0928: sb = new StringBuilder(fixture);
0929: assertSame(sb, sb.insert(0, new char[] { 'a', 'b' }, 0, 1));
0930: assertEquals("a0000", sb.toString());
0931: assertEquals(5, sb.length());
0932:
0933: sb = new StringBuilder(fixture);
0934: assertSame(sb, sb.insert(2, new char[] { 'a', 'b' }, 0, 2));
0935: assertEquals("00ab00", sb.toString());
0936: assertEquals(6, sb.length());
0937:
0938: sb = new StringBuilder(fixture);
0939: assertSame(sb, sb.insert(2, new char[] { 'a', 'b' }, 0, 1));
0940: assertEquals("00a00", sb.toString());
0941: assertEquals(5, sb.length());
0942:
0943: sb = new StringBuilder(fixture);
0944: assertSame(sb, sb.insert(4, new char[] { 'a', 'b' }, 0, 2));
0945: assertEquals("0000ab", sb.toString());
0946: assertEquals(6, sb.length());
0947:
0948: sb = new StringBuilder(fixture);
0949: assertSame(sb, sb.insert(4, new char[] { 'a', 'b' }, 0, 1));
0950: assertEquals("0000a", sb.toString());
0951: assertEquals(5, sb.length());
0952:
0953: /*
0954: * TODO This NPE is the behavior on Sun's JRE 5.0_5, but it's
0955: * undocumented. The assumption is that this method behaves like
0956: * String.valueOf(char[]), which does throw a NPE too, but that is also
0957: * undocumented.
0958: */
0959:
0960: try {
0961: sb.insert(0, (char[]) null, 0, 2);
0962: fail("no NPE");
0963: } catch (NullPointerException e) {
0964: // Expected
0965: }
0966:
0967: try {
0968: sb = new StringBuilder(fixture);
0969: sb.insert(-1, new char[] { 'a', 'b' }, 0, 2);
0970: fail("no SIOOBE, negative index");
0971: } catch (StringIndexOutOfBoundsException e) {
0972: // Expected
0973: }
0974:
0975: try {
0976: sb = new StringBuilder(fixture);
0977: sb.insert(5, new char[] { 'a', 'b' }, 0, 2);
0978: fail("no SIOOBE, index too large index");
0979: } catch (StringIndexOutOfBoundsException e) {
0980: // Expected
0981: }
0982:
0983: try {
0984: sb = new StringBuilder(fixture);
0985: sb.insert(5, new char[] { 'a', 'b' }, -1, 2);
0986: fail("no SIOOBE, negative offset");
0987: } catch (StringIndexOutOfBoundsException e) {
0988: // Expected
0989: }
0990:
0991: try {
0992: sb = new StringBuilder(fixture);
0993: sb.insert(5, new char[] { 'a', 'b' }, 0, -1);
0994: fail("no SIOOBE, negative length");
0995: } catch (StringIndexOutOfBoundsException e) {
0996: // Expected
0997: }
0998:
0999: try {
1000: sb = new StringBuilder(fixture);
1001: sb.insert(5, new char[] { 'a', 'b' }, 0, 3);
1002: fail("no SIOOBE, too long");
1003: } catch (StringIndexOutOfBoundsException e) {
1004: // Expected
1005: }
1006: }
1007:
1008: /**
1009: * @tests java.lang.StringBuilder.insert(int, CharSequence)
1010: */
1011: public void test_insertILjava_lang_CharSequence() {
1012: final String fixture = "0000";
1013: StringBuilder sb = new StringBuilder(fixture);
1014: assertSame(sb, sb.insert(0, (CharSequence) "ab"));
1015: assertEquals("ab0000", sb.toString());
1016: assertEquals(6, sb.length());
1017:
1018: sb = new StringBuilder(fixture);
1019: assertSame(sb, sb.insert(2, (CharSequence) "ab"));
1020: assertEquals("00ab00", sb.toString());
1021: assertEquals(6, sb.length());
1022:
1023: sb = new StringBuilder(fixture);
1024: assertSame(sb, sb.insert(4, (CharSequence) "ab"));
1025: assertEquals("0000ab", sb.toString());
1026: assertEquals(6, sb.length());
1027:
1028: sb = new StringBuilder(fixture);
1029: assertSame(sb, sb.insert(4, (CharSequence) null));
1030: assertEquals("0000null", sb.toString());
1031: assertEquals(8, sb.length());
1032:
1033: try {
1034: sb = new StringBuilder(fixture);
1035: sb.insert(-1, (CharSequence) "ab");
1036: fail("no IOOBE, negative index");
1037: } catch (IndexOutOfBoundsException e) {
1038: // Expected
1039: }
1040:
1041: try {
1042: sb = new StringBuilder(fixture);
1043: sb.insert(5, (CharSequence) "ab");
1044: fail("no IOOBE, index too large index");
1045: } catch (IndexOutOfBoundsException e) {
1046: // Expected
1047: }
1048: }
1049:
1050: /**
1051: * @tests java.lang.StringBuilder.insert(int, CharSequence, int, int)
1052: */
1053: @SuppressWarnings("cast")
1054: public void test_insertILjava_lang_CharSequenceII() {
1055: final String fixture = "0000";
1056: StringBuilder sb = new StringBuilder(fixture);
1057: assertSame(sb, sb.insert(0, (CharSequence) "ab", 0, 2));
1058: assertEquals("ab0000", sb.toString());
1059: assertEquals(6, sb.length());
1060:
1061: sb = new StringBuilder(fixture);
1062: assertSame(sb, sb.insert(0, (CharSequence) "ab", 0, 1));
1063: assertEquals("a0000", sb.toString());
1064: assertEquals(5, sb.length());
1065:
1066: sb = new StringBuilder(fixture);
1067: assertSame(sb, sb.insert(2, (CharSequence) "ab", 0, 2));
1068: assertEquals("00ab00", sb.toString());
1069: assertEquals(6, sb.length());
1070:
1071: sb = new StringBuilder(fixture);
1072: assertSame(sb, sb.insert(2, (CharSequence) "ab", 0, 1));
1073: assertEquals("00a00", sb.toString());
1074: assertEquals(5, sb.length());
1075:
1076: sb = new StringBuilder(fixture);
1077: assertSame(sb, sb.insert(4, (CharSequence) "ab", 0, 2));
1078: assertEquals("0000ab", sb.toString());
1079: assertEquals(6, sb.length());
1080:
1081: sb = new StringBuilder(fixture);
1082: assertSame(sb, sb.insert(4, (CharSequence) "ab", 0, 1));
1083: assertEquals("0000a", sb.toString());
1084: assertEquals(5, sb.length());
1085:
1086: sb = new StringBuilder(fixture);
1087: assertSame(sb, sb.insert(4, (CharSequence) null, 0, 2));
1088: assertEquals("0000nu", sb.toString());
1089: assertEquals(6, sb.length());
1090:
1091: try {
1092: sb = new StringBuilder(fixture);
1093: sb.insert(-1, (CharSequence) "ab", 0, 2);
1094: fail("no IOOBE, negative index");
1095: } catch (IndexOutOfBoundsException e) {
1096: // Expected
1097: }
1098:
1099: try {
1100: sb = new StringBuilder(fixture);
1101: sb.insert(5, (CharSequence) "ab", 0, 2);
1102: fail("no IOOBE, index too large index");
1103: } catch (IndexOutOfBoundsException e) {
1104: // Expected
1105: }
1106:
1107: try {
1108: sb = new StringBuilder(fixture);
1109: sb.insert(5, (CharSequence) "ab", -1, 2);
1110: fail("no IOOBE, negative offset");
1111: } catch (IndexOutOfBoundsException e) {
1112: // Expected
1113: }
1114:
1115: try {
1116: sb = new StringBuilder(fixture);
1117: sb.insert(5, new char[] { 'a', 'b' }, 0, -1);
1118: fail("no IOOBE, negative length");
1119: } catch (IndexOutOfBoundsException e) {
1120: // Expected
1121: }
1122:
1123: try {
1124: sb = new StringBuilder(fixture);
1125: sb.insert(5, new char[] { 'a', 'b' }, 0, 3);
1126: fail("no IOOBE, too long");
1127: } catch (IndexOutOfBoundsException e) {
1128: // Expected
1129: }
1130: }
1131:
1132: /**
1133: * @tests java.lang.StringBuilder.insert(int, double)
1134: */
1135: public void test_insertID() {
1136: final String fixture = "0000";
1137: StringBuilder sb = new StringBuilder(fixture);
1138: assertSame(sb, sb.insert(0, -1D));
1139: assertEquals("-1.00000", sb.toString());
1140: assertEquals(8, sb.length());
1141:
1142: sb = new StringBuilder(fixture);
1143: assertSame(sb, sb.insert(0, 0D));
1144: assertEquals("0.00000", sb.toString());
1145: assertEquals(7, sb.length());
1146:
1147: sb = new StringBuilder(fixture);
1148: assertSame(sb, sb.insert(2, 1D));
1149: assertEquals("001.000", sb.toString());
1150: assertEquals(7, sb.length());
1151:
1152: sb = new StringBuilder(fixture);
1153: assertSame(sb, sb.insert(4, 2D));
1154: assertEquals("00002.0", sb.toString());
1155: assertEquals(7, sb.length());
1156:
1157: try {
1158: sb = new StringBuilder(fixture);
1159: sb.insert(-1, 1D);
1160: fail("no IOOBE, negative index");
1161: } catch (IndexOutOfBoundsException e) {
1162: // Expected
1163: }
1164:
1165: try {
1166: sb = new StringBuilder(fixture);
1167: sb.insert(5, 1D);
1168: fail("no IOOBE, index too large index");
1169: } catch (IndexOutOfBoundsException e) {
1170: // Expected
1171: }
1172: }
1173:
1174: /**
1175: * @tests java.lang.StringBuilder.insert(int, float)
1176: */
1177: public void test_insertIF() {
1178: final String fixture = "0000";
1179: StringBuilder sb = new StringBuilder(fixture);
1180: assertSame(sb, sb.insert(0, -1F));
1181: assertEquals("-1.00000", sb.toString());
1182: assertEquals(8, sb.length());
1183:
1184: sb = new StringBuilder(fixture);
1185: assertSame(sb, sb.insert(0, 0F));
1186: assertEquals("0.00000", sb.toString());
1187: assertEquals(7, sb.length());
1188:
1189: sb = new StringBuilder(fixture);
1190: assertSame(sb, sb.insert(2, 1F));
1191: assertEquals("001.000", sb.toString());
1192: assertEquals(7, sb.length());
1193:
1194: sb = new StringBuilder(fixture);
1195: assertSame(sb, sb.insert(4, 2F));
1196: assertEquals("00002.0", sb.toString());
1197: assertEquals(7, sb.length());
1198:
1199: try {
1200: sb = new StringBuilder(fixture);
1201: sb.insert(-1, 1F);
1202: fail("no IOOBE, negative index");
1203: } catch (IndexOutOfBoundsException e) {
1204: // Expected
1205: }
1206:
1207: try {
1208: sb = new StringBuilder(fixture);
1209: sb.insert(5, 1F);
1210: fail("no IOOBE, index too large index");
1211: } catch (IndexOutOfBoundsException e) {
1212: // Expected
1213: }
1214: }
1215:
1216: /**
1217: * @tests java.lang.StringBuilder.insert(int, int)
1218: */
1219: public void test_insertII() {
1220: final String fixture = "0000";
1221: StringBuilder sb = new StringBuilder(fixture);
1222: assertSame(sb, sb.insert(0, -1));
1223: assertEquals("-10000", sb.toString());
1224: assertEquals(6, sb.length());
1225:
1226: sb = new StringBuilder(fixture);
1227: assertSame(sb, sb.insert(0, 0));
1228: assertEquals("00000", sb.toString());
1229: assertEquals(5, sb.length());
1230:
1231: sb = new StringBuilder(fixture);
1232: assertSame(sb, sb.insert(2, 1));
1233: assertEquals("00100", sb.toString());
1234: assertEquals(5, sb.length());
1235:
1236: sb = new StringBuilder(fixture);
1237: assertSame(sb, sb.insert(4, 2));
1238: assertEquals("00002", sb.toString());
1239: assertEquals(5, sb.length());
1240:
1241: try {
1242: sb = new StringBuilder(fixture);
1243: sb.insert(-1, 1);
1244: fail("no IOOBE, negative index");
1245: } catch (IndexOutOfBoundsException e) {
1246: // Expected
1247: }
1248:
1249: try {
1250: sb = new StringBuilder(fixture);
1251: sb.insert(5, 1);
1252: fail("no IOOBE, index too large index");
1253: } catch (IndexOutOfBoundsException e) {
1254: // Expected
1255: }
1256: }
1257:
1258: /**
1259: * @tests java.lang.StringBuilder.insert(int, long)
1260: */
1261: public void test_insertIJ() {
1262: final String fixture = "0000";
1263: StringBuilder sb = new StringBuilder(fixture);
1264: assertSame(sb, sb.insert(0, -1L));
1265: assertEquals("-10000", sb.toString());
1266: assertEquals(6, sb.length());
1267:
1268: sb = new StringBuilder(fixture);
1269: assertSame(sb, sb.insert(0, 0L));
1270: assertEquals("00000", sb.toString());
1271: assertEquals(5, sb.length());
1272:
1273: sb = new StringBuilder(fixture);
1274: assertSame(sb, sb.insert(2, 1L));
1275: assertEquals("00100", sb.toString());
1276: assertEquals(5, sb.length());
1277:
1278: sb = new StringBuilder(fixture);
1279: assertSame(sb, sb.insert(4, 2L));
1280: assertEquals("00002", sb.toString());
1281: assertEquals(5, sb.length());
1282:
1283: try {
1284: sb = new StringBuilder(fixture);
1285: sb.insert(-1, 1L);
1286: fail("no IOOBE, negative index");
1287: } catch (IndexOutOfBoundsException e) {
1288: // Expected
1289: }
1290:
1291: try {
1292: sb = new StringBuilder(fixture);
1293: sb.insert(5, 1L);
1294: fail("no IOOBE, index too large index");
1295: } catch (IndexOutOfBoundsException e) {
1296: // Expected
1297: }
1298: }
1299:
1300: /**
1301: * @tests java.lang.StringBuilder.insert(int, Object)
1302: */
1303: public void test_insertILjava_lang_Object() {
1304: final String fixture = "0000";
1305: StringBuilder sb = new StringBuilder(fixture);
1306: assertSame(sb, sb.insert(0, Fixture.INSTANCE));
1307: assertEquals("fixture0000", sb.toString());
1308: assertEquals(11, sb.length());
1309:
1310: sb = new StringBuilder(fixture);
1311: assertSame(sb, sb.insert(2, Fixture.INSTANCE));
1312: assertEquals("00fixture00", sb.toString());
1313: assertEquals(11, sb.length());
1314:
1315: sb = new StringBuilder(fixture);
1316: assertSame(sb, sb.insert(4, Fixture.INSTANCE));
1317: assertEquals("0000fixture", sb.toString());
1318: assertEquals(11, sb.length());
1319:
1320: sb = new StringBuilder(fixture);
1321: assertSame(sb, sb.insert(4, (Object) null));
1322: assertEquals("0000null", sb.toString());
1323: assertEquals(8, sb.length());
1324:
1325: try {
1326: sb = new StringBuilder(fixture);
1327: sb.insert(-1, Fixture.INSTANCE);
1328: fail("no IOOBE, negative index");
1329: } catch (IndexOutOfBoundsException e) {
1330: // Expected
1331: }
1332:
1333: try {
1334: sb = new StringBuilder(fixture);
1335: sb.insert(5, Fixture.INSTANCE);
1336: fail("no IOOBE, index too large index");
1337: } catch (IndexOutOfBoundsException e) {
1338: // Expected
1339: }
1340: }
1341:
1342: /**
1343: * @tests java.lang.StringBuilder.insert(int, String)
1344: */
1345: public void test_insertILjava_lang_String() {
1346: final String fixture = "0000";
1347: StringBuilder sb = new StringBuilder(fixture);
1348: assertSame(sb, sb.insert(0, "fixture"));
1349: assertEquals("fixture0000", sb.toString());
1350: assertEquals(11, sb.length());
1351:
1352: sb = new StringBuilder(fixture);
1353: assertSame(sb, sb.insert(2, "fixture"));
1354: assertEquals("00fixture00", sb.toString());
1355: assertEquals(11, sb.length());
1356:
1357: sb = new StringBuilder(fixture);
1358: assertSame(sb, sb.insert(4, "fixture"));
1359: assertEquals("0000fixture", sb.toString());
1360: assertEquals(11, sb.length());
1361:
1362: sb = new StringBuilder(fixture);
1363: assertSame(sb, sb.insert(4, (Object) null));
1364: assertEquals("0000null", sb.toString());
1365: assertEquals(8, sb.length());
1366:
1367: try {
1368: sb = new StringBuilder(fixture);
1369: sb.insert(-1, "fixture");
1370: fail("no IOOBE, negative index");
1371: } catch (IndexOutOfBoundsException e) {
1372: // Expected
1373: }
1374:
1375: try {
1376: sb = new StringBuilder(fixture);
1377: sb.insert(5, "fixture");
1378: fail("no IOOBE, index too large index");
1379: } catch (IndexOutOfBoundsException e) {
1380: // Expected
1381: }
1382: }
1383:
1384: /**
1385: * @tests java.lang.StringBuilder.lastIndexOf(String)
1386: */
1387: public void test_lastIndexOfLjava_lang_String() {
1388: final String fixture = "0123456789";
1389: StringBuilder sb = new StringBuilder(fixture);
1390: assertEquals(0, sb.lastIndexOf("0"));
1391: assertEquals(0, sb.lastIndexOf("012"));
1392: assertEquals(-1, sb.lastIndexOf("02"));
1393: assertEquals(8, sb.lastIndexOf("89"));
1394:
1395: try {
1396: sb.lastIndexOf(null);
1397: fail("no NPE");
1398: } catch (NullPointerException e) {
1399: // Expected
1400: }
1401: }
1402:
1403: /**
1404: * @tests java.lang.StringBuilder.lastIndexOf(String, int)
1405: */
1406: public void test_lastIndexOfLjava_lang_StringI() {
1407: final String fixture = "0123456789";
1408: StringBuilder sb = new StringBuilder(fixture);
1409: assertEquals(0, sb.lastIndexOf("0"));
1410: assertEquals(0, sb.lastIndexOf("012"));
1411: assertEquals(-1, sb.lastIndexOf("02"));
1412: assertEquals(8, sb.lastIndexOf("89"));
1413:
1414: assertEquals(0, sb.lastIndexOf("0"), 0);
1415: assertEquals(0, sb.lastIndexOf("012"), 0);
1416: assertEquals(-1, sb.lastIndexOf("02"), 0);
1417: assertEquals(8, sb.lastIndexOf("89"), 0);
1418:
1419: assertEquals(-1, sb.lastIndexOf("0"), 5);
1420: assertEquals(-1, sb.lastIndexOf("012"), 5);
1421: assertEquals(-1, sb.lastIndexOf("02"), 0);
1422: assertEquals(8, sb.lastIndexOf("89"), 5);
1423:
1424: try {
1425: sb.lastIndexOf(null, 0);
1426: fail("no NPE");
1427: } catch (NullPointerException e) {
1428: // Expected
1429: }
1430: }
1431:
1432: /**
1433: * @tests java.lang.StringBuilder.length()
1434: */
1435: public void test_length() {
1436: StringBuilder sb = new StringBuilder();
1437: assertEquals(0, sb.length());
1438: sb.append("0000");
1439: assertEquals(4, sb.length());
1440: }
1441:
1442: /**
1443: * @tests java.lang.StringBuilder.offsetByCodePoints(int, int)'
1444: */
1445: public void test_offsetByCodePointsII() {
1446: int result = new StringBuilder("a\uD800\uDC00b")
1447: .offsetByCodePoints(0, 2);
1448: assertEquals(3, result);
1449:
1450: result = new StringBuilder("abcd").offsetByCodePoints(3, -1);
1451: assertEquals(2, result);
1452:
1453: result = new StringBuilder("a\uD800\uDC00b")
1454: .offsetByCodePoints(0, 3);
1455: assertEquals(4, result);
1456:
1457: result = new StringBuilder("a\uD800\uDC00b")
1458: .offsetByCodePoints(3, -1);
1459: assertEquals(1, result);
1460:
1461: result = new StringBuilder("a\uD800\uDC00b")
1462: .offsetByCodePoints(3, 0);
1463: assertEquals(3, result);
1464:
1465: result = new StringBuilder("\uD800\uDC00bc")
1466: .offsetByCodePoints(3, 0);
1467: assertEquals(3, result);
1468:
1469: result = new StringBuilder("a\uDC00bc").offsetByCodePoints(3,
1470: -1);
1471: assertEquals(2, result);
1472:
1473: result = new StringBuilder("a\uD800bc").offsetByCodePoints(3,
1474: -1);
1475: assertEquals(2, result);
1476:
1477: StringBuilder sb = new StringBuilder();
1478: sb.append("abc");
1479: try {
1480: sb.offsetByCodePoints(-1, 1);
1481: fail("No IOOBE for negative index.");
1482: } catch (IndexOutOfBoundsException e) {
1483:
1484: }
1485:
1486: try {
1487: sb.offsetByCodePoints(0, 4);
1488: fail("No IOOBE for offset that's too large.");
1489: } catch (IndexOutOfBoundsException e) {
1490:
1491: }
1492:
1493: try {
1494: sb.offsetByCodePoints(3, -4);
1495: fail("No IOOBE for offset that's too small.");
1496: } catch (IndexOutOfBoundsException e) {
1497:
1498: }
1499:
1500: try {
1501: sb.offsetByCodePoints(3, 1);
1502: fail("No IOOBE for index that's too large.");
1503: } catch (IndexOutOfBoundsException e) {
1504:
1505: }
1506:
1507: try {
1508: sb.offsetByCodePoints(4, -1);
1509: fail("No IOOBE for index that's too large.");
1510: } catch (IndexOutOfBoundsException e) {
1511:
1512: }
1513: }
1514:
1515: /**
1516: * @tests java.lang.StringBuilder.replace(int, int, String)'
1517: */
1518: public void test_replaceIILjava_lang_String() {
1519: final String fixture = "0000";
1520: StringBuilder sb = new StringBuilder(fixture);
1521: assertSame(sb, sb.replace(1, 3, "11"));
1522: assertEquals("0110", sb.toString());
1523: assertEquals(4, sb.length());
1524:
1525: sb = new StringBuilder(fixture);
1526: assertSame(sb, sb.replace(1, 2, "11"));
1527: assertEquals("01100", sb.toString());
1528: assertEquals(5, sb.length());
1529:
1530: sb = new StringBuilder(fixture);
1531: assertSame(sb, sb.replace(4, 5, "11"));
1532: assertEquals("000011", sb.toString());
1533: assertEquals(6, sb.length());
1534:
1535: sb = new StringBuilder(fixture);
1536: assertSame(sb, sb.replace(4, 6, "11"));
1537: assertEquals("000011", sb.toString());
1538: assertEquals(6, sb.length());
1539:
1540: // FIXME Undocumented NPE in Sun's JRE 5.0_5
1541: try {
1542: sb.replace(1, 2, null);
1543: fail("No NPE");
1544: } catch (NullPointerException e) {
1545: // Expected
1546: }
1547:
1548: try {
1549: sb = new StringBuilder(fixture);
1550: sb.replace(-1, 2, "11");
1551: fail("No SIOOBE, negative start");
1552: } catch (StringIndexOutOfBoundsException e) {
1553: // Expected
1554: }
1555:
1556: try {
1557: sb = new StringBuilder(fixture);
1558: sb.replace(5, 2, "11");
1559: fail("No SIOOBE, start > length");
1560: } catch (StringIndexOutOfBoundsException e) {
1561: // Expected
1562: }
1563:
1564: try {
1565: sb = new StringBuilder(fixture);
1566: sb.replace(3, 2, "11");
1567: fail("No SIOOBE, start > end");
1568: } catch (StringIndexOutOfBoundsException e) {
1569: // Expected
1570: }
1571:
1572: // Regression for HARMONY-348
1573: StringBuilder buffer = new StringBuilder("1234567");
1574: buffer.replace(2, 6, "XXX");
1575: assertEquals("12XXX7", buffer.toString());
1576: }
1577:
1578: /**
1579: * @tests java.lang.StringBuilder.reverse()
1580: */
1581: public void test_reverse() {
1582: final String fixture = "0123456789";
1583: StringBuilder sb = new StringBuilder(fixture);
1584: assertSame(sb, sb.reverse());
1585: assertEquals("9876543210", sb.toString());
1586:
1587: sb = new StringBuilder("012345678");
1588: assertSame(sb, sb.reverse());
1589: assertEquals("876543210", sb.toString());
1590:
1591: sb.setLength(1);
1592: assertSame(sb, sb.reverse());
1593: assertEquals("8", sb.toString());
1594:
1595: sb.setLength(0);
1596: assertSame(sb, sb.reverse());
1597: assertEquals("", sb.toString());
1598: }
1599:
1600: /**
1601: * @tests java.lang.StringBuilder.setCharAt(int, char)
1602: */
1603: public void test_setCharAtIC() {
1604: final String fixture = "0000";
1605: StringBuilder sb = new StringBuilder(fixture);
1606: sb.setCharAt(0, 'A');
1607: assertEquals("A000", sb.toString());
1608: sb.setCharAt(1, 'B');
1609: assertEquals("AB00", sb.toString());
1610: sb.setCharAt(2, 'C');
1611: assertEquals("ABC0", sb.toString());
1612: sb.setCharAt(3, 'D');
1613: assertEquals("ABCD", sb.toString());
1614:
1615: try {
1616: sb.setCharAt(-1, 'A');
1617: fail("No IOOBE, negative index");
1618: } catch (IndexOutOfBoundsException e) {
1619: // Expected
1620: }
1621:
1622: try {
1623: sb.setCharAt(4, 'A');
1624: fail("No IOOBE, index == length");
1625: } catch (IndexOutOfBoundsException e) {
1626: // Expected
1627: }
1628:
1629: try {
1630: sb.setCharAt(5, 'A');
1631: fail("No IOOBE, index > length");
1632: } catch (IndexOutOfBoundsException e) {
1633: // Expected
1634: }
1635: }
1636:
1637: /**
1638: * @tests java.lang.StringBuilder.setLength(int)'
1639: */
1640: public void test_setLengthI() {
1641: final String fixture = "0123456789";
1642: StringBuilder sb = new StringBuilder(fixture);
1643: sb.setLength(5);
1644: assertEquals(5, sb.length());
1645: assertEquals("01234", sb.toString());
1646: sb.setLength(6);
1647: assertEquals(6, sb.length());
1648: assertEquals("01234\0", sb.toString());
1649: sb.setLength(0);
1650: assertEquals(0, sb.length());
1651: assertEquals("", sb.toString());
1652:
1653: try {
1654: sb.setLength(-1);
1655: fail("No IOOBE, negative length.");
1656: } catch (IndexOutOfBoundsException e) {
1657: // Expected
1658: }
1659: }
1660:
1661: /**
1662: * @tests java.lang.StringBuilder.subSequence(int, int)
1663: */
1664: public void test_subSequenceII() {
1665: final String fixture = "0123456789";
1666: StringBuilder sb = new StringBuilder(fixture);
1667: CharSequence ss = sb.subSequence(0, 5);
1668: assertEquals("01234", ss.toString());
1669:
1670: ss = sb.subSequence(0, 0);
1671: assertEquals("", ss.toString());
1672:
1673: try {
1674: sb.subSequence(-1, 1);
1675: fail("No IOOBE, negative start.");
1676: } catch (IndexOutOfBoundsException e) {
1677: // Expected
1678: }
1679:
1680: try {
1681: sb.subSequence(0, -1);
1682: fail("No IOOBE, negative end.");
1683: } catch (IndexOutOfBoundsException e) {
1684: // Expected
1685: }
1686:
1687: try {
1688: sb.subSequence(0, fixture.length() + 1);
1689: fail("No IOOBE, end > length.");
1690: } catch (IndexOutOfBoundsException e) {
1691: // Expected
1692: }
1693:
1694: try {
1695: sb.subSequence(3, 2);
1696: fail("No IOOBE, start > end.");
1697: } catch (IndexOutOfBoundsException e) {
1698: // Expected
1699: }
1700: }
1701:
1702: /**
1703: * @tests java.lang.StringBuilder.substring(int)
1704: */
1705: public void test_substringI() {
1706: final String fixture = "0123456789";
1707: StringBuilder sb = new StringBuilder(fixture);
1708: String ss = sb.substring(0);
1709: assertEquals(fixture, ss);
1710:
1711: ss = sb.substring(10);
1712: assertEquals("", ss);
1713:
1714: try {
1715: sb.substring(-1);
1716: fail("No SIOOBE, negative start.");
1717: } catch (StringIndexOutOfBoundsException e) {
1718: // Expected
1719: }
1720:
1721: try {
1722: sb.substring(0, -1);
1723: fail("No SIOOBE, negative end.");
1724: } catch (StringIndexOutOfBoundsException e) {
1725: // Expected
1726: }
1727:
1728: try {
1729: sb.substring(fixture.length() + 1);
1730: fail("No SIOOBE, start > length.");
1731: } catch (StringIndexOutOfBoundsException e) {
1732: // Expected
1733: }
1734: }
1735:
1736: /**
1737: * @tests java.lang.StringBuilder.substring(int, int)
1738: */
1739: public void test_substringII() {
1740: final String fixture = "0123456789";
1741: StringBuilder sb = new StringBuilder(fixture);
1742: String ss = sb.substring(0, 5);
1743: assertEquals("01234", ss);
1744:
1745: ss = sb.substring(0, 0);
1746: assertEquals("", ss);
1747:
1748: try {
1749: sb.substring(-1, 1);
1750: fail("No SIOOBE, negative start.");
1751: } catch (StringIndexOutOfBoundsException e) {
1752: // Expected
1753: }
1754:
1755: try {
1756: sb.substring(0, -1);
1757: fail("No SIOOBE, negative end.");
1758: } catch (StringIndexOutOfBoundsException e) {
1759: // Expected
1760: }
1761:
1762: try {
1763: sb.substring(0, fixture.length() + 1);
1764: fail("No SIOOBE, end > length.");
1765: } catch (StringIndexOutOfBoundsException e) {
1766: // Expected
1767: }
1768:
1769: try {
1770: sb.substring(3, 2);
1771: fail("No SIOOBE, start > end.");
1772: } catch (StringIndexOutOfBoundsException e) {
1773: // Expected
1774: }
1775: }
1776:
1777: /**
1778: * @tests java.lang.StringBuilder.toString()'
1779: */
1780: public void test_toString() {
1781: final String fixture = "0123456789";
1782: StringBuilder sb = new StringBuilder(fixture);
1783: assertEquals(fixture, sb.toString());
1784: }
1785:
1786: /**
1787: * @tests java.lang.StringBuilder.trimToSize()'
1788: */
1789: public void test_trimToSize() {
1790: final String fixture = "0123456789";
1791: StringBuilder sb = new StringBuilder(fixture);
1792: assertTrue(sb.capacity() > fixture.length());
1793: assertEquals(fixture.length(), sb.length());
1794: assertEquals(fixture, sb.toString());
1795: int prevCapacity = sb.capacity();
1796: sb.trimToSize();
1797: assertTrue(prevCapacity > sb.capacity());
1798: assertEquals(fixture.length(), sb.length());
1799: assertEquals(fixture, sb.toString());
1800: }
1801:
1802: // comparator for StringBuilder objects
1803: private static final SerializableAssert STRING_BILDER_COMPARATOR = new SerializableAssert() {
1804: public void assertDeserialized(Serializable initial,
1805: Serializable deserialized) {
1806:
1807: StringBuilder init = (StringBuilder) initial;
1808: StringBuilder desr = (StringBuilder) deserialized;
1809:
1810: assertEquals("toString", init.toString(), desr.toString());
1811: }
1812: };
1813:
1814: /**
1815: * @tests serialization/deserialization.
1816: */
1817: public void testSerializationSelf() throws Exception {
1818:
1819: SerializationTest.verifySelf(new StringBuilder("0123456789"),
1820: STRING_BILDER_COMPARATOR);
1821: }
1822:
1823: /**
1824: * @tests serialization/deserialization compatibility with RI.
1825: */
1826: public void testSerializationCompatibility() throws Exception {
1827:
1828: SerializationTest.verifyGolden(this , new StringBuilder(
1829: "0123456789"), STRING_BILDER_COMPARATOR);
1830: }
1831:
1832: private static final class Fixture {
1833: static final Fixture INSTANCE = new Fixture();
1834:
1835: private Fixture() {
1836: super ();
1837: }
1838:
1839: @Override
1840: public String toString() {
1841: return "fixture";
1842: }
1843: }
1844: }
|