Skip to content

Commit

Permalink
Merge pull request #20 from lucproglangcourse/master
Browse files Browse the repository at this point in the history
aded spec for the different versions of fakeps, fixes #18
  • Loading branch information
klaeufer committed Mar 7, 2016
2 parents c93a7f2 + 83cb5be commit f86b6f2
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/test/scala/common/TreeBuilderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package common
import org.scalatest.WordSpec

/** Tests for the tree-building algorithm. */
trait TreeBuilderSpec extends WordSpec with TreeBuilder {
abstract class TreeBuilderSpec(label: String) extends WordSpec with TreeBuilder {

"The tree builder" when {
"The " + label + " tree builder" when {
"given an empty list of processes" should {
"build the correct process tree" in {
assert(buildTree(Iterator.empty) == TreeFixtures.empty)
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/fakeps/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import scala.util.Try
object Main extends App {

val arg = Try { args(0).toInt }
if (arg.isFailure) {
Console.err.println("usage: fakeps n (where n = number of process table entries)")
if (arg.isFailure || arg.get < 1) {
Console.err.println("usage: fakeps n (where n > 0 = number of process table entries)")
System.exit(1)
}

Expand Down
31 changes: 31 additions & 0 deletions src/test/scala/fakeps/Spec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package edu.luc.etl.osdi.processtree.scala
package fakeps

import org.scalatest.FunSuite

/** Tests for the fakeps algorithms. */
class Spec extends FunSuite {

val sizes = Seq(10, 100, 1000, 10000)

type FakePS = Int => Iterator[(Int, Int)]

def isFlattenedTree(i: Iterator[(Int, Int)]): Boolean = {
val ps = i.toMap
(ps.get(1) == Some(0)) && (ps - 1).values.forall(ps.contains(_))
}

def testFakePs(f: FakePS, label: String): Unit =
test(label + " should return a proper flattened ps tree") {
for (s <- sizes)
assert(isFlattenedTree(f(s)))
}

testFakePs(fakePsMutable, "fakePsMutable")
testFakePs(fakePsFold, "fakePsFold")
testFakePs(fakePsFoldSlow, "fakePsFoldSlow")
testFakePs(fakePsArray, "fakePsArray")
testFakePs(fakePsArrayPar, "fakePsArrayPar")
testFakePs(fakePsArrayTrie, "fakePsArrayTrie")
testFakePs(fakePsArraySTM, "fakePsArraySTM")
}
11 changes: 8 additions & 3 deletions src/test/scala/fakeps/fakeps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import scala.collection.mutable.ArrayBuffer
import scala.util.Random
import common.Process

/**
* Utility methods for generating a fake list of processes of the specified length.
*/
/** Utility methods for generating a fake list of processes of the specified length. */
package object fakeps {

/**
Expand All @@ -15,6 +13,7 @@ package object fakeps {
* because of a bug in Map.+(vararg).
*/
def fakePsFoldSlow(n: Int): Iterator[(Int, Int)] = reverseEdges {
require { n > 0 }
(2 to n).foldLeft {
Map(0 -> Seq(1), 1 -> Seq.empty)
} { (ps, nextPid) =>
Expand All @@ -28,6 +27,7 @@ package object fakeps {
* using an immutable implementation.
*/
def fakePsFold(n: Int): Iterator[(Int, Int)] = reverseEdges {
require { n > 0 }
(2 to n).foldLeft {
Map(0 -> Seq(1), 1 -> Seq.empty)
} { (ps, nextPid) =>
Expand All @@ -41,6 +41,7 @@ package object fakeps {
* using a mutable implementation.
*/
def fakePsMutable(n: Int): Iterator[(Int, Int)] = reverseEdges {
require { n > 0 }
import scala.collection.mutable.Map
val ps = Map(0 -> ArrayBuffer(1), 1 -> ArrayBuffer.empty[Int])
(2 to n) foreach { nextPid =>
Expand All @@ -56,6 +57,7 @@ package object fakeps {
* using a mutable implementation.
*/
def fakePsArray(n: Int): Iterator[(Int, Int)] = {
require { n > 0 }
val ps = Vector.fill(n + 1)(ArrayBuffer.empty[Int])
ps(0) += 1
(2 to n) foreach { nextPid =>
Expand All @@ -71,6 +73,7 @@ package object fakeps {
* concurrent queue (from Java).
*/
def fakePsArrayPar(n: Int): Iterator[(Int, Int)] = {
require { n > 0 }
import java.util.concurrent.ConcurrentLinkedQueue
import scala.collection.JavaConversions._
val ps = Vector.fill(n + 1)(new ConcurrentLinkedQueue[Int])
Expand All @@ -87,6 +90,7 @@ package object fakeps {
* using a mutable implementation with a parallel range and a lock-free trie.
*/
def fakePsArrayTrie(n: Int): Iterator[(Int, Int)] = {
require { n > 0 }
import scala.collection.concurrent.TrieMap
val ps = Vector.fill(n + 1)(TrieMap.empty[Int, Unit])
ps(0) += (1 -> (()))
Expand All @@ -102,6 +106,7 @@ package object fakeps {
* using a mutable implementation with a parallel range and STM.
*/
def fakePsArraySTM(n: Int): Iterator[(Int, Int)] = {
require { n > 0 }
import scala.concurrent.stm._
val ps = Vector.fill(n + 1)(TSet.empty[Int])
atomic { implicit tx => ps(0) += 1 }
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/fold/Spec.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.luc.etl.osdi.processtree.scala
package fold

class Spec extends common.TreeBuilderSpec with FoldTreeBuilder
class Spec extends common.TreeBuilderSpec("fold") with FoldTreeBuilder
2 changes: 1 addition & 1 deletion src/test/scala/groupby/Spec.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.luc.etl.osdi.processtree.scala
package groupby

class Spec extends common.TreeBuilderSpec with GroupByTreeBuilder
class Spec extends common.TreeBuilderSpec("groupby") with GroupByTreeBuilder
2 changes: 1 addition & 1 deletion src/test/scala/mutable/Spec.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.luc.etl.osdi.processtree.scala
package mutable

class Spec extends common.TreeBuilderSpec with MutableTreeBuilder
class Spec extends common.TreeBuilderSpec("mutable") with MutableTreeBuilder

0 comments on commit f86b6f2

Please sign in to comment.