From 52ccdc5627e3d3d34019b57560601a7e3ec89483 Mon Sep 17 00:00:00 2001 From: michelou Date: Mon, 18 Jun 2007 09:19:21 +0000 Subject: added tests for scala.Iterable --- test/files/run/collections.scala | 86 ++++++++++++++++++++-------------------- test/files/run/iterables.check | 2 + test/files/run/iterables.scala | 14 +++++++ 3 files changed, 59 insertions(+), 43 deletions(-) create mode 100644 test/files/run/iterables.check create mode 100644 test/files/run/iterables.scala (limited to 'test/files') diff --git a/test/files/run/collections.scala b/test/files/run/collections.scala index 3f3bfb6b06..7c723c9fc7 100644 --- a/test/files/run/collections.scala +++ b/test/files/run/collections.scala @@ -5,113 +5,113 @@ object Test extends Application { val printTime = false - def sum[A](xs: Iterable[int]) = (0 /: xs)((x, y) => x + y) + def sum[A](xs: Iterable[Int]) = (0 /: xs)((x, y) => x + y) - def time(op: => unit): unit = { + def time(op: => Unit) { val start = currentTime op - if (printTime) Console.println(" time = "+(currentTime - start)+"ms") + if (printTime) println(" time = "+(currentTime - start)+"ms") } - def test(msg: String, s0: collection.immutable.Set[int], iters: int) = { - Console.println("***** "+msg+":") + def test(msg: String, s0: collection.immutable.Set[Int], iters: Int) = { + println("***** "+msg+":") var s = s0 s = s + 2 s = s + (3, 4000, 10000) - Console.println("test1: "+sum(s)) + println("test1: "+sum(s)) time { s = s ++ (List.range(0, iters) map (2*)) - Console.println("test2: "+sum(s)+", iters = "+iters) + println("test2: "+sum(s)+", iters = "+iters) } time { var x = 0 - for (val i <- (0 to 10000)) - if (s contains i) x = x + i - Console.println("test3: "+x) + for (i <- 0 to 10000) + if (s contains i) x += i + println("test3: "+x) } } - def test(msg: String, s0: collection.mutable.Set[int], iters: int) = { - Console.println("***** "+msg+":") + def test(msg: String, s0: collection.mutable.Set[Int], iters: Int) = { + println("***** "+msg+":") var s = s0 s = s + 2 s = s + (3, 4000, 10000) - Console.println("test1: "+sum(s)) + println("test1: "+sum(s)) time { s = s ++ (List.range(0, iters) map (2*)) - Console.println("test2: "+sum(s)+", iters = "+iters) + println("test2: "+sum(s)+", iters = "+iters) } time { var x = 0 - for (val i <- (0 to 10000)) - if (s contains i) x = x + i - Console.println("test3: "+x) + for (i <- 0 to 10000) + if (s contains i) x += i + println("test3: "+x) } } - def test(msg: String, s0: collection.immutable.Map[int, int], iters: int) = { - Console.println("***** "+msg+":") + def test(msg: String, s0: collection.immutable.Map[Int, Int], iters: Int) = { + println("***** "+msg+":") var s = s0 s = s + (2 -> 2) s = s + (3 -> 3, 4000 -> 4000, 10000 -> 10000) - Console.println("test1: "+sum(s map (_._2))) + println("test1: "+sum(s map (_._2))) time { s = s ++ (List.range(0, iters) map (x => x * 2 -> x * 2)) - Console.println("test2: "+sum(s map (_._2))+", iters = "+iters) + println("test2: "+sum(s map (_._2))+", iters = "+iters) } time { var x = 0 - for (val i <- (0 to 10000)) + for (i <- 0 to 10000) s get i match { - case Some(i) => x = x + i + case Some(i) => x += i case None => } - Console.println("test3: "+x) + println("test3: "+x) } if (iters == 5000) { time { var s1 = s var x = 0 - for (val i <- (0 to 10000)) { + for (i <- 0 to 10000) { s get i match { - case Some(i) => x = x + i + case Some(i) => x += i case None => } s1 = s1 + ((i + 10000) -> i) } - Console.println("test4: "+x) + println("test4: "+x) } } } - def test(msg: String, s0: collection.mutable.Map[int, int], iters: int) = { - Console.println("***** "+msg+":") + def test(msg: String, s0: collection.mutable.Map[Int, Int], iters: Int) = { + println("***** "+msg+":") var s = s0 s = s + (2 -> 2) s = s + (3 -> 3, 4000 -> 4000, 10000 -> 10000) - Console.println("test1: "+sum(s map (_._2))) + println("test1: "+sum(s map (_._2))) time { s = s ++ (List.range(0, iters) map (x => x * 2 -> x * 2)) - Console.println("test2: "+sum(s map (_._2))+", iters = "+iters) + println("test2: "+sum(s map (_._2))+", iters = "+iters) } time { var x = 0 - for (val i <- (0 to 10000)) + for (i <- 0 to 10000) s get i match { - case Some(i) => x = x + i + case Some(i) => x += i case None => } - Console.println("test3: "+x) + println("test3: "+x) } } - test("mutable.HashSet", new mutable.HashSet[int], 5000) - test("immutable.Set", immutable.Set[int](), 5000) - test("immutable.ListSet", new immutable.ListSet[int], 5000) - test("immutable.TreeSet", new immutable.TreeSet[int], 5000) - test("mutable.HashMap", new mutable.HashMap[int, int], 5000) - test("immutable.Map", immutable.Map[int, int](), 5000) - test("immutable.TreeMap", new immutable.TreeMap[int, int], 5000) - test("immutable.ListMap", new immutable.ListMap[int, int], 3000) - test("immutable.UnBalancedTreeMap", new immutable.UnbalancedTreeMap[int, int], 1000) + test("mutable.HashSet", new mutable.HashSet[Int], 5000) + test("immutable.Set", immutable.Set[Int](), 5000) + test("immutable.ListSet", new immutable.ListSet[Int], 5000) + test("immutable.TreeSet", new immutable.TreeSet[Int], 5000) + test("mutable.HashMap", new mutable.HashMap[Int, Int], 5000) + test("immutable.Map", immutable.Map[Int, Int](), 5000) + test("immutable.TreeMap", new immutable.TreeMap[Int, Int], 5000) + test("immutable.ListMap", new immutable.ListMap[Int, Int], 3000) + test("immutable.UnBalancedTreeMap", new immutable.UnbalancedTreeMap[Int, Int], 1000) } diff --git a/test/files/run/iterables.check b/test/files/run/iterables.check new file mode 100644 index 0000000000..2660f51aa5 --- /dev/null +++ b/test/files/run/iterables.check @@ -0,0 +1,2 @@ +false +0,1,2,3,4,5,6,7,8,9 diff --git a/test/files/run/iterables.scala b/test/files/run/iterables.scala new file mode 100644 index 0000000000..7a29b80e8d --- /dev/null +++ b/test/files/run/iterables.scala @@ -0,0 +1,14 @@ +object Test extends Application { + class Test(n: Int) extends Iterable[Int] { + private var i = 0 + def elements = new Iterator[Int] { + def hasNext = i < n + def next = + if (hasNext) { val v = i; i += 1; v } + else throw new IndexOutOfBoundsException("empty iterator") + } + } + val x = new Test(10) + println(x.isEmpty) + println(x.mkString(",")) +} -- cgit v1.2.3