Advertisement
Guest User

Untitled

a guest
Sep 29th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.94 KB | None | 0 0
  1. import Solution._
  2. import cmpsci220._
  3. import cmpsci220.hw._
  4. import cmpsci220.hw.measurement._
  5.  
  6. class TrivialTestSuite extends org.scalatest.FunSuite {
  7.  
  8.     test("The solution object must be defined") {
  9.         val obj : cmpsci220.hw.measurement.MeasurementFunctions = Solution
  10.     }
  11.     test("the revOrder outputs") {
  12.         val nums = Solution.revOrder(5)
  13.         assert(nums === cmpsci220.List(5,4,3,2,1))
  14.     }
  15.     test("revOrder must output Empty() on 0 call") {
  16.         val nums = revOrder(0)
  17.         assert (nums === cmpsci220.Empty())
  18.     }
  19.     test("time function should return accurate ms") {
  20.         val nums = Solution.time((n:Int) => Thread.sleep(n), 10)
  21.         assert(nums === 10)
  22.     }
  23.     test("basic test of average time") {
  24.         val nums = Solution.averageTime(10, (n:Int)=>Thread.sleep(n), 10)
  25.         assert(nums === 10)
  26.     }
  27.     test("basic test of random ints") {
  28.         val nums = Solution.randomInts(5)
  29.         assert (cmpsci220.length(nums) === 5)
  30.     }
  31.  
  32.     test("THIS TEST DALE") {
  33.     val result = cmpsci220.hw.isMemberAllOrdLst(List(6,2,3,4,5), insertAllOrdLst(List(6,2,3,4,5)))
  34.     assert(result === true)
  35.     }
  36.  
  37.     /**
  38.      * Calculates the average time needed to insert values into the set
  39.      *
  40.      * @param insertAll a function that inserts a list of values into an empty set
  41.      * @param trials the number of trials to run
  42.      * @param values the list of values to insert
  43.      * @returns A pair (n, t), where n is the number of values and t is the
  44.      *          average time needed to insert all values, divided by the
  45.      *          number of values
  46.      */
  47.     def timeInsertAll[A](insertAll: List[Int] => A, trials: Int)(values: List[Int]): (Double, Double) = {
  48.       val n = length(values).toDouble
  49.       val t = averageTime(trials, insertAll, values)
  50.       (n, t / n)
  51.     }
  52.  
  53.     test("timing insertAllAVL on random input") {
  54.       // You may need to tweak this data to suit your computer
  55.       val data = map((x: Int) => randomInts(math.pow(2, x + 1).toInt), revOrder(16))
  56.  
  57.       val timing = map(timeInsertAll(insertAllAVL, 5), data)
  58.       println(timing)
  59.     }
  60.  
  61.     test("timing insertAllBST on ordered input") {
  62.       // You may need to tweak this data to suit your computer
  63.       val data = map((x: Int) => revOrder(math.pow(2, x + 1).toInt), revOrder(10))
  64.       val timing = map(timeInsertAll(insertAllBST, 5), data)
  65.  
  66.       val line = linearRegression(timing)
  67.       assert(line.rSquared >= 0.85)
  68.     }
  69.  
  70.     def isNonZeroTime(pt: (Double, Double)): Boolean = pt._2 != 0
  71.  
  72.     test("timing insertAllAVL on ordered input") {
  73.       // You may need to tweak this data to suit your computer
  74.       val data = map((x: Int) => revOrder(math.pow(2, x + 1).toInt), revOrder(14))
  75.  
  76.       val timing = map(timeInsertAll(insertAllAVL, 5), data)
  77.       // Remove points with 0 as the y-coordinate, so that log is defined
  78.       val zeroesRemoved = filter(isNonZeroTime, timing)
  79.       // Put the X-axis on a log scale, so that we can fit a line
  80.       val logScaleX = map((xy: (Double, Double)) => (math.log(xy._1), xy._2), zeroesRemoved)
  81.  
  82.       val line = linearRegression(logScaleX)
  83.       assert(line.rSquared >= 0.85)
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement