From 865c883e4fa0d50f4d4edd1240716b1bd67672e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20=C3=81ngel=20Dausa?= Date: Fri, 5 Jan 2024 17:57:46 -0300 Subject: [PATCH 1/6] feat: Add stack isEmpty method. --- src/main/java/collections/stack/Stack.java | 11 ++++++++++ .../java/collections/stack/QueueTest.java | 20 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/main/java/collections/stack/Stack.java create mode 100644 src/test/java/collections/stack/QueueTest.java diff --git a/src/main/java/collections/stack/Stack.java b/src/main/java/collections/stack/Stack.java new file mode 100644 index 0000000..c8c67f6 --- /dev/null +++ b/src/main/java/collections/stack/Stack.java @@ -0,0 +1,11 @@ +package collections.stack; + +/** + * Simple FIFO Stack implementation backed by a circular Array + */ +public class Stack { + public boolean isEmpty() { + return true; + } + +} \ No newline at end of file diff --git a/src/test/java/collections/stack/QueueTest.java b/src/test/java/collections/stack/QueueTest.java new file mode 100644 index 0000000..55a05ba --- /dev/null +++ b/src/test/java/collections/stack/QueueTest.java @@ -0,0 +1,20 @@ +package collections.stack; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class StackTest { + + private Stack stack; + + @BeforeEach + void setup() { + stack = new Stack(); + } + + @Test + void newStack_IsEmpty() { + Assertions.assertTrue(stack.isEmpty()); + } +} From 50872478257512270b90d7a4351e3b23ba88bbf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20=C3=81ngel=20Dausa?= Date: Thu, 8 Feb 2024 14:22:13 -0300 Subject: [PATCH 2/6] refactor: rebase with master --- src/main/java/collections/stack/Stack.java | 38 ++++++++++++++--- .../java/collections/stack/StackTest.java | 41 +++++++++++++++++++ 2 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 src/test/java/collections/stack/StackTest.java diff --git a/src/main/java/collections/stack/Stack.java b/src/main/java/collections/stack/Stack.java index c8c67f6..035a48b 100644 --- a/src/main/java/collections/stack/Stack.java +++ b/src/main/java/collections/stack/Stack.java @@ -1,11 +1,39 @@ package collections.stack; -/** - * Simple FIFO Stack implementation backed by a circular Array - */ +import collections.exception.OverflowException; +import collections.exception.UnderflowException; + public class Stack { + private static final int CAPACITY = 10; + + private final int[] elements = new int[CAPACITY]; + private int size = 0; + private int headIdx = CAPACITY; + public boolean isEmpty() { - return true; + return size == 0; } -} \ No newline at end of file + private boolean isFull() { + return size == elements.length; + } + + public void push(int element) { + if (isFull()) { + throw new OverflowException(); + } + + headIdx--; + size++; + elements[headIdx] = element; + } + + public int pop() { + if (isEmpty()) { + throw new UnderflowException(); + } + + size--; + return elements[headIdx--]; + } +} diff --git a/src/test/java/collections/stack/StackTest.java b/src/test/java/collections/stack/StackTest.java new file mode 100644 index 0000000..7d4a187 --- /dev/null +++ b/src/test/java/collections/stack/StackTest.java @@ -0,0 +1,41 @@ +package collections.stack; + +import collections.exception.OverflowException; +import collections.exception.UnderflowException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class StackTest { + + private Stack stack; + + @BeforeEach + void setup() { + stack = new Stack(); + } + + @Test + void newStack_IsEmpty() { + Assertions.assertTrue(stack.isEmpty()); + } + + @Test + void afterOnePush_IsNotEmpty() { + stack.push(0); + Assertions.assertFalse(stack.isEmpty()); + } + + @Test + void willThrowUnderflowException_WhenEmptyStackIsDequeued() { + Assertions.assertThrowsExactly(UnderflowException.class, () -> stack.pop()); + } + + @Test + void afterOnePushAndOnePop_WillBeEmpty() { + stack.push(0); + stack.pop(); + Assertions.assertTrue(stack.isEmpty()); + } + +} From 531cf81882f350f952b5e974bb75a0c0ab61a05a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20=C3=81ngel=20Dausa?= Date: Sat, 25 Nov 2023 20:03:04 -0300 Subject: [PATCH 3/6] fix: remove unsed import. --- src/test/java/collections/stack/StackTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/collections/stack/StackTest.java b/src/test/java/collections/stack/StackTest.java index 7d4a187..d8e7575 100644 --- a/src/test/java/collections/stack/StackTest.java +++ b/src/test/java/collections/stack/StackTest.java @@ -1,6 +1,5 @@ package collections.stack; -import collections.exception.OverflowException; import collections.exception.UnderflowException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; From 45410e14fbb0f8713a9c422aa678b1c99593d838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20=C3=81ngel=20Dausa?= Date: Thu, 8 Feb 2024 14:14:46 -0300 Subject: [PATCH 4/6] refactor: Use only one index --- src/main/java/collections/stack/Stack.java | 8 ++------ src/test/java/collections/stack/StackTest.java | 7 +++++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/collections/stack/Stack.java b/src/main/java/collections/stack/Stack.java index 035a48b..3ef7f99 100644 --- a/src/main/java/collections/stack/Stack.java +++ b/src/main/java/collections/stack/Stack.java @@ -8,7 +8,6 @@ public class Stack { private final int[] elements = new int[CAPACITY]; private int size = 0; - private int headIdx = CAPACITY; public boolean isEmpty() { return size == 0; @@ -23,9 +22,7 @@ public void push(int element) { throw new OverflowException(); } - headIdx--; - size++; - elements[headIdx] = element; + elements[size++] = element; } public int pop() { @@ -33,7 +30,6 @@ public int pop() { throw new UnderflowException(); } - size--; - return elements[headIdx--]; + return elements[--size]; } } diff --git a/src/test/java/collections/stack/StackTest.java b/src/test/java/collections/stack/StackTest.java index d8e7575..8b49614 100644 --- a/src/test/java/collections/stack/StackTest.java +++ b/src/test/java/collections/stack/StackTest.java @@ -37,4 +37,11 @@ void afterOnePushAndOnePop_WillBeEmpty() { Assertions.assertTrue(stack.isEmpty()); } + @Test + void afterPushingXAndY_PopWillReturnYAndX() { + stack.push(1); + stack.push(2); + Assertions.assertEquals(2, stack.pop()); + Assertions.assertEquals(1, stack.pop()); + } } From 256c1531c6280bc6599148b8aa4855fe5b964171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20=C3=81ngel=20Dausa?= Date: Thu, 8 Feb 2024 14:23:23 -0300 Subject: [PATCH 5/6] refactor: remove unused class --- .../java/collections/stack/QueueTest.java | 20 ------------------- 1 file changed, 20 deletions(-) delete mode 100644 src/test/java/collections/stack/QueueTest.java diff --git a/src/test/java/collections/stack/QueueTest.java b/src/test/java/collections/stack/QueueTest.java deleted file mode 100644 index 55a05ba..0000000 --- a/src/test/java/collections/stack/QueueTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package collections.stack; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class StackTest { - - private Stack stack; - - @BeforeEach - void setup() { - stack = new Stack(); - } - - @Test - void newStack_IsEmpty() { - Assertions.assertTrue(stack.isEmpty()); - } -} From 14cecb0ace74d16f1c3284c6786013c6de2082ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20=C3=81ngel=20Dausa?= Date: Thu, 8 Feb 2024 14:40:41 -0300 Subject: [PATCH 6/6] test: Test stack logic. --- .../java/collections/stack/StackTest.java | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/test/java/collections/stack/StackTest.java b/src/test/java/collections/stack/StackTest.java index 8b49614..eb4636f 100644 --- a/src/test/java/collections/stack/StackTest.java +++ b/src/test/java/collections/stack/StackTest.java @@ -10,7 +10,7 @@ class StackTest { private Stack stack; @BeforeEach - void setup() { + void setUp() { stack = new Stack(); } @@ -26,8 +26,8 @@ void afterOnePush_IsNotEmpty() { } @Test - void willThrowUnderflowException_WhenEmptyStackIsDequeued() { - Assertions.assertThrowsExactly(UnderflowException.class, () -> stack.pop()); + void willThrowUnderflowException_WhenEmptyStackIsPopped() { + Assertions.assertThrows(UnderflowException.class, stack::pop); } @Test @@ -38,10 +38,25 @@ void afterOnePushAndOnePop_WillBeEmpty() { } @Test - void afterPushingXAndY_PopWillReturnYAndX() { - stack.push(1); - stack.push(2); - Assertions.assertEquals(2, stack.pop()); - Assertions.assertEquals(1, stack.pop()); + void afterTwoPushesAndOnePop_WillNotBeEmpty() { + stack.push(0); + stack.push(0); + stack.pop(); + Assertions.assertFalse(stack.isEmpty()); } -} + + @Test + void afterPushingX_WillPopX() { + stack.push(42); + Assertions.assertEquals(42, stack.pop()); + } + + @Test + void afterPushingXAndY_WillPopYAndX() { + stack.push(42); + stack.push(100); + Assertions.assertEquals(100, stack.pop()); + Assertions.assertEquals(42, stack.pop()); + } + +} \ No newline at end of file