add a testsuite, align CR handling with Java™ code
authormirabilos <t.glaser@tarent.de>
Tue, 21 Feb 2017 21:42:00 +0000 (22:42 +0100)
committermirabilos <t.glaser@tarent.de>
Tue, 21 Feb 2017 21:42:00 +0000 (22:42 +0100)
mksh/ssv/csv2ssv.c
mksh/ssv/test-c.sh [new file with mode: 0644]

index 8543f2d..ec0b82f 100644 (file)
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2015
+ * Copyright (c) 2015, 2017
  *     mirabilos <mirabilos@evolvis.org>
  *
  * Provided that these terms and disclaimer and all copyright notices
@@ -97,11 +97,14 @@ main(int argc, char *argv[])
        setlinebuf(stdout);
 
        while (cp <= ep) {
+ normal:
                switch ((c = *cp++)) {
                case 0x00:
                case 0x1C:
                        errx(1, "\\x%02X found at offset %zu",
                            (unsigned int)c, (size_t)(cp - mp) - 1);
+               case 0x0D:
+                       break;
                case 0x0A:
                        if (cp > ep)
                                goto nl_out;
@@ -121,6 +124,24 @@ main(int argc, char *argv[])
                        continue;
                }
 
+               if (c != cq) {
+                       /* 0x0D */
+                       while (cp <= ep) {
+                               switch ((c = *cp++)) {
+                               case 0x0A:
+                                       bp = cp;
+                                       /* FALLTHROUGH */
+                               default:
+                                       --cp;
+                                       --bp;
+                                       goto normal;
+                               case 0x0D:
+                                       break;
+                               }
+                       }
+                       continue;
+               }
+
                /* c == cq */
                while (cp <= ep) {
                        if (!(c = *cp++) || c == 0x1C) {
diff --git a/mksh/ssv/test-c.sh b/mksh/ssv/test-c.sh
new file mode 100644 (file)
index 0000000..765575e
--- /dev/null
@@ -0,0 +1,166 @@
+#!/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