From be1d83845bd785056894248cbc03a518dd356626 Mon Sep 17 00:00:00 2001 From: Gwani-28 Date: Wed, 3 Jun 2026 17:54:57 +0900 Subject: [PATCH] test fallback math helper coverage --- test/operators.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/operators.js b/test/operators.js index 07d15aa5..63c14449 100644 --- a/test/operators.js +++ b/test/operators.js @@ -18,6 +18,22 @@ function assertCloseTo(expected, actual, delta) { return assert.ok(Math.abs(expected - actual) <= delta); } +function withNativeMathUnavailable(names, callback) { + var original = {}; + names.forEach(function (name) { + original[name] = Math[name]; + Math[name] = null; + }); + + try { + callback(); + } finally { + names.forEach(function (name) { + Math[name] = original[name]; + }); + } +} + describe('Operators', function () { var parser = new Parser(); @@ -1026,4 +1042,21 @@ describe('Operators', function () { assertCloseTo(parser.parse('log2 x').toJSFunction('x')(3), 1.584962500721156, delta); }); }); + + describe('fallback math implementations', function () { + it('uses fallback implementations when native Math helpers are unavailable', function () { + withNativeMathUnavailable(['expm1', 'log1p', 'log2'], function () { + var fallbackParser = new Parser(); + var delta = 1e-15; + + assertCloseTo(fallbackParser.evaluate('expm1 1'), 1.718281828459045, delta); + assertCloseTo(fallbackParser.evaluate('log1p 9'), 2.302585092994046, delta); + assertCloseTo(fallbackParser.evaluate('log2 8'), 3, delta); + + assertCloseTo(fallbackParser.parse('expm1 x').toJSFunction('x')(2), 6.38905609893065, delta); + assertCloseTo(fallbackParser.parse('log1p x').toJSFunction('x')(1), 0.6931471805599453, delta); + assertCloseTo(fallbackParser.parse('log2 x').toJSFunction('x')(3), 1.584962500721156, delta); + }); + }); + }); });