commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r226934 - in /jakarta/commons/proper/lang/trunk/src: java/org/apache/commons/lang/text/StrBuilder.java test/org/apache/commons/lang/text/StrBuilderTest.java
Date Tue, 02 Aug 2005 03:55:01 GMT
Author: ggregory
Date: Mon Aug  1 20:54:55 2005
New Revision: 226934

URL: http://svn.apache.org/viewcvs?rev=226934&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=226934&r1=226933&r2=226934&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
Mon Aug  1 20:54:55 2005
@@ -1383,7 +1383,7 @@
      * @return the last index of the character, or -1 if not found
      */
     public int lastIndexOf(char ch) {
-        return lastIndexOf(ch, 0);
+        return lastIndexOf(ch, size - 1);
     }
 
     /**
@@ -1398,9 +1398,8 @@
         if (startIndex < 0) {
             return -1;
         }
-        char[] thisBuf = buf;
         for (int i = startIndex; i >= 0; i--) {
-            if (thisBuf[i] == ch) {
+            if (buf[i] == ch) {
                 return i;
             }
         }
@@ -1416,7 +1415,7 @@
      * @return the last index of the string, or -1 if not found
      */
     public int lastIndexOf(String str) {
-        return lastIndexOf(str, size);
+        return lastIndexOf(str, size - 1);
     }
 
     /**
@@ -1439,11 +1438,11 @@
             if (strLen == 1) {
                 return lastIndexOf(str.charAt(0), startIndex);
             }
-            char[] thisBuf = buf;
+
             outer:
-            for (int i = startIndex - strLen; i >= 0; i--) {
+            for (int i = startIndex - strLen + 1; i >= 0; i--) {
                 for (int j = 0; j < strLen; j++) {
-                    if (str.charAt(j) != thisBuf[i + j]) {
+                    if (str.charAt(j) != buf[i + j]) {
                         continue outer;
                     }
                 }

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=226934&r1=226933&r2=226934&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
Mon Aug  1 20:54:55 2005
@@ -1345,4 +1345,152 @@
         actual = sb.reverse().toString();
         assertEquals("true", actual);
     }
+    
+    public void testIndexOfChar() {
+        StrBuilder sb = new StrBuilder("abab");
+        
+        assertEquals (0, sb.indexOf('a'));
+        //should work like String#indexOf
+        assertEquals ("abab".indexOf('a'), sb.indexOf('a'));
+        
+        assertEquals(1, sb.indexOf('b'));
+        assertEquals ("abab".indexOf('b'), sb.indexOf('b'));
+        
+        assertEquals (-1, sb.indexOf('z'));
+    }
+    
+    public void testLastIndexOfChar() {
+        StrBuilder sb = new StrBuilder("abab");
+        
+        assertEquals (2, sb.lastIndexOf('a'));
+        //should work like String#lastIndexOf
+        assertEquals ("abab".lastIndexOf('a'), sb.lastIndexOf('a'));
+        
+        assertEquals(3, sb.lastIndexOf('b'));
+        assertEquals ("abab".lastIndexOf('b'), sb.lastIndexOf('b'));
+        
+        assertEquals (-1, sb.lastIndexOf('z'));
+    }
+    
+    public void testIndexOfCharInt() {
+        StrBuilder sb = new StrBuilder("abab");
+        
+        assertEquals (2, sb.indexOf('a', 1));
+        //should work like String#indexOf
+        assertEquals ("abab".indexOf('a', 1), sb.indexOf('a', 1));
+        
+        assertEquals(3, sb.indexOf('b', 2));
+        assertEquals ("abab".indexOf('b', 2), sb.indexOf('b', 2));
+        
+        assertEquals (-1, sb.indexOf('z', 2));
+        
+        sb = new StrBuilder("xyzabc");
+        assertEquals (2, sb.indexOf('z', 0));
+        assertEquals (-1, sb.indexOf('z', 3));
+    }
+    
+    public void testLastIndexOfCharInt() {
+        StrBuilder sb = new StrBuilder("abab");
+        
+        assertEquals (0, sb.lastIndexOf('a', 1));
+        //should work like String#lastIndexOf
+        assertEquals ("abab".lastIndexOf('a', 1), sb.lastIndexOf('a', 1));
+        
+        assertEquals(1, sb.lastIndexOf('b', 2));
+        assertEquals ("abab".lastIndexOf('b', 2), sb.lastIndexOf('b', 2));
+        
+        assertEquals (-1, sb.lastIndexOf('z', 2));
+        
+        sb = new StrBuilder("xyzabc");
+        assertEquals (2, sb.lastIndexOf('z', sb.length()));
+        assertEquals (-1, sb.lastIndexOf('z', 1));
+    }
+    
+    public void testIndexOfString() {
+        StrBuilder sb = new StrBuilder("abab");
+        
+        assertEquals (0, sb.indexOf("a"));
+        //should work like String#indexOf
+        assertEquals ("abab".indexOf("a"), sb.indexOf("a"));
+        
+        assertEquals (0, sb.indexOf("ab"));
+        //should work like String#indexOf
+        assertEquals ("abab".indexOf("ab"), sb.indexOf("ab"));
+        
+        assertEquals(1, sb.indexOf("b"));
+        assertEquals ("abab".indexOf("b"), sb.indexOf("b"));
+        
+        assertEquals(1, sb.indexOf("ba"));
+        assertEquals ("abab".indexOf("ba"), sb.indexOf("ba"));
+        
+        assertEquals (-1, sb.indexOf("z"));
+    }
+    
+    public void testLastIndexOfString() {
+        StrBuilder sb = new StrBuilder("abab");
+        
+        assertEquals (2, sb.lastIndexOf("a"));
+        //should work like String#lastIndexOf
+        assertEquals ("abab".lastIndexOf("a"), sb.lastIndexOf("a"));
+        
+        assertEquals (2, sb.lastIndexOf("ab"));
+        //should work like String#lastIndexOf
+        assertEquals ("abab".lastIndexOf("ab"), sb.lastIndexOf("ab"));
+        
+        assertEquals(3, sb.lastIndexOf("b"));
+        assertEquals ("abab".lastIndexOf("b"), sb.lastIndexOf("b"));
+        
+        assertEquals(1, sb.lastIndexOf("ba"));
+        assertEquals ("abab".lastIndexOf("ba"), sb.lastIndexOf("ba"));
+        
+        assertEquals (-1, sb.lastIndexOf("z"));
+    }
+    
+    public void testIndexOfStringInt() {
+        StrBuilder sb = new StrBuilder("abab");
+        
+        assertEquals (2, sb.indexOf("a", 1));
+        //should work like String#indexOf
+        assertEquals ("abab".indexOf("a", 1), sb.indexOf("a", 1));
+        
+        assertEquals (2, sb.indexOf("ab", 1));
+        //should work like String#indexOf
+        assertEquals ("abab".indexOf("ab", 1), sb.indexOf("ab", 1));
+        
+        assertEquals(3, sb.indexOf("b", 2));
+        assertEquals ("abab".indexOf("b", 2), sb.indexOf("b", 2));
+        
+        assertEquals(1, sb.indexOf("ba", 1));
+        assertEquals ("abab".indexOf("ba", 2), sb.indexOf("ba", 2));
+        
+        assertEquals (-1, sb.indexOf("z", 2));
+        
+        sb = new StrBuilder("xyzabc");
+        assertEquals (2, sb.indexOf("za", 0));
+        assertEquals (-1, sb.indexOf("za", 3));
+    }
+    
+    public void testLastIndexOfStringInt() {
+        StrBuilder sb = new StrBuilder("abab");
+        
+        assertEquals (0, sb.lastIndexOf("a", 1));
+        //should work like String#lastIndexOf
+        assertEquals ("abab".lastIndexOf("a", 1), sb.lastIndexOf("a", 1));
+        
+        assertEquals (0, sb.lastIndexOf("ab", 1));
+        //should work like String#lastIndexOf
+        assertEquals ("abab".lastIndexOf("ab", 1), sb.lastIndexOf("ab", 1));
+        
+        assertEquals(1, sb.lastIndexOf("b", 2));
+        assertEquals ("abab".lastIndexOf("b", 2), sb.lastIndexOf("b", 2));
+        
+        assertEquals(1, sb.lastIndexOf("ba", 2));
+        assertEquals ("abab".lastIndexOf("ba", 2), sb.lastIndexOf("ba", 2));
+        
+        assertEquals (-1, sb.lastIndexOf("z", 2));
+        
+        sb = new StrBuilder("xyzabc");
+        assertEquals (2, sb.lastIndexOf("za", sb.length()));
+        assertEquals (-1, sb.lastIndexOf("za", 1));
+    }
 }



---------------------------------------------------------------------
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