diff options
Diffstat (limited to 'test/junit/scala/collection/mutable')
8 files changed, 170 insertions, 63 deletions
diff --git a/test/junit/scala/collection/mutable/ArrayBuilderTest.scala b/test/junit/scala/collection/mutable/ArrayBuilderTest.scala deleted file mode 100644 index b7190ee5d5..0000000000 --- a/test/junit/scala/collection/mutable/ArrayBuilderTest.scala +++ /dev/null @@ -1,28 +0,0 @@ -package scala.collection.mutable - -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 -import org.junit.Test -import scala.collection.mutable - -@RunWith(classOf[JUnit4]) -class ArrayBuilderTest { - @Test - def reusable() { - val builder = new ArrayBuilder.ofInt - val vector = Vector.range(1, 17) - val expected = Vector.range(1, 17).toArray - - builder ++= vector - val actual = builder.result() - assert ( actual.deep == expected.deep ) - - builder.clear() - val expected2 = Array[Int](100) - builder += 100 - - // Previously created array MUST be immutable even after `result`, `clear` and some operation are called - assert( actual.deep == expected.deep ) - assert( builder.result().deep == expected2.deep ) - } -} diff --git a/test/junit/scala/collection/mutable/BitSetTest.scala b/test/junit/scala/collection/mutable/BitSetTest.scala index d56cc45601..f0a0ef5d75 100644 --- a/test/junit/scala/collection/mutable/BitSetTest.scala +++ b/test/junit/scala/collection/mutable/BitSetTest.scala @@ -1,13 +1,13 @@ package scala.collection.mutable +import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 -import org.junit.{Test, Ignore} @RunWith(classOf[JUnit4]) class BitSetTest { // Test for SI-8910 - @Test def capacityExpansionTest() { + @Test def capacityExpansionTest(): Unit = { val bitSet = BitSet.empty val size = bitSet.toBitMask.length bitSet ^= bitSet @@ -20,7 +20,7 @@ class BitSetTest { assert(bitSet.toBitMask.length == size, "Capacity of bitset changed after &~=") } - @Test def test_SI8917() { + @Test def test_SI8917(): Unit = { val bigBitSet = BitSet(1, 100, 10000) val littleBitSet = BitSet(100) bigBitSet &= littleBitSet @@ -28,4 +28,17 @@ class BitSetTest { littleBitSet &= bigBitSet assert(littleBitSet.toBitMask.length < bigBitSet.toBitMask.length, "Needlessly extended the size of bitset on &=") } + + @Test def test_SI8647(): Unit = { + val bs = BitSet() + bs.map(_ + 1) // Just needs to compile + val xs = bs: SortedSet[Int] + xs.map(_ + 1) // Also should compile (did before) + } + + @Test def t10164(): Unit = { + val bs = BitSet() + val last = (bs ++ (0 to 128)).last // Just needs not to throw + assert(last == 128) + } } diff --git a/test/junit/scala/collection/mutable/HashMapTest.scala b/test/junit/scala/collection/mutable/HashMapTest.scala new file mode 100644 index 0000000000..cc1979a920 --- /dev/null +++ b/test/junit/scala/collection/mutable/HashMapTest.scala @@ -0,0 +1,38 @@ +package scala.collection +package mutable + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class HashMapTest { + + @Test + def getOrElseUpdate_mutationInCallback() { + val hm = new mutable.HashMap[String, String]() + // add enough elements to resize the hash table in the callback + def add() = 1 to 100000 foreach (i => hm(i.toString) = "callback") + hm.getOrElseUpdate("0", { + add() + "" + }) + assertEquals(Some(""), hm.get("0")) + } + + @Test + def getOrElseUpdate_evalOnce(): Unit = { + var i = 0 + val hm = new mutable.HashMap[Int, Int]() + hm.getOrElseUpdate(0, {i += 1; i}) + assertEquals(1, hm(0)) + } + + @Test + def getOrElseUpdate_noEval(): Unit = { + val hm = new mutable.HashMap[Int, Int]() + hm.put(0, 0) + hm.getOrElseUpdate(0, throw new AssertionError()) + } +} diff --git a/test/junit/scala/collection/mutable/OpenHashMapTest.scala b/test/junit/scala/collection/mutable/OpenHashMapTest.scala index 1459c14d78..e9f2a52bf6 100644 --- a/test/junit/scala/collection/mutable/OpenHashMapTest.scala +++ b/test/junit/scala/collection/mutable/OpenHashMapTest.scala @@ -1,9 +1,10 @@ package scala.collection.mutable -import org.junit.Test import org.junit.Assert._ +import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 +import org.openjdk.jol.info.{GraphPathRecord, GraphVisitor, GraphWalker} /** Tests for [[OpenHashMap]]. */ @RunWith(classOf[JUnit4]) @@ -28,7 +29,13 @@ class OpenHashMapTest { val fieldMirror = mirror.reflect(m).reflectField(termSym) */ // Use Java reflection instead for now. - val field = m.getClass.getDeclaredField("scala$collection$mutable$OpenHashMap$$deleted") + val field = + try { // Name may or not be mangled, depending on what the compiler authors are doing. + m.getClass.getDeclaredField("scala$collection$mutable$OpenHashMap$$deleted") + } catch { + case _: NoSuchFieldException => + m.getClass.getDeclaredField("deleted") + } field.setAccessible(true) m.put(0, 0) @@ -39,4 +46,50 @@ class OpenHashMapTest { // TODO assertEquals(0, fieldMirror.get.asInstanceOf[Int]) assertEquals(0, field.getInt(m)) } + + /** Test that an [[OpenHashMap]] frees references to a deleted key (SI-9522). */ + @Test + def freesDeletedKey { + import scala.language.reflectiveCalls + + class MyClass { + override def hashCode() = 42 + } + + val counter = new GraphVisitor() { + private[this] var instanceCount: Int = _ + + def countInstances(obj: AnyRef) = { + instanceCount = 0 + val walker = new GraphWalker(obj) + walker.addVisitor(this) + walker.walk + instanceCount + } + + override def visit(record: GraphPathRecord) { + if (record.klass() == classOf[MyClass]) instanceCount += 1 + } + } + + val m = OpenHashMap.empty[MyClass, Int] + val obj = new MyClass + assertEquals("Found a key instance in the map before adding one!?", 0, counter.countInstances(m)) + m.put(obj, 0) + assertEquals("There should be only one key instance in the map.", 1, counter.countInstances(m)) + m.put(obj, 1) + assertEquals("There should still be only one key instance in the map.", 1, counter.countInstances(m)) + m.remove(obj) + assertEquals("There should be no key instance in the map.", 0, counter.countInstances(m)) + + val obj2 = new MyClass + assertEquals("The hash codes of the test objects need to match.", obj.##, obj2.##) + m.put(obj, 0) + m.put(obj2, 0) + assertEquals("There should be two key instances in the map.", 2, counter.countInstances(m)) + m.remove(obj) + assertEquals("There should be one key instance in the map.", 1, counter.countInstances(m)) + m.remove(obj2) + assertEquals("There should be no key instance in the map.", 0, counter.countInstances(m)) + } } diff --git a/test/junit/scala/collection/mutable/PriorityQueueTest.scala b/test/junit/scala/collection/mutable/PriorityQueueTest.scala index a14f1bf4c8..faedcf11f0 100644 --- a/test/junit/scala/collection/mutable/PriorityQueueTest.scala +++ b/test/junit/scala/collection/mutable/PriorityQueueTest.scala @@ -14,6 +14,12 @@ class PriorityQueueTest { priorityQueue.enqueue(elements :_*) @Test + def orderingReverseReverse() { + val pq = new mutable.PriorityQueue[Nothing]()((_,_)=>42) + assert(pq.ord eq pq.reverse.reverse.ord) + } + + @Test def canSerialize() { val outputStream = new ByteArrayOutputStream() new ObjectOutputStream(outputStream).writeObject(priorityQueue) @@ -27,6 +33,7 @@ class PriorityQueueTest { val objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes)) val deserializedPriorityQueue = objectInputStream.readObject().asInstanceOf[PriorityQueue[Int]] + //correct sequencing is also tested here: assert(deserializedPriorityQueue.dequeueAll == elements.sorted.reverse) } } diff --git a/test/junit/scala/collection/mutable/TreeMapTest.scala b/test/junit/scala/collection/mutable/TreeMapTest.scala new file mode 100644 index 0000000000..ce79621c6f --- /dev/null +++ b/test/junit/scala/collection/mutable/TreeMapTest.scala @@ -0,0 +1,34 @@ +package scala.collection.mutable + +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +import scala.collection.mutable + +@RunWith(classOf[JUnit4]) +class TreeMapTest { + + @Test + def rangeMkString() { + + val map = mutable.TreeMap[String, String]() + + List("a", "b", "c", "d").foreach(v => map.put(v, v)) + + val range = map.range("b", "c") + + val valuesRange = range.values + val keysRange = range.keys + + assertEquals(1, valuesRange.size) + assertEquals(1, keysRange.size) + + assertEquals("b", valuesRange.mkString(",")) + assertEquals("b", keysRange.mkString(",")) + assertEquals("b -> b", range.mkString(",")) + + } + +} diff --git a/test/junit/scala/collection/mutable/TreeSetTest.scala b/test/junit/scala/collection/mutable/TreeSetTest.scala new file mode 100644 index 0000000000..50b004befc --- /dev/null +++ b/test/junit/scala/collection/mutable/TreeSetTest.scala @@ -0,0 +1,20 @@ +package scala.collection.mutable + +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +import scala.collection.mutable + + +@RunWith(classOf[JUnit4]) +class TreeSetTest { + + @Test + def rangeMkString() { + + val set = mutable.TreeSet("a", "b", "c", "d") + assertEquals("b", set.range("b", "c").mkString(",")) + } +} diff --git a/test/junit/scala/collection/mutable/WrappedArrayBuilderTest.scala b/test/junit/scala/collection/mutable/WrappedArrayBuilderTest.scala deleted file mode 100644 index 940a53abbd..0000000000 --- a/test/junit/scala/collection/mutable/WrappedArrayBuilderTest.scala +++ /dev/null @@ -1,30 +0,0 @@ -package scala.collection.mutable - -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 -import org.junit.Test - -import scala.collection.mutable -import scala.reflect.ClassTag - -@RunWith(classOf[JUnit4]) -class WrappedArrayBuilderTest { - @Test - def reusable() { - val builder = new WrappedArrayBuilder(ClassTag.Int) - val vector = Vector.range(1, 17) - val expected = new WrappedArray.ofInt(Vector.range(1, 17).toArray) - - builder ++= vector - val actual = builder.result() - assert( actual == expected ) - - builder.clear() - val expected2 = new WrappedArray.ofInt(Array[Int](100)) - builder += 100 - - // Previously created WrappedArray MUST be immutable even after `result`, `clear` and some operation are called - assert( actual == expected ) - assert( builder.result() == expected2 ) - } -} |