Revise testdir/T.split per PR #42.

This commit is contained in:
Arnold D. Robbins 2019-07-16 20:50:23 +03:00
parent 891690942a
commit 147521b831

View File

@ -1,41 +1,119 @@
echo T.split: misc tests of field splitting and split command
#!/bin/sh
awk=${awk-../a.out}
echo a:bc:def >foo0
echo a:bc:def >foo1
$awk '{ FS = ":"; print $1 }' foo0 >foo2
diff foo1 foo2 || echo 'BAD: T.split 0.1'
WORKDIR=$(mktemp -d /tmp/nawktest.XXXXXX)
echo a:bc:def >foo0
echo 1 >foo1
$awk '{ FS = ":"; print NF }' foo0 >foo2
diff foo1 foo2 || echo 'BAD: T.split 0.2'
TEMP0=$WORKDIR/test.temp.0
TEMP1=$WORKDIR/test.temp.1
TEMP2=$WORKDIR/test.temp.2
RESULT=0
fail() {
echo "$1" >&2
RESULT=1
}
echo T.split: misc tests of field splitting and split command
$awk 'BEGIN {
# Assign string to $0, then change FS.
FS = ":"
$0="a:bc:def"
FS = "-"
print FS, $1, NF
# Assign number to $0, then change FS.
FS = "2"
$0=1212121
FS="3"
print FS, $1, NF
}' > $TEMP1
echo '- a 3
3 1 4' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.1'
$awk 'BEGIN {
# FS changes after getline.
FS = ":"
"echo a:bc:def" | getline
FS = "-"
print FS, $1, NF
}' > $TEMP1
echo '- a 3' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.2'
echo '
a
a:b
c:d:e
e:f:g:h' >foo0
e:f:g:h' > $TEMP0
$awk 'BEGIN {
FS = ":"
while (getline <"foo0" > 0)
while (getline <"'$TEMP0'" > 0)
print NF
}' >foo1
}' > $TEMP1
echo '0
1
2
3
4' >foo2
diff foo1 foo2 || echo 'BAD: T.split 0.3'
4' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.3'
# getline var shouldn't impact fields.
echo 'f b a' > $TEMP0
$awk '{
FS = ":"
getline a < "/etc/passwd"
print $1
}' $TEMP0 > $TEMP1
echo 'f' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.4'
echo 'a b c d
foo
e f g h i
bar' > $TEMP0
$awk '{
FS=":"
getline v
print $2, NF
FS=" "
}' $TEMP0 > $TEMP1
echo 'b 4
f 5' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.5'
echo 'a.b.c=d.e.f
g.h.i=j.k.l
m.n.o=p.q.r' > $TEMP0
echo 'b
h
n' > $TEMP1
$awk 'BEGIN { FS="=" } { FS="."; $0=$1; print $2; FS="="; }' $TEMP0 > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split (record assignment 1)'
echo 'a.b.c=d.e.f
g.h.i=j.k.l
m.n.o=p.q.r' > $TEMP0
echo 'd.e.f
b
j.k.l
h
p.q.r
n' > $TEMP1
$awk 'BEGIN { FS="=" } { print $2; FS="."; $0=$1; print $2; FS="="; }' $TEMP0 > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split (record assignment 2)'
echo 'abc
de
f
' >foo0
who | sed 10q >> foo0
sed 10q /etc/passwd >> foo0
' > $TEMP0
who | sed 10q >> $TEMP0
sed 10q /etc/passwd >> $TEMP0
$awk '
{ n = split($0, x, "")
@ -46,11 +124,11 @@ $awk '
s = s x[i]
if (s != $0) print "error 2", NR
print s
}' foo0 >foo1
}' $TEMP0 > $TEMP1
diff foo0 foo1 || echo 'BAD: T.split 1'
diff $TEMP0 $TEMP1 || fail 'BAD: T.split 1'
# assumes same foo0! bad design
# assumes same test.temp.0! bad design
$awk '
@ -62,9 +140,9 @@ $awk '
s = s x[i]
if (s != $0) print "error 2", NR
print s
}' foo0 >foo1
}' $TEMP0 > $TEMP1
diff foo0 foo1 || echo 'BAD: T.split //'
diff $TEMP0 $TEMP1 || fail 'BAD: T.split //'
$awk '
BEGIN { FS = "" }
@ -76,11 +154,11 @@ BEGIN { FS = "" }
s = s x[i]
if (s != $0) print "error 2", NR
print s
}' foo0 >foo2
}' $TEMP0 > $TEMP2
diff foo0 foo2 || echo 'BAD: T.split 2'
diff $TEMP0 $TEMP2 || fail 'BAD: T.split 2'
# assumes same foo0!
# assumes same test.temp.0!
$awk '
BEGIN { FS = "" }
@ -92,45 +170,55 @@ BEGIN { FS = "" }
s = s $i
if (s != $0) print "error 2", NR
print s
}' foo0 >foo3
}' $TEMP0 > $TEMP2
diff foo0 foo3 || echo 'BAD: T.split 3'
diff $TEMP0 $TEMP2 || fail 'BAD: T.split 3'
$awk '
{ n = split( $0, temp, /^@@@ +/ )
print n
}' >foo1 <<XXX
}' > $TEMP1 <<XXX
@@@ xxx
@@@ xxx
@@@ xxx
XXX
echo '2
2
2' >foo2
diff foo1 foo2 || echo 'BAD: T.split 4'
2' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split 4'
rm foo*
rm -f $WORKDIR/test.temp*
echo '
a
bc
def' >foo0
def' > $TEMP0
$awk '
{ print split($0, x, "")
}' foo0 >foo1
}' $TEMP0 > $TEMP1
echo '0
1
2
3' >foo2
diff foo1 foo2 || echo 'BAD: T.split null 3rd arg'
3' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split null 3rd arg'
rm foo*
rm -f $WORKDIR/test.temp*
$awk 'BEGIN {
a[1]="a b"
print split(a[1],a),a[1],a[2]
}' >foo1
}' > $TEMP1
echo '2 a b' >foo2
diff foo1 foo2 || echo 'BAD: T.split(a[1],a)'
echo '2 a b' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split(a[1],a)'
$awk 'BEGIN {
a = "cat\n\n\ndog"
split(a, b, "[\r\n]+")
print b[1], b[2]
}' > $TEMP1
echo 'cat dog' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split(a, b, "[\r\n]+")'
exit $RESULT