aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2016-08-21 16:59:22 +0200
committerKatharina Fey <kookie@spacekookie.de>2016-08-21 16:59:22 +0200
commitd1f9b84e6926e1f0f1259ec79091873fd221cf01 (patch)
tree7f6a1adedd91eacdecbe5482f133c9d6769a3c10
parent98a9a31789203e051404b9bcf2faa29bafd9e4bf (diff)
Adding test file with different test suites (as well as examples).
This should hopefully make it easier to understand the API :)
-rw-r--r--CMakeLists.txt10
-rw-r--r--lib/dyn_utils.c6
-rw-r--r--test/main.c113
3 files changed, 128 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 85eb9af..5f6d416 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,7 +12,7 @@ set(CMAKE_BUILD_TYPE Debug)
# Create our project for further reference
project(libdyntree)
-set(DYN_TREE_SRC lib/dyn_tree.c)
+set(DYN_TREE_SRC lib/dyn_tree.c lib/dyn_utils.c)
# Define our library in cmake
add_library(libdyntree SHARED ${DYN_TREE_SRC})
@@ -23,3 +23,11 @@ target_include_directories(libdyntree PRIVATE "lib")
# since the name starts with 'lib' dont add it again
set_target_properties(libdyntree PROPERTIES PREFIX "")
+
+################### TESTING CODE BELOW ###################
+
+set(TEST_SRC test/main.c)
+add_executable(dyntree_test ${TEST_SRC})
+
+# Library dependencies for the http extention
+target_link_libraries(dyntree_test libdyntree) \ No newline at end of file
diff --git a/lib/dyn_utils.c b/lib/dyn_utils.c
new file mode 100644
index 0000000..81e6d1d
--- /dev/null
+++ b/lib/dyn_utils.c
@@ -0,0 +1,6 @@
+#include <dtree/dyn_tree.h>
+
+const char *rdb_error_getmsg(dt_err *e)
+{
+
+} \ No newline at end of file
diff --git a/test/main.c b/test/main.c
new file mode 100644
index 0000000..51e626a
--- /dev/null
+++ b/test/main.c
@@ -0,0 +1,113 @@
+
+#include <stdio.h>
+#include <dtree/dyn_tree.h>
+
+/**
+ * A small test that creates a tree, splits the nodes
+ * and then merges them again.
+ */
+dt_err split_and_merge();
+
+dt_err search_for_payload();
+
+#define TEST(function) \
+ printf("Running '%s'...", #function); \
+ fflush(stdout); \
+ err = function(); \
+ printf(" %s\n", (err == 0) ? "OK!" : "FAILED!"); \
+ if(err) goto end;
+
+int main(void)
+{
+ dt_err err;
+ printf("=== libdyntree test suite ===\n");
+
+ /* Search inside trees */
+ TEST(search_for_payload)
+
+ /* Split and merge trees */
+ TEST(split_and_merge)
+
+ end:
+ printf("==== done ====\n");
+ return err;
+}
+
+
+/*************** TEST IMPLEMENTATIONS ****************/
+
+dt_err split_and_merge()
+{
+ dt_err err;
+
+ /* Allocate a node named root */
+ dtree *root;
+ err = dtree_malloc(&root);
+ if(err) goto exit;
+
+ /* Add child as a recursive node to root */
+ dtree *child;
+ err = dtree_addrecursive(root, &child);
+ if(err) goto exit;
+
+ /* Make child a literal node containing the works of shakespeare */
+ const char *hamlet = "To be, or not to be: that is the question:\n"
+ "Whether 'tis nobler in the mind to suffer\n"
+ "The slings and arrows of outrageous fortune,\n"
+ "Or to take arms against a sea of troubles,\n"
+ "And by opposing end them? To die: to sleep;\n"
+ "No more; and by a sleep to say we end\n"
+ "The heart-ache and the thousand natural shocks\n"
+ "That flesh is heir to, 'tis a consummation\n"
+ "Devoutly to be wish'd. To die, to sleep;";
+
+ err = dtree_addliteral(child, hamlet, REAL_STRLEN(hamlet));
+ if(err) goto exit;
+
+ /* Split our tree into two single-nodes */
+ err = dtree_split_trees(root, child);
+ if(err) goto exit;
+
+ /* Re-merge because they miss each other */
+ err = dtree_merge_trees(root, child);
+ if(err) goto exit;
+
+ /* Cleanup */
+ exit:
+ dtree_free(root);
+ return err;
+}
+
+dt_err search_for_payload()
+{
+ dt_err err;
+
+ dtree *root, *a, *b, *found;
+ err = dtree_malloc(&root);
+ if(err) goto exit;
+
+ const char *string = "This is some data!";
+ err = dtree_addrecursive(root, &a);
+ if(err) goto exit;
+
+ err = dtree_addliteral(a, string, REAL_STRLEN(string));
+ if(err) goto exit;
+
+ err = dtree_addrecursive(root, &b);
+ if(err) goto exit;
+
+ err = dtree_addnumeral(b, 1337);
+ if(err) goto exit;
+
+ /* Try to find our data again */
+
+ err = dtree_search_payload(root, &found, (void*) string, LITERAL);
+ if(err) goto exit;
+
+ err = dtree_search_payload(root, &found, (void*) 1337, NUMERAL);
+ if(err) goto exit;
+
+ exit:
+ dtree_free(root);
+ return err;
+} \ No newline at end of file