harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r379184 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/BitSet.java test/java/org/apache/harmony/tests/java/util/AllTests.java test/java/org/apache/harmony/tests/java/util/BitSetTest.java
Date Mon, 20 Feb 2006 17:47:40 GMT
Author: tellison
Date: Mon Feb 20 09:47:34 2006
New Revision: 379184

URL: http://svn.apache.org/viewcvs?rev=379184&view=rev
Log:
Fix for HARMONY-98 (java.util.BitSet.clear(int toIndex,int fromIndex) throws unexpected IndexOutOfBoundsException
when toIndex=fromIndex)
plus similar problems with set, get, and flip.

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/BitSetTest.java
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/BitSet.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/AllTests.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/BitSet.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/BitSet.java?rev=379184&r1=379183&r2=379184&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/BitSet.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/BitSet.java
Mon Feb 20 09:47:34 2006
@@ -18,6 +18,8 @@
 
 import java.io.Serializable;
 
+import com.ibm.oti.util.Msg;
+
 /**
  * The BitSet class implements a bit field. Each element in a BitSet can be
  * on(1) or off(0). A BitSet is created with a given size and grows when this
@@ -88,9 +90,13 @@
 	 * @return A copy of this BitSet.
 	 */
 	public Object clone() {
-		BitSet bs = new BitSet(this.size());
-		System.arraycopy(bits, 0, bs.bits, 0, bits.length);
-		return bs;
+		try {
+			BitSet clone = (BitSet) super.clone();
+			clone.bits = (long[]) bits.clone();
+			return clone;
+		} catch (CloneNotSupportedException e) {
+			return null;
+		}
 	}
 
 	/**
@@ -187,10 +193,11 @@
 		if (pos >= 0) {
 			if (pos < bits.length * ELM_SIZE)
 				return (bits[pos / ELM_SIZE] & (1L << (pos % ELM_SIZE))) != 0;
-			return false;
+			else
+				return false;
 		} else
-			throw new IndexOutOfBoundsException(com.ibm.oti.util.Msg
-					.getString("K0006")); //$NON-NLS-1$
+			// Negative index specified
+			throw new IndexOutOfBoundsException(Msg.getString("K0006"));
 	}
 
 	/**
@@ -209,14 +216,17 @@
 	 * @see #get(int)
 	 */
 	public BitSet get(int pos1, int pos2) {
-		if (pos1 >= 0 && pos2 > 0 && pos2 > pos1) {
-			if (pos2 >= bits.length * ELM_SIZE)
-				growBits(pos2);
+		if (pos1 >= 0 && pos2 >= 0 && pos2 >= pos1) {
+			int last = (bits.length * ELM_SIZE);
+			if (pos1 >= last || pos1 == pos2)
+				return new BitSet(0);
+			if (pos2 > last)
+				pos2 = last;
 
 			int idx1 = pos1 / ELM_SIZE;
-			int idx2 = pos2 / ELM_SIZE;
+			int idx2 = (pos2 - 1) / ELM_SIZE;
 			long factor1 = (~0L) << (pos1 % ELM_SIZE);
-			long factor2 = Long.MAX_VALUE >> (ELM_SIZE - (pos2 % ELM_SIZE) - 1);
+			long factor2 = (~0L) >>> (ELM_SIZE - (pos2 % ELM_SIZE));
 
 			if (idx1 == idx2) {
 				long result = (bits[idx1] & (factor1 & factor2)) >>> (pos1 % ELM_SIZE);
@@ -249,8 +259,7 @@
 				return new BitSet(newbits);
 			}
 		} else
-			throw new IndexOutOfBoundsException(com.ibm.oti.util.Msg
-					.getString("K0006")); //$NON-NLS-1$
+			throw new IndexOutOfBoundsException(Msg.getString("K0006"));
 	}
 
 	/**
@@ -309,14 +318,16 @@
 	 * @see #set(int)
 	 */
 	public void set(int pos1, int pos2) {
-		if (pos1 >= 0 && pos2 > 0 && pos2 > pos1) {
+		if (pos1 >= 0 && pos2 >= 0 && pos2 >= pos1) {
+			if (pos1 == pos2)
+				return;
 			if (pos2 >= bits.length * ELM_SIZE)
 				growBits(pos2);
 
 			int idx1 = pos1 / ELM_SIZE;
-			int idx2 = pos2 / ELM_SIZE;
+			int idx2 = (pos2 - 1) / ELM_SIZE;
 			long factor1 = (~0L) << (pos1 % ELM_SIZE);
-			long factor2 = Long.MAX_VALUE >> (ELM_SIZE - (pos2 % ELM_SIZE) - 1);
+			long factor2 = (~0L) >>> (ELM_SIZE - (pos2 % ELM_SIZE));
 
 			if (idx1 == idx2)
 				bits[idx1] |= (factor1 & factor2);
@@ -327,8 +338,7 @@
 					bits[i] |= (~0L);
 			}
 		} else
-			throw new IndexOutOfBoundsException(com.ibm.oti.util.Msg
-					.getString("K0006")); //$NON-NLS-1$
+			throw new IndexOutOfBoundsException(Msg.getString("K0006"));
 	}
 
 	/**
@@ -380,11 +390,9 @@
 		if (pos >= 0) {
 			if (pos < bits.length * ELM_SIZE)
 				bits[pos / ELM_SIZE] &= ~(1L << (pos % ELM_SIZE));
-			else
-				growBits(pos); // Bit is cleared for free if we have to grow
 		} else
-			throw new IndexOutOfBoundsException(com.ibm.oti.util.Msg
-					.getString("K0006")); //$NON-NLS-1$
+			// Negative index specified
+			throw new IndexOutOfBoundsException(Msg.getString("K0006"));
 	}
 
 	/**
@@ -402,14 +410,17 @@
 	 * @see #clear(int)
 	 */
 	public void clear(int pos1, int pos2) {
-		if (pos1 >= 0 && pos2 > 0 && pos2 > pos1) {
-			if (pos2 >= bits.length * ELM_SIZE)
-				growBits(pos2);
+		if (pos1 >= 0 && pos2 >= 0 && pos2 >= pos1) {
+			int last = (bits.length * ELM_SIZE);
+			if (pos1 >= last || pos1 == pos2)
+				return;
+			if (pos2 > last)
+				pos2 = last;
 
 			int idx1 = pos1 / ELM_SIZE;
-			int idx2 = pos2 / ELM_SIZE;
+			int idx2 = (pos2 - 1) / ELM_SIZE;
 			long factor1 = (~0L) << (pos1 % ELM_SIZE);
-			long factor2 = Long.MAX_VALUE >> (ELM_SIZE - (pos2 % ELM_SIZE) - 1);
+			long factor2 = (~0L) >>> (ELM_SIZE - (pos2 % ELM_SIZE));
 
 			if (idx1 == idx2)
 				bits[idx1] &= ~(factor1 & factor2);
@@ -420,9 +431,8 @@
 					bits[i] = 0L;
 			}
 		} else
-			throw new IndexOutOfBoundsException(com.ibm.oti.util.Msg
-					.getString("K0006")); //$NON-NLS-1$
-	}
+			throw new IndexOutOfBoundsException(Msg.getString("K0006"));
+}
 
 	/**
 	 * Flips the bit at index pos. Grows the BitSet if pos > size.
@@ -459,14 +469,16 @@
 	 * @see #flip(int)
 	 */
 	public void flip(int pos1, int pos2) {
-		if (pos1 >= 0 && pos2 > 0 && pos2 > pos1) {
+		if (pos1 >= 0 && pos2 >= 0 && pos2 >= pos1) {
+			if (pos1 == pos2)
+				return;
 			if (pos2 >= bits.length * ELM_SIZE)
 				growBits(pos2);
 
 			int idx1 = pos1 / ELM_SIZE;
-			int idx2 = pos2 / ELM_SIZE;
+			int idx2 = (pos2 - 1) / ELM_SIZE;
 			long factor1 = (~0L) << (pos1 % ELM_SIZE);
-			long factor2 = Long.MAX_VALUE >> (ELM_SIZE - (pos2 % ELM_SIZE) - 1);
+			long factor2 = (~0L) >>> (ELM_SIZE - (pos2 % ELM_SIZE));
 
 			if (idx1 == idx2)
 				bits[idx1] ^= (factor1 & factor2);
@@ -477,8 +489,7 @@
 					bits[i] ^= (~0L);
 			}
 		} else
-			throw new IndexOutOfBoundsException(com.ibm.oti.util.Msg
-					.getString("K0006")); //$NON-NLS-1$
+			throw new IndexOutOfBoundsException(Msg.getString("K0006"));
 	}
 
 	/**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/AllTests.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/AllTests.java?rev=379184&r1=379183&r2=379184&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/AllTests.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/AllTests.java
Mon Feb 20 09:47:34 2006
@@ -28,8 +28,9 @@
 		TestSuite suite = new TestSuite(
 				"Test for org.apache.harmony.tests.java.util");
 		//$JUnit-BEGIN$
-		suite.addTestSuite(ArraysTest.class);
 		suite.addTestSuite(CollectionsTest.class);
+		suite.addTestSuite(BitSetTest.class);
+		suite.addTestSuite(ArraysTest.class);
 		suite.addTestSuite(IdentityHashMapTest.class);
 		//$JUnit-END$
 		return suite;

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/BitSetTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/BitSetTest.java?rev=379184&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/BitSetTest.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/BitSetTest.java
Mon Feb 20 09:47:34 2006
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2006 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.tests.java.util;
+
+import java.util.BitSet;
+
+import junit.framework.TestCase;
+
+public class BitSetTest extends TestCase {
+
+	/**
+	 * @tests java.util.BitSet#clear(int, int)
+	 */
+	public void test_clearII() {
+		// Regression for HARMONY-98
+		BitSet bitset = new BitSet();
+		for (int i = 0; i < 20; i++) {
+			bitset.set(i);
+		}
+		bitset.clear(10, 10);
+	}
+
+	/**
+	 * @tests java.util.BitSet#clear(int, int)
+	 */
+	public void test_flipII() {
+		BitSet bitset = new BitSet();
+		for (int i = 0; i < 20; i++) {
+			bitset.set(i);
+		}
+		bitset.flip(10, 10);
+	}
+
+	/**
+	 * @tests java.util.BitSet#get(int, int)
+	 */
+	public void test_getII() {
+		BitSet bitset = new BitSet(30);
+		bitset.get(3, 3);
+	}
+
+	/**
+	 * @tests java.util.BitSet#set(int, int)
+	 */
+	public void test_setII() {
+		BitSet bitset = new BitSet(30);
+		bitset.set(29, 29);
+	}
+}



Mime
View raw message