awk/testdir/T.builtin
zoulasc 110bdc6b3e
misc fixes (#69)
* Add a test for german case folding.

* Add a function to copy a string with a string with a larger allocation
  (to be used by the case folding routines)
* Add printf attributes to the printf-like functions and fix one format
  warning
* Cleanup the tempfree macro
* make more functions static
* rename fp to frp (FRame Pointer) to avoid shadowing with fp (File Pointer).
* add more const
* fix indent in UPLUS case
* add locale-aware case folding
* make nfiles size_t
* fix bugs in file closing:
    - compare fclose to EOF and pclose to -1
    - use nfiles instead of FOPEN_MAX in closeall
    - don't close files we did not open (0,1,2) fpurge/fflush instead

* - use NUL instead of 0 for char comparisons
- add ISWS() macro
- use continue; instead of ;

* Check for existance of the german locale before using it.

* Add missing parentheses, thanks Arnold.
2020-02-06 21:25:36 +02:00

73 lines
1.9 KiB
Plaintext
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)'
if locale -a | grep -qsi de_DE.UTF-8; then
(export LANG=de_DE.UTF-8 && echo 'Dürst' |
$awk '{ printf("%s|%s|%s\n", tolower($0), toupper($0), $0)}') >foo1
echo 'dürst|DÜRST|Dürst' >foo2
diff foo1 foo2 || echo 'BAD: T.builtin (toupper/tolower) for utf-8'
fi
$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'