summaryrefslogtreecommitdiff
path: root/regress/lib
diff options
context:
space:
mode:
Diffstat (limited to 'regress/lib')
-rw-r--r--regress/lib/libedit/read/Makefile18
-rw-r--r--regress/lib/libedit/read/test_read_char.c114
-rw-r--r--regress/lib/libedit/read/test_read_char.sh46
3 files changed, 178 insertions, 0 deletions
diff --git a/regress/lib/libedit/read/Makefile b/regress/lib/libedit/read/Makefile
new file mode 100644
index 00000000000..5f4539e2251
--- /dev/null
+++ b/regress/lib/libedit/read/Makefile
@@ -0,0 +1,18 @@
+# $OpenBSD: Makefile,v 1.1 2016/02/11 15:37:20 schwarze Exp $
+#
+# Author: Ingo Schwarze <schwarze@openbsd.org>, 2016. Public Domain.
+#
+# Run "cd /usr/src/lib/libedit && make obj && make depend" first.
+
+REGRESS_TARGETS = check
+
+PROG = test_read_char
+CPPFLAGS += -DWIDECHAR
+CPPFLAGS += -I${.CURDIR}/../../../../lib/libedit
+CPPFLAGS += -I${.OBJDIR}/../../../../lib/libedit
+MAN =
+
+check:
+ sh ${.CURDIR}/test_read_char.sh
+
+.include <bsd.regress.mk>
diff --git a/regress/lib/libedit/read/test_read_char.c b/regress/lib/libedit/read/test_read_char.c
new file mode 100644
index 00000000000..94487134c05
--- /dev/null
+++ b/regress/lib/libedit/read/test_read_char.c
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <err.h>
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "read.c"
+
+/*
+ * Glue for unit tests of libedit/read.c.
+ * Rather than linking in all the various libedit modules,
+ * provide dummies for those functions called in read.c.
+ * Most aren't actually called in read_char().
+ * Requires "make obj && make depend" in src/lib/libedit.
+ */
+
+#define EL EditLine *el __attribute__((__unused__))
+#define UU __attribute__((__unused__))
+
+int ch_enlargebufs(EL, size_t addlen UU) { return 1; }
+void ch_reset(EL, int mclear UU) { }
+void el_resize(EL) { }
+int el_set(EL, int op UU, ...) { return 0; }
+void re_clear_display(EL) { }
+void re_clear_lines(EL) { }
+void re_refresh(EL) { }
+void re_refresh_cursor(EL) { }
+void sig_clr(EL) { }
+void sig_set(EL) { }
+void terminal__flush(EL) { }
+void terminal_beep(EL) { }
+int tty_cookedmode(EL) { return 0; }
+int tty_rawmode(EL) { return 0; }
+
+int
+keymacro_get(EL, Char *ch, keymacro_value_t *val)
+{
+ val->str = NULL;
+ *ch = '\0';
+ return XK_STR;
+}
+
+#undef EL
+#undef UU
+
+/*
+ * Unit test steering program for editline/read.c, read_char().
+ * Reads from standard input until read_char() returns 0.
+ * Writes the code points read to standard output in %x format.
+ * If EILSEQ is set after read_char(), indicating that there were some
+ * garbage bytes before the character, the code point gets * prefixed.
+ * The return value is indicated by appending to the code point:
+ * a comma for 1, a full stop for 0, [%d] otherwise.
+ * Errors out on unexpected failure (setlocale failure, malloc
+ * failure, or unexpected errno).
+ * Since ENOMSG is very unlikely to occur, it is used to make
+ * sure that read_char() doesn't clobber errno.
+ */
+
+int
+main(void)
+{
+ EditLine el;
+ int irc;
+ Char cp;
+
+ if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL)
+ err(1, "setlocale");
+ el.el_flags = CHARSET_IS_UTF8;
+ el.el_infd = STDIN_FILENO;
+ if ((el.el_signal = calloc(1, sizeof(*el.el_signal))) == NULL)
+ err(1, NULL);
+ do {
+ errno = ENOMSG;
+ irc = read_char(&el, &cp);
+ switch (errno) {
+ case ENOMSG:
+ break;
+ case EILSEQ:
+ putchar('*');
+ break;
+ default:
+ err(1, NULL);
+ }
+ printf("%x", cp);
+ switch (irc) {
+ case 1:
+ putchar(',');
+ break;
+ case 0:
+ putchar('.');
+ break;
+ default:
+ printf("[%d]", irc);
+ break;
+ }
+ } while (irc != 0);
+ return 0;
+}
diff --git a/regress/lib/libedit/read/test_read_char.sh b/regress/lib/libedit/read/test_read_char.sh
new file mode 100644
index 00000000000..5ed2f1b7f41
--- /dev/null
+++ b/regress/lib/libedit/read/test_read_char.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+#
+# Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+testrc()
+{
+ stdin=$1
+ expected=$2
+ result=`echo -n "$stdin" | ./test_read_char`
+ if [ "$result" != "${expected}" ]; then
+ echo "input: >>>$stdin<<<"
+ echo "expected: >>>$expected<<<"
+ echo "result: >>>$result<<<"
+ exit 1;
+ fi
+}
+
+testrc "" "0."
+testrc "a" "61,0."
+testrc "ab" "61,62,0."
+testrc "\0303\0251" "e9,0." # valid UTF-8
+testrc "\0303" "0." # incomplete UTF-8
+testrc "\0303a" "*61,0."
+testrc "\0303ab" "*61,62,0."
+testrc "\0355\0277\0277ab" "*61,62,0." # surrogate
+testrc "\0200" "*0." # isolated continuation byte
+testrc "\0200ab" "*61,62,0."
+testrc "a\0200bc" "61,*62,63,0."
+testrc "\0200\0303\0251" "*e9,0."
+testrc "\0200\0303ab" "*61,62,0."
+testrc "\0377ab" "*61,62,0." # invalid byte
+testrc "\0355\0277\0377ab" "*61,62,0."
+
+exit 0