diff options
author | Guillaume Martres <smarter@ubuntu.com> | 2015-05-23 16:48:15 +0200 |
---|---|---|
committer | Guillaume Martres <smarter@ubuntu.com> | 2015-05-23 18:55:40 +0200 |
commit | e61e59f739d889549993afdd743cbaf71a95c45e (patch) | |
tree | 8b390944a546f0c29de4076a028dd8a6c76bedc7 /tests/run | |
parent | 942c41b1bdc6db60d530eee70d9dba68acfb54e6 (diff) | |
download | dotty-e61e59f739d889549993afdd743cbaf71a95c45e.tar.gz dotty-e61e59f739d889549993afdd743cbaf71a95c45e.tar.bz2 dotty-e61e59f739d889549993afdd743cbaf71a95c45e.zip |
Add some run tests related to value classes
Diffstat (limited to 'tests/run')
-rw-r--r-- | tests/run/genericValueClass.check | 2 | ||||
-rw-r--r-- | tests/run/genericValueClass.scala | 20 | ||||
-rw-r--r-- | tests/run/t5608.check | 1 | ||||
-rw-r--r-- | tests/run/t5608.scala | 12 | ||||
-rw-r--r-- | tests/run/t5866.check | 2 | ||||
-rw-r--r-- | tests/run/t5866.scala | 11 | ||||
-rw-r--r-- | tests/run/t6260-delambdafy.check | 4 | ||||
-rw-r--r-- | tests/run/t6260-delambdafy.scala | 12 | ||||
-rw-r--r-- | tests/run/t6260.check | 1 | ||||
-rw-r--r-- | tests/run/t6260.scala | 12 | ||||
-rw-r--r-- | tests/run/t6534.scala | 14 | ||||
-rw-r--r-- | tests/run/t6574b.check | 1 | ||||
-rw-r--r-- | tests/run/t6574b.scala | 7 | ||||
-rw-r--r-- | tests/run/t7019.scala | 10 | ||||
-rw-r--r-- | tests/run/value-class-extractor-2.check | 8 | ||||
-rw-r--r-- | tests/run/value-class-extractor-2.scala | 109 | ||||
-rw-r--r-- | tests/run/value-class-extractor.check | 9 | ||||
-rw-r--r-- | tests/run/value-class-extractor.scala | 92 | ||||
-rw-r--r-- | tests/run/valueclasses-constr.check | 10 | ||||
-rw-r--r-- | tests/run/valueclasses-constr.scala | 79 |
20 files changed, 416 insertions, 0 deletions
diff --git a/tests/run/genericValueClass.check b/tests/run/genericValueClass.check new file mode 100644 index 000000000..ec3a41a6a --- /dev/null +++ b/tests/run/genericValueClass.check @@ -0,0 +1,2 @@ +(1,abc) +(2,def) diff --git a/tests/run/genericValueClass.scala b/tests/run/genericValueClass.scala new file mode 100644 index 000000000..5873eace1 --- /dev/null +++ b/tests/run/genericValueClass.scala @@ -0,0 +1,20 @@ + +import scala.language.implicitConversions + +object Test extends dotty.runtime.LegacyApp { + class ArrowAssocClass[A](val __leftOfArrow: A) extends AnyVal { + @inline def -> [B](y: B): Tuple2[A, B] = Tuple2(__leftOfArrow, y) + def →[B](y: B): Tuple2[A, B] = ->(y) + } + + { + @inline implicit def ArrowAssoc[A](x: A): ArrowAssocClass[A] = new ArrowAssocClass(x) + val x = 1 -> "abc" + println(x) + } + + { + val y = 2 -> "def" + println(y) + } +} diff --git a/tests/run/t5608.check b/tests/run/t5608.check new file mode 100644 index 000000000..ba70d2170 --- /dev/null +++ b/tests/run/t5608.check @@ -0,0 +1 @@ +A@6 diff --git a/tests/run/t5608.scala b/tests/run/t5608.scala new file mode 100644 index 000000000..43e912122 --- /dev/null +++ b/tests/run/t5608.scala @@ -0,0 +1,12 @@ +object Test { + def main(args:Array[String]): Unit = { + val ns = Array(3L, 3L, 3L) + val a1: A = new A(ns(0)) + val a2: A = new A(ns(0)) + println(a1 + a2) + } +} + +class A(val u: Long) extends AnyVal { + def +(other: A) = new A(other.u + u) +} diff --git a/tests/run/t5866.check b/tests/run/t5866.check new file mode 100644 index 000000000..9f4ec729a --- /dev/null +++ b/tests/run/t5866.check @@ -0,0 +1,2 @@ +0.0 +Foo(0.0) diff --git a/tests/run/t5866.scala b/tests/run/t5866.scala new file mode 100644 index 000000000..120773eff --- /dev/null +++ b/tests/run/t5866.scala @@ -0,0 +1,11 @@ +class Foo(val d: Double) extends AnyVal { + override def toString = s"Foo($d)" +} +object Test { + def main(args: Array[String]): Unit = { + val d: Double = null.asInstanceOf[Double] + println(d) + val f: Foo = null.asInstanceOf[Foo] + println(f) + } +} diff --git a/tests/run/t6260-delambdafy.check b/tests/run/t6260-delambdafy.check new file mode 100644 index 000000000..b3ec1b3cc --- /dev/null +++ b/tests/run/t6260-delambdafy.check @@ -0,0 +1,4 @@ +f(C@2e) + +apply +get$Lambda diff --git a/tests/run/t6260-delambdafy.scala b/tests/run/t6260-delambdafy.scala new file mode 100644 index 000000000..a4a3cfc8f --- /dev/null +++ b/tests/run/t6260-delambdafy.scala @@ -0,0 +1,12 @@ +class C[A](private val a: Any) extends AnyVal + +object Test { + val f = (x: C[Any]) => {println(s"f($x)"); x} + def main(args: Array[String]): Unit = { + f(new C(".")) + val methods = f.getClass.getDeclaredMethods.map(_.getName).sorted + println("") + println(methods.mkString("\n")) + } +} + diff --git a/tests/run/t6260.check b/tests/run/t6260.check new file mode 100644 index 000000000..54f98a10f --- /dev/null +++ b/tests/run/t6260.check @@ -0,0 +1 @@ +Box(abcabc) diff --git a/tests/run/t6260.scala b/tests/run/t6260.scala new file mode 100644 index 000000000..3ad4926c8 --- /dev/null +++ b/tests/run/t6260.scala @@ -0,0 +1,12 @@ +class Box[X <: CharSequence](val x: X) extends AnyVal { + def map[Y <: CharSequence](f: X => Y): Box[Y] = + ((bx: Box[X]) => new Box(f(bx.x)))(this) + override def toString = s"Box($x)" +} + +object Test { + def main(args: Array[String]): Unit = { + val g = (x: String) => x + x + println(new Box("abc") map g) + } +} diff --git a/tests/run/t6534.scala b/tests/run/t6534.scala new file mode 100644 index 000000000..33df97e41 --- /dev/null +++ b/tests/run/t6534.scala @@ -0,0 +1,14 @@ +trait Foo extends Any { override def equals(x: Any) = false } +trait Ding extends Any { override def hashCode = -1 } + +class Bippy1(val x: Int) extends AnyVal with Foo { } // warn +class Bippy2(val x: Int) extends AnyVal with Ding { } // warn + +object Test { + def main(args: Array[String]): Unit = { + val b1 = new Bippy1(71) + val b2 = new Bippy2(71) + assert(b1 == b1 && b1.## == b1.x.##, ((b1, b1.##))) + assert(b2 == b2 && b2.## == b2.x.##, ((b2, b2.##))) + } +} diff --git a/tests/run/t6574b.check b/tests/run/t6574b.check new file mode 100644 index 000000000..e10fa4f81 --- /dev/null +++ b/tests/run/t6574b.check @@ -0,0 +1 @@ +List(5, 4, 3, 2, 1) diff --git a/tests/run/t6574b.scala b/tests/run/t6574b.scala new file mode 100644 index 000000000..56f952074 --- /dev/null +++ b/tests/run/t6574b.scala @@ -0,0 +1,7 @@ +object Test extends dotty.runtime.LegacyApp { + implicit class AnyOps(val i: Int) extends AnyVal { + private def parentsOf(x: Int): List[Int] = if (x == 0) Nil else x :: parentsOf(x - 1) + def parents: List[Int] = parentsOf(i) + } + println((5).parents) +} diff --git a/tests/run/t7019.scala b/tests/run/t7019.scala new file mode 100644 index 000000000..5dcc09d2b --- /dev/null +++ b/tests/run/t7019.scala @@ -0,0 +1,10 @@ +final class Foo(val i: Int) extends AnyVal { + def foo() = go(i) + private[this] def go(i: Int) = i * 2 +} + +object Test { + def main(args: Array[String]): Unit = { + assert(new Foo(1).foo() == 2) + } +} diff --git a/tests/run/value-class-extractor-2.check b/tests/run/value-class-extractor-2.check new file mode 100644 index 000000000..5903b996b --- /dev/null +++ b/tests/run/value-class-extractor-2.check @@ -0,0 +1,8 @@ +String +List +Int +Something else +String +List +Int +Something else diff --git a/tests/run/value-class-extractor-2.scala b/tests/run/value-class-extractor-2.scala new file mode 100644 index 000000000..5850d42f0 --- /dev/null +++ b/tests/run/value-class-extractor-2.scala @@ -0,0 +1,109 @@ +final class Opt[+A >: Null](val value: A) extends AnyVal { + def get: A = value + def isEmpty = value == null + def isDefined = !isEmpty +} +object Opt { + final val None = new Opt[Null](null) + def apply[A >: Null](value: A): Opt[A] = if (value == null) None else new Opt[A](value) +} + +object ValueOpt { + // public java.lang.String unapply(java.lang.Object); + // 0: aload_1 + // 1: instanceof #16 // class java/lang/String + // 4: ifeq 21 + // 7: getstatic #21 // Field Opt$.MODULE$:LOpt$; + // 10: astore_2 + // 11: ldc #23 // String String + // 13: checkcast #16 // class java/lang/String + // 16: astore 5 + // 18: goto 71 + // 21: aload_1 + // 22: instanceof #25 // class scala/collection/immutable/List + // 25: ifeq 42 + // 28: getstatic #21 // Field Opt$.MODULE$:LOpt$; + // 31: astore_3 + // 32: ldc #27 // String List + // 34: checkcast #16 // class java/lang/String + // 37: astore 5 + // 39: goto 71 + // 42: aload_1 + // 43: instanceof #29 // class java/lang/Integer + // 46: ifeq 64 + // 49: getstatic #21 // Field Opt$.MODULE$:LOpt$; + // 52: astore 4 + // 54: ldc #31 // String Int + // 56: checkcast #16 // class java/lang/String + // 59: astore 5 + // 61: goto 71 + // 64: getstatic #21 // Field Opt$.MODULE$:LOpt$; + // 67: pop + // 68: aconst_null + // 69: astore 5 + // 71: aload 5 + // 73: areturn + def unapply(x: Any): Opt[String] = x match { + case _: String => Opt("String") + case _: List[_] => Opt("List") + case _: Int => Opt("Int") + case _ => Opt.None + } +} +object RegularOpt { + // public scala.Option<java.lang.String> unapply(java.lang.Object); + // 0: aload_1 + // 1: instanceof #16 // class java/lang/String + // 4: ifeq 20 + // 7: new #18 // class scala/Some + // 10: dup + // 11: ldc #20 // String String + // 13: invokespecial #23 // Method scala/Some."<init>":(Ljava/lang/Object;)V + // 16: astore_2 + // 17: goto 64 + // 20: aload_1 + // 21: instanceof #25 // class scala/collection/immutable/List + // 24: ifeq 40 + // 27: new #18 // class scala/Some + // 30: dup + // 31: ldc #27 // String List + // 33: invokespecial #23 // Method scala/Some."<init>":(Ljava/lang/Object;)V + // 36: astore_2 + // 37: goto 64 + // 40: aload_1 + // 41: instanceof #29 // class java/lang/Integer + // 44: ifeq 60 + // 47: new #18 // class scala/Some + // 50: dup + // 51: ldc #31 // String Int + // 53: invokespecial #23 // Method scala/Some."<init>":(Ljava/lang/Object;)V + // 56: astore_2 + // 57: goto 64 + // 60: getstatic #36 // Field scala/None$.MODULE$:Lscala/None$; + // 63: astore_2 + // 64: aload_2 + // 65: areturn + def unapply(x: Any): Option[String] = x match { + case _: String => Some("String") + case _: List[_] => Some("List") + case _: Int => Some("Int") + case _ => None + } +} + +object Test { + def f(x: Any) = x match { + case ValueOpt(s) => s + case _ => "Something else" + } + def g(x: Any) = x match { + case RegularOpt(s) => s + case _ => "Something else" + } + val xs = List("abc", Nil, 5, Test) + + def main(args: Array[String]): Unit = { + xs map f foreach println + xs map g foreach println + } +} diff --git a/tests/run/value-class-extractor.check b/tests/run/value-class-extractor.check new file mode 100644 index 000000000..e16447118 --- /dev/null +++ b/tests/run/value-class-extractor.check @@ -0,0 +1,9 @@ +'a' +'b' +'c' +NoChar +Some(a) +Some(b) +Some(c) +None +9 diff --git a/tests/run/value-class-extractor.scala b/tests/run/value-class-extractor.scala new file mode 100644 index 000000000..5628fea47 --- /dev/null +++ b/tests/run/value-class-extractor.scala @@ -0,0 +1,92 @@ +final class NonNullChar(val get: Char) extends AnyVal { + def isEmpty = get == 0.toChar + override def toString = if (isEmpty) "NoChar" else s"'$get'" +} +object NonNullChar { + @inline final val None = new NonNullChar(0.toChar) +} + +final class SomeProduct /*extends Product3[String, Int, List[String]]*/ { + def canEqual(x: Any) = x.isInstanceOf[SomeProduct] + def _1 = "abc" + def _2 = 5 + def _3 = List("bippy") + def isEmpty = false + def isDefined = !isEmpty + def get = this +} +object SomeProduct { + def unapply(x: SomeProduct) = x +} + +object Test { + def prod(x: SomeProduct): Int = x match { + case SomeProduct(x, y, z) => x.length + y + z.length + case _ => -1 + } + + def f(x: Char): NonNullChar = x match { + case 'a' => new NonNullChar('a') + case 'b' => new NonNullChar('b') + case 'c' => new NonNullChar('c') + case _ => NonNullChar.None + } + // public char f(char); + // 0: iload_1 + // 1: tableswitch { // 97 to 99 + // 97: 47 + // 98: 42 + // 99: 37 + // default: 28 + // } + // 28: getstatic #19 // Field NonNullChar$.MODULE$:LNonNullChar$; + // 31: invokevirtual #23 // Method NonNullChar$.None:()C + // 34: goto 49 + // 37: bipush 99 + // 39: goto 49 + // 42: bipush 98 + // 44: goto 49 + // 47: bipush 97 + // 49: ireturn + def g(x: Char): Option[Char] = x match { + case 'a' => Some('a') + case 'b' => Some('b') + case 'c' => Some('c') + case _ => None + } + // public scala.Option<java.lang.Object> g(char); + // 0: iload_1 + // 1: tableswitch { // 97 to 99 + // 97: 64 + // 98: 49 + // 99: 34 + // default: 28 + // } + // 28: getstatic #33 // Field scala/None$.MODULE$:Lscala/None$; + // 31: goto 76 + // 34: new #35 // class scala/Some + // 37: dup + // 38: bipush 99 + // 40: invokestatic #41 // Method scala/runtime/BoxesRunTime.boxToCharacter:(C)Ljava/lang/Character; + // 43: invokespecial #44 // Method scala/Some."<init>":(Ljava/lang/Object;)V + // 46: goto 76 + // 49: new #35 // class scala/Some + // 52: dup + // 53: bipush 98 + // 55: invokestatic #41 // Method scala/runtime/BoxesRunTime.boxToCharacter:(C)Ljava/lang/Character; + // 58: invokespecial #44 // Method scala/Some."<init>":(Ljava/lang/Object;)V + // 61: goto 76 + // 64: new #35 // class scala/Some + // 67: dup + // 68: bipush 97 + // 70: invokestatic #41 // Method scala/runtime/BoxesRunTime.boxToCharacter:(C)Ljava/lang/Character; + // 73: invokespecial #44 // Method scala/Some."<init>":(Ljava/lang/Object;)V + // 76: areturn + def main(args: Array[String]): Unit = { + "abcd" foreach (ch => println(f(ch))) + "abcd" foreach (ch => println(g(ch))) + println(prod(new SomeProduct)) + } +} + + diff --git a/tests/run/valueclasses-constr.check b/tests/run/valueclasses-constr.check new file mode 100644 index 000000000..785e6fa25 --- /dev/null +++ b/tests/run/valueclasses-constr.check @@ -0,0 +1,10 @@ +16 +00:16:40 +16 +00:16:40 +16 +00:16:40 +16 +00:16:40 +16 +00:16:40 diff --git a/tests/run/valueclasses-constr.scala b/tests/run/valueclasses-constr.scala new file mode 100644 index 000000000..73ac29435 --- /dev/null +++ b/tests/run/valueclasses-constr.scala @@ -0,0 +1,79 @@ +package test1 { + object TOD { + final val SecondsPerDay = 86400 + + def apply(seconds: Int) = { + val n = seconds % SecondsPerDay + new TOD(if (n >= 0) n else n + SecondsPerDay) + } + } + + final class TOD (val secondsOfDay: Int) extends AnyVal { + def hours = secondsOfDay / 3600 + def minutes = (secondsOfDay / 60) % 60 + def seconds = secondsOfDay % 60 + + override def toString = "%02d:%02d:%02d".format(hours, minutes, seconds) + } +} +package test2 { + object TOD { + final val SecondsPerDay = 86400 + + def apply(seconds: Int) = { + val n = seconds % SecondsPerDay + new TOD(if (n >= 0) n else n + SecondsPerDay) + } + } + + final class TOD private[test2] (val secondsOfDay: Int) extends AnyVal { + def hours = secondsOfDay / 3600 + def minutes = (secondsOfDay / 60) % 60 + def seconds = secondsOfDay % 60 + + override def toString = "%02d:%02d:%02d".format(hours, minutes, seconds) + } + + object Client { + def newTOD(x: Int) = new TOD(x) + } +} + +package test3 { + object TOD { + final val SecondsPerDay = 86400 + + def apply(seconds: Int) = { + val n = seconds % SecondsPerDay + new TOD(if (n >= 0) n else n + SecondsPerDay) + } + } + + final class TOD private (val secondsOfDay: Int) extends AnyVal { + def hours = secondsOfDay / 3600 + def minutes = (secondsOfDay / 60) % 60 + def seconds = secondsOfDay % 60 + + override def toString = "%02d:%02d:%02d".format(hours, minutes, seconds) + } +} + +object Test extends dotty.runtime.LegacyApp { + + val y1: test1.TOD = new test1.TOD(1000) + val y2: test2.TOD = test2.Client.newTOD(1000) + val x1: test1.TOD = test1.TOD(1000) + val x2: test2.TOD = test2.TOD(1000) + val x3: test3.TOD = test3.TOD(1000) + println(y1.minutes) + println(y1) + println(y2.minutes) + println(y2) + println(x1.minutes) + println(x1) + println(x2.minutes) + println(x2) + println(x3.minutes) + println(x3) +} + |