Commit 05f5d54

mo khan <mo.khan@gmail.com>
2020-07-04 19:10:46
Split stack_test.c
1 parent 24700ad
src/01/01b/main.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+#include "stack.h"
+
+int main(int argc, char *argv[])
+{
+  return 0;
+}
src/01/01b/Makefile
@@ -0,0 +1,37 @@
+#!/usr/bin/make -f
+SHELL=/bin/sh
+
+CC=clang
+TEST_LIBS = -lcgreen
+
+BUILDDIR := build
+OBJS := $(addprefix $(BUILDDIR)/,stack.o)
+TEST_OBJS := $(addprefix $(BUILDDIR)/,stack_test.o)
+
+$(BUILDDIR)/%.o : %.c
+	$(COMPILE.c) $(OUTPUT_OPTION) $<
+
+.PHONY: all
+all: $(OBJS) $(BUILDDIR)/main.o
+	$(CC) $(OBJS) $(BUILDDIR)/main.o -o $(BUILDDIR)/program
+
+.PHONY: test
+test: $(OBJS) $(TEST_OBJS)
+	$(CC) $(OBJS) $(TEST_OBJS) $(TEST_LIBS) -o $(BUILDDIR)/test
+
+$(OBJS): | $(BUILDDIR)
+
+$(TEST_OBJS): | $(BUILDDIR)
+
+$(BUILDDIR):
+	mkdir $(BUILDDIR)
+
+.PHONY: clean
+clean:
+	rm -fr build
+
+run : all
+	./build/program
+
+run_test : test
+	cgreen-runner -c -v $(BUILDDIR)/test
src/01/01b/README.md
@@ -7,7 +7,7 @@ Student ID: 3431709
 
 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.
+Analyze the running time of the `push(x)` and `pop()` operations based on this implementation.
 
 2. Description of the Code:
 3. Errors and Warnings:
src/01/01b/stack.c
@@ -0,0 +1,99 @@
+#include "stack.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+static Queue *newQueue(){
+  Queue *queue = malloc(sizeof(Queue));
+  queue->size = 0;
+  queue->head = NULL;
+  return queue;
+}
+
+Stack *initialize() {
+  Stack *stack = malloc(sizeof(Stack));
+  stack->q1 = newQueue();
+  return stack;
+}
+
+Node *newNode(int data) {
+  Node *node = malloc(sizeof(Node));
+  node->data = data;
+  node->next = NULL;
+  return node;
+}
+
+void enqueue(Queue *q, int data) {
+  Node *node = newNode(data);
+  q->size++;
+
+  if (q->head == NULL) {
+    q->head = node;
+  } else {
+    Node *tail = q->head;
+
+    while (tail->next != NULL)
+      tail = tail->next;
+    tail->next = node;
+  }
+}
+
+int dequeue(Queue *q) {
+  if (q->head == NULL) return -1;
+
+  Node *node = q->head;
+  int data = node->data;
+
+  q->head = node->next;
+  free(node);
+  return data;
+}
+
+// The running time is linear time.
+void push(Stack *stack, int data) {
+  enqueue(stack->q1, data);
+}
+
+// The running time is linear time.
+int pop(Stack *stack) {
+  int count = stack->q1->size - 1;
+  Queue *tmp = newQueue();
+
+  for (int i = 0; i < count; i++)
+    enqueue(tmp, dequeue(stack->q1));
+
+  int data = dequeue(stack->q1);
+  free(stack->q1);
+  stack->q1 = tmp;
+  return data;
+}
+
+int size(Stack *stack) {
+  return stack->q1->size;
+}
+
+void destroy(Stack *stack) {
+  int count = size(stack);
+
+  for (int i = 0; i < count; i++)
+    pop(stack);
+
+  free(stack->q1);
+  free(stack);
+}
+
+static void inspect(Queue *q) {
+  Node *tmp = q->head;
+
+  if (q->size == 0) {
+    printf("[]\n");
+    return;
+  }
+
+  printf("[");
+  for (int i = 0; i < q->size; i++) {
+    printf("%d,", tmp->data);
+    tmp = tmp->next;
+  }
+  printf("\b]\n");
+}
+
src/01/01b/stack.h
@@ -0,0 +1,22 @@
+struct node {
+  int data;
+  struct node *next;
+};
+
+typedef struct node Node;
+
+typedef struct {
+  Node *head;
+  int size;
+} Queue;
+
+typedef struct {
+  Queue *q1;
+} Stack;
+
+
+Stack *initialize();
+void push(Stack *stack, int data);
+int pop(Stack *stack);
+int size(Stack *stack);
+void destroy(Stack *stack);
src/01/01b/stack_test.c
@@ -1,124 +1,5 @@
 #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.
-*/
-
-struct node {
-  int data;
-  struct node *next;
-};
-
-typedef struct node Node;
-
-typedef struct {
-  Node *head;
-  int size;
-} Queue;
-
-typedef struct {
-  Queue *q1;
-} Stack;
-
-static Queue *newQueue(){
-  Queue *queue = malloc(sizeof(Queue));
-  queue->size = 0;
-  queue->head = NULL;
-  return queue;
-}
-
-static Stack *initialize() {
-  Stack *stack = malloc(sizeof(Stack));
-  stack->q1 = newQueue();
-  return stack;
-}
-
-Node *newNode(int data) {
-  Node *node = malloc(sizeof(Node));
-  node->data = data;
-  node->next = NULL;
-  return node;
-}
-
-void enqueue(Queue *q, int data) {
-  Node *node = newNode(data);
-  q->size++;
-
-  if (q->head == NULL) {
-    q->head = node;
-  } else {
-    Node *tail = q->head;
-
-    while (tail->next != NULL)
-      tail = tail->next;
-    tail->next = node;
-  }
-}
-
-int dequeue(Queue *q) {
-  if (q->head == NULL) return -1;
-
-  Node *node = q->head;
-  int data = node->data;
-
-  q->head = node->next;
-  free(node);
-  return data;
-}
-
-// The running time is linear time.
-static void push(Stack *stack, int data) {
-  enqueue(stack->q1, data);
-}
-
-// The running time is linear time.
-static int pop(Stack *stack) {
-  int count = stack->q1->size - 1;
-  Queue *tmp = newQueue();
-
-  for (int i = 0; i < count; i++)
-    enqueue(tmp, dequeue(stack->q1));
-
-  int data = dequeue(stack->q1);
-  free(stack->q1);
-  stack->q1 = tmp;
-  return data;
-}
-
-int size(Stack *stack) {
-  return stack->q1->size;
-}
-
-static void destroy(Stack *stack) {
-  int count = size(stack);
-
-  for (int i = 0; i < count; i++)
-    pop(stack);
-
-  free(stack->q1);
-  free(stack);
-}
-
-static void inspect(Queue *q) {
-  Node *tmp = q->head;
-
-  if (q->size == 0) {
-    printf("[]\n");
-    return;
-  }
-
-  printf("[");
-  for (int i = 0; i < q->size; i++) {
-    printf("%d,", tmp->data);
-    tmp = tmp->next;
-  }
-  printf("\b]\n");
-}
+#include "stack.h"
 
 Describe(Stack);
 BeforeEach(Stack){ }
@@ -166,3 +47,9 @@ TestSuite *stack_tests() {
 
   return suite;
 }
+
+int main(int argc, char **argv) {
+  TestSuite *suite = create_test_suite();
+  add_suite(suite, stack_tests());
+  return run_test_suite(suite, create_text_reporter());
+}