test_calculator.js (1658B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 ChromeUtils.defineESModuleGetters(this, { 8 Calculator: 9 "moz-src:///browser/components/urlbar/UrlbarProviderCalculator.sys.mjs", 10 }); 11 12 const FORMULAS = [ 13 ["1+1", "2"], 14 ["3+4*2/(1-5)", "1"], 15 ["39+4*2/(1-5)", "37"], 16 ["(39+4)*2/(1-5)", "-21.5"], 17 ["4+-5", "-1"], 18 ["-5*6", "-30"], 19 ["-5.5*6", "-33"], 20 ["-5.5*-6.4", "35.2"], 21 ["-6-6-6", "-18"], 22 ["6-6-6", "-6"], 23 [".001 /2", "0.0005"], 24 ["(0-.001)/2", "-0.0005"], 25 ["-.001/(0-2)", "0.0005"], 26 ["1000000000000000000000000+1", "1.0e24"], 27 ["1000000000000000000000000-1", "1.0e24"], 28 ["1e+30+10", "1.0e30"], 29 ["1e+30*10", "1.0e31"], 30 ["1e+30/100", "1.0e28"], 31 ["10/1000000000000000000000000", "1.0e-23"], 32 ["10/-1000000000000000000000000", "-1.0e-23"], 33 ["1,500.5+2.5", "1503"], // Ignore commas when using decimal seperators 34 ["1,5+2,5", "4"], // Support comma seperators 35 ["1.500,5+2,5", "1503"], // Ignore periods when using comma decimal seperators 36 ["3^3", "27"], 37 ["2+3^3", "29"], 38 ["4^0.5+4^-0.5", "2.5"], 39 ["3+8*64/(1-3)^2^3", "5"], 40 ["1/0", "undefined"], 41 ["1/3", "0.333333333"], 42 ["1/(3*10^10)", "3.33333333e-11"], 43 ["1,000,000+500", "1000500"], 44 ["1.000.000+500", "1000500"], 45 ]; 46 47 add_task(function test() { 48 for (let [formula, result] of FORMULAS) { 49 let postfix = Calculator.infix2postfix(formula); 50 Assert.strictEqual( 51 Calculator.evaluatePostfix(postfix), 52 result, 53 `${formula} should equal ${result}` 54 ); 55 } 56 });