--- /dev/null
+#!/bin/mksh
+#-
+# Copyright © 2017
+# mirabilos <mirabilos@evolvis.org>
+#
+# Provided that these terms and disclaimer and all copyright notices
+# are retained or reproduced in an accompanying document, permission
+# is granted to deal in this work without restriction, including un‐
+# limited rights to use, publicly perform, distribute, sell, modify,
+# merge, give away, or sublicence.
+#
+# This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
+# the utmost extent permitted by applicable law, neither express nor
+# implied; without malicious intent or gross negligence. In no event
+# may a licensor, author or contributor be held liable for indirect,
+# direct, other damage, loss, or other issues arising in any way out
+# of dealing in the work, even if advised of the possibility of such
+# damage or existence of a defect, except proven that it results out
+# of said person’s immediate fault when using the work as intended.
+
+export LC_ALL=C; unset LANGUAGE
+cd "$(dirname "$0")"
+grv=0
+gcc -Wall -fprofile-arcs -ftest-coverage csv2ssv.c -o csv2ssv || exit 255
+
+runtest() {
+ local name=$1; shift
+
+ [[ $name = -E* ]] || cat >x.csv
+ if [[ $name = -* ]]; then
+ ./csv2ssv "$@" x.csv >/dev/null 2>&1
+ set -A rvs -- ${PIPESTATUS[@]} 0
+ else
+ ./csv2ssv "$@" x.csv | \
+ diff -u --label expected --label got /dev/fd/4 -
+ set -A rvs -- ${PIPESTATUS[@]}
+ fi
+ set -A rvr
+ rv=0
+ case ${rvs[1]} {
+ (0)
+ ;;
+ (1)
+ rv=1
+ rvr+=(diff-delta)
+ ;;
+ (*)
+ rv=1
+ rvr+=(diff-trouble-${rvs[1]})
+ ;;
+ }
+ case ${rvs[0]} {
+ (0)
+ if [[ $name = -* ]]; then
+ rv=1
+ rvr+=(expected-fail)
+ fi
+ ;;
+ (*)
+ if [[ $name != -* ]]; then
+ rv=1
+ rvr+=(unexpected-${rvs[0]})
+ fi
+ ;;
+ }
+ if (( rv )); then
+ print -r -- "FAIL $name ${rvr[@]}"
+ grv=1
+ else
+ print -r -- "pass $name"
+ fi
+}
+
+runtest usage -h <<\EOI 4<<\EOO
+EOI
+EOO
+
+runtest -usage -? <<\EOI
+EOI
+
+runtest -args foo <<\EOI
+EOI
+
+:>x.csv
+runtest -Esize
+
+chmod 000 x.csv
+runtest -Eopen
+
+rm -f x.csv
+
+runtest basic-quoted -s , -q \" <<\EOI 4<<\EOO
+Key,Value
+first,1\"23
+4""5,6"7"8\"
+second,a\r\r\rb
+third,a\rb\rc\r
+fourth,"a\r\rb\r
+\r
+c
+d"
+fifth,a\r\r\r
+sixth,"a\r
+b"\r
+EOI
+Key\1cValue
+first\1c1\23\r4"5,678\
+second\1ca\r\r\rb
+third\1ca\rb\rc
+fourth\1ca\r\rb\r\rc\rd
+fifth\1ca
+sixth\1ca\rb
+EOO
+#"
+
+runtest basic-unquoted -s , <<\EOI 4<<\EOO
+Key,Value
+first,1\"23
+4""5,6"7"8\"
+second,a\r\r\rb
+third,a\rb\rc\r
+fourth,"a\r\rb\r
+\r
+c
+d"
+fifth,a\r\r\r
+EOI
+Key\1cValue
+first\1c1\"23
+4""5\1c6"7"8\"
+second\1ca\r\r\rb
+third\1ca\rb\rc
+fourth\1c"a\r\rb
+
+c
+d"
+fifth\1ca
+EOO
+
+print 'a\0b' >x.csv
+runtest -Enormalnul
+
+runtest -normalsep <<\EOI
+a\1cb
+EOI
+
+print '"a\0b"' >x.csv
+runtest -Eqnul -q \"
+
+runtest -qsep -q \" <<\EOI
+"a\1cb"
+EOI
+
+print -nr -- "x" >x.csv
+runtest -Eueof
+
+print -nr -- "x\r" >x.csv
+runtest -Ecreof
+
+runtest -qeof -q \" <<\EOI
+foo"bar
+EOI
+#"
+
+gcov csv2ssv.c || exit 255
+exit $grv