Skip to content

Commit

Permalink
Merge pull request #9076 from lrytz/t12048
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Jun 22, 2020
2 parents a9d3775 + 26623f2 commit 69d2537
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/library/scala/collection/immutable/IntMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ sealed abstract class IntMap[+T] extends AbstractMap[Int, T]

override protected[this] def className = "IntMap"

override def isEmpty = this == IntMap.Nil
override def isEmpty = this eq IntMap.Nil
override def knownSize: Int = if (isEmpty) 0 else super.knownSize
override def filter(f: ((Int, T)) => Boolean): IntMap[T] = this match {
case IntMap.Bin(prefix, mask, left, right) => {
Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/collection/immutable/LongMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ sealed abstract class LongMap[+T] extends AbstractMap[Long, T]

override protected[this] def className = "LongMap"

override def isEmpty = this == LongMap.Nil
override def isEmpty = this eq LongMap.Nil
override def knownSize: Int = if (isEmpty) 0 else super.knownSize
override def filter(f: ((Long, T)) => Boolean): LongMap[T] = this match {
case LongMap.Bin(prefix, mask, left, right) => {
Expand Down
12 changes: 12 additions & 0 deletions src/testkit/scala/tools/testkit/AssertUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,18 @@ object AssertUtil {
def readyOrNot(awaitable: Awaitable[_]): Boolean = Try(Await.ready(awaitable, TestDuration.Standard)).isSuccess

def withoutATrace[A](body: => A) = NoTrace(body)

private lazy val modsField = classOf[Field].getDeclaredField("modifiers").tap(_.setAccessible(true))

def getFieldAccessible[T: ClassTag](n: String): Field =
implicitly[ClassTag[T]]
.runtimeClass.getDeclaredField(n)
.tap { f =>
if ((f.getModifiers & Modifier.FINAL) != 0)
modsField.setInt(f, f.getModifiers() & ~Modifier.FINAL)
if ((f.getModifiers & Modifier.PUBLIC) == 0)
f.setAccessible(true)
}
}

object TestDuration {
Expand Down
31 changes: 31 additions & 0 deletions test/junit/scala/collection/immutable/IntMapTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package scala.collection.immutable

import org.junit.Assert._
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

import scala.tools.testkit.AssertUtil.{getFieldAccessible => f}

@RunWith(classOf[JUnit4])
class IntMapTest {
@Test
def `isEmpty O(1)`(): Unit = {
val m = IntMap(1 -> (), 2 -> (), 3 -> ())
f[IntMap.Bin[_]]("left").set(m, null)
f[IntMap.Bin[_]]("right").set(m, null)
assertFalse(m.isEmpty) // no NPE, does not access left or right
}
}
32 changes: 32 additions & 0 deletions test/junit/scala/collection/immutable/LongMapTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package scala.collection.immutable

import org.junit.Assert._
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

import scala.tools.testkit.AssertUtil.{getFieldAccessible => f}

@RunWith(classOf[JUnit4])
class LongMapTest {

@Test
def `isEmpty O(1)`(): Unit = {
val m = LongMap(1l -> (), 2l -> (), 3l -> ())
f[LongMap.Bin[_]]("left").set(m, null)
f[LongMap.Bin[_]]("right").set(m, null)
assertFalse(m.isEmpty) // no NPE, does not access left or right
}
}

0 comments on commit 69d2537

Please sign in to comment.