summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorpaltherr <paltherr@epfl.ch>2003-07-18 13:51:23 +0000
committerpaltherr <paltherr@epfl.ch>2003-07-18 13:51:23 +0000
commite9c280e68e136274fea9d88b40af6645e0fbb970 (patch)
treefb96b7dfbbb21a98877db0a4380cf8cb63a61c96 /test
parent30309b2ba2d8bfa6fb656cd760f6315581204d1d (diff)
downloadscala-e9c280e68e136274fea9d88b40af6645e0fbb970.tar.gz
scala-e9c280e68e136274fea9d88b40af6645e0fbb970.tar.bz2
scala-e9c280e68e136274fea9d88b40af6645e0fbb970.zip
- Merged sort1 into Course-2002-04
Diffstat (limited to 'test')
-rw-r--r--test/files/pos/sort1.scala33
-rw-r--r--test/files/run/Course-2002-04.check12
-rw-r--r--test/files/run/Course-2002-04.scala85
-rw-r--r--test/pos/sort1.scala33
4 files changed, 96 insertions, 67 deletions
diff --git a/test/files/pos/sort1.scala b/test/files/pos/sort1.scala
deleted file mode 100644
index a6eb6a7618..0000000000
--- a/test/files/pos/sort1.scala
+++ /dev/null
@@ -1,33 +0,0 @@
-object test {
-
- type String = java.lang.String;
-
- def While(def c: Boolean)(def b: Unit): Unit =
- if (c) { b ; While(c)(b) }
- else ();
-
- def sort(a: Array[Double]): Unit = {
-
- def swap(i: Int, j: Int): Unit = {
- val t = a(i) ; val u = a.apply(j) ; a(i) = u ; a(j) = t
- }
-
- def sort1(l: Int, r: Int): Unit = {
- val pivot = a((l + r) / 2);
- var i = l, j = r;
- While (i <= j) {
- While (a(i) < pivot) { i = i + 1 }
- While (a(j) > pivot) { j = j - 1 }
- if (i <= j) {
- swap(i, j);
- i = i + 1;
- j = j - 1;
- }
- }
- if (l < j) sort1(l, j);
- if (j < r) sort1(i, r);
- }
-
- sort1(0, a.length - 1);
- }
-}
diff --git a/test/files/run/Course-2002-04.check b/test/files/run/Course-2002-04.check
index 876643cda7..bf4218fb32 100644
--- a/test/files/run/Course-2002-04.check
+++ b/test/files/run/Course-2002-04.check
@@ -6,6 +6,18 @@ list4 = List(1,1,2,3,3,4,4,5,6,7,8,8,8)
list5 = List(8,8,8,7,6,5,4,4,3,3,2,1,1)
list6 = List(8,8,8,7,6,5,4,4,3,3,2,1,1)
+list0: List() -> List()
+list1: List(0) -> List(0)
+list2: List(0,1) -> List(0,1)
+list3: List(1,0) -> List(0,1)
+list4: List(0,1,2) -> List(0,1,2)
+list5: List(1,0,2) -> List(0,1,2)
+list6: List(0,1,2) -> List(0,1,2)
+list7: List(1,0,2) -> List(0,1,2)
+list8: List(2,0,1) -> List(0,1,2)
+list9: List(2,1,0) -> List(0,1,2)
+listA: List(6,3,1,8,7,1,2,5,8,4) -> List(1,1,2,3,4,5,6,7,8,8)
+
f(x) = 5x^3+7x^2+5x+9
f(0) = 9.0
f(1) = 26.0
diff --git a/test/files/run/Course-2002-04.scala b/test/files/run/Course-2002-04.scala
index 7370b9150c..714996e435 100644
--- a/test/files/run/Course-2002-04.scala
+++ b/test/files/run/Course-2002-04.scala
@@ -43,6 +43,88 @@ object M0 {
object M1 {
+ def mergesort[a] (less : (a,a) => Boolean) (xs: Array[a]): Unit = {
+
+ def While(def c: Boolean)(def b: Unit): Unit =
+ if (c) { b ; While(c)(b) } else ();
+
+ def swap(i: Int, j: Int): Unit = {
+ val t = xs(i);
+ val u = xs(j);
+ xs(i) = u;
+ xs(j) = t;
+ }
+
+ def sort1(l: Int, r: Int): Unit = {
+ val pivot = xs((l + r) / 2);
+ var i = l, j = r;
+ While (i <= j) {
+ While (less(xs(i), pivot)) { i = i + 1 }
+ While (less(pivot, xs(j))) { j = j - 1 }
+ if (i <= j) {
+ swap(i, j);
+ i = i + 1;
+ j = j - 1;
+ }
+ }
+ if (l < j) sort1(l, j);
+ if (j < r) sort1(i, r);
+ }
+
+ if (xs.length > 0) sort1(0, xs.length - 1);
+ }
+
+ def list2array(list: List[Int]): Array[Int] = {
+ val array = new Array[Int](list.length);
+ list.copyToArray(array, 0);
+ array;
+ }
+
+ def array2list(array: Array[Int]): List[Int] = {
+ var list = List[Int]();
+ List.range(0, array.length).map(i => list = array(i) :: list);
+ list.reverse;
+ }
+
+ def isort(list: List[Int]): List[Int] = {
+ val array = list2array(list);
+ mergesort[Int]((x,y) => x < y)(array);
+ array2list(array);
+ }
+
+ def test = {
+ val list0 = List();
+ val list1 = List(0);
+ val list2 = List(0,1);
+ val list3 = List(1,0);
+ val list4 = List(0,1,2);
+ val list5 = List(1,0,2);
+ val list6 = List(0,1,2);
+ val list7 = List(1,0,2);
+ val list8 = List(2,0,1);
+ val list9 = List(2,1,0);
+ val listA = List(6,3,1,8,7,1,2,5,8,4);
+
+ System.out.println("list0: " + list0 + " -> " + isort(list0));
+ System.out.println("list1: " + list1 + " -> " + isort(list1));
+ System.out.println("list2: " + list2 + " -> " + isort(list2));
+ System.out.println("list3: " + list3 + " -> " + isort(list3));
+ System.out.println("list4: " + list4 + " -> " + isort(list4));
+ System.out.println("list5: " + list5 + " -> " + isort(list5));
+ System.out.println("list6: " + list6 + " -> " + isort(list6));
+ System.out.println("list7: " + list7 + " -> " + isort(list7));
+ System.out.println("list8: " + list8 + " -> " + isort(list8));
+ System.out.println("list9: " + list9 + " -> " + isort(list9));
+ System.out.println("listA: " + listA + " -> " + isort(listA));
+ System.out.println();
+ }
+
+}
+
+//############################################################################
+
+object M2 {
+
def horner (x : Double, coefs : List[Double]) : Double = {
if (coefs.isEmpty)
0
@@ -63,7 +145,7 @@ object M1 {
//############################################################################
-object M2 {
+object M3 {
def dotproduct (v : List[Double], w : List[Double]) : Double = {
if (v.isEmpty)
@@ -154,6 +236,7 @@ object Test {
M0.test;
M1.test;
M2.test;
+ M3.test;
()
}
}
diff --git a/test/pos/sort1.scala b/test/pos/sort1.scala
deleted file mode 100644
index a6eb6a7618..0000000000
--- a/test/pos/sort1.scala
+++ /dev/null
@@ -1,33 +0,0 @@
-object test {
-
- type String = java.lang.String;
-
- def While(def c: Boolean)(def b: Unit): Unit =
- if (c) { b ; While(c)(b) }
- else ();
-
- def sort(a: Array[Double]): Unit = {
-
- def swap(i: Int, j: Int): Unit = {
- val t = a(i) ; val u = a.apply(j) ; a(i) = u ; a(j) = t
- }
-
- def sort1(l: Int, r: Int): Unit = {
- val pivot = a((l + r) / 2);
- var i = l, j = r;
- While (i <= j) {
- While (a(i) < pivot) { i = i + 1 }
- While (a(j) > pivot) { j = j - 1 }
- if (i <= j) {
- swap(i, j);
- i = i + 1;
- j = j - 1;
- }
- }
- if (l < j) sort1(l, j);
- if (j < r) sort1(i, r);
- }
-
- sort1(0, a.length - 1);
- }
-}