aboutsummaryrefslogtreecommitdiff
path: root/testdir/T.builtin
blob: 411a5e595443f9b07e8c65c756735f396ba641d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
echo T.builtin: test miscellaneous builtin functions

awk=${awk-../a.out}

$awk 'BEGIN { print index(123, substr(123, 2)) }' >foo1
echo 2 >foo2
diff foo1 foo2 || echo 'BAD: T.builtin (index/substr)'

$awk 'BEGIN {
	pi = 2 * atan2(1, 0)
	printf("%.5f %.3f %.3f %.5f %.3f\n",
		pi, sin(pi), cos(pi/2), exp(log(pi)), log(exp(10)))
}' >foo1
echo '3.14159 0.000 0.000 3.14159 10.000' >foo2
diff foo1 foo2 || echo 'BAD: T.builtin (sin/cos)'

$awk 'BEGIN {
	s = srand(1)	# set a real random start
	for (i = 1; i <= 10; i++)
		print rand() >"foo1"
	srand(s)	# reset it
	for (i = 1; i <= 10; i++)
		print rand() >"foo2"
}'
diff foo1 foo2 || echo 'BAD: T.builtin (rand)'

echo 'hello, WORLD!' |
$awk '{ printf("%s|%s|%s\n", tolower($0), toupper($0), $0)}' >foo1
echo 'hello, world!|HELLO, WORLD!|hello, WORLD!' >foo2
diff foo1 foo2 || echo 'BAD: T.builtin (toupper/tolower)'

$awk 'BEGIN {
	j = 1; sprintf("%d", 99, ++j)	# does j get incremented?
	if (j != 2)
		print "BAD: T.builtin (printf arg list not evaluated)"
}'

$awk 'BEGIN {
	j = 1; substr("", 1, ++j)	# does j get incremented?
	if (j != 2)
		print "BAD: T.builtin (substr arg list not evaluated)"
}'

$awk 'BEGIN {
	j = 1; sub(/1/, ++j, z)	# does j get incremented?
	if (j != 2)
		print "BAD: T.builtin (sub() arg list not evaluated)"
}'

$awk 'BEGIN {
	j = 1; length("zzzz", ++j, ++j)	# does j get incremented?
	if (j != 3)
		print "BAD: T.builtin (excess length args not evaluated)"
}' 2>foo
grep 'too many arg' foo >/dev/null || echo 'T.bad: too many args not caught'

echo 'a
a b
a b c' >foo0
echo '1
2
3' >foo1
$awk '{ n = split($0, x); print length(x) }' <foo0 >foo2
diff foo1 foo2 || echo 'BAD: T.builtin length array'