Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for NodeStack and rework #6009

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 26 additions & 120 deletions src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,159 +3,65 @@
/**
* Implementation of a stack using nodes. Unlimited size, no arraylist.
*
* @author Kyler Smith, 2017
*/
public class NodeStack<Item> {

/**
* Entry point for the program.
*/
public static void main(String[] args) {
NodeStack<Integer> stack = new NodeStack<Integer>();

stack.push(3);
stack.push(4);
stack.push(5);
System.out.println("Testing :");
stack.print(); // prints : 5 4 3

Integer x = stack.pop(); // x = 5
stack.push(1);
stack.push(8);
Integer y = stack.peek(); // y = 8
System.out.println("Testing :");
stack.print(); // prints : 8 1 4 3

System.out.println("Testing :");
System.out.println("x : " + x);
System.out.println("y : " + y);
}

/**
* Information each node should contain.
*
* @value data : information of the value in the node
* @value head : the head of the stack
* @value next : the next value from this node
* @value previous : the last value from this node
* @value size : size of the stack
*/
private Item data;
private NodeStack<Item> previous;
private NodeStack<Item> head;
private int size = 0;

private static NodeStack<?> head;
private NodeStack<?> previous;
private static int size = 0;

/**
* Constructors for the NodeStack.
*/
public NodeStack() {
}

private NodeStack(Item item) {
this.data = item;
}

/**
* Put a value onto the stack.
*
* @param item : value to be put on the stack.
*/
public void push(Item item) {
NodeStack<Item> newNs = new NodeStack<Item>(item);
NodeStack<Item> newNode = new NodeStack<>(item);

if (this.isEmpty()) {
NodeStack.setHead(new NodeStack<>(item));
newNs.setNext(null);
newNs.setPrevious(null);
if (isEmpty()) {
head = newNode;
} else {
newNs.setPrevious(NodeStack.head);
NodeStack.head.setNext(newNs);
NodeStack.setHead(newNs);
newNode.previous = head;
head = newNode;
}

NodeStack.setSize(NodeStack.getSize() + 1);
size++;
}

/**
* Value to be taken off the stack.
*
* @return item : value that is returned.
*/
public Item pop() {
Item item = (Item) NodeStack.head.getData();

NodeStack.setHead(NodeStack.head.getPrevious());
NodeStack.head.setNext(null);

NodeStack.setSize(NodeStack.getSize() - 1);
if (isEmpty()) {
throw new IllegalStateException("Stack is empty, cannot peek element");
}

Item item = head.data;
head = head.previous;
size--;
return item;
}

/**
* Value that is next to be taken off the stack.
*
* @return item : the next value that would be popped off the stack.
*/
public Item peek() {
return (Item) NodeStack.head.getData();
if (isEmpty()) {
throw new IllegalStateException("Stack is empty, cannot peek element");
}
return head.data;
}

/**
* If the stack is empty or there is a value in.
*
* @return boolean : whether or not the stack has anything in it.
*/
public boolean isEmpty() {
return NodeStack.getSize() == 0;
return size == 0;
}

/**
* Returns the size of the stack.
*
* @return int : number of values in the stack.
*/
public int size() {
return NodeStack.getSize();
return size;
}

/**
* Print the contents of the stack in the following format.
*
* <p>
* x <- head (next out) y z <- tail (first in) . . .
*/
public void print() {
lorduke22 marked this conversation as resolved.
Show resolved Hide resolved
for (NodeStack<?> n = NodeStack.head; n != null; n = n.previous) {
System.out.println(n.getData().toString());
NodeStack<Item> current = head;
while (current != null) {
System.out.println(current.data + " ");
current = current.previous;
}
}

private static void setHead(NodeStack<?> ns) {
NodeStack.head = ns;
}

private void setNext(NodeStack<?> next) {
}

private NodeStack<?> getPrevious() {
return previous;
}

private void setPrevious(NodeStack<?> previous) {
this.previous = previous;
}

private static int getSize() {
return size;
}

private static void setSize(int size) {
NodeStack.size = size;
}

private Item getData() {
return this.data;
System.out.println();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.thealgorithms.datastructures.stacks;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class NodeStackTest {

private NodeStack<Integer> stack;
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();

@BeforeEach
void setUp() {
stack = new NodeStack<>();
System.setOut(new PrintStream(outputStreamCaptor));
}

@Test
void testPushAndPeek() {
stack.push(3);
assertEquals(3, stack.peek(), "Peek should return the last pushed item");
stack.push(4);
stack.push(5);
assertEquals(5, stack.peek(), "Peek should return the last pushed item");
}

@Test
void testPop() {
stack.push(5);
stack.push(7);
assertEquals(7, stack.pop(), "Pop should return the last pushed item");
assertEquals(5, stack.pop(), "Pop should return the next item in stack");
assertTrue(stack.isEmpty(), "Stack should be empty after popping all elements");
}

@Test
void testIsEmpty() {
assertTrue(stack.isEmpty(), "New stack should be empty");
stack.push(1);
assertFalse(stack.isEmpty(), "Stack should not be empty after push");
}

@Test
void testSize() {
assertEquals(0, stack.size(), "New stack should have size 0");
stack.push(10);
stack.push(20);
assertEquals(2, stack.size(), "Stack size should be 2 after two pushes");
}

@Test
void testPopOnEmptyStack() {
assertThrows(IllegalStateException.class, () -> { stack.pop(); });
}

@Test
void testPeekOnEmptyStack() {
assertThrows(IllegalStateException.class, () -> { stack.peek(); });
}

@Test
void testPrintEmptyStack() {
stack.print();
assertEquals("", outputStreamCaptor.toString().trim(), "The output of an empty stack should be an empty string.");
}
}