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