From 0ef0f40ae31bf8a1e3d5b9c6eea7ef5b5a073192 Mon Sep 17 00:00:00 2001 From: Burak Emir Date: Wed, 24 Jan 2007 15:20:25 +0000 Subject: moved working tests to files --- test/files/pos/bug402.scala | 7 +++ test/files/pos/bug419.scala | 11 ++++ test/files/pos/bug430.scala | 20 +++++++ test/files/pos/bug443.scala | 14 +++++ test/files/pos/bug796.scala | 26 +++++++++ test/files/pos/gosh.scala | 44 +++++++++++++++ test/files/pos/init.scala | 14 +++++ test/files/pos/unapplyComplex.scala | 37 +++++++++++++ test/files/pos/unapplyContexts2.scala | 11 ++++ test/files/pos/unapplyGeneric.scala | 11 ++++ test/files/pos/unapplyNeedsMemberType.scala | 25 +++++++++ test/files/pos/unapplySeq.scala | 26 +++++++++ test/files/run/bug405.scala | 5 ++ test/files/run/retclosure.scala | 23 ++++++++ test/files/run/unapply.scala | 86 +++++++++++++++++++++++++++++ 15 files changed, 360 insertions(+) create mode 100644 test/files/pos/bug402.scala create mode 100644 test/files/pos/bug419.scala create mode 100644 test/files/pos/bug430.scala create mode 100644 test/files/pos/bug443.scala create mode 100644 test/files/pos/bug796.scala create mode 100644 test/files/pos/gosh.scala create mode 100644 test/files/pos/init.scala create mode 100644 test/files/pos/unapplyComplex.scala create mode 100644 test/files/pos/unapplyContexts2.scala create mode 100644 test/files/pos/unapplyGeneric.scala create mode 100644 test/files/pos/unapplyNeedsMemberType.scala create mode 100644 test/files/pos/unapplySeq.scala create mode 100644 test/files/run/bug405.scala create mode 100644 test/files/run/retclosure.scala create mode 100644 test/files/run/unapply.scala (limited to 'test/files') diff --git a/test/files/pos/bug402.scala b/test/files/pos/bug402.scala new file mode 100644 index 0000000000..a5a3df4825 --- /dev/null +++ b/test/files/pos/bug402.scala @@ -0,0 +1,7 @@ +object Main { + def main(args: Array[String]): Unit = { + val x: Any = 2 + System.out.println((0.5).isInstanceOf[Int]); + System.out.println(x.isInstanceOf[Int]); + } +} diff --git a/test/files/pos/bug419.scala b/test/files/pos/bug419.scala new file mode 100644 index 0000000000..65dcb04356 --- /dev/null +++ b/test/files/pos/bug419.scala @@ -0,0 +1,11 @@ +trait Bar { + class Config {} + var config: Config; // aha, traits can have variables? +} + +object Foo extends Bar { + + class FooConfig extends Config; + var config: Config = new FooConfig() // or not + +} diff --git a/test/files/pos/bug430.scala b/test/files/pos/bug430.scala new file mode 100644 index 0000000000..7e3e7eaaec --- /dev/null +++ b/test/files/pos/bug430.scala @@ -0,0 +1,20 @@ +object Test extends Application { + def foo[T <% Ordered[T]](x: T): Unit = System.out.println(""+(x < x)+" "+(x <= x)) + def bar(x: Unit ): Unit = foo(x); + def bar(x: Boolean): Unit = foo(x); + def bar(x: Byte ): Unit = foo(x); + def bar(x: Short ): Unit = foo(x); + def bar(x: Int ): Unit = foo(x); + def bar(x: Long ): Unit = foo(x); + def bar(x: Float ): Unit = foo(x); + def bar(x: Double ): Unit = foo(x); + bar(()) + bar(true) + bar(1: byte) + bar(1: short) + bar('a') + bar(1) + bar(1l) + bar(1.0f) + bar(1.0) +} diff --git a/test/files/pos/bug443.scala b/test/files/pos/bug443.scala new file mode 100644 index 0000000000..5b83e9d2cb --- /dev/null +++ b/test/files/pos/bug443.scala @@ -0,0 +1,14 @@ +object Test { + + def lookup(): Option[Pair[String, String]] = + (null: Option[Pair[String, String]]) match { + case Some(Pair(_, _)) => + if (true) + Some(Pair(null, null)) + else + lookup() match { + case Some(_) => Some(null) + case None => None + } + } +} diff --git a/test/files/pos/bug796.scala b/test/files/pos/bug796.scala new file mode 100644 index 0000000000..756c103e7c --- /dev/null +++ b/test/files/pos/bug796.scala @@ -0,0 +1,26 @@ +/** I know what I am doing is wrong -- since I am about to look into + * this bug, I add a test in pending/pos... however, I am afraid that + * once this bug is fixed, this test case might go into test/pos + * there it adds to the huge number of tiny little test cases. + * + * Ideally, an option in the bugtracking system would automatically + * handle "pos" bugs. + */ +object Test extends Application { + + object Twice { + def apply(x: int) = x * 2 + def unapply(x: int): Option[Tuple1[int]] = + if (x % 2 == 0) Some(Tuple1(x / 2)) + else None + } + + def test(x: int) = x match { + case Twice(y) => "x is two times "+y + case _ => "x is odd" + } + + Console.println(test(3)) + Console.println(test(4)) + +} diff --git a/test/files/pos/gosh.scala b/test/files/pos/gosh.scala new file mode 100644 index 0000000000..c4cd3df80b --- /dev/null +++ b/test/files/pos/gosh.scala @@ -0,0 +1,44 @@ +object ShapeTest extends Application { + + class Point(x : int, y : int) { + override def toString() = "[" + x + "," + y + "]" + } + + abstract class Shape { + def draw() : unit + } + + class Line(s : Point, e : Point) extends Shape { + def draw() : unit = { Console.println("draw line " + s + "," + e) } + } + + abstract class Foo { + type T <: Object + + def show(o : T) : unit + def print() : unit = {Console.println("in Foo")} + } + + abstract class ShapeFoo extends Foo { + type T <: Shape + def show(o : T) : unit = { o.draw() } + override def print() : unit = {Console.println("in ShapeFoo")} + } + + class LineFoo extends ShapeFoo { + type T = Line + override def print() : unit = {Console.println("in LineFoo")} + } + + val p1 = new Point(1,4) + val p2 = new Point(12, 28) + + val l1 = new Line(p1, p2) + + + val l = new ShapeFoo{ // ** // + type T = Line // ** // + override def print() : unit = {Console.println("in LineFoo")} // ** // + } + l.show(l1) // ** // +} diff --git a/test/files/pos/init.scala b/test/files/pos/init.scala new file mode 100644 index 0000000000..c51446c804 --- /dev/null +++ b/test/files/pos/init.scala @@ -0,0 +1,14 @@ +class Foo { + + var cnt = 0 + + class Bar { + cnt = cnt + 1 + val id = cnt + } +} + +object Test extends Application { + val foo = new Foo + Console.println((new foo.Bar).id) +} diff --git a/test/files/pos/unapplyComplex.scala b/test/files/pos/unapplyComplex.scala new file mode 100644 index 0000000000..54080eb86f --- /dev/null +++ b/test/files/pos/unapplyComplex.scala @@ -0,0 +1,37 @@ +trait Complex extends Product2[double,double] + +class ComplexRect(val _1:double, val _2:double) extends Complex { + override def toString = "ComplexRect("+_1+","+_2+")" +} + +class ComplexPolar(val _1:double, val _2:double) extends Complex { + override def toString = "ComplexPolar("+_1+","+_2+")" +} + +object ComplexRect { + def unapply(z:Complex): Option[Complex] = { + if(z.isInstanceOf[ComplexRect]) Some(z) else z match { + case ComplexPolar(mod, arg) => + Some(new ComplexRect(mod*Math.cos(arg), mod*Math.sin(arg))) +} } } + +object ComplexPolar { + def unapply(z:Complex): Option[Complex] = { + if(z.isInstanceOf[ComplexPolar]) Some(z) else z match { + case ComplexRect(re,im) => + Some(new ComplexPolar(Math.sqrt(re*re + im*im), Math.atan(re/im))) +} } } + +object Test { + def main(args:Array[String]) = { + new ComplexRect(1,1) match { + case ComplexPolar(mod,arg) => // z @ ??? + Console.println("mod"+mod+"arg"+arg) + } + val Komplex = ComplexRect + new ComplexPolar(Math.sqrt(2),Math.Pi / 4.0) match { + case Komplex(re,im) => // z @ ??? + Console.println("re"+re+" im"+im) + } + } +} diff --git a/test/files/pos/unapplyContexts2.scala b/test/files/pos/unapplyContexts2.scala new file mode 100644 index 0000000000..fcf1e2ff62 --- /dev/null +++ b/test/files/pos/unapplyContexts2.scala @@ -0,0 +1,11 @@ +trait Analyzer { + val WILDCARD = "23" +} + +trait Contexts2 requires Analyzer { + class Context { + def collect(sels: List[String]): List[String] = sels match { + case List(WILDCARD) => val dummy = WILDCARD; Nil + } + } +} diff --git a/test/files/pos/unapplyGeneric.scala b/test/files/pos/unapplyGeneric.scala new file mode 100644 index 0000000000..bf88816885 --- /dev/null +++ b/test/files/pos/unapplyGeneric.scala @@ -0,0 +1,11 @@ +object Bar { + def unapply[A,B](bar:Bar[A,B]) = Some(bar) +} + +class Bar[A,B](val _1:A, val _2:B) extends Product2[A,B] + +object Test { + new Bar(2, 'a') match { + case Bar(x,y) => + } +} diff --git a/test/files/pos/unapplyNeedsMemberType.scala b/test/files/pos/unapplyNeedsMemberType.scala new file mode 100644 index 0000000000..b423257e04 --- /dev/null +++ b/test/files/pos/unapplyNeedsMemberType.scala @@ -0,0 +1,25 @@ +// error with -Xunapply, (because of missing call to memberType?) + +trait Gunk[a] { + + type Seq + + object Cons { + def unapply(s: Seq) = unapply_Cons(s) + } + def unapply_Cons(s: Any): Option[Tuple2[a, Seq]] +} + +class Join[a] extends Gunk[a] { + type Seq = JoinSeq + + abstract class JoinSeq + case class App(xs: Seq, ys: Seq) extends JoinSeq + + def append(s1: Seq, s2: Seq): Seq = s1 // mock implementation + + def unapply_Cons(s: Any) = s match { + case App(Cons(x, xs), ys) => Some(Pair(x, append(xs, ys))) + case _ => null + } +} diff --git a/test/files/pos/unapplySeq.scala b/test/files/pos/unapplySeq.scala new file mode 100644 index 0000000000..aac211de5a --- /dev/null +++ b/test/files/pos/unapplySeq.scala @@ -0,0 +1,26 @@ +object FooSeq { + def unapplySeq(x:Any): Option[Product2[Int,Seq[String]]] = { + if(x.isInstanceOf[Bar]) { + val y = x.asInstanceOf[Bar] + Some({y.size, y.name}) + } else None + } + + def main(args:Array[String]) = { + val b = new Bar + b match { + case FooSeq(s:Int,_,n:String) => Console.println("size "+s+" name "+n) + } + b.size = 54 + b.name = List("large","L") + b match { + case FooSeq(s:Int,_,n:String) => Console.println("size "+s+" name "+n) + } + } +} + +class Bar { + var size: Int = 50 + var name: Seq[String] = List("medium","M") +} + diff --git a/test/files/run/bug405.scala b/test/files/run/bug405.scala new file mode 100644 index 0000000000..a1e3864496 --- /dev/null +++ b/test/files/run/bug405.scala @@ -0,0 +1,5 @@ +object Test extends Application { + val x = M; + object M; + assert(x eq M) +} diff --git a/test/files/run/retclosure.scala b/test/files/run/retclosure.scala new file mode 100644 index 0000000000..d354cb3586 --- /dev/null +++ b/test/files/run/retclosure.scala @@ -0,0 +1,23 @@ +/* Test return expressions inside closures. + * + * See bug#834 */ + +object Test { + def response: String = { + def check: Option[String] = { + val closure: String=>Nothing = + p => return Some("some problem") // should return from check + + closure("whatever") + } + + check match { + case Some(problem) => "check failed: " + problem + case None => "ok" + } + } + + def main(args: Array[String]) { + Console.println(response) + } +} diff --git a/test/files/run/unapply.scala b/test/files/run/unapply.scala new file mode 100644 index 0000000000..d84711519f --- /dev/null +++ b/test/files/run/unapply.scala @@ -0,0 +1,86 @@ +import scala.testing.SUnit._ + +object Test { + def main(args:Array[String]) = { + Foo.run + Mas.run + Lis.run + } +} + +// this class is used for representation +class Bar { + var size: Int = 50 + var name: String = "medium" +} + +// test basic unapply for 0, 1 and 2 args and with precise type test +object Fii { + def unapply(x: Any): boolean = x.isInstanceOf[Bar] +} +object Faa { + def unapply(x: Any): Option[String] = if(x.isInstanceOf[Bar]) Some(x.asInstanceOf[Bar].name) else None +} +object FaaPrecise { + def unapply(x: Bar): Option[String] = Some(x.name) +} +object FaaPreciseSome { + def unapply(x: Bar) = Some(x.name) // return type Some[String] +} +object Foo extends Assert { + def unapply(x: Any): Option[Product2[Int, String]] = x match { + case y: Bar => Some(Tuple(y.size, y.name)) + case _ => None + } + def doMatch1(b:Bar) = b match { + case Foo(s:Int, n:String) => {s,n} + } + def doMatch2(b:Bar) = b match { + case Fii() => null + } + def doMatch3(b:Bar) = b match { + case Faa(n:String) => n + } + def doMatch4(b:Bar) = (b:Any) match { + case FaaPrecise(n:String) => n + } + def doMatch5(b:Bar) = (b:Any) match { + case FaaPreciseSome(n:String) => n + } + def run { + val b = new Bar + assertEquals(doMatch1(b),{50,"medium"}) + assertEquals(doMatch2(b),null) + assertEquals(doMatch3(b),"medium") + assertEquals(doMatch4(b),"medium") + assertEquals(doMatch5(b),"medium") + } +} + +// same, but now object is not top-level +object Mas extends Assert { + object Gaz { + def unapply(x: Any): Option[Product2[Int, String]] = x match { + case y: Baz => Some(Tuple(y.size, y.name)) + case _ => None + } + } + class Baz { + var size: Int = 60 + var name: String = "too large" + } + def run { + val b = new Baz + assertEquals(b match { + case Gaz(s:Int, n:String) => {s,n} + }, {60,"too large"}) + } +} + +object Lis extends Assert { + def run { + assertEquals((List(1,2,3): Any) match { case List(x,y,_*) => {x,y}}, {1,2}) + } +} + + -- cgit v1.2.3