Created
October 28, 2015 22:06
-
-
Save michaellihs/e4a028818db939dc3109 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.ch.lihsmi.collections | |
import spock.lang.* | |
import ch.lihsmi.collections.ArrayStack | |
class EmptyStack extends Specification { | |
def stack = new ArrayStack<Object>() | |
def "isEmpty returns true on empty stack"() { | |
expect: | |
stack.isEmpty() | |
} | |
def "pop throws expected exception"() { | |
when: | |
stack.pop() | |
then: | |
thrown(ch.lihsmi.collections.EmptyStackException) | |
} | |
def "top throws expected exception"() { | |
when: | |
stack.top() | |
then: | |
thrown(ch.lihsmi.collections.EmptyStackException) | |
} | |
def "push adds an element to the stack"() { | |
String element = "element" | |
when: | |
stack.push(element) | |
then: | |
!stack.isEmpty() | |
stack.top() == element | |
} | |
def "topAndPop throws expected exception"() { | |
when: | |
stack.topAndPop() | |
then: | |
thrown(ch.lihsmi.collections.EmptyStackException) | |
} | |
} | |
class StackWithOneElement extends Specification { | |
def stack = new ArrayStack<Object>() | |
def setup() { | |
stack.push("first") | |
} | |
def "isEmpty"() { | |
expect: | |
!stack.isEmpty() | |
} | |
def "pop"() { | |
when: | |
stack.pop() | |
then: | |
stack.isEmpty() | |
} | |
def "topAndPop"() { | |
when: | |
def element = stack.topAndPop() | |
then: | |
element == "first" | |
stack.isEmpty() | |
} | |
def "push"() { | |
when: | |
stack.push("second") | |
then: | |
stack.top() == "second" | |
!stack.empty | |
} | |
} | |
class StackWithThreeElements extends Specification { | |
def stack = new ArrayStack<Object>() | |
def setup() { | |
stack.push("first") | |
stack.push("second") | |
stack.push("third") | |
} | |
def "isEmpty"() { | |
expect: | |
!stack.isEmpty() | |
} | |
def "top"() { | |
expect: | |
stack.top() == "third" | |
} | |
def "pop"() { | |
expect: | |
stack.pop() | |
!stack.isEmpty() | |
stack.pop() | |
!stack.isEmpty() | |
stack.pop() | |
stack.isEmpty() | |
} | |
def "topAndPop"() { | |
expect: | |
stack.topAndPop() == "third" | |
!stack.isEmpty() | |
stack.topAndPop() == "second" | |
!stack.isEmpty() | |
stack.topAndPop() == "first" | |
stack.isEmpty() | |
} | |
def "push"() { | |
when: | |
stack.push("fourth") | |
then: | |
stack.top() == "fourth" | |
!stack.isEmpty() | |
} | |
} | |
class StackWithMoreElementsThanCapacity extends Specification { | |
def stack = new ArrayStack<Integer>() | |
def setup() { | |
(1..50).toList().each { | |
stack.push(it) | |
} | |
} | |
def "topAndPop"() { | |
(1..50).toList().reverse().each { | |
expect: | |
!stack.isEmpty() | |
stack.pop() == it | |
} | |
expect: | |
stack.isEmpty() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment