xref: /freebsd/contrib/one-true-awk/testdir/T.builtin (revision cb7aa33ac6cd46a5434798e50363136e64f3ae98)
1echo T.builtin: test miscellaneous builtin functions
2
3awk=${awk-../a.out}
4
5$awk 'BEGIN { print index(123, substr(123, 2)) }' >foo1
6echo 2 >foo2
7diff foo1 foo2 || echo 'BAD: T.builtin (index/substr)'
8
9$awk 'BEGIN {
10	pi = 2 * atan2(1, 0)
11	printf("%.5f %.3f %.3f %.5f %.3f\n",
12		pi, sin(pi), cos(pi/2), exp(log(pi)), log(exp(10)))
13}' >foo1
14echo '3.14159 0.000 0.000 3.14159 10.000' >foo2
15diff foo1 foo2 || echo 'BAD: T.builtin (sin/cos)'
16
17$awk 'BEGIN {
18	s = srand(1)	# set a real random start
19	for (i = 1; i <= 10; i++)
20		print rand() >"foo1"
21	srand(s)	# reset it
22	for (i = 1; i <= 10; i++)
23		print rand() >"foo2"
24}'
25diff foo1 foo2 || echo 'BAD: T.builtin (rand)'
26
27echo 'hello, WORLD!' |
28$awk '{ printf("%s|%s|%s\n", tolower($0), toupper($0), $0)}' >foo1
29echo 'hello, world!|HELLO, WORLD!|hello, WORLD!' >foo2
30diff foo1 foo2 || echo 'BAD: T.builtin (toupper/tolower)'
31
32
33if locale -a | grep -qsi de_DE.UTF-8; then
34	(export LANG=de_DE.UTF-8 && echo 'Dürst' |
35	$awk '{ printf("%s|%s|%s\n", tolower($0), toupper($0), $0)}') >foo1
36	echo 'dürst|DÜRST|Dürst' >foo2
37	diff foo1 foo2 || echo 'BAD: T.builtin (toupper/tolower) for utf-8'
38	(export LC_NUMERIC=de_DE.UTF-8 && $awk 'BEGIN { print 0.01 }' /dev/null) >foo1
39	echo "0.01" >foo2
40	diff foo1 foo2 || echo 'BAD: T.builtin LC_NUMERIC radix (.) handling'
41fi
42
43$awk 'BEGIN {
44	j = 1; sprintf("%d", 99, ++j)	# does j get incremented?
45	if (j != 2)
46		print "BAD: T.builtin (printf arg list not evaluated)"
47}'
48
49$awk 'BEGIN {
50	j = 1; substr("", 1, ++j)	# does j get incremented?
51	if (j != 2)
52		print "BAD: T.builtin (substr arg list not evaluated)"
53}'
54
55$awk 'BEGIN {
56	j = 1; sub(/1/, ++j, z)	# does j get incremented?
57	if (j != 2)
58		print "BAD: T.builtin (sub() arg list not evaluated)"
59}'
60
61$awk 'BEGIN {
62	j = 1; length("zzzz", ++j, ++j)	# does j get incremented?
63	if (j != 3)
64		print "BAD: T.builtin (excess length args not evaluated)"
65}' 2>foo
66grep 'too many arg' foo >/dev/null || echo 'T.bad: too many args not caught'
67
68echo 'a
69a b
70a b c' >foo0
71echo '1
722
733' >foo1
74$awk '{ n = split($0, x); print length(x) }' <foo0 >foo2
75diff foo1 foo2 || echo 'BAD: T.builtin length array'
76
77# Test for backslash handling
78cat << \EOF >foo0
79BEGIN {
80    print "A\
81B";
82    print "CD"
83}
84EOF
85$awk -f foo0 /dev/null >foo1
86cat << \EOF >foo2
87AB
88CD
89EOF
90diff foo1 foo2 || echo 'BAD: T.builtin continuation handling (backslash)'
91