1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2012 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                 Eclipse Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#          http://www.eclipse.org/org/documents/epl-v10.html           #
11#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27
28Command=${0##*/}
29integer Errors=0
30
31tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32trap "cd /; rm -rf $tmp" EXIT
33
34tmp1=$tmp/tmp1.csv
35tmp2=$tmp/tmp2.csv
36cat > $tmp1 <<- \EOF
37	CAT,"CVE CCODE","NECA OCN",ST,LATA,AP,"New InterState
38	Orig","New Inter""""State
39	Term","New IntraState
40	Orig","New IntraState
41	Term"
42	CLEC,XXXX,AAAA,RB,ABC,comp," 0.2 "," 0.4 "," 0.6 "," 0.8 "
43	CLEC,YYYY,QQQQ,SX,123,mmmm," 0.3 "," 0.5 "," 0.7 "," 0.9 "
44EOF
45integer count=0 nfields
46IFS=${2-,}
47for j in 1 2
48do	typeset -a arr
49	while read -A -S arr
50	do	((nfields=${#arr[@]}))
51		if	((++count==1))
52		then	((nfields==10)) || err_exit 'first record should contain 10 fields'
53			[[ ${arr[7]} == $'New Inter""State\nTerm' ]] || err_exit $'7th field of record 1 should contain New Inter""State\nTerm'
54		fi
55		for	((i=0; i < nfields;i++))
56		do	delim=$IFS
57			if	((i == nfields-1))
58			then	delim=$'\r\n'
59			fi
60			if ((i==1))
61			then	printf "%#q%s"  "${arr[i]}" "$delim"
62			else	printf "%(csv)q%s"  "${arr[i]}" "$delim"
63			fi
64		done
65	done < $tmp1 > $tmp2
66done
67diff "$tmp1" "$tmp2" >/dev/null 2>&1 || err_exit "files $tmp1 and $tmp2 differ"
68
69exit $((Errors<125?Errors:125))
70
71