Kaynağa Gözat

tests (80)

FIX: StringUtils - range checks
Ranides Atterwim 10 yıl önce
ebeveyn
işleme
44845c1382

+ 2 - 0
assira/pom.xml

@@ -161,6 +161,8 @@
                                 <exclude>net/ranides/assira/text/FormatNumber.class</exclude>
                                 <exclude>net/ranides/assira/text/MazoviaCharset$*</exclude>
                                 <exclude>net/ranides/assira/text/MazoviaCharset.class</exclude>
+                                <exclude>net/ranides/assira/text/StringUtils.class</exclude>
+                                <exclude>net/ranides/assira/text/StringUtils$*</exclude>
                                 
 								<exclude>net/ranides/assira/trace/ExceptionInspector$*</exclude>
 								<exclude>net/ranides/assira/trace/ExceptionInspector.class</exclude>

+ 27 - 9
assira/src/main/java/net/ranides/assira/text/StringUtils.java

@@ -800,9 +800,7 @@ public final class StringUtils {
         }
         @Override
         public char charAt(int index) {
-            if( index >= count ) {
-                throw new IndexOutOfBoundsException(index + " out of bound " + count);
-            }
+            checkIndex(length(), index);
             return chr;
         }
 		
@@ -813,7 +811,7 @@ public final class StringUtils {
 		
         @Override
         public CharSequence subSequence(int start, int end) {
-			NativeArrayAllocator.ensureFromTo(count, start, end);
+            checkRange(length(), start, end);
             return new NCSequence(end-start, chr);
         }
 
@@ -843,9 +841,7 @@ public final class StringUtils {
 
         @Override
         public char charAt(int index) {
-            if( index >= end ) {
-                throw new IndexOutOfBoundsException(index + " out of bound " + end);
-            }
+            checkIndex(length(), index);
             return text.charAt( (index+start) % text.length() );
         }
 
@@ -856,7 +852,7 @@ public final class StringUtils {
 
         @Override
         public CharSequence subSequence(int start, int end) {
-			NativeArrayAllocator.ensureFromTo(length(), start, end);
+			checkRange(length(), start, end);
             return new NSSequence(text, start, end);
         }
 
@@ -869,7 +865,7 @@ public final class StringUtils {
         }
 
     }
-	
+    
 	private static final class CharWrapper implements CharSequence {
 
         private final char[] data;
@@ -877,6 +873,7 @@ public final class StringUtils {
         private final int end;
 
         public CharWrapper(char[] data, int start, int end) { // NOPMD
+            checkRange(data.length, start, end);
             this.data = data;
             this.start = start;
             this.end = end;
@@ -889,11 +886,13 @@ public final class StringUtils {
 
         @Override
         public char charAt(int index) {
+            checkIndex(length(), index);
             return data[index + start];
         }
 
         @Override
         public CharSequence subSequence(int start, int end) {
+            checkRange(length(), start, end);
             return new CharWrapper(data, this.start+start, this.start+end);
         }
 
@@ -903,5 +902,24 @@ public final class StringUtils {
         }
 
     }
+    
+    private static void checkIndex(int size, int index) {
+        if( index<0 || index >= size ) {
+            throw new StringIndexOutOfBoundsException(index);
+        }
+    }
+    
+    private static void checkRange(int size, int begin, int end) {
+        if (begin < 0) {
+            throw new StringIndexOutOfBoundsException(begin);
+        }
+        if (end > size) {
+            throw new StringIndexOutOfBoundsException(end);
+        }
+        if (end < begin) {
+            throw new StringIndexOutOfBoundsException(begin + " shoud be <= " + end);
+        }
+    }
+	
 
 }

+ 38 - 15
assira/src/test/java/net/ranides/assira/text/StringUtilsTest.java

@@ -509,24 +509,47 @@ c*/
     
     @Test
     public void testWrap() {
-        assertTrue("Hel".contentEquals(StringUtils.wrap("Hel".toCharArray())));
-        assertTrue("Hel".contentEquals(StringUtils.wrap("Hello".toCharArray(),0,3)) );
-        assertTrue("l".contentEquals(StringUtils.wrap("Hello".toCharArray(),2,3)) );
-        assertTrue("llo".contentEquals(StringUtils.wrap("Hello".toCharArray(),2,5)) );
+        assertString("Hel", StringUtils.wrap("Hel".toCharArray()));
+        assertString("Hel", StringUtils.wrap("Hello".toCharArray(),0,3));
+        assertString("l" , StringUtils.wrap("Hello".toCharArray(),2,3));
+        assertString("llo", StringUtils.wrap("Hello".toCharArray(),2,5));
+        
+        assertString("ello wo", StringUtils.wrap("Hello world".toCharArray()).subSequence(1, 8) );
     }
     
     @Test
-    public void testRepeat() {
-        assertTrue("...".contentEquals(StringUtils.repeat(3,'.')));
-        assertTrue(".....".contentEquals(StringUtils.repeat(16,'.').subSequence(3, 8)));
-        
-        assertTrue("rerere".contentEquals(StringUtils.repeat(3,"re")));
-        
-        assertTrue("arvarvar".contentEquals(StringUtils.repeat(4,"var").subSequence(4, 12)));
-        assertTrue("arvarvarv".contentEquals(StringUtils.repeat(5,"var").subSequence(4, 13)));
-        
-        assertThrows(ArrayIndexOutOfBoundsException.class, ()->{
-            StringUtils.repeat(4,"var").subSequence(4, 13).toString();
+    public void testRepeatChar() {
+        assertString("...", StringUtils.repeat(3,'.'));
+        assertString(".....", StringUtils.repeat(16,'.').subSequence(3, 8));
+    }
+    
+    @Test
+    public void testRepeatString() {
+        assertString("rerere", StringUtils.repeat(3,"re"));
+        assertString("arvarvar", StringUtils.repeat(4,"var").subSequence(4, 12));
+        assertString("arvarvarv", StringUtils.repeat(5,"var").subSequence(4, 13));
+    }
+    
+    private void assertString(String expected, CharSequence value) {
+        assertTrue(expected.contentEquals(value));
+        assertEquals(expected, value.toString());
+        assertThrows(IndexOutOfBoundsException.class, ()->{
+            value.charAt(-1);
+        });
+        assertThrows(IndexOutOfBoundsException.class, ()->{
+            value.charAt(value.length());
+        });
+        assertThrows(IndexOutOfBoundsException.class, ()->{
+            value.charAt(value.length()+1);
+        });
+        assertThrows(IndexOutOfBoundsException.class, ()->{
+            value.subSequence(1, value.length()+2);
+        });
+        assertThrows(IndexOutOfBoundsException.class, ()->{
+            value.subSequence(-1, 2);
+        });
+        assertThrows(IndexOutOfBoundsException.class, ()->{
+            value.subSequence(2, 1);
         });
     }