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