Commit 729303c

mo khan <mo.khan@gmail.com>
2020-06-28 19:23:58
start to build a MinStack
1 parent b432929
Changed files (1)
assignments
assignments/01/min_stack_test.c
@@ -15,12 +15,71 @@ Describe(MinStack);
 BeforeEach(MinStack){ }
 AfterEach(MinStack){ }
 
-Ensure(MinStack, when_getting_head) {
+struct node {
+  int data;
+  struct node *next;
+};
+
+typedef struct node Node;
+
+typedef struct {
+  Node *head;
+} Stack;
+
+static Stack *initialize() {
+  Stack *stack = malloc(sizeof(Stack));
+  stack->head = NULL;
+  return stack;
+}
+
+static int size(Stack *stack) {
+  Node *current = stack->head;
+  int i;
+  for (i = 0; current != NULL; i++)
+    current = current->next;
+  return i;
+}
+
+static void push(Stack *stack, int data) {
+  Node *node = malloc(sizeof(Node));
+  node->next = NULL;
+  node->data = data;
+
+  stack->head = node;
+}
+
+static int min(Stack *stack) {
+  if (stack && stack->head)
+    return stack->head->data;
+
+  return (int)NULL;
+}
+
+Ensure(MinStack, when_empty) {
+  Stack *stack = initialize();
+
+  assert_that(size(stack), is_equal_to(0));
+  assert_that(min(stack), is_equal_to(NULL));
+
+  free(stack);
+}
+
+Ensure(MinStack, when_pushing_a_single_integer) {
+  Stack *stack = initialize();
+
+  push(stack, 1);
+
+  assert_that(size(stack), is_equal_to(1));
+  assert_that(min(stack), is_equal_to(1));
+  /*assert_that(pop(stack), is_equal(1));*/
+
+  free(stack);
 }
 
 TestSuite *min_stack_tests() {
   TestSuite *suite = create_test_suite();
 
-  /*add_test_with_context(suite, MinStack, when_pushing_it);*/
+  add_test_with_context(suite, MinStack, when_empty);
+  add_test_with_context(suite, MinStack, when_pushing_a_single_integer);
   return suite;
 }