You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/08/09 06:42:00 UTC
svn commit: r429959 [3/3] - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java:
org/apache/harmony/luni/tests/java/lang/ tests/api/java/lang/
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/ShortTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/ShortTest.java?rev=429959&r1=429958&r2=429959&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/ShortTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/ShortTest.java Tue Aug 8 21:41:59 2006
@@ -18,7 +18,328 @@
import junit.framework.TestCase;
public class ShortTest extends TestCase {
+ private Short sp = new Short((short) 18000);
+ private Short sp2 = new Short((short) 18000);
+ private Short sn = new Short((short) -19000);
+ /**
+ * @tests java.lang.Short#byteValue()
+ */
+ public void test_byteValue() {
+ // Test for method byte java.lang.Short.byteValue()
+ assertEquals("Returned incorrect byte value", 0, new Short(Short.MIN_VALUE)
+ .byteValue());
+ assertEquals("Returned incorrect byte value", -1, new Short(Short.MAX_VALUE)
+ .byteValue());
+ }
+
+ /**
+ * @tests java.lang.Short#compareTo(java.lang.Short)
+ */
+ public void test_compareToLjava_lang_Short() {
+ // Test for method int java.lang.Short.compareTo(java.lang.Short)
+ Short s = new Short((short) 1);
+ Short x = new Short((short) 3);
+ assertTrue(
+ "Should have returned negative value when compared to greater short",
+ s.compareTo(x) < 0);
+ x = new Short((short) -1);
+ assertTrue(
+ "Should have returned positive value when compared to lesser short",
+ s.compareTo(x) > 0);
+ x = new Short((short) 1);
+ assertEquals("Should have returned zero when compared to equal short",
+ 0, s.compareTo(x));
+
+ try {
+ new Short((short)0).compareTo(null);
+ fail("No NPE");
+ } catch (NullPointerException e) {
+ }
+ }
+
+ /**
+ * @tests java.lang.Short#decode(java.lang.String)
+ */
+ public void test_decodeLjava_lang_String2() {
+ // Test for method java.lang.Short
+ // java.lang.Short.decode(java.lang.String)
+ assertTrue("Did not decode -1 correctly", Short.decode("-1")
+ .shortValue() == (short) -1);
+ assertTrue("Did not decode -100 correctly", Short.decode("-100")
+ .shortValue() == (short) -100);
+ assertTrue("Did not decode 23 correctly", Short.decode("23")
+ .shortValue() == (short) 23);
+ assertTrue("Did not decode 0x10 correctly", Short.decode("0x10")
+ .shortValue() == (short) 16);
+ assertTrue("Did not decode 32767 correctly", Short.decode("32767")
+ .shortValue() == (short) 32767);
+ assertTrue("Did not decode -32767 correctly", Short.decode("-32767")
+ .shortValue() == (short) -32767);
+ assertTrue("Did not decode -32768 correctly", Short.decode("-32768")
+ .shortValue() == (short) -32768);
+
+ boolean exception = false;
+ try {
+ Short.decode("123s");
+ } catch (NumberFormatException e) {
+ // correct
+ exception = true;
+ }
+ assertTrue("Did not throw NumberFormatException decoding 123s",
+ exception);
+
+ exception = false;
+ try {
+ Short.decode("32768");
+ } catch (NumberFormatException e) {
+ // Correct
+ exception = true;
+ }
+ assertTrue("Failed to throw exception for MAX_VALUE + 1", exception);
+
+ exception = false;
+ try {
+ Short.decode("-32769");
+ } catch (NumberFormatException e) {
+ // Correct
+ exception = true;
+ }
+ assertTrue("Failed to throw exception for MIN_VALUE - 1", exception);
+
+ exception = false;
+ try {
+ Short.decode("0x8000");
+ } catch (NumberFormatException e) {
+ // Correct
+ exception = true;
+ }
+ assertTrue("Failed to throw exception for hex MAX_VALUE + 1", exception);
+
+ exception = false;
+ try {
+ Short.decode("-0x8001");
+ } catch (NumberFormatException e) {
+ // Correct
+ exception = true;
+ }
+ assertTrue("Failed to throw exception for hex MIN_VALUE - 1", exception);
+ }
+
+ /**
+ * @tests java.lang.Short#parseShort(java.lang.String)
+ */
+ public void test_parseShortLjava_lang_String2() {
+ // Test for method short java.lang.Short.parseShort(java.lang.String)
+ short sp = Short.parseShort("32746");
+ short sn = Short.parseShort("-32746");
+
+ assertTrue("Incorrect parse of short", sp == (short) 32746
+ && (sn == (short) -32746));
+ assertEquals("Returned incorrect value for 0", 0, Short.parseShort("0"));
+ assertTrue("Returned incorrect value for most negative value", Short
+ .parseShort("-32768") == (short) 0x8000);
+ assertTrue("Returned incorrect value for most positive value", Short
+ .parseShort("32767") == 0x7fff);
+
+ boolean exception = false;
+ try {
+ Short.parseShort("32768");
+ } catch (NumberFormatException e) {
+ // Correct
+ exception = true;
+ }
+ assertTrue("Failed to throw exception for MAX_VALUE + 1", exception);
+
+ exception = false;
+ try {
+ Short.parseShort("-32769");
+ } catch (NumberFormatException e) {
+ // Correct
+ exception = true;
+ }
+ assertTrue("Failed to throw exception for MIN_VALUE - 1", exception);
+ }
+
+ /**
+ * @tests java.lang.Short#parseShort(java.lang.String, int)
+ */
+ public void test_parseShortLjava_lang_StringI2() {
+ // Test for method short java.lang.Short.parseShort(java.lang.String,
+ // int)
+ boolean aThrow = true;
+ assertEquals("Incorrectly parsed hex string",
+ 255, Short.parseShort("FF", 16));
+ assertEquals("Incorrectly parsed oct string",
+ 16, Short.parseShort("20", 8));
+ assertEquals("Incorrectly parsed dec string",
+ 20, Short.parseShort("20", 10));
+ assertEquals("Incorrectly parsed bin string",
+ 4, Short.parseShort("100", 2));
+ assertEquals("Incorrectly parsed -hex string", -255, Short
+ .parseShort("-FF", 16));
+ assertEquals("Incorrectly parsed -oct string",
+ -16, Short.parseShort("-20", 8));
+ assertEquals("Incorrectly parsed -bin string", -4, Short
+ .parseShort("-100", 2));
+ assertEquals("Returned incorrect value for 0 hex", 0, Short.parseShort("0",
+ 16));
+ assertTrue("Returned incorrect value for most negative value hex",
+ Short.parseShort("-8000", 16) == (short) 0x8000);
+ assertTrue("Returned incorrect value for most positive value hex",
+ Short.parseShort("7fff", 16) == 0x7fff);
+ assertEquals("Returned incorrect value for 0 decimal", 0, Short.parseShort(
+ "0", 10));
+ assertTrue("Returned incorrect value for most negative value decimal",
+ Short.parseShort("-32768", 10) == (short) 0x8000);
+ assertTrue("Returned incorrect value for most positive value decimal",
+ Short.parseShort("32767", 10) == 0x7fff);
+
+ try {
+ Short.parseShort("FF", 2);
+ } catch (NumberFormatException e) {
+ // Correct
+ aThrow = false;
+ }
+ if (aThrow) {
+ fail(
+ "Failed to throw exception when passed hex string and base 2 radix");
+ }
+
+ boolean exception = false;
+ try {
+ Short.parseShort("10000000000", 10);
+ } catch (NumberFormatException e) {
+ // Correct
+ exception = true;
+ }
+ assertTrue(
+ "Failed to throw exception when passed string larger than 16 bits",
+ exception);
+
+ exception = false;
+ try {
+ Short.parseShort("32768", 10);
+ } catch (NumberFormatException e) {
+ // Correct
+ exception = true;
+ }
+ assertTrue("Failed to throw exception for MAX_VALUE + 1", exception);
+
+ exception = false;
+ try {
+ Short.parseShort("-32769", 10);
+ } catch (NumberFormatException e) {
+ // Correct
+ exception = true;
+ }
+ assertTrue("Failed to throw exception for MIN_VALUE - 1", exception);
+
+ exception = false;
+ try {
+ Short.parseShort("8000", 16);
+ } catch (NumberFormatException e) {
+ // Correct
+ exception = true;
+ }
+ assertTrue("Failed to throw exception for hex MAX_VALUE + 1", exception);
+
+ exception = false;
+ try {
+ Short.parseShort("-8001", 16);
+ } catch (NumberFormatException e) {
+ // Correct
+ exception = true;
+ }
+ assertTrue("Failed to throw exception for hex MIN_VALUE + 1", exception);
+ }
+
+ /**
+ * @tests java.lang.Short#toString()
+ */
+ public void test_toString2() {
+ // Test for method java.lang.String java.lang.Short.toString()
+ assertTrue("Invalid string returned", sp.toString().equals("18000")
+ && (sn.toString().equals("-19000")));
+ assertEquals("Returned incorrect string", "32767", new Short((short) 32767)
+ .toString());
+ assertEquals("Returned incorrect string", "-32767", new Short((short) -32767)
+ .toString());
+ assertEquals("Returned incorrect string", "-32768", new Short((short) -32768)
+ .toString());
+ }
+
+ /**
+ * @tests java.lang.Short#toString(short)
+ */
+ public void test_toStringS2() {
+ // Test for method java.lang.String java.lang.Short.toString(short)
+ assertEquals("Returned incorrect string", "32767", Short.toString((short) 32767)
+ );
+ assertEquals("Returned incorrect string", "-32767", Short.toString((short) -32767)
+ );
+ assertEquals("Returned incorrect string", "-32768", Short.toString((short) -32768)
+ );
+ }
+
+ /**
+ * @tests java.lang.Short#valueOf(java.lang.String)
+ */
+ public void test_valueOfLjava_lang_String2() {
+ // Test for method java.lang.Short
+ // java.lang.Short.valueOf(java.lang.String)
+ assertEquals("Returned incorrect short", -32768, Short.valueOf("-32768")
+ .shortValue());
+ assertEquals("Returned incorrect short", 32767, Short.valueOf("32767")
+ .shortValue());
+ }
+
+ /**
+ * @tests java.lang.Short#valueOf(java.lang.String, int)
+ */
+ public void test_valueOfLjava_lang_StringI2() {
+ // Test for method java.lang.Short
+ // java.lang.Short.valueOf(java.lang.String, int)
+ boolean aThrow = true;
+ assertEquals("Incorrectly parsed hex string", 255, Short.valueOf("FF", 16)
+ .shortValue());
+ assertEquals("Incorrectly parsed oct string", 16, Short.valueOf("20", 8)
+ .shortValue());
+ assertEquals("Incorrectly parsed dec string", 20, Short.valueOf("20", 10)
+ .shortValue());
+ assertEquals("Incorrectly parsed bin string", 4, Short.valueOf("100", 2)
+ .shortValue());
+ assertEquals("Incorrectly parsed -hex string", -255, Short.valueOf("-FF", 16)
+ .shortValue());
+ assertEquals("Incorrectly parsed -oct string", -16, Short.valueOf("-20", 8)
+ .shortValue());
+ assertEquals("Incorrectly parsed -bin string", -4, Short.valueOf("-100", 2)
+ .shortValue());
+ assertTrue("Did not decode 32767 correctly", Short.valueOf("32767", 10)
+ .shortValue() == (short) 32767);
+ assertTrue("Did not decode -32767 correctly", Short.valueOf("-32767",
+ 10).shortValue() == (short) -32767);
+ assertTrue("Did not decode -32768 correctly", Short.valueOf("-32768",
+ 10).shortValue() == (short) -32768);
+ try {
+ Short.valueOf("FF", 2);
+ } catch (NumberFormatException e) {
+ // Correct
+ aThrow = false;
+ }
+ if (aThrow) {
+ fail(
+ "Failed to throw exception when passed hex string and base 2 radix");
+ }
+ try {
+ Short.valueOf("10000000000", 10);
+ } catch (NumberFormatException e) {
+ // Correct
+ return;
+ }
+ fail(
+ "Failed to throw exception when passed string larger than 16 bits");
+ }
/**
* @tests java.lang.Short#valueOf(byte)
*/
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java?rev=429959&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java Tue Aug 8 21:41:59 2006
@@ -0,0 +1,913 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.luni.tests.java.lang;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Locale;
+
+public class String2Test extends junit.framework.TestCase {
+
+ String hw1 = "HelloWorld";
+
+ String hw2 = "HelloWorld";
+
+ String hwlc = "helloworld";
+
+ String hwuc = "HELLOWORLD";
+
+ String hello1 = "Hello";
+
+ String world1 = "World";
+
+ String comp11 = "Test String";
+
+ Object obj = new Object();
+
+ char[] buf = { 'W', 'o', 'r', 'l', 'd' };
+
+ char[] rbuf = new char[5];
+
+ /**
+ * @tests java.lang.String#String()
+ */
+ public void test_Constructor() {
+ // Test for method java.lang.String()
+ assertTrue("Created incorrect string", new String().equals(""));
+ }
+
+ /**
+ * @tests java.lang.String#String(byte[])
+ */
+ public void test_Constructor$B() {
+ // Test for method java.lang.String(byte [])
+ assertTrue("Failed to create string", new String(hw1.getBytes())
+ .equals(hw1));
+ }
+
+ /**
+ * @tests java.lang.String#String(byte[], int)
+ */
+ public void test_Constructor$BI() {
+ // Test for method java.lang.String(byte [], int)
+ String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0);
+ assertTrue("Incorrect string returned: " + s, s.equals("ABCDE"));
+ s = new String(new byte[] { 65, 66, 67, 68, 69 }, 1);
+ assertTrue("Did not use nonzero hibyte", !s.equals("ABCDE"));
+ }
+
+ /**
+ * @tests java.lang.String#String(byte[], int, int)
+ */
+ public void test_Constructor$BII() {
+ // Test for method java.lang.String(byte [], int, int)
+ assertTrue("Failed to create string", new String(hw1.getBytes(), 0, hw1
+ .getBytes().length).equals(hw1));
+
+ boolean exception = false;
+ try {
+ new String(new byte[0], 0, Integer.MAX_VALUE);
+ } catch (IndexOutOfBoundsException e) {
+ exception = true;
+ }
+ assertTrue("Did not throw exception", exception);
+ }
+
+ /**
+ * @tests java.lang.String#String(byte[], int, int, int)
+ */
+ public void test_Constructor$BIII() {
+ // Test for method java.lang.String(byte [], int, int, int)
+ String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 1, 3);
+ assertTrue("Incorrect string returned: " + s, s.equals("BCD"));
+ s = new String(new byte[] { 65, 66, 67, 68, 69 }, 1, 0, 5);
+ assertTrue("Did not use nonzero hibyte", !s.equals("ABCDE"));
+ }
+
+ /**
+ * @tests java.lang.String#String(byte[], int, int, java.lang.String)
+ */
+ public void test_Constructor$BIILjava_lang_String() {
+ // Test for method java.lang.String(byte [], int, int, java.lang.String)
+ String s = null;
+ try {
+ s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 5, "8859_1");
+ } catch (Exception e) {
+ fail("Threw exception : " + e.getMessage());
+ }
+ assertTrue("Incorrect string returned: " + s, s.equals("ABCDE"));
+ }
+
+ /**
+ * @tests java.lang.String#String(byte[], java.lang.String)
+ */
+ public void test_Constructor$BLjava_lang_String() {
+ // Test for method java.lang.String(byte [], java.lang.String)
+ String s = null;
+ try {
+ s = new String(new byte[] { 65, 66, 67, 68, 69 }, "8859_1");
+ } catch (Exception e) {
+ fail("Threw exception : " + e.getMessage());
+ }
+ assertTrue("Incorrect string returned: " + s, s.equals("ABCDE"));
+ }
+
+ /**
+ * @tests java.lang.String#String(char[])
+ */
+ public void test_Constructor$C() {
+ // Test for method java.lang.String(char [])
+ assertEquals("Failed Constructor test", "World", new String(buf));
+ }
+
+ /**
+ * @tests java.lang.String#String(char[], int, int)
+ */
+ public void test_Constructor$CII() {
+ // Test for method java.lang.String(char [], int, int)
+ char[] buf = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
+ String s = new String(buf, 0, buf.length);
+ assertTrue("Incorrect string created", hw1.equals(s));
+
+ boolean exception = false;
+ try {
+ new String(new char[0], 0, Integer.MAX_VALUE);
+ } catch (IndexOutOfBoundsException e) {
+ exception = true;
+ }
+ assertTrue("Did not throw exception", exception);
+ }
+
+ /**
+ * @tests java.lang.String#String(int[], int, int)
+ */
+ public void test_Constructor$III() {
+ // Test for method java.lang.String(int [], int, int)
+ try {
+ new String(new int[0], 2, Integer.MAX_VALUE);
+ fail("Did not throw exception");
+ } catch (IndexOutOfBoundsException e) {
+ //expected
+ }
+ }
+
+ /**
+ * @tests java.lang.String#String(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method java.lang.String(java.lang.String)
+ String s = new String("Hello World");
+ assertEquals("Failed to construct correct string", "Hello World", s
+ );
+ }
+
+ /**
+ * @tests java.lang.String#String(java.lang.StringBuffer)
+ */
+ public void test_ConstructorLjava_lang_StringBuffer() {
+ // Test for method java.lang.String(java.lang.StringBuffer)
+ StringBuffer sb = new StringBuffer();
+ sb.append("HelloWorld");
+ assertEquals("Created incorrect string", "HelloWorld", new String(sb)
+ );
+ }
+
+ /**
+ * @tests java.lang.String#charAt(int)
+ */
+ public void test_charAtI() {
+ // Test for method char java.lang.String.charAt(int)
+ assertTrue("Incorrect character returned", hw1.charAt(5) == 'W'
+ && (hw1.charAt(1) != 'Z'));
+ }
+
+ /**
+ * @tests java.lang.String#compareTo(java.lang.String)
+ */
+ public void test_compareToLjava_lang_String() {
+ // Test for method int java.lang.String.compareTo(java.lang.String)
+ assertTrue("Returned incorrect value for first < second", "aaaaab"
+ .compareTo("aaaaac") < 0);
+ assertEquals("Returned incorrect value for first = second", 0, "aaaaac"
+ .compareTo("aaaaac"));
+ assertTrue("Returned incorrect value for first > second", "aaaaac"
+ .compareTo("aaaaab") > 0);
+ assertTrue("Considered case to not be of importance", !("A"
+ .compareTo("a") == 0));
+
+ try {
+ "fixture".compareTo(null);
+ fail("No NPE");
+ } catch (NullPointerException e) {
+ }
+ }
+
+ /**
+ * @tests java.lang.String#compareToIgnoreCase(java.lang.String)
+ */
+ public void test_compareToIgnoreCaseLjava_lang_String() {
+ // Test for method int
+ // java.lang.String.compareToIgnoreCase(java.lang.String)
+ assertTrue("Returned incorrect value for first < second", "aaaaab"
+ .compareToIgnoreCase("aaaaac") < 0);
+ assertEquals("Returned incorrect value for first = second", 0, "aaaaac"
+ .compareToIgnoreCase("aaaaac"));
+ assertTrue("Returned incorrect value for first > second", "aaaaac"
+ .compareToIgnoreCase("aaaaab") > 0);
+ assertEquals("Considered case to not be of importance", 0, "A"
+ .compareToIgnoreCase("a"));
+
+ assertTrue("0xbf should not compare = to 'ss'", "\u00df"
+ .compareToIgnoreCase("ss") != 0);
+ assertEquals("0x130 should compare = to 'i'", 0, "\u0130"
+ .compareToIgnoreCase("i"));
+ assertEquals("0x131 should compare = to 'i'", 0, "\u0131"
+ .compareToIgnoreCase("i"));
+
+ Locale defLocale = Locale.getDefault();
+ try {
+ Locale.setDefault(new Locale("tr", ""));
+ assertEquals("Locale tr: 0x130 should compare = to 'i'", 0, "\u0130"
+ .compareToIgnoreCase("i"));
+ assertEquals("Locale tr: 0x131 should compare = to 'i'", 0, "\u0131"
+ .compareToIgnoreCase("i"));
+ } finally {
+ Locale.setDefault(defLocale);
+ }
+
+ try {
+ "fixture".compareToIgnoreCase(null);
+ fail("No NPE");
+ } catch (NullPointerException e) {
+ }
+ }
+
+ /**
+ * @tests java.lang.String#concat(java.lang.String)
+ */
+ public void test_concatLjava_lang_String() {
+ // Test for method java.lang.String
+ // java.lang.String.concat(java.lang.String)
+ assertTrue("Concatenation failed to produce correct string", hello1
+ .concat(world1).equals(hw1));
+ boolean exception = false;
+ try {
+ String a = new String("test");
+ String b = null;
+ a.concat(b);
+ } catch (NullPointerException e) {
+ exception = true;
+ }
+ assertTrue("Concatenation failed to throw NP exception (1)", exception);
+ exception = false;
+ try {
+ String a = new String("");
+ String b = null;
+ a.concat(b);
+ } catch (NullPointerException e) {
+ exception = true;
+ }
+ assertTrue("Concatenation failed to throw NP exception (2)", exception);
+
+ String s1 = "";
+ String s2 = "s2";
+ String s3 = s1.concat(s2);
+ assertEquals(s2, s3);
+
+ s3 = s2.concat(s1);
+ assertSame(s2, s3);
+ }
+
+ /**
+ * @tests java.lang.String#copyValueOf(char[])
+ */
+ public void test_copyValueOf$C() {
+ // Test for method java.lang.String java.lang.String.copyValueOf(char
+ // [])
+ char[] t = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
+ assertEquals("copyValueOf returned incorrect String", "HelloWorld", String.copyValueOf(
+ t));
+ }
+
+ /**
+ * @tests java.lang.String#copyValueOf(char[], int, int)
+ */
+ public void test_copyValueOf$CII() {
+ // Test for method java.lang.String java.lang.String.copyValueOf(char
+ // [], int, int)
+ char[] t = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
+ assertEquals("copyValueOf returned incorrect String", "World", String.copyValueOf(
+ t, 5, 5));
+ }
+
+ /**
+ * @tests java.lang.String#endsWith(java.lang.String)
+ */
+ public void test_endsWithLjava_lang_String() {
+ // Test for method boolean java.lang.String.endsWith(java.lang.String)
+ assertTrue("Failed to fine ending string", hw1.endsWith("ld"));
+ }
+
+ /**
+ * @tests java.lang.String#equals(java.lang.Object)
+ */
+ public void test_equalsLjava_lang_Object() {
+ // Test for method boolean java.lang.String.equals(java.lang.Object)
+ assertTrue("String not equal", hw1.equals(hw2) && !(hw1.equals(comp11)));
+ }
+
+ /**
+ * @tests java.lang.String#equalsIgnoreCase(java.lang.String)
+ */
+ public void test_equalsIgnoreCaseLjava_lang_String() {
+ // Test for method boolean
+ // java.lang.String.equalsIgnoreCase(java.lang.String)
+ assertTrue("lc version returned unequal to uc", hwlc
+ .equalsIgnoreCase(hwuc));
+ }
+
+ /**
+ * @tests java.lang.String#getBytes()
+ */
+ public void test_getBytes() {
+ // Test for method byte [] java.lang.String.getBytes()
+ byte[] sbytes = hw1.getBytes();
+ for (int i = 0; i < hw1.length(); i++)
+ assertTrue("Returned incorrect bytes", sbytes[i] == (byte) hw1
+ .charAt(i));
+
+ char[] chars = new char[1];
+ for (int i = 0; i < 65536; i++) {
+ // skip surrogates
+ if (i == 0xd800)
+ i = 0xe000;
+ byte[] result = null;
+ chars[0] = (char) i;
+ String string = new String(chars);
+ try {
+ result = string.getBytes("8859_1");
+ if (i < 256) {
+ assertEquals((byte)i, result[0]);
+ } else {
+ /*
+ * Substitue character should be 0x1A [1], but may be
+ * '?' character.
+ * [1] http://en.wikipedia.org/wiki/Substitute_character
+ */
+ assertTrue(result[0] == '?' || result[0] == 0x1a);
+ }
+ } catch (java.io.UnsupportedEncodingException e) {
+ }
+ try {
+ result = string.getBytes("UTF8");
+ int length = i < 0x80 ? 1 : (i < 0x800 ? 2 : 3);
+ assertTrue("Wrong length UTF8: " + Integer.toHexString(i),
+ result.length == length);
+ assertTrue(
+ "Wrong bytes UTF8: " + Integer.toHexString(i),
+ (i < 0x80 && result[0] == i)
+ || (i >= 0x80
+ && i < 0x800
+ && result[0] == (byte) (0xc0 | ((i & 0x7c0) >> 6)) && result[1] == (byte) (0x80 | (i & 0x3f)))
+ || (i >= 0x800
+ && result[0] == (byte) (0xe0 | (i >> 12))
+ && result[1] == (byte) (0x80 | ((i & 0xfc0) >> 6)) && result[2] == (byte) (0x80 | (i & 0x3f))));
+ } catch (java.io.UnsupportedEncodingException e) {
+ }
+
+ String bytes = null;
+ try {
+ bytes = new String(result, "UTF8");
+ assertTrue("Wrong UTF8 byte length: " + bytes.length() + "("
+ + i + ")", bytes.length() == 1);
+ assertTrue(
+ "Wrong char UTF8: "
+ + Integer.toHexString(bytes.charAt(0)) + " ("
+ + i + ")", bytes.charAt(0) == i);
+ } catch (java.io.UnsupportedEncodingException e) {
+ }
+ }
+
+ byte[] bytes = new byte[1];
+ for (int i = 0; i < 256; i++) {
+ bytes[0] = (byte) i;
+ String result = null;
+ try {
+ result = new String(bytes, "8859_1");
+ assertEquals("Wrong char length", 1, result.length());
+ assertTrue("Wrong char value", result.charAt(0) == (char) i);
+ } catch (java.io.UnsupportedEncodingException e) {
+ }
+ }
+ }
+
+ /**
+ * @tests java.lang.String#getBytes(int, int, byte[], int)
+ */
+ public void test_getBytesII$BI() {
+ // Test for method void java.lang.String.getBytes(int, int, byte [],
+ // int)
+ byte[] buf = new byte[5];
+ "Hello World".getBytes(6, 11, buf, 0);
+ assertEquals("Returned incorrect bytes", "World", new String(buf));
+
+ Exception exception = null;
+ try {
+ "Hello World".getBytes(-1, 1, null, 0);
+ fail("Expected StringIndexOutOfBoundsException");
+ } catch (StringIndexOutOfBoundsException e) {
+ } catch (NullPointerException e) {
+ fail("Threw wrong exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.String#getBytes(java.lang.String)
+ */
+ public void test_getBytesLjava_lang_String() throws Exception {
+ // Test for method byte [] java.lang.String.getBytes(java.lang.String)
+ byte[] buf = "Hello World".getBytes();
+ assertEquals("Returned incorrect bytes", "Hello World", new String(buf));
+
+ try {
+ "string".getBytes("8849_1");
+ fail("No UnsupportedEncodingException");
+ } catch (UnsupportedEncodingException e) {
+ }
+
+ byte[] bytes = "\u3048".getBytes("UTF-8");
+ byte[] expected = new byte[] {(byte)0xE3, (byte)0x81, (byte)0x88};
+ assertEquals(expected[0], bytes[0]);
+ assertEquals(expected[1], bytes[1]);
+ assertEquals(expected[2], bytes[2]);
+
+ // Regression for HARMONY-663
+ try {
+ "string".getBytes("?Q?D??_??_6ffa?+vG?_??�??");
+ fail("No UnsupportedEncodingException");
+ } catch (UnsupportedEncodingException e) {
+ //expected
+ }
+ }
+
+ /**
+ * @tests java.lang.String#getChars(int, int, char[], int)
+ */
+ public void test_getCharsII$CI() {
+ // Test for method void java.lang.String.getChars(int, int, char [],
+ // int)
+ hw1.getChars(5, hw1.length(), rbuf, 0);
+
+ for (int i = 0; i < rbuf.length; i++)
+ assertTrue("getChars returned incorrect char(s)", rbuf[i] == buf[i]);
+ }
+
+ /**
+ * @tests java.lang.String#hashCode()
+ */
+ public void test_hashCode() {
+ // Test for method int java.lang.String.hashCode()
+ int hwHashCode = 0;
+ final int hwLength = hw1.length();
+ int powerOfThirtyOne = 1;
+ for (int counter = hwLength - 1; counter >= 0; counter--) {
+ hwHashCode += ((int) hw1.charAt(counter)) * powerOfThirtyOne;
+ powerOfThirtyOne *= 31;
+ }
+ assertTrue("String did not hash to correct value--got: "
+ + String.valueOf(hw1.hashCode()) + " but wanted: "
+ + String.valueOf(hwHashCode), hw1.hashCode() == hwHashCode);
+ assertTrue("The empty string \"\" did not hash to zero", 0 == ""
+ .hashCode());
+ }
+
+ /**
+ * @tests java.lang.String#indexOf(int)
+ */
+ public void test_indexOfI() {
+ // Test for method int java.lang.String.indexOf(int)
+ assertEquals("Invalid index returned", 1, hw1.indexOf('e'));
+
+ }
+
+ /**
+ * @tests java.lang.String#indexOf(int, int)
+ */
+ public void test_indexOfII() {
+ // Test for method int java.lang.String.indexOf(int, int)
+ assertEquals("Invalid character index returned", 5, hw1.indexOf('W', 2));
+
+ }
+
+ /**
+ * @tests java.lang.String#indexOf(java.lang.String)
+ */
+ public void test_indexOfLjava_lang_String() {
+ // Test for method int java.lang.String.indexOf(java.lang.String)
+ assertTrue("Failed to find string", hw1.indexOf("World") > 0);
+ assertTrue("Failed to find string", !(hw1.indexOf("ZZ") > 0));
+ }
+
+ /**
+ * @tests java.lang.String#indexOf(java.lang.String, int)
+ */
+ public void test_indexOfLjava_lang_StringI() {
+ // Test for method int java.lang.String.indexOf(java.lang.String, int)
+ assertTrue("Failed to find string", hw1.indexOf("World", 0) > 0);
+ assertTrue("Found string outside index", !(hw1.indexOf("Hello", 6) > 0));
+ assertEquals("Did not accept valid negative starting position", 0, hello1
+ .indexOf("", -5));
+ assertEquals("Reported wrong error code", 5, hello1.indexOf("", 5));
+ assertEquals("Wrong for empty in empty", 0, "".indexOf("", 0));
+ }
+
+ /**
+ * @tests java.lang.String#intern()
+ */
+ public void test_intern() {
+ // Test for method java.lang.String java.lang.String.intern()
+ assertTrue("Intern returned incorrect result", hw1.intern() == hw2
+ .intern());
+ }
+
+ /**
+ * @tests java.lang.String#lastIndexOf(int)
+ */
+ public void test_lastIndexOfI() {
+ // Test for method int java.lang.String.lastIndexOf(int)
+ assertEquals("Failed to return correct index", 5, hw1.lastIndexOf('W'));
+ assertEquals("Returned index for non-existent char",
+ -1, hw1.lastIndexOf('Z'));
+
+ }
+
+ /**
+ * @tests java.lang.String#lastIndexOf(int, int)
+ */
+ public void test_lastIndexOfII() {
+ // Test for method int java.lang.String.lastIndexOf(int, int)
+ assertEquals("Failed to return correct index",
+ 5, hw1.lastIndexOf('W', 6));
+ assertEquals("Returned index for char out of specified range", -1, hw1
+ .lastIndexOf('W', 4));
+ assertEquals("Returned index for non-existent char", -1, hw1.lastIndexOf('Z',
+ 9));
+
+ }
+
+ /**
+ * @tests java.lang.String#lastIndexOf(java.lang.String)
+ */
+ public void test_lastIndexOfLjava_lang_String() {
+ // Test for method int java.lang.String.lastIndexOf(java.lang.String)
+ assertEquals("Returned incorrect index", 5, hw1.lastIndexOf("World"));
+ assertEquals("Found String outside of index", -1, hw1
+ .lastIndexOf("HeKKKKKKKK"));
+ }
+
+ /**
+ * @tests java.lang.String#lastIndexOf(java.lang.String, int)
+ */
+ public void test_lastIndexOfLjava_lang_StringI() {
+ // Test for method int java.lang.String.lastIndexOf(java.lang.String,
+ // int)
+ assertEquals("Returned incorrect index", 5, hw1.lastIndexOf("World", 9));
+ int result = hw1.lastIndexOf("Hello", 2);
+ assertTrue("Found String outside of index: " + result, result == 0);
+ assertEquals("Reported wrong error code",
+ -1, hello1.lastIndexOf("", -5));
+ assertEquals("Did not accept valid large starting position", 5, hello1
+ .lastIndexOf("", 5));
+ }
+
+ /**
+ * @tests java.lang.String#length()
+ */
+ public void test_length() {
+ // Test for method int java.lang.String.length()
+ assertEquals("Invalid length returned", 11, comp11.length());
+ }
+
+ /**
+ * @tests java.lang.String#regionMatches(int, java.lang.String, int, int)
+ */
+ public void test_regionMatchesILjava_lang_StringII() {
+ // Test for method boolean java.lang.String.regionMatches(int,
+ // java.lang.String, int, int)
+ String bogusString = "xxcedkedkleiorem lvvwr e''' 3r3r 23r";
+
+ assertTrue("identical regions failed comparison", hw1.regionMatches(2,
+ hw2, 2, 5));
+ assertTrue("Different regions returned true", !hw1.regionMatches(2,
+ bogusString, 2, 5));
+ }
+
+ /**
+ * @tests java.lang.String#regionMatches(boolean, int, java.lang.String,
+ * int, int)
+ */
+ public void test_regionMatchesZILjava_lang_StringII() {
+ // Test for method boolean java.lang.String.regionMatches(boolean, int,
+ // java.lang.String, int, int)
+
+ String bogusString = "xxcedkedkleiorem lvvwr e''' 3r3r 23r";
+
+ assertTrue("identical regions failed comparison", hw1.regionMatches(
+ false, 2, hw2, 2, 5));
+ assertTrue("identical regions failed comparison with different cases",
+ hw1.regionMatches(true, 2, hw2, 2, 5));
+ assertTrue("Different regions returned true", !hw1.regionMatches(true,
+ 2, bogusString, 2, 5));
+ assertTrue("identical regions failed comparison with different cases",
+ hw1.regionMatches(false, 2, hw2, 2, 5));
+ }
+
+ /**
+ * @tests java.lang.String#replace(char, char)
+ */
+ public void test_replaceCC() {
+ // Test for method java.lang.String java.lang.String.replace(char, char)
+ assertEquals("Failed replace", "HezzoWorzd", hw1.replace('l', 'z'));
+ }
+
+ /**
+ * @tests java.lang.String#startsWith(java.lang.String)
+ */
+ public void test_startsWithLjava_lang_String() {
+ // Test for method boolean java.lang.String.startsWith(java.lang.String)
+ assertTrue("Failed to find string", hw1.startsWith("Hello"));
+ assertTrue("Found incorrect string", !hw1.startsWith("T"));
+ }
+
+ /**
+ * @tests java.lang.String#startsWith(java.lang.String, int)
+ */
+ public void test_startsWithLjava_lang_StringI() {
+ // Test for method boolean java.lang.String.startsWith(java.lang.String,
+ // int)
+ assertTrue("Failed to find string", hw1.startsWith("World", 5));
+ assertTrue("Found incorrect string", !hw1.startsWith("Hello", 5));
+ }
+
+ /**
+ * @tests java.lang.String#substring(int)
+ */
+ public void test_substringI() {
+ // Test for method java.lang.String java.lang.String.substring(int)
+ assertEquals("Incorrect substring returned",
+ "World", hw1.substring(5));
+ assertTrue("not identical", hw1.substring(0) == hw1);
+ }
+
+ /**
+ * @tests java.lang.String#substring(int, int)
+ */
+ public void test_substringII() {
+ // Test for method java.lang.String java.lang.String.substring(int, int)
+ assertTrue("Incorrect substring returned", hw1.substring(0, 5).equals(
+ "Hello")
+ && (hw1.substring(5, 10).equals("World")));
+ assertTrue("not identical", hw1.substring(0, hw1.length()) == hw1);
+ }
+
+ /**
+ * @tests java.lang.String#toCharArray()
+ */
+ public void test_toCharArray() {
+ // Test for method char [] java.lang.String.toCharArray()
+
+ String s = new String(buf, 0, buf.length);
+ char[] schars = s.toCharArray();
+ for (int i = 0; i < s.length(); i++)
+ assertTrue("Returned incorrect char aray", buf[i] == schars[i]);
+ }
+
+ /**
+ * @tests java.lang.String#toLowerCase()
+ */
+ public void test_toLowerCase() {
+ // Test for method java.lang.String java.lang.String.toLowerCase()
+ assertTrue("toLowerCase case conversion did not succeed", hwuc
+ .toLowerCase().equals(hwlc));
+
+ assertEquals("a) Sigma has same lower case value at end of word with Unicode 3.0",
+ "\u03c3", "\u03a3".toLowerCase());
+ assertEquals("b) Sigma has same lower case value at end of word with Unicode 3.0",
+ "a \u03c3", "a \u03a3".toLowerCase());
+ }
+
+ /**
+ * @tests java.lang.String#toLowerCase(java.util.Locale)
+ */
+ public void test_toLowerCaseLjava_util_Locale() {
+ // Test for method java.lang.String
+ // java.lang.String.toLowerCase(java.util.Locale)
+ assertTrue("toLowerCase case conversion did not succeed", hwuc
+ .toLowerCase(java.util.Locale.getDefault()).equals(hwlc));
+ assertEquals("Invalid \\u0049 for English", "\u0069", "\u0049".toLowerCase(
+ Locale.ENGLISH));
+ assertEquals("Invalid \\u0049 for Turkish", "\u0131", "\u0049".toLowerCase(
+ new Locale("tr", "")));
+ }
+
+ private static String writeString(String in) {
+ StringBuffer result = new StringBuffer();
+ result.append("\"");
+ for (int i = 0; i < in.length(); i++) {
+ result.append(" 0x" + Integer.toHexString(in.charAt(i)));
+ }
+ result.append("\"");
+ return result.toString();
+ }
+
+ /**
+ * @tests java.lang.String#toLowerCase(java.util.Locale)
+ */
+ public void test_toLowerCaseLjava_util_Locale_subtest0() {
+ // Test for method java.lang.String
+ // java.lang.String.toLowerCase(java.util.Locale)
+ }
+
+ /**
+ * @tests java.lang.String#toString()
+ */
+ public void test_toString() {
+ // Test for method java.lang.String java.lang.String.toString()
+ assertTrue("Incorrect string returned", hw1.toString().equals(hw1));
+ }
+
+ /**
+ * @tests java.lang.String#toUpperCase()
+ */
+ public void test_toUpperCase() {
+ // Test for method java.lang.String java.lang.String.toUpperCase()
+ assertTrue("Returned string is not UpperCase", hwlc.toUpperCase()
+ .equals(hwuc));
+
+ assertEquals("Wrong conversion", "SS", "\u00df".toUpperCase());
+
+ String s = "a\u00df\u1f56";
+ assertTrue("Invalid conversion", !s.toUpperCase().equals(s));
+
+ }
+
+ /**
+ * @tests java.lang.String#toUpperCase(java.util.Locale)
+ */
+ public void test_toUpperCaseLjava_util_Locale() {
+ // Test for method java.lang.String
+ // java.lang.String.toUpperCase(java.util.Locale)
+ assertTrue("Returned string is not UpperCase", hwlc.toUpperCase()
+ .equals(hwuc));
+ assertEquals("Invalid \\u0069 for English", "\u0049", "\u0069".toUpperCase(
+ Locale.ENGLISH));
+ assertEquals("Invalid \\u0069 for Turkish", "\u0130", "\u0069".toUpperCase(
+ new Locale("tr", "")));
+ }
+
+ /**
+ * @tests java.lang.String#toUpperCase(java.util.Locale)
+ */
+ public void test_toUpperCaseLjava_util_Locale_subtest0() {
+ // Test for method java.lang.String
+ // java.lang.String.toUpperCase(java.util.Locale)
+ }
+
+ /**
+ * @tests java.lang.String#trim()
+ */
+ public void test_trim() {
+ // Test for method java.lang.String java.lang.String.trim()
+ assertTrue("Incorrect string returned", " HelloWorld ".trim().equals(
+ hw1));
+ }
+
+ /**
+ * @tests java.lang.String#valueOf(char[])
+ */
+ public void test_valueOf$C() {
+ // Test for method java.lang.String java.lang.String.valueOf(char [])
+ assertEquals("Returned incorrect String",
+ "World", String.valueOf(buf));
+ }
+
+ /**
+ * @tests java.lang.String#valueOf(char[], int, int)
+ */
+ public void test_valueOf$CII() {
+ // Test for method java.lang.String java.lang.String.valueOf(char [],
+ // int, int)
+ char[] t = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
+ assertEquals("copyValueOf returned incorrect String", "World", String.valueOf(t,
+ 5, 5));
+ }
+
+ /**
+ * @tests java.lang.String#valueOf(char)
+ */
+ public void test_valueOfC() {
+ // Test for method java.lang.String java.lang.String.valueOf(char)
+ for (int i = 0; i < 65536; i++)
+ assertTrue("Incorrect valueOf(char) returned: " + i, String
+ .valueOf((char) i).charAt(0) == (char) i);
+ }
+
+ /**
+ * @tests java.lang.String#valueOf(double)
+ */
+ public void test_valueOfD() {
+ // Test for method java.lang.String java.lang.String.valueOf(double)
+ assertEquals("Incorrect double string returned", "1.7976931348623157E308", String.valueOf(
+ Double.MAX_VALUE));
+ }
+
+ /**
+ * @tests java.lang.String#valueOf(float)
+ */
+ public void test_valueOfF() {
+ // Test for method java.lang.String java.lang.String.valueOf(float)
+ assertTrue("incorrect float string returned--got: "
+ + String.valueOf(1.0F) + " wanted: 1.0", String.valueOf(1.0F)
+ .equals("1.0"));
+ assertTrue("incorrect float string returned--got: "
+ + String.valueOf(0.9F) + " wanted: 0.9", String.valueOf(0.9F)
+ .equals("0.9"));
+ assertTrue("incorrect float string returned--got: "
+ + String.valueOf(109.567F) + " wanted: 109.567", String
+ .valueOf(109.567F).equals("109.567"));
+ }
+
+ /**
+ * @tests java.lang.String#valueOf(int)
+ */
+ public void test_valueOfI() {
+ // Test for method java.lang.String java.lang.String.valueOf(int)
+ assertEquals("returned invalid int string", "1", String.valueOf(1));
+ }
+
+ /**
+ * @tests java.lang.String#valueOf(long)
+ */
+ public void test_valueOfJ() {
+ // Test for method java.lang.String java.lang.String.valueOf(long)
+ assertEquals("returned incorrect long string", "927654321098", String.valueOf(
+ 927654321098L));
+ }
+
+ /**
+ * @tests java.lang.String#valueOf(java.lang.Object)
+ */
+ public void test_valueOfLjava_lang_Object() {
+ // Test for method java.lang.String
+ // java.lang.String.valueOf(java.lang.Object)
+ assertTrue("Incorrect Object string returned", obj.toString().equals(
+ String.valueOf(obj)));
+ }
+
+ /**
+ * @tests java.lang.String#valueOf(boolean)
+ */
+ public void test_valueOfZ() {
+ // Test for method java.lang.String java.lang.String.valueOf(boolean)
+ assertTrue("Incorrect boolean string returned", String.valueOf(false)
+ .equals("false")
+ && (String.valueOf(true).equals("true")));
+ }
+
+ /**
+ * @tests java.lang.String#contentEquals(CharSequence cs)
+ */
+ public void test_contentEqualsLjava_lang_CharSequence() {
+ // Test for method java.lang.String java.lang.String.contentEquals(CharSequence cs)
+ assertFalse("Incorrect result of compare", "qwerty".contentEquals(""));
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+
+ protected void doneSuite() {
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringBuffer2Test.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringBuffer2Test.java?rev=429959&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringBuffer2Test.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringBuffer2Test.java Tue Aug 8 21:41:59 2006
@@ -0,0 +1,617 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.luni.tests.java.lang;
+
+public class StringBuffer2Test extends junit.framework.TestCase {
+
+ StringBuffer testBuffer;
+
+ /**
+ * @tests java.lang.StringBuffer#StringBuffer()
+ */
+ public void test_Constructor() {
+ // Test for method java.lang.StringBuffer()
+ new StringBuffer();
+ assertTrue("Invalid buffer created", true);
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#StringBuffer(int)
+ */
+ public void test_ConstructorI() {
+ // Test for method java.lang.StringBuffer(int)
+ StringBuffer sb = new StringBuffer(8);
+ assertEquals("Newly constructed buffer is of incorrect length", 0, sb
+ .length());
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#StringBuffer(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method java.lang.StringBuffer(java.lang.String)
+
+ StringBuffer sb = new StringBuffer("HelloWorld");
+
+ assertTrue("Invalid buffer created", sb.length() == 10
+ && (sb.toString().equals("HelloWorld")));
+
+ boolean pass = false;
+ try {
+ new StringBuffer(null);
+ } catch (NullPointerException e) {
+ pass = true;
+ }
+ assertTrue("Should throw NullPointerException", pass);
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#append(char[])
+ */
+ public void test_append$C() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.append(char [])
+ char buf[] = new char[4];
+ "char".getChars(0, 4, buf, 0);
+ testBuffer.append(buf);
+ assertEquals("Append of char[] failed",
+ "This is a test bufferchar", testBuffer.toString());
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#append(char[], int, int)
+ */
+ public void test_append$CII() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.append(char [], int, int)
+ StringBuffer sb = new StringBuffer();
+ char[] buf1 = { 'H', 'e', 'l', 'l', 'o' };
+ char[] buf2 = { 'W', 'o', 'r', 'l', 'd' };
+ sb.append(buf1, 0, buf1.length);
+ assertEquals("Buffer is invalid length after append", 5, sb.length());
+ sb.append(buf2, 0, buf2.length);
+ assertEquals("Buffer is invalid length after append", 10, sb.length());
+ assertTrue("Buffer contains invalid chars", (sb.toString()
+ .equals("HelloWorld")));
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#append(char)
+ */
+ public void test_appendC() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.append(char)
+ StringBuffer sb = new StringBuffer();
+ char buf1 = 'H';
+ char buf2 = 'W';
+ sb.append(buf1);
+ assertEquals("Buffer is invalid length after append", 1, sb.length());
+ sb.append(buf2);
+ assertEquals("Buffer is invalid length after append", 2, sb.length());
+ assertTrue("Buffer contains invalid chars",
+ (sb.toString().equals("HW")));
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#append(double)
+ */
+ public void test_appendD() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.append(double)
+ StringBuffer sb = new StringBuffer();
+ sb.append(Double.MAX_VALUE);
+ assertEquals("Buffer is invalid length after append", 22, sb.length());
+ assertEquals("Buffer contains invalid characters",
+ "1.7976931348623157E308", sb.toString());
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#append(float)
+ */
+ public void test_appendF() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.append(float)
+ StringBuffer sb = new StringBuffer();
+ final float floatNum = 900.87654F;
+ sb.append(floatNum);
+ assertTrue("Buffer is invalid length after append: " + sb.length(), sb
+ .length() == String.valueOf(floatNum).length());
+ assertTrue("Buffer contains invalid characters", sb.toString().equals(
+ String.valueOf(floatNum)));
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#append(int)
+ */
+ public void test_appendI() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.append(int)
+ StringBuffer sb = new StringBuffer();
+ sb.append(9000);
+ assertEquals("Buffer is invalid length after append", 4, sb.length());
+ sb.append(1000);
+ assertEquals("Buffer is invalid length after append", 8, sb.length());
+ assertEquals("Buffer contains invalid characters",
+ "90001000", sb.toString());
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#append(long)
+ */
+ public void test_appendJ() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.append(long)
+
+ StringBuffer sb = new StringBuffer();
+ long t = 927654321098L;
+ sb.append(t);
+ assertEquals("Buffer is of invlaid length", 12, sb.length());
+ assertEquals("Buffer contains invalid characters",
+ "927654321098", sb.toString());
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#append(java.lang.Object)
+ */
+ public void test_appendLjava_lang_Object() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.append(java.lang.Object)
+ StringBuffer sb = new StringBuffer();
+ Object obj1 = new Object();
+ Object obj2 = new Object();
+ sb.append(obj1);
+ sb.append(obj2);
+ assertTrue("Buffer contains invalid characters", sb.toString().equals(
+ obj1.toString() + obj2.toString()));
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#append(java.lang.String)
+ */
+ public void test_appendLjava_lang_String() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.append(java.lang.String)
+ StringBuffer sb = new StringBuffer();
+ String buf1 = "Hello";
+ String buf2 = "World";
+ sb.append(buf1);
+ assertEquals("Buffer is invalid length after append", 5, sb.length());
+ sb.append(buf2);
+ assertEquals("Buffer is invalid length after append", 10, sb.length());
+ assertTrue("Buffer contains invalid chars", (sb.toString()
+ .equals("HelloWorld")));
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#append(boolean)
+ */
+ public void test_appendZ() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.append(boolean)
+ StringBuffer sb = new StringBuffer();
+ sb.append(false);
+ assertEquals("Buffer is invalid length after append", 5, sb.length());
+ sb.append(true);
+ assertEquals("Buffer is invalid length after append", 9, sb.length());
+ assertTrue("Buffer is invalid length after append", (sb.toString()
+ .equals("falsetrue")));
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#capacity()
+ */
+ public void test_capacity() {
+ // Test for method int java.lang.StringBuffer.capacity()
+ StringBuffer sb = new StringBuffer(10);
+ assertEquals("Returned incorrect capacity", 10, sb.capacity());
+ sb.ensureCapacity(100);
+ assertTrue("Returned incorrect capacity", sb.capacity() >= 100);
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#charAt(int)
+ */
+ public void test_charAtI() {
+ // Test for method char java.lang.StringBuffer.charAt(int)
+ assertEquals("Returned incorrect char", 's', testBuffer.charAt(3));
+
+ // Test for StringIndexOutOfBoundsException
+ boolean exception = false;
+ try {
+ testBuffer.charAt(-1);
+ } catch (StringIndexOutOfBoundsException e) {
+ exception = true;
+ } catch (ArrayIndexOutOfBoundsException e) {
+ }
+ assertTrue("Should throw StringIndexOutOfBoundsException", exception);
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#delete(int, int)
+ */
+ public void test_deleteII() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.delete(int, int)
+ testBuffer.delete(7, 7);
+ assertEquals("Deleted chars when start == end", "This is a test buffer", testBuffer.toString()
+ );
+ testBuffer.delete(4, 14);
+ assertEquals("Deleted incorrect chars",
+ "This buffer", testBuffer.toString());
+
+ testBuffer = new StringBuffer("This is a test buffer");
+ String sharedStr = testBuffer.toString();
+ testBuffer.delete(0, testBuffer.length());
+ assertEquals("Didn't clone shared buffer", "This is a test buffer", sharedStr
+ );
+ assertTrue("Deleted incorrect chars", testBuffer.toString().equals(""));
+ testBuffer.append("more stuff");
+ assertEquals("Didn't clone shared buffer 2", "This is a test buffer", sharedStr
+ );
+ assertEquals("Wrong contents", "more stuff", testBuffer.toString());
+ try {
+ testBuffer.delete(-5, 2);
+ } catch (IndexOutOfBoundsException e) {
+ }
+ assertEquals("Wrong contents 2",
+ "more stuff", testBuffer.toString());
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#deleteCharAt(int)
+ */
+ public void test_deleteCharAtI() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.deleteCharAt(int)
+ testBuffer.deleteCharAt(3);
+ assertEquals("Deleted incorrect char",
+ "Thi is a test buffer", testBuffer.toString());
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#ensureCapacity(int)
+ */
+ public void test_ensureCapacityI() {
+ // Test for method void java.lang.StringBuffer.ensureCapacity(int)
+ StringBuffer sb = new StringBuffer(10);
+
+ sb.ensureCapacity(100);
+ assertTrue("Failed to increase capacity", sb.capacity() >= 100);
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#getChars(int, int, char[], int)
+ */
+ public void test_getCharsII$CI() {
+ // Test for method void java.lang.StringBuffer.getChars(int, int, char
+ // [], int)
+
+ char[] buf = new char[10];
+ testBuffer.getChars(4, 8, buf, 2);
+ assertTrue("Returned incorrect chars", new String(buf, 2, 4)
+ .equals(testBuffer.toString().substring(4, 8)));
+
+ boolean exception = false;
+ try {
+ StringBuffer buf2 = new StringBuffer("");
+ buf2.getChars(0, 0, new char[5], 2);
+ } catch (IndexOutOfBoundsException e) {
+ exception = true;
+ }
+ assertTrue("did not expect IndexOutOfBoundsException", !exception);
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#insert(int, char[])
+ */
+ public void test_insertI$C() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.insert(int, char [])
+ char buf[] = new char[4];
+ "char".getChars(0, 4, buf, 0);
+ testBuffer.insert(15, buf);
+ assertEquals("Insert test failed",
+ "This is a test charbuffer", testBuffer.toString());
+
+ boolean exception = false;
+ StringBuffer buf1 = new StringBuffer("abcd");
+ try {
+ buf1.insert(-1, (char[]) null);
+ } catch (StringIndexOutOfBoundsException e) {
+ exception = true;
+ } catch (NullPointerException e) {
+ }
+ assertTrue("Should throw StringIndexOutOfBoundsException", exception);
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#insert(int, char[], int, int)
+ */
+ public void test_insertI$CII() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.insert(int, char [], int, int)
+ char[] c = new char[] { 'n', 'o', 't', ' ' };
+ testBuffer.insert(8, c, 0, 4);
+ assertTrue("Insert failed: " + testBuffer.toString(), testBuffer
+ .toString().equals("This is not a test buffer"));
+
+ boolean exception = false;
+ StringBuffer buf1 = new StringBuffer("abcd");
+ try {
+ buf1.insert(-1, (char[]) null, 0, 0);
+ } catch (StringIndexOutOfBoundsException e) {
+ exception = true;
+ } catch (NullPointerException e) {
+ }
+ assertTrue("Should throw StringIndexOutOfBoundsException", exception);
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#insert(int, char)
+ */
+ public void test_insertIC() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.insert(int, char)
+ testBuffer.insert(15, 'T');
+ assertEquals("Insert test failed",
+ "This is a test Tbuffer", testBuffer.toString());
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#insert(int, double)
+ */
+ public void test_insertID() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.insert(int, double)
+ testBuffer.insert(15, Double.MAX_VALUE);
+ assertTrue("Insert test failed", testBuffer.toString().equals(
+ "This is a test " + Double.MAX_VALUE + "buffer"));
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#insert(int, float)
+ */
+ public void test_insertIF() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.insert(int, float)
+ testBuffer.insert(15, Float.MAX_VALUE);
+ String testBufferString = testBuffer.toString();
+ String expectedResult = "This is a test "
+ + String.valueOf(Float.MAX_VALUE) + "buffer";
+ assertTrue("Insert test failed, got: " + "\'" + testBufferString + "\'"
+ + " but wanted: " + "\'" + expectedResult + "\'",
+ testBufferString.equals(expectedResult));
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#insert(int, int)
+ */
+ public void test_insertII() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.insert(int, int)
+ testBuffer.insert(15, 100);
+ assertEquals("Insert test failed",
+ "This is a test 100buffer", testBuffer.toString());
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#insert(int, long)
+ */
+ public void test_insertIJ() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.insert(int, long)
+ testBuffer.insert(15, 88888888888888888L);
+ assertEquals("Insert test failed",
+ "This is a test 88888888888888888buffer", testBuffer.toString());
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#insert(int, java.lang.Object)
+ */
+ public void test_insertILjava_lang_Object() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.insert(int, java.lang.Object)
+ Object obj1 = new Object();
+ testBuffer.insert(15, obj1);
+ assertTrue("Insert test failed", testBuffer.toString().equals(
+ "This is a test " + obj1.toString() + "buffer"));
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#insert(int, java.lang.String)
+ */
+ public void test_insertILjava_lang_String() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.insert(int, java.lang.String)
+
+ testBuffer.insert(15, "STRING ");
+ assertEquals("Insert test failed",
+ "This is a test STRING buffer", testBuffer.toString());
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#insert(int, boolean)
+ */
+ public void test_insertIZ() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.insert(int, boolean)
+ testBuffer.insert(15, true);
+ assertEquals("Insert test failed",
+ "This is a test truebuffer", testBuffer.toString());
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#length()
+ */
+ public void test_length() {
+ // Test for method int java.lang.StringBuffer.length()
+ assertEquals("Incorrect length returned", 21, testBuffer.length());
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#replace(int, int, java.lang.String)
+ */
+ public void test_replaceIILjava_lang_String() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.replace(int, int, java.lang.String)
+ testBuffer.replace(5, 9, "is a replaced");
+ assertTrue("Replace failed, wanted: " + "\'"
+ + "This is a replaced test buffer" + "\'" + " but got: " + "\'"
+ + testBuffer.toString() + "\'", testBuffer.toString().equals(
+ "This is a replaced test buffer"));
+ assertEquals("insert1", "text", new StringBuffer().replace(0, 0, "text")
+ .toString());
+ assertEquals("insert2", "123text", new StringBuffer("123").replace(3, 3, "text")
+ .toString());
+ assertEquals("insert2", "1text23", new StringBuffer("123").replace(1, 1, "text")
+ .toString());
+ }
+
+ private String writeString(String in) {
+ StringBuffer result = new StringBuffer();
+ result.append("\"");
+ for (int i = 0; i < in.length(); i++) {
+ result.append(" 0x" + Integer.toHexString(in.charAt(i)));
+ }
+ result.append("\"");
+ return result.toString();
+ }
+
+ private void reverseTest(String id, String org, String rev, String back) {
+ // create non-shared StringBuffer
+ StringBuffer sb = new StringBuffer(org);
+ sb.reverse();
+ String reversed = sb.toString();
+ assertTrue("reversed surrogate " + id + ": " + writeString(reversed),
+ reversed.equals(rev));
+ // create non-shared StringBuffer
+ sb = new StringBuffer(reversed);
+ sb.reverse();
+ reversed = sb.toString();
+ assertTrue("reversed surrogate " + id + "a: " + writeString(reversed),
+ reversed.equals(back));
+
+ // test algorithm when StringBuffer is shared
+ sb = new StringBuffer(org);
+ String copy = sb.toString();
+ assertEquals(org, copy);
+ sb.reverse();
+ reversed = sb.toString();
+ assertTrue("reversed surrogate " + id + ": " + writeString(reversed),
+ reversed.equals(rev));
+ sb = new StringBuffer(reversed);
+ copy = sb.toString();
+ assertEquals(rev, copy);
+ sb.reverse();
+ reversed = sb.toString();
+ assertTrue("reversed surrogate " + id + "a: " + writeString(reversed),
+ reversed.equals(back));
+
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#reverse()
+ */
+ public void test_reverse() {
+ // Test for method java.lang.StringBuffer
+ // java.lang.StringBuffer.reverse()
+ String org;
+ org = "a";
+ reverseTest("0", org, org, org);
+
+ org = "ab";
+ reverseTest("1", org, "ba", org);
+
+ org = "abcdef";
+ reverseTest("2", org, "fedcba", org);
+
+ org = "abcdefg";
+ reverseTest("3", org, "gfedcba", org);
+
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#setCharAt(int, char)
+ */
+ public void test_setCharAtIC() {
+ // Test for method void java.lang.StringBuffer.setCharAt(int, char)
+ StringBuffer s = new StringBuffer("HelloWorld");
+ s.setCharAt(4, 'Z');
+ assertEquals("Returned incorrect char", 'Z', s.charAt(4));
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#setLength(int)
+ */
+ public void test_setLengthI() {
+ // Test for method void java.lang.StringBuffer.setLength(int)
+ testBuffer.setLength(1000);
+ assertEquals("Failed to increase length", 1000, testBuffer.length());
+ assertTrue("Increase in length trashed buffer", testBuffer.toString()
+ .startsWith("This is a test buffer"));
+ testBuffer.setLength(2);
+ assertEquals("Failed to decrease length", 2, testBuffer.length());
+ assertEquals("Decrease in length failed",
+ "Th", testBuffer.toString());
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#substring(int)
+ */
+ public void test_substringI() {
+ // Test for method java.lang.String
+ // java.lang.StringBuffer.substring(int)
+ assertEquals("Returned incorrect substring", "is a test buffer", testBuffer.substring(5)
+ );
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#substring(int, int)
+ */
+ public void test_substringII() {
+ // Test for method java.lang.String
+ // java.lang.StringBuffer.substring(int, int)
+ assertEquals("Returned incorrect substring", "is", testBuffer.substring(5, 7)
+ );
+ }
+
+ /**
+ * @tests java.lang.StringBuffer#toString()
+ */
+ public void test_toString() {
+ // Test for method java.lang.String java.lang.StringBuffer.toString()
+ assertEquals("Incorrect string value returned", "This is a test buffer", testBuffer.toString()
+ );
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ testBuffer = new StringBuffer("This is a test buffer");
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+
+ protected void doneSuite() {
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringBuffer2Test.java
------------------------------------------------------------------------------
svn:eol-style = native