Skip to content

Instantly share code, notes, and snippets.

@NightRa
Created September 22, 2013 07:54
Show Gist options
  • Save NightRa/6657765 to your computer and use it in GitHub Desktop.
Save NightRa/6657765 to your computer and use it in GitHub Desktop.
Some very exhaustive tests for Deque
//Created By Ilan Godik
package elementaryDataStructures
import org.scalatest.{GivenWhenThen, Matchers, FlatSpec}
class DequeTest extends FlatSpec with Matchers with GivenWhenThen {
"A Deque" should "throw an NullPointerException when calling addFirst with null" in {
intercept[NullPointerException] {
val model = new Deque[String]
model.addFirst(null)
}
}
it should "throw an NullPointerException when calling addLast with null" in {
intercept[NullPointerException] {
val model = new Deque[String]
model.addLast(null)
}
}
it should "add and remove correctly to start" in {
Given("a Deque")
val model = new Deque[Int]
model.addFirst(2)
model.addFirst(3)
When("we add an element to first")
model.addFirst(5)
Then("calling removeFirst should return that element.")
assert(model.removeFirst === 5)
}
it should "add and remove correcty to end" in{
Given("a Deque")
val model = new Deque[Int]
model.addLast(5)
model.addFirst(3)
When("we add an element to end")
model.addLast(1)
Then("calling removeLast should return that element.")
assert(model.removeLast === 1)
}
it should "its size incremented when adding an element" in{
Given("a Deque")
val model = new Deque[Int]
When("we add an element")
model.addFirst(5)
Then("it's size should increment")
assert(model.size === 1)
}
it should "return the elements in order if we call removeLast after adding with addFirst" in{
val model = new Deque[Int]
for(i <- 1 to 5){
model.addFirst(i)
}
for(i <- 1 to 5){
assert(model.removeLast() === i)
}
}
it should "behave correctly when jumping between emptied states" in{
Given("an Empty Deque")
val model = new Deque[Int]
When("we add an element in start")
model.addFirst(5)
Then("when we removeFirst, we should get that element")
assert(model.removeFirst() === 5)
When("we add an element to end")
model.addLast(7)
Then("when we removeFirst, we should get that element.")
assert(model.removeFirst() === 7)
}
"An empty Deque" should "return true in isEmpty" in {
val model = new Deque[String]
assert(model.isEmpty)
}
it should "not be empty when we add an element" in {
val model = new Deque[String]
model.addFirst("hello")
assert(!model.isEmpty)
}
it should "throw NoSuchElementException if we remove from start" in{
Given("an empty Deque")
val model = new Deque[String]
When("calling removeFirst")
Then("it should throw NoSuchElementException")
intercept[NoSuchElementException]{
model.removeFirst()
}
}
it should "throw NoSuchElementException if we remove from end" in{
Given("an empty Deque")
val model = new Deque[String]
When("calling removeLast")
Then("it should throw NoSuchElementException")
intercept[NoSuchElementException]{
model.removeLast()
}
}
it should "have size 0" in{
Given("an empty Deque")
val model = new Deque[String]
When("calling size")
Then("it should return 0")
assert(model.size === 0)
}
it should "be returned by the no argument constructor" in{
When("constructing from the no argument constructor")
Then("we should get an empty Deque")
assert(new Deque[Int].isEmpty)
}
"DequeIterator" should "throw UnsupportedOperationException if we call remove on it" in{
Given("a DequeIterator")
val model = new Deque[Int]
model.addFirst(5)
model.addLast(7)
val iterator = model.iterator
When("calling remove()")
Then("it should trow UnsupportedOperationException")
intercept[UnsupportedOperationException]{
iterator.remove()
}
}
it should "be empty for an empty Deque" in{
Given("an empty Deque")
val model = new Deque[String]
When("we get it's iterator")
val iterator = model.iterator
Then("it should be empty")
assert(iterator.hasNext === false)
}
it should "not be empty for a non empty Deque" in{
Given("a non empty Deque")
val model = new Deque[Int]
model.addFirst(5)
When("we get it's iterator")
val iterator = model.iterator
Then("it shouldn't be empty")
assert(iterator.hasNext === true)
}
it should "iterate over elements from start to end" in{
Given("a non empty Deque")
val model = new Deque[Int]
model.addFirst(5)
model.addFirst(7)
model.addLast(1)
// 7,5,1
When("we iterate using it's iterator")
val iterator = model.iterator
Then("we should iterate from start to end")
assert(iterator.next === 7)
assert(iterator.next === 5)
assert(iterator.next === 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment