1#!/bin/sh
2
3echo T.csv: tests of csv field splitting, no embedded newlines
4
5awk=${awk-../a.out}
6
7$awk '
8BEGIN {
9	FS = "\t"
10	awk = "../a.out --csv"
11}
12NF == 0 || $1 ~ /^#/ {
13	next
14}
15$1 ~ /try/ {	# new test
16	nt++
17	sub(/try /, "")
18	prog = $0
19	printf("%3d  %s\n", nt, prog)
20	prog = sprintf("%s '"'"'%s'"'"'", awk, prog)
21	# print "prog is", prog
22	nt2 = 0
23	while (getline > 0) {
24		if (NF == 0)	# blank line terminates a sequence
25			break
26		input = $1
27		for (i = 2; i < NF; i++)	# input data
28			input = input "\t" $i
29		test = sprintf("./echo '"'"'%s'"'"' | %s >foo1; ",
30			input, prog)
31		if ($NF == "\"\"")
32			output = ">foo2;"
33		else
34			output = sprintf("./echo '"'"'%s'"'"' >foo2; ", $NF)
35		gsub(/\\t/, "\t", output)
36		gsub(/\\n/, "\n", output)
37		run = sprintf("cmp foo1 foo2 || echo test %d.%d failed",
38			nt, ++nt2)
39		# print  "input is", input
40		# print  "test is", test
41		# print  "output is", output
42		# print  "run is", run
43		system(test output run)
44	}
45	tt += nt2
46}
47END { print tt, "tests" }
48' <<\!!!!
49# General format:
50# try program as rest of line
51# $1	$2	$3	output1  (\t for tab, \n for newline,
52# $1	$2	$3	output2  ("" for null)
53# ... terminated by blank line
54
55
56try  { for (i=1; i<=NF; i++) printf("[%s]", $i); printf("\n") }
57a	[a]
58  a	[  a]
59,a	[][a]
60 , a	[ ][ a]
61a,b	[a][b]
62a,b,c	[a][b][c]
63""	[]
64"abc"	[abc]
65"a""b"	[a"b]
66"a","b"	[a][b]
67a""b	[a""b]
68"a,b"	[a,b]
69""""	["]
70""""""	[""]
71"""x"""	["x"]
72""","""	[","]
73,,""	[][][]
74a""b	[a""b]
75a''b	[a''b]
76,,	[][][]
77a,	[a][]
78"",	[][]
79,	[][]
80!!!!
81