vtest "Test std.real() conversion and std.round"

server s1 {
	rxreq
	txresp
} -start

vinyl v1 -vcl+backend {
	import std;

	sub vcl_deliver {
		set resp.http.t0 = std.real(req.http.foo, 0.0);
		set resp.http.t1 = std.real(req.http.bar, 0.0);
		set resp.http.t2 = std.real(req.http.nonexistent, 0.0);
		set resp.http.t3 = std.real("1.", 0.0);
		set resp.http.t4 = std.real("1.0", 0.0);
		set resp.http.t5 = std.real("1.0 ", 0.0);
		set resp.http.t6 = std.real(" 1.0", 0.0);
		set resp.http.t7 = std.real(" 1.0 ", 0.0);
		set resp.http.t8 = std.round(42.42);
		set resp.http.t9 = std.round(-42.42);
	}
} -start

client c1 {
	txreq -hdr "foo: 1.00" -hdr "bar: 1.00a"
	rxresp
	expect resp.status == 200
	expect resp.http.t0 == 1.000
	expect resp.http.t1 == 0.000
	expect resp.http.t2 == 0.000
	expect resp.http.t3 == 1.000
	expect resp.http.t4 == 1.000
	expect resp.http.t5 == 1.000
	expect resp.http.t6 == 1.000
	expect resp.http.t7 == 1.000
	expect resp.http.t8 == 42.000
	expect resp.http.t9 == -42.000
} -run

vinyl v1 -vcl+backend {
	import std;

	sub vcl_deliver {
		set resp.http.converted = std.real(req.http.foo, 0);

		if (req.http.nofb) {
			set resp.http.nofb = std.real(req.http.foo);
		}

		if (req.http.bool) {
			set resp.http.bool =
			    std.real(bool=req.http.bool != "");
		}
		if (req.http.bytes) {
			set resp.http.bytes =
			    std.real(bytes=std.bytes(req.http.bytes));
		}
		if (req.http.duration) {
			set resp.http.duration =
			    std.real(duration=std.duration(req.http.duration));
		}
		if (req.http.integer) {
			set resp.http.integer =
			    std.real(integer=std.integer(req.http.integer));
		}
		if (req.http.time) {
			set resp.http.time =
			    std.real(time=std.time(req.http.time));
		}
	}
}

client c1 {
	txreq -hdr "foo: 1" \
	      -hdr "bool: whatever" \
	      -hdr "bytes: 1b" \
	      -hdr "duration: 1s" \
	      -hdr "integer: 1" \
	      -hdr "time: Thu, 01 Jan 1970 00:00:01 GMT"
	rxresp
	expect resp.http.converted == 1.000
	expect resp.http.bool == 1.000
	expect resp.http.duration == 1.000
	expect resp.http.bytes == 1.000
	expect resp.http.integer == 1.000
	expect resp.http.time == 1.000

	txreq -hdr "foo: -1"
	rxresp
	expect resp.http.converted == -1.000

	txreq -hdr "foo: 0"
	rxresp
	expect resp.http.converted == 0.000

	# VCL_INT_MAX
	txreq -hdr "foo: 999999999999.999" \
	      -hdr "bytes: 999999999999b" \
	      -hdr "duration: 999999999999.999s" \
	      -hdr "integer: 999999999999.000" \
	      -hdr "time: 999999999999.999"
	rxresp
	expect resp.http.converted == 999999999999.999
	expect resp.http.bytes == 999999999999.000
	expect resp.http.duration == 999999999999.999
	expect resp.http.integer == 999999999999.000
	expect resp.http.time == 999999999999.999

	# VCL_INT_MIN
	txreq -hdr "foo: -999999999999.999" \
	      -hdr "duration: -999999999999.999s" \
	      -hdr "integer: -999999999999"
	rxresp
	expect resp.http.converted == -999999999999.999
	expect resp.http.duration == -999999999999.999
	expect resp.http.integer == -999999999999.000

	txreq -hdr "foo: bar"
	rxresp
	expect resp.http.converted == 0.000

} -run
client c1 {
	txreq -hdr "nofb: NAN"
	rxresp
	expect resp.status == 503
} -run
client c1 {
	txreq -hdr "nofb: -INF"
	rxresp
	expect resp.status == 503
} -run
client c1 {
	txreq -hdr "nofb: x"
	rxresp
	expect resp.status == 503
} -run
