summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/unit/test_calculator.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /browser/components/urlbar/tests/unit/test_calculator.js
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/components/urlbar/tests/unit/test_calculator.js')
-rw-r--r--browser/components/urlbar/tests/unit/test_calculator.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/browser/components/urlbar/tests/unit/test_calculator.js b/browser/components/urlbar/tests/unit/test_calculator.js
new file mode 100644
index 0000000000..7fa899f320
--- /dev/null
+++ b/browser/components/urlbar/tests/unit/test_calculator.js
@@ -0,0 +1,46 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+ChromeUtils.defineESModuleGetters(this, {
+ Calculator: "resource:///modules/UrlbarProviderCalculator.sys.mjs",
+});
+
+const FORMULAS = [
+ ["1+1", 2],
+ ["3+4*2/(1-5)", 1],
+ ["39+4*2/(1-5)", 37],
+ ["(39+4)*2/(1-5)", -21.5],
+ ["4+-5", -1],
+ ["-5*6", -30],
+ ["-5.5*6", -33],
+ ["-5.5*-6.4", 35.2],
+ ["-6-6-6", -18],
+ ["6-6-6", -6],
+ [".001 /2", 0.0005],
+ ["(0-.001)/2", -0.0005],
+ ["-.001/(0-2)", 0.0005],
+ ["1000000000000000000000000+1", 1e24],
+ ["1000000000000000000000000-1", 1e24],
+ ["1e+30+10", 1e30],
+ ["1e+30*10", 1e31],
+ ["1e+30/100", 1e28],
+ ["10/1000000000000000000000000", 1e-23],
+ ["10/-1000000000000000000000000", -1e-23],
+ ["1,500.5+2.5", 1503], // Ignore commas when using decimal seperators
+ ["1,5+2,5", 4], // Support comma seperators
+ ["1.500,5+2,5", 1503], // Ignore periods when using comma decimal seperators
+];
+
+add_task(function test() {
+ for (let [formula, result] of FORMULAS) {
+ let postfix = Calculator.infix2postfix(formula);
+ Assert.equal(
+ Calculator.evaluatePostfix(postfix),
+ result,
+ `${formula} should equal ${result}`
+ );
+ }
+});