commit 3c88c1a6fece6f3e92dc4876fbc9609394d22f47
parent 0f8af7612e393d40705e953306bc0555f026c2f7
Author: Jan Klemkow <j.klemkow@wemelug.de>
Date: Sat, 9 Jul 2016 20:33:20 +0200
add tests
Diffstat:
M | Makefile | | | 2 | +- |
A | sl_test.c | | | 114 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 115 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
@@ -9,7 +9,7 @@ LIBS = -L/usr/local/lib -lutf
all: lchat
clean:
- rm -f lchat *.o *.core
+ rm -f lchat *.o *.core sl_test
test: sl_test
./sl_test
diff --git a/sl_test.c b/sl_test.c
@@ -0,0 +1,114 @@
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "slackline.h"
+
+static void
+strokes(struct slackline *sl, const char *str)
+{
+ for (const char *c = str; *c != '\0'; c++)
+ sl_keystroke(sl, *c);
+}
+
+static void
+print_state(struct slackline *sl)
+{
+ printf("rcur: %zu bcur: %zu rlen: %zu blen: %zu buf: \"%s\" ",
+ sl->rcur, sl->bcur, sl->rlen, sl->blen, sl->buf);
+
+ for (size_t i = 0; i < strlen(sl->buf); i++)
+ printf("%2x", sl->buf[i]);
+
+ putchar('\n');
+}
+
+static void
+check_init(struct slackline *sl)
+{
+ assert(sl != NULL);
+ assert(sl->buf != NULL);
+ assert(sl->ptr == sl->buf);
+ assert(sl->rlen == 0);
+ assert(sl->blen == 0);
+ assert(sl->rcur== 0);
+ assert(sl->bcur== 0);
+ assert(sl->last == sl->buf);
+}
+
+static void
+check_ascii(struct slackline *sl)
+{
+ strokes(sl, "abc");
+ assert(sl->blen == 3);
+ assert(sl->rlen == 3);
+ assert(sl->bcur == 3);
+ assert(sl->rcur == 3);
+ assert(sl->last - sl->buf == sl->blen);
+
+ strokes(sl, "\x08"); /* backspace */
+ assert(sl->blen == 2);
+ assert(sl->rlen == 2);
+ assert(sl->bcur == 2);
+ assert(sl->rcur == 2);
+ assert(sl->last - sl->buf == sl->blen);
+
+ strokes(sl, "\x1b[D"); /* left arrow key */
+ assert(sl->blen == 2);
+ assert(sl->rlen == 2);
+ assert(sl->bcur == 1);
+ assert(sl->rcur == 1);
+ assert(sl->last - sl->buf == sl->blen);
+}
+
+static void
+check_utf8(struct slackline *sl)
+{
+ /* ae | oe | ue */
+ strokes(sl, "\xC3\xA4\xC3\xBC\xC3\xB6");
+ assert(sl->blen == 6);
+ assert(sl->rlen == 3);
+ assert(sl->bcur == 6);
+ assert(sl->rcur == 3);
+ assert(sl->last - sl->buf == sl->blen);
+
+ strokes(sl, "\x08"); /* backspace */
+ assert(sl->blen == 4);
+ assert(sl->rlen == 2);
+ assert(sl->bcur == 4);
+ assert(sl->rcur == 2);
+ assert(sl->last - sl->buf == sl->blen);
+
+ strokes(sl, "\x1b[D"); /* left arrow key */
+ assert(sl->blen == 4);
+ assert(sl->rlen == 2);
+ assert(sl->bcur == 2);
+ assert(sl->rcur == 1);
+ assert(sl->last - sl->buf == sl->blen);
+
+ strokes(sl, "\x1b[C"); /* right arrow key */
+ assert(sl->blen == 4);
+ assert(sl->rlen == 2);
+ assert(sl->bcur == 4);
+ assert(sl->rcur == 2);
+ assert(sl->last - sl->buf == sl->blen);
+}
+
+int
+main(void)
+{
+ struct slackline *sl;
+
+ sl = sl_init();
+ check_init(sl);
+ check_ascii(sl);
+
+ sl_reset(sl);
+ check_init(sl);
+ check_utf8(sl);
+
+ sl_free(sl);
+
+ return EXIT_SUCCESS;
+}