import org.specs._ import org.scalacheck._ import com.codecommit.collection.Vector object VectorSpecs extends Specification with Scalacheck { import Prop._ val vector = Vector[Int]() "persistent vector" should { "have infinite bounds" in { vector.length mustEqual 0 // finite length val prop = property { i: Int => // infinite bounds try { vector(i) == 0 } catch { case _:IndexOutOfBoundsException => i < 0 } } prop must pass } "store a single element" in { val prop = property { i: Int => (vector(0) = i)(0) == i } prop must pass } "replace single element" in { val vector = Vector("daniel", "chris", "joseph", "renee") val prop = property { i: Int => if (i < 0) true else { val newVector = (vector(i) = "test")(i) = "newTest" newVector(i) == "newTest" } } prop must pass } "store multiple elements in order" in { val prop = property { list: List[Int] => val newVector = list.foldLeft(vector) { _ + _ } val res = for (i <- 0 until list.length) yield newVector(i) == list(i) res forall { _ == true } } prop must pass } "store lots of elements" in { val LENGTH = 100000 val vector = (0 until LENGTH).foldLeft(Vector[Int]()) { _ + _ } vector.length mustEqual LENGTH for (i <- 0 until LENGTH) { vector(i) mustEqual i } } "store at arbitrary points" in { val vector = Vector(1, 2, 3, 4, 5) val prop = property { others: List[(Int, Int)] => val (newVector, resMap) = others.foldLeft(vector, Map[Int, Int]()) { (inTuple, tuple) => val (i, value) = tuple val (vec, map) = inTuple if (i < 0) (vec, map) else (vec(i) = value, map + (i -> value)) } val res = for { (i, _) <- others } yield if (i < 0) true else newVector(i) == resMap(i) res forall { _ == true } } prop must pass } "implement foldLeft" in { val nums = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9) nums.foldLeft(0) { _ + _ } mustEqual 45 } "implement map" in { val strs = Vector("1", "2", "3", "4", "5", "6", "7", "8", "9") val nums = strs map { _.toInt } nums mustEqual Vector(1, 2, 3, 4, 5, 6, 7, 8, 9) } "implement equals" in { val prop = property { list: List[Int] => val vecA = list.foldLeft(new Vector[Int]) { _ + _ } val vecB = list.foldLeft(new Vector[Int]) { _ + _ } vecA == vecB } prop must pass } } }