package edd.util; import static org.junit.Assert.*; import java.util.NoSuchElementException; import org.junit.Test; /** * Pruebas de unidad para la clase Stack. */ public class StackTest { @Test public void test1() { Stack s = new Stack<>(); s.push(4); assertEquals(4, (int) s.peek()); s.push(8); assertEquals(8, (int) s.peek()); s.push(15); assertEquals(15, (int) s.peek()); s.push(16); assertEquals(16, (int) s.peek()); s.push(23); assertEquals(23, (int) s.peek()); s.push(42); } @Test public void test2() { Stack s = new Stack<>(); s.push("Doc"); s.push("Grumpy"); s.push("Happy"); s.push("Sleepy"); s.push("Bashful"); s.push("Sneezy"); assertEquals("Sneezy", s.peek()); assertEquals("Sneezy", s.pop()); assertEquals("Bashful", s.peek()); assertEquals("Bashful", s.pop()); assertEquals("Sleepy", s.peek()); assertEquals("Sleepy", s.pop()); assertEquals("Happy", s.peek()); assertEquals("Happy", s.pop()); assertEquals("Grumpy", s.peek()); assertEquals("Grumpy", s.pop()); assertEquals("Doc", s.peek()); assertEquals("Doc", s.pop()); try { s.pop(); fail(); } catch (NoSuchElementException e) { assertEquals("Stack Underflow", e.getMessage()); } try { s.peek(); fail(); } catch (NoSuchElementException e) { assertEquals("Stack Underflow", e.getMessage()); } } @Test public void test3() { Stack s = new Stack<>(); assertTrue(s.isEmpty()); assertEquals(0, s.size()); s.push(4.0); assertFalse(s.isEmpty()); assertEquals(1, s.size()); s.push(8.0); assertFalse(s.isEmpty()); assertEquals(2, s.size()); s.push(15.0); assertFalse(s.isEmpty()); assertEquals(3, s.size()); s.push(16.0); assertFalse(s.isEmpty()); assertEquals(4, s.size()); s.push(23.0); assertFalse(s.isEmpty()); assertEquals(5, s.size()); s.push(42.0); assertFalse(s.isEmpty()); assertEquals(6, s.size()); assertEquals(42.0, s.pop(), 0.0); assertFalse(s.isEmpty()); assertEquals(5, s.size()); assertEquals(23.0, s.pop(), 0.0); assertFalse(s.isEmpty()); assertEquals(4, s.size()); assertEquals(16.0, s.pop(), 0.0); assertFalse(s.isEmpty()); assertEquals(3, s.size()); assertEquals(15.0, s.pop(), 0.0); assertFalse(s.isEmpty()); assertEquals(2, s.size()); assertEquals(8.0, s.pop(), 0.0); assertFalse(s.isEmpty()); assertEquals(1, s.size()); assertEquals(4.0, s.pop(), 0.0); assertTrue(s.isEmpty()); assertEquals(0, s.size()); } @Test public void test4() { Stack s1 = new Stack<>(); Stack s2 = new Stack<>(); s1.push('a'); s1.push('b'); s1.push('c'); s1.push('d'); s1.push('e'); s1.push('f'); s2.push(s1.pop()); s2.push(s1.pop()); s2.push(s1.pop()); s2.push(s1.pop()); s2.push(s1.pop()); s2.push(s1.pop()); assertTrue(s1.isEmpty()); assertEquals('a', (char) s2.pop()); assertEquals('b', (char) s2.pop()); assertEquals('c', (char) s2.pop()); assertEquals('d', (char) s2.pop()); assertEquals('e', (char) s2.pop()); assertEquals('f', (char) s2.pop()); assertTrue(s2.isEmpty()); } }