summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormichelou <michelou@epfl.ch>2007-03-08 14:58:02 +0000
committermichelou <michelou@epfl.ch>2007-03-08 14:58:02 +0000
commit8514f85695a5ec0fd179d4304b071e249fa4ab3b (patch)
tree16e75f84faddb36d51ecb6374f9a076d2b18c984
parent60d600e1a14d3cc9c099a9c3a69d0f3b94c3544d (diff)
downloadscala-8514f85695a5ec0fd179d4304b071e249fa4ab3b.tar.gz
scala-8514f85695a5ec0fd179d4304b071e249fa4ab3b.tar.bz2
scala-8514f85695a5ec0fd179d4304b071e249fa4ab3b.zip
added more test cases for class BitSet
-rw-r--r--src/library/scala/collection/BitSet.scala14
-rw-r--r--src/library/scala/collection/immutable/BitSet.scala2
-rw-r--r--test/files/run/bitsets.check35
-rw-r--r--test/files/run/bitsets.scala74
4 files changed, 98 insertions, 27 deletions
diff --git a/src/library/scala/collection/BitSet.scala b/src/library/scala/collection/BitSet.scala
index 62d970c4b7..0952e01432 100644
--- a/src/library/scala/collection/BitSet.scala
+++ b/src/library/scala/collection/BitSet.scala
@@ -1,7 +1,7 @@
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL **
-** __\ \/ /__/ __ |/ /__/ __ | **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
@@ -35,8 +35,13 @@ abstract class BitSet extends Set[Int] {
* @param i ...
* @return <code>true</code> if bit <code>i</code> is set.
*/
- def contains(i: Int): Boolean =
- (i < capacity) && ((arr(offset(i)) & mask(i)) != 0)
+ def contains(i: Int): Boolean = {
+ (i < capacity) && {
+ val j = offset(i)
+ (0 <= j) && (j < arr.length) &&
+ ((arr(j) & mask(i)) != 0)
+ }
+ }
def capacity: Int
@@ -61,7 +66,8 @@ abstract class BitSet extends Set[Int] {
def toArray: Array[Int] = {
val length = memsize(capacity)
val newarr = new Array[Int](length)
- arraycopy(this.arr, 0, newarr, 0, length)
+ if (arr.length > 0)
+ arraycopy(this.arr, 0, newarr, 0, length)
newarr
}
diff --git a/src/library/scala/collection/immutable/BitSet.scala b/src/library/scala/collection/immutable/BitSet.scala
index 9dad0ba39e..946b75a406 100644
--- a/src/library/scala/collection/immutable/BitSet.scala
+++ b/src/library/scala/collection/immutable/BitSet.scala
@@ -40,7 +40,7 @@ class BitSet(val size: Int, val capacity: Int, ba: Array[Int], copy: Boolean)
import compat.Platform.arraycopy
protected val arr: Array[Int] = {
- val ba1 = if (ba != null && ba.length > 0) ba else Array(0)
+ val ba1 = if (ba != null) ba else new Array[Int](0)
if (copy) {
val arr = new Array[Int](ba1.length)
arraycopy(ba1, 0, arr, 0, ba1.length)
diff --git a/test/files/run/bitsets.check b/test/files/run/bitsets.check
index 31b718b19d..e9e6df08ad 100644
--- a/test/files/run/bitsets.check
+++ b/test/files/run/bitsets.check
@@ -1,9 +1,30 @@
-s0 = Set()
-s1 = Set(2)
-s2 = Set(2)
-xs0 = List()
+ms0 = Set(2)
+ms1 = Set(2)
+ms2 = Set(2)
+mb0 = false
+mb1 = false
+mb2 = false
+xs0 = List(2)
xs1 = List(2)
xs2 = List(2)
-ys0 = List(0)
-ys1 = List(4)
-ys2 = List(4)
+ma0 = List(4)
+ma1 = List(4)
+ma2 = List(4)
+
+is0 = Set()
+is1 = Set()
+is2 = Set(2)
+is3 = Set()
+ib0 = false
+ib1 = false
+ib2 = false
+ib3 = false
+ys0 = List()
+ys1 = List()
+ys2 = List(2)
+ys3 = List()
+ia0 = List(0)
+ia1 = List(0)
+ia2 = List(4)
+ia3 = List()
+
diff --git a/test/files/run/bitsets.scala b/test/files/run/bitsets.scala
index 3b27bf7629..d296b81bd3 100644
--- a/test/files/run/bitsets.scala
+++ b/test/files/run/bitsets.scala
@@ -5,26 +5,70 @@
//############################################################################
-object Test extends Application {
- import scala.collection.mutable.{BitSet => MBitSet}
+object TestMutable {
+ import scala.collection.mutable.BitSet
+
+ val ms0 = new BitSet
+ val ms1 = new BitSet(8)
+ val ms2 = new BitSet(0)
+ ms0 += 2
+ ms1 ++= List(1, 2)
+ ms1 -= 1
+ ms1 --= List(1)
+ ms2(2) = true
+ ms2(3) = false
+
+ Console.println("ms0 = " + ms0)
+ Console.println("ms1 = " + ms1)
+ Console.println("ms2 = " + ms2)
+
+ Console.println("mb0 = " + ms0.contains(-1))
+ Console.println("mb1 = " + ms1.contains(2))
+ Console.println("mb2 = " + ms2.contains(3))
+
+ Console.println("xs0 = " + ms0.elements.toList)
+ Console.println("xs1 = " + ms1.elements.toList)
+ Console.println("xs2 = " + ms2.elements.toList)
+
+ Console.println("ma0 = " + ms0.toArray.toList)
+ Console.println("ma1 = " + ms1.toArray.toList)
+ Console.println("ma2 = " + ms2.toArray.toList)
+ Console.println
+}
+
+object TestImmutable {
import scala.collection.immutable.BitSet
- val s0 = new BitSet(8, 8, null, false)
- val s1 = new MBitSet(8)
- val s2 = new BitSet(8, 8, Array(4), false)
- s1 += 2
+ val is0 = new BitSet(8, 1, null, false)
+ val is1 = new BitSet(8, 1, Array(), false)
+ val is2 = new BitSet(8, 8, Array(4), false)
+ val is3 = new BitSet(0, 0, null, false)
+
+ Console.println("is0 = " + is0)
+ Console.println("is1 = " + is1)
+ Console.println("is2 = " + is2)
+ Console.println("is3 = " + is3)
- Console.println("s0 = " + s0)
- Console.println("s1 = " + s1)
- Console.println("s2 = " + s2)
+ Console.println("ib0 = " + is0.contains(-1))
+ Console.println("ib1 = " + is1.contains(0))
+ Console.println("ib2 = " + is2.contains(2))
+ Console.println("ib3 = " + is3.contains(2))
- Console.println("xs0 = " + s0.elements.toList)
- Console.println("xs1 = " + s1.elements.toList)
- Console.println("xs2 = " + s2.elements.toList)
+ Console.println("ys0 = " + is0.elements.toList)
+ Console.println("ys1 = " + is1.elements.toList)
+ Console.println("ys2 = " + is2.elements.toList)
+ Console.println("ys3 = " + is3.elements.toList)
- Console.println("ys0 = " + s0.toArray.toList)
- Console.println("ys1 = " + s1.toArray.toList)
- Console.println("ys2 = " + s2.toArray.toList)
+ Console.println("ia0 = " + is0.toArray.toList)
+ Console.println("ia1 = " + is1.toArray.toList)
+ Console.println("ia2 = " + is2.toArray.toList)
+ Console.println("ia3 = " + is3.toArray.toList)
+ Console.println
+}
+
+object Test extends Application {
+ TestMutable
+ TestImmutable
}
//############################################################################