diff options
| -rw-r--r-- | Makefile | 7 | ||||
| -rw-r--r-- | assignments/01/README.md | 3 | ||||
| -rw-r--r-- | assignments/01/stack_test.c | 53 | ||||
| -rw-r--r-- | main.c | 2 |
4 files changed, 62 insertions, 3 deletions
@@ -13,8 +13,8 @@ doc : doc/ run : main ./main -main : main.o words_test.o words.o priority_queue.o priority_queue_test.o - $(CC) main.o words_test.o words.o priority_queue.o priority_queue_test.o -lcgreen -o main +main : main.o words_test.o words.o priority_queue.o priority_queue_test.o stack_test.o + $(CC) main.o words_test.o words.o priority_queue.o priority_queue_test.o stack_test.o -lcgreen -o main main.o : main.c $(CC) -c main.c @@ -31,6 +31,9 @@ priority_queue.o : assignments/01/priority_queue.c priority_queue_test.o : assignments/01/priority_queue_test.c $(CC) -c assignments/01/priority_queue_test.c +stack_test.o : assignments/01/stack_test.c + $(CC) -c assignments/01/stack_test.c + clean: rm -f main *.o rm -fr doc diff --git a/assignments/01/README.md b/assignments/01/README.md index 9594a3c..9957da2 100644 --- a/assignments/01/README.md +++ b/assignments/01/README.md @@ -13,7 +13,8 @@ You must score at least 50 to pass the assignment. * The `size()` method is used to determine how many items are in the queue. * The `add(x)` function is linear time O(n). * The `deleteMin(x)` function is constant time O(1). - * b. 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. + * b. 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. 2. Swap two adjacent elements in a list by adjusting only the links (and not the data) using: * a. singly-linked list. * b. doubly-linked list. diff --git a/assignments/01/stack_test.c b/assignments/01/stack_test.c new file mode 100644 index 0000000..99d924d --- /dev/null +++ b/assignments/01/stack_test.c @@ -0,0 +1,53 @@ +#include <cgreen/cgreen.h> + +/* +Implement the stack methods using two queues + +* `push(x)` +* `pop()` + +Analyze the running time of the `push(x)` and `pop()` operations based on this implementation. +*/ + +typedef struct { +} Stack; + +static Stack *initialize() { + Stack *stack = malloc(sizeof(Stack)); + return stack; +} + +static void push(Stack *stack, int data) { +} + +static int pop(Stack *stack) { + return 0; +} + +static void destroy(Stack *stack) { + free(stack); +} + +Describe(Stack); +BeforeEach(Stack){ } +AfterEach(Stack){ } + +Ensure(Stack, returns_last_item_pushed_to_stack) { + Stack *stack = initialize(); + + push(stack, 1); + push(stack, 2); + + assert_that(pop(stack), is_equal_to(2)); + assert_that(pop(stack), is_equal_to(1)); + + destroy(stack); +} + +TestSuite *stack_tests() { + TestSuite *suite = create_test_suite(); + + add_test_with_context(suite, Stack, returns_last_item_pushed_to_stack); + + return suite; +} @@ -2,12 +2,14 @@ TestSuite *words_tests(); TestSuite *priority_queue_tests(); +TestSuite *stack_tests(); int main(int argc, char **argv) { TestSuite *suite = create_test_suite(); add_suite(suite, words_tests()); add_suite(suite, priority_queue_tests()); + add_suite(suite, stack_tests()); if (argc > 1) return run_single_test(suite, argv[1], create_text_reporter()); |
