Skip to content

Instantly share code, notes, and snippets.

@jonas
Last active June 9, 2017 13:59
Show Gist options
  • Save jonas/a0728dbb7667d9c6f47f55ff5ed87f72 to your computer and use it in GitHub Desktop.
Save jonas/a0728dbb7667d9c6f47f55ff5ed87f72 to your computer and use it in GitHub Desktop.
Scala Native CollectionReachabilitySuite
package scala.scalanative.linker
import org.scalatest._
import scalanative.nir.Global
class CollectionReachabilitySuite extends ReachabilitySuite {
val sources = Seq("""
trait Collection[E] {
def isEmpty: Boolean
def size: Int
}
trait Set[E] extends Collection[E]
abstract class AbstractCollection[E] extends Collection[E] {
override def isEmpty = size == 0
}
abstract class AbstractSet[E] extends AbstractCollection[E] with Set[E]
class TreeSet[E] extends AbstractSet[E] {
override def size = 42
}
""")
val Object = g("java.lang.Object")
val Collection = g("Collection")
val Collection_isEmpty = g("Collection", "isEmpty_bool")
val Set = g("Set")
val Set_isEmpty = g("Set", "isEmpty_bool")
val AbstractCollection = g("AbstractCollection")
val AbstractCollection_isEmpty = g("AbstractCollection", "isEmpty_bool")
val AbstractSet = g("AbstractSet")
val AbstractSet_isEmpty = g("AbstractSet", "isEmpty_bool")
val TreeSet = g("TreeSet")
val TreeSet_isEmpty = g("TreeSet", "isEmpty_bool")
testReachable("Collection")(Collection)(Collection)
testReachable("Collection_isEmpty")(Collection_isEmpty)(Collection, Collection_isEmpty)
testReachable("Set")(Set)(Set, Collection)
testReachable("Set_isEmpty")(Set_isEmpty)(Set, Set_isEmpty, Collection, Collection_isEmpty)
testReachable("AbstractCollection")(AbstractCollection)(AbstractCollection, Collection, Object)
testReachable("AbstractCollection_isEmpty")(AbstractCollection_isEmpty)(
AbstractCollection, AbstractCollection_isEmpty, Collection, Collection_isEmpty)
testReachable("AbstractSet")(AbstractSet)(AbstractSet, Set, AbstractCollection, Collection, Object)
testReachable("AbstractSet_isEmpty")(AbstractSet_isEmpty)(
AbstractSet, AbstractSet_isEmpty, AbstractCollection, AbstractCollection_isEmpty, Collection, Collection_isEmpty)
testReachable("TreeSet")(TreeSet)(TreeSet, AbstractSet, Set, AbstractCollection, Collection, Object)
testReachable("TreeSet_isEmpty")(TreeSet_isEmpty)(
TreeSet, TreeSet_isEmpty, AbstractSet, AbstractSet_isEmpty, AbstractCollection, AbstractCollection_isEmpty, Collection, Collection_isEmpty)
}
> console
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_73).
Type in expressions to have them evaluated.
Type :help for more information.
scala> trait Collection[E] {
| def isEmpty: Boolean
| def size: Int
| }
defined trait Collection
scala> trait Set[E] extends Collection[E]
defined trait Set
scala>
scala> abstract class AbstractCollection[E] extends Collection[E] {
| override def isEmpty = size == 0
| }
defined class AbstractCollection
scala> abstract class AbstractSet[E] extends AbstractCollection[E] with Set[E]
defined class AbstractSet
scala>
scala> class TreeSet[E] extends AbstractSet[E] {
| override def size = 42
| }
defined class TreeSet
scala> println((new TreeSet[Int]).size)
42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment