commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r227149 - in /jakarta/commons/proper/lang/trunk/src: java/org/apache/commons/lang/text/StrBuilder.java test/org/apache/commons/lang/text/StrBuilderTest.java
Date Wed, 03 Aug 2005 02:50:56 GMT
Author: ggregory
Date: Tue Aug  2 19:50:53 2005
New Revision: 227149

URL: http://svn.apache.org/viewcvs?rev=227149&view=rev
Log:
Fixes and better unit test code coverage from Nathan Beyer [nbeyer@kc.rr.com].

Modified:
    jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
    jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java

Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java?rev=227149&r1=227148&r2=227149&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
Tue Aug  2 19:50:53 2005
@@ -1058,12 +1058,15 @@
     public StrBuilder replace(int startIndex, int endIndex, StrBuilder builder) {
         endIndex = validateRange(startIndex, endIndex);
         int insertLen = builder.length();
-        int removeLen = endIndex = startIndex;
+        int removeLen = endIndex - startIndex;
         if (insertLen > removeLen) {
             ensureCapacity(size - removeLen + insertLen);
         }
         if (insertLen != removeLen) {
+            //shift the current characters to the right
             System.arraycopy(buf, endIndex, buf, startIndex + insertLen, size - endIndex);
+            //adjust the size accordingly
+            size += (insertLen - removeLen);
         }
         builder.getChars(0, insertLen, buf, startIndex);
         return this;
@@ -1233,7 +1236,7 @@
         } else if (length >= size) {
             return new String(buf, 0, size);
         } else {
-            return new String(buf, size - length, size);
+            return new String(buf, size - length, length);
         }
     }
 

Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java?rev=227149&r1=227148&r2=227149&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java
Tue Aug  2 19:50:53 2005
@@ -375,6 +375,8 @@
         assertEquals("ddbbcc", sb.toString());
         sb.replace('a', 'd');
         assertEquals("ddbbcc", sb.toString());
+        sb.replace('d', 'd');
+        assertEquals("ddbbcc", sb.toString());
     }
     
     /**
@@ -394,6 +396,18 @@
         assertEquals("ddbbcc", sb.toString());
     }
     
+    public void testReplaceIntIntStrBuilder() {
+        StrBuilder sb = new StrBuilder("abc");
+        sb.replace(0, 1, new StrBuilder ("d"));
+        assertEquals("dbc", sb.toString());
+        sb.replace(0, 1, new StrBuilder ("aaa"));
+        assertEquals("aaabc", sb.toString());
+        
+        sb = new StrBuilder("aabbcc");
+        sb.replace(0, 2, new StrBuilder("d"));
+        assertEquals("dbbcc", sb.toString());
+    }
+    
     public void testSetCharAt() {
         StrBuilder sb = new StrBuilder();
         try {
@@ -435,6 +449,7 @@
         assertTrue(sb.startsWith("a"));
         assertTrue(sb.startsWith("ab"));
         assertTrue(sb.startsWith("abc"));
+        assertFalse(sb.startsWith("cba"));
     }
     
     public void testEndsWith() {
@@ -452,6 +467,7 @@
         assertTrue(sb.endsWith("c"));
         assertTrue(sb.endsWith("bc"));
         assertTrue(sb.endsWith("abc"));
+        assertFalse(sb.endsWith("cba"));
         assertFalse(sb.endsWith("abcd"));
         assertFalse(sb.endsWith(" abc"));
         assertFalse(sb.endsWith("abc "));
@@ -1492,5 +1508,85 @@
         sb = new StrBuilder("xyzabc");
         assertEquals (2, sb.lastIndexOf("za", sb.length()));
         assertEquals (-1, sb.lastIndexOf("za", 1));
+    }
+    
+    public void testContainsChar() {
+        StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz");
+        assertTrue (sb.contains('a'));
+        assertTrue (sb.contains('o'));
+        assertTrue (sb.contains('z'));
+        assertFalse (sb.contains('1'));
+    }
+    
+    public void testContainsString() {
+        StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz");
+        assertTrue (sb.contains("a"));
+        assertTrue (sb.contains("pq"));
+        assertTrue (sb.contains("z"));
+        assertFalse (sb.contains("zyx"));
+    }
+    
+    public void testMidString() {
+        StrBuilder sb = new StrBuilder("hello goodbye hello");
+        assertEquals ("goodbye", sb.midString(6, 7));
+        assertEquals ("hello", sb.midString(0, 5));
+        assertEquals ("hello", sb.midString(-5, 5));
+        assertEquals ("", sb.midString(0, -1));
+        assertEquals ("", sb.midString(20, 2));
+    }
+    
+    public void testRightString() {
+        StrBuilder sb = new StrBuilder("left right");
+        assertEquals ("right", sb.rightString(5));
+        assertEquals ("", sb.rightString(0));
+        assertEquals ("", sb.rightString(-5));
+        assertEquals ("left right", sb.rightString(15));
+    }
+    
+    public void testLeftString() {
+        StrBuilder sb = new StrBuilder("left right");
+        assertEquals ("left", sb.leftString(4));
+        assertEquals ("", sb.leftString(0));
+        assertEquals ("", sb.leftString(-5));
+        assertEquals ("left right", sb.leftString(15));
+    }
+    
+    public void testSubstringInt() {
+        StrBuilder sb = new StrBuilder ("hello goodbye");
+        assertEquals ("goodbye", sb.substring(6));
+        assertEquals ("hello goodbye".substring(6), sb.substring(6));
+        assertEquals ("hello goodbye", sb.substring(0));
+        assertEquals ("hello goodbye".substring(0), sb.substring(0));
+        try {
+            sb.substring(-1);
+            fail ();
+        } catch (StringIndexOutOfBoundsException e) {}
+        
+        try {
+            sb.substring(15);
+            fail ();
+        } catch (StringIndexOutOfBoundsException e) {}
+    
+    }
+    
+    public void testSubstringIntInt() {
+        StrBuilder sb = new StrBuilder ("hello goodbye");
+        assertEquals ("hello", sb.substring(0, 5));
+        assertEquals ("hello goodbye".substring(0, 6), sb.substring(0, 6));
+        
+        assertEquals ("goodbye", sb.substring(6, 13));
+        assertEquals ("hello goodbye".substring(6,13), sb.substring(6, 13));
+        
+        assertEquals ("goodbye", sb.substring(6, 20));
+        
+        try {
+            sb.substring(-1, 5);
+            fail();
+        } catch (StringIndexOutOfBoundsException e) {}
+        
+        try {
+            sb.substring(15, 20);
+            fail();
+        } catch (StringIndexOutOfBoundsException e) {}
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message