|
| 1 | +-- regression tests for vstruct |
| 2 | +-- tests for fixed bugs go here to make sure they don't recur |
| 3 | +-- tests are named after the bug they trigger, not the intended behaviour |
| 4 | + |
| 5 | +local test = require "vstruct.test.common" |
| 6 | +local frexp = require "vstruct.frexp" |
| 7 | + |
| 8 | +-- No point in running these tests if there isn't a builtin frexp to compare to. |
| 9 | +if not math.frexp then return end |
| 10 | + |
| 11 | +test.group "frexp()" |
| 12 | + |
| 13 | +local function test_frexp(x) |
| 14 | + local m,e = math.frexp(x) |
| 15 | + if m >= 1 or m <= -1 then |
| 16 | + -- At least on my machine, frexp() sometimes returns 1 as the mantissa, in |
| 17 | + -- violation as the spec. Correct this so that we can more properly compare |
| 18 | + -- things. |
| 19 | + m,e = m/2, e+1 |
| 20 | + end |
| 21 | + local m2,e2 = frexp(x) |
| 22 | + local ok = m == m2 and e == e2 |
| 23 | + test.record("frexp equivalence", ok, x, |
| 24 | + "(builtin) %f,%f != %f,%f (lua)", m, e, m2, e2) |
| 25 | +end |
| 26 | + |
| 27 | +local inf = math.huge |
| 28 | +local z = 0.0 |
| 29 | +local nz = -z |
| 30 | +local doubles = { |
| 31 | + 0.0, 1.0, 2.0, |
| 32 | + 1/2, 1/4, 1/8, |
| 33 | + 4.9406564584124654418e-324, |
| 34 | + 7.4169128616906696301e-309, |
| 35 | + 1.483382572338133926e-308, |
| 36 | + 2.225073858507200889e-308, |
| 37 | + 2.2250738585072013831e-308, |
| 38 | + 2.2250738585072018772e-308, |
| 39 | + 2.9667651446762683461e-308, |
| 40 | + 3.7084564308453353091e-308, |
| 41 | + 4.4501477170144022721e-308, |
| 42 | + 8.9884656743115795386e+307, |
| 43 | + 8.9884656743115815345e+307, |
| 44 | + 1.1984620899082105386e+308, |
| 45 | + 1.4980776123852631234e+308, |
| 46 | + 1.7976931348623157081e+308, |
| 47 | + 4.9406564584124654418e-324, |
| 48 | + 2.225073858507200889e-308, |
| 49 | + 2.2250738585072013831e-308, |
| 50 | + 1.7976931348623157081e+308, |
| 51 | + 0.0, |
| 52 | + nz, |
| 53 | + -4.9406564584124654418e-324, |
| 54 | + -7.4169128616906696301e-309, |
| 55 | + -1.483382572338133926e-308, |
| 56 | + -2.225073858507200889e-308, |
| 57 | + -2.2250738585072013831e-308, |
| 58 | + -2.2250738585072018772e-308, |
| 59 | + -2.9667651446762683461e-308, |
| 60 | + -3.7084564308453353091e-308, |
| 61 | + -4.4501477170144022721e-308, |
| 62 | + -8.9884656743115795386e+307, |
| 63 | + -8.9884656743115815345e+307, |
| 64 | + -1.1984620899082105386e+308, |
| 65 | + -1.4980776123852631234e+308, |
| 66 | + -1.7976931348623157081e+308, |
| 67 | + -4.9406564584124654418e-324, |
| 68 | + -2.225073858507200889e-308, |
| 69 | + -2.2250738585072013831e-308, |
| 70 | + -1.7976931348623157081e+308, |
| 71 | + nz, |
| 72 | +} |
| 73 | + |
| 74 | +for _,double in ipairs(doubles) do |
| 75 | + test_frexp(double) |
| 76 | +end |
0 commit comments