summaryrefslogtreecommitdiff
path: root/src/01/06
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-07-05 14:01:59 -0600
committermo khan <mo.khan@gmail.com>2020-07-05 14:01:59 -0600
commit8bf32957fef23b85b1421c38b6416047bcba476e (patch)
tree3eece4d270f52c91a6c22e84b6eb636fd57326c1 /src/01/06
parente27441426cd127c1f9969da1fcd2fde0ab415586 (diff)
run make fmt
Diffstat (limited to 'src/01/06')
-rw-r--r--src/01/06/main.c5
-rw-r--r--src/01/06/min_stack.c22
-rw-r--r--src/01/06/min_stack_test.c41
3 files changed, 39 insertions, 29 deletions
diff --git a/src/01/06/main.c b/src/01/06/main.c
index 6a86346..53e41e0 100644
--- a/src/01/06/main.c
+++ b/src/01/06/main.c
@@ -2,8 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
Stack *stack = initialize();
printf("=== COMP-272 - Assignment 1 - Question 6 ===\n");
@@ -18,7 +17,7 @@ int main(int argc, char *argv[])
printf("Popping:\n==========\n");
inspect(stack);
- while(size(stack) > 0) {
+ while (size(stack) > 0) {
printf("Pop: %d, Min: %d\n", pop(stack), min(stack));
inspect(stack);
}
diff --git a/src/01/06/min_stack.c b/src/01/06/min_stack.c
index 679f67b..f800943 100644
--- a/src/01/06/min_stack.c
+++ b/src/01/06/min_stack.c
@@ -1,8 +1,8 @@
+#include "min_stack.h"
#include <stdio.h>
#include <stdlib.h>
-#include "min_stack.h"
-static Node *new(int data, Node *next) {
+static Node *new (int data, Node *next) {
Node *node = malloc(sizeof(Node));
node->next = next;
node->data = data;
@@ -17,36 +17,34 @@ Stack *initialize(void) {
return self;
}
-int size(Stack *self) {
- return self->size;
-}
+int size(Stack *self) { return self->size; }
void push(Stack *self, int data) {
if (!self->min || (data < self->min->data))
- self->min = new(data, self->min);
+ self->min = new (data, self->min);
- self->head = new(data, self->head);
+ self->head = new (data, self->head);
self->size++;
}
void each(Node *head, Visitor block) {
Node *tmp = head;
- while(tmp) {
+ while (tmp) {
(*block)(tmp);
tmp = tmp->next;
}
}
int min(Stack *self) {
- if(self->min)
+ if (self->min)
return self->min->data;
if (self->head) {
int min = self->head->data;
Node *tmp = self->head->next;
- while(tmp) {
+ while (tmp) {
if (tmp->data < min)
min = tmp->data;
tmp = tmp->next;
@@ -72,9 +70,7 @@ int pop(Stack *self) {
return data;
}
-void print_node(Node *node) {
- printf("[%d]", node->data);
-}
+void print_node(Node *node) { printf("[%d]", node->data); }
void inspect(Stack *stack) {
printf("\t");
diff --git a/src/01/06/min_stack_test.c b/src/01/06/min_stack_test.c
index c796dca..b189883 100644
--- a/src/01/06/min_stack_test.c
+++ b/src/01/06/min_stack_test.c
@@ -1,9 +1,9 @@
-#include <cgreen/cgreen.h>
#include "min_stack.h"
+#include <cgreen/cgreen.h>
Describe(MinStack);
-BeforeEach(MinStack){ }
-AfterEach(MinStack){ }
+BeforeEach(MinStack) {}
+AfterEach(MinStack) {}
Ensure(MinStack, when_empty) {
Stack *stack = initialize();
@@ -51,7 +51,9 @@ Ensure(MinStack, when_a_single_item_is_on_the_stack_it_is_the_min) {
free(stack);
}
-Ensure(MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item) {
+Ensure(
+ MinStack,
+ when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item) {
Stack *stack = initialize();
push(stack, 200);
@@ -61,7 +63,9 @@ Ensure(MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_ret
free(stack);
}
-Ensure(MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero) {
+Ensure(
+ MinStack,
+ when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero) {
Stack *stack = initialize();
push(stack, 200);
@@ -72,7 +76,9 @@ Ensure(MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_ret
free(stack);
}
-Ensure(MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null) {
+Ensure(
+ MinStack,
+ when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null) {
Stack *stack = initialize();
push(stack, 200);
@@ -159,15 +165,24 @@ TestSuite *min_stack_tests() {
add_test_with_context(suite, MinStack, when_empty_it_has_a_size_of_zero);
add_test_with_context(suite, MinStack, when_empty_it_has_a_min_of_null);
- add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_it_has_a_size_of_one);
- add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_it_is_the_min);
-
- add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item);
- add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero);
- add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null);
+ add_test_with_context(
+ suite, MinStack, when_a_single_item_is_on_the_stack_it_has_a_size_of_one);
+ add_test_with_context(suite, MinStack,
+ when_a_single_item_is_on_the_stack_it_is_the_min);
+
+ add_test_with_context(
+ suite, MinStack,
+ when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item);
+ add_test_with_context(
+ suite, MinStack,
+ when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero);
+ add_test_with_context(
+ suite, MinStack,
+ when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null);
add_test_with_context(suite, MinStack, when_pushing_a_single_integer);
- add_test_with_context(suite, MinStack, when_pushing_multiple_integers_out_of_order);
+ add_test_with_context(suite, MinStack,
+ when_pushing_multiple_integers_out_of_order);
return suite;
}