diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/benchmarks/src/main/scala/scala/collection/immutable/ListBenchmark.scala | 72 | ||||
-rw-r--r-- | test/files/pos/t9331.scala | 6 | ||||
-rw-r--r-- | test/files/run/existentials-in-compiler.check | 68 | ||||
-rw-r--r-- | test/files/run/existentials-in-compiler.scala | 2 | ||||
-rw-r--r-- | test/files/run/repl-colon-type.check | 8 | ||||
-rw-r--r-- | test/files/run/t1459.check | 3 | ||||
-rw-r--r-- | test/files/run/t1459/InheritingPrinter.scala | 6 | ||||
-rw-r--r-- | test/files/run/t1459/JavaPrinter.java | 7 | ||||
-rw-r--r-- | test/files/run/t1459/ScalaPrinter.scala | 6 | ||||
-rw-r--r-- | test/files/run/t1459/Test.java | 15 | ||||
-rw-r--r-- | test/files/run/t1459/VarArg.java | 3 | ||||
-rw-r--r-- | test/files/run/t1459generic.check | 4 | ||||
-rw-r--r-- | test/files/run/t1459generic/Impl.scala | 4 | ||||
-rw-r--r-- | test/files/run/t1459generic/Test.java | 10 | ||||
-rw-r--r-- | test/files/run/t1459generic/VarargGeneric.java | 7 | ||||
-rw-r--r-- | test/files/run/t9013/Test.java | 20 | ||||
-rw-r--r-- | test/files/run/t9013/test.scala | 18 | ||||
-rw-r--r-- | test/files/run/t9114.scala | 31 | ||||
-rw-r--r-- | test/junit/scala/collection/IndexedSeqTest.scala | 578 |
19 files changed, 829 insertions, 39 deletions
diff --git a/test/benchmarks/src/main/scala/scala/collection/immutable/ListBenchmark.scala b/test/benchmarks/src/main/scala/scala/collection/immutable/ListBenchmark.scala new file mode 100644 index 0000000000..94844dcae2 --- /dev/null +++ b/test/benchmarks/src/main/scala/scala/collection/immutable/ListBenchmark.scala @@ -0,0 +1,72 @@ +package scala.collection.immutable + +import java.util.concurrent.TimeUnit + +import org.openjdk.jmh.annotations._ + +object ListBenchmark { + case class Content(value: Int) +} + +@BenchmarkMode(Array(Mode.AverageTime)) +@Fork(2) +@Threads(1) +@Warmup(iterations = 10) +@Measurement(iterations = 10) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@State(Scope.Benchmark) +class ListBenchmark { + import ListBenchmark._ + @Param(Array("0", "1", "10", "100", "1000")) + var size: Int = _ + + var values: List[Content] = _ + var mid: Content = _ + var last: Content = _ + + + @Setup(Level.Trial) def initKeys(): Unit = { + values = List.tabulate(size)(v => Content(v)) + mid = Content(size / 2) + last = Content(Math.max(0,size -1)) + } + + @Benchmark def filter_includeAll: Any = { + values.filter(v => true) + } + + @Benchmark def filter_excludeAll: Any = { + values.filter(_ => false) + } + + @Benchmark def filter_exc_mid: Any = { + values.filter(v => v.value != mid.value) + } + + @Benchmark def filter_from_mid: Any = { + values.filter(v => v.value <= mid.value) + } + + @Benchmark def filter_exc_last: Any = { + values.filter(v => v.value != last.value) + } + + @Benchmark def filter_only_last: Any = { + values.filter(v => v.value == last.value) + } + + @Setup(Level.Trial) def initKeys(): Unit = { + values = List.tabulate(size)(n => if (n == size / 2) "mid" else "") + } + + @Benchmark def mapConserve_identity: Any = { + values.mapConserve(x => x) + } + + @Benchmark def mapConserve_modifyAll: Any = { + values.mapConserve(x => "replace") + } + @Benchmark def mapConserve_modifyMid: Any = { + values.mapConserve(x => if (x == "mid") "replace" else x) + } +} diff --git a/test/files/pos/t9331.scala b/test/files/pos/t9331.scala new file mode 100644 index 0000000000..00a667886f --- /dev/null +++ b/test/files/pos/t9331.scala @@ -0,0 +1,6 @@ +import scala.language.higherKinds + +trait Proxy[+T] +case class Stuff[+P[PP] <: Proxy[PP]]() { + // canEqual was incorrectly synthetized and started reporting a kind error. +} diff --git a/test/files/run/existentials-in-compiler.check b/test/files/run/existentials-in-compiler.check index b0d852865d..8800df0823 100644 --- a/test/files/run/existentials-in-compiler.check +++ b/test/files/run/existentials-in-compiler.check @@ -2,28 +2,28 @@ abstract trait Bippy[A <: AnyRef, B] extends AnyRef extest.Bippy[_ <: AnyRef, _] abstract trait BippyBud[A <: AnyRef, B, C <: List[A]] extends AnyRef - extest.BippyBud[A,B,C] forSome { A <: AnyRef; B; C <: List[A] } + extest.BippyBud[?0,?1,?2] forSome { type ?0 <: AnyRef; type ?1; type ?2 <: List[?0] } abstract trait BippyLike[A <: AnyRef, B <: List[A], This <: extest.BippyLike[A,B,This] with extest.Bippy[A,B]] extends AnyRef - extest.BippyLike[A,B,This] forSome { A <: AnyRef; B <: List[A]; This <: extest.BippyLike[A,B,This] with extest.Bippy[A,B] } + extest.BippyLike[?0,?1,?2] forSome { type ?0 <: AnyRef; type ?1 <: List[?0]; type ?2 <: extest.BippyLike[?0,?1,?2] with extest.Bippy[?0,?1] } abstract trait Contra[-A >: AnyRef, -B] extends AnyRef - extest.Contra[AnyRef, _] + extest.Contra[_ >: AnyRef, _] abstract trait ContraLike[-A >: AnyRef, -B >: List[A]] extends AnyRef - extest.ContraLike[A,B] forSome { -A >: AnyRef; -B >: List[A] } + extest.ContraLike[?0,?1] forSome { type ?0 >: AnyRef; type ?1 >: List[?0] } abstract trait Cov01[+A <: AnyRef, +B] extends AnyRef - extest.Cov01[AnyRef,Any] + extest.Cov01[_ <: AnyRef, _] abstract trait Cov02[+A <: AnyRef, B] extends AnyRef - extest.Cov02[AnyRef, _] + extest.Cov02[_ <: AnyRef, _] abstract trait Cov03[+A <: AnyRef, -B] extends AnyRef - extest.Cov03[AnyRef, _] + extest.Cov03[_ <: AnyRef, _] abstract trait Cov04[A <: AnyRef, +B] extends AnyRef - extest.Cov04[_ <: AnyRef, Any] + extest.Cov04[_ <: AnyRef, _] abstract trait Cov05[A <: AnyRef, B] extends AnyRef extest.Cov05[_ <: AnyRef, _] @@ -32,7 +32,7 @@ abstract trait Cov06[A <: AnyRef, -B] extends AnyRef extest.Cov06[_ <: AnyRef, _] abstract trait Cov07[-A <: AnyRef, +B] extends AnyRef - extest.Cov07[_ <: AnyRef, Any] + extest.Cov07[_ <: AnyRef, _] abstract trait Cov08[-A <: AnyRef, B] extends AnyRef extest.Cov08[_ <: AnyRef, _] @@ -41,16 +41,16 @@ abstract trait Cov09[-A <: AnyRef, -B] extends AnyRef extest.Cov09[_ <: AnyRef, _] abstract trait Cov11[+A <: AnyRef, +B <: List[_]] extends AnyRef - extest.Cov11[AnyRef,List[_]] + extest.Cov11[_ <: AnyRef, _ <: List[_]] abstract trait Cov12[+A <: AnyRef, B <: List[_]] extends AnyRef - extest.Cov12[AnyRef, _ <: List[_]] + extest.Cov12[_ <: AnyRef, _ <: List[_]] abstract trait Cov13[+A <: AnyRef, -B <: List[_]] extends AnyRef - extest.Cov13[AnyRef, _ <: List[_]] + extest.Cov13[_ <: AnyRef, _ <: List[_]] abstract trait Cov14[A <: AnyRef, +B <: List[_]] extends AnyRef - extest.Cov14[_ <: AnyRef, List[_]] + extest.Cov14[_ <: AnyRef, _ <: List[_]] abstract trait Cov15[A <: AnyRef, B <: List[_]] extends AnyRef extest.Cov15[_ <: AnyRef, _ <: List[_]] @@ -59,7 +59,7 @@ abstract trait Cov16[A <: AnyRef, -B <: List[_]] extends AnyRef extest.Cov16[_ <: AnyRef, _ <: List[_]] abstract trait Cov17[-A <: AnyRef, +B <: List[_]] extends AnyRef - extest.Cov17[_ <: AnyRef, List[_]] + extest.Cov17[_ <: AnyRef, _ <: List[_]] abstract trait Cov18[-A <: AnyRef, B <: List[_]] extends AnyRef extest.Cov18[_ <: AnyRef, _ <: List[_]] @@ -68,16 +68,16 @@ abstract trait Cov19[-A <: AnyRef, -B <: List[_]] extends AnyRef extest.Cov19[_ <: AnyRef, _ <: List[_]] abstract trait Cov21[+A, +B] extends AnyRef - extest.Cov21[Any,Any] + extest.Cov21[_, _] abstract trait Cov22[+A, B] extends AnyRef - extest.Cov22[Any, _] + extest.Cov22[_, _] abstract trait Cov23[+A, -B] extends AnyRef - extest.Cov23[Any, _] + extest.Cov23[_, _] abstract trait Cov24[A, +B] extends AnyRef - extest.Cov24[_, Any] + extest.Cov24[_, _] abstract trait Cov25[A, B] extends AnyRef extest.Cov25[_, _] @@ -86,7 +86,7 @@ abstract trait Cov26[A, -B] extends AnyRef extest.Cov26[_, _] abstract trait Cov27[-A, +B] extends AnyRef - extest.Cov27[_, Any] + extest.Cov27[_, _] abstract trait Cov28[-A, B] extends AnyRef extest.Cov28[_, _] @@ -95,43 +95,43 @@ abstract trait Cov29[-A, -B] extends AnyRef extest.Cov29[_, _] abstract trait Cov31[+A, +B, C <: (A, B)] extends AnyRef - extest.Cov31[A,B,C] forSome { +A; +B; C <: (A, B) } + extest.Cov31[?0,?1,?2] forSome { type ?0; type ?1; type ?2 <: (?0, ?1) } abstract trait Cov32[+A, B, C <: (A, B)] extends AnyRef - extest.Cov32[A,B,C] forSome { +A; B; C <: (A, B) } + extest.Cov32[?0,?1,?2] forSome { type ?0; type ?1; type ?2 <: (?0, ?1) } abstract trait Cov33[+A, -B, C <: Tuple2[A, _]] extends AnyRef - extest.Cov33[A,B,C] forSome { +A; -B; C <: Tuple2[A, _] } + extest.Cov33[?0,?1,?2] forSome { type ?0; type ?1; type ?2 <: Tuple2[?0, _] } abstract trait Cov34[A, +B, C <: (A, B)] extends AnyRef - extest.Cov34[A,B,C] forSome { A; +B; C <: (A, B) } + extest.Cov34[?0,?1,?2] forSome { type ?0; type ?1; type ?2 <: (?0, ?1) } abstract trait Cov35[A, B, C <: (A, B)] extends AnyRef - extest.Cov35[A,B,C] forSome { A; B; C <: (A, B) } + extest.Cov35[?0,?1,?2] forSome { type ?0; type ?1; type ?2 <: (?0, ?1) } abstract trait Cov36[A, -B, C <: Tuple2[A, _]] extends AnyRef - extest.Cov36[A,B,C] forSome { A; -B; C <: Tuple2[A, _] } + extest.Cov36[?0,?1,?2] forSome { type ?0; type ?1; type ?2 <: Tuple2[?0, _] } abstract trait Cov37[-A, +B, C <: Tuple2[_, B]] extends AnyRef - extest.Cov37[A,B,C] forSome { -A; +B; C <: Tuple2[_, B] } + extest.Cov37[?0,?1,?2] forSome { type ?0; type ?1; type ?2 <: Tuple2[_, ?1] } abstract trait Cov38[-A, B, C <: Tuple2[_, B]] extends AnyRef - extest.Cov38[A,B,C] forSome { -A; B; C <: Tuple2[_, B] } + extest.Cov38[?0,?1,?2] forSome { type ?0; type ?1; type ?2 <: Tuple2[_, ?1] } abstract trait Cov39[-A, -B, C <: Tuple2[_, _]] extends AnyRef extest.Cov39[_, _, _ <: Tuple2[_, _]] abstract trait Cov41[+A >: Null, +B] extends AnyRef - extest.Cov41[Any,Any] + extest.Cov41[_ >: Null, _] abstract trait Cov42[+A >: Null, B] extends AnyRef - extest.Cov42[Any, _] + extest.Cov42[_ >: Null, _] abstract trait Cov43[+A >: Null, -B] extends AnyRef - extest.Cov43[Any, _] + extest.Cov43[_ >: Null, _] abstract trait Cov44[A >: Null, +B] extends AnyRef - extest.Cov44[_ >: Null, Any] + extest.Cov44[_ >: Null, _] abstract trait Cov45[A >: Null, B] extends AnyRef extest.Cov45[_ >: Null, _] @@ -140,7 +140,7 @@ abstract trait Cov46[A >: Null, -B] extends AnyRef extest.Cov46[_ >: Null, _] abstract trait Cov47[-A >: Null, +B] extends AnyRef - extest.Cov47[_ >: Null, Any] + extest.Cov47[_ >: Null, _] abstract trait Cov48[-A >: Null, B] extends AnyRef extest.Cov48[_ >: Null, _] @@ -149,8 +149,8 @@ abstract trait Cov49[-A >: Null, -B] extends AnyRef extest.Cov49[_ >: Null, _] abstract trait Covariant[+A <: AnyRef, +B] extends AnyRef - extest.Covariant[AnyRef,Any] + extest.Covariant[_ <: AnyRef, _] abstract trait CovariantLike[+A <: AnyRef, +B <: List[A], +This <: extest.CovariantLike[A,B,This] with extest.Covariant[A,B]] extends AnyRef - extest.CovariantLike[A,B,This] forSome { +A <: AnyRef; +B <: List[A]; +This <: extest.CovariantLike[A,B,This] with extest.Covariant[A,B] } + extest.CovariantLike[?0,?1,?2] forSome { type ?0 <: AnyRef; type ?1 <: List[?0]; type ?2 <: extest.CovariantLike[?0,?1,?2] with extest.Covariant[?0,?1] } diff --git a/test/files/run/existentials-in-compiler.scala b/test/files/run/existentials-in-compiler.scala index e516eddf95..e35b7231c2 100644 --- a/test/files/run/existentials-in-compiler.scala +++ b/test/files/run/existentials-in-compiler.scala @@ -79,7 +79,7 @@ package extest { exitingTyper { clazz.info println(clazz.defString) - println(" " + classExistentialType(clazz) + "\n") + println(" " + classExistentialType(clazz.owner.typeOfThis, clazz) + "\n") } } } diff --git a/test/files/run/repl-colon-type.check b/test/files/run/repl-colon-type.check index 1217e8d8c2..5b7a3c7506 100644 --- a/test/files/run/repl-colon-type.check +++ b/test/files/run/repl-colon-type.check @@ -75,7 +75,7 @@ TypeRef( ) TypeRef( TypeSymbol( - sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with Product with GenericTraversableTemplate[A,List] with LinearSeqOptimized[A,List[A]] with Serializable + sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with Product with GenericTraversableTemplate[A,List] with LinearSeqOptimized[A,List[A]] with FilteredTraversableInternal[A,List[A]] with Serializable ) args = List( @@ -142,7 +142,7 @@ TypeRef( args = List( TypeRef( TypeSymbol( - sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with Product with GenericTraversableTemplate[A,List] with LinearSeqOptimized[A,List[A]] with Serializable + sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with Product with GenericTraversableTemplate[A,List] with LinearSeqOptimized[A,List[A]] with FilteredTraversableInternal[A,List[A]] with Serializable ) args = List( @@ -175,7 +175,7 @@ PolyType( args = List( TypeRef( TypeSymbol( - sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with Product with GenericTraversableTemplate[A,List] with LinearSeqOptimized[A,List[A]] with Serializable + sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with Product with GenericTraversableTemplate[A,List] with LinearSeqOptimized[A,List[A]] with FilteredTraversableInternal[A,List[A]] with Serializable ) args = List(TypeParamTypeRef(TypeParam(T <: AnyVal))) @@ -198,7 +198,7 @@ PolyType( params = List(TermSymbol(x: T), TermSymbol(y: List[U])) resultType = TypeRef( TypeSymbol( - sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with Product with GenericTraversableTemplate[A,List] with LinearSeqOptimized[A,List[A]] with Serializable + sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with Product with GenericTraversableTemplate[A,List] with LinearSeqOptimized[A,List[A]] with FilteredTraversableInternal[A,List[A]] with Serializable ) args = List(TypeParamTypeRef(TypeParam(U >: T))) diff --git a/test/files/run/t1459.check b/test/files/run/t1459.check new file mode 100644 index 0000000000..93b4c5a810 --- /dev/null +++ b/test/files/run/t1459.check @@ -0,0 +1,3 @@ +JavaPrinter: one two three +InheritingPrinter extends JavaPrinter: one two three +ScalaPrinter: onetwothree diff --git a/test/files/run/t1459/InheritingPrinter.scala b/test/files/run/t1459/InheritingPrinter.scala new file mode 100644 index 0000000000..70301307f5 --- /dev/null +++ b/test/files/run/t1459/InheritingPrinter.scala @@ -0,0 +1,6 @@ +class InheritingPrinter extends JavaPrinter { + override def doit(s: String*) { + print("InheritingPrinter extends ") + super.doit(s: _*); + } +}
\ No newline at end of file diff --git a/test/files/run/t1459/JavaPrinter.java b/test/files/run/t1459/JavaPrinter.java new file mode 100644 index 0000000000..3912ea613a --- /dev/null +++ b/test/files/run/t1459/JavaPrinter.java @@ -0,0 +1,7 @@ +public class JavaPrinter implements VarArg { + public void doit(String... s) { + System.out.print("JavaPrinter: "); + for(String str : s) + System.out.print(str + " "); + } +} diff --git a/test/files/run/t1459/ScalaPrinter.scala b/test/files/run/t1459/ScalaPrinter.scala new file mode 100644 index 0000000000..46305804c2 --- /dev/null +++ b/test/files/run/t1459/ScalaPrinter.scala @@ -0,0 +1,6 @@ +class ScalaPrinter extends VarArg { + override def doit(s: String*) = { + print("ScalaPrinter: ") + s.foreach(print _) + } +}
\ No newline at end of file diff --git a/test/files/run/t1459/Test.java b/test/files/run/t1459/Test.java new file mode 100644 index 0000000000..3cf91e2e8b --- /dev/null +++ b/test/files/run/t1459/Test.java @@ -0,0 +1,15 @@ +public class Test { + public static void main(String[] args) { + VarArg jp = new JavaPrinter(); + VarArg ip = new InheritingPrinter(); + VarArg sp = new ScalaPrinter(); + doYourThing(jp); + doYourThing(ip); + doYourThing(sp); + } + + public static void doYourThing(VarArg va) { + va.doit("one", "two", "three"); + System.out.println(); + } +} diff --git a/test/files/run/t1459/VarArg.java b/test/files/run/t1459/VarArg.java new file mode 100644 index 0000000000..7039f99e7b --- /dev/null +++ b/test/files/run/t1459/VarArg.java @@ -0,0 +1,3 @@ +public interface VarArg { + void doit(String... s); +} diff --git a/test/files/run/t1459generic.check b/test/files/run/t1459generic.check new file mode 100644 index 0000000000..346fadbc0d --- /dev/null +++ b/test/files/run/t1459generic.check @@ -0,0 +1,4 @@ +Note: t1459generic/Test.java uses unchecked or unsafe operations. +Note: Recompile with -Xlint:unchecked for details. +ab +ab diff --git a/test/files/run/t1459generic/Impl.scala b/test/files/run/t1459generic/Impl.scala new file mode 100644 index 0000000000..9234e70456 --- /dev/null +++ b/test/files/run/t1459generic/Impl.scala @@ -0,0 +1,4 @@ +class Impl extends VarargGeneric[String] { + def genericOne(x: String, arg: String): String = x + arg + def genericVar(x: String, args: String*): String = x + args.head +} diff --git a/test/files/run/t1459generic/Test.java b/test/files/run/t1459generic/Test.java new file mode 100644 index 0000000000..a97158796b --- /dev/null +++ b/test/files/run/t1459generic/Test.java @@ -0,0 +1,10 @@ +public class Test { + public static void main(String[] args) throws Exception { + VarargGeneric vg = new Impl(); + System.out.println(vg.genericOne("a", "b")); + System.out.println(vg.genericVar("a", "b")); + // should not result in java.lang.AbstractMethodError: Impl.genericVar(Ljava/lang/Object;[Ljava/lang/String;)Ljava/lang/String; + // --> genericVar needs a varargs bridge (scala -> java varargs) and a standard generics bridge + // (for comparison, including genericOne, which needs only a generics bridge) + } +} diff --git a/test/files/run/t1459generic/VarargGeneric.java b/test/files/run/t1459generic/VarargGeneric.java new file mode 100644 index 0000000000..9b37a0fe3f --- /dev/null +++ b/test/files/run/t1459generic/VarargGeneric.java @@ -0,0 +1,7 @@ +public interface VarargGeneric<T> { + String genericOne(T x, String args); + // we cannot annotate this with @SafeVarargs, because + // it's in an interface. so that's why a warning from + // javac appears in the checkfile. + String genericVar(T x, String... args); +} diff --git a/test/files/run/t9013/Test.java b/test/files/run/t9013/Test.java new file mode 100644 index 0000000000..14152b16ac --- /dev/null +++ b/test/files/run/t9013/Test.java @@ -0,0 +1,20 @@ +import java.util.Comparator; + +public class Test { + public static void main(String[] args) { + ClassImplementsClass c = new ClassImplementsClass(); + + c.x("a", "b", "c"); + c.y("a", "b", "c"); + c.z("a", "b", "c"); + + VarargAbstractClass i = new ClassImplementsClass(); + + i.x("a", "b", "c"); + i.y("a", "b", "c"); + // System.out.println(i.z("a", "b", "c")); // still incurs a LinkageError. + // Perhaps due to Uncurry: + // > for every repeated Java parameter `x: T...' --> x: Array[T], except: + // > if T is an unbounded abstract type, replace --> x: Array[Object] + } +} diff --git a/test/files/run/t9013/test.scala b/test/files/run/t9013/test.scala new file mode 100644 index 0000000000..073f8d086f --- /dev/null +++ b/test/files/run/t9013/test.scala @@ -0,0 +1,18 @@ +import scala.annotation.varargs + +abstract class VarargAbstractClass[T] { + @varargs + def x(els: String*): Int + + @varargs + def y(els: String*): Int + + @varargs + def z(els: T*): Int +} +class ClassImplementsClass extends VarargAbstractClass[String] { + + override def x(els: String*): Int = els.length + override def y(els: String*): Int = els.length + override def z(els: String*): Int = els.length +} diff --git a/test/files/run/t9114.scala b/test/files/run/t9114.scala new file mode 100644 index 0000000000..656a5c7d8d --- /dev/null +++ b/test/files/run/t9114.scala @@ -0,0 +1,31 @@ +import annotation.unchecked + +class Test { + trait Two[A, B] + type One[A] = Two[A,A] + class View extends One[Any] + + def checkAny(x: Some[One[Any]]) = x match { // okay + case Some(_: View) => true + case _ => false + } + def checkAbstract[A](x: Some[One[A]]) = x match { // okay + case Some(_: View) => true + case _ => false + } + + def checkExistential(x: Some[One[_]]) = x match { + case Some(_: View) => true // compiler crash + case _ => false + } +} + +object Test { + def main(args: Array[String]): Unit = { + val t1 = new Test + val t2 = new Test + assert(t1.checkAny(Some(new t1.View))) + assert(t1.checkAbstract(Some(new t1.View))) + assert(t1.checkExistential(Some(new t1.View))) + } +} diff --git a/test/junit/scala/collection/IndexedSeqTest.scala b/test/junit/scala/collection/IndexedSeqTest.scala new file mode 100644 index 0000000000..a33849e60b --- /dev/null +++ b/test/junit/scala/collection/IndexedSeqTest.scala @@ -0,0 +1,578 @@ +package scala.collection + +import org.junit.Test +import org.junit.Ignore +import org.junit.Assert.{assertEquals, _} +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +// with the Ant JUnit runner, it's necessary to @Ignore the abstract +// classes here, or JUnit tries to instantiate them. the annotations +// can be removed when this is merged forward (TODO 2.12.x) + +/** + * base class for testing common methods on a various implementations + * + * @tparam T the collection type + * @tparam E the element type + */ +@RunWith(classOf[JUnit4]) +@Ignore +abstract class IndexedTest[T, E] { + + protected def size = 10 + + /** + * create a new instance of the test data with known values + */ + protected def underTest(size: Int): T + + /** + * returns the value of the data that is expected to be present int the original data at index {{{index}}} + * This is conceptully the same as the normal apply operation but unavaialbe due to the base types of T not supporting apply + * + * @param index the index to use for the returned value + * @return the value at the specified index + */ + protected def expectedValueAtIndex(index: Int): E + /** + * check some simple indexed access + */ + @Test def checkIndexedAccess: Unit = { + val test = underTest(size) + for (i <- 0 until size) { + assertEquals(s" at index $i", expectedValueAtIndex(i), get(test, i)) + } + } + + /** + * check simple equallity of the initial data. + * More a test of the infra that we use in this est than a full test of equallity + */ + @Test def checkEquals: Unit = { + val test1 = underTest(size) + val test2 = underTest(size) + doAssertEquals("", test1, test2) + assertNotSame(test1, test2) + expectSameContent("basic equallity", false, test1, test2, 0, size) + } + + protected def expectSameContent(txt: String, canBeSame:Boolean, orig: T, test: T, offset: Int, len: Int): Unit = { + val txtAndState = s"$txt canBeSame $canBeSame isMutableContent $isMutableContent isTakeAllSame $isTakeAllSame offset $offset len $len length(test) ${length(test)}" + val isValidSame = canBeSame && !isMutableContent && offset == 0 && len == size + if (isValidSame && isTakeAllSame) + assertSame(txtAndState, orig, test) + else + assertNotSame(txtAndState, orig, test) + assertSame(txtAndState, len, length(test)) + for (i <- 0 until len) { + assertEquals(s" $txtAndState $i $offset $len", expectedValueAtIndex(i + offset), get(test, i)) + } + } + + /** + * check the operation of {{{take}}} when the parameter is less than the size of the test data + */ + @Test def checkTakeNormal: Unit = { + val orig = underTest(size) + for (len <- 0 until size) { + val taken = take(orig, len) + expectSameContent(s" len $len", true, orig, taken, 0, len) + } + } + + /** + * check the operation of {{{slice}}} within the bounds of the source + */ + @Test def checkSliceNormal: Unit = { + val orig = underTest(size) + for ( + from <- 0 until size; + to <- from until size) { + + val sliced = slice(orig, from, to) + expectSameContent(s"from $from, to $to", true, orig, sliced, from, to - from) + } + } + + /** + * check the operation of {{{take}}} works for size of 0 + * There is a special case tha for some implementations empty will be a singleton + */ + @Test def checkTakeEmpty: Unit = { + val orig = underTest(size) + val empty1 = take(orig, 0) + val empty2 = take(orig, 0) + assertEquals(0, length(empty1)) + if (isEmptyConstant) assertSame(empty1, empty2) + } + + /** + * check the operation of {{{slice}}} works for size of 0 + * There is a special case tha for some implementations empty will be a singleton + */ + @Test def checkSliceEmpty: Unit = { + val orig = underTest(size) + for (start <- 0 until size) { + val empty1 = slice(orig, start, start) + val empty2 = slice(orig, start, start) + assertEquals(s"start $start", 0, length(empty1)) + if (isEmptyConstant) assertSame(s"start $start", empty1, empty2) + } + } + + /** + * check the operation of {{{take}}} works for the entire content + * There is a special case that for some immutable implementations they can share the result + */ + @Test def checkTakeAll: Unit = { + val orig = underTest(size) + val all = take(orig, size) + assertEquals(size, length(all)) + expectSameContent("take all", true, orig, all, 0, size) + if (isMutableContent) + assertNotSame(orig, all) + else if (isTakeAllSame) + assertSame(orig, all) + } + + /** + * check the operation of {{{slice}}} works for the entire content + * There is a special case that for some immutable implementations they can share the result + */ + @Test def checkSliceAll: Unit = { + val orig = underTest(size) + val all = slice(orig, 0, size) + assertEquals(size, length(all)) + expectSameContent("", true, orig, all, 0, size) + if (isMutableContent) + assertNotSame(orig, all) + else if (isTakeAllSame) + assertSame(orig, all) + } + + /** + * check that take operates appropriately for negative values + * take and slice should be lenient and silently ignore any data outside valid ranges + */ + @Test def checkTakeNeg: Unit = { + val orig = underTest(size) + val e = take(orig, 0) + for (len <- List(-1, -10, -99, Int.MinValue)) { + val empty = take(orig, len) + assertEquals(s"len $len", 0, length(empty)) + if (isEmptyConstant) assertSame(s"len $len", e, empty) + } + } + + /** + * check that take operates appropriately for lengths that exceed the input size + * take and slice should be lenient and silently ignore any data outside valid ranges + */ + @Test def checkTakeTooBig: Unit = { + val orig = underTest(size) + val e = take(orig, 0) + for (len <- List(size + 1, size + 10, Int.MaxValue)) { + val all = take(orig, len) + assertEquals(s"len $len", size, length(all)) + expectSameContent("", true, orig, all, 0, size) + } + } + + /** + * check that slice operates appropriately for negative start point + * take and slice should be lenient and silently ignore any data outside valid ranges + */ + @Test def checkSliceFromNeg: Unit = { + val orig = underTest(size) + for ( + from <- List(-1, -10, -99, Int.MinValue); + to <- List(-1, 0, 1, 5)) { + val start = slice(orig, from, to) + expectSameContent(s"from $from, to $to", true, orig, start, 0, Math.max(0, to)) + } + } + + /** + * check that slice operates appropriately for out of range end values + * take and slice should be lenient and silently ignore any data outside valid ranges + */ + @Test def checkSliceToTooBig: Unit = { + val orig = underTest(size) + for ( + from <- List(-1, -10, -99, Int.MinValue, 0, 1, 5); + to <- List(size + 1, size + 10, Int.MaxValue)) { + val start = slice(orig, from, to) + val realStart = Math.max(0, from) + val realLen = size - realStart + expectSameContent(s"from $from, to $to", true, orig, start, realStart, realLen) + } + } + + /** + * check that slice operates appropriately for negative values start and ends too large + * take and slice should be lenient and silently ignore any data outside valid ranges + */ + @Test def checkSliceFromNegAndToTooBig: Unit = { + val orig = underTest(size) + for ( + from <- List(-1, -10, -99, Int.MinValue); + to <- List(size + 1, size + 10, Int.MaxValue)) { + val all = slice(orig, from, to) + expectSameContent(s"from $from, to $to", true, orig, all, 0, size) + } + } + + protected def intercept[EX <: Exception : Manifest](fn: => Any) { + try { + val res = fn + fail(s"expected exception was not thrown: $res") + } catch { + case failed: AssertionError => throw failed + case e: Exception if manifest[EX].runtimeClass.isAssignableFrom(e.getClass) => + } + } + + //accessors + //the length of underTest + def length(underTest: T): Int + + //the value at index i of underTest + def get(underTest: T, i: Int): E + + def slice(underTest: T, from: Int, to: Int): T + + def take(underTest: T, size: Int): T + + //behaviour + /** is an empty value the same JVM instance */ + def isEmptyConstant: Boolean + + /** is a take / slice that results in all the data returned return this + * This is only relevant if !isMutableContent + */ + def isTakeAllSame: Boolean + + /** is the content of the collection mutable. + * If mutable there is not data sharing allowed by take/slice, if immutable then data sharing is possible + * and tested based on isTakeAllSame + */ + def isMutableContent: Boolean + + //helpers + //delegate equals check for support arrays + def doAssertEquals(txt: String, expected: T, actual: T) + +} +package IndexedTestImpl { + import java.lang.reflect.{Array => jlArray} + import java.lang.{Boolean => jlBoolean} + import java.lang.{Byte => jlByte} + import java.lang.{Short => jlShort} + import java.lang.{Integer => jlInt} + import java.lang.{Long => jlLong} + import java.lang.{Float => jlFloat} + import java.lang.{Double => jlDouble} + import java.lang.{Character => jlChar} + + import scala.collection.immutable.{StringLike, StringOps, WrappedString} + import scala.collection.mutable + import scala.runtime.BoxedUnit + trait DataProvider[E] { + protected def expectedValueAtIndex(index: Int): E = { + val someNumber = index + jlInt.bitCount(index) + toType(someNumber) + } + + protected def toType(n: Int): E + } + trait StringTestData extends DataProvider [String] { + def toType(n: Int) = n.toString + } + trait ByteTestData extends DataProvider [Byte] { + def toType(n: Int) = n.toByte + } + trait ShortTestData extends DataProvider [Short] { + def toType(n: Int) = n.toShort + } + trait IntTestData extends DataProvider [Int] { + def toType(n: Int) = n + } + trait LongTestData extends DataProvider [Long] { + def toType(n: Int) = n + } + trait FloatTestData extends DataProvider [Float] { + def toType(n: Int) = n + } + trait DoubleTestData extends DataProvider [Double] { + def toType(n: Int) = n + } + trait CharTestData extends DataProvider [Char] { + def toType(n: Int)= (n+64).toChar + } + trait BooleanTestData extends DataProvider [Boolean] { + def toType(n: Int)= (n & 0) == 0 + } + trait UnitTestData extends DataProvider [BoxedUnit] { + def toType(n: Int)= if ((n & 0) == 0) null else BoxedUnit.UNIT + } + + @Ignore + abstract class ArrayTest[E] ( + //the object or primitive type of the array + val TYPE: Class[_]) extends IndexedTest[Array[E], E]{ + override final def length(underTest: Array[E]) = underTest.length + + override def get(underTest: Array[E], i: Int) = underTest(i) + + override def slice(underTest: Array[E], from: Int, to: Int) = underTest.slice(from, to) + + override def take(underTest: Array[E], size: Int) = underTest.take(size) + + override def isEmptyConstant = false + + override def isMutableContent = true + + override def isTakeAllSame = false + + override def doAssertEquals(txt: String, expected: Array[E], actual: Array[E]): Unit = { + assertEquals(txt, expected.mkString("'"), actual.mkString("'")) + } + + override def underTest(size: Int): Array[E] = { + val res = jlArray.newInstance(TYPE, size) + for (i <- 0 until size) { + jlArray.set(res, i, expectedValueAtIndex(i)) + } + res.asInstanceOf[Array[E]] + } + } + + + @Ignore + abstract class WrappedArrayTest[E]( + //the object or primitive type of the array + val TYPE: Class[_]) extends IndexedTest[mutable.WrappedArray[E], E] with DataProvider[E]{ + import mutable.WrappedArray + override final def length(underTest: WrappedArray[E]) = underTest.length + + override def get(underTest: WrappedArray[E], i: Int) = underTest(i) + + override def slice(underTest: WrappedArray[E], from: Int, to: Int) = underTest.slice(from, to) + + override def take(underTest: WrappedArray[E], size: Int) = underTest.take(size) + + override def isEmptyConstant = false + + override def isMutableContent = true + + override def isTakeAllSame = false + + override def doAssertEquals(txt: String, expected: WrappedArray[E], actual: WrappedArray[E]): Unit = { + assertEquals(txt, expected.mkString("'"), actual.mkString("'")) + } + + override def underTest(size: Int): WrappedArray[E] = { + val res = jlArray.newInstance(TYPE, size) + for (i <- 0 until size) { + jlArray.set(res, i, expectedValueAtIndex(i)) + } + WrappedArray.make(res.asInstanceOf[Array[E]]) + } + } + + //construct the data using java as much as possible to avoid invalidating the test + + @Ignore + abstract class MutableIndexedSeqTest[T <: mutable.Seq[E], E] extends IndexedTest[T, E] with DataProvider[E]{ + override final def length(underTest: T) = underTest.length + + override def get(underTest: T, i: Int) = underTest(i) + + override def slice(underTest: T, from: Int, to: Int) = underTest.slice(from, to).asInstanceOf[T] + + override def take(underTest: T, size: Int) = underTest.take(size).asInstanceOf[T] + + override def isEmptyConstant = false + + override def isMutableContent = true + + override def isTakeAllSame = true + + override def doAssertEquals(txt: String, expected: T, actual: T): Unit = { + assertEquals(txt, expected, actual) + } + + def createEmpty(size: Int) : T + + override protected def underTest(size: Int): T = { + val res:T = createEmpty(size) + for (i <- 0 until size) + res(i) = expectedValueAtIndex(i) + res + } + + } + @Ignore + abstract class ImmutableIndexedSeqTest[T <: SeqLike[E, T], E] extends IndexedTest[T, E] with DataProvider[E] { + override final def length(underTest: T) = underTest.length + + override def get(underTest: T, i: Int) = underTest(i) + + override def slice(underTest: T, from: Int, to: Int) = underTest.slice(from, to) + + override def take(underTest: T, size: Int) = underTest.take(size) + + override def isEmptyConstant = false + + override def isMutableContent = false + + override def isTakeAllSame = true + + override def doAssertEquals(txt: String, expected: T, actual: T): Unit = { + assertEquals(txt, expected, actual) + } + + } + @Ignore + abstract class StringOpsBaseTest extends IndexedTest[StringOps, Char] with DataProvider[Char] { + override final def length(underTest: StringOps) = underTest.length + + override def get(underTest: StringOps, i: Int) = underTest(i) + + override def slice(underTest: StringOps, from: Int, to: Int) = underTest.slice(from, to) + + override def take(underTest: StringOps, size: Int) = underTest.take(size) + + override def isEmptyConstant = false + + override def isMutableContent = false + + override def isTakeAllSame = false + + override def doAssertEquals(txt: String, expected: StringOps, actual: StringOps): Unit = { + assertEquals(txt, expected, actual) + } + + } + + class BooleanArrayTest extends ArrayTest[Boolean](jlBoolean.TYPE) with BooleanTestData + class ByteArrayTest extends ArrayTest[Byte](jlByte.TYPE) with ByteTestData + class ShortArrayTest extends ArrayTest[Short](jlShort.TYPE) with ShortTestData + class IntArrayTest extends ArrayTest[Int](jlInt.TYPE) with IntTestData + class LongArrayTest extends ArrayTest[Long](jlLong.TYPE) with LongTestData + class DoubleArrayTest extends ArrayTest[Double](jlDouble.TYPE) with DoubleTestData + class FloatArrayTest extends ArrayTest[Float](jlFloat.TYPE) with FloatTestData + class CharArrayTest extends ArrayTest[Char](jlChar.TYPE) with CharTestData + class UnitArrayTest extends ArrayTest[BoxedUnit](null) with UnitTestData { + override def underTest(size: Int): Array[BoxedUnit] = { + val res = new Array[Unit](size) + for (i <- 0 until size) { + jlArray.set(res, i, expectedValueAtIndex(i)) + } + res.asInstanceOf[Array[BoxedUnit]] + } + } + class RefArrayTest extends ArrayTest[String](classOf[String]) with StringTestData + + class BooleanWrappedArrayTest extends WrappedArrayTest[Boolean](jlBoolean.TYPE) with BooleanTestData + class ByteWrappedArrayTest extends WrappedArrayTest[Byte](jlByte.TYPE) with ByteTestData + class ShortWrappedArrayTest extends WrappedArrayTest[Short](jlShort.TYPE) with ShortTestData + class IntWrappedArrayTest extends WrappedArrayTest[Int](jlInt.TYPE) with IntTestData + class LongWrappedArrayTest extends WrappedArrayTest[Long](jlLong.TYPE) with LongTestData + class DoubleWrappedArrayTest extends WrappedArrayTest[Double](jlDouble.TYPE) with DoubleTestData + class FloatWrappedArrayTest extends WrappedArrayTest[Float](jlFloat.TYPE) with FloatTestData + class CharWrappedArrayTest extends WrappedArrayTest[Char](jlChar.TYPE) with CharTestData + class UnitWrappedArrayTest extends WrappedArrayTest[BoxedUnit](null) with UnitTestData { + import mutable.WrappedArray + override def underTest(size: Int): WrappedArray[BoxedUnit] = { + val res = new Array[Unit](size) + for (i <- 0 until size) { + jlArray.set(res, i, expectedValueAtIndex(i)) + } + WrappedArray.make(res.asInstanceOf[Array[Unit]]) + } + } + class RefWrappedArrayTest extends WrappedArrayTest[String](classOf[String]) with StringTestData + + class ListBufferTest extends MutableIndexedSeqTest[mutable.ListBuffer[String], String] with StringTestData { + import mutable.ListBuffer + override def createEmpty(size: Int): ListBuffer[String] = { + val res = new ListBuffer[String] + for (i <- 0 until size) + res += expectedValueAtIndex(i) + res + } + } + class ArraySeqTest extends MutableIndexedSeqTest[mutable.ArraySeq[String], String] with StringTestData { + import mutable.ArraySeq + override def createEmpty(size: Int): ArraySeq[String] = { + val res = new ArraySeq[String](size) + for (i <- 0 until size) + res (i) = expectedValueAtIndex(i) + res + } + } + class ArrayBufferTest extends MutableIndexedSeqTest[mutable.ArrayBuffer[String], String] with StringTestData { + import mutable.ArrayBuffer + override def createEmpty(size: Int): ArrayBuffer[String] = { + val res = new ArrayBuffer[String](size) + for (i <- 0 until size) + res += expectedValueAtIndex(i) + res + } + } + class ListTest extends ImmutableIndexedSeqTest[List[String], String] with StringTestData { + + override protected def underTest(size: Int): List[String] = { + var res:List[String] = Nil + var index = size-1 + while (index >= 0) { + res = expectedValueAtIndex(index) :: res + index -= 1 + } + res + } + } + class StringBuilderTest extends MutableIndexedSeqTest[StringBuilder, Char] with CharTestData { + + override def createEmpty(size: Int): StringBuilder = new StringBuilder(size) + + override protected def underTest(size: Int): StringBuilder = { + var res = createEmpty(size) + for (i <- 0 until size) + res += expectedValueAtIndex(i) + res + } + } + class StringOpsTest extends StringOpsBaseTest with CharTestData { + + override protected def underTest(size: Int): StringOps = { + var res = new StringBuilder(size) + for (i <- 0 until size) + res += expectedValueAtIndex(i) + res.toString + } + } + class WrappedStringTest extends ImmutableIndexedSeqTest[WrappedString, Char] with CharTestData { + + override def isTakeAllSame: Boolean = false + + override protected def underTest(size: Int): WrappedString = { + var res = new StringBuilder(size) + for (i <- 0 until size) + res += expectedValueAtIndex(i) + new WrappedString(res.toString) + } + } + class VectorTest extends ImmutableIndexedSeqTest[Vector[String], String] with StringTestData { + + override protected def underTest(size: Int): Vector[String] = { + var res = Vector.newBuilder[String] + for (i <- 0 until size) + res += expectedValueAtIndex(i) + res.result() + } + } + +} |