|
| 1 | +varnishtest vmod_math |
| 2 | + |
| 3 | +varnish v1 -vcl { |
| 4 | + import math; |
| 5 | + |
| 6 | + backend proforma none; |
| 7 | + |
| 8 | + sub vcl_recv { |
| 9 | + return (synth(200)); |
| 10 | + } |
| 11 | + |
| 12 | + sub vcl_synth { |
| 13 | + if (req.http.strfromdfmt) { |
| 14 | + set resp.http.result = math.strfromd(req.http.strfromdfmt, math.constant(M_PI)); |
| 15 | + return (deliver); |
| 16 | + } |
| 17 | + |
| 18 | + # formatting basics |
| 19 | + set resp.http."n.v" = 0.1; |
| 20 | + set resp.http."n.13a" = math.strfromd("%.13a", 0.1); |
| 21 | + set resp.http."n.17e" = math.strfromd("%.17e", 0.1); |
| 22 | + set resp.http."n.17f" = math.strfromd("%.17f", 0.1); |
| 23 | + set resp.http."n.g" = math.strfromd("%g", 0.1); |
| 24 | + |
| 25 | + # edge cases where approx makes a difference |
| 26 | + |
| 27 | + set resp.http."sinpi.v" = math.sin(math.constant(M_PI)); |
| 28 | + set resp.http."sinpi.17f" = math.strfromd("%.17f", math.sin(math.constant(M_PI))); |
| 29 | + set resp.http."sinpi.0.equals" = math.sin(math.constant(M_PI)) == 0.0; |
| 30 | + set resp.http."sinpi.0.approx" = math.approx(math.sin(math.constant(M_PI)), 0); |
| 31 | + set resp.http."sinpi.0.zero" = |
| 32 | + math.fpclassify(math.sin(math.constant(M_PI))) == |
| 33 | + math.fpclass(FP_ZERO); |
| 34 | + |
| 35 | + # VRT_DECIMAL_MAX == 0x426d1a94a1fffff8 |
| 36 | + set resp.http."some1.v" = 999999999999 + 0.999; |
| 37 | + set resp.http."some1.f" = math.strfromd("%f", 999999999999 + 0.999); |
| 38 | + # VRT_DECIMAL_MAX - ULP == 0x426d1a94a1fffff7 |
| 39 | + set resp.http."some2.v" = 999999999999 + (9.989 / 10); |
| 40 | + set resp.http."some2.f" = math.strfromd("%f", 999999999999 + (9.989 / 10)); |
| 41 | + |
| 42 | + set resp.http."some1.some2.equals" = |
| 43 | + (999999999999 + 0.999) == (999999999999 + (9.989 / 10)); |
| 44 | + set resp.http."some1.some2.approx" = |
| 45 | + math.approx(999999999999 + 0.999, 999999999999 + (9.989 / 10)); |
| 46 | + |
| 47 | + return (deliver); |
| 48 | + } |
| 49 | +} -start |
| 50 | + |
| 51 | +client c1 { |
| 52 | + txreq |
| 53 | + rxresp |
| 54 | + expect resp.status == 200 |
| 55 | + |
| 56 | + expect resp.http.n.v == 0.100 |
| 57 | + expect resp.http.n.13a == 0x1.999999999999ap-4 |
| 58 | + expect resp.http.n.17e == 1.00000000000000006e-01 |
| 59 | + expect resp.http.n.17f == 0.10000000000000001 |
| 60 | + expect resp.http.n.g == 0.1 |
| 61 | + |
| 62 | + expect resp.http.sinpi.v == 0.000 |
| 63 | + expect resp.http.sinpi.17f == 0.00000000000000012 |
| 64 | + expect resp.http.sinpi.0.equals == false |
| 65 | + expect resp.http.sinpi.0.approx == true |
| 66 | + expect resp.http.sinpi.0.zero == false |
| 67 | + |
| 68 | + expect resp.http.some1.v == 999999999999.999 |
| 69 | + expect resp.http.some1.f == 999999999999.999023 |
| 70 | + expect resp.http.some2.v == 999999999999.999 |
| 71 | + expect resp.http.some2.f == 999999999999.998901 |
| 72 | + expect resp.http.some1.some2.equals == false |
| 73 | + expect resp.http.some1.some2.approx == true |
| 74 | + |
| 75 | + txreq -hdr "strfromdfmt: %5f" |
| 76 | + rxresp |
| 77 | + expect resp.status == 500 |
| 78 | +} -run |
0 commit comments