summaryrefslogtreecommitdiff
path: root/test
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
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')
-rw-r--r--test/allocator.c4
-rw-r--r--test/buffer.c43
-rw-r--r--test/command.c6
-rw-r--r--test/container.c102
-rw-r--r--test/fake-reactor.h3
-rw-r--r--test/keyboard.c11
-rw-r--r--test/main.c5
-rw-r--r--test/minibuffer.c22
-rw-r--r--test/settings.c15
-rw-r--r--test/test.h1
-rw-r--r--test/text.c12
-rw-r--r--test/undo.c7
-rw-r--r--test/utf8.c9
13 files changed, 174 insertions, 66 deletions
diff --git a/test/allocator.c b/test/allocator.c
index ca1a7a0..fe01334 100644
--- a/test/allocator.c
+++ b/test/allocator.c
@@ -1,8 +1,8 @@
+#include "dged/allocator.h"
+
#include "assert.h"
#include "test.h"
-#include "allocator.h"
-
void test_frame_allocator() {
struct frame_allocator fa = frame_allocator_create(128);
diff --git a/test/buffer.c b/test/buffer.c
index 38ce468..a4ac754 100644
--- a/test/buffer.c
+++ b/test/buffer.c
@@ -1,45 +1,46 @@
-#include "assert.h"
-#include "test.h"
+#include <string.h>
-#include "buffer.h"
+#include "dged/buffer.h"
-#include <string.h>
+#include "assert.h"
+#include "test.h"
void test_move() {
- struct buffer b = buffer_create("test-buffer", false);
- ASSERT(b.dot.col == 0 && b.dot.line == 0,
+ struct buffer b = buffer_create("test-buffer");
+ struct buffer_view v = buffer_view_create(&b, false, false);
+ ASSERT(v.dot.col == 0 && v.dot.line == 0,
"Expected dot to be at buffer start");
// make sure we cannot move now
- buffer_backward_char(&b);
- buffer_backward_line(&b);
- ASSERT(b.dot.col == 0 && b.dot.line == 0,
+ buffer_backward_char(&v);
+ buffer_backward_line(&v);
+ ASSERT(v.dot.col == 0 && v.dot.line == 0,
"Expected to not be able to move backward in empty buffer");
- buffer_forward_char(&b);
- buffer_forward_line(&b);
- ASSERT(b.dot.col == 0 && b.dot.line == 0,
+ buffer_forward_char(&v);
+ buffer_forward_line(&v);
+ ASSERT(v.dot.col == 0 && v.dot.line == 0,
"Expected to not be able to move forward in empty buffer");
// add some text and try again
const char *txt = "testing movement";
- int lineindex = buffer_add_text(&b, (uint8_t *)txt, strlen(txt));
+ int lineindex = buffer_add_text(&v, (uint8_t *)txt, strlen(txt));
ASSERT(lineindex + 1 == 1, "Expected buffer to have one line");
- buffer_beginning_of_line(&b);
- buffer_forward_char(&b);
- ASSERT(b.dot.col == 1 && b.dot.line == 0,
+ buffer_beginning_of_line(&v);
+ buffer_forward_char(&v);
+ ASSERT(v.dot.col == 1 && v.dot.line == 0,
"Expected to be able to move forward by one char");
// now we have two lines
const char *txt2 = "\n";
- int lineindex2 = buffer_add_text(&b, (uint8_t *)txt2, strlen(txt2));
+ int lineindex2 = buffer_add_text(&v, (uint8_t *)txt2, strlen(txt2));
ASSERT(lineindex2 + 1 == 2, "Expected buffer to have two lines");
- buffer_backward_line(&b);
- buffer_beginning_of_line(&b);
- buffer_backward_char(&b);
+ buffer_backward_line(&v);
+ buffer_beginning_of_line(&v);
+ buffer_backward_char(&v);
ASSERT(
- b.dot.col == 0 && b.dot.line == 0,
+ v.dot.col == 0 && v.dot.line == 0,
"Expected to not be able to move backwards when at beginning of buffer");
buffer_destroy(&b);
diff --git a/test/command.c b/test/command.c
index e09cf35..8db02e0 100644
--- a/test/command.c
+++ b/test/command.c
@@ -1,9 +1,9 @@
#include "assert.h"
#include "test.h"
-#include "command.h"
-#include "hash.h"
-#include "hashmap.h"
+#include "dged/command.h"
+#include "dged/hash.h"
+#include "dged/hashmap.h"
void test_command_registry_create() {
struct commands cmds = command_registry_create(10);
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);
+}
diff --git a/test/fake-reactor.h b/test/fake-reactor.h
index 04d8306..33d1fbc 100644
--- a/test/fake-reactor.h
+++ b/test/fake-reactor.h
@@ -1,7 +1,8 @@
-#include "reactor.h"
#include <stdbool.h>
#include <stdint.h>
+#include "dged/reactor.h"
+
struct fake_reactor_impl {
bool (*poll_event)(void *userdata, uint32_t ev_id);
uint32_t (*register_interest)(void *userdata, int fd, enum interest interest);
diff --git a/test/keyboard.c b/test/keyboard.c
index a76d2a7..1ddbba5 100644
--- a/test/keyboard.c
+++ b/test/keyboard.c
@@ -1,12 +1,13 @@
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "dged/keyboard.h"
+
#include "assert.h"
#include "fake-reactor.h"
#include "test.h"
-#include "keyboard.h"
-#include "unistd.h"
-#include <stdlib.h>
-#include <string.h>
-
struct call_count {
uint32_t poll;
uint32_t reg;
diff --git a/test/main.c b/test/main.c
index 01b2645..4c241b3 100644
--- a/test/main.c
+++ b/test/main.c
@@ -4,8 +4,6 @@
#include <stdlib.h>
#include <time.h>
-#include "lang.h"
-#include "settings.h"
#include "test.h"
void handle_abort() { exit(1); }
@@ -44,6 +42,9 @@ int main() {
printf("\n 📓 \x1b[1;36mRunning settings tests...\x1b[0m\n");
run_settings_tests();
+ printf("\n 🎁 \x1b[1;36mRunning container tests...\x1b[0m\n");
+ run_container_tests();
+
struct timespec elapsed;
clock_gettime(CLOCK_MONOTONIC, &elapsed);
uint64_t elapsed_nanos =
diff --git a/test/minibuffer.c b/test/minibuffer.c
index 4e2b7f1..a6c7f96 100644
--- a/test/minibuffer.c
+++ b/test/minibuffer.c
@@ -2,12 +2,12 @@
#include "stdlib.h"
#include "test.h"
-#include "allocator.h"
-#include "buffer.h"
-#include "command.h"
-#include "display.h"
-#include "minibuffer.h"
-#include "settings.h"
+#include "dged/allocator.h"
+#include "dged/buffer.h"
+#include "dged/command.h"
+#include "dged/display.h"
+#include "dged/minibuffer.h"
+#include "dged/settings.h"
static struct buffer b = {0};
@@ -17,9 +17,8 @@ void *alloc_fn(size_t sz) { return frame_allocator_alloc(g_alloc, sz); }
void init() {
if (b.name == NULL) {
- struct commands commands = command_registry_create(10);
- settings_init(10, &commands);
- b = buffer_create("minibuffer", false);
+ settings_init(10);
+ b = buffer_create("minibuffer");
}
minibuffer_init(&b);
@@ -34,6 +33,7 @@ void destroy() {
void test_minibuffer_echo() {
uint32_t relline, relcol;
+ struct buffer_view view = buffer_view_create(&b, false, false);
// TODO: how to clear this?
struct frame_allocator alloc = frame_allocator_create(1024);
@@ -50,11 +50,13 @@ void test_minibuffer_echo() {
ASSERT(minibuffer_displaying(), "Minibuffer should now have text to display");
minibuffer_clear();
+ buffer_update(&view, 100, 1, list, 0, &relline, &relcol);
ASSERT(!minibuffer_displaying(),
"Minibuffer should have nothing to display after clearing");
minibuffer_echo_timeout(0, "You will not see me");
- buffer_update(&b, 100, 1, list, 0, &relline, &relcol);
+
+ buffer_update(&view, 100, 1, list, 0, &relline, &relcol);
ASSERT(!minibuffer_displaying(),
"A zero timeout echo should be cleared after first update");
diff --git a/test/settings.c b/test/settings.c
index dc448a5..b1fdc9a 100644
--- a/test/settings.c
+++ b/test/settings.c
@@ -1,14 +1,12 @@
-#include "assert.h"
-#include "test.h"
+#include <stdlib.h>
-#include "command.h"
-#include "settings.h"
+#include "dged/settings.h"
-#include <stdlib.h>
+#include "assert.h"
+#include "test.h"
void test_get() {
- struct commands commands = command_registry_create(10);
- settings_init(10, &commands);
+ settings_init(10);
settings_register_setting(
"my.setting",
(struct setting_value){.type = Setting_Bool, .bool_value = false});
@@ -40,8 +38,7 @@ void test_get() {
}
void test_set() {
- struct commands commands = command_registry_create(10);
- settings_init(10, &commands);
+ settings_init(10);
settings_register_setting(
"my.setting",
(struct setting_value){.type = Setting_Bool, .bool_value = false});
diff --git a/test/test.h b/test/test.h
index 047bbfb..b01fde4 100644
--- a/test/test.h
+++ b/test/test.h
@@ -18,5 +18,6 @@ void run_keyboard_tests();
void run_allocator_tests();
void run_minibuffer_tests();
void run_settings_tests();
+void run_container_tests();
#endif
diff --git a/test/text.c b/test/text.c
index cb18df5..3092dcc 100644
--- a/test/text.c
+++ b/test/text.c
@@ -1,13 +1,13 @@
-#include "assert.h"
-#include "stdio.h"
-#include "test.h"
-
-#include "text.h"
-
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
+#include "dged/text.h"
+
+#include "assert.h"
+#include "stdio.h"
+#include "test.h"
+
void assert_line_eq(struct text_chunk line, const char *txt, const char *msg) {
ASSERT(strncmp((const char *)line.text, txt, line.nbytes) == 0, msg);
}
diff --git a/test/undo.c b/test/undo.c
index d0e2fdb..a4b6ad0 100644
--- a/test/undo.c
+++ b/test/undo.c
@@ -1,9 +1,10 @@
+#include <stdlib.h>
+
+#include "dged/undo.h"
+
#include "assert.h"
#include "test.h"
-#include "undo.h"
-#include <stdlib.h>
-
void test_undo_insert() {
struct undo_stack undo;
diff --git a/test/utf8.c b/test/utf8.c
index 88e6a8c..d67c409 100644
--- a/test/utf8.c
+++ b/test/utf8.c
@@ -1,9 +1,10 @@
-#include "utf8.h"
+#include <string.h>
+#include <wchar.h>
+
+#include "dged/utf8.h"
+
#include "assert.h"
#include "test.h"
-#include "wchar.h"
-
-#include <string.h>
void test_nchars_nbytes() {
ASSERT(utf8_nchars((uint8_t *)"👴", strlen("👴")) == 1,