Commit c52aa3f
Changed files (3)
src
01
src/01/01b/README.md
@@ -10,6 +10,77 @@ Implement the stack methods `push(x)` and `pop()` using two queues.
Analyze the running time of the `push(x)` and `pop()` operations based on this implementation.
## Description of the Code
+
+The `push()` function is used to push a new item to the top of the stack.
+The `pop()` function is used to pop the item off the top of the stack.
+
+The implementation of the `push()` function operates in linear time `O(n)`.
+The implementation of the `pop()` function operates in linear time `O(n)`.
+
## Errors and Warnings
+
+The design this program I used [cgreen](https://cgreen-devs.github.io/) to unit test the pseudo public
+functions of the Stack interface.
+
+The [`stack_test.c`](./stack_test.c) file includes unit tests to cover the following scenarios:
+
+* popping an item off of an empty stack
+* popping an item off of the stack
+* popping successive items off of the stack
+* pushing an item onto a full stack
+* pushing an item onto the stack
+
+```bash
+モ make run_test
+mkdir build
+clang -c -o build/stack.o stack.c
+clang -c -o build/stack_test.o stack_test.c
+clang build/stack.o build/stack_test.o -lcgreen -o build/test
+Running "main" (3 tests)...
+ "stack_tests": 5 passes in 1ms.
+Completed "main": 5 passes in 1ms.
+```
+
## Sample Input and Output
+
+```bash
+モ make run
+clang build/stack.o build/main.o -o build/program
+./build/program
+=== COMP-272 - Assignment 1 - Question 1b ===
+Push: 383
+Push: 886
+Push: 777
+Push: 915
+Push: 793
+Push: 335
+Push: 386
+Push: 492
+Push: 649
+Push: 421
+
+[383,886,777,915,793,335,386,492,649,421]
+Pop: 421
+[383,886,777,915,793,335,386,492,649]
+Pop: 649
+[383,886,777,915,793,335,386,492]
+Pop: 492
+[383,886,777,915,793,335,386]
+Pop: 386
+[383,886,777,915,793,335]
+Pop: 335
+[383,886,777,915,793]
+Pop: 793
+[383,886,777,915]
+Pop: 915
+[383,886,777]
+Pop: 777
+[383,886]
+Pop: 886
+[383]
+Pop: 383
+[]
+Bye
+```
+
## Discussion
src/01/01b/stack.c
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <stdlib.h>
+static int MAX_SIZE = 2147483647;
+
/**
* Constructs a new instance of a queue
*
@@ -83,7 +85,8 @@ int dequeue(Queue *self) {
* @param data The data to push on to the stack
*/
void push(Stack *self, int data) {
- enqueue(self->q1, data);
+ if (self->q1->size < MAX_SIZE)
+ enqueue(self->q1, data);
}
/**
@@ -93,10 +96,14 @@ void push(Stack *self, int data) {
* @return The data associated with the item to pop off the stack
*/
int pop(Stack *self) {
- int count = self->q1->size - 1;
+ int count = self->q1->size;
+
+ if (count <= 0)
+ return 0;
+
Queue *tmp = newQueue();
- for (int i = 0; i < count; i++)
+ for (int i = 0; i < count - 1; i++)
enqueue(tmp, dequeue(self->q1));
int data = dequeue(self->q1);
src/01/01b/stack_test.c
@@ -1,5 +1,6 @@
-#include <cgreen/cgreen.h>
#include "stack.h"
+#include <cgreen/cgreen.h>
+#include <math.h>
Describe(Stack);
BeforeEach(Stack){ }
@@ -38,12 +39,35 @@ Ensure(Stack, pop_successive_items) {
destroy(stack);
}
+Ensure(Stack, when_popping_an_item_off_an_empty_stack) {
+ Stack *stack = initialize();
+
+ assert_that(pop(stack), is_equal_to(NULL));
+
+ destroy(stack);
+}
+
+Ensure(Stack, when_pushing_an_item_on_to_a_full_stack) {
+ Stack *stack = initialize();
+ int max = 2147483647;
+
+ for (int i = 0; i < max; i++)
+ push(stack, 1);
+
+ push(stack, 1);
+
+ assert_that(size(stack), is_equal_to(max));
+ destroy(stack);
+}
+
TestSuite *stack_tests() {
TestSuite *suite = create_test_suite();
- add_test_with_context(suite, Stack, push_onto_stack);
add_test_with_context(suite, Stack, pop_single_item);
add_test_with_context(suite, Stack, pop_successive_items);
+ add_test_with_context(suite, Stack, push_onto_stack);
+ add_test_with_context(suite, Stack, when_popping_an_item_off_an_empty_stack);
+ add_test_with_context(suite, Stack, when_pushing_an_item_on_to_a_full_stack);
return suite;
}