diff options
author | Tiark Rompf <tiark.rompf@epfl.ch> | 2010-04-02 13:11:23 +0000 |
---|---|---|
committer | Tiark Rompf <tiark.rompf@epfl.ch> | 2010-04-02 13:11:23 +0000 |
commit | cd51ac694d82c68a5c02f9f20a9460adbac780b4 (patch) | |
tree | 32fafdcc00c75f53d98bd4cc84eab13a6585e255 | |
parent | 7b43c30aa1746cb37ee65f2871e4e92e82896a10 (diff) | |
download | scala-cd51ac694d82c68a5c02f9f20a9460adbac780b4.tar.gz scala-cd51ac694d82c68a5c02f9f20a9460adbac780b4.tar.bz2 scala-cd51ac694d82c68a5c02f9f20a9460adbac780b4.zip |
closes #3242. review by community.
-rw-r--r-- | src/library/scala/collection/immutable/Vector.scala | 7 | ||||
-rw-r--r-- | test/files/run/t3242.check | 18 | ||||
-rw-r--r-- | test/files/run/t3242.scala | 49 | ||||
-rw-r--r-- | test/files/run/t3242b.scala | 17 |
4 files changed, 88 insertions, 3 deletions
diff --git a/src/library/scala/collection/immutable/Vector.scala b/src/library/scala/collection/immutable/Vector.scala index 3377943244..0da702f508 100644 --- a/src/library/scala/collection/immutable/Vector.scala +++ b/src/library/scala/collection/immutable/Vector.scala @@ -668,11 +668,12 @@ final class VectorBuilder[A]() extends Builder[A,Vector[A]] with VectorPointer[A } def result: Vector[A] = { - if (blockIndex + lo == 0) + val size = blockIndex + lo + if (size == 0) return Vector.empty - val s = new Vector[A](0, blockIndex + lo, 0) // should focus front or back? + val s = new Vector[A](0, size, 0) // should focus front or back? s.initFrom(this) - if (depth > 1) s.gotoPos(0, blockIndex + lo) + if (depth > 1) s.gotoPos(0, size - 1) // we're currently focused to size - 1, not size! s } diff --git a/test/files/run/t3242.check b/test/files/run/t3242.check new file mode 100644 index 0000000000..a145f6df8f --- /dev/null +++ b/test/files/run/t3242.check @@ -0,0 +1,18 @@ + append [num: 200] vec + remove [num: 200] vec + append [num: 400] vec + remove [num: 400] vec + append [num: 600] vec + remove [num: 600] vec + append [num: 800] vec + remove [num: 800] vec +>> comparison done, num: 200 + append [num: 2000] vec + remove [num: 2000] vec + append [num: 4000] vec + remove [num: 4000] vec + append [num: 6000] vec + remove [num: 6000] vec + append [num: 8000] vec + remove [num: 8000] vec +>> comparison done, num: 2000 diff --git a/test/files/run/t3242.scala b/test/files/run/t3242.scala new file mode 100644 index 0000000000..f8defaa5cd --- /dev/null +++ b/test/files/run/t3242.scala @@ -0,0 +1,49 @@ +object Test { + + def benchmarkA(num: Int) { + + type A = Int + + def updateM[M[_]](ms: M[A], update: (M[A], A)=>M[A]): M[A] = { + var is = ms + for (i <- 0 until num) is = update(is, i) + is + } + + // + def vectorAppend: Vector[A] = updateM[Vector](Vector(), (as, a)=>{ + val v = (as :+ a) + //println("==>append: i: "+i1+", v: "+v) + v + }) + // this will crash, Vector bug! + def vectorRemove(vec: Vector[A]): Vector[A] = updateM[Vector](vec, (as, a)=>{ + val v = (as filterNot{ _ == a}) + //val v = (is filter{ _ != i}) + //println("==>remove: i: "+a) + v + }) + + val ct = vectorAppend + println(" append [num: "+num+"] vec") + vectorRemove(ct) + println(" remove [num: "+num+"] vec") + } // BenchmarkA + + def comparison(num: Int): Unit = { + for (i <- 1 until 5) benchmarkA(num*i) + println(">> comparison done, num: "+num); + } + + def main(args: Array[String]): Unit = { + try { + //createBenchmarkA(23).testRun + + comparison(200) // OK + comparison(2000) // this will crach + + } catch { + case e: Exception => e.printStackTrace() + } + } +} diff --git a/test/files/run/t3242b.scala b/test/files/run/t3242b.scala new file mode 100644 index 0000000000..7a296aac15 --- /dev/null +++ b/test/files/run/t3242b.scala @@ -0,0 +1,17 @@ +import scala.collection.immutable._ + +object Test { + + def test(n: Int) = { + var vb = new VectorBuilder[Int] + for (i <- 0 until n) + vb += i + val v = vb.result + assert(v == (0 until n), "not same as (0 until " + n + "): " + v) + } + + def main(args: Array[String]): Unit = { + for (i <- 0 until 2000) + test(i) + } +} |