diff options
| author | mo khan <mo.khan@gmail.com> | 2020-07-18 15:56:44 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-07-18 15:56:44 -0600 |
| commit | 6441d1b608078b9940e06e2c3a2549fe50087865 (patch) | |
| tree | 8134e125a8af6d6e69d16e4073d5dce5839df4ee /src/02 | |
| parent | 07b83ec514719aeecb4c41c040cde5ebebbcd2be (diff) | |
Start to test if a btree is a bst
Diffstat (limited to 'src/02')
| -rw-r--r-- | src/02/02/Makefile | 37 | ||||
| -rw-r--r-- | src/02/02/btree.c | 16 | ||||
| -rw-r--r-- | src/02/02/btree.h | 11 | ||||
| -rw-r--r-- | src/02/02/btree_test.c | 35 | ||||
| -rw-r--r-- | src/02/02/main.c | 9 |
5 files changed, 108 insertions, 0 deletions
diff --git a/src/02/02/Makefile b/src/02/02/Makefile new file mode 100644 index 0000000..3e1c4e6 --- /dev/null +++ b/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 diff --git a/src/02/02/btree.c b/src/02/02/btree.c new file mode 100644 index 0000000..99288cc --- /dev/null +++ b/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; +} diff --git a/src/02/02/btree.h b/src/02/02/btree.h new file mode 100644 index 0000000..5be69b3 --- /dev/null +++ b/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); diff --git a/src/02/02/btree_test.c b/src/02/02/btree_test.c new file mode 100644 index 0000000..feb0e3f --- /dev/null +++ b/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()); +} diff --git a/src/02/02/main.c b/src/02/02/main.c new file mode 100644 index 0000000..fb9b722 --- /dev/null +++ b/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; +} |
