Compare commits
No commits in common. "actorbintree" and "example" have entirely different histories.
actorbintr
...
example
@ -1,4 +1,5 @@
|
|||||||
# DO NOT EDIT THIS FILE
|
# This configuration is not used for the final grading, you can change it if
|
||||||
|
# you know what you're doing.
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- build
|
- build
|
||||||
@ -25,7 +26,7 @@ grade:
|
|||||||
tags:
|
tags:
|
||||||
- cs206
|
- cs206
|
||||||
image:
|
image:
|
||||||
name: smarter3/moocs:reactive-actorbintree-2020-04-15
|
name: smarter3/moocs:progfun1-example-2020-02-14
|
||||||
entrypoint: [""]
|
entrypoint: [""]
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
before_script:
|
before_script:
|
||||||
|
|||||||
@ -1,4 +1,9 @@
|
|||||||
// Student tasks (i.e. submit, packageSubmission)
|
// Student tasks (i.e. submit, packageSubmission)
|
||||||
enablePlugins(StudentTasks)
|
enablePlugins(StudentTasks)
|
||||||
|
|
||||||
|
courseraId := ch.epfl.lamp.CourseraId(
|
||||||
|
key = "g4unnjZBEeWj7SIAC5PFxA",
|
||||||
|
itemId = "xIz9O",
|
||||||
|
premiumItemId = None,
|
||||||
|
partId = "d5jxI"
|
||||||
|
)
|
||||||
|
|||||||
28
build.sbt
28
build.sbt
@ -1,24 +1,8 @@
|
|||||||
course := "reactive"
|
course := "progfun1"
|
||||||
assignment := "actorbintree"
|
assignment := "example"
|
||||||
|
scalaVersion := "0.23.0-bin-20200211-5b006fb-NIGHTLY"
|
||||||
|
scalacOptions ++= Seq("-language:implicitConversions", "-deprecation")
|
||||||
|
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test
|
||||||
|
|
||||||
testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-a", "-v", "-s")
|
testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-a", "-v", "-s")
|
||||||
parallelExecution in Test := false
|
testSuite := "example.ListsSuite"
|
||||||
|
|
||||||
val akkaVersion = "2.6.0"
|
|
||||||
|
|
||||||
scalaVersion := "0.23.0-bin-20200211-5b006fb-NIGHTLY"
|
|
||||||
|
|
||||||
scalacOptions ++= Seq(
|
|
||||||
"-feature",
|
|
||||||
"-deprecation",
|
|
||||||
"-encoding", "UTF-8",
|
|
||||||
"-unchecked",
|
|
||||||
"-language:implicitConversions"
|
|
||||||
)
|
|
||||||
|
|
||||||
libraryDependencies ++= Seq(
|
|
||||||
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
|
|
||||||
"com.typesafe.akka" %% "akka-testkit" % akkaVersion % Test,
|
|
||||||
"com.novocode" % "junit-interface" % "0.11" % Test
|
|
||||||
).map(_.withDottyCompat(scalaVersion.value))
|
|
||||||
testSuite := "actorbintree.BinaryTreeSuite"
|
|
||||||
|
|||||||
Binary file not shown.
@ -1,189 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
|
||||||
*/
|
|
||||||
package actorbintree
|
|
||||||
|
|
||||||
import akka.actor._
|
|
||||||
import scala.collection.immutable.Queue
|
|
||||||
|
|
||||||
object BinaryTreeSet {
|
|
||||||
|
|
||||||
trait Operation {
|
|
||||||
def requester: ActorRef
|
|
||||||
def id: Int
|
|
||||||
def elem: Int
|
|
||||||
}
|
|
||||||
|
|
||||||
trait OperationReply {
|
|
||||||
def id: Int
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Request with identifier `id` to insert an element `elem` into the tree.
|
|
||||||
* The actor at reference `requester` should be notified when this operation
|
|
||||||
* is completed.
|
|
||||||
*/
|
|
||||||
case class Insert(requester: ActorRef, id: Int, elem: Int) extends Operation
|
|
||||||
|
|
||||||
/** Request with identifier `id` to check whether an element `elem` is present
|
|
||||||
* in the tree. The actor at reference `requester` should be notified when
|
|
||||||
* this operation is completed.
|
|
||||||
*/
|
|
||||||
case class Contains(requester: ActorRef, id: Int, elem: Int) extends Operation
|
|
||||||
|
|
||||||
/** Request with identifier `id` to remove the element `elem` from the tree.
|
|
||||||
* The actor at reference `requester` should be notified when this operation
|
|
||||||
* is completed.
|
|
||||||
*/
|
|
||||||
case class Remove(requester: ActorRef, id: Int, elem: Int) extends Operation
|
|
||||||
|
|
||||||
/** Request to perform garbage collection */
|
|
||||||
case object GC
|
|
||||||
|
|
||||||
/** Holds the answer to the Contains request with identifier `id`.
|
|
||||||
* `result` is true if and only if the element is present in the tree.
|
|
||||||
*/
|
|
||||||
case class ContainsResult(id: Int, result: Boolean) extends OperationReply
|
|
||||||
|
|
||||||
/** Message to signal successful completion of an insert or remove operation. */
|
|
||||||
case class OperationFinished(id: Int) extends OperationReply
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class BinaryTreeSet extends Actor {
|
|
||||||
import BinaryTreeSet._
|
|
||||||
import BinaryTreeNode._
|
|
||||||
|
|
||||||
def createRoot: ActorRef = context.actorOf(BinaryTreeNode.props(0, initiallyRemoved = true))
|
|
||||||
|
|
||||||
var root = createRoot
|
|
||||||
|
|
||||||
// optional
|
|
||||||
var pendingQueue = Queue.empty[Operation]
|
|
||||||
|
|
||||||
// optional
|
|
||||||
def receive = normal
|
|
||||||
|
|
||||||
// optional
|
|
||||||
/** Accepts `Operation` and `GC` messages. */
|
|
||||||
val normal: Receive = {
|
|
||||||
case op:Operation => root ! op
|
|
||||||
case GC => {
|
|
||||||
val newRoot = createRoot;
|
|
||||||
root ! CopyTo(newRoot)
|
|
||||||
context.become(garbageCollecting(newRoot))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// optional
|
|
||||||
/** Handles messages while garbage collection is performed.
|
|
||||||
* `newRoot` is the root of the new binary tree where we want to copy
|
|
||||||
* all non-removed elements into.
|
|
||||||
*/
|
|
||||||
def garbageCollecting(newRoot: ActorRef): Receive = {
|
|
||||||
case op:Operation => pendingQueue = pendingQueue.enqueue(op)
|
|
||||||
case CopyFinished =>
|
|
||||||
pendingQueue.foreach(newRoot ! _) //foreach preserves order of a queue (same as dequeueing)
|
|
||||||
root ! PoisonPill //Will also stop all of its children
|
|
||||||
pendingQueue = Queue.empty
|
|
||||||
root = newRoot;
|
|
||||||
context.become(normal)
|
|
||||||
//Ignore GC messages here
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
object BinaryTreeNode {
|
|
||||||
trait Position
|
|
||||||
|
|
||||||
case object Left extends Position
|
|
||||||
case object Right extends Position
|
|
||||||
|
|
||||||
case class CopyTo(treeNode: ActorRef)
|
|
||||||
case object CopyFinished
|
|
||||||
|
|
||||||
def props(elem: Int, initiallyRemoved: Boolean) = Props(classOf[BinaryTreeNode], elem, initiallyRemoved)
|
|
||||||
}
|
|
||||||
|
|
||||||
class BinaryTreeNode(val elem: Int, initiallyRemoved: Boolean) extends Actor {
|
|
||||||
import BinaryTreeNode._
|
|
||||||
import BinaryTreeSet._
|
|
||||||
|
|
||||||
var subtrees = Map[Position, ActorRef]()
|
|
||||||
var removed = initiallyRemoved
|
|
||||||
|
|
||||||
// optional
|
|
||||||
def receive = normal
|
|
||||||
|
|
||||||
def goDownTo(elem : Int) : Position = if(elem < this.elem) Left else Right
|
|
||||||
// optional
|
|
||||||
/** Handles `Operation` messages and `CopyTo` requests. */
|
|
||||||
val normal: Receive = {
|
|
||||||
case Insert (requester, id, elem) =>
|
|
||||||
if(elem == this.elem && !removed){
|
|
||||||
requester ! OperationFinished(id)
|
|
||||||
}else{
|
|
||||||
val nextPos = goDownTo(elem)
|
|
||||||
|
|
||||||
subtrees get nextPos match{
|
|
||||||
case Some(node) => node ! Insert(requester, id, elem)
|
|
||||||
case None => {
|
|
||||||
val newActorSubtree = (nextPos, context.actorOf(BinaryTreeNode.props(elem, false)))
|
|
||||||
subtrees = subtrees + newActorSubtree
|
|
||||||
requester ! OperationFinished(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case Contains(requester, id, elem) =>
|
|
||||||
if(elem == this.elem && !removed)
|
|
||||||
requester ! ContainsResult(id, true)
|
|
||||||
else{
|
|
||||||
//Need to search subtrees
|
|
||||||
subtrees get goDownTo(elem) match{
|
|
||||||
case Some(node) => node ! Contains(requester, id, elem)
|
|
||||||
case None => requester ! ContainsResult(id, false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case Remove (requester, id, elem) =>
|
|
||||||
if(elem == this.elem && !removed){
|
|
||||||
removed = true
|
|
||||||
requester ! OperationFinished(id)
|
|
||||||
}else{
|
|
||||||
subtrees get goDownTo(elem) match{
|
|
||||||
case Some(node) => node ! Remove(requester, id, elem)
|
|
||||||
case None => requester ! OperationFinished(id) // (elem isn't in the tree)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case CopyTo(newRoot) =>
|
|
||||||
//We are already done, nothing to do
|
|
||||||
if(removed && subtrees.isEmpty) context.parent ! CopyFinished
|
|
||||||
else{
|
|
||||||
if(!removed) newRoot ! Insert(self, elem, elem)
|
|
||||||
subtrees.values foreach(_ ! CopyTo(newRoot)) //Copy subtrees elems
|
|
||||||
//val insertConfirmed = if(removed) true else false, hence we can simply pass removed
|
|
||||||
context.become(copying(subtrees.values.toSet, removed))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// optional
|
|
||||||
/** `expected` is the set of ActorRefs whose replies we are waiting for,
|
|
||||||
* `insertConfirmed` tracks whether the copy of this node to the new tree has been confirmed.
|
|
||||||
*/
|
|
||||||
def copying(expected: Set[ActorRef], insertConfirmed: Boolean): Receive = {
|
|
||||||
//To catch the insert of this node into the new tree beeing finished
|
|
||||||
case OperationFinished(_) => {
|
|
||||||
if(expected.isEmpty) context.parent ! CopyFinished
|
|
||||||
else context.become(copying(expected, true))
|
|
||||||
}
|
|
||||||
|
|
||||||
case CopyFinished => {
|
|
||||||
val newExp = expected-sender
|
|
||||||
if(insertConfirmed && newExp.isEmpty){
|
|
||||||
context.parent ! CopyFinished
|
|
||||||
}else{
|
|
||||||
context.become(copying(newExp, insertConfirmed))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
49
src/main/scala/example/Lists.scala
Normal file
49
src/main/scala/example/Lists.scala
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package example
|
||||||
|
|
||||||
|
object Lists {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method computes the sum of all elements in the list xs. There are
|
||||||
|
* multiple techniques that can be used for implementing this method, and
|
||||||
|
* you will learn during the class.
|
||||||
|
*
|
||||||
|
* For this example assignment you can use the following methods in class
|
||||||
|
* `List`:
|
||||||
|
*
|
||||||
|
* - `xs.isEmpty: Boolean` returns `true` if the list `xs` is empty
|
||||||
|
* - `xs.head: Int` returns the head element of the list `xs`. If the list
|
||||||
|
* is empty an exception is thrown
|
||||||
|
* - `xs.tail: List[Int]` returns the tail of the list `xs`, i.e. the the
|
||||||
|
* list `xs` without its `head` element
|
||||||
|
*
|
||||||
|
* ''Hint:'' instead of writing a `for` or `while` loop, think of a recursive
|
||||||
|
* solution.
|
||||||
|
*
|
||||||
|
* @param xs A list of natural numbers
|
||||||
|
* @return The sum of all elements in `xs`
|
||||||
|
*/
|
||||||
|
def sum(list: List[Int]): Int = list match {
|
||||||
|
case Nil => 0
|
||||||
|
case x :: xs => x + sum(xs)
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* This method returns the largest element in a list of integers. If the
|
||||||
|
* list `xs` is empty it throws a `java.util.NoSuchElementException`.
|
||||||
|
*
|
||||||
|
* You can use the same methods of the class `List` as mentioned above.
|
||||||
|
*
|
||||||
|
* ''Hint:'' Again, think of a recursive solution instead of using looping
|
||||||
|
* constructs. You might need to define an auxiliary method.
|
||||||
|
*
|
||||||
|
* @param xs A list of natural numbers
|
||||||
|
* @return The largest element in `xs`
|
||||||
|
* @throws java.util.NoSuchElementException if `xs` is an empty list
|
||||||
|
*/
|
||||||
|
def max(xs: List[Int]): Int = {
|
||||||
|
def loop(ys: List[Int], max: Int): Int = ys match{
|
||||||
|
case head::tail => loop(tail, if(head>max) then head else max)
|
||||||
|
case Nil => max
|
||||||
|
}
|
||||||
|
loop(xs, Int.MinValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,126 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
|
||||||
*/
|
|
||||||
package actorbintree
|
|
||||||
|
|
||||||
import akka.actor.{ActorRef, ActorSystem, Props, actorRef2Scala, scala2ActorRef}
|
|
||||||
import akka.testkit.{ImplicitSender, TestKit, TestProbe}
|
|
||||||
import org.junit.Test
|
|
||||||
import org.junit.Assert._
|
|
||||||
|
|
||||||
import scala.util.Random
|
|
||||||
import scala.concurrent.duration._
|
|
||||||
|
|
||||||
class BinaryTreeSuite extends TestKit(ActorSystem("BinaryTreeSuite")) with ImplicitSender {
|
|
||||||
|
|
||||||
import actorbintree.BinaryTreeSet._
|
|
||||||
|
|
||||||
def receiveN(requester: TestProbe, ops: Seq[Operation], expectedReplies: Seq[OperationReply]): Unit =
|
|
||||||
requester.within(5.seconds) {
|
|
||||||
val repliesUnsorted = for (i <- 1 to ops.size) yield try {
|
|
||||||
requester.expectMsgType[OperationReply]
|
|
||||||
} catch {
|
|
||||||
case ex: Throwable if ops.size > 10 => sys.error(s"failure to receive confirmation $i/${ops.size}\n$ex")
|
|
||||||
case ex: Throwable => sys.error(s"failure to receive confirmation $i/${ops.size}\nRequests:" + ops.mkString("\n ", "\n ", "") + s"\n$ex")
|
|
||||||
}
|
|
||||||
val replies = repliesUnsorted.sortBy(_.id)
|
|
||||||
if (replies != expectedReplies) {
|
|
||||||
val pairs = (replies zip expectedReplies).zipWithIndex filter (x => x._1._1 != x._1._2)
|
|
||||||
fail("unexpected replies:" + pairs.map(x => s"at index ${x._2}: got ${x._1._1}, expected ${x._1._2}").mkString("\n ", "\n ", ""))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def verify(probe: TestProbe, ops: Seq[Operation], expected: Seq[OperationReply]): Unit = {
|
|
||||||
val topNode = system.actorOf(Props[BinaryTreeSet])
|
|
||||||
|
|
||||||
ops foreach { op =>
|
|
||||||
topNode ! op
|
|
||||||
}
|
|
||||||
|
|
||||||
receiveN(probe, ops, expected)
|
|
||||||
// the grader also verifies that enough actors are created
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test def `proper inserts and lookups (5pts)`(): Unit = {
|
|
||||||
val topNode = system.actorOf(Props[BinaryTreeSet])
|
|
||||||
|
|
||||||
topNode ! Contains(testActor, id = 1, 1)
|
|
||||||
expectMsg(ContainsResult(1, false))
|
|
||||||
|
|
||||||
topNode ! Insert(testActor, id = 2, 1)
|
|
||||||
topNode ! Contains(testActor, id = 3, 1)
|
|
||||||
|
|
||||||
expectMsg(OperationFinished(2))
|
|
||||||
expectMsg(ContainsResult(3, true))
|
|
||||||
()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test def `instruction example (5pts)`(): Unit = {
|
|
||||||
val requester = TestProbe()
|
|
||||||
val requesterRef = requester.ref
|
|
||||||
val ops = List(
|
|
||||||
Insert(requesterRef, id=100, 1),
|
|
||||||
Contains(requesterRef, id=50, 2),
|
|
||||||
Remove(requesterRef, id=10, 1),
|
|
||||||
Insert(requesterRef, id=20, 2),
|
|
||||||
Contains(requesterRef, id=80, 1),
|
|
||||||
Contains(requesterRef, id=70, 2)
|
|
||||||
)
|
|
||||||
|
|
||||||
val expectedReplies = List(
|
|
||||||
OperationFinished(id=10),
|
|
||||||
OperationFinished(id=20),
|
|
||||||
ContainsResult(id=50, false),
|
|
||||||
ContainsResult(id=70, true),
|
|
||||||
ContainsResult(id=80, false),
|
|
||||||
OperationFinished(id=100)
|
|
||||||
)
|
|
||||||
|
|
||||||
verify(requester, ops, expectedReplies)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test def `behave identically to built-in set (includes GC) (40pts)`(): Unit = {
|
|
||||||
val rnd = new Random()
|
|
||||||
def randomOperations(requester: ActorRef, count: Int): Seq[Operation] = {
|
|
||||||
def randomElement: Int = rnd.nextInt(100)
|
|
||||||
def randomOperation(requester: ActorRef, id: Int): Operation = rnd.nextInt(4) match {
|
|
||||||
case 0 => Insert(requester, id, randomElement)
|
|
||||||
case 1 => Insert(requester, id, randomElement)
|
|
||||||
case 2 => Contains(requester, id, randomElement)
|
|
||||||
case 3 => Remove(requester, id, randomElement)
|
|
||||||
}
|
|
||||||
|
|
||||||
for (seq <- 0 until count) yield randomOperation(requester, seq)
|
|
||||||
}
|
|
||||||
|
|
||||||
def referenceReplies(operations: Seq[Operation]): Seq[OperationReply] = {
|
|
||||||
var referenceSet = Set.empty[Int]
|
|
||||||
def replyFor(op: Operation): OperationReply = op match {
|
|
||||||
case Insert(_, seq, elem) =>
|
|
||||||
referenceSet = referenceSet + elem
|
|
||||||
OperationFinished(seq)
|
|
||||||
case Remove(_, seq, elem) =>
|
|
||||||
referenceSet = referenceSet - elem
|
|
||||||
OperationFinished(seq)
|
|
||||||
case Contains(_, seq, elem) =>
|
|
||||||
ContainsResult(seq, referenceSet(elem))
|
|
||||||
}
|
|
||||||
|
|
||||||
for (op <- operations) yield replyFor(op)
|
|
||||||
}
|
|
||||||
|
|
||||||
val requester = TestProbe()
|
|
||||||
val topNode = system.actorOf(Props[BinaryTreeSet])
|
|
||||||
val count = 1000
|
|
||||||
|
|
||||||
val ops = randomOperations(requester.ref, count)
|
|
||||||
val expectedReplies = referenceReplies(ops)
|
|
||||||
|
|
||||||
ops foreach { op =>
|
|
||||||
topNode ! op
|
|
||||||
if (rnd.nextDouble() < 0.1) topNode ! GC
|
|
||||||
}
|
|
||||||
receiveN(requester, ops, expectedReplies)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
99
src/test/scala/example/ListsSuite.scala
Normal file
99
src/test/scala/example/ListsSuite.scala
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
package example
|
||||||
|
|
||||||
|
import org.junit._
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class implements a JUnit test suite for the methods in object
|
||||||
|
* `Lists` that need to be implemented as part of this assignment. A test
|
||||||
|
* suite is simply a collection of individual tests for some specific
|
||||||
|
* component of a program.
|
||||||
|
*
|
||||||
|
* To run this test suite, start "sbt" then run the "test" command.
|
||||||
|
*/
|
||||||
|
class ListsSuite {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests are written using the @Test annotation
|
||||||
|
*
|
||||||
|
* The most common way to implement a test body is using the method `assert`
|
||||||
|
* which tests that its argument evaluates to `true`. So one of the simplest
|
||||||
|
* successful tests is the following:
|
||||||
|
*/
|
||||||
|
@Test def `one plus one is two (0pts)`: Unit =
|
||||||
|
assert(1 + 1 == 2)
|
||||||
|
|
||||||
|
@Test def `one plus one is three (0pts)?`: Unit =
|
||||||
|
assert(1 + 1 != 3) // This assertion fails! Go ahead and fix it.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One problem with the previous (failing) test is that JUnit will
|
||||||
|
* only tell you that a test failed, but it will not tell you what was
|
||||||
|
* the reason for the failure. The output looks like this:
|
||||||
|
*
|
||||||
|
* {{{
|
||||||
|
* [info] - one plus one is three? *** FAILED ***
|
||||||
|
* }}}
|
||||||
|
*
|
||||||
|
* This situation can be improved by using a Assert.assertEquals
|
||||||
|
* (this is only possible in JUnit). So if you
|
||||||
|
* run the next test, JUnit will show the following output:
|
||||||
|
*
|
||||||
|
* {{{
|
||||||
|
* [info] - details why one plus one is not three *** FAILED ***
|
||||||
|
* [info] 2 did not equal 3 (ListsSuite.scala:67)
|
||||||
|
* }}}
|
||||||
|
*
|
||||||
|
* We recommend to always use the Assert.assertEquals equality operator
|
||||||
|
* when writing tests.
|
||||||
|
*/
|
||||||
|
@Test def `details why one plus one is not three (0pts)`: Unit =
|
||||||
|
Assert.assertEquals(2, 1 + 1) // Fix me, please!
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exceptional behavior of a methods can be tested using a try/catch
|
||||||
|
* and a failed assertion.
|
||||||
|
*
|
||||||
|
* In the following example, we test the fact that the method `intNotZero`
|
||||||
|
* throws an `IllegalArgumentException` if its argument is `0`.
|
||||||
|
*/
|
||||||
|
@Test def `intNotZero throws an exception if its argument is 0`: Unit =
|
||||||
|
try
|
||||||
|
intNotZero(0)
|
||||||
|
Assert.fail("No exception has been thrown")
|
||||||
|
catch
|
||||||
|
case e: IllegalArgumentException => ()
|
||||||
|
|
||||||
|
def intNotZero(x: Int): Int =
|
||||||
|
if x == 0 then throw new IllegalArgumentException("zero is not allowed")
|
||||||
|
else x
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Now we finally write some tests for the list functions that have to be
|
||||||
|
* implemented for this assignment. We fist import all members of the
|
||||||
|
* `List` object.
|
||||||
|
*/
|
||||||
|
import Lists._
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We only provide two very basic tests for you. Write more tests to make
|
||||||
|
* sure your `sum` and `max` methods work as expected.
|
||||||
|
*
|
||||||
|
* In particular, write tests for corner cases: negative numbers, zeros,
|
||||||
|
* empty lists, lists with repeated elements, etc.
|
||||||
|
*
|
||||||
|
* It is allowed to have multiple `assert` statements inside one test,
|
||||||
|
* however it is recommended to write an individual `test` statement for
|
||||||
|
* every tested aspect of a method.
|
||||||
|
*/
|
||||||
|
@Test def `sum of a few numbers (10pts)`: Unit =
|
||||||
|
assert(sum(List(1,2,0)) == 3)
|
||||||
|
|
||||||
|
@Test def `max of a few numbers (10pts)`: Unit =
|
||||||
|
assert(max(List(3, 7, 2)) == 7)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Rule def individualTestTimeout = new org.junit.rules.Timeout(1000)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user