db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r497398 - in /db/derby/code/trunk/java: engine/org/apache/derby/iapi/services/io/FormatableBitSet.java testing/org/apache/derbyTesting/unitTests/junit/FormatableBitSetTest.java
Date Thu, 18 Jan 2007 11:10:32 GMT
Author: kahatlen
Date: Thu Jan 18 03:10:31 2007
New Revision: 497398

URL: http://svn.apache.org/viewvc?view=rev&rev=497398
Log:
DERBY-2191: Make boundary checking more consistent in FormatableBitSet

Patch contributed by Dyre Tjeldvoll.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatableBitSet.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/FormatableBitSetTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatableBitSet.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatableBitSet.java?view=diff&rev=497398&r1=497397&r2=497398
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatableBitSet.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatableBitSet.java
Thu Jan 18 03:10:31 2007
@@ -74,6 +74,14 @@
 
 	private transient int	lengthAsBits;
 
+	private void checkPosition(int p) {
+		if (p < 0 || lengthAsBits <= p) {
+			throw new
+				IllegalArgumentException("Bit position "+p+
+										 " is outside the legal range");
+		}
+	}
+
 	/**
 	 * Niladic Constructor
 	 */
@@ -446,29 +454,12 @@
 	 */
 	public final boolean isSet(int position)
 	{
-		if (SanityManager.DEBUG)
-		{
-			if (position >= this.getLength())
-            {
-                SanityManager.THROWASSERT(
-                   "Attempt to get a bit position (" + position +
-                   ")" +
-                   "that exceeds the max length (" + this.getLength() + ")");
-            }
-		}
+		checkPosition(position);
 
-		try {
-
-			int bytepos = position / 8;
-			int bitpos = 7 - (position % 8);
-
-			return ((value[bytepos] & (1 << bitpos)) != 0);
+		int bytepos = position / 8;
+		int bitpos = 7 - (position % 8);
 
-		} catch (ArrayIndexOutOfBoundsException e) {
-			// Should not happen, handle it just in case not all cases are tested
-			// by insane server.
-			return false;
-		}
+		return ((value[bytepos] & (1 << bitpos)) != 0);
 	}
 
 	/**
@@ -490,21 +481,7 @@
 	 */
 	public void set(int position)
 	{
-
-		if (SanityManager.DEBUG)
-		{
-            if (position >= this.getLength())
-            {
-                SanityManager.THROWASSERT(
-				   "Attempt to set a bit position that exceeds the max length ("
-                   + this.getLength() + ")");
-            }
-		}
-
-		// Should not happen, handle it just in case not all cases are tested
-		// by insane server.
-		if (position >= getLength())
-			grow(position);
+		checkPosition(position);
 
 		int bytepos = position / 8;
 		int bitpos = 7 - (position % 8);
@@ -520,26 +497,10 @@
 	 */
 	public void clear(int position)
 	{
-		int	bytepos;
-		int	bitpos;
-
-		if (SanityManager.DEBUG)
-		{
-            if (position >= this.getLength())
-            {
-                SanityManager.THROWASSERT(
-                   "Attempt to set a bit position that exceeds the max length ("
-                   + this.getLength() + ")");
-            }
-		}
+		checkPosition(position);
 
-		// Should not happen, handle it just in case not all cases are tested
-		// by insane server.
-		if (position >= getLength())
-			grow(position);
-
-		bytepos = position / 8;
-		bitpos = 7 - (position % 8);
+		int bytepos = position / 8;
+		int bitpos = 7 - (position % 8);
 
 		value[bytepos] &= ~(1 << bitpos);
 	}

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/FormatableBitSetTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/FormatableBitSetTest.java?view=diff&rev=497398&r1=497397&r2=497398
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/FormatableBitSetTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/FormatableBitSetTest.java
Thu Jan 18 03:10:31 2007
@@ -267,17 +267,16 @@
 
     // Test cases for isSet(int)
     public void testIsSetEmpty() {
-        empty.isSet(-1);
-        if (SanityManager.DEBUG) {
-            try { empty.isSet(0); fail(); } catch (AssertFailure af) {}
-        }
-        else {
-            assertFalse(empty.isSet(0));
-        }
+        try { empty.isSet(-8); fail(); } catch (IllegalArgumentException iae) {}
+        try { empty.isSet(-1); fail(); } catch (IllegalArgumentException iae) {}
+        try { empty.isSet(0); fail(); } catch (IllegalArgumentException iae) {}
     }
     public void testIsSet() {
-        // Should trigger an exception?
-        assertFalse(bitset18C.isSet(-1));
+        try { bitset18C.isSet(-8); fail(); }
+        catch (IllegalArgumentException iae) {}
+
+        try { bitset18C.isSet(-1); fail(); }
+        catch (IllegalArgumentException iae) {}
 
         assertFalse(bitset18C.isSet(0));
         assertFalse(bitset18C.isSet(1));
@@ -297,74 +296,42 @@
         assertTrue(bitset18C.isSet(15));
         assertTrue(bitset18C.isSet(16));
         assertTrue(bitset18C.isSet(17));
-
-        if (SanityManager.DEBUG) {
-            try { bitset18C.isSet(18); fail(); } catch (AssertFailure af) {}
-        }
-        else {
-            // Should fail?
-            assertFalse(bitset18C.isSet(18));
-        }
+        try { bitset18C.isSet(18); fail(); }
+        catch (IllegalArgumentException iae) {}
     }
 
     // Test cases for set(int)
     public void testSetEmpty() {
-        try { empty.set(-1); fail(); } 
-        catch (ArrayIndexOutOfBoundsException e) {}
-        if (SanityManager.DEBUG) {
-            try { empty.set(0); fail(); } catch (AssertFailure af) {}
-        }
-        else {
-            try { empty.set(0); fail(); }
-            catch (ArrayIndexOutOfBoundsException e) {}
-        }
+        try { empty.set(-8); fail(); } catch (IllegalArgumentException iae) {}
+        try { empty.set(-1); fail(); } catch (IllegalArgumentException iae) {}
+        try { empty.set(0); fail(); } catch (IllegalArgumentException iae) {}
     }
     public void testSet() {
-        // Should trigger an exception?
-        bitset18.set(-1);
+        try { bitset18.set(-8); fail(); }
+        catch (IllegalArgumentException iae) {}
+        try { bitset18.set(-1); fail(); }
+        catch (IllegalArgumentException iae) {}
         bitset18.set(0);
         bitset18.set(1);
-        if (SanityManager.DEBUG) {
-            try { bitset18.set(18); fail(); } catch (AssertFailure af) {}
-        }
-        else {
-            bitset18.set(18);
-            assertEquals(18,bitset18.getLength());
-            assertEquals(3,bitset18.getLengthInBytes());
-            assertEquals(3,bitset18.getByteArray().length);
-            assertEquals(9,bitset18.getNumBitsSet());
-        }
+        try { bitset18.set(18); fail(); }
+        catch (IllegalArgumentException iae) {}
     }
 
     // Test cases for clear(int)
     public void testClearEmpty() {
-        try { empty.clear(-1); fail(); } 
-        catch (ArrayIndexOutOfBoundsException e) {}
-        if (SanityManager.DEBUG) {
-            try { empty.clear(0); fail(); } catch (AssertFailure af) {}
-        }
-        else {
-            try { empty.clear(0); fail(); } 
-            catch (ArrayIndexOutOfBoundsException e) {}
-        }
+        try { empty.clear(-8); fail(); } catch (IllegalArgumentException iae) {}
+        try { empty.clear(-1); fail(); } catch (IllegalArgumentException iae) {}
+        try { empty.clear(0); fail(); } catch (IllegalArgumentException iae) {}
     }
     public void testClear() {
-        // Should trigger an exception?
-        bitset18.clear(-1);
+        try { bitset18.clear(-8); fail(); }
+        catch (IllegalArgumentException iae) {}
+        try { bitset18.clear(-1); fail(); }
+        catch (IllegalArgumentException iae) {}
         bitset18.clear(0);
         bitset18.clear(1);
-
-        if (SanityManager.DEBUG) {
-            try { bitset18.clear(18); fail(); } catch (AssertFailure af) {}
-        }
-        else {
-            bitset18.clear(18);
-            assertEquals(18,bitset18.getLength());
-            assertEquals(3,bitset18.getLengthInBytes());
-            assertEquals(3,bitset18.getByteArray().length);
-            // Should have been 9?
-            assertEquals(7,bitset18.getNumBitsSet());
-        }
+        try { bitset18.clear(18); fail(); }
+        catch (IllegalArgumentException iae) {}
     }
 
     // Test cases for anySetBit()
@@ -507,13 +474,8 @@
             try { bitset18.xor(bitset18C); fail(); } catch (AssertFailure af) {}
         }
         else {
-            bitset18.xor(bitset18C);
-            // Should have been 18?
-            assertEquals(17,bitset18.getLength());
-            assertEquals(3,bitset18.getLengthInBytes());
-            assertEquals(3,bitset18.getByteArray().length);
-            // Should have been 9?
-            assertEquals(13,bitset18.getNumBitsSet());
+            try { bitset18.xor(bitset18C); fail(); }
+            catch (IllegalArgumentException iae) {}
         }
         //assertEquals(14,bitset18.getNumBitsSet());
     }



Mime
View raw message