Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.autotest;
- import org.junit.Test;
- import java.util.ArrayList;
- import static org.junit.Assert.*;
- public class TestStackAr {
- @Test
- public void aNewStackShouldBeEmpty() {
- StackAr stack = new StackAr();
- boolean isEmpty = stack.isEmpty();
- assertTrue(isEmpty);
- }
- @Test
- public void aNewStackShouldntBeFull() {
- StackAr stack = new StackAr();
- boolean isFull = stack.isFull();
- assertFalse(isFull);
- }
- @Test
- public void anEmptyStackShouldHaveSizeZero() {
- StackAr stack = new StackAr();
- assertEquals(0, stack.size());
- }
- @Test
- public void stackSizeShouldIncreaseWhenItemsAreAdded() {
- StackAr stack = new StackAr();
- stack.push(new Object());
- assertEquals(1, stack.size());
- }
- @Test
- public void stackCapacityShouldBeTheOnePassedToTheConstructor() {
- StackAr stack = new StackAr(1);
- stack.push(new Object());
- assertTrue(stack.isFull());
- }
- @Test(expected = IllegalArgumentException.class)
- public void aStackCantHaveNegativeCapacity() {
- new StackAr(-1);
- }
- @Test
- public void stackShouldntBeFullWhenThereIsStillSpace() {
- StackAr stack = new StackAr(4);
- stack.push(new Object());
- assertFalse(stack.isFull());
- }
- @Test(expected = IllegalStateException.class)
- public void exceptionShouldBeThrownWhenCapacityIsExceeded() {
- StackAr stack = new StackAr(1);
- stack.push(new Object());
- stack.push(new Object());
- }
- @Test(expected = IllegalStateException.class)
- public void exceptionShouldBeThrownWhenCheckingTheTopOfAnEmptyStack() {
- StackAr stack = new StackAr();
- stack.top();
- }
- @Test
- public void stackSizeShouldStayTheSameWhenAskedForTheTop(){
- StackAr stack = new StackAr();
- stack.push(new Object());
- stack.top();
- assertEquals(1, stack.size());
- }
- @Test
- public void theStackTopShouldBeTheLastItemPushed(){
- StackAr stack = new StackAr();
- Object item = new Object();
- stack.push(item);
- Object top = stack.top();
- assertSame(item, top);
- }
- @Test
- public void stackSizeShouldDecreaseWhenItemsAreRemoved() {
- StackAr stack = new StackAr();
- stack.push(new Object());
- stack.pop();
- assertEquals(0, stack.size());
- }
- @Test(expected = IllegalStateException.class)
- public void exceptionShouldBeThrownWhenAnEmptyStackIsPopped() {
- StackAr stack = new StackAr();
- stack.pop();
- }
- @Test
- public void aPoppedItemShouldBeTheSameThatWasPushed(){
- StackAr stack = new StackAr();
- Object item = new Object();
- stack.push(item);
- Object popped = stack.pop();
- assertSame(item, popped);
- }
- @Test
- public void anItemShouldRemainUnchangedWhenANewItemIsPushedOnTopOfIt(){
- StackAr stack = new StackAr();
- Object item = new Object();
- stack.push(item);
- stack.push(new Object());
- stack.pop();
- assertSame(item, stack.top());
- }
- @Test
- public void twoDifferentStacksShouldHaveDifferentHashCodes(){
- StackAr stackOne = new StackAr();
- StackAr stackTwo = new StackAr();
- stackOne.push(new Object());
- stackTwo.push(new Object());
- assertNotEquals(stackOne.hashCode(), stackTwo.hashCode());
- }
- @Test
- public void twoEqualStacksShouldHaveEqualHashCodes(){
- StackAr stackOne = new StackAr();
- StackAr stackTwo = new StackAr();
- stackOne.push(new Object());
- stackTwo.push(stackOne.top());
- assertEquals(stackOne.hashCode(), stackTwo.hashCode());
- }
- @Test
- public void aStackShouldBeEqualToItself(){
- StackAr stack = new StackAr();
- stack.push(new Object());
- assertEquals(stack, stack);
- }
- @Test
- public void emptyStacksShouldBeEqual(){
- StackAr stackOne = new StackAr();
- StackAr stackTwo = new StackAr();
- assertEquals(stackOne, stackTwo);
- }
- @Test
- public void aStackShouldntBeEqualToNull(){
- StackAr stack = new StackAr();
- assertNotEquals(stack, null);
- }
- @Test
- public void aStackShouldntBeEqualToSomethingOtherThanAStack(){
- StackAr stack = new StackAr();
- assertNotEquals(stack, new ArrayList<>());
- }
- @Test
- public void stacksWithDifferentSizeShouldntBeEqual(){
- StackAr stackOne = new StackAr();
- StackAr stackTwo = new StackAr();
- stackOne.push("foo");
- stackOne.push("bar");
- stackTwo.push("foo");
- assertNotEquals(stackOne, stackTwo);
- }
- @Test
- public void stacksWithDifferentCapacityShouldntBeEqual(){
- StackAr stackOne = new StackAr(10);
- StackAr stackTwo = new StackAr(20);
- assertNotEquals(stackOne, stackTwo);
- }
- @Test
- public void stacksWithDifferentItemsShouldntBeEqual(){
- StackAr stackOne = new StackAr();
- StackAr stackTwo = new StackAr();
- stackOne.push(new Object());
- stackTwo.push(new Object());
- assertNotEquals(stackOne, stackTwo);
- }
- @Test
- public void stacksWithTheSameItemsShouldBeEqual(){
- StackAr stackOne = new StackAr();
- StackAr stackTwo = new StackAr();
- stackOne.push(new Object());
- stackTwo.push(stackOne.top());
- assertEquals(stackOne, stackTwo);
- }
- @Test
- public void aStackWithAPoppedItemShouldntBeEqualToAstackWithIt(){
- StackAr stackOne = new StackAr();
- StackAr stackTwo = new StackAr();
- stackOne.push(new Object());
- stackTwo.push(stackOne.top());
- stackTwo.pop();
- assertNotEquals(stackOne, stackTwo);
- }
- @Test
- public void anEmptyStackToStringShouldBeProperlyFormatted(){
- StackAr stack = new StackAr();
- String print = stack.toString();
- assertEquals("[]", print);
- }
- @Test
- public void aStackWithOneItemToStringShouldBeProperlyFormatted(){
- StackAr stack = new StackAr();
- stack.push("foo");
- String print = stack.toString();
- assertEquals("[foo]", print);
- }
- @Test
- public void aStackWithMoreThanOneItemToStringShouldBeProperlyFormatted(){
- StackAr stack = new StackAr();
- stack.push("foo");
- stack.push("bar");
- String print = stack.toString();
- assertEquals("[foo,bar]", print);
- }
- @Test
- public void poppedItemsShouldntAppearInAStacksToString(){
- StackAr stack = new StackAr();
- stack.push("foo");
- stack.push("bar");
- stack.pop();
- String print = stack.toString();
- assertEquals("[foo]", print);
- }
- //MUTATION
- @Test
- public void capacityZeroShouldBeValid(){
- try {
- new StackAr(0);
- }catch (Exception e){
- fail("No exception expected");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement