summaryrefslogtreecommitdiff
path: root/test/intformat.awk
diff options
context:
space:
mode:
authorArnold D. Robbins <arnold@skeeve.com>2010-07-16 14:49:57 +0300
committerArnold D. Robbins <arnold@skeeve.com>2010-07-16 14:49:57 +0300
commit6a2caf2157d87b4b582b2494bdd7d6a688dd0b1f (patch)
tree9a2862cc11be4832f188cfbdce175120ceba5024 /test/intformat.awk
parent315bd501ca696bc3e3c938b4604d8dac7a6f512f (diff)
downloadgawk-6a2caf2157d87b4b582b2494bdd7d6a688dd0b1f.tar.gz
Move to gawk-3.1.6.gawk-3.1.6
Diffstat (limited to 'test/intformat.awk')
-rw-r--r--test/intformat.awk74
1 files changed, 74 insertions, 0 deletions
diff --git a/test/intformat.awk b/test/intformat.awk
new file mode 100644
index 00000000..56ce606b
--- /dev/null
+++ b/test/intformat.awk
@@ -0,0 +1,74 @@
+function abs(x) {
+ return (x+0 >= 0) ? x : -x
+}
+
+function check(x,what, f,res) {
+ for (f in formats) {
+ res = sprintf(f,x)
+ if (formats[f] == "non-decimal") {
+ if ((x >= 0) && (res !~ /e+/)) {
+ if (abs(strtonum(res)-x) > 1e-5*abs(x))
+ printf "(sprintf(%s,%s) = %s)-%g = %g\n",
+ f,what,res,x,strtonum(res)-x
+ }
+ }
+ else if (abs(res-x) > 1e-5*abs(x))
+ printf "(sprintf(%s,%s) = %s)-%g = %g\n",
+ f,what,res,x,res-x
+ }
+}
+
+function check_cons(fmt,base,rot,mexp, i,j,dig,res,s) {
+ # first off, check that zero formats properly
+ if ((s = sprintf(fmt,0)) != "0")
+ printf "(sprintf(%s,0) = %s) != 0\n",fmt,s
+
+ res = "1"
+ dig = 1
+ j = 0
+ for (i = 0; i <= mexp; i++) {
+ s = sprintf(fmt,base^i)
+ if (s ~ /e+/)
+ return
+ if (s != res)
+ printf "(sprintf(%s,%d^%d) = %s) != %s\n",
+ fmt,base,i,s,res
+ if (++j == rot) {
+ dig = 1
+ res = ("10"substr(res,2))
+ j = 0
+ }
+ else {
+ dig *= 2
+ res = (dig substr(res,2))
+ }
+ }
+}
+
+BEGIN {
+ formats["%s"] = ""
+ formats["%d"] = ""
+ formats["%.0f"] = ""
+ formats["0%o"] = "non-decimal"
+ formats["0x%x"] = "non-decimal"
+
+ check(0,"0")
+ for (i = 0; i <= 308; i++) {
+ check(10^i,"10^"i)
+ check(-10^i,"-10^"i)
+ }
+ for (i = 0; i <= 1023; i++) {
+ check(2^i,"2^"i)
+ check(-2^i,"-2^"i)
+ }
+
+ check_cons("%d",10,1,9)
+ check_cons("%x",2,4,31)
+ check_cons("%o",2,3,31)
+
+ # make sure basic %d and %x are working properly
+ printf "%d %d %x\n",3.7,-3.7,23.7
+
+ # check another problem in gawk 3.1.5: precision over 30 crashes
+ printf "%.55d\n",1
+}