From 96fbd7bfe252026f59d1f5e8aa33f2d8fae65769 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 4 Mar 2015 13:52:44 +0100 Subject: Allow several units to be pickle-tested at once. Also: Make Pickler a plain phase; it is neither a macro transformer nor a miniphase. Add tests to pickleOK that are known to be stable under pickling/unpicking by comparing tree representations via show. --- tests/pos/pickleOK/Coder.scala | 59 ++++++++++++++++++++++++++++++++ tests/pos/pickleOK/alias.scala | 3 ++ tests/pos/pickleOK/new-array.scala | 17 +++++++++ tests/pos/pickleOK/templateParents.scala | 24 +++++++++++++ tests/pos/pickleOK/unapply.scala | 11 ++++++ tests/pos/pickleOK/zoo.scala | 41 ++++++++++++++++++++++ 6 files changed, 155 insertions(+) create mode 100644 tests/pos/pickleOK/Coder.scala create mode 100644 tests/pos/pickleOK/alias.scala create mode 100644 tests/pos/pickleOK/new-array.scala create mode 100644 tests/pos/pickleOK/templateParents.scala create mode 100644 tests/pos/pickleOK/unapply.scala create mode 100644 tests/pos/pickleOK/zoo.scala (limited to 'tests/pos') diff --git a/tests/pos/pickleOK/Coder.scala b/tests/pos/pickleOK/Coder.scala new file mode 100644 index 000000000..77bbd134c --- /dev/null +++ b/tests/pos/pickleOK/Coder.scala @@ -0,0 +1,59 @@ +import collection.mutable.HashMap + +class Coder(words: List[String]) { + + (2 -> "ABC", new ArrowAssoc('3') -> "DEF") + + private val mnemonics = Map( + '2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL", + '6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ") + + + ('1', "1") match { + case (digit, str) => true + case _ => false + } + + /** Invert the mnemonics map to give a map from chars 'A' ... 'Z' to '2' ... '9' */ + private val charCode0: Map[Char, Char] = mnemonics withFilter { + case (digit, str) => true + case _ => false + } flatMap { x$1 => + x$1 match { + case (digit, str) => str map (ltr => ltr -> digit) + } + } + + private val charCode: Map[Char, Char] = + for ((digit, str) <- mnemonics; ltr <- str) yield ltr -> digit + + /** Maps a word to the digit string it can represent */ + private def wordCode(word: String): String = word map charCode + + /** A map from digit strings to the words that represent them */ + private val wordsForNum: Map[String, List[String]] = + words groupBy wordCode withDefaultValue Nil + + /** All ways to encode a number as a list of words */ + def encode(number: String): Set[List[String]] = + if (number.isEmpty) Set(Nil) + else { + for { + splitPoint <- 1 to number.length + word <- wordsForNum(number take splitPoint) + rest <- encode(number drop splitPoint) + } yield word :: rest + }.toSet + + /** Maps a number to a list of all word phrases that can represent it */ + def translate(number: String): Set[String] = encode(number) map (_ mkString " ") + +} + +object Coder { + def main(args : Array[String]) : Unit = { + val coder = new Coder(List("Scala", "sobls", "Python", "Ruby", "C", "A", "rocks", "sucks", "works", "Racka")) +// println(coder.wordsForNum) + println(coder.translate("7225276257")) + } +} diff --git a/tests/pos/pickleOK/alias.scala b/tests/pos/pickleOK/alias.scala new file mode 100644 index 000000000..a66edc73a --- /dev/null +++ b/tests/pos/pickleOK/alias.scala @@ -0,0 +1,3 @@ +class A(val x: Int) + +class B(x: Int) extends A(x) diff --git a/tests/pos/pickleOK/new-array.scala b/tests/pos/pickleOK/new-array.scala new file mode 100644 index 000000000..a323783de --- /dev/null +++ b/tests/pos/pickleOK/new-array.scala @@ -0,0 +1,17 @@ +package newArray + +object Test { + val w = new Array[String](10) + val x = new Array[Int](10) + def f[T: reflect.ClassTag] = new Array[T](10) + val y = new Array[Any](10) + val z = new Array[Unit](10) +} +object Test2 { + val w: Array[Any] = new Array(10) + val x: Array[Int] = new Array(10) + def f[T: reflect.ClassTag]: Array[T] = new Array(10) + val y: Array[Any] = new Array(10) + val z: Array[Unit] = new Array(10) +} + diff --git a/tests/pos/pickleOK/templateParents.scala b/tests/pos/pickleOK/templateParents.scala new file mode 100644 index 000000000..316d8c81c --- /dev/null +++ b/tests/pos/pickleOK/templateParents.scala @@ -0,0 +1,24 @@ +object templateParents { + + // traits do not call a constructor + class C[+T](val x: T) + trait D extends C[String] + trait E extends C[Int] + class F extends C[Boolean](true) { + def foo = x + } + new C("abc") with D + +} + +object templateParents1 { + // tests inference of synthesized class type + class C[+T] + trait D extends C[String] + trait E extends C[Int] + + val x = new D with E + + val y: C[Int & String] = x +} + diff --git a/tests/pos/pickleOK/unapply.scala b/tests/pos/pickleOK/unapply.scala new file mode 100644 index 000000000..ba885be73 --- /dev/null +++ b/tests/pos/pickleOK/unapply.scala @@ -0,0 +1,11 @@ +object test { + class Foo[T](val arg : T) + + object Foo { + def unapply [a](m : Foo[a]) = Some (m.arg) + } + def matchAndGetArgFromFoo[b]( e:Foo[b]):b = {e match { case Foo(x) => x }} +// Unapply node here will have type argument [a] instantiated to scala.Nothing: +// UnApply(TypeApply(Select(Ident(Foo),unapply),List(TypeTree[TypeVar(PolyParam(a) -> TypeRef(ThisType(TypeRef(NoPrefix,scala)),Nothing))])),List(),List(Bind(x,Ident(_)))) +// but the type of the UnApply node itself is correct: RefinedType(TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,)),test$)),Foo), test$$Foo$$a, TypeAlias(TypeRef(NoPrefix,a))) +} diff --git a/tests/pos/pickleOK/zoo.scala b/tests/pos/pickleOK/zoo.scala new file mode 100644 index 000000000..02dac8f5b --- /dev/null +++ b/tests/pos/pickleOK/zoo.scala @@ -0,0 +1,41 @@ +object Test { +trait FoodStuff +trait Meat extends FoodStuff { + type IsMeat = Any +} +trait Grass extends FoodStuff { + type IsGrass = Any +} +trait Animal { + type Food <: FoodStuff + def eats(food: Food): Unit + def gets: Food +} +trait Cow extends Animal { + type IsMeat = Any + type Food <: Grass + def eats(food: Grass): Unit + def gets: Food +} +trait Lion extends Animal { + type Food = Meat + def eats(food: Meat): Unit + def gets: Meat +} +def newMeat: Meat = new Meat { +} +def newGrass: Grass = new Grass { +} +def newCow: Cow = new Cow { + type Food = Grass + def eats(food: Grass) = () + def gets = newGrass +} +def newLion: Lion = new Lion { + def eats(food: Meat) = () + def gets = newMeat +} +val milka = newCow +val leo = newLion +//leo.eats(milka) // structural select not supported +} -- cgit v1.2.3