Commit 6441d1b

mo khan <mo.khan@gmail.com>
2020-07-18 21:56:44
Start to test if a btree is a bst
1 parent 07b83ec
src/02/02/btree.c
@@ -0,0 +1,16 @@
+#include "btree.h"
+
+BTree *btree_init(int data) {
+  BTree *tree = malloc(sizeof(BTree));
+  tree->left = NULL;
+  tree->right = NULL;
+  tree->data = data;
+  return tree;
+}
+
+bool btree_is_bst(BTree *tree) {
+  if (tree) {
+    return true;
+  }
+  return false;
+}
src/02/02/btree.h
@@ -0,0 +1,11 @@
+#include <stdlib.h>
+#include <stdbool.h>
+
+typedef struct node {
+  struct node *left;
+  struct node *right;
+  int data;
+} BTree;
+
+BTree *btree_init(int data);
+bool btree_is_bst(BTree *tree);
src/02/02/btree_test.c
@@ -0,0 +1,35 @@
+#include "btree.h"
+#include <cgreen/cgreen.h>
+#include <string.h>
+
+Describe(BinaryTree);
+BeforeEach(BinaryTree) {}
+AfterEach(BinaryTree) {}
+
+Ensure(BinaryTree, when_a_tree_is_NULL) {
+  bool result = btree_is_bst(NULL);
+
+  assert_that(result, is_equal_to(false));
+}
+
+Ensure(BinaryTree, when_a_tree_has_a_single_node) {
+  BTree *tree = btree_init(100);
+  bool result = btree_is_bst(tree);
+
+  assert_that(result, is_equal_to(true));
+}
+
+TestSuite *binary_search_tree_tests() {
+  TestSuite *suite = create_test_suite();
+
+  add_test_with_context(suite, BinaryTree, when_a_tree_is_NULL);
+  add_test_with_context(suite, BinaryTree, when_a_tree_has_a_single_node);
+
+  return suite;
+}
+
+int main(int argc, char **argv) {
+  TestSuite *suite = create_test_suite();
+  add_suite(suite, binary_search_tree_tests());
+  return run_test_suite(suite, create_text_reporter());
+}
src/02/02/main.c
@@ -0,0 +1,9 @@
+#include "bst.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char *argv[]) {
+  printf("=== COMP-272 - Assignment 02 - Question 02 ===\n");
+  printf("Bye\n");
+  return 0;
+}
src/02/02/Makefile
@@ -0,0 +1,37 @@
+#!/usr/bin/make -f
+SHELL=/bin/sh
+
+CC=clang
+TEST_LIBS = -lcgreen
+
+BUILDDIR := build
+OBJS := $(addprefix $(BUILDDIR)/,btree.o)
+TEST_OBJS := $(addprefix $(BUILDDIR)/,btree_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