765575e067f8cf4fbe4ea85f93de59599a6d5282
[shellsnippets/shellsnippets.git] / mksh / ssv / test-c.sh
1 #!/bin/mksh
2 #-
3 # Copyright © 2017
4 #       mirabilos <mirabilos@evolvis.org>
5 #
6 # Provided that these terms and disclaimer and all copyright notices
7 # are retained or reproduced in an accompanying document, permission
8 # is granted to deal in this work without restriction, including un‐
9 # limited rights to use, publicly perform, distribute, sell, modify,
10 # merge, give away, or sublicence.
11 #
12 # This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
13 # the utmost extent permitted by applicable law, neither express nor
14 # implied; without malicious intent or gross negligence. In no event
15 # may a licensor, author or contributor be held liable for indirect,
16 # direct, other damage, loss, or other issues arising in any way out
17 # of dealing in the work, even if advised of the possibility of such
18 # damage or existence of a defect, except proven that it results out
19 # of said person’s immediate fault when using the work as intended.
20
21 export LC_ALL=C; unset LANGUAGE
22 cd "$(dirname "$0")"
23 grv=0
24 gcc -Wall -fprofile-arcs -ftest-coverage csv2ssv.c -o csv2ssv || exit 255
25
26 runtest() {
27         local name=$1; shift
28
29         [[ $name = -E* ]] || cat >x.csv
30         if [[ $name = -* ]]; then
31                 ./csv2ssv "$@" x.csv >/dev/null 2>&1
32                 set -A rvs -- ${PIPESTATUS[@]} 0
33         else
34                 ./csv2ssv "$@" x.csv | \
35                     diff -u --label expected --label got /dev/fd/4 -
36                 set -A rvs -- ${PIPESTATUS[@]}
37         fi
38         set -A rvr
39         rv=0
40         case ${rvs[1]} {
41         (0)
42                 ;;
43         (1)
44                 rv=1
45                 rvr+=(diff-delta)
46                 ;;
47         (*)
48                 rv=1
49                 rvr+=(diff-trouble-${rvs[1]})
50                 ;;
51         }
52         case ${rvs[0]} {
53         (0)
54                 if [[ $name = -* ]]; then
55                         rv=1
56                         rvr+=(expected-fail)
57                 fi
58                 ;;
59         (*)
60                 if [[ $name != -* ]]; then
61                         rv=1
62                         rvr+=(unexpected-${rvs[0]})
63                 fi
64                 ;;
65         }
66         if (( rv )); then
67                 print -r -- "FAIL $name ${rvr[@]}"
68                 grv=1
69         else
70                 print -r -- "pass $name"
71         fi
72 }
73
74 runtest usage -h <<\EOI 4<<\EOO
75 EOI
76 EOO
77
78 runtest -usage -? <<\EOI
79 EOI
80
81 runtest -args foo <<\EOI
82 EOI
83
84 :>x.csv
85 runtest -Esize
86
87 chmod 000 x.csv
88 runtest -Eopen
89
90 rm -f x.csv
91
92 runtest basic-quoted -s , -q \" <<\EOI 4<<\EOO
93 Key,Value
94 first,1\"23
95 4""5,6"7"8\"
96 second,a\r\r\rb
97 third,a\rb\rc\r
98 fourth,"a\r\rb\r
99 \r
100 c
101 d"
102 fifth,a\r\r\r
103 sixth,"a\r
104 b"\r
105 EOI
106 Key\1cValue
107 first\1c1\23\r4"5,678\
108 second\1ca\r\r\rb
109 third\1ca\rb\rc
110 fourth\1ca\r\rb\r\rc\rd
111 fifth\1ca
112 sixth\1ca\rb
113 EOO
114 #"
115
116 runtest basic-unquoted -s , <<\EOI 4<<\EOO
117 Key,Value
118 first,1\"23
119 4""5,6"7"8\"
120 second,a\r\r\rb
121 third,a\rb\rc\r
122 fourth,"a\r\rb\r
123 \r
124 c
125 d"
126 fifth,a\r\r\r
127 EOI
128 Key\1cValue
129 first\1c1\"23
130 4""5\1c6"7"8\"
131 second\1ca\r\r\rb
132 third\1ca\rb\rc
133 fourth\1c"a\r\rb
134
135 c
136 d"
137 fifth\1ca
138 EOO
139
140 print 'a\0b' >x.csv
141 runtest -Enormalnul
142
143 runtest -normalsep <<\EOI
144 a\1cb
145 EOI
146
147 print '"a\0b"' >x.csv
148 runtest -Eqnul -q \"
149
150 runtest -qsep -q \" <<\EOI
151 "a\1cb"
152 EOI
153
154 print -nr -- "x" >x.csv
155 runtest -Eueof
156
157 print -nr -- "x\r" >x.csv
158 runtest -Ecreof
159
160 runtest -qeof -q \" <<\EOI
161 foo"bar
162 EOI
163 #"
164
165 gcov csv2ssv.c || exit 255
166 exit $grv