1*23f24377SWarner Loshecho T.func: test user-defined functions 2*23f24377SWarner Losh 3*23f24377SWarner Loshawk=${awk-../a.out} 4*23f24377SWarner Losh 5*23f24377SWarner Loshecho '10 2 6*23f24377SWarner Losh2 10 7*23f24377SWarner Losh10 10 8*23f24377SWarner Losh10 1e1 9*23f24377SWarner Losh1e1 9' | $awk ' 10*23f24377SWarner Losh# tests whether function returns sensible type bits 11*23f24377SWarner Losh 12*23f24377SWarner Loshfunction assert(cond) { # assertion 13*23f24377SWarner Losh if (cond) print 1; else print 0 14*23f24377SWarner Losh} 15*23f24377SWarner Losh 16*23f24377SWarner Loshfunction i(x) { return x } 17*23f24377SWarner Losh 18*23f24377SWarner Losh{ m=$1; n=i($2); assert(m>n) } 19*23f24377SWarner Losh' >foo1 20*23f24377SWarner Loshecho '1 21*23f24377SWarner Losh0 22*23f24377SWarner Losh0 23*23f24377SWarner Losh0 24*23f24377SWarner Losh1' >foo2 25*23f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.func (function return type)' 26*23f24377SWarner Losh 27*23f24377SWarner Loshecho 'data: data' >foo1 28*23f24377SWarner Losh$awk ' 29*23f24377SWarner Loshfunction test1(array) { array["test"] = "data" } 30*23f24377SWarner Loshfunction test2(array) { return(array["test"]) } 31*23f24377SWarner LoshBEGIN { test1(foo); print "data: " test2(foo) } 32*23f24377SWarner Losh' >foo2 33*23f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.func (array type)' 34*23f24377SWarner Losh 35*23f24377SWarner Losh$awk ' 36*23f24377SWarner LoshBEGIN { code() } 37*23f24377SWarner LoshEND { codeout("x") } 38*23f24377SWarner Loshfunction code() { ; } 39*23f24377SWarner Loshfunction codeout(ex) { print ex } 40*23f24377SWarner Losh' /dev/null >foo1 41*23f24377SWarner Loshecho x >foo2 42*23f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.func (argument passing)' 43*23f24377SWarner Losh 44*23f24377SWarner Losh$awk ' 45*23f24377SWarner LoshBEGIN { unireghf() } 46*23f24377SWarner Losh 47*23f24377SWarner Loshfunction unireghf(hfeed) { 48*23f24377SWarner Losh hfeed[1]=0 49*23f24377SWarner Losh rcell("foo",hfeed) 50*23f24377SWarner Losh hfeed[1]=0 51*23f24377SWarner Losh rcell("bar",hfeed) 52*23f24377SWarner Losh} 53*23f24377SWarner Losh 54*23f24377SWarner Loshfunction rcell(cellname,hfeed) { 55*23f24377SWarner Losh print cellname 56*23f24377SWarner Losh} 57*23f24377SWarner Losh' >foo1 58*23f24377SWarner Loshecho "foo 59*23f24377SWarner Loshbar" >foo2 60*23f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.func (convert arg to array)' 61*23f24377SWarner Losh 62*23f24377SWarner Losh$awk ' 63*23f24377SWarner Loshfunction f(n) { 64*23f24377SWarner Losh if (n <= 1) 65*23f24377SWarner Losh return 1 66*23f24377SWarner Losh else 67*23f24377SWarner Losh return n * f(n-1) 68*23f24377SWarner Losh} 69*23f24377SWarner Losh{ print f($1) } 70*23f24377SWarner Losh' <<! >foo2 71*23f24377SWarner Losh0 72*23f24377SWarner Losh1 73*23f24377SWarner Losh2 74*23f24377SWarner Losh3 75*23f24377SWarner Losh4 76*23f24377SWarner Losh5 77*23f24377SWarner Losh6 78*23f24377SWarner Losh7 79*23f24377SWarner Losh8 80*23f24377SWarner Losh9 81*23f24377SWarner Losh! 82*23f24377SWarner Loshcat <<! >foo1 83*23f24377SWarner Losh1 84*23f24377SWarner Losh1 85*23f24377SWarner Losh2 86*23f24377SWarner Losh6 87*23f24377SWarner Losh24 88*23f24377SWarner Losh120 89*23f24377SWarner Losh720 90*23f24377SWarner Losh5040 91*23f24377SWarner Losh40320 92*23f24377SWarner Losh362880 93*23f24377SWarner Losh! 94*23f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.func (factorial)' 95*23f24377SWarner Losh 96*23f24377SWarner Losh$awk ' 97*23f24377SWarner Loshfunction ack(m,n) { 98*23f24377SWarner Losh k = k+1 99*23f24377SWarner Losh if (m == 0) return n+1 100*23f24377SWarner Losh if (n == 0) return ack(m-1, 1) 101*23f24377SWarner Losh return ack(m-1, ack(m, n-1)) 102*23f24377SWarner Losh} 103*23f24377SWarner Losh{ k = 0; print ack($1,$2), "(" k " calls)" } 104*23f24377SWarner Losh' <<! >foo2 105*23f24377SWarner Losh0 0 106*23f24377SWarner Losh1 1 107*23f24377SWarner Losh2 2 108*23f24377SWarner Losh3 3 109*23f24377SWarner Losh3 4 110*23f24377SWarner Losh3 5 111*23f24377SWarner Losh! 112*23f24377SWarner Loshcat <<! >foo1 113*23f24377SWarner Losh1 (1 calls) 114*23f24377SWarner Losh3 (4 calls) 115*23f24377SWarner Losh7 (27 calls) 116*23f24377SWarner Losh61 (2432 calls) 117*23f24377SWarner Losh125 (10307 calls) 118*23f24377SWarner Losh253 (42438 calls) 119*23f24377SWarner Losh! 120*23f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.func (ackermann)' 121*23f24377SWarner Losh 122*23f24377SWarner Losh$awk ' 123*23f24377SWarner LoshEND { print "end" } 124*23f24377SWarner Losh{ print fib($1) } 125*23f24377SWarner Loshfunction fib(n) { 126*23f24377SWarner Losh if (n <= 1) return 1 127*23f24377SWarner Losh else return add(fib(n-1), fib(n-2)) 128*23f24377SWarner Losh} 129*23f24377SWarner Loshfunction add(m,n) { return m+n } 130*23f24377SWarner LoshBEGIN { print "begin" } 131*23f24377SWarner Losh' <<! >foo2 132*23f24377SWarner Losh1 133*23f24377SWarner Losh3 134*23f24377SWarner Losh5 135*23f24377SWarner Losh10 136*23f24377SWarner Losh! 137*23f24377SWarner Loshcat <<! >foo1 138*23f24377SWarner Loshbegin 139*23f24377SWarner Losh1 140*23f24377SWarner Losh3 141*23f24377SWarner Losh8 142*23f24377SWarner Losh89 143*23f24377SWarner Loshend 144*23f24377SWarner Losh! 145*23f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.func (fib)' 146*23f24377SWarner Losh 147*23f24377SWarner Losh$awk ' 148*23f24377SWarner Loshfunction foo() { 149*23f24377SWarner Losh for (i = 1; i <= 2; i++) 150*23f24377SWarner Losh return 3 151*23f24377SWarner Losh print "should not see this" 152*23f24377SWarner Losh} 153*23f24377SWarner LoshBEGIN { foo(); exit } 154*23f24377SWarner Losh' >foo1 155*23f24377SWarner Loshgrep 'should not' foo1 && echo 'BAD: T.func (return)' 156*23f24377SWarner Losh 157*23f24377SWarner Losh# this exercises multiple free of temp cells 158*23f24377SWarner Loshecho 'eqn 159*23f24377SWarner Losheqn2' >foo1 160*23f24377SWarner Losh$awk 'BEGIN { eprocess("eqn", "x", contig) 161*23f24377SWarner Losh process("tbl" ) 162*23f24377SWarner Losh eprocess("eqn" "2", "x", contig) 163*23f24377SWarner Losh } 164*23f24377SWarner Loshfunction eprocess(file, first, contig) { 165*23f24377SWarner Losh print file 166*23f24377SWarner Losh} 167*23f24377SWarner Loshfunction process(file) { 168*23f24377SWarner Losh close(file) 169*23f24377SWarner Losh}' >foo2 170*23f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.func (eqn)' 171*23f24377SWarner Losh 172*23f24377SWarner Loshecho 1 >foo1 173*23f24377SWarner Losh$awk 'function f() { n = 1; exit } 174*23f24377SWarner Losh BEGIN { n = 0; f(); n = 2 }; END { print n}' >foo2 175*23f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.func (exit in function)' 176*23f24377SWarner Losh 177*23f24377SWarner Loshecho 1 >foo1 178*23f24377SWarner Losh$awk ' 179*23f24377SWarner LoshBEGIN { n = 10 180*23f24377SWarner Losh for (i = 1; i <= n; i++) 181*23f24377SWarner Losh for (j = 1; j <= n; j++) 182*23f24377SWarner Losh x[i,j] = n * i + j 183*23f24377SWarner Losh for (i = 1; i <= n; i++) 184*23f24377SWarner Losh for (j = 1; j <= n; j++) 185*23f24377SWarner Losh if ((i,j) in x) 186*23f24377SWarner Losh k++ 187*23f24377SWarner Losh print (k == n^2) 188*23f24377SWarner Losh } 189*23f24377SWarner Losh' >foo2 190*23f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.func (multi-dim subscript)' 191*23f24377SWarner Losh 192*23f24377SWarner Loshecho '<> 0' >foo1 193*23f24377SWarner Losh$awk ' 194*23f24377SWarner Loshfunction foo() { i = 0 } 195*23f24377SWarner Losh BEGIN { x = foo(); printf "<%s> %d\n", x, x }' >foo2 196*23f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.func (fall off end)' 197