summaryrefslogtreecommitdiff
path: root/test/container.c
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2023-04-06 23:23:46 +0200
committerAlbert Cervin <albert@acervin.com>2023-05-01 22:19:14 +0200
commita123725a12e948d78badb2cb686d38548f1c633b (patch)
treec92c46134ef5536fbbf3bf08983c4f0dea1aaf58 /test/container.c
parentb5ed4cf757afc50afb6ac499eee7b87a2648fa4c (diff)
downloaddged-a123725a12e948d78badb2cb686d38548f1c633b.tar.gz
dged-a123725a12e948d78badb2cb686d38548f1c633b.tar.xz
dged-a123725a12e948d78badb2cb686d38548f1c633b.zip
Implement window handling
Also implement searching. fix undo boundaries when it checked for other save point, it used && instead of == which caused it to overwrite other types. Fix bytes vs chars bug in text_get_region
Diffstat (limited to 'test/container.c')
-rw-r--r--test/container.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/test/container.c b/test/container.c
new file mode 100644
index 0000000..8be7fc9
--- /dev/null
+++ b/test/container.c
@@ -0,0 +1,102 @@
+#include <stdint.h>
+
+#include "dged/btree.h"
+
+#include "assert.h"
+#include "test.h"
+
+void test_empty_bintree() {
+ BINTREE_ENTRY_TYPE(node, int);
+ BINTREE(node) tree;
+
+ BINTREE_INIT(&tree);
+
+ ASSERT(BINTREE_ROOT(&tree) == NULL,
+ "Expected root of tree to be NULL initially");
+ struct node *res = BINTREE_ROOT(&tree);
+ BINTREE_NEXT(res);
+ ASSERT(res == NULL, "Expected there to be no \"next\" initially");
+ struct node *n = BINTREE_ROOT(&tree);
+ BINTREE_FIRST(n);
+ bool loop_empty = true;
+ while (n != NULL) {
+ loop_empty = false;
+ BINTREE_NEXT(n);
+ }
+
+ ASSERT(loop_empty, "Expected an empty tree to yield an empty loop");
+
+ BINTREE_FREE_NODES(BINTREE_ROOT(&tree), node);
+}
+
+void test_bintree_iter() {
+ BINTREE_ENTRY_TYPE(node, char);
+ BINTREE(node) tree;
+ BINTREE_INIT(&tree);
+
+ // insert at the root
+ BINTREE_SET_ROOT(&tree, 'a');
+
+ struct node *root = BINTREE_ROOT(&tree);
+ ASSERT(BINTREE_VALUE(root) == 'a', "Expected root to have its value");
+
+ // insert first child (left)
+ BINTREE_INSERT(root, 'b');
+ ASSERT(BINTREE_VALUE(root->left) == 'b',
+ "Expected first child to have its value");
+
+ // insert second child
+ BINTREE_INSERT(root, 'c');
+ ASSERT(BINTREE_VALUE(root->right) == 'c',
+ "Expected second child to have its value");
+
+ // insert first child of second child
+ struct node *right = BINTREE_RIGHT(root);
+ BINTREE_INSERT(right, 'd');
+
+ // iterate
+ char chars[4] = {0};
+ uint32_t nchars = 0;
+ struct node *n = BINTREE_ROOT(&tree);
+ BINTREE_FIRST(n);
+ while (n != NULL) {
+ chars[nchars] = BINTREE_VALUE(n);
+ ++nchars;
+ BINTREE_NEXT(n);
+ }
+
+ ASSERT(nchars == 4, "Expected tree to have 4 nodes after insertions");
+ ASSERT(chars[0] == 'b' && chars[1] == 'a' && chars[2] == 'd' &&
+ chars[3] == 'c',
+ "Expected tree to have been traversed correctly");
+
+ // find
+ struct node *res = NULL;
+ BINTREE_FIND(&tree, 'b', res);
+ ASSERT(res != NULL && res == BINTREE_LEFT(root),
+ "Expected existing value to be found");
+ ASSERT(BINTREE_VALUE(res) == 'b',
+ "Expected found node to contain the searched for value");
+
+ // remove
+ struct node *to_remove = BINTREE_LEFT(right);
+ BINTREE_REMOVE(to_remove);
+
+ nchars = 0;
+ n = BINTREE_ROOT(&tree);
+ BINTREE_FIRST(n);
+ while (n != NULL) {
+ ++nchars;
+ BINTREE_NEXT(n);
+ }
+
+ ASSERT(nchars == 3, "Expected three nodes to remain after removing one");
+ BINTREE_FREE_NODES(to_remove, node);
+
+ BINTREE_FREE_NODES(root, node);
+}
+
+void run_container_tests() {
+ run_test(test_empty_bintree);
+ run_test(test_bintree_iter);
+}