summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormichelou <michelou@epfl.ch>2006-10-05 12:22:34 +0000
committermichelou <michelou@epfl.ch>2006-10-05 12:22:34 +0000
commite627aa0456e4578f2f025c2c21ef33a0be7dd1a0 (patch)
treef61333bc5970c076106a9da1b36752531aed6f9b /src
parent054f9fcc98382ba420e75382ae6fb9d5c3cd4ce8 (diff)
downloadscala-e627aa0456e4578f2f025c2c21ef33a0be7dd1a0.tar.gz
scala-e627aa0456e4578f2f025c2c21ef33a0be7dd1a0.tar.bz2
scala-e627aa0456e4578f2f025c2c21ef33a0be7dd1a0.zip
cleaned up code and comments in scala/testing/*...
cleaned up code and comments in scala/testing/*.scala
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/testing/Benchmark.scala66
-rw-r--r--src/library/scala/testing/SUnit.scala189
-rw-r--r--src/library/scala/testing/UnitTest.scala64
3 files changed, 172 insertions, 147 deletions
diff --git a/src/library/scala/testing/Benchmark.scala b/src/library/scala/testing/Benchmark.scala
index 2db1954a0c..c3ccacb084 100644
--- a/src/library/scala/testing/Benchmark.scala
+++ b/src/library/scala/testing/Benchmark.scala
@@ -9,64 +9,70 @@
// $Id$
-package scala.testing;
+package scala.testing
/** <code>Benchmark</code> can be used to quickly turn an existing
* class into a benchmark. Here is a short example:
*
* <pre>
- * object sort1 extends Sorter with Benchmark {
- * def run = sort(List.range(1, 1000));
+ * <b>object</b> sort1 <b>extends</b> Sorter <b>with</b> Benchmark {
+ * <b>def</b> run = sort(List.range(1, 1000))
* }
* </pre>
- *
- * The run method has to be defined by the user, who will perform
- * the timed operation there.
- * Run the benchmark as follows:
- * <pre>
- * scala sort1 5 times.log
- * </pre>
- * This will run the benchmark 5 times and log the execution times in
- * a file called times.log
+ * <p>
+ * The run method has to be defined by the user, who will perform
+ * the timed operation there.
+ * Run the benchmark as follows:
+ * </p>
+ * <pre>
+ * &gt; scala sort1 5 times.log
+ * </pre>
+ * <p>
+ * This will run the benchmark 5 times and log the execution times in
+ * a file called <code>times.log</code>
+ * </p>
*/
trait Benchmark {
/** this method should be implemented by the concrete benchmark */
- def run: Unit;
+ def run: Unit
/** Run the benchmark the specified number of times
* and return a list with the execution times in milliseconds
- * in reverse order of the execution */
+ * in reverse order of the execution
+ *
+ * @param noTimes ...
+ * @return ...
+ */
def runBenchmark(noTimes: Int): List[Long] =
-
for (val i <- List.range(1, noTimes + 1)) yield {
- val startTime = System.currentTimeMillis();
- run;
- val stopTime = System.currentTimeMillis();
- System.gc();
+ val startTime = System.currentTimeMillis()
+ run
+ val stopTime = System.currentTimeMillis()
+ System.gc()
stopTime - startTime
}
/**
- * The entry point. It takes two arguments: the number of
- * consecutive runs, and the name of a log file where to
- * append the times.
- *
- */
+ * The entry point. It takes two arguments: the number of
+ * consecutive runs, and the name of a log file where to
+ * append the times.
+ */
def main(args: Array[String]): Unit = {
if (args.length > 1) {
- val logFile = new java.io.FileWriter(args(1), true); // append, not overwrite
+ val logFile = new java.io.FileWriter(args(1), true) // append, not overwrite
+ val eol = System.getProperty("line.separator", "\n")
- logFile.write(getClass().getName());
+ logFile.write(getClass().getName())
for (val t <- runBenchmark(Integer.parseInt(args(0))))
- logFile.write("\t\t" + t);
+ logFile.write("\t\t" + t)
- logFile.write("\n");
- logFile.flush();
+ logFile.write(eol)
+ logFile.flush()
} else
- Console.println("Usage: scala benchmarks.program <runs> <logfile>");
+ Console.println("Usage: scala benchmarks.program <runs> <logfile>")
}
}
diff --git a/src/library/scala/testing/SUnit.scala b/src/library/scala/testing/SUnit.scala
index 1468959929..517d77bd66 100644
--- a/src/library/scala/testing/SUnit.scala
+++ b/src/library/scala/testing/SUnit.scala
@@ -9,82 +9,88 @@
// $Id$
-package scala.testing;
+package scala.testing
+import scala.collection.mutable.ArrayBuffer
import scala.runtime.compat.StringBuilder
/**
- * Unit testing methods in the spirit of JUnit framework.
- * use these classes like this:
-<code>
- import scala.testing.SUnit;
- import SUnit._;
-
-
- class MyTest(n:String) extends TestCase(n) {
-
- override def runTest() = n match {
- case "myTest1" => assertTrue( true );
- case "myTest2" => assertTrue( "hello", false );
- }
- }
-
- val r = new TestResult();
- suite.run(r);
- for(val tf &lt;- r.failures()) {
- Console.println(tf.toString())
- }
-}
-</code>
+ * <p>
+ * Unit testing methods in the spirit of
+ * <a href="http://www.junit.org/" target="_top">JUnit</a> framework.
+ * </p>
+ * <p>
+ * Use these classes like this:
+ * </p>
+ * <pre>
+ * import scala.testing.SUnit
+ * import SUnit._
+ *
+ * <b>class</b> MyTest(n: String) <b>extends</b> TestCase(n) {
+ *
+ * <b>override def</b> runTest() = n <b>match</b> {
+ * <b>case</b> "myTest1" => assertTrue(<b>true</b>)
+ * <b>case</b> "myTest2" => assertTrue("hello", <b>false</b>)
+ * }
+ * }
+ *
+ * <b>val</b> r = <b>new</b> TestResult()
+ * suite.run(r)
+ * for (<b>val</b> tf &lt;- r.failures()) {
+ * Console.println(tf.toString())
+ * }
+ * </pre>
*/
object SUnit {
/** a Test can be run with its result being collected */
trait Test {
- def run(r: TestResult):Unit;
+ def run(r: TestResult): Unit
}
- /** a TestCase defines the fixture to run multiple tests */
+ /** The class <code>TestCase</code> defines the fixture to run multiple
+ * tests.
+ *
+ * @param name ...
+ */
class TestCase(val name: String) extends Test with Assert {
- protected def createResult() =
- new TestResult();
- protected def runTest(): Unit =
- {}
+ protected def createResult() = new TestResult()
+
+ protected def runTest(): Unit = {}
def run(r: TestResult): Unit =
try {
- runTest();
+ runTest()
} catch {
- case t:Throwable => r.addFailure(this, t);
+ case t:Throwable => r.addFailure(this, t)
}
- def run(): Unit =
- run(createResult());
+ def run(): Unit = run(createResult())
- def setUp() =
- {}
+ def setUp() = {}
- def tearDown() =
- {}
+ def tearDown() = {}
- override def toString() =
- name;
+ override def toString() = name
}
- /** a TestFailure collects a failed test together with the thrown exception */
- class TestFailure(val failedTest:Test, val thrownException:Throwable) {
+ /** The class <code>TestFailure</code> collects a failed test together
+ * with the thrown exception.
+ */
+ class TestFailure(val failedTest: Test, val thrownException: Throwable) {
+ private val eol = System.getProperty("line.separator", "\n")
- def this(p:Pair[Test,Throwable]) = this(p._1, p._2);
+ def this(p: Pair[Test, Throwable]) = this(p._1, p._2)
override def toString() =
- failedTest.toString()+" failed due to "+thrownException.toString();
+ failedTest.toString() + " failed due to " + thrownException.toString()
def trace(): String = {
- val s = new StringBuilder();
- for(val trElem <- thrownException.getStackTrace()) {
- s.append(trElem.toString());
- s.append('\n');
+ val s = new StringBuilder()
+ for (val trElem <- thrownException.getStackTrace()) {
+ s.append(trElem.toString())
+ s.append(eol)
}
s.toString()
}
@@ -92,102 +98,105 @@ object SUnit {
/** a TestResult collects the result of executing a test case */
class TestResult {
- val buf =
- new scala.collection.mutable.ArrayBuffer[Pair[Test,Throwable]]();
+ val buf = new ArrayBuffer[Pair[Test, Throwable]]()
def addFailure(test:Test, t:Throwable) =
- buf += Pair(test,t);
+ buf += Pair(test, t)
def failureCount() =
- buf.length;
+ buf.length
def failures() =
- buf.elements map { x => new TestFailure(x) };
+ buf.elements map { x => new TestFailure(x) }
}
- /** a TestSuite runs a composite of test cases */
+ /** The class <code>TestSuite</code> runs a composite of test cases.
+ */
class TestSuite(tests:Test*) extends Test {
- def this(names:Seq[String], constr:String=>Test) =
- this((names.toList map constr):_*);
+ def this(names: Seq[String], constr: String => Test) =
+ this((names.toList map constr):_*)
- val buf =
- new scala.collection.mutable.ArrayBuffer[Test]();
+ val buf = new ArrayBuffer[Test]()
- buf ++= tests;
+ buf ++= tests
def addTest(t: Test) =
- buf += t;
+ buf += t
- def run(r: TestResult):Unit = {
+ def run(r: TestResult): Unit =
for(val t <- buf) {
- t.run(r);
+ t.run(r)
}
- }
+
}
/** an AssertFailed is thrown for a failed assertion */
- case class AssertFailed(msg:String) extends java.lang.RuntimeException {
- override def toString() =
- "failed assertion:"+msg;
+ case class AssertFailed(msg: String) extends java.lang.RuntimeException {
+ override def toString() = "failed assertion:" + msg
}
/** this class defined useful assert methods */
trait Assert {
/** equality */
- def assertEquals[A](msg:String, expected:A, actual: => A): Unit =
- if( expected != actual ) fail(msg);
+ def assertEquals[A](msg: String, expected: A, actual: => A): Unit =
+ if (expected != actual) fail(msg)
/** equality */
- def assertEquals[A](expected:A, actual: => A): Unit =
- assertEquals("(no message)", expected, actual);
+ def assertEquals[A](expected: A, actual: => A): Unit =
+ assertEquals("(no message)", expected, actual)
/** falseness */
- def assertFalse(msg:String, actual: => Boolean): Unit =
- assertEquals(msg, false, actual);
+ def assertFalse(msg: String, actual: => Boolean): Unit =
+ assertEquals(msg, false, actual)
+
/** falseness */
def assertFalse(actual: => Boolean): Unit =
- assertFalse("(no message)", actual);
+ assertFalse("(no message)", actual)
/** not null */
def assertNotNull(msg:String, actual: => AnyRef): Unit =
- if( null == actual ) fail(msg);
+ if (null == actual) fail(msg)
/** not null */
def assertNotNull(actual: => AnyRef): Unit =
- assertNotNull("(no message)", actual);
+ assertNotNull("(no message)", actual)
/** reference inequality */
- def assertNotSame(msg:String, expected: => AnyRef, actual: => AnyRef): Unit =
- if(expected.eq(actual)) fail(msg);
+ def assertNotSame(msg: String, expected: => AnyRef, actual: => AnyRef): Unit =
+ if (expected.eq(actual)) fail(msg)
+
/** reference inequality */
def assertNotSame(expected: => AnyRef, actual: => AnyRef): Unit =
- assertNotSame("(no message)", expected, actual);
+ assertNotSame("(no message)", expected, actual)
/** null */
- def assertNull(msg:String, actual: => AnyRef): Unit =
- if( null != actual ) fail(msg);
+ def assertNull(msg: String, actual: => AnyRef): Unit =
+ if (null != actual) fail(msg)
+
/** null */
def assertNull(actual: => AnyRef): Unit =
- assertNull("(no message)", actual);
-
+ assertNull("(no message)", actual)
/** reference equality */
- def assertSame(msg:String, expected: => AnyRef, actual: => AnyRef): Unit =
- if(!expected.eq(actual)) fail(msg);
+ def assertSame(msg: String, expected: => AnyRef, actual: => AnyRef): Unit =
+ if(!expected.eq(actual)) fail(msg)
+
/** reference equality */
def assertSame(expected: => AnyRef, actual: => AnyRef): Unit =
- assertSame("(no message)", expected, actual);
+ assertSame("(no message)", expected, actual)
/** trueness */
- def assertTrue(msg:String, actual: => Boolean): Unit =
- assertEquals(msg, true, actual);
+ def assertTrue(msg: String, actual: => Boolean): Unit =
+ assertEquals(msg, true, actual)
+
/** trueness */
def assertTrue(actual: => Boolean): Unit =
- assertTrue("(no message)", actual);
+ assertTrue("(no message)", actual)
- /** throws AssertFailed with given message */
- def fail(msg:String): Unit =
- throw new AssertFailed(msg);
+ /** throws <code>AssertFailed</code> with given message <code>msg</code>.
+ */
+ def fail(msg: String): Unit =
+ throw new AssertFailed(msg)
}
}
diff --git a/src/library/scala/testing/UnitTest.scala b/src/library/scala/testing/UnitTest.scala
index fbd20af3c8..38b6fb17e3 100644
--- a/src/library/scala/testing/UnitTest.scala
+++ b/src/library/scala/testing/UnitTest.scala
@@ -9,7 +9,7 @@
// $Id$
-package scala.testing;
+package scala.testing
/**
@@ -18,45 +18,55 @@ package scala.testing;
*/
object UnitTest {
- class Report( report_ok:()=>Unit, report_fail:(String,String)=>Unit ) {
- def ok:Unit = report_ok();
- def fail( actual:String, expected:String ):Unit =
- report_fail( actual, expected );
+ class Report(report_ok: () => Unit, report_fail: (String,String) => Unit) {
+ def ok: Unit = report_ok()
+ def fail(actual: String, expected: String): Unit =
+ report_fail(actual, expected)
}
var report = new Report(
{ () => Console.println("passed ok") },
- { (actual:String, expected:String) =>
- Console.print("failed! we got");
- Console.print( "\""+ actual +"\"" );
- Console.println(" but expected \"" + expected + "\"") });
+ { (actual: String, expected: String) =>
+ Console.print("failed! we got")
+ Console.print( "\""+ actual +"\"" )
+ Console.println(" but expected \"" + expected + "\"") })
- def setReporter( r:Report ) = {
- this.report = r;
+ /**
+ * @param r ...
+ */
+ def setReporter(r: Report) = {
+ this.report = r
}
- def assertSameElements[a]( actual:Seq[a] , expected: Seq[a] ):Unit =
- if( actual.sameElements( expected ) )
- report.ok
+ /**
+ * @param actual ...
+ * @param expected ...
+ */
+ def assertSameElements[a](actual: Seq[a], expected: Seq[a]): Unit =
+ if (actual.sameElements(expected))
+ report.ok
else
- report.fail( actual.toString(), expected.toString() );
+ report.fail(actual.toString(), expected.toString())
- def assertEquals[a]( actual: a, expected: a ):Unit =
- if( actual == expected )
- report.ok
+ /**
+ * @param actual ...
+ * @param expected ...
+ */
+ def assertEquals[a](actual: a, expected: a): Unit =
+ if (actual == expected)
+ report.ok
else
- report.fail( actual.toString(), expected.toString() );
+ report.fail(actual.toString(), expected.toString())
- def assertTrue( actual: Boolean ):Unit = assertEquals(actual, true);
- def assertFalse( actual: Boolean ):Unit = assertEquals(actual, false);
+ def assertTrue(actual: Boolean): Unit = assertEquals(actual, true)
+ def assertFalse(actual: Boolean): Unit = assertEquals(actual, false)
-
- def assertNotEquals[a]( actual: a, expected: a ):Unit =
- if( actual != expected )
- report.ok
+ def assertNotEquals[a]( actual: a, expected: a): Unit =
+ if (actual != expected)
+ report.ok
else
- report.fail( actual.toString(), "x != "+expected.toString() );
+ report.fail(actual.toString(), "x != "+expected.toString())
- //def test[a]( def doit: a, expected: a ):Unit = assertEquals( doit, expected );
+ //def test[a](def doit: a, expected: a): Unit = assertEquals(doit, expected)
} // unitTest