Blame test/intformat.awk

Packit 575503
function abs(x) {
Packit 575503
	return (x+0 >= 0) ? x : -x
Packit 575503
}
Packit 575503
Packit 575503
function check(x,what,  f,res) {
Packit 575503
	for (f in formats) {
Packit 575503
		res = sprintf(f,x)
Packit 575503
		if (formats[f] == "non-decimal") {
Packit 575503
			if ((x >= 0) && (res !~ /e+/)) {
Packit 575503
				if (abs(strtonum(res)-x) > 1e-5*abs(x))
Packit 575503
				       printf "(sprintf(%s,%s) = %s)-%g = %g\n",
Packit 575503
					      f,what,res,x,strtonum(res)-x
Packit 575503
			}
Packit 575503
		}
Packit 575503
		else if (abs(res-x) > 1e-5*abs(x))
Packit 575503
			printf "(sprintf(%s,%s) = %s)-%g = %g\n",
Packit 575503
			       f,what,res,x,res-x
Packit 575503
	}
Packit 575503
}
Packit 575503
Packit 575503
function check_cons(fmt,base,rot,mexp,  i,j,dig,res,s) {
Packit 575503
	# first off, check that zero formats properly
Packit 575503
	if ((s = sprintf(fmt,0)) != "0")
Packit 575503
		printf "(sprintf(%s,0) = %s) != 0\n",fmt,s
Packit 575503
Packit 575503
	res = "1"
Packit 575503
	dig = 1
Packit 575503
	j = 0
Packit 575503
	for (i = 0; i <= mexp; i++) {
Packit 575503
		s = sprintf(fmt,base^i)
Packit 575503
		if (s ~ /e+/)
Packit 575503
			return
Packit 575503
		if (s != res)
Packit 575503
			printf "(sprintf(%s,%d^%d) = %s) != %s\n",
Packit 575503
			       fmt,base,i,s,res
Packit 575503
		if (++j == rot) {
Packit 575503
			dig = 1
Packit 575503
			res = ("10"substr(res,2))
Packit 575503
			j = 0
Packit 575503
		}
Packit 575503
		else {
Packit 575503
			dig *= 2
Packit 575503
			res = (dig substr(res,2))
Packit 575503
		}
Packit 575503
	}
Packit 575503
}
Packit 575503
Packit 575503
BEGIN {
Packit 575503
	# this doesn't necessarily have to be exact since we're checking
Packit 575503
	# magnitude rather than precision
Packit 575503
	if (!HUGEVAL) HUGEVAL = 1.7976931348623157e+308
Packit 575503
Packit 575503
	formats["%s"] = ""
Packit 575503
	formats["%d"] = ""
Packit 575503
	formats["%.0f"] = ""
Packit 575503
	formats["0%o"] = "non-decimal"
Packit 575503
	formats["0x%x"] = "non-decimal"
Packit 575503
Packit 575503
	check(0,"0")
Packit 575503
	limit = HUGEVAL / 10
Packit 575503
	value = 1
Packit 575503
	for (i = 0; ; i++) {
Packit 575503
		check(10^i,"10^"i)
Packit 575503
		check(-10^i,"-10^"i)
Packit 575503
		#[probably should test value against 10^i here]
Packit 575503
		if (value >= limit) break
Packit 575503
		value *= 10
Packit 575503
	}
Packit 575503
	limit = HUGEVAL / 2
Packit 575503
	value = 1
Packit 575503
	for (i = 0; ; i++) {
Packit 575503
		check(2^i,"2^"i)
Packit 575503
		check(-2^i,"-2^"i)
Packit 575503
		#[probably should test value against 2^i here]
Packit 575503
		if (value >= limit) break
Packit 575503
		value *= 2
Packit 575503
	}
Packit 575503
Packit 575503
	check_cons("%d",10,1,9)
Packit 575503
	check_cons("%x",2,4,31)
Packit 575503
	check_cons("%o",2,3,31)
Packit 575503
Packit 575503
	# make sure basic %d and %x are working properly
Packit 575503
	printf "%d %d %x\n",3.7,-3.7,23.7
Packit 575503
Packit 575503
	# check another problem in gawk 3.1.5: precision over 30 crashes
Packit 575503
	printf "%.55d\n",1
Packit 575503
}