test: LinkedListStackTest (#5366)

This commit is contained in:
Alex Klymenko
2024-08-23 13:28:47 +02:00
committed by GitHub
parent 844aeaf701
commit a6fcbf585f

View File

@ -0,0 +1,71 @@
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.assertTrue;
import java.util.NoSuchElementException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class LinkedListStackTest {
private LinkedListStack stack;
@BeforeEach
public void setUp() {
stack = new LinkedListStack();
}
@Test
public void testPushAndPeek() {
stack.push(1);
stack.push(2);
stack.push(3);
assertEquals(3, stack.peek());
assertEquals(3, stack.getSize());
}
@Test
public void testPop() {
stack.push(1);
stack.push(2);
stack.push(3);
assertEquals(3, stack.pop());
assertEquals(2, stack.pop());
assertEquals(1, stack.pop());
assertTrue(stack.isEmpty());
}
@Test
public void testPopEmptyStack() {
org.junit.jupiter.api.Assertions.assertThrows(NoSuchElementException.class, () -> stack.pop());
}
@Test
public void testPeekEmptyStack() {
org.junit.jupiter.api.Assertions.assertThrows(NoSuchElementException.class, () -> stack.peek());
}
@Test
public void testIsEmpty() {
assertTrue(stack.isEmpty());
stack.push(1);
assertFalse(stack.isEmpty());
stack.pop();
assertTrue(stack.isEmpty());
}
@Test
public void testToString() {
stack.push(1);
stack.push(2);
stack.push(3);
assertEquals("3->2->1", stack.toString());
}
}