summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2022-11-02 22:20:04 +0100
committerAlbert Cervin <albert@acervin.com>2022-11-16 23:33:49 +0100
commit2f4cb88d5c60f725323739300bb49dfa8923e7d5 (patch)
tree6ec22c2be92eff05f18e5919e747faab56e555ad /test
downloaddged-2f4cb88d5c60f725323739300bb49dfa8923e7d5.tar.gz
dged-2f4cb88d5c60f725323739300bb49dfa8923e7d5.tar.xz
dged-2f4cb88d5c60f725323739300bb49dfa8923e7d5.zip
šŸŽ‰ And so it begins
Diffstat (limited to 'test')
-rw-r--r--test/assert.c20
-rw-r--r--test/assert.h10
-rw-r--r--test/buffer.c46
-rw-r--r--test/main.c24
-rw-r--r--test/test.h11
-rw-r--r--test/text.c82
-rw-r--r--test/utf8.c13
7 files changed, 206 insertions, 0 deletions
diff --git a/test/assert.c b/test/assert.c
new file mode 100644
index 0000000..b252d36
--- /dev/null
+++ b/test/assert.c
@@ -0,0 +1,20 @@
+#include "assert.h"
+
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void assert(bool cond, const char *cond_str, const char *file, int line,
+ const char *msg) {
+ if (!cond) {
+ printf("\n%s:%d: assert failed (%s): %s\n", file, line, cond_str, msg);
+ raise(SIGABRT);
+ }
+}
+
+void assert_streq(const char *left, const char *right, const char *file,
+ int line, const char *msg) {
+ assert(strcmp(left, right) == 0, "<left string> == <right string>", file,
+ line, msg);
+}
diff --git a/test/assert.h b/test/assert.h
new file mode 100644
index 0000000..8b730b2
--- /dev/null
+++ b/test/assert.h
@@ -0,0 +1,10 @@
+#include <stdbool.h>
+
+#define ASSERT(cond, msg) assert(cond, #cond, __FILE__, __LINE__, msg)
+#define ASSERT_STR_EQ(left, right, msg) \
+ assert_streq(left, right, __FILE__, __LINE__, msg)
+
+void assert(bool cond, const char *cond_str, const char *file, int line,
+ const char *msg);
+void assert_streq(const char *left, const char *right, const char *file,
+ int line, const char *msg);
diff --git a/test/buffer.c b/test/buffer.c
new file mode 100644
index 0000000..d7d9b0b
--- /dev/null
+++ b/test/buffer.c
@@ -0,0 +1,46 @@
+#include "assert.h"
+#include "test.h"
+
+#include "buffer.h"
+
+#include <string.h>
+
+void test_move() {
+ struct buffer b = buffer_create("test-buffer");
+ ASSERT(b.dot_col == 0 && b.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,
+ "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,
+ "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));
+ 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,
+ "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));
+ ASSERT(lineindex2 + 1 == 2, "Expected buffer to have two lines");
+ buffer_backward_line(&b);
+ buffer_beginning_of_line(&b);
+ buffer_backward_char(&b);
+ ASSERT(
+ b.dot_col == 0 && b.dot_line == 0,
+ "Expected to not be able to move backwards when at beginning of buffer");
+}
+
+void run_buffer_tests() { run_test(test_move); }
diff --git a/test/main.c b/test/main.c
new file mode 100644
index 0000000..f124f0c
--- /dev/null
+++ b/test/main.c
@@ -0,0 +1,24 @@
+#include <locale.h>
+#include <signal.h>
+#include <stdlib.h>
+
+#include "test.h"
+
+void handle_abort() { exit(1); }
+
+int main() {
+ setlocale(LC_ALL, "");
+ signal(SIGABRT, handle_abort);
+
+ printf("\nšŸŒ \x1b[1;36mRunning utf8 tests...\x1b[0m\n");
+ run_utf8_tests();
+
+ printf("\nšŸ“œ \x1b[1;36mRunning text tests...\x1b[0m\n");
+ run_text_tests();
+
+ printf("\nšŸ•“ļø \x1b[1;36mRunning buffer tests...\x1b[0m\n");
+ run_buffer_tests();
+
+ printf("\nšŸŽ‰ \x1b[1;32mDone! All tests successful!\x1b[0m\n");
+ return 0;
+}
diff --git a/test/test.h b/test/test.h
new file mode 100644
index 0000000..ae6f22d
--- /dev/null
+++ b/test/test.h
@@ -0,0 +1,11 @@
+#include <stdio.h>
+
+#define run_test(fn) \
+ printf(" 🧜 running \x1b[1;36m" #fn "\033[0m... "); \
+ fflush(stdout); \
+ fn(); \
+ printf("\033[32mok!\033[0m\n");
+
+void run_buffer_tests();
+void run_utf8_tests();
+void run_text_tests();
diff --git a/test/text.c b/test/text.c
new file mode 100644
index 0000000..ec99890
--- /dev/null
+++ b/test/text.c
@@ -0,0 +1,82 @@
+#include "assert.h"
+#include "test.h"
+
+#include "text.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+
+void assert_line_equal(struct txt_line *line) {}
+
+void test_add_text() {
+ uint32_t lines_added, cols_added;
+ struct text *t = text_create(10);
+ const char *txt = "This is line 1\n";
+ text_append(t, 0, 0, (uint8_t *)txt, strlen(txt), &lines_added, &cols_added);
+ ASSERT(text_num_lines(t) == 2,
+ "Expected text to have two lines after insertion");
+
+ ASSERT(text_line_size(t, 0) == 14 && text_line_length(t, 0) == 14,
+ "Expected line 1 to have 14 chars and 14 bytes");
+ ASSERT_STR_EQ((const char *)text_get_line(t, 0).text, "This is line 1",
+ "Expected line 1 to be line 1");
+
+ const char *txt2 = "This is line 2\n";
+ text_append(t, 1, 0, (uint8_t *)txt2, strlen(txt2), &lines_added,
+ &cols_added);
+ ASSERT_STR_EQ((const char *)text_get_line(t, 1).text, "This is line 2",
+ "Expected line 2 to be line 2");
+}
+
+void test_delete_text() {
+ uint32_t lines_added, cols_added;
+ struct text *t = text_create(10);
+ const char *txt = "This is line 1";
+ text_append(t, 0, 0, (uint8_t *)txt, strlen(txt), &lines_added, &cols_added);
+
+ text_delete(t, 0, 12, 2);
+ ASSERT(text_line_length(t, 0) == 12,
+ "Expected line to be 12 chars after deleting two");
+ ASSERT(strncmp((const char *)text_get_line(t, 0).text, "This is line",
+ text_line_size(t, 0)) == 0,
+ "Expected two chars to be deleted");
+
+ text_delete(t, 0, 0, 25);
+ ASSERT(text_get_line(t, 0).nbytes == 0,
+ "Expected line to be empty after many chars removed");
+
+ const char *txt2 = "This is line 1\nThis is line 2\nThis is line 3";
+ text_append(t, 0, 0, (uint8_t *)txt2, strlen(txt2), &lines_added,
+ &cols_added);
+ text_delete(t, 1, 11, 3);
+ ASSERT(text_line_length(t, 1) == 11,
+ "Expected line to contain 11 chars after deletion");
+ struct txt_line line = text_get_line(t, 1);
+ ASSERT(strncmp((const char *)line.text, "This is lin", line.nbytes) == 0,
+ "Expected deleted characters to be gone in the second line");
+
+ // test utf-8
+ struct text *t2 = text_create(10);
+ const char *txt3 = "Emojis: šŸ‡«šŸ‡® 🐮\n";
+ text_append(t2, 0, 0, (uint8_t *)txt3, strlen(txt3), &lines_added,
+ &cols_added);
+
+ // TODO: Fix when graphemes are implemented, should be 11, right now it counts
+ // the two unicode code points šŸ‡« and šŸ‡® as two chars.
+ ASSERT(text_line_length(t2, 0) == 12,
+ "Line length should be 12 (even though there "
+ "are more bytes in the line).");
+
+ text_delete(t2, 0, 10, 2);
+ ASSERT(text_line_length(t2, 0) == 10,
+ "Line length should be 10 after deleting the cow emoji and a space");
+ struct txt_line line2 = text_get_line(t2, 0);
+ ASSERT(strncmp((const char *)line2.text, "Emojis: šŸ‡«šŸ‡®", line2.nbytes) == 0,
+ "Expected cow emoji plus space to be deleted");
+}
+
+void run_text_tests() {
+ run_test(test_add_text);
+ run_test(test_delete_text);
+}
diff --git a/test/utf8.c b/test/utf8.c
new file mode 100644
index 0000000..5b020c3
--- /dev/null
+++ b/test/utf8.c
@@ -0,0 +1,13 @@
+#include "utf8.h"
+#include "assert.h"
+#include "test.h"
+#include "wchar.h"
+
+void test_nchars_nbytes() {
+ ASSERT(utf8_nchars((uint8_t *)"šŸ‘“", 2) == 1,
+ "Expected old man emoji to be 1 char");
+ ASSERT(utf8_nbytes((uint8_t *)"šŸ‘“", 1) == 4,
+ "Expected old man emoji to be 4 bytes");
+}
+
+void run_utf8_tests() { run_test(test_nchars_nbytes); }