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