diff options
Diffstat (limited to 'test/files/jvm')
31 files changed, 1024 insertions, 8 deletions
diff --git a/test/files/jvm/NestedAnnotations.java b/test/files/jvm/NestedAnnotations.java new file mode 100644 index 0000000000..8f2327dcce --- /dev/null +++ b/test/files/jvm/NestedAnnotations.java @@ -0,0 +1,25 @@ +package test; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +public class NestedAnnotations { + + @OuterAnno(inner=@InnerAnno(name="inner")) + String field; + + @Target({FIELD}) + @Retention(RUNTIME) + public static @interface InnerAnno { + String name(); + } + + @Target({FIELD}) + @Retention(RUNTIME) + public static @interface OuterAnno { + InnerAnno inner(); + } +} diff --git a/test/files/jvm/OuterEnum.java b/test/files/jvm/OuterEnum.java new file mode 100644 index 0000000000..75d3f34223 --- /dev/null +++ b/test/files/jvm/OuterEnum.java @@ -0,0 +1,5 @@ +package enums; + +public class OuterEnum { + public enum Foo { Bar } +} diff --git a/test/files/jvm/OuterTParams.java b/test/files/jvm/OuterTParams.java new file mode 100644 index 0000000000..1d3db49fcf --- /dev/null +++ b/test/files/jvm/OuterTParams.java @@ -0,0 +1,6 @@ +public class OuterTParams<A> { + class InnerClass { + // Cannot parse method signature: "()TA;" + public A method() { return null; } + } +} diff --git a/test/files/jvm/SourceAnnotation.java b/test/files/jvm/SourceAnnotation.java new file mode 100644 index 0000000000..047751ddfe --- /dev/null +++ b/test/files/jvm/SourceAnnotation.java @@ -0,0 +1,9 @@ +package test; + +import java.lang.annotation.*; + +@Retention(value=RetentionPolicy.RUNTIME) +public @interface SourceAnnotation { + public String value(); + public String[] mails() default { "bill.gates@bloodsuckers.com" }; +} diff --git a/test/files/jvm/annotations.check b/test/files/jvm/annotations.check new file mode 100644 index 0000000000..418301331f --- /dev/null +++ b/test/files/jvm/annotations.check @@ -0,0 +1,35 @@ +class java.rmi.RemoteException +class java.io.IOException +@java.lang.Deprecated() +@test.SourceAnnotation(mails={scala@lists.epfl.ch,scala-lounge@lists.epfl.ch}, value=http://scala-lang.org) +class Test4$Foo1 + +@test.SourceAnnotation(mails={you@bloodsuckers.com}, value=http://bloodsuckers.com) +class Test4$Foo2 + +@test.SourceAnnotation(mails={bill.gates@bloodsuckers.com}, value=http://bloodsuckers.com) +class Test4$Foo3 + +@test.SourceAnnotation(mails={bill.gates@bloodsuckers.com}, value=file:///dev/null) +private final int Test4$Foo4.x + +@test.SourceAnnotation(mails={bill.gates@bloodsuckers.com}, value=file:///dev/null) +public int Test4$Foo4.x() + +@test.SourceAnnotation(mails={bill.gates@bloodsuckers.com}, value=file:///dev/zero) +public int Test4$Foo5.bar() + +@test.SourceAnnotation(mails={bill.gates@bloodsuckers.com}, value=primary constructor) +public Test4$Foo6(java.lang.String) + +@test.SourceAnnotation(mails={bill.gates@bloodsuckers.com}, value=secondary constructor) +public Test4$Foo7() + +@test.SourceAnnotation(mails={bill.gates@bloodsuckers.com}, value=constructor val) +private final int Test4$Foo8.n + +@test.SourceAnnotation(mails={bill.gates@bloodsuckers.com}, value=constructor val) +public int Test4$Foo8.n() + +0 +99 diff --git a/test/files/jvm/annotations.scala b/test/files/jvm/annotations.scala new file mode 100644 index 0000000000..9af2d333ba --- /dev/null +++ b/test/files/jvm/annotations.scala @@ -0,0 +1,159 @@ +object Test1 { + class Foo { + @remote + def foo: Unit = () + } + def run { + val method = classOf[Foo].getMethod("foo") + method.getExceptionTypes foreach println + } +} + +object Test2 { + import java.io.{BufferedReader,FileReader, IOException} + class Reader(fname: String) { + private val in = new BufferedReader(new FileReader(fname)) + + @throws(classOf[IOException]) + def read() = in.read() + } + def run { + val method = classOf[Reader].getMethod("read") + method.getExceptionTypes foreach println + } +} + +/* Java: +public class Main { + @Deprecated + public void foo() {} + public static void main(String[] args) throws Exception { + Method method = Class.forName("test.Main").getMethod("foo", new Class[]{}); + Annotation annotation = method.getAnnotation(Deprecated.class); + System.out.println(annotation); // @java.lang.Deprecated() + } +} +*/ +object Test3 { + import java.lang.Deprecated + class Foo { + @Deprecated + def foo: Unit = () + } + def run { + val method = classOf[Foo].getMethod("foo") + val annotation = method.getAnnotation(classOf[Deprecated]) + println(annotation) + } +} + +/* Java: +@Retention(value=RetentionPolicy.RUNTIME) +@interface Source { + public String url(); + public String mail(); +} +@Source(url="http://scala.epfl.ch", mail="scala@lists.epfl.ch") +class Foo {} +public class Main { + public static void main(String[] args) throws Exception { + Class clazz = Class.forName("test.Foo"); + Annotation[] annotations = clazz.getAnnotations(); + for (int i = 0; i < annotations.length; i++) + System.out.println(annotations[i]); + // @test.Main$Source(url=http://scala-lang.org, mail=scala@lists.epfl.ch) + } +} +*/ +object Test4 { + import test.SourceAnnotation // defined in SourceAnnotation.java + @SourceAnnotation{val value = "http://scala-lang.org", + val mails = Array("scala@lists.epfl.ch", "scala-lounge@lists.epfl.ch")} + class Foo1 + @SourceAnnotation("http://bloodsuckers.com") { val mails = Array("you@bloodsuckers.com") } + class Foo2 + @SourceAnnotation("http://bloodsuckers.com") + class Foo3 + class Foo4 { + @SourceAnnotation("file:///dev/null") + val x = 1 + } + class Foo5 { + @SourceAnnotation("file:///dev/zero") + def bar: Int = 0 + } + class Foo6 @SourceAnnotation("primary constructor")(s: String) { + // to guarantee that primary constructor annotations + // are not applied to secondary constructors + def this() = this("") + } + class Foo7(s: String) { + @SourceAnnotation("secondary constructor") + def this() = this("") + } + class Foo8(@SourceAnnotation("constructor val") val n: Int) {} + def run { + import java.lang.annotation.Annotation + import java.lang.reflect.AnnotatedElement + def printSourceAnnotations(target: AnnotatedElement) { + //print SourceAnnotation in a predefined way to insure + // against difference in the JVMs (e.g. Sun's vs IBM's) + def printSourceAnnotation(a: Annotation) { + val ann = a.asInstanceOf[SourceAnnotation] + println("@test.SourceAnnotation(mails=" + ann.mails.deepMkString("{", ",", "}") + + ", value=" + ann.value + ")") + } + val anns = target.getAnnotations() + anns foreach printSourceAnnotation + if (anns.length > 0) { + println(target) + println + } + } + printSourceAnnotations(classOf[Foo1]) + printSourceAnnotations(classOf[Foo2]) + printSourceAnnotations(classOf[Foo3]) + classOf[Foo4].getDeclaredFields foreach printSourceAnnotations + classOf[Foo4].getDeclaredMethods foreach printSourceAnnotations + classOf[Foo5].getDeclaredMethods foreach printSourceAnnotations + classOf[Foo6].getDeclaredConstructors foreach printSourceAnnotations + classOf[Foo7].getDeclaredConstructors foreach printSourceAnnotations + classOf[Foo8].getDeclaredFields foreach printSourceAnnotations + classOf[Foo8].getDeclaredMethods foreach printSourceAnnotations + } +} + +object Test5 { + import scala.reflect.BeanProperty + import java.lang.Integer + + class Count { + // we use "Integer" instead of "Int" because of Java reflection + @BeanProperty + private var count: Integer = 0 + + private val getter = + getClass().getMethod("getCount") + private val setter = + getClass().getMethod("setCount", classOf[Integer]) + + def get = getter.invoke(this).asInstanceOf[Integer].intValue + def set(n: Int) = setter.invoke(this, new Integer(n)) + } + def run { + val count = new Count + println(count.get) + count.set(99) + println(count.get) + } +} + +object Test { + def main(args: Array[String]) { + Test1.run + Test2.run + Test3.run // requires the use of -target:jvm-1.5 + Test4.run + Test5.run + } +} diff --git a/test/files/jvm/bug676.check b/test/files/jvm/bug676.check new file mode 100644 index 0000000000..5d1c9925f9 --- /dev/null +++ b/test/files/jvm/bug676.check @@ -0,0 +1 @@ +RUNTIME diff --git a/test/files/jvm/bug676.scala b/test/files/jvm/bug676.scala new file mode 100644 index 0000000000..bcd27e4e21 --- /dev/null +++ b/test/files/jvm/bug676.scala @@ -0,0 +1,8 @@ +object Test { + def main(args: Array[String]): Unit = { + import java.lang.annotation.Retention; + val c = classOf[Retention] + val r: Retention = c.getAnnotation(c).asInstanceOf[Retention]; + Console.println(r.value) + } +} diff --git a/test/files/jvm/bug680.check b/test/files/jvm/bug680.check new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/files/jvm/bug680.check diff --git a/test/files/jvm/bug680.scala b/test/files/jvm/bug680.scala new file mode 100644 index 0000000000..b0b0c9f7c0 --- /dev/null +++ b/test/files/jvm/bug680.scala @@ -0,0 +1,6 @@ +object Test { + def main(args:Array[String]) { + val sb = new java.lang.StringBuilder() // use Java 1.5 + sb.setLength(0) + } +} diff --git a/test/files/jvm/console.check b/test/files/jvm/console.check new file mode 100644 index 0000000000..ef7c169844 --- /dev/null +++ b/test/files/jvm/console.check @@ -0,0 +1,3 @@ +true11.0.. +1 +Argument nr. 1 has value 3.33 diff --git a/test/files/jvm/console.scala b/test/files/jvm/console.scala new file mode 100644 index 0000000000..b07765675c --- /dev/null +++ b/test/files/jvm/console.scala @@ -0,0 +1,14 @@ + +/** Test scala.Console functionality. */ +object Test extends Application { + + import Console._ + print(true) + print(1) + print(1.0) + flush + println("..") + println(1) + printf("Argument nr. %d has value %1.2f\n", + 1, 10.0/3) +} diff --git a/test/files/jvm/genericNest.scala b/test/files/jvm/genericNest.scala new file mode 100644 index 0000000000..c1b0210117 --- /dev/null +++ b/test/files/jvm/genericNest.scala @@ -0,0 +1,13 @@ +/** found in genericNest.jar, compiled from OuterTParams.java */ +import nestpkg._; + +// bug #695 +object ForceParse extends OuterTParams[AnyRef] { + // Force import of HarderToParse<A>.InnerClass, + // which has confusing method signature. + var field: InnerClass = null +} + +object Test extends Application { + ForceParse +} diff --git a/test/files/jvm/interpreter.check b/test/files/jvm/interpreter.check new file mode 100644 index 0000000000..86d6adbbec --- /dev/null +++ b/test/files/jvm/interpreter.check @@ -0,0 +1,218 @@ +Type in expressions to have them evaluated. +Type :help for more information. + +scala> +scala> +scala> res0: Int = 7 + +scala> | | | | gcd: (Int,Int)Int + +scala> five: Int = 5 + +scala> x: Int = 1 + +scala> x: Int = 2 + +scala> three: Int = 3 + +scala> defined type alias anotherint + +scala> four: anotherint = 4 + +scala> <console>:5: error: type mismatch; + found : java.lang.String("hello") + required: anotherint + val bogus: anotherint = "hello" + ^ + +scala> defined trait PointlessTrait + +scala> x: Int = 2 +y: Int = 3 + +scala> hello + +scala> +scala> +scala> t1513: Array[Null] = Array(null) + +scala> +scala> atom: scala.xml.Atom[Unit] = () + +scala> +scala> defined class S + +scala> fish: S = fish + +scala> +scala> arr: Array[java.lang.String] = Array(What's, up, doc?) + +scala> +scala> arrInt: Any = Array(1, 2, 3) + +scala> +scala> arrArrInt: Any = Array(Array(1, 2), Array(3, 4)) + +scala> +scala> +scala> defined class Foo + +scala> defined class Bar + +scala> foo2bar: (Foo)Bar + +scala> bar: Bar = Bar(3) + +scala> +scala> +scala> import bar._ + +scala> m: Int = 3 + +scala> +scala> +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> one: Int = 1 + +scala> +scala> +scala> x1: Int = 1 + +scala> x2: Int = 1 + +scala> x3: Int = 1 + +scala> x4: Int = 1 + +scala> x5: Int = 1 + +scala> x6: Int = 1 + +scala> x7: Int = 1 + +scala> x8: Int = 1 + +scala> x9: Int = 1 + +scala> x10: Int = 1 + +scala> x11: Int = 1 + +scala> x12: Int = 1 + +scala> x13: Int = 1 + +scala> x14: Int = 1 + +scala> x15: Int = 1 + +scala> x16: Int = 1 + +scala> x17: Int = 1 + +scala> x18: Int = 1 + +scala> x19: Int = 1 + +scala> x20: Int = 1 + +scala> +scala> two: Int = 2 + +scala> +scala> +scala> +scala> +scala> <console>:1: error: '=' expected but '=>' found. + def x => y => z + ^ + +scala> <console>:1: error: expected start of definition + [1,2,3] + ^ + +scala> +scala> +scala> +scala> | | | | res3: scala.xml.Elem = +<a> + <b d="dd" c="c"/></a> + +scala> +scala> +scala> | | | | +scala> +scala> +scala> +scala> | | | res4: java.lang.String = + + hello + there + + +scala> +scala> | | You typed two blank lines. Starting a new command. + +scala> +scala> match: Int + +scala> x: Int = 1 + +scala> +scala> +scala> defined class Exp +defined class Fact +defined class Term + +scala> | | <console>:15: warning: match is not exhaustive! +missing combination Term + + def f(e: Exp) = e match { // non-exhaustive warning here + ^ +f: (Exp)Int + +scala> +scala> +plusOne: (Int)Int +res0: Int = 6 +res0: java.lang.String = after reset +<console>:5: error: not found: value plusOne + plusOne(5) // should be undefined now + ^ diff --git a/test/files/jvm/interpreter.scala b/test/files/jvm/interpreter.scala new file mode 100644 index 0000000000..21c88734e0 --- /dev/null +++ b/test/files/jvm/interpreter.scala @@ -0,0 +1,179 @@ +import scala.tools.nsc._ +import java.io.{BufferedReader, StringReader, PrintWriter, + Writer, OutputStreamWriter} + +object Test { + val testCodeString = <code> +// basics +3+4 +def gcd(x: Int, y: Int): Int = {{ + if (x == 0) y + else if (y == 0) x + else gcd(y%x, x) +}} +val five = gcd(15,35) +var x = 1 +x = 2 +val three = x+1 +type anotherint = Int +val four: anotherint = 4 +val bogus: anotherint = "hello" +trait PointlessTrait +val (x,y) = (2,3) +println("hello") + +// ticket #1513 +val t1513 = Array(null) +// ambiguous toString problem from #547 +val atom = new scala.xml.Atom() +// overriding toString problem from #1404 +class S(override val toString : String) +val fish = new S("fish") +// Test that arrays pretty print nicely. +val arr = Array("What's", "up", "doc?") +// Test that arrays pretty print nicely, even when we give them type Any +val arrInt : Any = Array(1,2,3) +// Test that nested arrays are pretty-printed correctly +val arrArrInt : Any = Array(Array(1, 2), Array(3, 4)) + +// implicit conversions +case class Foo(n: Int) +case class Bar(n: Int) +implicit def foo2bar(foo: Foo) = Bar(foo.n) +val bar: Bar = Foo(3) + +// importing from a previous result +import bar._ +val m = n + +// stressing the imports mechanism +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 +val one = 1 + + +val x1 = 1 +val x2 = 1 +val x3 = 1 +val x4 = 1 +val x5 = 1 +val x6 = 1 +val x7 = 1 +val x8 = 1 +val x9 = 1 +val x10 = 1 +val x11 = 1 +val x12 = 1 +val x13 = 1 +val x14 = 1 +val x15 = 1 +val x16 = 1 +val x17 = 1 +val x18 = 1 +val x19 = 1 +val x20 = 1 + +val two = one + x5 + + +// interior syntax errors should *not* go into multi-line input mode. +// both of the following should abort immediately: +def x => y => z +[1,2,3] + + +// multi-line XML +<a> +<b + c="c" + d="dd" +/></a> + + +/* + /* + multi-line comment + */ +*/ + + +// multi-line string +""" +hello +there +""" + +(1 + // give up early by typing two blank lines + + +// defining and using quoted names should work (ticket #323) +def `match` = 1 +val x = `match` + +// multiple classes defined on one line +sealed class Exp; class Fact extends Exp; class Term extends Exp +def f(e: Exp) = e match {{ // non-exhaustive warning here + case _:Fact => 3 +}} + +</code>.text + + /** A writer that skips the first line of text. The first + * line of interpreter output is skipped because it includes + * a version number. */ + class Skip1Writer(writer: Writer) extends Writer { + var seenNL = false + + def write(cbuf: Array[Char], off: Int, len: Int) { + if (seenNL) + writer.write(cbuf, off, len) + else { + val slice : Array[Char] = cbuf.slice(off, off+len) + val i = slice.indexOf('\n') + if (i >= 0) { + seenNL = true + writer.write(slice, i+1, slice.length-(i+1)) + } else { + // skip it + } + } + } + + def close() { writer.close() } + def flush() { writer.flush() } + } + + + def main(args: Array[String]) { + val input = new BufferedReader(new StringReader(testCodeString)) + val output = new PrintWriter( + new Skip1Writer(new OutputStreamWriter(Console.out))) + val repl = new InterpreterLoop(input, output) + repl.main(new Settings) + println() + + val interp = new Interpreter(new Settings) + interp.interpret("def plusOne(x: Int) = x + 1") + interp.interpret("plusOne(5)") + interp.reset() + interp.interpret("\"after reset\"") + interp.interpret("plusOne(5) // should be undefined now") + } +} diff --git a/test/files/jvm/mkAnnotationsJar.sh b/test/files/jvm/mkAnnotationsJar.sh new file mode 100755 index 0000000000..0cd5e631a7 --- /dev/null +++ b/test/files/jvm/mkAnnotationsJar.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +############################################################################## +# Author : Nikolay Mihaylov +# Revision: $Id$ +############################################################################## + +############################################################################## +# variables + +OBJDIR=./classes + +if [ -z "${JAVA_HOME}" ]; then + echo "environment variable JAVA_HOME is undefined." + exit +fi + +JAVAC=${JAVA_HOME}/bin/javac +JAVAC_OPTIONS="-source 1.5 -target 1.5" + +JAR=${JAVA_HOME}/bin/jar + +############################################################################## +# commands + +mkdir -p ${OBJDIR} +${JAVAC} ${JAVAC_OPTIONS} -d ${OBJDIR} SourceAnnotation.java NestedAnnotations.java +${JAR} cf ../lib/annotations.jar -C ${OBJDIR} . +rm -rf ${OBJDIR} diff --git a/test/files/jvm/outerEnum.check b/test/files/jvm/outerEnum.check new file mode 100644 index 0000000000..ebd7525b33 --- /dev/null +++ b/test/files/jvm/outerEnum.check @@ -0,0 +1 @@ +Bar diff --git a/test/files/jvm/outerEnum.scala b/test/files/jvm/outerEnum.scala new file mode 100644 index 0000000000..18794b7dbe --- /dev/null +++ b/test/files/jvm/outerEnum.scala @@ -0,0 +1,9 @@ +import enums._ + +object Test extends Application { + def foo { + val res: OuterEnum.Foo = OuterEnum.Foo.Bar + println(res) + } + foo +} diff --git a/test/files/jvm/stringbuilder.scala b/test/files/jvm/stringbuilder.scala new file mode 100644 index 0000000000..cbbfa022b0 --- /dev/null +++ b/test/files/jvm/stringbuilder.scala @@ -0,0 +1,100 @@ +/* $Id$ */ + +import testing.SUnit._ + +/** Test the Scala implementation of class <code>scala.StringBuilder</code>. + * + * @author Stephane Micheloud + */ +object Test extends TestConsoleMain { + def suite = new TestSuite( + Test1, //ctor, reverse + Test2, //append + Test3, //insert + Test4 //indexOf, lastIndexOf + ) +} + +object Test1 extends TestCase("ctor") with Assert { + override def enableStackTrace = false + override def runTest { + val j0 = new java.lang.StringBuilder("abc") // Java 1.5+ + val s0 = new StringBuilder("abc") + assertEquals("s0 equals j0", false, s0 equals j0) + assertEquals("s0.toString equals j0.toString", true, s0.toString equals j0.toString) + + val str = """ +Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages. It is also fully interoperable with Java.""" + val j1 = new java.lang.StringBuilder(100) append str + val s1 = new java.lang.StringBuilder(100) append str + assertEquals("s1.toString equals j1.toString", true, s1.toString equals j1.toString) + + val j2 = j0 reverse + val s2 = s0 reverse; + //println("j2="+j2+", s2="+s2)//debug + assertEquals("s2.toString equals j2.toString", true, s2.toString equals j2.toString) + + val j3 = j2; j3 setCharAt (0, j3 charAt 2) + val s3 = s2; s3(0) = s3(2) + //println("j3="+j3+", s3="+s3)//debug + assertEquals("s3.toString equals j3.toString", true, s3.toString equals j3.toString) + } +} + +object Test2 extends TestCase("append") with Assert { + override def enableStackTrace = false + override def runTest { + val j0 = new java.lang.StringBuilder("abc") // Java 1.5+ + val s0 = new StringBuilder("abc") + + j0 append true append (1.toByte) append 'a' append 9 append -1L append 1.2e-10f append -2.1e+100d + s0 append true append (1.toByte) append 'a' append 9 append -1L append 1.2e-10f append -2.1e+100d + + assertEquals("s0.toString equals j0.toString", true, s0.toString equals j0.toString) + + val j1 = new java.lang.StringBuilder // Java 1.5+ + val s1 = new StringBuilder + j1 append "###" append Array('0', '1', '2') append "xyz".subSequence(0, 3) + s1 append "###" append Array('0', '1', '2') append List('x', 'y', 'z') + assertEquals("s1.toString equals j1.toString", true, s1.toString equals j1.toString) + } +} + +object Test3 extends TestCase("insert") with Assert { + override def enableStackTrace = false + override def runTest { + val j0 = new java.lang.StringBuilder("abc") // Java 1.5+ + val s0 = new StringBuilder("abc") + + j0 insert (0, true) insert (0, 1.toByte) insert (0, 'a') insert (0, 88.toShort) insert (0, 9) insert (0, -1L) + s0 insert (0, true) insert (0, 1.toByte) insert (0, 'a') insert (0, 88.toShort) insert (0, 9) insert (0, -1L) + //println("j0="+j0+", s0="+s0)//debug + assertEquals("s0.toString equals j0.toString", true, s0.toString equals j0.toString) + + val j1 = new java.lang.StringBuilder // Java 1.5+ + val s1 = new StringBuilder + j1 insert (0, "###") insert (0, Array('0', '1', '2')) insert (0, "xyz".subSequence(0, 3)) + s1 insert (0, "###") insert (0, Array('0', '1', '2')) insert (0, List('x', 'y', 'z')) + //println("j1="+j1+", s1="+s1)//debug + assertEquals("s1.toString equals j1.toString", true, s1.toString equals j1.toString) + + } +} + +object Test4 extends TestCase("indefOf") with Assert { + override def enableStackTrace = false + override def runTest { + val j0 = new java.lang.StringBuilder("abc") // Java 1.5+ + val s0 = new StringBuilder("abc") + + val j1 = j0 indexOf("c") + val s1 = s0 indexOf("c") + //println("j1="+j1+", s1="+s1)//debug + assertEquals("s1 == j1", true, s1 == j1) + + val j2 = j0 append "123abc" lastIndexOf("c") + val s2 = s0 append "123abc" lastIndexOf("c") + //println("j2="+j2+", s2="+s2)//debug + assertEquals("s2 == j2", true, s2 == j2) + } +} diff --git a/test/files/jvm/sync-var.check b/test/files/jvm/sync-var.check new file mode 100644 index 0000000000..e77aa319a5 --- /dev/null +++ b/test/files/jvm/sync-var.check @@ -0,0 +1 @@ +50005000 50005000 true diff --git a/test/files/jvm/sync-var.scala b/test/files/jvm/sync-var.scala new file mode 100644 index 0000000000..aa6ae9fa34 --- /dev/null +++ b/test/files/jvm/sync-var.scala @@ -0,0 +1,53 @@ +import java.util.concurrent._ +import java.util.concurrent.atomic._ + +object Test { def main(args: Array[String]) { + +val n = 10000 +val i = new AtomicInteger(n) +val j = new AtomicInteger(n) +val sum = new AtomicInteger + +val q = new scala.concurrent.SyncVar[Int] + +val producers = (1 to 3).force map { z => new Thread { + override def run() { + var again = true + while (again) { + val x = i.getAndDecrement() + if (x > 0) + q put x + else + again = false + } + } +} } + +val summers = (1 to 7).force map { z => new Thread { + override def run() { + val x = j.decrementAndGet() + if (x >= 0) { + sum addAndGet q.take() + } + if (x > 0) { + run() + } else { + // done + } + } +} } + +summers foreach { _.start() } +producers foreach { _.start() } + +summers foreach { _.join() } + +val got = sum.get +val expected = (n + 1) * n / 2 +println(got + " " + expected + " " + (got == expected)) + +producers foreach { _.join() } + +} } + +// vim: set ts=2 sw=2 et: diff --git a/test/files/jvm/t0014.check b/test/files/jvm/t0014.check new file mode 100644 index 0000000000..bece7db7af --- /dev/null +++ b/test/files/jvm/t0014.check @@ -0,0 +1 @@ +test.NestedAnnotations diff --git a/test/files/jvm/t0014.scala b/test/files/jvm/t0014.scala new file mode 100644 index 0000000000..a1948702dc --- /dev/null +++ b/test/files/jvm/t0014.scala @@ -0,0 +1,5 @@ +object Test { + def main(args: Array[String]) { + println(classOf[test.NestedAnnotations].getName) + } +} diff --git a/test/files/jvm/t1461.scala b/test/files/jvm/t1461.scala new file mode 100644 index 0000000000..f0e3cea6cd --- /dev/null +++ b/test/files/jvm/t1461.scala @@ -0,0 +1,18 @@ + +object Test { + def main(args: Array[String]) { + val jl = classOf[Foo].getMethod("jl", classOf[Baz[_]]) + jl.getGenericParameterTypes // works fine + + val l = classOf[Foo].getMethod("l", classOf[Baz[_]]) + // By debugger inspection l.signature is (Ltest/Baz<J>;)V + l.getGenericParameterTypes // throws GenericSignatureFormatError + } +} + +class Baz[T] + +class Foo { + def l(b: Baz[Long]) { } + def jl(b: Baz[java.lang.Long]) { } +} diff --git a/test/files/jvm/t1464.check b/test/files/jvm/t1464.check new file mode 100644 index 0000000000..c508d5366f --- /dev/null +++ b/test/files/jvm/t1464.check @@ -0,0 +1 @@ +false diff --git a/test/files/jvm/t1464/MyTrait.scala b/test/files/jvm/t1464/MyTrait.scala new file mode 100644 index 0000000000..0b8ccc412b --- /dev/null +++ b/test/files/jvm/t1464/MyTrait.scala @@ -0,0 +1,5 @@ +trait MyTrait { + type K + def findChildByClass[T <: K with MyTrait]: Unit + +} diff --git a/test/files/jvm/t1464/Test.java b/test/files/jvm/t1464/Test.java new file mode 100644 index 0000000000..235848b1df --- /dev/null +++ b/test/files/jvm/t1464/Test.java @@ -0,0 +1,6 @@ +public class Test { + public static void main(String[] args) { + Object o = new Object(); + System.out.println(o instanceof MyTrait); + } +} diff --git a/test/files/jvm/throws-annot.check b/test/files/jvm/throws-annot.check new file mode 100644 index 0000000000..a0ed82b106 --- /dev/null +++ b/test/files/jvm/throws-annot.check @@ -0,0 +1,21 @@ +read throws: class java.io.IOException +read annotations: +readWith2 throws: class java.lang.ClassCastException, class java.io.IOException +readWith2 annotations: +readMixed throws: class java.io.IOException, class java.lang.NullPointerException +readMixed annotations: @java.lang.Deprecated() +readMixed2 throws: class java.io.IOException, class java.lang.NullPointerException +readMixed2 annotations: @java.lang.Deprecated() +readNoEx throws: +readNoEx annotations: @java.lang.Deprecated() +Testing mirror class +read throws: class java.io.IOException +read annotations: +readWith2 throws: class java.lang.ClassCastException, class java.io.IOException +readWith2 annotations: +readMixed throws: class java.io.IOException, class java.lang.NullPointerException +readMixed annotations: @java.lang.Deprecated() +readMixed2 throws: class java.io.IOException, class java.lang.NullPointerException +readMixed2 annotations: @java.lang.Deprecated() +readNoEx throws: +readNoEx annotations: @java.lang.Deprecated() diff --git a/test/files/jvm/throws-annot.scala b/test/files/jvm/throws-annot.scala new file mode 100644 index 0000000000..90b58b9976 --- /dev/null +++ b/test/files/jvm/throws-annot.scala @@ -0,0 +1,74 @@ +/** Test the @throws annotation */ +import java.io.IOException + +object TestThrows { + + abstract class Foo { + + @throws(classOf[IOException]) + def read(): Int + + @throws(classOf[ClassCastException]) + @throws(classOf[IOException]) + def readWith2(): Int + + @throws(classOf[IOException]) + @Deprecated + @throws(classOf[NullPointerException]) + def readMixed(): Int + + @Deprecated + @throws(classOf[IOException]) + @throws(classOf[NullPointerException]) + def readMixed2(): Int + + @Deprecated + def readNoEx(): Int + } + + def checkMethod(cls: Class[_], name: String) { + val method = cls.getMethod(name) + println(name + " throws: " + method.getExceptionTypes.mkString("", ", ", "")) + println(name + " annotations: " + method.getDeclaredAnnotations.mkString("", ", ", "")) + } + + def run(cls: Class[_]) { + checkMethod(cls, "read") + checkMethod(cls, "readWith2") + checkMethod(cls, "readMixed") + checkMethod(cls, "readMixed2") + checkMethod(cls, "readNoEx") + } +} + +/** Test the top-level mirror that is has the annotations. */ +object TL { + + @throws(classOf[IOException]) + def read(): Int = 0 + + @throws(classOf[ClassCastException]) + @throws(classOf[IOException]) + def readWith2(): Int = 0 + + @throws(classOf[IOException]) + @Deprecated + @throws(classOf[NullPointerException]) + def readMixed(): Int = 0 + + @Deprecated + @throws(classOf[IOException]) + @throws(classOf[NullPointerException]) + def readMixed2(): Int = 0 + + @Deprecated + def readNoEx(): Int = 0 +} + +object Test { + def main(args: Array[String]) { + TestThrows.run(classOf[TestThrows.Foo]) + println("Testing mirror class") + TestThrows.run(Class.forName("TL")) + } +} diff --git a/test/files/jvm/typerep.check b/test/files/jvm/typerep.check index ff98348304..ca93180946 100644..100755 --- a/test/files/jvm/typerep.check +++ b/test/files/jvm/typerep.check @@ -9,10 +9,12 @@ Float Double String Unit +Class[Int] Some[Int] Some[Int] Some[Some[Int]] +Some[List[Int]] None None diff --git a/test/files/jvm/typerep.scala b/test/files/jvm/typerep.scala index 47bd335d32..fc94817adf 100644..100755 --- a/test/files/jvm/typerep.scala +++ b/test/files/jvm/typerep.scala @@ -49,7 +49,7 @@ object testPrimitives { println(getType(0.0d)) println(getType("abc")) println(getType(())) // Unit -// println(getType(classOf[Int])) // Class + println(getType(classOf[Int])) // Class println } @@ -58,6 +58,8 @@ object testOptions { val x: Option[Int] = Some(2) println(getType(x)) println(getType(Some(Some(3)))) + println(getType(Some(List(3)))) + //println(getType(Some(None: List[Int]))) // error: no implicit argument matching parameter type TypeRep[object None] was foun println(getType(None: Option[Int])) val y: Option[Int] = None println(getType(y)) @@ -156,7 +158,7 @@ object TypeRep { def getType[A](x: Option[A])(implicit rep: TypeRep[A]): TypeRep[Option[A]] = (x match { case Some(v) => SomeRep(rep) - case None => NoneRep // or NoneRep(rep) + case None => NoneRep }).asInstanceOf[TypeRep[Option[A]]] def getType[A](x: List[A])(implicit rep: TypeRep[A]): TypeRep[List[A]] = (x match { @@ -174,12 +176,14 @@ object TypeRep { implicit def doubleRep: TypeRep[Double] = DoubleRep implicit def unitRep: TypeRep[Unit] = UnitRep -// implicit def classRep: TypeRep[Class] = ClassRep implicit def stringRep: TypeRep[String] = StringRep - implicit def noneRep: TypeRep[Option[Nothing]] = NoneRep[Nothing](NothingRep.asInstanceOf[TypeRep[Nothing]]) + //implicit def noneRep: TypeRep[None.type] = NoneRep//[Nothing](NothingRep.asInstanceOf[TypeRep[Nothing]]) implicit def anyRep: TypeRep[Any] = AnyRep implicit def nothingRep: TypeRep[Nothing] = NothingRep + implicit def classRep[A](implicit elemrep: TypeRep[A]): TypeRep[Class[A]] = + ClassRep(elemrep) + implicit def someRep[A](implicit elemrep: TypeRep[A]): TypeRep[Some[A]] = SomeRep(elemrep) @@ -189,9 +193,10 @@ object TypeRep { implicit def arrayRep[A](implicit elemrep: TypeRep[A]): TypeRep[Array[A]] = ArrayRep(elemrep) - implicit def tuple2Rep[A1, A2](implicit _1: TypeRep[A1], _2: TypeRep[A2]): TypeRep[Tuple2[A1, A2]] = + implicit def tuple2Rep[A1, A2](implicit _1: TypeRep[A1], _2: TypeRep[A2]): TypeRep[(A1, A2)] = Tuple2Rep(_1, _2) - implicit def tuple3Rep[A1, A2, A3](implicit _1: TypeRep[A1], _2: TypeRep[A2], _3: TypeRep[A3]): TypeRep[Tuple3[A1, A2, A3]] = + + implicit def tuple3Rep[A1, A2, A3](implicit _1: TypeRep[A1], _2: TypeRep[A2], _3: TypeRep[A3]): TypeRep[(A1, A2, A3)] = Tuple3Rep(_1, _2, _3) implicit def tuple4Rep[A1, A2, A3, A4](implicit _1: TypeRep[A1], _2: TypeRep[A2], _3: TypeRep[A3], _4: TypeRep[A4]): TypeRep[Tuple4[A1, A2, A3, A4]] = Tuple4Rep(_1, _2, _3, _4) @@ -263,7 +268,7 @@ object TypeRep { case object StringRep extends TypeRep[String] { override def toString = "String" } - case object NoneRep extends TypeRep[Option[Nothing]] { + case object NoneRep extends TypeRep[None.type] { override def toString = "None" } case object NilRep extends TypeRep[Nil.type] { @@ -277,6 +282,10 @@ object TypeRep { } @serializable + case class ClassRep[A](elemRep: TypeRep[A]) extends TypeRep[Class[A]] { + override def toString = "Class[" + elemRep + "]" + } + @serializable case class SomeRep[A](elemRep: TypeRep[A]) extends TypeRep[Some[A]] { override def toString = "Some[" + elemRep + "]" } @@ -296,7 +305,7 @@ object TypeRep { } @serializable - case class Tuple2Rep[A1, A2](_1: TypeRep[A1], _2: TypeRep[A2]) extends TypeRep[Tuple2[A1, A2]] { + case class Tuple2Rep[A1, A2](_1: TypeRep[A1], _2: TypeRep[A2]) extends TypeRep[(A1, A2)] { override def toString = "Tuple2[" + _1 + ", " + _2 + "]" } @serializable |