FormulaTests.js 247 KB
Newer Older
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1
/*
2
 * (c) Copyright Ascensio System SIA 2010-2017
Alexander.Trofimov's avatar
Alexander.Trofimov committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * This program is a free software product. You can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License (AGPL)
 * version 3 as published by the Free Software Foundation. In accordance with
 * Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
 * that Ascensio System SIA expressly excludes the warranty of non-infringement
 * of any third-party rights.
 *
 * This program is distributed WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE. For
 * details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
 *
 * You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
 * EU, LV-1021.
 *
 * The  interactive user interfaces in modified source and object code versions
 * of the Program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU AGPL version 3.
 *
 * Pursuant to Section 7(b) of the License you must retain the original Product
 * logo when distributing the program. Pursuant to Section 7(e) we decline to
 * grant you any rights under trademark law for use of our trademarks.
 *
 * All the Product's GUI elements, including illustrations and icon sets, as
 * well as technical writing content are licensed under the terms of the
 * Creative Commons Attribution-ShareAlike 4.0 International. See the License
 * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
 *
 */

dmitry.shahtanov's avatar
dmitry.shahtanov committed
33
$( function () {
34

35
    function toFixed( n ) {
36
        return n//.toFixed( AscCommonExcel.cExcelSignificantDigits ) - 0;
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
37 38 39 40
    }

    function difBetween( a, b ) {
        return Math.abs( a - b ) < dif
41 42
    }

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
43
    function _getPMT( fZins, fZzr, fBw, fZw, nF ){
44 45 46 47 48 49 50 51 52 53 54 55 56 57
        var fRmz;
        if( fZins == 0.0 )
            fRmz = ( fBw + fZw ) / fZzr;
        else{
            var	fTerm = Math.pow( 1.0 + fZins, fZzr );
            if( nF > 0 )
                fRmz = ( fZw * fZins / ( fTerm - 1.0 ) + fBw * fZins / ( 1.0 - 1.0 / fTerm ) ) / ( 1.0 + fZins );
            else
                fRmz = fZw * fZins / ( fTerm - 1.0 ) + fBw * fZins / ( 1.0 - 1.0 / fTerm );
        }

        return -fRmz;
    }

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
58
    function _getFV( fZins, fZzr, fRmz, fBw, nF ){
59 60 61 62 63 64 65 66 67 68 69 70 71 72
        var fZw;
        if( fZins == 0.0 )
            fZw = fBw + fRmz * fZzr;
        else{
            var fTerm = Math.pow( 1.0 + fZins, fZzr );
            if( nF > 0 )
                fZw = fBw * fTerm + fRmz * ( 1.0 + fZins ) * ( fTerm - 1.0 ) / fZins;
            else
                fZw = fBw * fTerm + fRmz * ( fTerm - 1.0 ) / fZins;
        }

        return -fZw;
    }

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    function _getDDB( cost, salvage, life, period, factor ) {
        var ddb, ipmt, oldCost, newCost;
        ipmt = factor / life;
        if ( ipmt >= 1 ) {
            ipmt = 1;
            if ( period == 1 )
                oldCost = cost;
            else
                oldCost = 0;
        }
        else
            oldCost = cost * Math.pow( 1 - ipmt, period - 1 );
        newCost = cost * Math.pow( 1 - ipmt, period );

        if ( newCost < salvage )
            ddb = oldCost - salvage;
        else
            ddb = oldCost - newCost;
        if ( ddb < 0 )
            ddb = 0;
        return ddb;
    }

    function _getIPMT(rate, per, pv, type, pmt) {
        var ipmt;

        if ( per == 1 ) {
            if ( type > 0 )
                ipmt = 0;
            else
                ipmt = -pv;
        }
        else {
            if ( type > 0 )
                ipmt = _getFV( rate, per - 2, pmt, pv, 1 ) - pmt;
            else
                ipmt = _getFV( rate, per - 1, pmt, pv, 0 );
        }
        return ipmt * rate
    }

114 115 116 117 118 119 120 121 122 123
    function _diffDate(d1, d2, mode){
        var date1 = d1.getDate(),
            month1 = d1.getMonth(),
            year1 = d1.getFullYear(),
            date2 = d2.getDate(),
            month2 = d2.getMonth(),
            year2 = d2.getFullYear();

        switch ( mode ) {
            case 0:
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
124
                return Math.abs( GetDiffDate360( date1, month1, year1, date2, month2, year2, true ) );
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
            case 1:
                var yc = Math.abs( year2 - year1 ),
                    sd = year1 > year2 ? d2 : d1,
                    yearAverage = sd.isLeapYear() ? 366 : 365, dayDiff = Math.abs( d2 - d1 );
                for ( var i = 0; i < yc; i++ ) {
                    sd.addYears( 1 );
                    yearAverage += sd.isLeapYear() ? 366 : 365;
                }
                yearAverage /= (yc + 1);
                dayDiff /= c_msPerDay;
                return dayDiff;
            case 2:
                var dayDiff = Math.abs( d2 - d1 );
                dayDiff /= c_msPerDay;
                return dayDiff;
            case 3:
                var dayDiff = Math.abs( d2 - d1 );
                dayDiff /= c_msPerDay;
                return dayDiff;
            case 4:
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
145
                return Math.abs( GetDiffDate360( date1, month1, year1, date2, month2, year2, false ) );
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
            default:
                return "#NUM!";
        }
    }

    function _yearFrac(d1, d2, mode) {
        var date1 = d1.getDate(),
            month1 = d1.getMonth()+1,
            year1 = d1.getFullYear(),
            date2 = d2.getDate(),
            month2 = d2.getMonth()+1,
            year2 = d2.getFullYear();

        switch ( mode ) {
            case 0:
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
161
                return Math.abs( GetDiffDate360( date1, month1, year1, date2, month2, year2, true ) ) / 360;
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
            case 1:
                var yc = /*Math.abs*/( year2 - year1 ),
                    sd = year1 > year2 ? new Date(d2) : new Date(d1),
                    yearAverage = sd.isLeapYear() ? 366 : 365, dayDiff = /*Math.abs*/( d2 - d1 );
                for ( var i = 0; i < yc; i++ ) {
                    sd.addYears( 1 );
                    yearAverage += sd.isLeapYear() ? 366 : 365;
                }
                yearAverage /= (yc + 1);
                dayDiff /= (yearAverage * c_msPerDay);
                return dayDiff;
            case 2:
                var dayDiff = Math.abs( d2 - d1 );
                dayDiff /= (360 * c_msPerDay);
                return dayDiff;
            case 3:
                var dayDiff = Math.abs( d2 - d1 );
                dayDiff /= (365 * c_msPerDay);
                return dayDiff;
            case 4:
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
182
                return Math.abs( GetDiffDate360( date1, month1, year1, date2, month2, year2, false ) ) / 360;
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
            default:
                return "#NUM!";
        }
    }

    function _lcl_GetCouppcd(settl, matur, freq){
        matur.setFullYear( settl.getFullYear() );
        if( matur < settl )
            matur.addYears( 1 );
        while( matur > settl ){
            matur.addMonths( -12 / freq );
        }
    }

    function _lcl_GetCoupncd( settl, matur, freq ){
        matur.setFullYear( settl.getFullYear() );
        if( matur > settl )
            matur.addYears( -1 );
        while( matur <= settl ){
            matur.addMonths( 12 / freq );
        }
    }

    function _getcoupdaybs( settl, matur, frequency, basis ) {
        _lcl_GetCouppcd( settl, matur, frequency );
        return _diffDate( settl, matur, basis );
    }

    function _getcoupdays( settl, matur, frequency, basis ) {
        _lcl_GetCouppcd( settl, matur, frequency );
        var n = new Date( matur )
        n.addMonths( 12 / frequency );
        return _diffDate( matur, n, basis );
    }

    function _getdiffdate( d1,d2, nMode ){
        var bNeg = d1 > d2;

        if( bNeg )
        {
            var n = d2;
            d2 = d1;
            d1 = n;
        }

        var nRet,pOptDaysIn1stYear

        var nD1 = d1.getDate(),
            nM1 = d1.getMonth(),
            nY1  = d1.getFullYear(),
            nD2 = d2.getDate(),
            nM2 = d2.getMonth(),
            nY2 = d2.getFullYear();

        switch( nMode )
        {
            case 0:			// 0=USA (NASD) 30/360
            case 4:			// 4=Europe 30/360
            {
                var bLeap = d1.isLeapYear()
                var nDays, nMonths/*, nYears*/;

                nMonths = nM2 - nM1;
                nDays = nD2 - nD1;

                nMonths += ( nY2 - nY1 ) * 12;

                nRet = nMonths * 30 + nDays;
                if( nMode == 0 && nM1 == 2 && nM2 != 2 && nY1 == nY2 )
                    nRet -= bLeap? 1 : 2;

                pOptDaysIn1stYear = 360;
            }
                break;
            case 1:			// 1=exact/exact
                pOptDaysIn1stYear = d1.isLeapYear() ? 366 : 365;
                nRet = d2 - d1;
                break;
            case 2:			// 2=exact/360
                nRet = d2 - d1;
                pOptDaysIn1stYear = 360;
                break;
            case 3:			//3=exact/365
                nRet = d2 - d1;
                pOptDaysIn1stYear = 365;
                break;
        }

        return (bNeg ? -nRet : nRet) / c_msPerDay / pOptDaysIn1stYear;
    }

    function _getprice( nSettle, nMat, fRate, fYield, fRedemp, nFreq, nBase ){

276 277 278
        var fdays = AscCommonExcel.getcoupdays( new Date(nSettle), new Date(nMat), nFreq, nBase ),
            fdaybs = AscCommonExcel.getcoupdaybs( new Date(nSettle), new Date(nMat), nFreq, nBase ),
            fnum = AscCommonExcel.getcoupnum( new Date(nSettle), (nMat), nFreq, nBase ),
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
279 280 281 282
            fdaysnc = ( fdays - fdaybs ) / fdays,
            fT1 = 100 * fRate / nFreq,
            fT2 = 1 + fYield / nFreq,
            res = fRedemp / ( Math.pow( 1 + fYield / nFreq, fnum - 1 + fdaysnc ) );
283

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
284 285
        /*var fRet = fRedemp / ( Math.pow( 1.0 + fYield / nFreq, fnum - 1.0 + fdaysnc ) );
        fRet -= 100.0 * fRate / nFreq * fdaybs / fdays;
286 287 288 289

        var fT1 = 100.0 * fRate / nFreq;
        var fT2 = 1.0 + fYield / nFreq;

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
290 291
        for( var fK = 0.0 ; fK < fnum ; fK++ ){
            fRet += fT1 / Math.pow( fT2, fK + fdaysnc );
292 293
        }

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
294 295 296 297 298 299 300 301 302 303 304 305 306
        return fRet;*/

        if( fnum == 1){
            return (fRedemp + fT1) / (1 + fdaysnc * fYield / nFreq) - 100 * fRate / nFreq * fdaybs / fdays;
        }

        res -= 100 * fRate / nFreq * fdaybs / fdays;

        for ( var i = 0; i < fnum; i++ ) {
            res += fT1 / Math.pow( fT2, i + fdaysnc );
        }

        return res;
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    }

    function _getYield( nSettle, nMat, fCoup, fPrice, fRedemp, nFreq, nBase ){
        var fRate = fCoup, fPriceN = 0.0, fYield1 = 0.0, fYield2 = 1.0;
        var fPrice1 = _getprice( nSettle, nMat, fRate, fYield1, fRedemp, nFreq, nBase );
        var fPrice2 = _getprice( nSettle, nMat, fRate, fYield2, fRedemp, nFreq, nBase );
        var fYieldN = ( fYield2 - fYield1 ) * 0.5;

        for( var nIter = 0 ; nIter < 100 && fPriceN != fPrice ; nIter++ )
        {
            fPriceN = _getprice( nSettle, nMat, fRate, fYieldN, fRedemp, nFreq, nBase );

            if( fPrice == fPrice1 )
                return fYield1;
            else if( fPrice == fPrice2 )
                return fYield2;
            else if( fPrice == fPriceN )
                return fYieldN;
            else if( fPrice < fPrice2 )
            {
                fYield2 *= 2.0;
                fPrice2 = _getprice( nSettle, nMat, fRate, fYield2, fRedemp, nFreq, nBase );

                fYieldN = ( fYield2 - fYield1 ) * 0.5;
            }
            else
            {
                if( fPrice < fPriceN )
                {
                    fYield1 = fYieldN;
                    fPrice1 = fPriceN;
                }
                else
                {
                    fYield2 = fYieldN;
                    fPrice2 = fPriceN;
                }

                fYieldN = fYield2 - ( fYield2 - fYield1 ) * ( ( fPrice - fPrice2 ) / ( fPrice1 - fPrice2 ) );
            }
        }

        if( Math.abs( fPrice - fPriceN ) > fPrice / 100.0 )
            return "#NUM!";		// result not precise enough

        return fYieldN;
    }

    function _getyieldmat( nSettle, nMat, nIssue, fRate, fPrice, nBase ){

        var fIssMat = _yearFrac( nIssue, nMat, nBase );
        var fIssSet = _yearFrac( nIssue, nSettle, nBase );
        var fSetMat = _yearFrac( nSettle, nMat, nBase );

        var y = 1.0 + fIssMat * fRate;
        y /= fPrice / 100.0 + fIssSet * fRate;
        y--;
        y /= fSetMat;

        return y;

    }
369

370 371 372 373 374 375 376 377 378 379 380 381
    function _coupnum( settlement, maturity, frequency, basis ) {

        basis = ( basis !== undefined ? basis : 0 );

        var n = new Date(maturity);
        _lcl_GetCouppcd( settlement, n, frequency );
        var nMonths = (maturity.getFullYear() - n.getFullYear()) * 12 + maturity.getMonth() - n.getMonth();
        return nMonths * frequency / 12 ;

    }

    function _duration( settlement, maturity, coupon, yld, frequency, basis ){
382 383 384
        var dbc = AscCommonExcel.getcoupdaybs(new Date( settlement ),new Date( maturity ),frequency,basis),
            coupD = AscCommonExcel.getcoupdays(new Date( settlement ),new Date( maturity ),frequency,basis),
            numCoup = AscCommonExcel.getcoupnum(new Date( settlement ),new Date( maturity ),frequency);
385

386 387 388 389
        if ( settlement >= maturity || basis < 0 || basis > 4 || ( frequency != 1 && frequency != 2 && frequency != 4 ) || yld < 0 || coupon < 0 ){
            return "#NUM!";
        }

390
        var duration = 0, p = 0;
391

392 393 394
        var dsc = coupD - dbc;
        var diff = dsc / coupD - 1;
        yld = yld / frequency + 1;
395 396


397 398 399 400 401 402
        coupon *= 100/frequency;

        for(var index = 1; index <= numCoup; index++ ){
            var di = index + diff;

            var yldPOW = Math.pow( yld, di);
403

404 405 406 407
            duration += di * coupon / yldPOW;

            p += coupon / yldPOW;
        }
408

409 410
        duration += (diff + numCoup) * 100 / Math.pow( yld, diff + numCoup);
        p += 100 / Math.pow( yld, diff + numCoup);
411

412
        return duration / p / frequency ;
413 414
    }

415 416 417
    var c_msPerDay = AscCommonExcel.c_msPerDay;
    var parserFormula = AscCommonExcel.parserFormula;
    var GetDiffDate360 = AscCommonExcel.GetDiffDate360;
418 419
    var fSortAscending = AscCommon.fSortAscending;
    var g_oIdCounter = AscCommon.g_oIdCounter;
420

421
    var oParser, wb, ws, dif = 1e-9,
422
        data = getTestWorkbook(),
423
        sData = data + "", tmp;
424
    if ( AscCommon.c_oSerFormat.Signature === sData.substring( 0, AscCommon.c_oSerFormat.Signature.length ) ) {
425
        wb = new AscCommonExcel.Workbook( new AscCommonExcel.asc_CHandlersList(), {wb:{getWorksheet:function(){}}} );
426
        AscCommon.History.init(wb);
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
427

428
        AscCommon.g_oTableId.init();
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
429 430 431
        if ( this.User )
            g_oIdCounter.Set_UserId(this.User.asc_getId());

432 433 434 435 436 437 438
        AscCommonExcel.g_oUndoRedoCell = new AscCommonExcel.UndoRedoCell(wb);
        AscCommonExcel.g_oUndoRedoWorksheet = new AscCommonExcel.UndoRedoWoorksheet(wb);
        AscCommonExcel.g_oUndoRedoWorkbook = new AscCommonExcel.UndoRedoWorkbook(wb);
        AscCommonExcel.g_oUndoRedoCol = new AscCommonExcel.UndoRedoRowCol(wb, false);
        AscCommonExcel.g_oUndoRedoRow = new AscCommonExcel.UndoRedoRowCol(wb, true);
        AscCommonExcel.g_oUndoRedoComment = new AscCommonExcel.UndoRedoComment(wb);
        AscCommonExcel.g_oUndoRedoAutoFilters = new AscCommonExcel.UndoRedoAutoFilters(wb);
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
439
//        g_oUndoRedoGraphicObjects = new UndoRedoGraphicObjects(wb);
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
440 441
        g_oIdCounter.Set_Load(false);

442
        var oBinaryFileReader = new AscCommonExcel.BinaryFileReader();
443
        oBinaryFileReader.Read( sData, wb );
444
        ws = wb.getWorksheet( wb.getActive() );
445
        AscCommonExcel.getFormulasInfo();
446 447
    }

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
448
    /*QUnit.log( function ( details ) {
449
        console.log( "Log: " + details.name + ", result - " + details.result );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
450
    } );*/
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

    module( "Formula" );

    test( "Test: \"Absolute reference\"", function () {

        ws.getRange2( "A7" ).setValue( "1" );
        ws.getRange2( "A8" ).setValue( "2" );
        ws.getRange2( "A9" ).setValue( "3" );
        oParser = new parserFormula( 'A$7+A8', "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3 );

        oParser = new parserFormula( 'A$7+A$8', "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3 );

        oParser = new parserFormula( '$A$7+$A$8', "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3 );

        oParser = new parserFormula( 'SUM($A$7:$A$9)', "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 6 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
474
    } );
475 476 477 478 479 480 481 482
	
	test( "Test: \"Cross\"", function () {

		ws.getRange2( "A7" ).setValue( "1" );
		ws.getRange2( "A8" ).setValue( "2" );
		ws.getRange2( "A9" ).setValue( "3" );
		oParser = new parserFormula( 'A7:A9', null, ws );
		ok( oParser.parse() );
483 484 485 486 487
		strictEqual( oParser.calculate().cross(new Asc.Range(0, 5, 0, 5), ws.getId()).getValue(), "#VALUE!" );
		strictEqual( oParser.calculate().cross(new Asc.Range(0, 6, 0, 6), ws.getId()).getValue(), 1 );
		strictEqual( oParser.calculate().cross(new Asc.Range(0, 7, 0, 7), ws.getId()).getValue(), 2 );
		strictEqual( oParser.calculate().cross(new Asc.Range(0, 8, 0, 8), ws.getId()).getValue(), 3 );
		strictEqual( oParser.calculate().cross(new Asc.Range(0, 9, 0, 9), ws.getId()).getValue(), "#VALUE!" );
488

489
	} );
490

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
	test( "Test: \"Parse intersection\"", function () {

		ws.getRange2( "A7" ).setValue( "1" );
		ws.getRange2( "A8" ).setValue( "2" );
		ws.getRange2( "A9" ).setValue( "3" );
		oParser = new parserFormula( '1     +    (    A7   +A8   )   *   2', "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.assemble(), "1+(A7+A8)*2" );
		strictEqual( oParser.calculate().getValue(), 7 );

		oParser = new parserFormula( 'sum                    A1:A5', "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.assemble(), "sum A1:A5" );
		strictEqual( oParser.calculate().getValue(), "#VALUE!" );

		oParser = new parserFormula( 'sum(   A1:A5    ,        B1:B5     )     ', "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.assemble(), "SUM(A1:A5,B1:B5)" );
		strictEqual( oParser.calculate().getValue(), 0 );

		oParser = new parserFormula( 'sum(   A1:A5    ,        B1:B5  , "    3 , 14 15 92 6 "   )     ', "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.assemble(), 'SUM(A1:A5,B1:B5,"    3 , 14 15 92 6 ")' );
		strictEqual( oParser.calculate().getValue(), "#VALUE!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
516
	} );
517 518 519



Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
520
    test( "Test: \"Arithmetical operations\"", function () {
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
        oParser = new parserFormula( '1+3', "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 4 );

        oParser = new parserFormula( '(1+2)*4+3', "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), (1 + 2) * 4 + 3 );

        oParser = new parserFormula( '2^52', "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), Math.pow( 2, 52 ) );

        oParser = new parserFormula( '-10', "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), -10 );

        oParser = new parserFormula( '-10*2', "A1", ws );
        ok( oParser.parse() );
539
        strictEqual( oParser.calculate().getValue(), -20 );
540 541 542

        oParser = new parserFormula( '-10+10', "A1", ws );
        ok( oParser.parse() );
543
        strictEqual( oParser.calculate().getValue(), 0 );
544 545 546 547 548 549 550

        oParser = new parserFormula( '12%', "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 0.12 );

        oParser = new parserFormula( "2<>\"3\"", "A1", ws );
        ok( oParser.parse() );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
551
        strictEqual( oParser.calculate().getValue(), "TRUE", "2<>\"3\"" );
552 553 554

        oParser = new parserFormula( "2=\"3\"", "A1", ws );
        ok( oParser.parse() );
555 556
        strictEqual( oParser.calculate().getValue(), "FALSE", "2=\"3\"" );

557 558 559 560 561 562
        oParser = new parserFormula( "2>\"3\"", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "FALSE", "2>\"3\"" );

        oParser = new parserFormula( "\"f\">\"3\"", "A1", ws );
        ok( oParser.parse() );
563
        strictEqual( oParser.calculate().getValue(), "TRUE" );
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

        oParser = new parserFormula( "\"f\"<\"3\"", "A1", ws );
        ok( oParser.parse() );
        strictEqual( "FALSE", oParser.calculate().getValue(), "FALSE" );

        oParser = new parserFormula( "FALSE>=FALSE", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "TRUE" );

        oParser = new parserFormula( "\"TRUE\"&\"TRUE\"", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "TRUETRUE" );

        oParser = new parserFormula( "10*\"\"", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
580 581 582 583

        oParser = new parserFormula( "-TRUE", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), -1 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
584
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
585 586 587 588

    test( "Test: \"SIN have wrong arguments count\"", function () {
        oParser = new parserFormula( 'SIN(3.1415926,3.1415926*2)', "A1", ws );
        ok( !oParser.parse() );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
589
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
590 591 592 593 594

    test( "Test: \"SIN(3.1415926)\"", function () {
        oParser = new parserFormula( 'SIN(3.1415926)', "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), Math.sin( 3.1415926 ) );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
595
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
596 597 598 599 600

    test( "Test: \"COS(PI()/2)\"", function () {
        oParser = new parserFormula( 'COS(PI()/2)', "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), Math.cos( Math.PI / 2 ) );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
601
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
602

GoshaZotov's avatar
GoshaZotov committed
603 604 605 606 607 608 609 610 611 612 613
	test( "Test: \"ACOT(2)\"", function () {
		oParser = new parserFormula( 'ACOT(2)', "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), Math.PI / 2 - Math.atan(2) );
	} );

	test( "Test: \"ACOTH(6)\"", function () {
		oParser = new parserFormula( 'ACOTH(6)', "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), Math.atanh(1 / 6) );
	} );
614

GoshaZotov's avatar
GoshaZotov committed
615 616 617


	test( "Test: \"COT\"", function () {
618
		oParser = new parserFormula( 'COT(30)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
619 620
		ok( oParser.parse(), 'COT(30)' );
		strictEqual( oParser.calculate().getValue().toFixed(3) - 0, -0.156, 'COT(30)' );
621 622

		oParser = new parserFormula( 'COT(0)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
623 624
		ok( oParser.parse(), 'COT(0)' );
		strictEqual( oParser.calculate().getValue(), "#DIV/0!", 'COT(0)' );
625 626

		oParser = new parserFormula( 'COT(1000000000)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
627 628
		ok( oParser.parse(), 'COT(1000000000)' );
		strictEqual( oParser.calculate().getValue(), "#NUM!", 'COT(1000000000)' );
629 630

		oParser = new parserFormula( 'COT(-1000000000)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
631 632
		ok( oParser.parse(), 'COT(-1000000000)' );
		strictEqual( oParser.calculate().getValue(), "#NUM!", 'COT(-1000000000)' );
633 634

		oParser = new parserFormula( 'COT(test)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
635 636
		ok( oParser.parse(), 'COT(test)' );
		strictEqual( oParser.calculate().getValue(), "#NAME?", 'COT(test)' );
637 638

		oParser = new parserFormula( 'COT("test")', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
639 640
		ok( oParser.parse(), 'COT("test")' );
		strictEqual( oParser.calculate().getValue(), "#VALUE!", 'COT("test")' );
641
	} );
GoshaZotov's avatar
GoshaZotov committed
642 643

	test( "Test: \"COTH\"", function () {
644
		oParser = new parserFormula( 'COTH(2)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
645 646
		ok( oParser.parse(), 'COTH(2)' );
		strictEqual( oParser.calculate().getValue().toFixed(3) - 0, 1.037, 'COTH(2)' );
647 648

		oParser = new parserFormula( 'COTH(0)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
649 650
		ok( oParser.parse(), 'COTH(0)' );
		strictEqual( oParser.calculate().getValue(), "#DIV/0!", 'COTH(0)' );
651 652

		oParser = new parserFormula( 'COTH(1000000000)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
653 654
		ok( oParser.parse(), 'COTH(1000000000)' );
		strictEqual( oParser.calculate().getValue(), 1, 'COTH(1000000000)' );
655 656

		oParser = new parserFormula( 'COTH(-1000000000)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
657 658
		ok( oParser.parse(), 'COTH(-1000000000)' );
		strictEqual( oParser.calculate().getValue(), -1, 'COTH(-1000000000)' );
659 660

		oParser = new parserFormula( 'COTH(test)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
661 662
		ok( oParser.parse(), 'COTH(test)' );
		strictEqual( oParser.calculate().getValue(), "#NAME?", 'COTH(test)' );
663 664

		oParser = new parserFormula( 'COTH("test")', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
665 666
		ok( oParser.parse(), 'COTH("test")' );
		strictEqual( oParser.calculate().getValue(), "#VALUE!", 'COTH("test")' );
667
	} );
GoshaZotov's avatar
GoshaZotov committed
668 669

	test( "Test: \"CSC\"", function () {
GoshaZotov's avatar
GoshaZotov committed
670
		oParser = new parserFormula( 'CSC(15)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
671 672
		ok( oParser.parse(), 'CSC(15)' );
		strictEqual( oParser.calculate().getValue().toFixed(3) - 0, 1.538, 'CSC(15)' );
GoshaZotov's avatar
GoshaZotov committed
673 674

		oParser = new parserFormula( 'CSC(0)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
675 676
		ok( oParser.parse(), 'CSC(0)' );
		strictEqual( oParser.calculate().getValue(), "#DIV/0!", 'CSC(0)' );
GoshaZotov's avatar
GoshaZotov committed
677 678

		oParser = new parserFormula( 'CSC(1000000000)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
679 680
		ok( oParser.parse(), 'CSC(1000000000)' );
		strictEqual( oParser.calculate().getValue(), "#NUM!", 'CSC(1000000000)' );
GoshaZotov's avatar
GoshaZotov committed
681 682

		oParser = new parserFormula( 'CSC(-1000000000)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
683 684
		ok( oParser.parse(), 'CSC(-1000000000)' );
		strictEqual( oParser.calculate().getValue(), "#NUM!", 'CSC(-1000000000)' );
GoshaZotov's avatar
GoshaZotov committed
685 686

		oParser = new parserFormula( 'CSC(test)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
687 688
		ok( oParser.parse(), 'CSC(test)' );
		strictEqual( oParser.calculate().getValue(), "#NAME?", 'CSC(test)' );
GoshaZotov's avatar
GoshaZotov committed
689 690

		oParser = new parserFormula( 'CSC("test")', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
691 692
		ok( oParser.parse(), 'CSC("test")' );
		strictEqual( oParser.calculate().getValue(), "#VALUE!", 'CSC("test")' );
GoshaZotov's avatar
GoshaZotov committed
693
	} );
694

GoshaZotov's avatar
GoshaZotov committed
695
	test( "Test: \"CSCH\"", function () {
GoshaZotov's avatar
GoshaZotov committed
696
		oParser = new parserFormula( 'CSCH(1.5)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
697 698
		ok( oParser.parse(), 'CSCH(1.5)' );
		strictEqual( oParser.calculate().getValue().toFixed(4) - 0, 0.4696, 'CSCH(1.5)' );
GoshaZotov's avatar
GoshaZotov committed
699 700

		oParser = new parserFormula( 'CSCH(0)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
701 702
		ok( oParser.parse(), 'CSCH(0)' );
		strictEqual( oParser.calculate().getValue(), "#DIV/0!", 'CSCH(0)' );
GoshaZotov's avatar
GoshaZotov committed
703 704

		oParser = new parserFormula( 'CSCH(1000000000)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
705 706
		ok( oParser.parse(), 'CSCH(1000000000)' );
		strictEqual( oParser.calculate().getValue(), 0, 'CSCH(1000000000)' );
GoshaZotov's avatar
GoshaZotov committed
707 708

		oParser = new parserFormula( 'CSCH(-1000000000)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
709 710
		ok( oParser.parse(), 'CSCH(-1000000000)' );
		strictEqual( oParser.calculate().getValue(), 0, 'CSCH(-1000000000)' );
GoshaZotov's avatar
GoshaZotov committed
711 712

		oParser = new parserFormula( 'CSCH(test)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
713 714
		ok( oParser.parse(), 'CSCH(test)' );
		strictEqual( oParser.calculate().getValue(), "#NAME?", 'CSCH(test)' );
GoshaZotov's avatar
GoshaZotov committed
715 716

		oParser = new parserFormula( 'CSCH("test")', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
717 718
		ok( oParser.parse(), 'CSCH("test")' );
		strictEqual( oParser.calculate().getValue(), "#VALUE!", 'CSCH("test")' );
GoshaZotov's avatar
GoshaZotov committed
719 720
	} );

GoshaZotov's avatar
GoshaZotov committed
721
	test( "Test: \"SEC\"", function () {
GoshaZotov's avatar
GoshaZotov committed
722
		oParser = new parserFormula( 'SEC(45)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
723 724
		ok( oParser.parse(), 'SEC(45)' );
		strictEqual( oParser.calculate().getValue().toFixed(5) - 0, 1.90359, 'SEC(45)' );
GoshaZotov's avatar
GoshaZotov committed
725 726

		oParser = new parserFormula( 'SEC(30)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
727 728
		ok( oParser.parse(), 'SEC(30)' );
		strictEqual( oParser.calculate().getValue().toFixed(5) - 0, 6.48292, 'SEC(30)' );
GoshaZotov's avatar
GoshaZotov committed
729 730

		oParser = new parserFormula( 'SEC(0)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
731 732
		ok( oParser.parse(), 'SEC(0)' );
		strictEqual( oParser.calculate().getValue(), 1, 'SEC(0)' );
GoshaZotov's avatar
GoshaZotov committed
733 734

		oParser = new parserFormula( 'SEC(1000000000)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
735 736
		ok( oParser.parse(), 'SEC(1000000000)' );
		strictEqual( oParser.calculate().getValue(), "#NUM!", 'SEC(1000000000)' );
GoshaZotov's avatar
GoshaZotov committed
737 738

		oParser = new parserFormula( 'SEC(test)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
739 740
		ok( oParser.parse(), 'SEC(test)' );
		strictEqual( oParser.calculate().getValue(), "#NAME?", 'SEC(test)' );
GoshaZotov's avatar
GoshaZotov committed
741 742

		oParser = new parserFormula( 'SEC("test")', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
743 744
		ok( oParser.parse(), 'SEC("test")' );
		strictEqual( oParser.calculate().getValue(), "#VALUE!", 'SEC("test")' );
GoshaZotov's avatar
GoshaZotov committed
745 746
	} );

GoshaZotov's avatar
GoshaZotov committed
747
	test( "Test: \"SECH\"", function () {
GoshaZotov's avatar
GoshaZotov committed
748
		oParser = new parserFormula( 'SECH(5)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
749 750
		ok( oParser.parse(), 'SECH(5)' );
		strictEqual( oParser.calculate().getValue().toFixed(3) - 0, 0.013, 'SECH(5)' );
GoshaZotov's avatar
GoshaZotov committed
751 752

		oParser = new parserFormula( 'SECH(0)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
753 754
		ok( oParser.parse(), 'SECH(0)' );
		strictEqual( oParser.calculate().getValue(), 1, 'SECH(0)' );
GoshaZotov's avatar
GoshaZotov committed
755 756

		oParser = new parserFormula( 'SECH(1000000000)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
757 758
		ok( oParser.parse(), 'SECH(1000000000)' );
		strictEqual( oParser.calculate().getValue(), 0, 'SECH(1000000000)' );
GoshaZotov's avatar
GoshaZotov committed
759 760

		oParser = new parserFormula( 'SECH(test)', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
761 762
		ok( oParser.parse(), 'SECH(test)' );
		strictEqual( oParser.calculate().getValue(), "#NAME?", 'SECH(test)' );
GoshaZotov's avatar
GoshaZotov committed
763 764

		oParser = new parserFormula( 'SECH("test")', "A1", ws );
GoshaZotov's avatar
GoshaZotov committed
765 766
		ok( oParser.parse(), 'SECH("test")' );
		strictEqual( oParser.calculate().getValue(), "#VALUE!", 'SECH("test")' );
GoshaZotov's avatar
GoshaZotov committed
767 768
	} );

769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
	test( "Test: \"FLOOR.PRECISE\"", function () {
		oParser = new parserFormula( 'FLOOR.PRECISE(-3.2, -1)', "A1", ws );
		ok( oParser.parse(), 'FLOOR.PRECISE(-3.2, -1)' );
		strictEqual( oParser.calculate().getValue(), -4, 'FLOOR.PRECISE(-3.2, -1)' );

		oParser = new parserFormula( 'FLOOR.PRECISE(3.2, 1)', "A1", ws );
		ok( oParser.parse(), 'FLOOR.PRECISE(3.2, 1)' );
		strictEqual( oParser.calculate().getValue(), 3, 'FLOOR.PRECISE(3.2, 1)' );

		oParser = new parserFormula( 'FLOOR.PRECISE(-3.2, 1)', "A1", ws );
		ok( oParser.parse(), 'FLOOR.PRECISE(-3.2, 1)' );
		strictEqual( oParser.calculate().getValue(), -4, 'FLOOR.PRECISE(-3.2, 1)' );

		oParser = new parserFormula( 'FLOOR.PRECISE(3.2, -1)', "A1", ws );
		ok( oParser.parse(), 'FLOOR.PRECISE(3.2, -1)' );
		strictEqual( oParser.calculate().getValue(), 3, 'FLOOR.PRECISE(3.2, -1)' );

		oParser = new parserFormula( 'FLOOR.PRECISE(3.2)', "A1", ws );
		ok( oParser.parse(), 'FLOOR.PRECISE(3.2)' );
		strictEqual( oParser.calculate().getValue(), 3, 'FLOOR.PRECISE(3.2)' );

		oParser = new parserFormula( 'FLOOR.PRECISE(test)', "A1", ws );
		ok( oParser.parse(), 'FLOOR.PRECISE(test)' );
		strictEqual( oParser.calculate().getValue(), "#NAME?", 'FLOOR.PRECISE(test)' );
	} );
GoshaZotov's avatar
GoshaZotov committed
794

GoshaZotov's avatar
GoshaZotov committed
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
	test( "Test: \"FLOOR.MATH\"", function () {
		oParser = new parserFormula( 'FLOOR.MATH(24.3, 5)', "A1", ws );
		ok( oParser.parse(), 'FLOOR.MATH(24.3, 5)' );
		strictEqual( oParser.calculate().getValue(), 20, 'FLOOR.MATH(24.3, 5)' );

		oParser = new parserFormula( 'FLOOR.MATH(6.7)', "A1", ws );
		ok( oParser.parse(), 'FLOOR.MATH(6.7)' );
		strictEqual( oParser.calculate().getValue(), 6, 'FLOOR.MATH(6.7)' );

		oParser = new parserFormula( 'FLOOR.MATH(-8.1, 5)', "A1", ws );
		ok( oParser.parse(), 'FLOOR.MATH(-8.1, 5)' );
		strictEqual( oParser.calculate().getValue(), -10, 'FLOOR.MATH(-8.1, 5)' );

		oParser = new parserFormula( 'FLOOR.MATH(-5.5, 2, -1)', "A1", ws );
		ok( oParser.parse(), 'FLOOR.MATH(-5.5, 2, -1)' );
		strictEqual( oParser.calculate().getValue(), -4, 'FLOOR.MATH(-5.5, 2, -1)' );
	} );

GoshaZotov's avatar
GoshaZotov committed
813
	test( "Test: \"CEILING.MATH\"", function () {
GoshaZotov's avatar
GoshaZotov committed
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
		oParser = new parserFormula( 'CEILING.MATH(24.3, 5)', "A1", ws );
		ok( oParser.parse(), 'CEILING.MATH(24.3, 5)' );
		strictEqual( oParser.calculate().getValue(), 25, 'CEILING.MATH(24.3, 5)' );

		oParser = new parserFormula( 'CEILING.MATH(6.7)', "A1", ws );
		ok( oParser.parse(), 'CEILING.MATH(6.7)' );
		strictEqual( oParser.calculate().getValue(), 7, 'CEILING.MATH(6.7)' );

		oParser = new parserFormula( 'CEILING.MATH(-8.1, 2)', "A1", ws );
		ok( oParser.parse(), 'CEILING.MATH(-8.1, 2)' );
		strictEqual( oParser.calculate().getValue(), -8, 'CEILING.MATH(-8.1, 2)' );

		oParser = new parserFormula( 'CEILING.MATH(-5.5, 2, -1)', "A1", ws );
		ok( oParser.parse(), 'CEILING.MATH(-5.5, 2, -1)' );
		strictEqual( oParser.calculate().getValue(), -6, 'CEILING.MATH(-5.5, 2, -1)' );
	} );

GoshaZotov's avatar
GoshaZotov committed
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
	test( "Test: \"CEILING.PRECISE\"", function () {
		oParser = new parserFormula( 'CEILING.PRECISE(4.3)', "A1", ws );
		ok( oParser.parse(), 'CEILING.PRECISE(4.3)' );
		strictEqual( oParser.calculate().getValue(), 5, 'CEILING.PRECISE(4.3)' );

		oParser = new parserFormula( 'CEILING.PRECISE(-4.3)', "A1", ws );
		ok( oParser.parse(), 'CEILING.PRECISE(-4.3)' );
		strictEqual( oParser.calculate().getValue(), -4, 'CEILING.PRECISE(-4.3)' );

		oParser = new parserFormula( 'CEILING.PRECISE(4.3, 2)', "A1", ws );
		ok( oParser.parse(), 'CEILING.PRECISE(4.3, 2)' );
		strictEqual( oParser.calculate().getValue(), 6, 'CEILING.PRECISE(4.3, 2)' );

		oParser = new parserFormula( 'CEILING.PRECISE(4.3,-2)', "A1", ws );
		ok( oParser.parse(), 'CEILING.PRECISE(4.3,-2)' );
		strictEqual( oParser.calculate().getValue(), 6, 'CEILING.PRECISE(4.3,-2)' );

		oParser = new parserFormula( 'CEILING.PRECISE(-4.3,2)', "A1", ws );
		ok( oParser.parse(), 'CEILING.PRECISE(-4.3,2)' );
		strictEqual( oParser.calculate().getValue(), -4, 'CEILING.PRECISE(-4.3,2)' );

		oParser = new parserFormula( 'CEILING.PRECISE(-4.3,-2)', "A1", ws );
		ok( oParser.parse(), 'CEILING.PRECISE(-4.3,-2)' );
		strictEqual( oParser.calculate().getValue(), -4, 'CEILING.PRECISE(-4.3,-2)' );

		oParser = new parserFormula( 'CEILING.PRECISE(test)', "A1", ws );
		ok( oParser.parse(), 'CEILING.PRECISE(test)' );
		strictEqual( oParser.calculate().getValue(), "#NAME?", 'CEILING.PRECISE(test)' );
	} );

GoshaZotov's avatar
GoshaZotov committed
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
	test( "Test: \"ISO.CEILING\"", function () {
		oParser = new parserFormula( 'ISO.CEILING(4.3)', "A1", ws );
		ok( oParser.parse(), 'ISO.CEILING(4.3)' );
		strictEqual( oParser.calculate().getValue(), 5, 'ISO.CEILING(4.3)' );

		oParser = new parserFormula( 'ISO.CEILING(-4.3)', "A1", ws );
		ok( oParser.parse(), 'ISO.CEILING(-4.3)' );
		strictEqual( oParser.calculate().getValue(), -4, 'ISO.CEILING(-4.3)' );

		oParser = new parserFormula( 'ISO.CEILING(4.3, 2)', "A1", ws );
		ok( oParser.parse(), 'ISO.CEILING(4.3, 2)' );
		strictEqual( oParser.calculate().getValue(), 6, 'ISO.CEILING(4.3, 2)' );

		oParser = new parserFormula( 'ISO.CEILING(4.3,-2)', "A1", ws );
		ok( oParser.parse(), 'ISO.CEILING(4.3,-2)' );
		strictEqual( oParser.calculate().getValue(), 6, 'ISO.CEILING(4.3,-2)' );

		oParser = new parserFormula( 'ISO.CEILING(-4.3,2)', "A1", ws );
		ok( oParser.parse(), 'ISO.CEILING(-4.3,2)' );
		strictEqual( oParser.calculate().getValue(), -4, 'ISO.CEILING(-4.3,2)' );

		oParser = new parserFormula( 'ISO.CEILING(-4.3,-2)', "A1", ws );
		ok( oParser.parse(), 'ISO.CEILING(-4.3,-2)' );
		strictEqual( oParser.calculate().getValue(), -4, 'ISO.CEILING(-4.3,-2)' );
	} );

GoshaZotov's avatar
GoshaZotov committed
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
	test( "Test: \"COMBINA\"", function () {
		oParser = new parserFormula( 'COMBINA(4,3)', "A1", ws );
		ok( oParser.parse(), 'COMBINA(4,3)' );
		strictEqual( oParser.calculate().getValue(), 20, 'COMBINA(4,3)' );

		oParser = new parserFormula( 'COMBINA(10,3)', "A1", ws );
		ok( oParser.parse(), 'COMBINA(10,3)' );
		strictEqual( oParser.calculate().getValue(), 220, 'COMBINA(10,3)' );

		oParser = new parserFormula( 'COMBINA(3,10)', "A1", ws );
		ok( oParser.parse(), 'COMBINA(3,10)' );
		strictEqual( oParser.calculate().getValue(), "#NUM!", 'COMBINA(10,3)' );

		oParser = new parserFormula( 'COMBINA(10,-3)', "A1", ws );
		ok( oParser.parse(), 'COMBINA(10,-3)' );
		strictEqual( oParser.calculate().getValue(), "#NUM!", 'COMBINA(10,-3)' );
	} );

905 906 907 908 909 910 911 912 913 914 915 916 917 918
	test( "Test: \"DECIMAL\"", function () {
		oParser = new parserFormula( 'DECIMAL("FF",16)', "A1", ws );
		ok( oParser.parse(), 'DECIMAL("FF",16)' );
		strictEqual( oParser.calculate().getValue(), 255, 'DECIMAL("FF",16)' );

		oParser = new parserFormula( 'DECIMAL(111,2)', "A1", ws );
		ok( oParser.parse(), 'DECIMAL(111,2)' );
		strictEqual( oParser.calculate().getValue(), 7, 'DECIMAL(111,2)' );

		oParser = new parserFormula( 'DECIMAL("zap",36)', "A1", ws );
		ok( oParser.parse(), 'DECIMAL("zap",36)' );
		strictEqual( oParser.calculate().getValue(), 45745, 'DECIMAL("zap",36)' );
	} );

GoshaZotov's avatar
GoshaZotov committed
919 920 921 922 923 924 925 926 927 928 929 930 931 932
	test( "Test: \"BASE\"", function () {
		oParser = new parserFormula( 'BASE(7,2)', "A1", ws );
		ok( oParser.parse(), 'BASE(7,2)' );
		strictEqual( oParser.calculate().getValue(), "111", 'BASE(7,2)' );

		oParser = new parserFormula( 'BASE(100,16)', "A1", ws );
		ok( oParser.parse(), 'BASE(100,16)' );
		strictEqual( oParser.calculate().getValue(), "64", 'BASE(100,16)' );

		oParser = new parserFormula( 'BASE(15,2,10)', "A1", ws );
		ok( oParser.parse(), 'BASE(15,2,10)' );
		strictEqual( oParser.calculate().getValue(), "0000001111", 'BASE(15,2,10)' );
	} );

933 934 935 936 937
	test( "Test: \"ARABIC('LVII')\"", function () {
		oParser = new parserFormula( 'ARABIC("LVII")', "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 57 );
	} );
GoshaZotov's avatar
GoshaZotov committed
938

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
939 940 941 942
    test( "Test: \"SUM(1,2,3)\"", function () {
        oParser = new parserFormula( 'SUM(1,2,3)', "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 + 2 + 3 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
943
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
944 945 946 947 948

    test( "Test: \"\"s\"&5\"", function () {
        oParser = new parserFormula( "\"s\"&5", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "s5" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
949
    } );
950

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
    test( "Test: \"String+Number\"", function () {
        oParser = new parserFormula( "1+\"099\"", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 100 );

        ws.getRange2( "A1469" ).setValue( "'099" );
        ws.getRange2( "A1470" ).setValue( "\"099\"" );

        oParser = new parserFormula( "1+A1469", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 100 );


        oParser = new parserFormula( "1+A1470", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
968
    } );
969

970 971 972 973
    test( "Test: \"POWER(2,8)\"", function () {
        oParser = new parserFormula( "POWER(2,8)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), Math.pow( 2, 8 ) );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
974
    } );
975 976 977 978 979

    test( "Test: \"POWER(0,-3)\"", function () {
        oParser = new parserFormula( "POWER(0,-3)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#DIV/0!" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
980
    } );
981 982 983 984 985

    test( "Test: \"ISNA(A1)\"", function () {
        var r = ws.getRange2( "K1" );
        ws.getRange2( "A1" ).setValue( "#N/A" );
        r.setValue( "=ISNA(A1)" );
986
        strictEqual( ws.getCell2( "K1" ).getValueWithFormat(), "TRUE" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
987
    } );
988

Alexander.Trofimov's avatar
Alexander.Trofimov committed
989 990
    test( "Test: \"ROUNDUP(31415.92654,-2)\"", function () {
        oParser = new parserFormula( "ROUNDUP(31415.92654,-2)", "A1", ws );
991 992
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 31500 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
993
    } );
994

Alexander.Trofimov's avatar
Alexander.Trofimov committed
995 996
    test( "Test: \"ROUNDUP(3.2,0)\"", function () {
        oParser = new parserFormula( "ROUNDUP(3.2,0)", "A1", ws );
997 998
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 4 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
999
    } );
1000

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1001 1002
    test( "Test: \"ROUNDUP(-3.14159,1)\"", function () {
        oParser = new parserFormula( "ROUNDUP(-3.14159,1)", "A1", ws );
1003 1004
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), -3.2 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1005
    } );
1006

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1007 1008
    test( "Test: \"ROUNDUP(3.14159,3)\"", function () {
        oParser = new parserFormula( "ROUNDUP(3.14159,3)", "A1", ws );
1009 1010
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3.142 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1011
    } );
1012

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1013 1014
    test( "Test: \"ROUNDDOWN(31415.92654,-2)\"", function () {
        oParser = new parserFormula( "ROUNDDOWN(31415.92654,-2)", "A1", ws );
1015 1016
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 31400 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1017
    } );
1018

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1019 1020
    test( "Test: \"ROUNDDOWN(-3.14159,1)\"", function () {
        oParser = new parserFormula( "ROUNDDOWN(-3.14159,1)", "A1", ws );
1021 1022
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), -3.1 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1023
    } );
1024

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1025 1026
    test( "Test: \"ROUNDDOWN(3.14159,3)\"", function () {
        oParser = new parserFormula( "ROUNDDOWN(3.14159,3)", "A1", ws );
1027 1028
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3.141 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1029
    } );
1030

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1031 1032
    test( "Test: \"ROUNDDOWN(3.2,0)\"", function () {
        oParser = new parserFormula( "ROUNDDOWN(3.2,0)", "A1", ws );
1033 1034
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1035
    } );
1036

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1037 1038 1039
    test( "Test: \"MROUND\"", function () {
        var multiple;//должен равняться значению второго аргумента
        function mroundHelper( num ) {
1040
            var multiplier = Math.pow( 10, Math.floor( Math.log( Math.abs( num ) ) / Math.log( 10 ) ) - AscCommonExcel.cExcelSignificantDigits + 1 );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1041 1042 1043 1044 1045 1046
            var nolpiat = 0.5 * (num > 0 ? 1 : num < 0 ? -1 : 0) * multiplier;
            var y = (num + nolpiat) / multiplier;
            y = y / Math.abs( y ) * Math.floor( Math.abs( y ) )
            var x = y * multiplier / multiple

            // var x = number / multiple;
1047
            var nolpiat = 5 * (x / Math.abs( x )) * Math.pow( 10, Math.floor( Math.log( Math.abs( x ) ) / Math.log( 10 ) ) - AscCommonExcel.cExcelSignificantDigits );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1048 1049 1050 1051 1052 1053 1054
            x = x + nolpiat;
            x = x | x;

            return x * multiple;
        }


Alexander.Trofimov's avatar
Alexander.Trofimov committed
1055
        oParser = new parserFormula( "MROUND(10,3)", "A1", ws );
1056
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1057 1058
        multiple = 3;
        strictEqual( oParser.calculate().getValue(), mroundHelper( 10 + 3 / 2 ) );
1059

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1060
        oParser = new parserFormula( "MROUND(-10,-3)", "A1", ws );
1061
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1062 1063
        multiple = -3;
        strictEqual( oParser.calculate().getValue(), mroundHelper( -10 + -3 / 2 ) );
1064

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1065
        oParser = new parserFormula( "MROUND(1.3,0.2)", "A1", ws );
1066
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1067 1068
        multiple = 0.2;
        strictEqual( oParser.calculate().getValue(), mroundHelper( 1.3 + 0.2 / 2 ) );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1069
    } );
1070 1071 1072 1073 1074

    test( "Test: \"T(\"HELLO\")\"", function () {
        oParser = new parserFormula( "T(\"HELLO\")", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "HELLO" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1075
    } );
1076 1077 1078 1079 1080

    test( "Test: \"T(123)\"", function () {
        oParser = new parserFormula( "T(123)", "A1", ws );
        ok( oParser.parse() );
        ok( !oParser.calculate().getValue(), "123" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1081
    } );
1082 1083 1084 1085

    test( "Test: YEAR", function () {
        oParser = new parserFormula( "YEAR(2013)", "A1", ws );
        ok( oParser.parse() );
1086
        if ( AscCommon.bDate1904 )
1087 1088 1089
            strictEqual( oParser.calculate().getValue(), 1909 );
        else
            strictEqual( oParser.calculate().getValue(), 1905 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1090
    } );
1091 1092 1093 1094

    test( "Test: DAY", function () {
        oParser = new parserFormula( "DAY(2013)", "A1", ws );
        ok( oParser.parse() );
1095
        if ( AscCommon.bDate1904 )
1096 1097 1098
            strictEqual( oParser.calculate().getValue(), 6 );
        else
            strictEqual( oParser.calculate().getValue(), 5 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1099
    } );
1100 1101 1102 1103 1104

    test( "Test: DAY 2", function () {
        oParser = new parserFormula( "DAY(\"20 may 2045\")", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 20 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1105
    } );
1106

1107
    test( "Test: MONTH #1", function () {
1108 1109 1110
        oParser = new parserFormula( "MONTH(2013)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 7 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1111
    } );
1112

1113 1114 1115 1116
    test( "Test: MONTH #2", function () {
        oParser = new parserFormula( "MONTH(DATE(2013,2,2))", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1117
    } );
1118 1119 1120 1121

    test( "Test: MONTH #3", function () {
        oParser = new parserFormula( "MONTH(NOW())", "A1", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1122
        strictEqual( oParser.calculate().getValue(), new Date().getUTCMonth() + 1 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1123
    } );
1124

1125 1126 1127 1128
    test( "Test: \"10-3\"", function () {
        oParser = new parserFormula( "10-3", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 7 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1129
    } );
1130 1131

    test( "Test: \"SUM\"", function () {
1132 1133 1134 1135 1136 1137 1138

        ws.getRange2( "S5" ).setValue( "=1" );
        ws.getRange2( "S6" ).setValue( "=-1/Fact(2)" );
        ws.getRange2( "S7" ).setValue( "=1/Fact(4)" );
        ws.getRange2( "S8" ).setValue( "=-1/Fact(6)" );

        oParser = new parserFormula( "SUM(S5:S8)", "A1", ws );
1139
        ok( oParser.parse() );
1140
//        strictEqual( oParser.calculate().getValue(), 1-1/Math.fact(2)+1/Math.fact(4)-1/Math.fact(6) );
1141
        ok( Math.abs( oParser.calculate().getValue() - (1 - 1 / Math.fact( 2 ) + 1 / Math.fact( 4 ) - 1 / Math.fact( 6 )) ) < dif );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1142
    } );
1143 1144

    test( "Test: \"MAX\"", function () {
1145 1146 1147 1148 1149 1150 1151

        ws.getRange2( "S5" ).setValue( "=1" );
        ws.getRange2( "S6" ).setValue( "=-1/Fact(2)" );
        ws.getRange2( "S7" ).setValue( "=1/Fact(4)" );
        ws.getRange2( "S8" ).setValue( "=-1/Fact(6)" );

        oParser = new parserFormula( "MAX(S5:S8)", "A1", ws );
1152
        ok( oParser.parse() );
1153
        strictEqual( oParser.calculate().getValue(), 1 );
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

		ws.getRange2( "S5" ).setValue( "#DIV/0!" );
		ws.getRange2( "S6" ).setValue( "TRUE" );
		ws.getRange2( "S7" ).setValue( "qwe" );
		ws.getRange2( "S8" ).setValue( "" );
		ws.getRange2( "S9" ).setValue( "-1" );
		oParser = new parserFormula( "MAX(S5)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), "#DIV/0!" );
		oParser = new parserFormula( "MAX(S6)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "MAX(S7)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "MAX(S8)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "MAX(S5:S9)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), "#DIV/0!" );
		oParser = new parserFormula( "MAX(S6:S9)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), -1 );
		oParser = new parserFormula( "MAX(-1, TRUE)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 1 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1181
    } );
1182 1183

    test( "Test: \"MAXA\"", function () {
1184 1185 1186 1187 1188 1189 1190

        ws.getRange2( "S5" ).setValue( "=1" );
        ws.getRange2( "S6" ).setValue( "=-1/Fact(2)" );
        ws.getRange2( "S7" ).setValue( "=1/Fact(4)" );
        ws.getRange2( "S8" ).setValue( "=-1/Fact(6)" );

        oParser = new parserFormula( "MAXA(S5:S8)", "A1", ws );
1191
        ok( oParser.parse() );
1192
        strictEqual( oParser.calculate().getValue(), 1 );
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219

		ws.getRange2( "S5" ).setValue( "#DIV/0!" );
		ws.getRange2( "S6" ).setValue( "TRUE" );
		ws.getRange2( "S7" ).setValue( "qwe" );
		ws.getRange2( "S8" ).setValue( "" );
		ws.getRange2( "S9" ).setValue( "-1" );
		oParser = new parserFormula( "MAXA(S5)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), "#DIV/0!" );
		oParser = new parserFormula( "MAXA(S6)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 1 );
		oParser = new parserFormula( "MAXA(S7)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "MAXA(S8)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "MAXA(S5:S9)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), "#DIV/0!" );
		oParser = new parserFormula( "MAXA(S6:S9)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 1 );
		oParser = new parserFormula( "MAXA(-1, TRUE)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 1 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1220
    } );
1221 1222

    test( "Test: \"MIN\"", function () {
1223 1224 1225 1226 1227 1228 1229

        ws.getRange2( "S5" ).setValue( "=1" );
        ws.getRange2( "S6" ).setValue( "=-1/Fact(2)" );
        ws.getRange2( "S7" ).setValue( "=1/Fact(4)" );
        ws.getRange2( "S8" ).setValue( "=-1/Fact(6)" );

        oParser = new parserFormula( "MIN(S5:S8)", "A1", ws );
1230
        ok( oParser.parse() );
1231
        strictEqual( oParser.calculate().getValue(), -1 / Math.fact( 2 ) );
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258

		ws.getRange2( "S5" ).setValue( "#DIV/0!" );
		ws.getRange2( "S6" ).setValue( "TRUE" );
		ws.getRange2( "S7" ).setValue( "qwe" );
		ws.getRange2( "S8" ).setValue( "" );
		ws.getRange2( "S9" ).setValue( "2" );
		oParser = new parserFormula( "MIN(S5)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), "#DIV/0!" );
		oParser = new parserFormula( "MIN(S6)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "MIN(S7)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "MIN(S8)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "MIN(S5:S9)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), "#DIV/0!" );
		oParser = new parserFormula( "MIN(S6:S9)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 2 );
		oParser = new parserFormula( "MIN(2, TRUE)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 1 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1259
    } );
1260 1261

    test( "Test: \"MINA\"", function () {
1262 1263 1264 1265 1266 1267 1268

        ws.getRange2( "S5" ).setValue( "=1" );
        ws.getRange2( "S6" ).setValue( "=-1/Fact(2)" );
        ws.getRange2( "S7" ).setValue( "=1/Fact(4)" );
        ws.getRange2( "S8" ).setValue( "=-1/Fact(6)" );

        oParser = new parserFormula( "MINA(S5:S8)", "A1", ws );
1269
        ok( oParser.parse() );
1270
        strictEqual( oParser.calculate().getValue(), -1 / Math.fact( 2 ) );
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297

		ws.getRange2( "S5" ).setValue( "#DIV/0!" );
		ws.getRange2( "S6" ).setValue( "TRUE" );
		ws.getRange2( "S7" ).setValue( "qwe" );
		ws.getRange2( "S8" ).setValue( "" );
		ws.getRange2( "S9" ).setValue( "2" );
		oParser = new parserFormula( "MINA(S5)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), "#DIV/0!" );
		oParser = new parserFormula( "MINA(S6)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 1 );
		oParser = new parserFormula( "MINA(S7)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "MINA(S8)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "MINA(S5:S9)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), "#DIV/0!" );
		oParser = new parserFormula( "MINA(S6:S9)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "MINA(2, TRUE)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 1 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1298
    } );
1299 1300 1301 1302 1303 1304

    test( "Test: SUM(S7:S9,{1,2,3})", function () {
        ws.getRange2( "S7" ).setValue( "1" );
        ws.getRange2( "S8" ).setValue( "2" );
        ws.getRange2( "S9" ).setValue( "3" );
        ws.getRange2( "S10" ).setValue( "=SUM(S7:S9,{1,2,3})" );
1305
        strictEqual( ws.getCell2( "S10" ).getValueWithFormat(), "12" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1306
    } );
1307 1308 1309 1310 1311

    test( "Test: ISREF", function () {
        oParser = new parserFormula( "ISREF(G0)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "FALSE" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1312
    } );
1313 1314 1315 1316 1317

    test( "Test: MOD", function () {
        oParser = new parserFormula( "MOD(7,3)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1318
    } );
1319 1320

    test( "Test: rename sheet #1", function () {
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1321
        ws = wb.getWorksheet( 0 );
1322
        ws.getRange2( "S95" ).setValue( "2" );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1323 1324
        ws = wb.getWorksheet( 1 );
        ws.getRange2( "S100" ).setValue( "=" + wb.getWorksheet( 0 ).getName() + "!S95" );
1325
        strictEqual( ws.getCell2( "S100" ).getValueWithFormat(), "2" );
1326

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1327
        wb.getWorksheet( 0 ).setName( "ЛистTEMP" );
1328

1329
        strictEqual( ws.getCell2( "S100" ).getFormula(), wb.getWorksheet( 0 ).getName() + "!S95" );
1330

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1331
    } );
1332 1333 1334 1335

    test( "Test: wrong ref", function () {
        oParser = new parserFormula( "1+XXX1", "A1", ws );
        ok( oParser.parse() );
1336
        strictEqual( oParser.calculate().getValue(), "#NAME?" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1337
    } );
1338 1339 1340 1341 1342

    test( "Test: \"CODE\"", function () {
        oParser = new parserFormula( "CODE(\"abc\")", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 97 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1343
    } );
1344 1345 1346 1347 1348

    test( "Test: \"CHAR\"", function () {
        oParser = new parserFormula( "CHAR(97)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "a" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1349
    } );
1350 1351 1352 1353 1354

    test( "Test: \"CHAR(CODE())\"", function () {
        oParser = new parserFormula( "CHAR(CODE(\"A\"))", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "A" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1355
    } );
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366

    test( "Test: \"PROPER\"", function () {
        oParser = new parserFormula( "PROPER(\"2-cent's worth\")", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "2-Cent'S Worth" );
        oParser = new parserFormula( "PROPER(\"76BudGet\")", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "76Budget" );
        oParser = new parserFormula( "PROPER(\"this is a TITLE\")", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "This Is A Title" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1367
    } );
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378

    test( "Test: \"GCD\"", function () {
        oParser = new parserFormula( "GCD(10,100,50)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 10 );
        oParser = new parserFormula( "GCD(24.6,36.2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 12 );
        oParser = new parserFormula( "GCD(-1,39,52)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1379
    } );
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392

    test( "Test: \"FIXED\"", function () {
        oParser = new parserFormula( "FIXED(1234567,-3)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "1,235,000" );
        oParser = new parserFormula( "FIXED(.555555,10)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "0.5555550000" );
        oParser = new parserFormula( "FIXED(1234567.555555,4,TRUE)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "1234567.5556" );
        oParser = new parserFormula( "FIXED(1234567)", "A1", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1393
        strictEqual( oParser.calculate().getValue(), "1,234,567.00" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1394
    } );
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409

    test( "Test: \"REPLACE\"", function () {

        oParser = new parserFormula( "REPLACE(\"abcdefghijk\",3,4,\"XY\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "abXYghijk" );

        oParser = new parserFormula( "REPLACE(\"abcdefghijk\",3,1,\"12345\")", "B2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "ab12345defghijk" );

        oParser = new parserFormula( "REPLACE(\"abcdefghijk\",15,4,\"XY\")", "C2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "abcdefghijkXY" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1410
    } );
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435

    test( "Test: \"SEARCH\"", function () {

        oParser = new parserFormula( "SEARCH(\"~*\",\"abc*dEF\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 4 );

        oParser = new parserFormula( "SEARCH(\"~\",\"abc~dEF\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 4 );

        oParser = new parserFormula( "SEARCH(\"de\",\"abcdEF\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 4 );

        oParser = new parserFormula( "SEARCH(\"?c*e\",\"abcdEF\")", "B2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2 );

        oParser = new parserFormula( "SEARCH(\"de\",\"dEFabcdEF\",3)", "C2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 7 );

        oParser = new parserFormula( "SEARCH(\"de\",\"dEFabcdEF\",30)", "C2", ws );
        ok( oParser.parse() );
1436
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );
1437

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1438
    } );
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457

    test( "Test: \"SUBSTITUTE\"", function () {

        oParser = new parserFormula( "SUBSTITUTE(\"abcaAabca\",\"a\",\"xx\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "xxbcxxAxxbcxx" );

        oParser = new parserFormula( "SUBSTITUTE(\"abcaaabca\",\"a\",\"xx\")", "B2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "xxbcxxxxxxbcxx" );

        oParser = new parserFormula( "SUBSTITUTE(\"abcaaabca\",\"a\",\"\",10)", "C2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "bcbc" );

        oParser = new parserFormula( "SUBSTITUTE(\"abcaaabca\",\"a\",\"xx\",3)", "C2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "abcaxxabca" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1458
    } );
1459 1460 1461 1462 1463 1464 1465

    test( "Test: \"TRIM\"", function () {

        oParser = new parserFormula( "TRIM(\"     abc         def      \")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "abc def" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1466
    } );
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479

    test( "Test: \"DOLLAR\"", function () {

        oParser = new parserFormula( "DOLLAR(1234.567)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "$1,234.57" );

        oParser = new parserFormula( "DOLLAR(1234.567,-2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "$1,200" );

        oParser = new parserFormula( "DOLLAR(-1234.567,4)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1480
        strictEqual( oParser.calculate().getValue(), "($1,234.5670)" );
1481

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1482
    } );
1483 1484 1485 1486 1487 1488 1489 1490 1491

    test( "Test: \"VALUE\"", function () {

        oParser = new parserFormula( "VALUE(\"123.456\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 123.456 );

        oParser = new parserFormula( "VALUE(\"$1,000\")", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1492
        strictEqual( oParser.calculate().getValue(), 1000 );
1493 1494 1495 1496 1497 1498 1499 1500

        oParser = new parserFormula( "VALUE(\"23-Mar-2002\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 37338 );

        oParser = new parserFormula( "VALUE(\"03-26-2006\")", "A2", ws );
        ok( oParser.parse() );

1501
        if ( AscCommon.bDate1904 )
1502 1503 1504 1505 1506 1507
            strictEqual( oParser.calculate().getValue(), 37340 );
        else
            strictEqual( oParser.calculate().getValue(), 38802 );

        oParser = new parserFormula( "VALUE(\"16:48:00\")-VALUE(\"12:17:12\")", "A2", ws );
        ok( oParser.parse() );
1508
        strictEqual( oParser.calculate().getValue(), AscCommon.g_oFormatParser.parse( "16:48:00" ).value - AscCommon.g_oFormatParser.parse( "12:17:12" ).value );
1509

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1510
    } );
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521

    test( "Test: \"DATEVALUE\"", function () {

        oParser = new parserFormula( "DATEVALUE(\"10-10-2010 10:26\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 40461 );

        oParser = new parserFormula( "DATEVALUE(\"10-10-2010 10:26\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 40461 );

1522 1523 1524
        tmp = ws.getRange2( "A7" );
        tmp.setNumFormat('@');
        tmp.setValue( "3-Mar" );
1525 1526
        oParser = new parserFormula( "DATEVALUE(A7)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
fix:  
Dmitry.Shahtanov committed
1527 1528 1529 1530
        var d = new Date();
        d.setUTCMonth(2);
        d.setUTCDate(3);
        strictEqual( oParser.calculate().getValue(), d.getExcelDate() );
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542

        oParser = new parserFormula( "DATEVALUE(\"$1,000\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

        oParser = new parserFormula( "DATEVALUE(\"23-Mar-2002\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 37338 );

        oParser = new parserFormula( "DATEVALUE(\"03-26-2006\")", "A2", ws );
        ok( oParser.parse() );

1543
        if ( AscCommon.bDate1904 )
1544 1545 1546
            strictEqual( oParser.calculate().getValue(), 37340 );
        else
            strictEqual( oParser.calculate().getValue(), 38802 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1547
    } );
1548 1549 1550

    test( "Test: \"EDATE\"", function () {

1551
        if ( !AscCommon.bDate1904 ) {
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
            oParser = new parserFormula( "EDATE(DATE(2006,1,31),5)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 38898 );

            oParser = new parserFormula( "EDATE(DATE(2004,2,29),12)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 38411 );

            ws.getRange2( "A7" ).setValue( "02-28-2004" );
            oParser = new parserFormula( "EDATE(A7,12)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 38411 );

            oParser = new parserFormula( "EDATE(DATE(2004,1,15),-23)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 37302 );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1568
        }
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
        else {
            oParser = new parserFormula( "EDATE(DATE(2006,1,31),5)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 37436 );

            oParser = new parserFormula( "EDATE(DATE(2004,2,29),12)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 36949 );

            ws.getRange2( "A7" ).setValue( "02-28-2004" );
            oParser = new parserFormula( "EDATE(A7,12)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 36949 );

            oParser = new parserFormula( "EDATE(DATE(2004,1,15),-23)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 35840 );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1586
        }
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1587
    } );
1588 1589 1590

    test( "Test: \"EOMONTH\"", function () {

1591
        if ( !AscCommon.bDate1904 ) {
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
            oParser = new parserFormula( "EOMONTH(DATE(2006,1,31),5)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 38898 );

            oParser = new parserFormula( "EOMONTH(DATE(2004,2,29),12)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 38411 );

            ws.getRange2( "A7" ).setValue( "02-28-2004" );
            oParser = new parserFormula( "EOMONTH(A7,12)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 38411 );

            oParser = new parserFormula( "EOMONTH(DATE(2004,1,15),-23)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 37315 );
        }
        else {
            oParser = new parserFormula( "EOMONTH(DATE(2006,1,31),5)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 37436 );

            oParser = new parserFormula( "EOMONTH(DATE(2004,2,29),12)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 36949 );

            ws.getRange2( "A7" ).setValue( "02-28-2004" );
            oParser = new parserFormula( "EOMONTH(A7,12)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 36949 );

            oParser = new parserFormula( "EOMONTH(DATE(2004,1,15),-23)", "A2", ws );
            ok( oParser.parse() );
            strictEqual( oParser.calculate().getValue(), 35853 );
        }
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1627
    } );
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642

    test( "Test: \"NETWORKDAYS\"", function () {

        oParser = new parserFormula( "NETWORKDAYS(DATE(2006,1,1),DATE(2006,1,31))", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 22 );

        oParser = new parserFormula( "NETWORKDAYS(DATE(2006,1,31),DATE(2006,1,1))", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), -22 );

        oParser = new parserFormula( "NETWORKDAYS(DATE(2006,1,1),DATE(2006,2,1),{\"01-02-2006\",\"01-16-2006\"})", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 21 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1643
    } );
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660

    test( "Test: \"SUMIF\"", function () {

        ws.getRange2( "A2" ).setValue( "100000" );
        ws.getRange2( "A3" ).setValue( "200000" );
        ws.getRange2( "A4" ).setValue( "300000" );
        ws.getRange2( "A5" ).setValue( "400000" );

        ws.getRange2( "B2" ).setValue( "7000" );
        ws.getRange2( "B3" ).setValue( "14000" );
        ws.getRange2( "B4" ).setValue( "21000" );
        ws.getRange2( "B5" ).setValue( "28000" );

        ws.getRange2( "C2" ).setValue( "250000" );

        oParser = new parserFormula( "SUMIF(A2:A5,\">160000\",B2:B5)", "A7", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1661
        strictEqual( oParser.calculate().getValue(), 63000 );
1662 1663 1664

        oParser = new parserFormula( "SUMIF(A2:A5,\">160000\")", "A8", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1665
        strictEqual( oParser.calculate().getValue(), 900000 );
1666 1667 1668

        oParser = new parserFormula( "SUMIF(A2:A5,300000,B2:B5)", "A9", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1669
        strictEqual( oParser.calculate().getValue(), 21000 );
1670 1671 1672

        oParser = new parserFormula( "SUMIF(A2:A5,\">\" & C2,B2:B5)", "A10", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1673
        strictEqual( oParser.calculate().getValue(), 49000 );
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698


        ws.getRange2( "A12" ).setValue( "Vegetables" );
        ws.getRange2( "A13" ).setValue( "Vegetables" );
        ws.getRange2( "A14" ).setValue( "Fruits" );
        ws.getRange2( "A15" ).setValue( "" );
        ws.getRange2( "A16" ).setValue( "Vegetables" );
        ws.getRange2( "A17" ).setValue( "Fruits" );

        ws.getRange2( "B12" ).setValue( "Tomatoes" );
        ws.getRange2( "B13" ).setValue( "Celery" );
        ws.getRange2( "B14" ).setValue( "Oranges" );
        ws.getRange2( "B15" ).setValue( "Butter" );
        ws.getRange2( "B16" ).setValue( "Carrots" );
        ws.getRange2( "B17" ).setValue( "Apples" );

        ws.getRange2( "C12" ).setValue( "2300" );
        ws.getRange2( "C13" ).setValue( "5500" );
        ws.getRange2( "C14" ).setValue( "800" );
        ws.getRange2( "C15" ).setValue( "400" );
        ws.getRange2( "C16" ).setValue( "4200" );
        ws.getRange2( "C17" ).setValue( "1200" );

        oParser = new parserFormula( "SUMIF(A12:A17,\"Fruits\",C12:C17)", "A19", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1699
        strictEqual( oParser.calculate().getValue(), 2000 );
1700 1701 1702

        oParser = new parserFormula( "SUMIF(A12:A17,\"Vegetables\",C12:C17)", "A20", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1703
        strictEqual( oParser.calculate().getValue(), 12000 );
1704 1705 1706

        oParser = new parserFormula( "SUMIF(B12:B17,\"*es\",C12:C17)", "A21", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1707
        strictEqual( oParser.calculate().getValue(), 4300 );
1708 1709 1710

        oParser = new parserFormula( "SUMIF(A12:A17,\"\",C12:C17)", "A22", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1711
        strictEqual( oParser.calculate().getValue(), 400 );
1712

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1713
    } );
1714

1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
    test( "Test: \"SUMIFS\"", function () {

        ws.getRange2( "A2" ).setValue( "5" );
        ws.getRange2( "A3" ).setValue( "4" );
        ws.getRange2( "A4" ).setValue( "15" );
        ws.getRange2( "A5" ).setValue( "3" );
        ws.getRange2( "A6" ).setValue( "22" );
        ws.getRange2( "A7" ).setValue( "12" );
        ws.getRange2( "A8" ).setValue( "10" );
        ws.getRange2( "A9" ).setValue( "33" );

        ws.getRange2( "B2" ).setValue( "Apples" );
        ws.getRange2( "B3" ).setValue( "Apples" );
        ws.getRange2( "B4" ).setValue( "Artichokes" );
        ws.getRange2( "B5" ).setValue( "Artichokes" );
        ws.getRange2( "B6" ).setValue( "Bananas" );
        ws.getRange2( "B7" ).setValue( "Bananas" );
        ws.getRange2( "B8" ).setValue( "Carrots" );
        ws.getRange2( "B9" ).setValue( "Carrots" );

        ws.getRange2( "C2" ).setValue( "Tom" );
        ws.getRange2( "C3" ).setValue( "Sarah" );
        ws.getRange2( "C4" ).setValue( "Tom" );
        ws.getRange2( "C5" ).setValue( "Sarah" );
        ws.getRange2( "C6" ).setValue( "Tom" );
        ws.getRange2( "C7" ).setValue( "Sarah" );
        ws.getRange2( "C8" ).setValue( "Tom" );
        ws.getRange2( "C9" ).setValue( "Sarah" );

        oParser = new parserFormula( "SUMIFS(A2:A9, B2:B9, \"=A*\", C2:C9, \"Tom\")", "A10", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 20 );

        oParser = new parserFormula( "SUMIFS(A2:A9, B2:B9, \"<>Bananas\", C2:C9, \"Tom\")", "A11", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 30 );

    } );
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763

    test( "Test: \"TEXT\"", function () {

        oParser = new parserFormula( "TEXT(1234.567,\"$0.00\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "$1234.57" );

        oParser = new parserFormula( "TEXT(0.125,\"0.0%\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "12.5%" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1764
    } );
1765 1766 1767 1768 1769

    test( "Test: \"WORKDAY\"", function () {

        oParser = new parserFormula( "WORKDAY(DATE(2006,1,1),0)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1770
        strictEqual( oParser.calculate().getValue(), 38718 );
1771 1772 1773

        oParser = new parserFormula( "WORKDAY(DATE(2006,1,1),10)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1774
        strictEqual( oParser.calculate().getValue(), 38730 );
1775 1776 1777

        oParser = new parserFormula( "WORKDAY(DATE(2006,1,1),-10)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1778
        strictEqual( oParser.calculate().getValue(), 38705 );
1779 1780 1781

        oParser = new parserFormula( "WORKDAY(DATE(2006,1,1),20,{\"1-2-2006\",\"1-16-2006\"})", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
1782
        strictEqual( oParser.calculate().getValue(), 38748 );
1783

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1784
    } );
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797

    test( "Test: \"TIMEVALUE\"", function () {
        oParser = new parserFormula( "timevalue(\"10:02:34\")", "A2", ws );
        ok( oParser.parse() );
        ok( Math.abs( oParser.calculate().getValue() - 0.4184490740740740 ) < dif );

        oParser = new parserFormula( "timevalue(\"02-01-2006 10:15:29 AM\")", "A2", ws );
        ok( oParser.parse() );
        ok( Math.abs( oParser.calculate().getValue() - 0.4274189814823330 ) < dif );

        oParser = new parserFormula( "timevalue(\"22:02\")", "A2", ws );
        ok( oParser.parse() );
        ok( Math.abs( oParser.calculate().getValue() - 0.9180555555555560 ) < dif );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1798
    } );
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817

    test( "Test: \"DAYS360\"", function () {

        oParser = new parserFormula( "DAYS360(DATE(2002,2,3),DATE(2005,5,31))", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1198 );

        oParser = new parserFormula( "DAYS360(DATE(2005,5,31),DATE(2002,2,3))", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), -1197 );

        oParser = new parserFormula( "DAYS360(DATE(2002,2,3),DATE(2005,5,31),FALSE)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1198 );

        oParser = new parserFormula( "DAYS360(DATE(2002,2,3),DATE(2005,5,31),TRUE)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1197 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1818
    } );
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908

    test( "Test: \"WEEKNUM\"", function () {
        oParser = new parserFormula( "WEEKNUM(DATE(2006,1,1))", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "WEEKNUM(DATE(2006,1,1),17)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "WEEKNUM(DATE(2006,1,1),1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "WEEKNUM(DATE(2006,1,1),21)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 52 );

        oParser = new parserFormula( "WEEKNUM(DATE(2006,2,1),1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 5 );

        oParser = new parserFormula( "WEEKNUM(DATE(2006,2,1),2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 6 );

        oParser = new parserFormula( "WEEKNUM(DATE(2006,2,1),11)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 6 );

        oParser = new parserFormula( "WEEKNUM(DATE(2007,1,1),15)", "A2", ws );//понед
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "WEEKNUM(DATE(2008,1,1),15)", "A2", ws );//втор
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "WEEKNUM(DATE(2003,1,1),15)", "A2", ws );//сред
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "WEEKNUM(DATE(2009,1,1),15)", "A2", ws );//чет
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "WEEKNUM(DATE(2010,1,1),15)", "A2", ws );//пят
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "WEEKNUM(DATE(2011,1,1),15)", "A2", ws );//суб
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "WEEKNUM(DATE(2012,1,1),11)", "A2", ws );//вск
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "WEEKNUM(DATE(2008,1,4),11)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "WEEKNUM(DATE(2008,1,10),11)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2 );

        oParser = new parserFormula( "WEEKNUM(DATE(2008,1,11),11)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2 );

        oParser = new parserFormula( "WEEKNUM(DATE(2008,1,17),11)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3 );

        oParser = new parserFormula( "WEEKNUM(DATE(2008,1,18),11)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3 );

        oParser = new parserFormula( "WEEKNUM(DATE(2008,1,24),11)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 4 );

        oParser = new parserFormula( "WEEKNUM(DATE(2013,1,1),21)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "WEEKNUM(DATE(2013,1,7))", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
1909
    } );
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954

    test( "Test: \"YEARFRAC\"", function () {
        function okWrapper( a, b ) {
            ok( Math.abs( a - b ) < dif );
        }

        oParser = new parserFormula( "YEARFRAC(DATE(2006,1,1),DATE(2006,3,26))", "A2", ws );
        ok( oParser.parse() );
        okWrapper( oParser.calculate().getValue(), 0.236111111 );

        oParser = new parserFormula( "YEARFRAC(DATE(2006,3,26),DATE(2006,1,1))", "A2", ws );
        ok( oParser.parse() );
        okWrapper( oParser.calculate().getValue(), 0.236111111 );

        oParser = new parserFormula( "YEARFRAC(DATE(2006,1,1),DATE(2006,7,1))", "A2", ws );
        ok( oParser.parse() );
        okWrapper( oParser.calculate().getValue(), 0.5 );

        oParser = new parserFormula( "YEARFRAC(DATE(2006,1,1),DATE(2007,9,1))", "A2", ws );
        ok( oParser.parse() );
        okWrapper( oParser.calculate().getValue(), 1.666666667 );

        oParser = new parserFormula( "YEARFRAC(DATE(2006,1,1),DATE(2006,7,1),0)", "A2", ws );
        ok( oParser.parse() );
        okWrapper( oParser.calculate().getValue(), 0.5 );

        oParser = new parserFormula( "YEARFRAC(DATE(2006,1,1),DATE(2006,7,1),1)", "A2", ws );
        ok( oParser.parse() );
        okWrapper( oParser.calculate().getValue(), 0.495890411 );

        oParser = new parserFormula( "YEARFRAC(DATE(2006,1,1),DATE(2006,7,1),2)", "A2", ws );
        ok( oParser.parse() );
        okWrapper( oParser.calculate().getValue(), 0.502777778 );

        oParser = new parserFormula( "YEARFRAC(DATE(2006,1,1),DATE(2006,7,1),3)", "A2", ws );
        ok( oParser.parse() );
        okWrapper( oParser.calculate().getValue(), 0.495890411 );

        oParser = new parserFormula( "YEARFRAC(DATE(2006,1,1),DATE(2006,7,1),4)", "A2", ws );
        ok( oParser.parse() );
        okWrapper( oParser.calculate().getValue(), 0.5 );

        oParser = new parserFormula( "YEARFRAC(DATE(2004,3,1),DATE(2006,3,1),1)", "A2", ws );
        ok( oParser.parse() );
        okWrapper( oParser.calculate().getValue(), 1.998175182481752 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1955
    } );
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973

    test( "Test: \"DATEDIF\"", function () {

        oParser = new parserFormula( "DATEDIF(DATE(2001,1,1),DATE(2003,1,1),\"Y\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2 );

        oParser = new parserFormula( "DATEDIF(DATE(2001,6,1),DATE(2002,8,15),\"D\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 440 );

        oParser = new parserFormula( "DATEDIF(DATE(2001,6,1),DATE(2002,8,15),\"YD\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 75 );

        oParser = new parserFormula( "DATEDIF(DATE(2001,6,1),DATE(2002,8,15),\"MD\")", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 14 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
1974
    } );
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010

    test( "Test: \"SUMPRODUCT\"", function () {

        oParser = new parserFormula( "SUMPRODUCT({2,3})", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 5 );

        oParser = new parserFormula( "SUMPRODUCT({2,3},{4,5})", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 23 );

        oParser = new parserFormula( "SUMPRODUCT({2,3},{4,5},{2,2})", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 46 );

        oParser = new parserFormula( "SUMPRODUCT({2,3;4,5},{2,2;3,4})", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 42 );

        ws.getRange2( "N44" ).setValue( "1" );
        ws.getRange2( "N45" ).setValue( "2" );
        ws.getRange2( "N46" ).setValue( "3" );
        ws.getRange2( "N47" ).setValue( "4" );

        ws.getRange2( "O44" ).setValue( "5" );
        ws.getRange2( "O45" ).setValue( "6" );
        ws.getRange2( "O46" ).setValue( "7" );
        ws.getRange2( "O47" ).setValue( "8" );

        ws.getRange2( "P44" ).setValue( "9" );
        ws.getRange2( "P45" ).setValue( "10" );
        ws.getRange2( "P46" ).setValue( "11" );
        ws.getRange2( "P47" ).setValue( "12" );
        oParser = new parserFormula( "SUMPRODUCT(N44:N47,O44:O47,P44:P47)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 780 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
2011
    } );
2012 2013 2014 2015 2016 2017 2018 2019 2020

    test( "Test: \"SINH\"", function () {

        oParser = new parserFormula( "SINH(0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 0 );

        oParser = new parserFormula( "SINH(1)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2021
        strictEqual( oParser.calculate().getValue(), ((Math.E - 1 / Math.E) / 2) );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
2022
    } );
2023 2024 2025 2026 2027 2028 2029 2030 2031

    test( "Test: \"COSH\"", function () {

        oParser = new parserFormula( "COSH(0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "COSH(1)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2032
        strictEqual( oParser.calculate().getValue(), ((Math.E + 1 / Math.E) / 2) );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
2033
    } );
2034 2035 2036 2037 2038 2039 2040 2041 2042

    test( "Test: \"TANH\"", function () {

        oParser = new parserFormula( "TANH(0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 0 );

        oParser = new parserFormula( "TANH(1)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2043
        strictEqual( difBetween( oParser.calculate().getValue(), ((Math.E * Math.E - 1) / (Math.E * Math.E + 1)) ), true );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
2044
    } );
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070

    test( "Test: \"COMBIN\"", function () {

        oParser = new parserFormula( "COMBIN(8,2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 28 );

        oParser = new parserFormula( "COMBIN(10,4)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 210 );

        oParser = new parserFormula( "COMBIN(6,5)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 6 );

        oParser = new parserFormula( "COMBIN(-6,5)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "COMBIN(3,5)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "COMBIN(6,-5)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
2071
    } );
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093

    test( "Test: \"FACTDOUBLE\"", function () {

        oParser = new parserFormula( "FACTDOUBLE(8)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2 * 4 * 6 * 8 );

        oParser = new parserFormula( "FACTDOUBLE(9)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 9 * 7 * 5 * 3 );

        oParser = new parserFormula( "FACTDOUBLE(6.5)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 6 * 4 * 2 );

        oParser = new parserFormula( "FACTDOUBLE(-6)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "FACTDOUBLE(600)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
2094
    } );
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115

    test( "Test: \"GCD\"", function () {
        oParser = new parserFormula( "LCM(5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 5 );

        oParser = new parserFormula( "LCM(24.6,36.2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 72 );

        oParser = new parserFormula( "LCM(-1,39,52)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "LCM(0,39,52)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "LCM(24,36,15)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 360 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
2116
    } );
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133

    test( "Test: \"RANDBETWEEN\"", function () {
        var res;
        oParser = new parserFormula( "RANDBETWEEN(1,6)", "A1", ws );
        ok( oParser.parse() );
        res = oParser.calculate().getValue()
        ok( res >= 1 && res <= 6 );

        oParser = new parserFormula( "RANDBETWEEN(-10,10)", "A1", ws );
        ok( oParser.parse() );
        res = oParser.calculate().getValue()
        ok( res >= -10 && res <= 10 );

        oParser = new parserFormula( "RANDBETWEEN(-25,-3)", "A1", ws );
        ok( oParser.parse() );
        res = oParser.calculate().getValue()
        ok( res >= -25 && res <= -3 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
2134
    } );
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151

    test( "Test: \"QUOTIENT\"", function () {
        oParser = new parserFormula( "QUOTIENT(1,6)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 0 );

        oParser = new parserFormula( "QUOTIENT(-10,3)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), -3 );

        oParser = new parserFormula( "QUOTIENT(5,3)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "QUOTIENT(5,0)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#DIV/0!" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
2152
    } );
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170

    test( "Test: \"TRUNC\"", function () {
        oParser = new parserFormula( "TRUNC(PI())", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3 );

        oParser = new parserFormula( "TRUNC(PI(),3)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3.141 );

        oParser = new parserFormula( "TRUNC(PI(),-2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 0 );

        oParser = new parserFormula( "TRUNC(-PI(),2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), -3.14 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2171
    } );
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185

    test( "Test: \"MULTINOMIAL\"", function () {
        oParser = new parserFormula( "MULTINOMIAL(2,3,4)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), Math.fact( 2 + 3 + 4 ) / (Math.fact( 2 ) * Math.fact( 3 ) * Math.fact( 4 )) );

        oParser = new parserFormula( "MULTINOMIAL(2,3,\"r\")", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

        oParser = new parserFormula( "MULTINOMIAL(150,50)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2186
    } );
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204

    test( "Test: \"SUMSQ\"", function () {
        oParser = new parserFormula( "SUMSQ(2.5,-3.6,2.4)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2.5 * 2.5 + 3.6 * 3.6 + 2.4 * 2.4 );

        oParser = new parserFormula( "SUMSQ(2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 4 );

        oParser = new parserFormula( "SUMSQ(150,50)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 150 * 150 + 50 * 50 );

        oParser = new parserFormula( "SUMSQ(150,\"f\")", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2205
    } );
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244

    test( "Test: \"ROMAN\"", function () {

        oParser = new parserFormula( "ROMAN(499,0)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "CDXCIX" );

        oParser = new parserFormula( "ROMAN(499,1)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "LDVLIV" );

        oParser = new parserFormula( "ROMAN(499,2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "XDIX" );

        oParser = new parserFormula( "ROMAN(499,3)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "VDIV" );

        oParser = new parserFormula( "ROMAN(499,4)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "ID" );

        oParser = new parserFormula( "ROMAN(2013,0)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "MMXIII" );

        oParser = new parserFormula( "ROMAN(2013,5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

        oParser = new parserFormula( "ROMAN(-2013,1)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

        oParser = new parserFormula( "ROMAN(2499,1)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "MMLDVLIV" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2245
    } );
2246

2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260
    test( "Test: \"SUMXMY2\"", function () {

        oParser = new parserFormula( "SUMXMY2({2,3,9,1,8,7,5},{6,5,11,7,5,4,4})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 79 );

        oParser = new parserFormula( "SUMXMY2({2,3,9;1,8,7},{6,5,11;7,5,4})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 78 );

        oParser = new parserFormula( "SUMXMY2(7,5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2261
    } );
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276

    test( "Test: \"SUMX2MY2\"", function () {

        oParser = new parserFormula( "SUMX2MY2({2,3,9,1,8,7,5},{6,5,11,7,5,4,4})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), -55 );

        oParser = new parserFormula( "SUMX2MY2({2,3,9;1,8,7},{6,5,11;7,5,4})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), -64 );

        oParser = new parserFormula( "SUMX2MY2(7,5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2277
    } );
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292

    test( "Test: \"SUMX2MY2\"", function () {

        oParser = new parserFormula( "SUMX2PY2({2,3,9,1,8,7,5},{6,5,11,7,5,4,4})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 521 );

        oParser = new parserFormula( "SUMX2PY2({2,3,9;1,8,7},{6,5,11;7,5,4})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 480 );

        oParser = new parserFormula( "SUMX2PY2(7,5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2293
    } );
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313

    test( "Test: \"SERIESSUM\"", function () {

        ws.getRange2( "A2" ).setValue( "=1" );
        ws.getRange2( "A3" ).setValue( "=-1/Fact(2)" );
        ws.getRange2( "A4" ).setValue( "=1/Fact(4)" );
        ws.getRange2( "A5" ).setValue( "=-1/Fact(6)" );

        oParser = new parserFormula( "SERIESSUM(PI()/4,0,2,A2:A5)", "A7", ws );
        ok( oParser.parse() );
        ok( Math.abs( oParser.calculate().getValue() - (1 - 1 / 2 * Math.pow( Math.PI / 4, 2 ) + 1 / Math.fact( 4 ) * Math.pow( Math.PI / 4, 4 ) - 1 / Math.fact( 6 ) * Math.pow( Math.PI / 4, 6 )) ) < dif );

        ws.getRange2( "B2" ).setValue( "=1" );
        ws.getRange2( "B3" ).setValue( "=-1/Fact(3)" );
        ws.getRange2( "B4" ).setValue( "=1/Fact(5)" );
        ws.getRange2( "B5" ).setValue( "=-1/Fact(7)" );

        oParser = new parserFormula( "SERIESSUM(PI()/4,1,2,B2:B5)", "B7", ws );
        ok( oParser.parse() );
        ok( Math.abs( oParser.calculate().getValue() - (Math.PI / 4 - 1 / Math.fact( 3 ) * Math.pow( Math.PI / 4, 3 ) + 1 / Math.fact( 5 ) * Math.pow( Math.PI / 4, 5 ) - 1 / Math.fact( 7 ) * Math.pow( Math.PI / 4, 7 )) ) < dif );
2314

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2315
    } );
2316

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
    /*
    * Mathematical Function
    * */
    test( "Test: \"CEILING\"", function () {

        oParser = new parserFormula( "CEILING(2.5,1)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3 );

        oParser = new parserFormula( "CEILING(-2.5,-2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), -4 );

        oParser = new parserFormula( "CEILING(-2.5,2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), -2 );

        oParser = new parserFormula( "CEILING(1.5,0.1)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1.5 );

        oParser = new parserFormula( "CEILING(0.234,0.01)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 0.24 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2342
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2343 2344


2345
    /*
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2346
     * Statistical Function
2347
     * */
2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359
    test( "Test: \"AVEDEV\"", function () {

        oParser = new parserFormula( "AVEDEV(-3.5,1.4,6.9,-4.5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 4.075 );

        oParser = new parserFormula( "AVEDEV({-3.5,1.4,6.9,-4.5})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 4.075 );

        oParser = new parserFormula( "AVEDEV(-3.5,1.4,6.9,-4.5,-0.3)", "A1", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2360
        strictEqual( difBetween( oParser.calculate().getValue(), 3.32 ), true );
2361

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2362
    } );
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381

    test( "Test: \"AVERAGE\"", function () {

        oParser = new parserFormula( "AVERAGE(1,2,3,4,5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3 );

        oParser = new parserFormula( "AVERAGE({1,2;3,4})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2.5 );

        oParser = new parserFormula( "AVERAGE({1,2,3,4,5},6,\"7\")", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 4 );

        oParser = new parserFormula( "AVERAGE({1,\"2\",TRUE,4})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2.5 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2382
    } );
2383 2384 2385 2386 2387 2388

    test( "Test: \"AVERAGEA\"", function () {

        ws.getRange2( "E2" ).setValue( "TRUE" );
        ws.getRange2( "E3" ).setValue( "FALSE" );

2389 2390 2391 2392 2393 2394 2395
		ws.getRange2( "F2" ).setValue( "10" );
		ws.getRange2( "F3" ).setValue( "7" );
		ws.getRange2( "F4" ).setValue( "9" );
		ws.getRange2( "F5" ).setValue( "2" );
		ws.getRange2( "F6" ).setValue( "Not available" );
		ws.getRange2( "F7" ).setValue( "" );

2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
        oParser = new parserFormula( "AVERAGEA(10,E1)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 10 );

        oParser = new parserFormula( "AVERAGEA(10,E2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 5.5 );

        oParser = new parserFormula( "AVERAGEA(10,E3)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 5 );

2408 2409 2410 2411 2412 2413 2414 2415
		oParser = new parserFormula( "AVERAGEA(F2:F6)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 5.6 );

		oParser = new parserFormula( "AVERAGEA(F2:F5,F7)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 7 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2416
    } );
2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428

    test( "Test: \"AVERAGEIF\"", function () {

        ws.getRange2( "E2" ).setValue( "10" );
        ws.getRange2( "E3" ).setValue( "20" );
        ws.getRange2( "E4" ).setValue( "28" );
        ws.getRange2( "E5" ).setValue( "30" );

        oParser = new parserFormula( "AVERAGEIF(E2:E5,\">15\")", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 26 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2429
    } );
2430

2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461
	test( "Test: \"AVERAGEIFS\"", function () {

		ws.getRange2( "E2" ).setValue( "Quiz" );
		ws.getRange2( "E3" ).setValue( "Grade" );
		ws.getRange2( "E4" ).setValue( "75" );
		ws.getRange2( "E5" ).setValue( "94" );

		ws.getRange2( "F2" ).setValue( "Quiz" );
		ws.getRange2( "F3" ).setValue( "Grade" );
		ws.getRange2( "F4" ).setValue( "85" );
		ws.getRange2( "F5" ).setValue( "80" );

		ws.getRange2( "G2" ).setValue( "Exam" );
		ws.getRange2( "G3" ).setValue( "Grade" );
		ws.getRange2( "G4" ).setValue( "87" );
		ws.getRange2( "G5" ).setValue( "88" );

		oParser = new parserFormula( "AVERAGEIFS(E2:E5,E2:E5,\">70\",E2:E5,\"<90\")", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 75 );

		oParser = new parserFormula( "AVERAGEIFS(F2:F5,F2:F5,\">95\")", "A2", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), "#DIV/0!" );

		oParser = new parserFormula( "AVERAGEIFS(G2:G5,G2:G5,\"<>Incomplete\",G2:G5,\">80\")", "A3", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 87.5 );

	} );

2462 2463
    test( "Test: \"BINOMDIST\"", function () {

2464 2465 2466 2467
        function binomdist( x, n, p ) {
            x = parseInt( x );
            n = parseInt( n );
            return Math.binomCoeff( n, x ) * Math.pow( p, x ) * Math.pow( 1 - p, n - x );
2468 2469 2470 2471
        }

        oParser = new parserFormula( "BINOMDIST(6,10,0.5,FALSE)", "A1", ws );
        ok( oParser.parse() );
2472
        ok( Math.abs( oParser.calculate().getValue() - binomdist( 6, 10, 0.5 ) ) < dif );
2473 2474 2475

        oParser = new parserFormula( "BINOMDIST(6,10,0.5,TRUE)", "A1", ws );
        ok( oParser.parse() );
2476
        ok( Math.abs( oParser.calculate().getValue() - (function () {
2477
            var bm = 0;
2478 2479
            for ( var y = 0; y <= 6; y++ ) {
                bm += binomdist( y, 10, 0.5 )
2480 2481 2482 2483 2484 2485 2486 2487
            }
            return bm;
        })() ) < dif );

        oParser = new parserFormula( "BINOMDIST(11,10,0.5,FALSE)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2488
    } );
2489 2490 2491 2492 2493

    test( "Test: \"CONFIDENCE\"", function () {

        oParser = new parserFormula( "CONFIDENCE(0.4,5,12)", "A1", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2494
        strictEqual( difBetween( oParser.calculate().getValue(), 1.214775614397568 ), true );
2495 2496 2497

        oParser = new parserFormula( "CONFIDENCE(0.75,9,7)", "A1", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2498
        strictEqual( difBetween( oParser.calculate().getValue(), 1.083909233527114 ), true );
2499

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2500
    } );
2501 2502 2503 2504 2505

    test( "Test: \"CORREL\"", function () {

        oParser = new parserFormula( "CORREL({2.532,5.621;2.1,3.4},{5.32,2.765;5.2,\"f\"})", "A1", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2506
        strictEqual( difBetween( oParser.calculate().getValue(), -0.988112020032211 ), true );
2507 2508 2509

        oParser = new parserFormula( "CORREL({1;2;3},{4;5;\"E\"})", "A1", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2510
        strictEqual( difBetween( oParser.calculate().getValue(), 1 ), true );
2511 2512 2513

        oParser = new parserFormula( "CORREL({1,2},{1,\"e\"})", "A1", ws );
        ok( oParser.parse() );
2514
        strictEqual( oParser.calculate().getValue(), "#DIV/0!" );
2515

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2516
    } );
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541

    test( "Test: \"COUNT\"", function () {

        ws.getRange2( "E2" ).setValue( "TRUE" );

        oParser = new parserFormula( "COUNT({1,2,3,4,5})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 5 );

        oParser = new parserFormula( "COUNT(1,2,3,4,5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 5 );

        oParser = new parserFormula( "COUNT({1,2,3,4,5},6,\"7\")", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 7 );

        oParser = new parserFormula( "COUNT(10,E150)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "COUNT(10,E2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564
		ws.getRange2( "S5" ).setValue( "#DIV/0!" );
		ws.getRange2( "S6" ).setValue( "TRUE" );
		ws.getRange2( "S7" ).setValue( "qwe" );
		ws.getRange2( "S8" ).setValue( "" );
		ws.getRange2( "S9" ).setValue( "2" );
		oParser = new parserFormula( "COUNT(S5)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "COUNT(S6)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "COUNT(S7)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "COUNT(S8)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "COUNT(S5:S9)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 1 );
		oParser = new parserFormula( "COUNT(S6:S9)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 1 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
2565
    } );
2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590

    test( "Test: \"COUNTA\"", function () {

        ws.getRange2( "E2" ).setValue( "TRUE" );

        oParser = new parserFormula( "COUNTA({1,2,3,4,5})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 5 );

        oParser = new parserFormula( "COUNTA(1,2,3,4,5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 5 );

        oParser = new parserFormula( "COUNTA({1,2,3,4,5},6,\"7\")", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 7 );

        oParser = new parserFormula( "COUNTA(10,E150)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "COUNTA(10,E2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2 );

2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
		ws.getRange2( "S5" ).setValue( "#DIV/0!" );
		ws.getRange2( "S6" ).setValue( "TRUE" );
		ws.getRange2( "S7" ).setValue( "qwe" );
		ws.getRange2( "S8" ).setValue( "" );
		ws.getRange2( "S9" ).setValue( "2" );
		oParser = new parserFormula( "COUNTA(S5)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 1 );
		oParser = new parserFormula( "COUNTA(S6)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 1 );
		oParser = new parserFormula( "COUNTA(S7)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 1 );
		oParser = new parserFormula( "COUNTA(S8)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 0 );
		oParser = new parserFormula( "COUNTA(S5:S9)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 4 );
		oParser = new parserFormula( "COUNTA(S6:S9)", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 3 );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
2614
    } );
2615

2616
    test( "Test: \"COUNTIFS\"", function () {
2617

2618 2619 2620 2621
        ws.getRange2( "A15" ).setValue( "Yes" );
        ws.getRange2( "A16" ).setValue( "Yes" );
        ws.getRange2( "A17" ).setValue( "Yes" );
        ws.getRange2( "A18" ).setValue( "No" );
2622

2623 2624 2625 2626
        ws.getRange2( "B15" ).setValue( "No" );
        ws.getRange2( "B16" ).setValue( "Yes" );
        ws.getRange2( "B17" ).setValue( "Yes" );
        ws.getRange2( "B18" ).setValue( "Yes" );
2627

2628 2629 2630 2631
		ws.getRange2( "C15" ).setValue( "No" );
		ws.getRange2( "C16" ).setValue( "No" );
		ws.getRange2( "C17" ).setValue( "Yes" );
		ws.getRange2( "C18" ).setValue( "Yes" );
2632

2633
        oParser = new parserFormula( "COUNTIFS(A15:C15,\"=Yes\")", "A1", ws );
2634
        ok( oParser.parse() );
2635
        strictEqual( oParser.calculate().getValue(), 1 );
2636

2637
        oParser = new parserFormula( "COUNTIFS(A15:A18,\"=Yes\",B15:B18,\"=Yes\")", "B1", ws );
2638 2639 2640
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2 );

2641
		oParser = new parserFormula( "COUNTIFS(A18:C18,\"=Yes\",A16:C16,\"=Yes\")", "C1", ws );
2642
        ok( oParser.parse() );
2643
        strictEqual( oParser.calculate().getValue(), 1 );
2644

2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661
		ws.getRange2( "D15" ).setValue( "1" );
		ws.getRange2( "D16" ).setValue( "2" );
		ws.getRange2( "D17" ).setValue( "3" );
		ws.getRange2( "D18" ).setValue( "4" );
		ws.getRange2( "D19" ).setValue( "5" );
		ws.getRange2( "D20" ).setValue( "6" );

		ws.getRange2( "E15" ).setValue( "5/1/2011" );
		ws.getRange2( "E16" ).setValue( "5/2/2011" );
		ws.getRange2( "E17" ).setValue( "5/3/2011" );
		ws.getRange2( "E18" ).setValue( "5/4/2011" );
		ws.getRange2( "E19" ).setValue( "5/5/2011" );
		ws.getRange2( "E20" ).setValue( "5/6/2011" );

		oParser = new parserFormula( "COUNTIFS(D15:D20,\"<6\",D15:D20,\">1\")", "D1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 4 );
2662

2663 2664 2665 2666 2667 2668 2669
		oParser = new parserFormula( "COUNTIFS(D15:D20,\"<5\",E15:E20,\"<5/3/2011\")", "E1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 2 );

		oParser = new parserFormula( "COUNTIFS(D15:D20,\"<\" & D19,E15:E20,\"<\" & E17)", "E1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 2 );
2670

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2671
    } );
2672

2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
	test( "Test: \"COUNTIFS\"", function () {

		ws.getRange2( "A7" ).setValue( "3" );
		ws.getRange2( "B7" ).setValue( "10" );
		ws.getRange2( "C7" ).setValue( "7" );
		ws.getRange2( "D7" ).setValue( "10" );

		ws.getRange2( "A8" ).setValue( "apples" );
		ws.getRange2( "B8" ).setValue( "oranges" );
		ws.getRange2( "C8" ).setValue( "grapes" );
		ws.getRange2( "D8" ).setValue( "melons" );


		oParser = new parserFormula( "COUNTIF(A7:D7,\"=10\")", "A1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 2 );

		oParser = new parserFormula( "COUNTIF(A7:D7,\">5\")", "B1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 3 );

		oParser = new parserFormula( "COUNTIF(A7:D7,\"<>10\")", "C1", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 2 );

		oParser = new parserFormula( "COUNTIF(A8:D8,\"*es\")", "A2", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 3 );

		oParser = new parserFormula( "COUNTIF(A8:D8,\"??a*\")", "B2", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 2 );

		oParser = new parserFormula( "COUNTIF(A8:D8,\"*l*\")", "C2", ws );
		ok( oParser.parse() );
		strictEqual( oParser.calculate().getValue(), 2 );

	} );

2712 2713 2714 2715
    test( "Test: \"COVAR\"", function () {

        oParser = new parserFormula( "COVAR({2.532,5.621;2.1,3.4},{5.32,2.765;5.2,6.7})", "A1", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2716
        strictEqual( difBetween( oParser.calculate().getValue(), -1.3753740625 ), true );
2717

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2718
        oParser = new parserFormula( "COVAR({1,2},{4,5})", "B1", ws );
2719
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2720
        strictEqual( difBetween( oParser.calculate().getValue(), 0.25 ), true );
2721

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2722
    } );
2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752

    test( "Test: \"CRITBINOM\"", function () {

        oParser = new parserFormula( "CRITBINOM(6,0.5,0.75)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 4 );

        oParser = new parserFormula( "CRITBINOM(12,0.3,0.95)", "B1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 6 );

        oParser = new parserFormula( "CRITBINOM(-12,0.3,0.95)", "B1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "CRITBINOM(-12,1.3,0.95)", "B1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "CRITBINOM(-12,-1.3,0.95)", "B1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "CRITBINOM(-12,0,0.95)", "B1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "CRITBINOM(-12,0.3,1.95)", "B1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );
2753

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2754
    } );
2755

2756 2757 2758 2759 2760 2761 2762 2763 2764 2765
    test( "Test: \"DEVSQ\"", function () {

        var ws1 = wb.getWorksheet( 1 );

        ws1.getRange2( "A1" ).setValue( "5.6" );
        ws1.getRange2( "A2" ).setValue( "8.2" );
        ws1.getRange2( "A3" ).setValue( "9.2" );

        oParser = new parserFormula( "DEVSQ(5.6,8.2,9.2)", "A1", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2766
        strictEqual( difBetween( oParser.calculate().getValue(), 6.906666666666665 ), true );
2767 2768 2769

        oParser = new parserFormula( "DEVSQ({5.6,8.2,9.2})", "A1", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2770
        strictEqual( difBetween( oParser.calculate().getValue(), 6.906666666666665 ), true );
2771 2772 2773

        oParser = new parserFormula( "DEVSQ(5.6,8.2,\"9.2\")", "A1", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2774
        strictEqual( difBetween( oParser.calculate().getValue(), 3.379999999999999 ), true );
2775 2776 2777

        oParser = new parserFormula( "DEVSQ(Лист2!A1:A3)", "A1", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2778
        strictEqual( difBetween( oParser.calculate().getValue(), 6.906666666666665 ), true );
2779

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2780
    } );
2781 2782 2783 2784 2785

    test( "Test: \"EXPONDIST\"", function () {

        oParser = new parserFormula( "EXPONDIST(0.2,10,FALSE)", "A1", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2786
        strictEqual( difBetween( oParser.calculate().getValue(), 1.353352832366127 ), true );
2787 2788 2789

        oParser = new parserFormula( "EXPONDIST(2.3,1.5,TRUE)", "A1", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
2790
        strictEqual( difBetween( oParser.calculate().getValue(), 0.968254363621932 ), true );
2791

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2792
    } );
2793 2794 2795

    test( "Test: \"FISHER\"", function () {

2796 2797
        function fisher( x ) {
            return toFixed( 0.5 * Math.ln( (1 + x) / (1 - x) ) );
2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815
        }

        oParser = new parserFormula( "FISHER(-0.43)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), fisher( -.43 ) );

        oParser = new parserFormula( "FISHER(0.578)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), fisher( 0.578 ) );

        oParser = new parserFormula( "FISHER(1)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "FISHER(-1)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2816
    } );
2817 2818 2819

    test( "Test: \"FISHERINV\"", function () {

2820 2821
        function fisherInv( x ) {
            return toFixed( ( Math.exp( 2 * x ) - 1 ) / ( Math.exp( 2 * x ) + 1 ) );
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839
        }

        oParser = new parserFormula( "FISHERINV(-0.43)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), fisherInv( -.43 ) );

        oParser = new parserFormula( "FISHERINV(0.578)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), fisherInv( 0.578 ) );

        oParser = new parserFormula( "FISHERINV(1)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), fisherInv( 1 ) );

        oParser = new parserFormula( "FISHERINV(-1)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), fisherInv( -1 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2840
    } );
2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871

    test( "Test: \"FORECAST\"", function () {

        function forecast( fx, y, x ) {

            var fSumDeltaXDeltaY = 0, fSumSqrDeltaX = 0, _x = 0, _y = 0, xLength = 0;
            for ( var i = 0; i < x.length; i++ ) {
                _x += x[i];
                _y += y[i];
                xLength++;
            }

            _x /= xLength;
            _y /= xLength;

            for ( var i = 0; i < x.length; i++ ) {

                var fValX = x[i];
                var fValY = y[i];

                fSumDeltaXDeltaY += ( fValX - _x ) * ( fValY - _y );
                fSumSqrDeltaX += ( fValX - _x ) * ( fValX - _x );

            }

            return toFixed( _y + fSumDeltaXDeltaY / fSumSqrDeltaX * ( fx - _x ) );

        }

        oParser = new parserFormula( "FORECAST(30,{6,7,9,15,21},{20,28,31,38,40})", "A1", ws );
        ok( oParser.parse() );
2872
        strictEqual( oParser.calculate().getValue(), forecast( 30, [6, 7, 9, 15, 21], [20, 28, 31, 38, 40] ) );
2873

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2874
    } );
2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894

    test( "Test: \"FREQUENCY\"", function () {

        ws.getRange2( "A202" ).setValue( "79" );
        ws.getRange2( "A203" ).setValue( "85" );
        ws.getRange2( "A204" ).setValue( "78" );
        ws.getRange2( "A205" ).setValue( "85" );
        ws.getRange2( "A206" ).setValue( "50" );
        ws.getRange2( "A207" ).setValue( "81" );
        ws.getRange2( "A208" ).setValue( "95" );
        ws.getRange2( "A209" ).setValue( "88" );
        ws.getRange2( "A210" ).setValue( "97" );

        ws.getRange2( "B202" ).setValue( "70" );
        ws.getRange2( "B203" ).setValue( "89" );
        ws.getRange2( "B204" ).setValue( "79" );

        oParser = new parserFormula( "FREQUENCY(A202:A210,B202:B204)", "A201", ws );
        ok( oParser.parse() );
        var a = oParser.calculate()
2895 2896 2897 2898
        strictEqual( a.getElement( 0 ).getValue(), 1 );
        strictEqual( a.getElement( 1 ).getValue(), 2 );
        strictEqual( a.getElement( 2 ).getValue(), 4 );
        strictEqual( a.getElement( 3 ).getValue(), 2 );
2899

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2900
    } );
2901 2902 2903 2904 2905

    test( "Test: \"GAMMALN\"", function () {

        oParser = new parserFormula( "GAMMALN(4.5)", "A1", ws );
        ok( oParser.parse() );
2906
        strictEqual( oParser.calculate().getValue().toFixed( 14 ) - 0, 2.45373657084244 );
2907 2908 2909 2910 2911

        oParser = new parserFormula( "GAMMALN(-4.5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2912
    } );
2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927

    test( "Test: \"GEOMEAN\"", function () {

        function geommean( x ) {

            var s1 = 0, _x = 1, xLength = 0, _tx;
            for ( var i = 0; i < x.length; i++ ) {
                _x *= x[i];
            }

            return  Math.pow( _x, 1 / x.length )
        }

        oParser = new parserFormula( "GEOMEAN(10.5,5.3,2.9)", "A1", ws );
        ok( oParser.parse() );
2928
        strictEqual( oParser.calculate().getValue(), geommean( [10.5, 5.3, 2.9] ) );
2929 2930 2931

        oParser = new parserFormula( "GEOMEAN(10.5,{5.3,2.9},\"12\")", "A1", ws );
        ok( oParser.parse() );
2932
        strictEqual( oParser.calculate().getValue(), geommean( [10.5, 5.3, 2.9, 12] ) );
2933 2934 2935 2936 2937

        oParser = new parserFormula( "GEOMEAN(10.5,{5.3,2.9},\"12\",0)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2938
    } );
2939 2940 2941 2942

    test( "Test: \"HARMEAN\"", function () {

        function harmmean( x ) {
2943

2944 2945
            var _x = 0, xLength = 0;
            for ( var i = 0; i < x.length; i++ ) {
2946
                _x += 1 / x[i];
2947 2948
                xLength++;
            }
2949
            return xLength / _x;
2950 2951 2952 2953
        }

        oParser = new parserFormula( "HARMEAN(10.5,5.3,2.9)", "A1", ws );
        ok( oParser.parse() );
2954
        strictEqual( oParser.calculate().getValue(), harmmean( [10.5, 5.3, 2.9] ) );
2955 2956 2957

        oParser = new parserFormula( "HARMEAN(10.5,{5.3,2.9},\"12\")", "A1", ws );
        ok( oParser.parse() );
2958
        strictEqual( oParser.calculate().getValue(), harmmean( [10.5, 5.3, 2.9, 12] ) );
2959 2960 2961 2962 2963

        oParser = new parserFormula( "HARMEAN(10.5,{5.3,2.9},\"12\",0)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2964
    } );
2965 2966 2967 2968

    test( "Test: \"HYPGEOMDIST\"", function () {

        function hypgeomdist( x, n, M, N ) {
2969
            return toFixed( Math.binomCoeff( M, x ) * Math.binomCoeff( N - M, n - x ) / Math.binomCoeff( N, n ) );
2970 2971 2972 2973
        }

        oParser = new parserFormula( "HYPGEOMDIST(1,4,8,20)", "A1", ws );
        ok( oParser.parse() );
2974
        strictEqual( oParser.calculate().getValue(), hypgeomdist( 1, 4, 8, 20 ) );
2975 2976 2977

        oParser = new parserFormula( "HYPGEOMDIST(1,4,8,20)", "A1", ws );
        ok( oParser.parse() );
2978
        strictEqual( oParser.calculate().getValue(), hypgeomdist( 1, 4, 8, 20 ) );
2979 2980 2981 2982 2983 2984 2985 2986 2987

        oParser = new parserFormula( "HYPGEOMDIST(-1,4,8,20)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "HYPGEOMDIST(5,4,8,20)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
2988
    } );
2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019

    test( "Test: \"INTERCEPT\"", function () {

        function intercept( y, x ) {

            var fSumDeltaXDeltaY = 0, fSumSqrDeltaX = 0, _x = 0, _y = 0, xLength = 0;
            for ( var i = 0; i < x.length; i++ ) {
                _x += x[i];
                _y += y[i];
                xLength++;
            }

            _x /= xLength;
            _y /= xLength;

            for ( var i = 0; i < x.length; i++ ) {

                var fValX = x[i];
                var fValY = y[i];

                fSumDeltaXDeltaY += ( fValX - _x ) * ( fValY - _y );
                fSumSqrDeltaX += ( fValX - _x ) * ( fValX - _x );

            }

            return toFixed( _y - fSumDeltaXDeltaY / fSumSqrDeltaX * _x );

        }

        oParser = new parserFormula( "INTERCEPT({6,7,9,15,21},{20,28,31,38,40})", "A1", ws );
        ok( oParser.parse() );
3020
        strictEqual( oParser.calculate().getValue(), intercept( [6, 7, 9, 15, 21], [20, 28, 31, 38, 40] ) );
3021

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3022
    } );
3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036

    test( "Test: \"KURT\"", function () {

        function kurt( x ) {

            var sumSQRDeltaX = 0, _x = 0, xLength = 0, standDev = 0, sumSQRDeltaXDivstandDev = 0;
            for ( var i = 0; i < x.length; i++ ) {
                _x += x[i];
                xLength++;
            }

            _x /= xLength;

            for ( var i = 0; i < x.length; i++ ) {
3037
                sumSQRDeltaX += Math.pow( x[i] - _x, 2 );
3038 3039 3040 3041 3042
            }

            standDev = Math.sqrt( sumSQRDeltaX / ( xLength - 1 ) );

            for ( var i = 0; i < x.length; i++ ) {
3043
                sumSQRDeltaXDivstandDev += Math.pow( (x[i] - _x) / standDev, 4 );
3044 3045
            }

3046
            return toFixed( xLength * (xLength + 1) / (xLength - 1) / (xLength - 2) / (xLength - 3) * sumSQRDeltaXDivstandDev - 3 * (xLength - 1) * (xLength - 1) / (xLength - 2) / (xLength - 3) )
3047 3048 3049 3050 3051

        }

        oParser = new parserFormula( "KURT(10.5,12.4,19.4,23.2)", "A1", ws );
        ok( oParser.parse() );
3052
        strictEqual( oParser.calculate().getValue(), kurt( [10.5, 12.4, 19.4, 23.2] ) );
3053 3054 3055

        oParser = new parserFormula( "KURT(10.5,{12.4,19.4},23.2)", "A1", ws );
        ok( oParser.parse() );
3056
        strictEqual( oParser.calculate().getValue(), kurt( [10.5, 12.4, 19.4, 23.2] ) );
3057 3058 3059 3060 3061

        oParser = new parserFormula( "KURT(10.5,12.4,19.4)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3062
    } );
3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073

    test( "Test: \"LARGE\"", function () {

        oParser = new parserFormula( "LARGE({3,5,3,5,4;4,2,4,6,7},3)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 5 );

        oParser = new parserFormula( "LARGE({3,5,3,5,4;4,2,4,6,7},7)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 4 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3074
    } );
3075 3076 3077 3078 3079

    test( "Test: \"MEDIAN\"", function () {

        function median( x ) {

3080
            x.sort(fSortAscending);
3081

3082 3083
            if ( x.length % 2 )
                return x[(x.length - 1) / 2];
3084
            else
3085
                return (x[x.length / 2 - 1] + x[x.length / 2]) / 2;
3086 3087 3088 3089
        }

        oParser = new parserFormula( "MEDIAN(10.5,12.4,19.4,23.2)", "A1", ws );
        ok( oParser.parse() );
3090
        strictEqual( oParser.calculate().getValue(), median( [10.5, 12.4, 19.4, 23.2] ) );
3091 3092 3093

        oParser = new parserFormula( "MEDIAN(10.5,{12.4,19.4},23.2)", "A1", ws );
        ok( oParser.parse() );
3094
        strictEqual( oParser.calculate().getValue(), median( [10.5, 12.4, 19.4, 23.2] ) );
3095 3096 3097

        oParser = new parserFormula( "MEDIAN(-3.5,1.4,6.9,-4.5)", "A1", ws );
        ok( oParser.parse() );
3098
        strictEqual( oParser.calculate().getValue(), median( [-3.5, 1.4, 6.9, -4.5] ) );
3099

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3100
    } );
3101 3102 3103 3104 3105

    test( "Test: \"MODE\"", function () {

        function mode( x ) {

3106
            x.sort(AscCommon.fSortDescending);
3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139

            if ( x.length < 1 )
                return "#VALUE!";
            else {
                var nMaxIndex = 0, nMax = 1, nCount = 1, nOldVal = x[0], i;

                for ( i = 1; i < x.length; i++ ) {
                    if ( x[i] == nOldVal )
                        nCount++;
                    else {
                        nOldVal = x[i];
                        if ( nCount > nMax ) {
                            nMax = nCount;
                            nMaxIndex = i - 1;
                        }
                        nCount = 1;
                    }
                }
                if ( nCount > nMax ) {
                    nMax = nCount;
                    nMaxIndex = i - 1;
                }
                if ( nMax == 1 && nCount == 1 )
                    return "#VALUE!";
                else if ( nMax == 1 )
                    return nOldVal;
                else
                    return x[nMaxIndex];
            }
        }

        oParser = new parserFormula( "MODE(9,1,5,1,9,5,6,6)", "A1", ws );
        ok( oParser.parse() );
3140
        strictEqual( oParser.calculate().getValue(), mode( [9, 1, 5, 1, 9, 5, 6, 6] ) );
3141 3142 3143

        oParser = new parserFormula( "MODE(1,9,5,1,9,5,6,6)", "A1", ws );
        ok( oParser.parse() );
3144
        strictEqual( oParser.calculate().getValue(), mode( [1, 9, 5, 1, 9, 5, 6, 6] ) );
3145 3146 3147

        oParser = new parserFormula( "MODE(1,9,5,5,9,5,6,6)", "A1", ws );
        ok( oParser.parse() );
3148 3149
        strictEqual( oParser.calculate().getValue(), mode( [1, 9, 5, 5, 9, 5, 6, 6] ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3150
    } );
3151 3152 3153 3154

    test( "Test: \"NORMDIST\"", function () {

        function normdist( x, mue, sigma, kum ) {
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
3155
            if ( sigma <= 0 )
3156 3157
                return "#NUM!";
            else if ( kum == false )
3158
                return toFixed( AscCommonExcel.phi( (x - mue) / sigma ) / sigma );
3159
            else
3160
                return toFixed( 0.5 + AscCommonExcel.gauss( (x - mue) / sigma ) );
3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175

        }

        oParser = new parserFormula( "NORMDIST(42,40,1.5,TRUE)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), normdist( 42, 40, 1.5, true ) );

        oParser = new parserFormula( "NORMDIST(42,40,1.5,FALSE)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), normdist( 42, 40, 1.5, false ) );

        oParser = new parserFormula( "NORMDIST(42,40,-1.5,TRUE)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), normdist( 42, 40, -1.5, true ) );

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
3176 3177 3178 3179
        oParser = new parserFormula( "NORMDIST(1,40,-1.5,TRUE)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), normdist( 1, 40, -1.5, true ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3180
    } );
3181 3182 3183 3184

    test( "Test: \"NORMSDIST\"", function () {

        function normsdist( x ) {
3185
            return toFixed( 0.5 + AscCommonExcel.gauss( x ) );
3186 3187 3188 3189 3190 3191 3192 3193 3194 3195
        }

        oParser = new parserFormula( "NORMSDIST(1.333333)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), normsdist( 1.333333 ) );

        oParser = new parserFormula( "NORMSDIST(-1.5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), normsdist( -1.5 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3196
    } );
3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220

    test( "Test: \"NEGBINOMDIST\"", function () {

        function negbinomdist( x, r, p ) {
            x = parseInt( x );
            r = parseInt( r );
            if ( x < 0 || r < 1 || p < 0 || p > 1 )
                return "#NUM!";
            else
                return toFixed( Math.binomCoeff( x + r - 1, r - 1 ) * Math.pow( p, r ) * Math.pow( 1 - p, x ) );
        }

        oParser = new parserFormula( "NEGBINOMDIST(6,10,0.5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), negbinomdist( 6, 10, 0.5 ) );

        oParser = new parserFormula( "NEGBINOMDIST(6,10,1.5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), negbinomdist( 6, 10, 1.5 ) );

        oParser = new parserFormula( "NEGBINOMDIST(20,10,0.63)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), negbinomdist( 20, 10, 0.63 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3221
    } );
3222 3223 3224 3225 3226 3227 3228

    test( "Test: \"NORMSINV\"", function () {

        function normsinv( x ) {
            if ( x <= 0.0 || x >= 1.0 )
                return "#N/A";
            else
3229
                return toFixed( AscCommonExcel.gaussinv( x ) );
3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251
        }

        oParser = new parserFormula( "NORMSINV(0.954)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), normsinv( 0.954 ) );

        oParser = new parserFormula( "NORMSINV(0.13)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), normsinv( 0.13 ) );

        oParser = new parserFormula( "NORMSINV(0.6782136)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), normsinv( 0.6782136 ) );

        oParser = new parserFormula( "NORMSINV(1.6782136)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), normsinv( 1.6782136 ) );

        oParser = new parserFormula( "NORMSINV(-1.6782136)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), normsinv( -1.6782136 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3252
    } );
3253 3254 3255 3256 3257 3258 3259

    test( "Test: \"LOGINV\"", function () {

        function loginv( x, mue, sigma ) {
            if ( sigma <= 0 || x <= 0 || x >= 1 )
                return "#NUM!";
            else
3260
                return toFixed( Math.exp( mue + sigma * ( AscCommonExcel.gaussinv( x ) ) ) );
3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277
        }

        oParser = new parserFormula( "LOGINV(0.039084,3.5,1.2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), loginv( 0.039084, 3.5, 1.2 ) );

        oParser = new parserFormula( "LOGINV(0,3.5,1.2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), loginv( 0, 3.5, 1.2 ) );

        oParser = new parserFormula( "LOGINV(0,3.5,1.2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), loginv( 10, 3.5, 1.2 ) );

        oParser = new parserFormula( "LOGINV(0,3.5,1.2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), loginv( -10, 3.5, 1.2 ) );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
3278
    } );
3279 3280 3281 3282 3283 3284 3285

    test( "Test: \"NORMINV\"", function () {

        function norminv( x, mue, sigma ) {
            if ( sigma <= 0.0 || x <= 0.0 || x >= 1.0 )
                return "#NUM!";
            else
3286
                return toFixed( AscCommonExcel.gaussinv( x ) * sigma + mue );
3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304
        }

        oParser = new parserFormula( "NORMINV(0.954,40,1.5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), norminv( 0.954, 40, 1.5 ) );

        oParser = new parserFormula( "NORMINV(0.13,100,0.5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), norminv( 0.13, 100, 0.5 ) );

        oParser = new parserFormula( "NORMINV(0.6782136,6,0.005)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), norminv( 0.6782136, 6, 0.005 ) );

        oParser = new parserFormula( "NORMINV(-1.6782136,7,0)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), norminv( -1.6782136, 7, 0 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3305
    } );
3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342

    test( "Test: \"PEARSON\"", function () {

        function pearson( x, y ) {

            var sumXDeltaYDelta = 0, sqrXDelta = 0, sqrYDelta = 0, _x = 0, _y = 0, xLength = 0;

            if ( x.length != y.length )
                return "#N/A"
            for ( var i = 0; i < x.length; i++ ) {

                _x += x[i]
                _y += y[i]
                xLength++;
            }

            _x /= xLength;
            _y /= xLength;

            for ( var i = 0; i < x.length; i++ ) {

                sumXDeltaYDelta += (x[i] - _x) * (y[i] - _y);
                sqrXDelta += (x[i] - _x) * (x[i] - _x);
                sqrYDelta += (y[i] - _y) * (y[i] - _y);

            }

            if ( sqrXDelta == 0 || sqrYDelta == 0 )
                return "#DIV/0!"
            else
                return toFixed( sumXDeltaYDelta / Math.sqrt( sqrXDelta * sqrYDelta ) );
        }

        oParser = new parserFormula( "PEARSON({9,7,5,3,1},{10,6,1,5,3})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), pearson( [9, 7, 5, 3, 1], [10, 6, 1, 5, 3] ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3343
    } );
3344 3345 3346 3347 3348

    test( "Test: \"PERCENTILE\"", function () {

        function percentile( A, k ) {

3349
            A.sort(fSortAscending)
3350 3351 3352

            var nSize = A.length;
            if ( A.length < 1 || nSize == 0 )
3353
                return new AscCommonExcel.cError( AscCommonExcel.cErrorType.not_available ).toString();
3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378
            else {
                if ( nSize == 1 )
                    return toFixed( A[0] );
                else {
                    var nIndex = Math.floor( k * (nSize - 1) );
                    var fDiff = k * (nSize - 1) - Math.floor( k * (nSize - 1) );
                    if ( fDiff == 0.0 )
                        return toFixed( A[nIndex] );
                    else {
                        return toFixed( A[nIndex] +
                            fDiff * (A[nIndex + 1] - A[nIndex]) );
                    }
                }
            }

        }

        oParser = new parserFormula( "PERCENTILE({1,3,2,4},0.3)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), percentile( [1, 3, 2, 4], 0.3 ) );

        oParser = new parserFormula( "PERCENTILE({1,3,2,4},0.75)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), percentile( [1, 3, 2, 4], 0.75 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3379
    } );
3380 3381 3382 3383 3384 3385 3386 3387 3388

    test( "Test: \"PERCENTRANK\"", function () {

        function percentrank( A, x, k ) {

            var tA = A, t, fNum = x;

            if ( !k ) k = 3;

3389
            tA.sort(fSortAscending);
3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434

            var nSize = tA.length;
            if ( tA.length < 1 || nSize == 0 )
                return "#N/A";

            else {
                if ( fNum < tA[0] || fNum > tA[nSize - 1] )
                    return "#N/A";
                else if ( nSize == 1 )
                    return 1
                else {
                    var fRes, nOldCount = 0, fOldVal = tA[0], i;
                    for ( i = 1; i < nSize && tA[i] < fNum; i++ ) {
                        if ( tA[i] != fOldVal ) {
                            nOldCount = i;
                            fOldVal = tA[i];
                        }
                    }
                    if ( tA[i] != fOldVal )
                        nOldCount = i;
                    if ( fNum == tA[i] )
                        fRes = nOldCount / (nSize - 1);
                    else {
                        if ( nOldCount == 0 ) {
                            fRes = 0.0;
                        }
                        else {
                            var fFract = ( fNum - tA[nOldCount - 1] ) /
                                ( tA[nOldCount] - tA[nOldCount - 1] );
                            fRes = ( nOldCount - 1 + fFract ) / (nSize - 1);
                        }
                    }
                    return fRes.toString().substr( 0, fRes.toString().indexOf( "." ) + 1 + k ) - 0;
                }
            }
        }

        oParser = new parserFormula( "PERCENTRANK({12,6,7,9,3,8},4)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), percentrank( [12, 6, 7, 9, 3, 8], 4 ) );

        oParser = new parserFormula( "PERCENTRANK({12,6,7,9,3,8},5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), percentrank( [12, 6, 7, 9, 3, 8], 5 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3435
    } );
3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478

    test( "Test: \"POISSON\"", function () {

        function poisson( x, l, cumulativeFlag ) {
            var _x = parseInt( x ), _l = l, f = cumulativeFlag;

            if ( f ) {
                var sum = 0;
                for ( var k = 0; k <= x; k++ ) {
                    sum += Math.pow( _l, k ) / Math.fact( k );
                }
                sum *= Math.exp( -_l );
                return toFixed( sum );
            }
            else {
                return toFixed( Math.exp( -_l ) * Math.pow( _l, _x ) / Math.fact( _x ) );
            }

        }

        oParser = new parserFormula( "POISSON(8,2,false)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), poisson( 8, 2, false ) );

        oParser = new parserFormula( "POISSON(8,2,true)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), poisson( 8, 2, true ) );

        oParser = new parserFormula( "POISSON(2.6,5,false)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), poisson( 2, 5, false ) );

        oParser = new parserFormula( "POISSON(2,5.7,true)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), poisson( 2, 5.7, true ) );

        oParser = new parserFormula( "POISSON(-6,5,true)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "POISSON(6,-5,false)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );
Alexander.Trofimov's avatar
Alexander.Trofimov committed
3479
    } );
3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490

    test( "Test: \"PROB\"", function () {

        oParser = new parserFormula( "PROB({0,1,2,3},{0.2,0.3,0.1,0.4},2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 0.1 );

        oParser = new parserFormula( "PROB({0,1,2,3},{0.2,0.3,0.1,0.4},1,4)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 0.8 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3491
    } );
3492 3493 3494 3495 3496 3497 3498

    test( "Test: \"PROB\"", function () {

        function quartile( A, k ) {

            var fFlag = k;

3499
            A.sort(fSortAscending);
3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574

            var nSize = A.length;
            if ( A.length < 1 || nSize == 0 )
                return "#N/A"
            else {
                if ( nSize == 1 )
                    return toFixed( A[0] );
                else {

                    if ( fFlag < 0.0 || fFlag > 4 )
                        return "#NUM!";
                    else if ( fFlag == 0.0 )
                        return toFixed( A[0] );
                    else if ( fFlag == 1.0 ) {
                        var nIndex = Math.floor( 0.25 * (nSize - 1) ),
                            fDiff = 0.25 * (nSize - 1) - Math.floor( 0.25 * (nSize - 1) );
                        if ( fDiff == 0.0 )
                            return toFixed( A[nIndex] );
                        else {
                            return toFixed( A[nIndex] +
                                fDiff * (A[nIndex + 1] - A[nIndex]) );
                        }
                    }
                    else if ( fFlag == 2.0 ) {
                        if ( nSize % 2 == 0 )
                            return toFixed( (A[nSize / 2 - 1] + A[nSize / 2]) / 2.0 );
                        else
                            return toFixed( A[(nSize - 1) / 2] );
                    }
                    else if ( fFlag == 3.0 ) {
                        var nIndex = Math.floor( 0.75 * (nSize - 1) ),
                            fDiff = 0.75 * (nSize - 1) - Math.floor( 0.75 * (nSize - 1) );
                        if ( fDiff == 0.0 )
                            return toFixed( A[nIndex] );
                        else {
                            return toFixed( A[nIndex] +
                                fDiff * (A[nIndex + 1] - A[nIndex]) );
                        }
                    }
                    else
                        return toFixed( A[nSize - 1] );

                }
            }

        }

        oParser = new parserFormula( "QUARTILE({1,2,4,7,8,9,10,12},-1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), quartile( [1, 2, 4, 7, 8, 9, 10, 12], -1 ) );

        oParser = new parserFormula( "QUARTILE({1,2,4,7,8,9,10,12},0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), quartile( [1, 2, 4, 7, 8, 9, 10, 12], 0 ) );

        oParser = new parserFormula( "QUARTILE({1,2,4,7,8,9,10,12},1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), quartile( [1, 2, 4, 7, 8, 9, 10, 12], 1 ) );

        oParser = new parserFormula( "QUARTILE({1,2,4,7,8,9,10,12},2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), quartile( [1, 2, 4, 7, 8, 9, 10, 12], 2 ) );

        oParser = new parserFormula( "QUARTILE({1,2,4,7,8,9,10,12},3)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), quartile( [1, 2, 4, 7, 8, 9, 10, 12], 3 ) );

        oParser = new parserFormula( "QUARTILE({1,2,4,7,8,9,10,12},4)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), quartile( [1, 2, 4, 7, 8, 9, 10, 12], 4 ) );

        oParser = new parserFormula( "QUARTILE({1,2,4,7,8,9,10,12},5)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), quartile( [1, 2, 4, 7, 8, 9, 10, 12], 5 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3575
    } );
3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616

    test( "Test: \"RSQ\"", function () {

        function rsq( x, y ) {

            var sumXDeltaYDelta = 0, sqrXDelta = 0, sqrYDelta = 0, _x = 0, _y = 0, xLength = 0;

            if ( x.length != y.length )
                return "#N/A"
            for ( var i = 0; i < x.length; i++ ) {

                _x += x[i]
                _y += y[i]
                xLength++;
            }

            _x /= xLength;
            _y /= xLength;

            for ( var i = 0; i < x.length; i++ ) {

                sumXDeltaYDelta += (x[i] - _x) * (y[i] - _y);
                sqrXDelta += (x[i] - _x) * (x[i] - _x);
                sqrYDelta += (y[i] - _y) * (y[i] - _y);

            }

            if ( sqrXDelta == 0 || sqrYDelta == 0 )
                return "#DIV/0!"
            else
                return toFixed( Math.pow( sumXDeltaYDelta / Math.sqrt( sqrXDelta * sqrYDelta ), 2 ) );
        }

        oParser = new parserFormula( "RSQ({9,7,5,3,1},{10,6,1,5,3})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), rsq( [9, 7, 5, 3, 1], [10, 6, 1, 5, 3] ) );

        oParser = new parserFormula( "RSQ({2,3,9,1,8,7,5},{6,5,11,7,5,4,4})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), rsq( [2, 3, 9, 1, 8, 7, 5], [6, 5, 11, 7, 5, 4, 4] ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3617
    } );
3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661

    test( "Test: \"SKEW\"", function () {

        function skew( x ) {

            var sumSQRDeltaX = 0, _x = 0, xLength = 0, standDev = 0, sumSQRDeltaXDivstandDev = 0;
            for ( var i = 0; i < x.length; i++ ) {

                _x += x[i];
                xLength++;

            }

            if ( xLength <= 2 )
                return "#N/A"

            _x /= xLength;

            for ( var i = 0; i < x.length; i++ ) {

                sumSQRDeltaX += Math.pow( x[i] - _x, 2 );

            }

            standDev = Math.sqrt( sumSQRDeltaX / ( xLength - 1 ) );

            for ( var i = 0; i < x.length; i++ ) {

                sumSQRDeltaXDivstandDev += Math.pow( (x[i] - _x) / standDev, 3 );

            }

            return toFixed( xLength / (xLength - 1) / (xLength - 2) * sumSQRDeltaXDivstandDev )

        }

        oParser = new parserFormula( "SKEW(3,4,5,2,3,4,5,6,4,7)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), skew( [3, 4, 5, 2, 3, 4, 5, 6, 4, 7] ) );

        oParser = new parserFormula( "SKEW({2,3,9,1,8,7,5},{6,5,11,7,5,4,4})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), skew( [2, 3, 9, 1, 8, 7, 5, 6, 5, 11, 7, 5, 4, 4] ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3662
    } );
3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673

    test( "Test: \"SMALL\"", function () {

        oParser = new parserFormula( "SMALL({3,5,3,5,4;4,2,4,6,7},3)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3 );

        oParser = new parserFormula( "SMALL({3,5,3,5,4;4,2,4,6,7},7)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 5 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3674
    } );
3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714

    test( "Test: \"SLOPE\"", function () {

        function slope( y, x ) {

            var sumXDeltaYDelta = 0, sqrXDelta = 0, _x = 0, _y = 0, xLength = 0;

            if ( x.length != y.length )
                return "#N/A"
            for ( var i = 0; i < x.length; i++ ) {

                _x += x[i]
                _y += y[i]
                xLength++;
            }

            _x /= xLength;
            _y /= xLength;

            for ( var i = 0; i < x.length; i++ ) {

                sumXDeltaYDelta += (x[i] - _x) * (y[i] - _y);
                sqrXDelta += (x[i] - _x) * (x[i] - _x);

            }

            if ( sqrXDelta == 0 )
                return "#DIV/0!"
            else
                return toFixed( sumXDeltaYDelta / sqrXDelta );
        }

        oParser = new parserFormula( "SLOPE({9,7,5,3,1},{10,6,1,5,3})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), slope( [9, 7, 5, 3, 1], [10, 6, 1, 5, 3] ) );

        oParser = new parserFormula( "SLOPE({2,3,9,1,8,7,5},{6,5,11,7,5,4,4})", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), slope( [2, 3, 9, 1, 8, 7, 5], [6, 5, 11, 7, 5, 4, 4] ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3715
    } );
3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738

    test( "Test: \"STANDARDIZE\"", function () {

        function STANDARDIZE( x, mean, sigma ) {

            if ( sigma <= 0 )
                return "#NUM!"
            else
                return toFixed( (x - mean) / sigma );
        }

        oParser = new parserFormula( "STANDARDIZE(42,40,1.5)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), STANDARDIZE( 42, 40, 1.5 ) );

        oParser = new parserFormula( "STANDARDIZE(22,12,2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), STANDARDIZE( 22, 12, 2 ) );

        oParser = new parserFormula( "STANDARDIZE(22,12,-2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), STANDARDIZE( 22, 12, -2 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3739
    } );
3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751

    test( "Test: \"STDEV\"", function () {

        function stdev() {
            var average = 0, res = 0;
            for ( var i = 0; i < arguments.length; i++ ) {
                average += arguments[i];
            }
            average /= arguments.length;
            for ( var i = 0; i < arguments.length; i++ ) {
                res += (arguments[i] - average) * (arguments[i] - average);
            }
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
3752
            return toFixed( Math.sqrt( res / (arguments.length - 1) ) );
3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769
        }

        oParser = new parserFormula( "STDEV(123,134,143,173,112,109)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), stdev( 123, 134, 143, 173, 112, 109 ) );

        ws.getRange2( "E400" ).setValue( "\"123\"" );
        ws.getRange2( "E401" ).setValue( "134" );
        ws.getRange2( "E402" ).setValue( "143" );
        ws.getRange2( "E403" ).setValue( "173" );
        ws.getRange2( "E404" ).setValue( "112" );
        ws.getRange2( "E405" ).setValue( "109" );

        oParser = new parserFormula( "STDEV(E400:E405)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), stdev( 134, 143, 173, 112, 109 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3770
    } );
3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790

    test( "Test: \"STDEVA\"", function () {

        ws.getRange2( "E400" ).setValue( "\"123\"" );
        ws.getRange2( "E401" ).setValue( "134" );
        ws.getRange2( "E402" ).setValue( "143" );
        ws.getRange2( "E403" ).setValue( "173" );
        ws.getRange2( "E404" ).setValue( "112" );
        ws.getRange2( "E405" ).setValue( "109" );


        function stdeva() {
            var average = 0, res = 0;
            for ( var i = 0; i < arguments.length; i++ ) {
                average += arguments[i];
            }
            average /= arguments.length;
            for ( var i = 0; i < arguments.length; i++ ) {
                res += (arguments[i] - average) * (arguments[i] - average);
            }
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
3791
            return toFixed( Math.sqrt( res / (arguments.length - 1) ) );
3792 3793 3794 3795 3796 3797 3798 3799 3800 3801
        }

        oParser = new parserFormula( "STDEVA(123,134,143,173,112,109)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), stdeva( 123, 134, 143, 173, 112, 109 ) );

        oParser = new parserFormula( "STDEVA(123,134,143,173,112,109)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), stdeva( 123, 134, 143, 173, 112, 109 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3802
    } );
3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834

    test( "Test: \"VAR\"", function () {

        function _var( x ) {

            var sumSQRDeltaX = 0, _x = 0, xLength = 0, standDev = 0, sumSQRDeltaXDivstandDev = 0;
            for ( var i = 0; i < x.length; i++ ) {
                _x += x[i];
                xLength++;
            }

            _x /= xLength;

            for ( var i = 0; i < x.length; i++ ) {
                sumSQRDeltaX += Math.pow( x[i] - _x, 2 );
            }

            return toFixed( sumSQRDeltaX / (xLength - 1) )

        }

        oParser = new parserFormula( "VAR(10.5,12.4,19.4,23.2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), _var( [10.5, 12.4, 19.4, 23.2] ) );

        oParser = new parserFormula( "VAR(10.5,{12.4,19.4},23.2)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), _var( [10.5, 12.4, 19.4, 23.2] ) );

        oParser = new parserFormula( "VAR(10.5,12.4,19.4)", "A1", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), _var( [10.5, 12.4, 19.4] ) );
3835

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3836
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868

    /*
    * Lookup and Reference
    */
    test( "Test: \"HLOOKUP\"", function () {

        ws.getRange2( "A401" ).setValue( "Axles" );ws.getRange2( "B401" ).setValue( "Bearings" );ws.getRange2( "C401" ).setValue( "Bolts" );
        ws.getRange2( "A402" ).setValue( "4" );ws.getRange2( "B402" ).setValue( "6" );ws.getRange2( "C402" ).setValue( "9" );
        ws.getRange2( "A403" ).setValue( "5" );ws.getRange2( "B403" ).setValue( "7" );ws.getRange2( "C403" ).setValue( "10" );
        ws.getRange2( "A404" ).setValue( "6" );ws.getRange2( "B404" ).setValue( "8" );ws.getRange2( "C404" ).setValue( "11" );


        oParser = new parserFormula( "HLOOKUP(\"Axles\",A401:C404,2,TRUE)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 4 );

        oParser = new parserFormula( "HLOOKUP(\"Bearings\",A401:C404,3,FALSE)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 7 );

        oParser = new parserFormula( "HLOOKUP(\"B\",A401:C404,3,TRUE)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 5 );

        oParser = new parserFormula( "HLOOKUP(\"Bolts\",A401:C404,4)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 11 );

        oParser = new parserFormula( "HLOOKUP(3,{1,2,3;\"a\",\"b\",\"c\";\"d\",\"e\",\"f\"},2,TRUE)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "c" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3869
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897

    test( "Test: \"VLOOKUP\"", function () {

        ws.getRange2( "A501" ).setValue( "Density" );ws.getRange2( "B501" ).setValue( "Bearings" );ws.getRange2( "C501" ).setValue( "Bolts" );

        ws.getRange2( "A502" ).setValue( "0.457" );ws.getRange2( "B502" ).setValue( "3.55" );ws.getRange2( "C502" ).setValue( "500" );
        ws.getRange2( "A503" ).setValue( "0.525" );ws.getRange2( "B503" ).setValue( "3.25" );ws.getRange2( "C503" ).setValue( "400" );
        ws.getRange2( "A504" ).setValue( "0.616" );ws.getRange2( "B504" ).setValue( "2.93" );ws.getRange2( "C504" ).setValue( "300" );
        ws.getRange2( "A505" ).setValue( "0.675" );ws.getRange2( "B505" ).setValue( "2.75" );ws.getRange2( "C505" ).setValue( "250" );
        ws.getRange2( "A506" ).setValue( "0.746" );ws.getRange2( "B506" ).setValue( "2.57" );ws.getRange2( "C506" ).setValue( "200" );
        ws.getRange2( "A507" ).setValue( "0.835" );ws.getRange2( "B507" ).setValue( "2.38" );ws.getRange2( "C507" ).setValue( "15" );
        ws.getRange2( "A508" ).setValue( "0.946" );ws.getRange2( "B508" ).setValue( "2.17" );ws.getRange2( "C508" ).setValue( "100" );
        ws.getRange2( "A509" ).setValue( "1.09" );ws.getRange2( "B509" ).setValue( "1.95" );ws.getRange2( "C509" ).setValue( "50" );
        ws.getRange2( "A510" ).setValue( "1.29" );ws.getRange2( "B510" ).setValue( "1.71" );ws.getRange2( "C510" ).setValue( "0" );


        oParser = new parserFormula( "VLOOKUP(1,A502:C510,2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2.17 );

        oParser = new parserFormula( "VLOOKUP(1,A502:C510,3,TRUE)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 100.00 );

        oParser = new parserFormula( "VLOOKUP(2,A502:C510,2,TRUE)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1.71 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3898
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
3899

3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919
    test( "Test: \"MATCH\"", function () {

        ws.getRange2( "A551" ).setValue( "28" );
        ws.getRange2( "A552" ).setValue( "29" );
        ws.getRange2( "A553" ).setValue( "31" );
        ws.getRange2( "A554" ).setValue( "45" );
        ws.getRange2( "A555" ).setValue( "89" );

        ws.getRange2( "B551" ).setValue( "89" );
        ws.getRange2( "B552" ).setValue( "45" );
        ws.getRange2( "B553" ).setValue( "31" );
        ws.getRange2( "B554" ).setValue( "29" );
        ws.getRange2( "B555" ).setValue( "28" );

        ws.getRange2( "C551" ).setValue( "89" );
        ws.getRange2( "C552" ).setValue( "45" );
        ws.getRange2( "C553" ).setValue( "31" );
        ws.getRange2( "C554" ).setValue( "29" );
        ws.getRange2( "C555" ).setValue( "28" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3920
        oParser = new parserFormula( "MATCH(30,A551:A555,-1)", "A2", ws );
3921 3922 3923
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#N/A" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3924
        oParser = new parserFormula( "MATCH(30,A551:A555,1)", "A2", ws );
3925 3926 3927
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3928
        oParser = new parserFormula( "MATCH(30,A551:A555,0)", "A2", ws );
3929 3930 3931
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#N/A" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3932
        oParser = new parserFormula( "MATCH(30,B551:B555)", "A2", ws );
3933 3934 3935
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#N/A" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3936
        oParser = new parserFormula( "MATCH(30,B551:B555,-1)", "A2", ws );
3937 3938 3939
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3940
        oParser = new parserFormula( "MATCH(30,B551:B555,0)", "A2", ws );
3941 3942 3943
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#N/A" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3944
        oParser = new parserFormula( "MATCH(31,C551:C555,0)", "A2", ws );
3945 3946 3947 3948 3949 3950 3951
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 3 );

        oParser = new parserFormula( "MATCH(\"b\",{\"a\";\"b\";\"c\"},0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 2 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
3952
    } );
3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983

    test( "Test: \"INDEX\"", function () {

        ws.getRange2( "A651" ).setValue( "1" );
        ws.getRange2( "A652" ).setValue( "2" );
        ws.getRange2( "A653" ).setValue( "3" );
        ws.getRange2( "A654" ).setValue( "4" );
        ws.getRange2( "A655" ).setValue( "5" );

        ws.getRange2( "B651" ).setValue( "6" );
        ws.getRange2( "B652" ).setValue( "7" );
        ws.getRange2( "B653" ).setValue( "8" );
        ws.getRange2( "B654" ).setValue( "9" );
        ws.getRange2( "B655" ).setValue( "10" );

        ws.getRange2( "C651" ).setValue( "11" );
        ws.getRange2( "C652" ).setValue( "12" );
        ws.getRange2( "C653" ).setValue( "13" );
        ws.getRange2( "C654" ).setValue( "14" );
        ws.getRange2( "C655" ).setValue( "15" );

        oParser = new parserFormula( "INDEX({\"Apples\",\"Lemons\";\"Bananas\",\"Pears\"},2,2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "Pears" );

        oParser = new parserFormula( "INDEX({\"Apples\",\"Lemons\";\"Bananas\",\"Pears\"},1,2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "Lemons" );

        oParser = new parserFormula( "INDEX(\"Apples\",2,2)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
fix:  
Dmitry.Shahtanov committed
3984
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );
3985 3986 3987 3988 3989 3990 3991

        oParser = new parserFormula( "INDEX({\"Apples\",\"Lemons\"},,2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "Lemons" );

        oParser = new parserFormula( "INDEX(A651:C655,,2)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
3992
        strictEqual( oParser.calculate().getValue().getValue(), 6 );
3993 3994 3995

        oParser = new parserFormula( "INDEX(A651:C655,3,2)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
3996
        strictEqual( oParser.calculate().getValue().getValue(), 8 );
3997 3998 3999 4000 4001

        oParser = new parserFormula( "INDEX(A651:C655,10,2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#REF!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4002
    } );
4003

4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025
    test( "Test: \"OFFSET\"", function () {

        ws.getRange2( "C150" ).setValue( "1" );
        ws.getRange2( "D150" ).setValue( "2" );
        ws.getRange2( "E150" ).setValue( "3" );

        ws.getRange2( "C151" ).setValue( "2" );
        ws.getRange2( "D151" ).setValue( "3" );
        ws.getRange2( "E151" ).setValue( "4" );

        ws.getRange2( "C152" ).setValue( "3" );
        ws.getRange2( "D152" ).setValue( "4" );
        ws.getRange2( "E152" ).setValue( "5" );

        oParser = new parserFormula( "OFFSET(C3,2,3,1,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().toString(), "F5" );

        oParser = new parserFormula( "SUM(OFFSET(C151:E155,-1,0,3,3))", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 27 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4026
        oParser = new parserFormula( "OFFSET(B3, -2, 0, 1, 1)", "A2", ws );
4027 4028 4029
        ok( oParser.parse() );
        strictEqual( oParser.calculate().toString(), "B1" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4030
    } );
4031

4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069
    /*
    * Financial
    */
    test( "Test: \"FV\"", function () {

        function fv( rate, nper, pmt, pv, type ) {
            var res;
            if ( type === undefined || type === null )
                type = 0;

            if ( pv === undefined || pv === null )
                pv = 0;

            if ( rate != 0 ) {
                res = -1 * ( pv * Math.pow( 1 + rate, nper ) + pmt * ( 1 + rate * type ) * ( Math.pow( 1 + rate, nper ) - 1) / rate );
            }
            else {
                res = -1 * ( pv + pmt * nper );
            }
            return res;
        }

        oParser = new parserFormula( "FV(0.06/12,10,-200,-500,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), fv( 0.06 / 12, 10, -200, -500, 1 ) );

        oParser = new parserFormula( "FV(0.12/12,12,-1000)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), fv( 0.12 / 12, 12, -1000 ) );

        oParser = new parserFormula( "FV(0.11/12,35,-2000,,1)", "A2", ws );
        ok( oParser.parse() );
        ok( Math.abs( oParser.calculate().getValue() - fv( 0.11 / 12, 35, -2000, null, 1 ) ) < dif );

        oParser = new parserFormula( "FV(0.06/12,12,-100,-1000,1)", "A2", ws );
        ok( oParser.parse() );
        ok( Math.abs( oParser.calculate().getValue() - fv( 0.06 / 12, 12, -100, -1000, 1 ) ) < dif );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4070
    } );
4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099

    test( "Test: \"PMT\"", function () {

        function pmt( rate, nper, pv, fv, type ) {
            var res;
            if ( type === undefined || type === null )
                type = 0;

            if ( fv === undefined || fv === null )
                fv = 0;

            if ( rate != 0 ) {
                res = -1 * ( pv * Math.pow( 1 + rate, nper ) + fv ) /
                    ( ( 1 + rate * type ) * ( Math.pow( 1 + rate, nper ) - 1 ) / rate );
            }
            else {
                res = -1 * ( pv + fv ) / nper;
            }
            return res;
        }

        oParser = new parserFormula( "PMT(0.08/12,10,10000)", "A2", ws );
        ok( oParser.parse() );
        ok( Math.abs( oParser.calculate().getValue() - pmt( 0.08 / 12, 10, 10000 ) ) < dif );

        oParser = new parserFormula( "PMT(0.08/12,10,10000,0,1)", "A2", ws );
        ok( oParser.parse() );
        ok( Math.abs( oParser.calculate().getValue() - pmt( 0.08 / 12, 10, 10000, 0, 1 ) ) < dif );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4100
    } );
4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139

    test( "Test: \"NPER\"", function () {

        function nper(rate,pmt,pv,fv,type){

            if ( rate === undefined || rate === null )
                rate = 0;

            if ( pmt === undefined || pmt === null )
                pmt = 0;

            if ( pv === undefined || pv === null )
                pv = 0;

            if ( type === undefined || type === null )
                type = 0;

            if ( fv === undefined || fv === null )
                fv = 0;

            var res;
            if ( rate != 0 ) {
                res = (-fv * rate + pmt * (1 + rate * type)) / (rate * pv + pmt * (1 + rate * type))
                res = Math.log( res ) / Math.log( 1+rate )
            }
            else {
                res = (- pv - fv )/ pmt ;
            }
            return res;
        }

        oParser = new parserFormula( "NPER(0.12/12,-100,-1000,10000,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), nper(0.12/12,-100,-1000,10000,1) );

        oParser = new parserFormula( "NPER(0.12/12,-100,-1000)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), nper(0.12/12,-100,-1000) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4140
    } );
4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161

    test( "Test: \"PV\"", function () {

        function pv( rate, nper, pmt, fv, type ) {
            if ( rate != 0 ) {
                return -1 * ( fv + pmt * (1 + rate * type) * ( (Math.pow( (1 + rate), nper ) - 1) / rate ) ) / Math.pow( 1 + rate, nper )
            }
            else {
                return -1 * ( fv + pmt * nper );
            }

        }

        oParser = new parserFormula( "PV(0.08/12,12*20,500,,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), pv( 0.08 / 12, 12 * 20, 500, 0, 0 ) );

        oParser = new parserFormula( "PV(0,12*20,500,,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), pv( 0, 12 * 20, 500, 0, 0 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4162
    } );
4163 4164 4165 4166 4167 4168 4169

    test( "Test: \"NPV\"", function () {

        oParser = new parserFormula( "NPV(0.1,-10000,3000,4200,6800)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1188.4434123352216 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4170
    } );
4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197

    test( "Test: \"EFFECT\"", function () {

        function effect(nr,np){

            if( nr <= 0 || np < 1 ) return "#NUM!"

            return Math.pow( ( 1 + nr/np ), np ) - 1;

        }

        oParser = new parserFormula( "EFFECT(0.0525,4)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), effect(0.0525,4) );

        oParser = new parserFormula( "EFFECT(0.0525,-4)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), effect(0.0525,-4) );

        oParser = new parserFormula( "EFFECT(0.0525,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), effect(0.0525,1) );

        oParser = new parserFormula( "EFFECT(-1,54)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), effect(-1,54) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4198
    } );
4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215

    test( "Test: \"ISPMT\"", function () {

        function ISPMT( rate, per, nper, pv ){

            return pv * rate * (per / nper - 1.0)

        }

        oParser = new parserFormula( "ISPMT(0.1/12,1,3*12,8000000)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), ISPMT(0.1/12,1,3*12,8000000) );

        oParser = new parserFormula( "ISPMT(0.1,1,3,8000000)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), ISPMT(0.1,1,3,8000000) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4216
    } );
4217 4218 4219 4220 4221 4222

    test( "Test: \"XNPV\"", function () {

        function xnpv( rate, valueArray, dateArray ){
            var res = 0, r = rate;

4223
            var d1 = dateArray[0];
4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248

            for( var i = 0; i < dateArray.length; i++ ){

                res += valueArray[i] / ( Math.pow( ( 1 + r ), ( dateArray[i] - d1 ) / 365 ) )
            }

            return res;
        }

        ws.getRange2( "A701" ).setValue( "39448" );
        ws.getRange2( "A702" ).setValue( "39508" );
        ws.getRange2( "A703" ).setValue( "39751" );
        ws.getRange2( "A704" ).setValue( "39859" );
        ws.getRange2( "A705" ).setValue( "39904" );

        oParser = new parserFormula( "XNPV(0.09,{-10000,2750,4250,3250,2750},A701:A705)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), xnpv( 0.09, [-10000,2750,4250,3250,2750], [39448,39508,39751,39859,39904] ) );

        ws.getRange2( "A705" ).setValue( "43191" );

        oParser = new parserFormula( "XNPV(0.09,{-10000,2750,4250,3250,2750},A701:A705)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), xnpv( 0.09, [-10000,2750,4250,3250,2750], [39448,39508,39751,39859,43191] ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4249
    } );
4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295

    test( "Test: \"IRR\"", function () {

        function irr( costArr, x ){

            if (!x) x = 0.1

            var nC = 0, g_Eps = 1e-7, fEps = 1.0, fZ = 0, fN = 0, xN = 0, nIM = 100, nMC = 0,arr0 = costArr[0], arrI, wasNegative = false, wasPositive = false;

            if( arr0 < 0 )
                wasNegative = true;
            else if( arr0 > 0 )
                wasPositive = true;

            while(fEps > g_Eps && nMC < nIM ){
                nC = 0; fZ = 0; fN = 0;
                fZ += costArr[0]/Math.pow( 1.0 + x, nC );
                fN += -nC * costArr[0]/Math.pow( 1 + x, nC + 1 );
                nC++;
                for(var i = 1; i < costArr.length; i++){
                    arrI = costArr[i];
                    fZ += arrI/Math.pow( 1.0 + x, nC );
                    fN += -nC * arrI/Math.pow( 1 + x, nC + 1 );
                    if( arrI < 0 )
                        wasNegative = true;
                    else if( arrI > 0 )
                        wasPositive = true
                    nC++
                }
                xN = x - fZ / fN;
                nMC ++;
                fEps = Math.abs( xN - x );
                x = xN;
            }


            if( !(wasNegative && wasPositive)  )
                return "#NUM!";

            if (fEps < g_Eps)
                return x;
            else
                return "#NUM!";

        }

4296

4297 4298
        oParser = new parserFormula( "IRR({-70000,12000,15000,18000,21000})", "A2", ws );
        ok( oParser.parse() );
4299
        strictEqual( oParser.calculate().getValue(), -0.021244848273410923 );
4300 4301 4302 4303 4304

        ws.getRange2( "A705" ).setValue( "43191" );

        oParser = new parserFormula( "IRR({-70000,12000,15000,18000,21000,26000})", "A2", ws );
        ok( oParser.parse() );
4305
        strictEqual( oParser.calculate().getValue(), 0.08663094803653171 );
4306 4307 4308

        oParser = new parserFormula( "IRR({-70000,12000,15000},-0.1)", "A2", ws );
        ok( oParser.parse() );
4309
        strictEqual( oParser.calculate().getValue(), -0.44350694133450463 );
4310 4311 4312

        oParser = new parserFormula( "IRR({-70000},-0.1)", "A2", ws );
        ok( oParser.parse() );
4313
        strictEqual( oParser.calculate().getValue(), "#NUM!" );
4314

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4315
    } );
4316 4317 4318 4319 4320 4321 4322 4323 4324

    test( "Test: \"ACCRINT\"", function () {

        oParser = new parserFormula( "ACCRINT(DATE(2006,3,1),DATE(2006,9,1),DATE(2006,5,1),0.1,1100,2,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 18.333333333333332 );

        oParser = new parserFormula( "ACCRINT(DATE(2006,3,1),DATE(2006,9,1),DATE(2006,5,1),0.1,,2,0)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4325
        strictEqual( oParser.calculate().getValue(), 16.666666666666664 );
4326

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338
        oParser = new parserFormula( "ACCRINT(DATE(2008,3,1),DATE(2008,8,31),DATE(2010,5,1),0.1,1000,2,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 216.94444444444444 );

        oParser = new parserFormula( "ACCRINT(DATE(2008,3,1),DATE(2008,8,31),DATE(2010,5,1),0.1,1000,2,0,TRUE)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 216.94444444444444 );

        oParser = new parserFormula( "ACCRINT(DATE(2008,3,1),DATE(2008,8,31),DATE(2010,5,1),0.1,1000,2,0,FALSE)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 216.66666666666666 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4339
    } );
4340 4341 4342 4343 4344 4345 4346 4347 4348

    test( "Test: \"ACCRINTM\"", function () {

        oParser = new parserFormula( "ACCRINTM(DATE(2006,3,1),DATE(2006,5,1),0.1,1100,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 18.333333333333332 );

        oParser = new parserFormula( "ACCRINTM(DATE(2006,3,1),DATE(2006,5,1),0.1,,0)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4349
        strictEqual( oParser.calculate().getValue(), 16.666666666666664 )
4350 4351 4352

        oParser = new parserFormula( "ACCRINTM(DATE(2006,3,1),DATE(2006,5,1),0.1,)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4353
        strictEqual( oParser.calculate().getValue(), 16.666666666666664 );
4354

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4355
    } );
4356 4357 4358 4359 4360 4361 4362

    test( "Test: \"AMORDEGRC\"", function () {

        oParser = new parserFormula( "AMORDEGRC(2400,DATE(2008,8,19),DATE(2008,12,31),300,1,0.15,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 776 );

Dmitry.Shahtanov's avatar
fix:  
Dmitry.Shahtanov committed
4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374
        oParser = new parserFormula( "AMORDEGRC(2400,DATE(2008,8,19),DATE(2008,12,31),300,1,0.50,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "AMORDEGRC(2400,DATE(2008,8,19),DATE(2008,12,31),300,1,0.20,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 819 );

        oParser = new parserFormula( "AMORDEGRC(2400,DATE(2008,8,19),DATE(2008,12,31),300,1,0.33,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 972 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4375
    } );
4376 4377 4378 4379 4380 4381 4382

    test( "Test: \"AMORLINC\"", function () {

        oParser = new parserFormula( "AMORLINC(2400,DATE(2008,8,19),DATE(2008,12,31),300,1,0.15,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 360 );

4383 4384 4385 4386
        oParser = new parserFormula( "AMORLINC(2400,DATE(2008,8,19),DATE(2008,12,31),300,1,0.70,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1484 );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4387
    } );
4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398

    test( "Test: \"CUMIPMT\"", function () {

        function cumipmt(fRate, nNumPeriods, fVal, nStartPer, nEndPer, nPayType){

            var fRmz, fZinsZ;

            if( nStartPer < 1 || nEndPer < nStartPer || fRate <= 0.0 || nEndPer > nNumPeriods  || nNumPeriods <= 0 ||
                fVal <= 0.0 || ( nPayType != 0 && nPayType != 1 ) )
                return "#NUM!"

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4399
            fRmz = _getPMT( fRate, nNumPeriods, fVal, 0.0, nPayType );
4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413

            fZinsZ = 0.0;

            if( nStartPer == 1 )
            {
                if( nPayType <= 0 )
                    fZinsZ = -fVal;

                nStartPer++;
            }

            for( var i = nStartPer ; i <= nEndPer ; i++ )
            {
                if( nPayType > 0 )
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4414
                    fZinsZ += _getFV( fRate, i - 2, fRmz, fVal, 1 ) - fRmz;
4415
                else
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4416
                    fZinsZ += _getFV( fRate, i - 1, fRmz, fVal, 0 );
4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432
            }

            fZinsZ *= fRate;

            return fZinsZ;

        }

        oParser = new parserFormula( "CUMIPMT(0.09/12,30*12,125000,1,1,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), cumipmt(0.09/12,30*12,125000,1,1,0) );

        oParser = new parserFormula( "CUMIPMT(0.09/12,30*12,125000,13,24,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), cumipmt(0.09/12,30*12,125000,13,24,0) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4433
    } );
4434

4435 4436 4437 4438 4439 4440 4441 4442 4443
    test( "Test: \"CUMPRINC\"", function () {

        function cumpring(fRate, nNumPeriods, fVal, nStartPer, nEndPer, nPayType){

            var fRmz, fKapZ;

            if( nStartPer < 1 || nEndPer < nStartPer || nEndPer < 1  || fRate <= 0 || nNumPeriods <= 0 || fVal <= 0 || ( nPayType != 0 && nPayType != 1 ) )
                return "#NUM!"

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4444
            fRmz = _getPMT( fRate, nNumPeriods, fVal, 0.0, nPayType );
4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463

            fKapZ = 0.0;

            var nStart = nStartPer;
            var nEnd = nEndPer;

            if( nStart == 1 )
            {
                if( nPayType <= 0 )
                    fKapZ = fRmz + fVal * fRate;
                else
                    fKapZ = fRmz;

                nStart++;
            }

            for( var i = nStart ; i <= nEnd ; i++ )
            {
                if( nPayType > 0 )
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4464
                    fKapZ += fRmz - ( _getFV( fRate, i - 2, fRmz, fVal, 1 ) - fRmz ) * fRate;
4465
                else
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4466
                    fKapZ += fRmz - _getFV( fRate, i - 1, fRmz, fVal, 0 ) * fRate;
4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484
            }

            return fKapZ

        }

        oParser = new parserFormula( "CUMPRINC(0.09/12,30*12,125000,1,1,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), cumpring(0.09/12,30*12,125000,1,1,0) );

        oParser = new parserFormula( "CUMPRINC(0.09/12,30*12,-125000,1,1,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), cumpring(0.09/12,30*12,-125000,1,1,0) );

        oParser = new parserFormula( "CUMPRINC(0.09/12,30*12,125000,13,24,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), cumpring(0.09/12,30*12,125000,13,24,0) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4485
    } );
4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505

    test( "Test: \"NOMINAL\"", function () {

        function nominal(rate,np){

            if( rate <= 0 || np < 1 )
                return "#NUM!"

            return ( Math.pow( rate + 1, 1 / np ) - 1 ) * np;

        }

        oParser = new parserFormula( "NOMINAL(0.053543,4)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), nominal(0.053543,4) );

        oParser = new parserFormula( "NOMINAL(0.053543,-4)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), nominal(0.053543,-4) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4506
    } );
4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523

    test( "Test: \"FVSCHEDULE\"", function () {

        function fvschedule(rate,shedList){

            for( var i = 0; i < shedList.length; i++){
                rate *= 1 + shedList[i]
            }

            return rate;

        }

        oParser = new parserFormula( "FVSCHEDULE(1,{0.09,0.11,0.1})", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), fvschedule(1,[0.09,0.11,0.1]) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4524
    } );
4525 4526 4527 4528 4529 4530 4531 4532

    test( "Test: \"DISC\"", function () {

        function disc( settlement, maturity, pr, redemption, basis ){

            if( settlement >= maturity || pr <= 0 || redemption <= 0 || basis < 0 || basis > 4 )
                return "#NUM!"

4533
            return ( 1.0 - pr / redemption ) / AscCommonExcel.yearFrac( settlement, maturity, basis );
4534 4535 4536 4537 4538 4539 4540

        }

        oParser = new parserFormula( "DISC(DATE(2007,1,25),DATE(2007,6,15),97.975,100,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), disc( new Date(2007,0,25),new Date(2007,5,15),97.975,100,1 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4541
    } );
4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571

    test( "Test: \"DOLLARDE\"", function () {

        function dollarde( fractionalDollar, fraction ){

            if( fraction < 0 )
                return "#NUM!";
            else if( fraction == 0 )
                return "#DIV/0!";

            var fInt = Math.floor( fractionalDollar ), res  = fractionalDollar - fInt;

            res /= fraction;

            res *= Math.pow( 10, Math.ceil( Math.log( fraction ) / Math.log( 10 ) ) );

            res += fInt;

            return res;

        }

        oParser = new parserFormula( "DOLLARDE(1.02,16)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), dollarde( 1.02,16 ) );

        oParser = new parserFormula( "DOLLARDE(1.1,32)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), dollarde( 1.1,32 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4572
    } );
4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602

    test( "Test: \"DOLLARFR\"", function () {

        function dollarde( fractionalDollar, fraction ){

            if( fraction < 0 )
                return "#NUM!";
            else if( fraction == 0 )
                return "#DIV/0!";

            var fInt = Math.floor( fractionalDollar ), res  = fractionalDollar - fInt;

            res *= fraction;

            res *= Math.pow( 10.0, -Math.ceil( Math.log( fraction ) / Math.log( 10 ) ) );

            res += fInt;

            return res;

        }

        oParser = new parserFormula( "DOLLARFR(1.125,16)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), dollarde( 1.125,16 ) );

        oParser = new parserFormula( "DOLLARFR(1.125,32)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), dollarde( 1.125,32 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4603
    } );
4604 4605 4606 4607 4608 4609 4610 4611

    test( "Test: \"RECEIVED\"", function () {

        function received( settlement, maturity, investment, discount, basis ){

            if( settlement >= maturity || investment <= 0 || discount <= 0 || basis < 0 || basis > 4 )
                return "#NUM!"

4612
            return investment / ( 1 - ( discount * AscCommonExcel.yearFrac( settlement, maturity, basis) ) )
4613 4614 4615 4616 4617 4618 4619

        }

        oParser = new parserFormula( "RECEIVED(DATE(2008,2,15),DATE(2008,5,15),1000000,0.0575,2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), received( new Date(2008,1,15),new Date(2008,4,15),1000000,0.0575,2 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4620
    } );
4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719

    test( "Test: \"RATE\"", function () {

        function RateIteration( fNper, fPayment, fPv, fFv, fPayType, fGuess ) {
            function approxEqual( a, b ) {
                if ( a == b )
                    return true;
                var x = a - b;
                return (x < 0.0 ? -x : x)
                    < ((a < 0.0 ? -a : a) * (1.0 / (16777216.0 * 16777216.0)));
            }

            var bValid = true, bFound = false, fX, fXnew, fTerm, fTermDerivation, fGeoSeries, fGeoSeriesDerivation;
            var nIterationsMax = 150, nCount = 0, fEpsilonSmall = 1.0E-14, SCdEpsilon = 1.0E-7;
            fFv = fFv - fPayment * fPayType;
            fPv = fPv + fPayment * fPayType;
            if ( fNper == Math.round( fNper ) ) {
                fX = fGuess.fGuess;
                var fPowN, fPowNminus1;
                while ( !bFound && nCount < nIterationsMax ) {
                    fPowNminus1 = Math.pow( 1.0 + fX, fNper - 1.0 );
                    fPowN = fPowNminus1 * (1.0 + fX);
                    if ( approxEqual( Math.abs( fX ), 0.0 ) ) {
                        fGeoSeries = fNper;
                        fGeoSeriesDerivation = fNper * (fNper - 1.0) / 2.0;
                    }
                    else {
                        fGeoSeries = (fPowN - 1.0) / fX;
                        fGeoSeriesDerivation = fNper * fPowNminus1 / fX - fGeoSeries / fX;
                    }
                    fTerm = fFv + fPv * fPowN + fPayment * fGeoSeries;
                    fTermDerivation = fPv * fNper * fPowNminus1 + fPayment * fGeoSeriesDerivation;
                    if ( Math.abs( fTerm ) < fEpsilonSmall )
                        bFound = true;
                    else {
                        if ( approxEqual( Math.abs( fTermDerivation ), 0.0 ) )
                            fXnew = fX + 1.1 * SCdEpsilon;
                        else
                            fXnew = fX - fTerm / fTermDerivation;
                        nCount++;
                        bFound = (Math.abs( fXnew - fX ) < SCdEpsilon);
                        fX = fXnew;
                    }
                }
                bValid =(fX >=-1.0);
            }
            else {
                fX = (fGuess.fGuest < -1.0) ? -1.0 : fGuess.fGuest;
                while ( bValid && !bFound && nCount < nIterationsMax ) {
                    if ( approxEqual( Math.abs( fX ), 0.0 ) ) {
                        fGeoSeries = fNper;
                        fGeoSeriesDerivation = fNper * (fNper - 1.0) / 2.0;
                    }
                    else {
                        fGeoSeries = (Math.pow( 1.0 + fX, fNper ) - 1.0) / fX;
                        fGeoSeriesDerivation = fNper * Math.pow( 1.0 + fX, fNper - 1.0 ) / fX - fGeoSeries / fX;
                    }
                    fTerm = fFv + fPv * pow( 1.0 + fX, fNper ) + fPayment * fGeoSeries;
                    fTermDerivation = fPv * fNper * Math.pow( 1.0 + fX, fNper - 1.0 ) + fPayment * fGeoSeriesDerivation;
                    if ( Math.abs( fTerm ) < fEpsilonSmall )
                        bFound = true;
                    else {
                        if ( approxEqual( Math.abs( fTermDerivation ), 0.0 ) )
                            fXnew = fX + 1.1 * SCdEpsilon;
                        else
                            fXnew = fX - fTerm / fTermDerivation;
                        nCount++;
                        bFound = (Math.abs( fXnew - fX ) < SCdEpsilon);
                        fX = fXnew;
                        bValid = (fX >= -1.0);
                    }
                }
            }
            fGuess.fGuess = fX;
            return bValid && bFound;
        }

        function rate(nper, pmt, pv, fv, type, quess){

            if ( fv === undefined ) fv = 0;
            if ( type === undefined ) type = 0;
            if ( quess === undefined ) quess = 0.1;

            var res = {fGuess:0};

            if( RateIteration(nper, pmt, pv, fv, type, res) )
                return res.fGuess;

            return "#VALUE!"
        }

        oParser = new parserFormula( "RATE(4*12,-200,8000)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( difBetween( oParser.calculate().getValue(), rate(4*12,-200,8000) ), true );

        oParser = new parserFormula( "RATE(4*12,-200,8000)*12", "A2", ws );
        ok( oParser.parse() );
        strictEqual( difBetween( oParser.calculate().getValue(), rate(4*12,-200,8000)*12 ), true );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4720
    } );
4721 4722 4723 4724 4725 4726 4727 4728

    test( "Test: \"INTRATE\"", function () {

        function intrate( settlement, maturity, investment, redemption, basis ){

            if( settlement >= maturity || investment <= 0 || redemption <= 0 || basis < 0 || basis > 4 )
                return "#NUM!"

4729
            return ( ( redemption / investment ) - 1 ) / AscCommonExcel.yearFrac( settlement, maturity, basis )
4730 4731 4732 4733 4734 4735 4736

        }

        oParser = new parserFormula( "INTRATE(DATE(2008,2,15),DATE(2008,5,15),1000000,1014420,2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), intrate( new Date(2008,1,15),new Date(2008,4,15),1000000,1014420,2 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4737
    } );
4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748

    test( "Test: \"TBILLEQ\"", function () {

        function tbilleq( settlement, maturity, discount ){

            maturity = Date.prototype.getDateFromExcel(maturity.getExcelDate() + 1)

            var d1 = settlement, d2 = maturity;
            var date1 = d1.getDate(), month1 = d1.getMonth(), year1 = d1.getFullYear(),
                date2 = d2.getDate(), month2 = d2.getMonth(), year2 = d2.getFullYear();

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4749
            var nDiff = GetDiffDate360( date1, month1, year1, date2, month2, year2, true )
4750 4751 4752 4753 4754 4755 4756 4757 4758 4759

            if( settlement >= maturity || discount <= 0 || nDiff > 360 )
                return "#NUM!"

            return ( 365 * discount ) / ( 360 - discount * nDiff );

        }

        oParser = new parserFormula( "TBILLEQ(DATE(2008,3,31),DATE(2008,6,1),0.0914)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4760
        strictEqual( oParser.calculate().getValue(), tbilleq( new Date(Date.UTC(2008,2,31)), new Date(Date.UTC(2008,5,1)), 0.0914 ) );
4761

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4762
    } );
4763 4764 4765 4766 4767 4768 4769 4770 4771 4772

    test( "Test: \"TBILLPRICE\"", function () {

        function tbillprice( settlement, maturity, discount ){

            maturity = Date.prototype.getDateFromExcel(maturity.getExcelDate() + 1)

            var d1 = settlement
            var d2 = maturity

4773
            var fFraction = AscCommonExcel.yearFrac(d1, d2, 0);
4774 4775 4776 4777 4778 4779 4780 4781 4782 4783

            if( fFraction - Math.floor( fFraction ) == 0 )
                return "#NUM!"

            return 100 * ( 1 - discount * fFraction );

        }

        oParser = new parserFormula( "TBILLPRICE(DATE(2008,3,31),DATE(2008,6,1),0.09)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4784
        strictEqual( oParser.calculate().getValue(), tbillprice( new Date(Date.UTC(2008,2,31)), new Date(Date.UTC(2008,5,1)), 0.09 ) );
4785

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4786
    } );
4787 4788 4789 4790 4791 4792 4793 4794 4795 4796

    test( "Test: \"TBILLYIELD\"", function () {

        function tbillyield( settlement, maturity, pr ){

            var d1 = settlement
            var d2 = maturity
            var date1 = d1.getDate(), month1 = d1.getMonth(), year1 = d1.getFullYear(),
                date2 = d2.getDate(), month2 = d2.getMonth(), year2 = d2.getFullYear();

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4797
            var nDiff = GetDiffDate360( date1, month1, year1, date2, month2, year2, true )
4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809
            nDiff++;
            if( settlement >= maturity || pr <= 0 || nDiff > 360 )
                return "#NUM!"

            return ( ( 100 - pr ) / pr) * (360 / nDiff);

        }

        oParser = new parserFormula( "TBILLYIELD(DATE(2008,3,31),DATE(2008,6,1),98.45)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), tbillyield( new Date(2008,2,31), new Date(2008,5,1), 98.45 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4810
    } );
4811

4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823
    test( "Test: \"COUPDAYBS\"", function () {

        function coupdaybs( settlement, maturity, frequency, basis ){

            basis = ( basis !== undefined ? basis : 0 );

            return _getcoupdaybs(settlement, maturity, frequency, basis)

        }

        oParser = new parserFormula( "COUPDAYBS(DATE(2007,1,25),DATE(2008,11,15),2,1)", "A2", ws );
        ok( oParser.parse() );
4824
        strictEqual( oParser.calculate().getValue(), 71 );
4825 4826 4827 4828 4829

        oParser = new parserFormula( "COUPDAYBS(DATE(2007,1,25),DATE(2008,11,15),2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), coupdaybs( new Date(2007,0,25), new Date(2008,10,15), 2 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4830
    } );
4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849

    test( "Test: \"COUPDAYS\"", function () {

        function coupdays( settlement, maturity, frequency, basis ){

            basis = ( basis !== undefined ? basis : 0 );

            return _getcoupdays(settlement, maturity, frequency, basis)

        }

        oParser = new parserFormula( "COUPDAYS(DATE(2007,1,25),DATE(2008,11,15),2,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), coupdays( new Date(2007,0,25), new Date(2008,10,15), 2, 1 ) );

        oParser = new parserFormula( "COUPDAYS(DATE(2007,1,25),DATE(2008,11,15),2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), coupdays( new Date(2007,0,25), new Date(2008,10,15), 2 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4850
    } );
4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869

    test( "Test: \"COUPDAYSNC\"", function () {

        function coupdaysnc( settlement, maturity, frequency, basis ) {

            basis = ( basis !== undefined ? basis : 0 );

            if ( (basis != 0) && (basis != 4) ) {

                _lcl_GetCoupncd( settlement, maturity, frequency );
                return _diffDate( settlement, maturity, basis );
            }

            return _getcoupdays( new Date( settlement ), new Date( maturity ), frequency, basis ) - _getcoupdaybs( new Date( settlement ), new Date( maturity ), frequency, basis );

        }

        oParser = new parserFormula( "COUPDAYSNC(DATE(2007,1,25),DATE(2008,11,15),2,1)", "A2", ws );
        ok( oParser.parse() );
4870
        strictEqual( oParser.calculate().getValue(), 110 );
4871 4872 4873 4874 4875

        oParser = new parserFormula( "COUPDAYSNC(DATE(2007,1,25),DATE(2008,11,15),2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), coupdaysnc( new Date(2007,0,25), new Date(2008,10,15), 2 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4876
    } );
4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891

    test( "Test: \"COUPNCD\"", function () {

        function coupncd( settlement, maturity, frequency, basis ) {

            basis = ( basis !== undefined ? basis : 0 );

            _lcl_GetCoupncd( settlement, maturity, frequency );

            return maturity.getExcelDate();

        }

        oParser = new parserFormula( "COUPNCD(DATE(2007,1,25),DATE(2008,11,15),2,1)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4892
        strictEqual( oParser.calculate().getValue(), coupncd( new Date(Date.UTC(2007,0,25)), new Date(Date.UTC(2008,10,15)), 2, 1 ) );
4893

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4894
    } );
4895 4896 4897 4898 4899

    test( "Test: \"COUPNUM\"", function () {

        oParser = new parserFormula( "COUPNUM(DATE(2007,1,25),DATE(2008,11,15),2,1)", "A2", ws );
        ok( oParser.parse() );
4900
        strictEqual( oParser.calculate().getValue(), _coupnum( new Date(2007,0,25), new Date(2008,10,15), 2, 1 ) );
4901

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4902
    } );
4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916

    test( "Test: \"COUPPCD\"", function () {

        function couppcd( settlement, maturity, frequency, basis ) {

            basis = ( basis !== undefined ? basis : 0 );

            _lcl_GetCouppcd( settlement, maturity, frequency );
            return maturity.getExcelDate();

        }

        oParser = new parserFormula( "COUPPCD(DATE(2007,1,25),DATE(2008,11,15),2,1)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4917
        strictEqual( oParser.calculate().getValue(), couppcd( new Date(Date.UTC(2007,0,25)), new Date(Date.UTC(2008,10,15)), 2, 1 ) );
4918

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4919
    } );
4920 4921 4922 4923 4924

    test( "Test: \"PRICE\"", function () {

        oParser = new parserFormula( "PRICE(DATE(2008,2,15),DATE(2017,11,15),0.0575,0.065,100,2,0)", "A2", ws );
        ok( oParser.parse() );
4925
        strictEqual( oParser.calculate().getValue(), _getprice( new Date( Date.UTC(2008, 1, 15 )), new Date( Date.UTC(2017, 10, 15 )), 0.0575, 0.065, 100, 2, 0 ) );
4926

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4927
    } );
4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938

    test( "Test: \"PRICEDISC\"", function () {

        function pricedisc(settl, matur, discount, redemption, basis){
            return redemption * ( 1.0 - discount * _getdiffdate( settl, matur, basis ) );
        }

        oParser = new parserFormula( "PRICEDISC(DATE(2008,2,16),DATE(2008,3,1),0.0525,100,2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), pricedisc( new Date(2008,1,16), new Date(2008,2,1),0.0525,100,2 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4939
    } );
4940 4941 4942 4943 4944

    test( "Test: \"PRICEMAT\"", function () {

        function pricemat( settl, matur, iss, rate, yld, basis ) {

4945 4946 4947
            var fIssMat = _yearFrac( new Date(iss), new Date(matur), basis );
            var fIssSet = _yearFrac( new Date(iss), new Date(settl), basis );
            var fSetMat = _yearFrac( new Date(settl), new Date(matur), basis );
4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960

            var res = 1.0 + fIssMat * rate;
            res /= 1.0 + fSetMat * yld;
            res -= fIssSet * rate;
            res *= 100.0;

            return res;
        }

        oParser = new parserFormula( "PRICEMAT(DATE(2008,2,15),DATE(2008,4,13),DATE(2007,11,11),0.061,0.061,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), pricemat( new Date(2008,1,15),new Date(2008,3,13),new Date(2007,10,11),0.061,0.061,0 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4961
    } );
4962 4963 4964 4965 4966

    test( "Test: \"YIELD\"", function () {

        oParser = new parserFormula( "YIELD(DATE(2008,2,15),DATE(2016,11,15),0.0575,95.04287,100,2,0)", "A2", ws );
        ok( oParser.parse() );
4967
        strictEqual( oParser.calculate().getValue(), _getYield( new Date(Date.UTC(2008,1,15)), new Date(Date.UTC(2016,10,15)),0.0575,95.04287,100,2,0 ) );
4968

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4969
    } );
4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984

    test( "Test: \"YIELDDISC\"", function () {

        function yielddisc( settlement, maturity, pr, redemption, basis ){

            var fRet = ( redemption / pr ) - 1.0;
            fRet /= _yearFrac( settlement, maturity, basis );
            return fRet;

        }

        oParser = new parserFormula( "YIELDDISC(DATE(2008,2,16),DATE(2008,3,1),99.795,100,2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), yielddisc( new Date( 2008, 1, 16 ), new Date( 2008, 2, 1 ), 99.795, 100, 2 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4985
    } );
4986 4987 4988 4989 4990 4991 4992

    test( "Test: \"YIELDMAT\"", function () {

        oParser = new parserFormula( "YIELDMAT(DATE(2008,3,15),DATE(2008,11,3),DATE(2007,11,8),0.0625,100.0123,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), _getyieldmat( new Date( 2008, 2, 15 ), new Date( 2008, 10, 3 ), new Date( 2007, 10, 8 ), 0.0625, 100.0123, 0 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
4993
    } );
4994

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011
    test( "Test: \"ODDLPRICE\"", function () {

        function oddlprice( settlement, maturity, last_interest, rate, yld, redemption, frequency, basis ){

            var fDCi = _yearFrac( last_interest, maturity, basis ) * frequency;
            var fDSCi = _yearFrac( settlement, maturity, basis ) * frequency;
            var fAi = _yearFrac( last_interest, settlement, basis ) * frequency;

            var res = redemption + fDCi * 100.0 * rate / frequency;
            res /= fDSCi * yld / frequency + 1.0;
            res -= fAi * 100.0 * rate / frequency;

            return res;
        }

        oParser = new parserFormula( "ODDLPRICE(DATE(2008,11,11),DATE(2021,3,1),DATE(2008,10,15),0.0785,0.0625,100,2,1)", "A2", ws );
        ok( oParser.parse() );
5012
        strictEqual( oParser.calculate().getValue(), oddlprice( new Date(Date.UTC(2008,10,11)), new Date(Date.UTC(2021,2,1)), new Date(Date.UTC(2008,9,15)), 0.0785, 0.0625, 100, 2, 1 ) );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5013

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5014
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5015 5016 5017 5018 5019

    test( "Test: \"ODDLYIELD\"", function () {

        function oddlyield( settlement, maturity, last_interest, rate, pr, redemption, frequency, basis ){

5020 5021 5022
            var fDCi = _yearFrac( last_interest, maturity, basis ) * frequency;
            var fDSCi = _yearFrac( settlement, maturity, basis ) * frequency;
            var fAi = _yearFrac( last_interest, settlement, basis ) * frequency;
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035

            var res = redemption + fDCi * 100.0 * rate / frequency;
            res /= pr + fAi * 100.0 * rate / frequency;
            res--;
            res *= frequency / fDSCi;

            return res;
        }

        oParser = new parserFormula( "ODDLYIELD(DATE(2008,11,11),DATE(2021,3,1),DATE(2008,10,15),0.0575,84.5,100,2,0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), oddlyield( new Date(2008,10,11), new Date(2021,2,1), new Date(2008,9,15), 0.0575, 84.5, 100, 2, 0 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5036
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5037

5038 5039 5040 5041
    test( "Test: \"DURATION\"", function () {

        oParser = new parserFormula( "DURATION(DATE(2008,1,1),DATE(2016,1,1),0.08,0.09,2,1)", "A2", ws );
        ok( oParser.parse() );
5042
        strictEqual( oParser.calculate().getValue(), _duration( new Date(Date.UTC(2008,0,1)), new Date(Date.UTC(2016,0,1)), 0.08, 0.09, 2, 1 ) );
5043

5044 5045 5046 5047 5048 5049 5050 5051
        oParser = new parserFormula( "DURATION(DATE(2008,1,1),DATE(2016,1,1),-0.08,0.09,2,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), _duration( new Date(Date.UTC(2008,0,1)), new Date(Date.UTC(2016,0,1)), -0.08, 0.09, 2, 1 ) );

        oParser = new parserFormula( "DURATION(DATE(2008,1,1),DATE(2016,1,1),-0.08,0.09,5,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), _duration( new Date(Date.UTC(2008,0,1)), new Date(Date.UTC(2016,0,1)), -0.08, 0.09, 5, 1 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5052
    } );
5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063

    test( "Test: \"MDURATION\"", function () {

        function mduration(settl, matur, coupon, yld, frequency, basis){

            return _duration( settl, matur, coupon, yld, frequency, basis ) / (1 + yld/frequency);

        }

        oParser = new parserFormula( "MDURATION(DATE(2008,1,1),DATE(2016,1,1),0.08,0.09,2,1)", "A2", ws );
        ok( oParser.parse() );
5064
        strictEqual( oParser.calculate().getValue(), mduration( new Date(Date.UTC(2008,0,1)), new Date(Date.UTC(2016,0,1)), 0.08, 0.09, 2, 1 ) );
5065

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5066
    } );
5067

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079
    test( "Test: \"SYD\"", function () {

        function syd( cost, salvage, life, per ){

            if( life == -1 || life == 0 )
                return "#NUM!";

            var res = 2;
            res *= cost - salvage;
            res *= life+1-per;
            res /= (life+1)*life;

5080
            return res < 0 ? "#NUM!" : res;
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094
        }

        oParser = new parserFormula( "SYD(30000,7500,10,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), syd( 30000,7500,10,1 ) );

        oParser = new parserFormula( "SYD(30000,7500,-1,10)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), syd( 30000,7500,-1,10 ) );

        oParser = new parserFormula( "SYD(30000,7500,-10,10)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), syd( 30000,7500,-10,10 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5095
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5096

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117
    test( "Test: \"PPMT\"", function () {

        function ppmt( rate, per, nper, pv, fv, type ){

            if( fv == undefined ) fv = 0;
            if( type == undefined ) type = 0;

            var fRmz = _getPMT(rate, nper, pv, fv, type);

            return fRmz - _getIPMT(rate, per, pv, type, fRmz);

        }

        oParser = new parserFormula( "PPMT(0.1/12,1,2*12,2000)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), ppmt( 0.1/12,1,2*12,2000 ) );

        oParser = new parserFormula( "PPMT(0.08,10,10,200000)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), ppmt( 0.08,10,10,200000 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5118
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168

    test( "Test: \"MIRR\"", function () {

        function mirr( valueArray, fRate1_invest, fRate1_reinvest ){

            fRate1_invest = fRate1_invest + 1;
            fRate1_reinvest = fRate1_reinvest + 1;

            var fNPV_reinvest = 0, fPow_reinvest = 1, fNPV_invest = 0, fPow_invest = 1, fCellValue,
                wasNegative = false, wasPositive = false;

            for(var i = 0; i < valueArray.length; i++){
                fCellValue = valueArray[i];

                if( fCellValue > 0 ){
                    wasPositive = true;
                    fNPV_reinvest += fCellValue * fPow_reinvest;
                }
                else if( fCellValue < 0 ){
                    wasNegative = true;
                    fNPV_invest += fCellValue * fPow_invest;
                }
                fPow_reinvest /= fRate1_reinvest;
                fPow_invest /= fRate1_invest;

            }

            if( !( wasNegative && wasPositive ) )
                return "#DIV/0!";

            var fResult = -fNPV_reinvest / fNPV_invest;
            fResult *= Math.pow( fRate1_reinvest, valueArray.length - 1 );
            fResult = Math.pow( fResult, 1 / (valueArray.length - 1) );

            return fResult - 1;

        }

        oParser = new parserFormula( "MIRR({-120000,39000,30000,21000,37000,46000},0.1,0.12)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), mirr( [-120000,39000,30000,21000,37000,46000],0.1,0.12 ) );

        oParser = new parserFormula( "MIRR({-120000,39000,30000,21000},0.1,0.12)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), mirr( [-120000,39000,30000,21000],0.1,0.12 ) );

        oParser = new parserFormula( "MIRR({-120000,39000,30000,21000,37000,46000},0.1,0.14)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), mirr( [-120000,39000,30000,21000,37000,46000],0.1,0.14 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5169
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5170 5171 5172 5173 5174 5175 5176 5177

    test( "Test: \"IPMT\"", function () {

        function ipmt( rate, per, nper, pv, fv, type ){

            if( fv == undefined ) fv = 0;
            if( type == undefined ) type = 0;

5178 5179
            var res = AscCommonExcel.getPMT(rate, nper, pv, fv, type);
            res = AscCommonExcel.getIPMT(rate, per, pv, type, res);
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193

            return res;

        }

        oParser = new parserFormula( "IPMT(0.1/12,1*3,3,8000)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), ipmt( 0.1/12,1*3,3,8000 ) );

        oParser = new parserFormula( "IPMT(0.1,3,3,8000)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), ipmt( 0.1,3,3,8000 ) );


Alexander.Trofimov's avatar
Alexander.Trofimov committed
5194
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5195 5196 5197 5198 5199

    test( "Test: \"DB\"", function () {

        function db( cost, salvage, life, period, month ){

5200
            if ( salvage >= cost ) {
5201
                return this.value = new AscCommonExcel.cNumber( 0 );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5202 5203
            }

5204
            if ( month < 1 || month > 12 || salvage < 0 || life <= 0 || period < 0 || life + 1 < period || cost < 0 ) {
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5205 5206 5207
                return "#NUM!";
            }

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258
            var nAbRate = 1 - Math.pow( salvage / cost, 1 / life );
            nAbRate = Math.floor( (nAbRate * 1000) + 0.5 ) / 1000;
            var nErsteAbRate = cost * nAbRate * month / 12;

            var res = 0;
            if ( Math.floor( period ) == 1 )
                res = nErsteAbRate;
            else {
                var nSummAbRate = nErsteAbRate, nMin = life;
                if ( nMin > period ) nMin = period;
                var iMax = Math.floor( nMin );
                for ( var i = 2; i <= iMax; i++ ) {
                    res = (cost - nSummAbRate) * nAbRate;
                    nSummAbRate += res;
                }
                if ( period > life )
                    res = ((cost - nSummAbRate) * nAbRate * (12 - month)) / 12;
            }

            return res

        }

        oParser = new parserFormula( "DB(1000000,100000,6,1,7)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), db(1000000,100000,6,1,7) );

        oParser = new parserFormula( "DB(1000000,100000,6,2,7)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), db(1000000,100000,6,2,7) );

        oParser = new parserFormula( "DB(1000000,100000,6,3,7)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), db(1000000,100000,6,3,7) );

        oParser = new parserFormula( "DB(1000000,100000,6,4,7)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), db(1000000,100000,6,4,7) );

        oParser = new parserFormula( "DB(1000000,100000,6,5,7)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), db(1000000,100000,6,5,7) );

        oParser = new parserFormula( "DB(1000000,100000,6,6,7)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), db(1000000,100000,6,6,7) );

        oParser = new parserFormula( "DB(1000000,100000,6,7,7)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), db(1000000,100000,6,7,7) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5259
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288

    test( "Test: \"DDB\"", function () {

        function ddb( cost, salvage, life, period, factor ){

            if( factor === undefined || factor === null ) factor = 2;
            return _getDDB(cost, salvage, life, period, factor);
        }

        oParser = new parserFormula( "DDB(2400,300,10*365,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), ddb(2400,300,10*365,1) );

        oParser = new parserFormula( "DDB(2400,300,10*12,1,2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), ddb(2400,300,10*12,1,2) );

        oParser = new parserFormula( "DDB(2400,300,10,1,2)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), ddb(2400,300,10,1,2) );

        oParser = new parserFormula( "DDB(2400,300,10,2,1.5)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), ddb(2400,300,10,2,1.5) );

        oParser = new parserFormula( "DDB(2400,300,10,10)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), ddb(2400,300,10,10) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5289
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304

    test( "Test: \"SLN\"", function () {

        function sln( cost, salvage, life ){

            if ( life == 0 ) return "#NUM!";

            return ( cost - salvage ) / life;
        }

        oParser = new parserFormula( "SLN(30000,7500,10)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), sln(30000,7500,10) );


Alexander.Trofimov's avatar
Alexander.Trofimov committed
5305
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333

    test( "Test: \"XIRR\"", function () {
        function lcl_sca_XirrResult( rValues, rDates, fRate ) {
            var D_0 = rDates[0];
            var r = fRate + 1;
            var fResult = rValues[0];
            for ( var i = 1, nCount = rValues.length; i < nCount; ++i )
                fResult += rValues[i] / Math.pow( r, (rDates[i] - D_0) / 365 );
            return fResult;
        }

        function lcl_sca_XirrResult_Deriv1( rValues, rDates, fRate ) {
            var D_0 = rDates[0];
            var r = fRate + 1;
            var fResult = 0;
            for ( var i = 1, nCount = rValues.length; i < nCount; ++i ) {
                var E_i = (rDates[i] - D_0) / 365;
                fResult -= E_i * rValues[i] / Math.pow( r, E_i + 1 );
            }
            return fResult;
        }

        function xirr( valueArray, dateArray, rate ) {

            var res = rate
            if ( res <= -1 )
                return "#NUM!"

5334
            var fMaxEps = 1e-6, maxIter = 100;
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5335

5336
            var newRate, eps, xirrRes, bContLoop;
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5337 5338 5339 5340 5341 5342 5343 5344
            do
             {
                 xirrRes = lcl_sca_XirrResult( valueArray, dateArray, res );
                 newRate = res - xirrRes / lcl_sca_XirrResult_Deriv1( valueArray, dateArray, res );
                 eps = Math.abs( newRate - res );
                 res = newRate;
                 bContLoop = (eps > fMaxEps) && (Math.abs( xirrRes ) > fMaxEps);
             }
5345
             while ( --maxIter && bContLoop );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361

            if ( bContLoop )
                return "#NUM!";

            return res;

        }

        ws.getRange2( "F100" ).setValue( "1/1/2008" );
        ws.getRange2( "G100" ).setValue( "3/1/2008" );
        ws.getRange2( "H100" ).setValue( "10/30/2008" );
        ws.getRange2( "I100" ).setValue( "2/15/2009" );
        ws.getRange2( "J100" ).setValue( "4/1/2009" );

        oParser = new parserFormula( "XIRR({-10000,2750,4250,3250,2750},F100:J100,0.1)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5362
		ok( difBetween( oParser.calculate().getValue(), 0.3733625335188316 ) );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5363

Dmitry.Shahtanov's avatar
fix:  
Dmitry.Shahtanov committed
5364 5365
        ws.getRange2( "F100" ).setValue( 0 );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5366
		ok( difBetween( oParser.calculate().getValue(), 0.0024114950175866895 ) );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5367

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5368
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5369

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5370
    test( "Test: \"VDB\"", function () {
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5371 5372


Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462
        function _getVDB( cost, salvage, life, life1, startperiod, factor){
            var fVdb=0, nLoopEnd = end = Math.ceil(startperiod),
                fTerm, fLia = 0, fRestwert = cost - salvage, bNowLia = false, fGda;

            for ( var i = 1; i <= nLoopEnd; i++){
                if(!bNowLia){

                    fGda = _getDDB(cost, salvage, life, i, factor);
                    fLia = fRestwert/ (life1 - (i-1));

                    if (fLia > fGda){
                        fTerm = fLia;
                        bNowLia = true;
                    }
                    else{
                        fTerm = fGda;
                        fRestwert -= fGda;
                    }

                }
                else{
                    fTerm = fLia;
                }

                if ( i == nLoopEnd)
                    fTerm *= ( startperiod + 1.0 - end );

                fVdb += fTerm;
            }
            return fVdb;
        }

        function vdb( cost, salvage, life, startPeriod, endPeriod, factor, flag ) {

            if( factor === undefined || factor === null ) factor = 2;
            if( flag === undefined || flag === null ) flag = false;

            var start = Math.floor(startPeriod),
                end   = Math.ceil(endPeriod),
                loopStart = start,
                loopEnd   = end;

            var res = 0;
            if ( flag ) {
                for ( var i = loopStart + 1; i <= loopEnd; i++ ) {
                    var ddb = _getDDB( cost, salvage, life, i, factor );

                    if ( i == loopStart + 1 )
                        ddb *= ( Math.min( endPeriod, start + 1 ) - startPeriod );
                    else if ( i == loopEnd )
                        ddb *= ( endPeriod + 1 - end );

                    res += ddb;
                }
            }
            else {

                var life1 = life;

                if ( !Math.approxEqual( startPeriod, Math.floor( startPeriod ) ) ) {
                    if ( factor > 1 ) {
                        if ( startPeriod > life / 2 || Math.approxEqual( startPeriod, life / 2 ) ) {
                            var fPart = startPeriod - life / 2;
                            startPeriod = life / 2;
                            endPeriod -= fPart;
                            life1 += 1;
                        }
                    }
                }

                cost -= _getVDB( cost, salvage, life, life1, startPeriod, factor );
                res = _getVDB( cost, salvage, life, life - startPeriod, endPeriod - startPeriod, factor );
            }

            return res;

        }

        oParser = new parserFormula( "VDB(2400,300,10*365,0,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), vdb(2400,300,10*365,0,1) );

        oParser = new parserFormula( "VDB(2400,300,10*12,0,1)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), vdb(2400,300,10*12,0,1) );

        oParser = new parserFormula( "VDB(2400,300,10*12,6,18)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), vdb(2400,300,10*12,6,18) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5463
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474

    test( "Test: \"ODDFPRICE\"", function () {

        oParser = new parserFormula( "ODDFPRICE(DATE(1999,2,28),DATE(2016,1,1),DATE(1998,2,28),DATE(2015,1,1),7%,0,100,2,1)", "A2", ws );
        ok( oParser.parse() );
        ok( difBetween(oParser.calculate().getValue(), 217.878453038674) );

        oParser = new parserFormula( "ODDFPRICE(DATE(2008,11,11),DATE(2021,3,1),DATE(2008,10,15),DATE(2009,3,1),0.0785,0.0625,100,2,1)", "A2", ws );
        ok( oParser.parse() );
        ok( difBetween(oParser.calculate().getValue(), 113.597717474079) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5475
        oParser = new parserFormula( "ODDFPRICE(DATE(1990,6,1),DATE(1995,12,31),DATE(1990,1,1),DATE(1990,12,31),6%,5%,1000,1,1)", "A2", ws );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5476 5477
        ok( oParser.parse() );
        ok( difBetween(oParser.calculate().getValue(), 790.11323221867) );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5478

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5479
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5480

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5481 5482
    test( "Test: \"ODDFYIELD\"", function () {

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5483
        oParser = new parserFormula( "ODDFYIELD(DATE(1990,6,1),DATE(1995,12,31),DATE(1990,1,1),DATE(1990,12,31),6%,790,100,1,1)", "A2", ws );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5484
        ok( oParser.parse() );
5485
        ok( oParser.assemble() == "ODDFYIELD(DATE(1990,6,1),DATE(1995,12,31),DATE(1990,1,1),DATE(1990,12,31),6%,790,100,1,1)" );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5486 5487 5488 5489
        ok( difBetween(oParser.calculate().getValue(),-0.2889178784774840 ) );

        oParser = new parserFormula( "ODDFYIELD(DATE(2008,11,11),DATE(2021,3,1),DATE(2008,10,15),DATE(2009,3,1),0.0575,84.5,100,2,0)", "A2", ws );
        ok( oParser.parse() );
5490
        ok( oParser.assemble() == "ODDFYIELD(DATE(2008,11,11),DATE(2021,3,1),DATE(2008,10,15),DATE(2009,3,1),0.0575,84.5,100,2,0)" );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5491 5492
        ok( difBetween(oParser.calculate().getValue(), 0.0772455415972989 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5493
        oParser = new parserFormula( "ODDFYIELD(DATE(2008,12,11),DATE(2021,4,1),DATE(2008,10,15),DATE(2009,4,1),6%,100,100,4,1)", "A2", ws );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5494
        ok( oParser.parse() );
5495
        ok( oParser.assemble() == "ODDFYIELD(DATE(2008,12,11),DATE(2021,4,1),DATE(2008,10,15),DATE(2009,4,1),6%,100,100,4,1)" );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5496 5497
        ok( difBetween(oParser.calculate().getValue(), 0.0599769985558904 ) );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5498
    } );
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
5499

5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538
    /*
    * Engineering
    * */

    test( "Test: \"BIN2DEC\"", function () {

        oParser = new parserFormula( "BIN2DEC(101010)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2DEC(101010)" );
        strictEqual( oParser.calculate().getValue(), 42 );

        oParser = new parserFormula( "BIN2DEC(\"101010\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2DEC(\"101010\")" );
        strictEqual( oParser.calculate().getValue(), 42 );

        oParser = new parserFormula( "BIN2DEC(111111111)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2DEC(111111111)" );
        strictEqual( oParser.calculate().getValue(), 511 );

        oParser = new parserFormula( "BIN2DEC(1000000000)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2DEC(1000000000)" );
        strictEqual( oParser.calculate().getValue(), -512 );

        oParser = new parserFormula( "BIN2DEC(1111111111)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2DEC(1111111111)" );
        strictEqual( oParser.calculate().getValue(), -1 );

        oParser = new parserFormula( "BIN2DEC(1234567890)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2DEC(1234567890)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "BIN2DEC(\"Hello World!\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2DEC(\"Hello World!\")" );
5539
        strictEqual( oParser.calculate().getValue(), "#NUM!" );
5540

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5541
    });
5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614

    test( "Test: \"BIN2HEX\"", function () {

        oParser = new parserFormula( "BIN2HEX(101010)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2HEX(101010)" );
        strictEqual( oParser.calculate().getValue(), "2A" );

        oParser = new parserFormula( "BIN2HEX(\"101010\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2HEX(\"101010\")" );
        strictEqual( oParser.calculate().getValue(), "2A" );

        oParser = new parserFormula( "BIN2HEX(111111111)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2HEX(111111111)" );
        strictEqual( oParser.calculate().getValue(), "1FF" );

        oParser = new parserFormula( "BIN2HEX(1000000000)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2HEX(1000000000)" );
        strictEqual( oParser.calculate().getValue(), "FFFFFFFE00" );

        oParser = new parserFormula( "BIN2HEX(1111111111)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2HEX(1111111111)" );
        strictEqual( oParser.calculate().getValue(), "FFFFFFFFFF" );

        oParser = new parserFormula( "BIN2HEX(101010,2)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2HEX(101010,2)" );
        strictEqual( oParser.calculate().getValue(), "2A" );

        oParser = new parserFormula( "BIN2HEX(101010,4)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2HEX(101010,4)" );
        strictEqual( oParser.calculate().getValue(), "002A" );

        oParser = new parserFormula( "BIN2HEX(101010,4.5)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2HEX(101010,4.5)" );
        strictEqual( oParser.calculate().getValue(), "002A" );

        oParser = new parserFormula( "BIN2HEX(1234567890)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2HEX(1234567890)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "BIN2HEX(\"Hello World!\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2HEX(\"Hello World!\")" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "BIN2HEX(101010101010)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2HEX(101010101010)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "BIN2HEX(101010,1)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2HEX(101010,1)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "BIN2HEX(101010,-4)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2HEX(101010,-4)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "BIN2HEX(101010, \"Hello World!\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2HEX(101010,\"Hello World!\")" );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5615
    });
5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688

    test( "Test: \"BIN2OCT\"", function () {

        oParser = new parserFormula( "BIN2OCT(101010)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2OCT(101010)" );
        strictEqual( oParser.calculate().getValue(), "52" );

        oParser = new parserFormula( "BIN2OCT(\"101010\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2OCT(\"101010\")" );
        strictEqual( oParser.calculate().getValue(), "52" );

        oParser = new parserFormula( "BIN2OCT(111111111)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2OCT(111111111)" );
        strictEqual( oParser.calculate().getValue(), "777" );

        oParser = new parserFormula( "BIN2OCT(1000000000)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2OCT(1000000000)" );
        strictEqual( oParser.calculate().getValue(), "7777777000" );

        oParser = new parserFormula( "BIN2OCT(1111111111)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2OCT(1111111111)" );
        strictEqual( oParser.calculate().getValue(), "7777777777" );

        oParser = new parserFormula( "BIN2OCT(101010, 2)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2OCT(101010,2)" );
        strictEqual( oParser.calculate().getValue(), "52" );

        oParser = new parserFormula( "BIN2OCT(101010, 4)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2OCT(101010,4)" );
        strictEqual( oParser.calculate().getValue(), "0052" );

        oParser = new parserFormula( "BIN2OCT(101010, 4.5)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2OCT(101010,4.5)" );
        strictEqual( oParser.calculate().getValue(), "0052" );

        oParser = new parserFormula( "BIN2OCT(\"Hello World!\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2OCT(\"Hello World!\")" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "BIN2OCT(1234567890)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2OCT(1234567890)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "BIN2OCT(101010101010)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2OCT(101010101010)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "BIN2OCT(101010, 1)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2OCT(101010,1)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "BIN2OCT(101010, -4)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2OCT(101010,-4)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "BIN2OCT(101010, \"Hello World!\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "BIN2OCT(101010,\"Hello World!\")" );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5689
    });
5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772

    test( "Test: \"DEC2BIN\"", function () {

        oParser = new parserFormula( "DEC2BIN(42)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(42)" );
        strictEqual( oParser.calculate().getValue(), "101010" );

        oParser = new parserFormula( "DEC2BIN(\"42\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(\"42\")" );
        strictEqual( oParser.calculate().getValue(), "101010" );

        oParser = new parserFormula( "DEC2BIN(-512)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(-512)" );
        strictEqual( oParser.calculate().getValue(), "1000000000" );

        oParser = new parserFormula( "DEC2BIN(-511)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(-511)" );
        strictEqual( oParser.calculate().getValue(), "1000000001" );

        oParser = new parserFormula( "DEC2BIN(-1)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(-1)" );
        strictEqual( oParser.calculate().getValue(), "1111111111" );

        oParser = new parserFormula( "DEC2BIN(0)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(0)" );
        strictEqual( oParser.calculate().getValue(), "0" );

        oParser = new parserFormula( "DEC2BIN(1)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(1)" );
        strictEqual( oParser.calculate().getValue(), "1" );

        oParser = new parserFormula( "DEC2BIN(510)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(510)" );
        strictEqual( oParser.calculate().getValue(), "111111110" );

        oParser = new parserFormula( "DEC2BIN(511)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(511)" );
        strictEqual( oParser.calculate().getValue(), "111111111" );

        oParser = new parserFormula( "DEC2BIN(42, 6)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(42,6)" );
        strictEqual( oParser.calculate().getValue(), "101010" );

        oParser = new parserFormula( "DEC2BIN(42, 8)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(42,8)" );
        strictEqual( oParser.calculate().getValue(), "00101010" );

        oParser = new parserFormula( "DEC2BIN(\"Hello World!\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(\"Hello World!\")" );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

        oParser = new parserFormula( "DEC2BIN(\"2a\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(\"2a\")" );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

        oParser = new parserFormula( "DEC2BIN(-513)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(-513)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "DEC2BIN(512)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(512)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "DEC2BIN(42, -8)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2BIN(42,-8)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5773
    });
5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841

    test( "Test: \"DEC2HEX\"", function () {

        oParser = new parserFormula( "DEC2HEX(42)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2HEX(42)" );
        strictEqual( oParser.calculate().getValue(), "2A" );

        oParser = new parserFormula( "DEC2HEX(\"42\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2HEX(\"42\")" );
        strictEqual( oParser.calculate().getValue(), "2A" );

        oParser = new parserFormula( "DEC2HEX(-549755813888)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2HEX(-549755813888)" );
        strictEqual( oParser.calculate().getValue(), "8000000000" );

        oParser = new parserFormula( "DEC2HEX(-549755813887)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2HEX(-549755813887)" );
        strictEqual( oParser.calculate().getValue(), "8000000001" );

        oParser = new parserFormula( "DEC2HEX(-1)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2HEX(-1)" );
        strictEqual( oParser.calculate().getValue(), "FFFFFFFFFF" );

        oParser = new parserFormula( "DEC2HEX(0)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2HEX(0)" );
        strictEqual( oParser.calculate().getValue(), "0" );

        oParser = new parserFormula( "DEC2HEX(1)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2HEX(1)" );
        strictEqual( oParser.calculate().getValue(), "1" );

        oParser = new parserFormula( "DEC2HEX(549755813886)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2HEX(549755813886)" );
        strictEqual( oParser.calculate().getValue(), "7FFFFFFFFE" );

        oParser = new parserFormula( "DEC2HEX(549755813887)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2HEX(549755813887)" );
        strictEqual( oParser.calculate().getValue(), "7FFFFFFFFF" );

        oParser = new parserFormula( "DEC2HEX(42, 2)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2HEX(42,2)" );
        strictEqual( oParser.calculate().getValue(), "2A" );

        oParser = new parserFormula( "DEC2HEX(42, 4)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2HEX(42,4)" );
        strictEqual( oParser.calculate().getValue(), "002A" );

        oParser = new parserFormula( "DEC2HEX(\"Hello World!\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2HEX(\"Hello World!\")" );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

        oParser = new parserFormula( "DEC2HEX(\"2a\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2HEX(\"2a\")" );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5842
    });
5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930

    test( "Test: \"DEC2OCT\"", function () {

        oParser = new parserFormula( "DEC2OCT(42)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(42)" );
        strictEqual( oParser.calculate().getValue(), "52" );

        oParser = new parserFormula( "DEC2OCT(\"42\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(\"42\")" );
        strictEqual( oParser.calculate().getValue(), "52" );

        oParser = new parserFormula( "DEC2OCT(-536870912)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(-536870912)" );
        strictEqual( oParser.calculate().getValue(), "4000000000" );

        oParser = new parserFormula( "DEC2OCT(-536870911)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(-536870911)" );
        strictEqual( oParser.calculate().getValue(), "4000000001" );

        oParser = new parserFormula( "DEC2OCT(-1)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(-1)" );
        strictEqual( oParser.calculate().getValue(), "7777777777" );

        oParser = new parserFormula( "DEC2OCT(0)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(0)" );
        strictEqual( oParser.calculate().getValue(), "0" );

        oParser = new parserFormula( "DEC2OCT(-0)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(-0)" );
        strictEqual( oParser.calculate().getValue(), "0" );

        oParser = new parserFormula( "DEC2OCT(1)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(1)" );
        strictEqual( oParser.calculate().getValue(), "1" );

        oParser = new parserFormula( "DEC2OCT(536870910)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(536870910)" );
        strictEqual( oParser.calculate().getValue(), "3777777776" );

        oParser = new parserFormula( "DEC2OCT(536870911)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(536870911)" );
        strictEqual( oParser.calculate().getValue(), "3777777777" );

        oParser = new parserFormula( "DEC2OCT(42, 2)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(42,2)" );
        strictEqual( oParser.calculate().getValue(), "52" );

        oParser = new parserFormula( "DEC2OCT(42, 4)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(42,4)" );
        strictEqual( oParser.calculate().getValue(), "0052" );

        oParser = new parserFormula( "DEC2OCT(\"Hello World!\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(\"Hello World!\")" );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

        oParser = new parserFormula( "DEC2OCT(\"2a\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(\"2a\")" );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

        oParser = new parserFormula( "DEC2OCT(-536870913)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(-536870913)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "DEC2OCT(536870912)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(536870912)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "DEC2OCT(42, 1)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "DEC2OCT(42,1)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
5931
    });
5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014

    test( "Test: \"HEX2BIN\"", function () {

        oParser = new parserFormula( "HEX2BIN(\"2a\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(\"2a\")" );
        strictEqual( oParser.calculate().getValue(), "101010" );

        oParser = new parserFormula( "HEX2BIN(\"fffffffe00\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(\"fffffffe00\")" );
        strictEqual( oParser.calculate().getValue(), "1000000000" );

        oParser = new parserFormula( "HEX2BIN(\"fffffffe01\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(\"fffffffe01\")" );
        strictEqual( oParser.calculate().getValue(), "1000000001" );

        oParser = new parserFormula( "HEX2BIN(\"ffffffffff\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(\"ffffffffff\")" );
        strictEqual( oParser.calculate().getValue(), "1111111111" );

        oParser = new parserFormula( "HEX2BIN(0)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(0)" );
        strictEqual( oParser.calculate().getValue(), "0" );

        oParser = new parserFormula( "HEX2BIN(1)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(1)" );
        strictEqual( oParser.calculate().getValue(), "1" );

        oParser = new parserFormula( "HEX2BIN(\"1fe\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(\"1fe\")" );
        strictEqual( oParser.calculate().getValue(), "111111110" );

        oParser = new parserFormula( "HEX2BIN(\"1ff\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(\"1ff\")" );
        strictEqual( oParser.calculate().getValue(), "111111111" );

        oParser = new parserFormula( "HEX2BIN(\"2a\",6)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(\"2a\",6)" );
        strictEqual( oParser.calculate().getValue(), "101010" );

        oParser = new parserFormula( "HEX2BIN(\"2a\",8)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(\"2a\",8)" );
        strictEqual( oParser.calculate().getValue(), "00101010" );

        oParser = new parserFormula( "HEX2BIN(\"Hello World!\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(\"Hello World!\")" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "HEX2BIN(\"fffffffdff\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(\"fffffffdff\")" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "HEX2BIN(\"200\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(\"200\")" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "HEX2BIN(\"2a\", 5)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(\"2a\",5)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "HEX2BIN(\"2a\", -8)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(\"2a\",-8)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!" );

        oParser = new parserFormula( "HEX2BIN(\"2a\", \"Hello World!\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2BIN(\"2a\",\"Hello World!\")" );
        strictEqual( oParser.calculate().getValue(), "#VALUE!" );

Alexander.Trofimov's avatar
Alexander.Trofimov committed
6015
    });
6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058

    test( "Test: \"HEX2DEC\"", function () {

        oParser = new parserFormula( "HEX2DEC(\"2a\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2DEC(\"2a\")" );
        strictEqual( oParser.calculate().getValue(), 42);

        oParser = new parserFormula( "HEX2DEC(\"8000000000\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2DEC(\"8000000000\")" );
        strictEqual( oParser.calculate().getValue(), -549755813888);

        oParser = new parserFormula( "HEX2DEC(\"ffffffffff\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2DEC(\"ffffffffff\")" );
        strictEqual( oParser.calculate().getValue(), -1);

        oParser = new parserFormula( "HEX2DEC(\"0\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2DEC(\"0\")" );
        strictEqual( oParser.calculate().getValue(), 0);

        oParser = new parserFormula( "HEX2DEC(\"1\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2DEC(\"1\")" );
        strictEqual( oParser.calculate().getValue(), 1);

        oParser = new parserFormula( "HEX2DEC(0)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2DEC(0)" );
        strictEqual( oParser.calculate().getValue(), 0);

        oParser = new parserFormula( "HEX2DEC(1)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2DEC(1)" );
        strictEqual( oParser.calculate().getValue(), 1);

        oParser = new parserFormula( "HEX2DEC(\"7fffffffff\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2DEC(\"7fffffffff\")" );
        strictEqual( oParser.calculate().getValue(), 549755813887);

Alexander.Trofimov's avatar
Alexander.Trofimov committed
6059
    });
6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122

    test( "Test: \"HEX2OCT\"", function () {

        oParser = new parserFormula( "HEX2OCT(\"2a\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2OCT(\"2a\")" );
        strictEqual( oParser.calculate().getValue(), "52");

        oParser = new parserFormula( "HEX2OCT(\"ffe0000000\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2OCT(\"ffe0000000\")" );
        strictEqual( oParser.calculate().getValue(), "4000000000");

        oParser = new parserFormula( "HEX2OCT(\"ffe0000001\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2OCT(\"ffe0000001\")" );
        strictEqual( oParser.calculate().getValue(), "4000000001");

        oParser = new parserFormula( "HEX2OCT(0)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2OCT(0)" );
        strictEqual( oParser.calculate().getValue(), "0");

        oParser = new parserFormula( "HEX2OCT(1)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2OCT(1)" );
        strictEqual( oParser.calculate().getValue(), "1");

        oParser = new parserFormula( "HEX2OCT(\"1ffffffe\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2OCT(\"1ffffffe\")" );
        strictEqual( oParser.calculate().getValue(), "3777777776");

        oParser = new parserFormula( "HEX2OCT(\"1fffffff\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2OCT(\"1fffffff\")" );
        strictEqual( oParser.calculate().getValue(), "3777777777");

        oParser = new parserFormula( "HEX2OCT(\"2a\",2)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2OCT(\"2a\",2)" );
        strictEqual( oParser.calculate().getValue(), "52");

        oParser = new parserFormula( "HEX2OCT(\"2a\",4)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2OCT(\"2a\",4)" );
        strictEqual( oParser.calculate().getValue(), "0052");

        oParser = new parserFormula( "HEX2OCT(\"Hello World!\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2OCT(\"Hello World!\")" );
        strictEqual( oParser.calculate().getValue(), "#NUM!");

        oParser = new parserFormula( "HEX2OCT(\"ffdfffffff\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2OCT(\"ffdfffffff\")" );
        strictEqual( oParser.calculate().getValue(), "#NUM!");

        oParser = new parserFormula( "HEX2OCT(\"2a\", 1)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "HEX2OCT(\"2a\",1)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!");

Alexander.Trofimov's avatar
Alexander.Trofimov committed
6123
    });
6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186

    test( "Test: \"OCT2BIN\"", function () {

        oParser = new parserFormula( "OCT2BIN(\"52\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2BIN(\"52\")" );
        strictEqual( oParser.calculate().getValue(), "101010");

        oParser = new parserFormula( "OCT2BIN(\"7777777000\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2BIN(\"7777777000\")" );
        strictEqual( oParser.calculate().getValue(), "1000000000");

        oParser = new parserFormula( "OCT2BIN(\"7777777001\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2BIN(\"7777777001\")" );
        strictEqual( oParser.calculate().getValue(), "1000000001");

        oParser = new parserFormula( "OCT2BIN(\"7777777777\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2BIN(\"7777777777\")" );
        strictEqual( oParser.calculate().getValue(), "1111111111");

        oParser = new parserFormula( "OCT2BIN(\"0\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2BIN(\"0\")" );
        strictEqual( oParser.calculate().getValue(), "0");

        oParser = new parserFormula( "OCT2BIN(\"1\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2BIN(\"1\")" );
        strictEqual( oParser.calculate().getValue(), "1");

        oParser = new parserFormula( "OCT2BIN(\"776\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2BIN(\"776\")" );
        strictEqual( oParser.calculate().getValue(), "111111110");

        oParser = new parserFormula( "OCT2BIN(\"777\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2BIN(\"777\")" );
        strictEqual( oParser.calculate().getValue(), "111111111");

        oParser = new parserFormula( "OCT2BIN(\"52\", 6)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2BIN(\"52\",6)" );
        strictEqual( oParser.calculate().getValue(), "101010");

        oParser = new parserFormula( "OCT2BIN(\"52\", 8)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2BIN(\"52\",8)" );
        strictEqual( oParser.calculate().getValue(), "00101010");

        oParser = new parserFormula( "OCT2BIN(\"Hello World!\", 8)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2BIN(\"Hello World!\",8)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!");

        oParser = new parserFormula( "OCT2BIN(\"52\",\"Hello World!\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2BIN(\"52\",\"Hello World!\")" );
        strictEqual( oParser.calculate().getValue(), "#VALUE!");

Alexander.Trofimov's avatar
Alexander.Trofimov committed
6187
    });
6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230

    test( "Test: \"OCT2DEC\"", function () {

        oParser = new parserFormula( "OCT2DEC(\"52\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2DEC(\"52\")" );
        strictEqual( oParser.calculate().getValue(), 42);

        oParser = new parserFormula( "OCT2DEC(\"4000000000\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2DEC(\"4000000000\")" );
        strictEqual( oParser.calculate().getValue(), -536870912);

        oParser = new parserFormula( "OCT2DEC(\"7777777777\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2DEC(\"7777777777\")" );
        strictEqual( oParser.calculate().getValue(), -1);

        oParser = new parserFormula( "OCT2DEC(\"0\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2DEC(\"0\")" );
        strictEqual( oParser.calculate().getValue(), 0);

        oParser = new parserFormula( "OCT2DEC(\"1\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2DEC(\"1\")" );
        strictEqual( oParser.calculate().getValue(), 1);

        oParser = new parserFormula( "OCT2DEC(\"3777777776\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2DEC(\"3777777776\")" );
        strictEqual( oParser.calculate().getValue(), 536870910);

        oParser = new parserFormula( "OCT2DEC(\"3777777777\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2DEC(\"3777777777\")" );
        strictEqual( oParser.calculate().getValue(), 536870911);

        oParser = new parserFormula( "OCT2DEC(\"3777777777\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2DEC(\"3777777777\")" );
        strictEqual( oParser.calculate().getValue(), 536870911);

Alexander.Trofimov's avatar
Alexander.Trofimov committed
6231
    });
6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299

    test( "Test: \"OCT2HEX\"", function () {

        oParser = new parserFormula( "OCT2HEX(\"52\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2HEX(\"52\")" );
        strictEqual( oParser.calculate().getValue(), "2A");

        oParser = new parserFormula( "OCT2HEX(\"4000000000\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2HEX(\"4000000000\")" );
        strictEqual( oParser.calculate().getValue(), "FFE0000000");

        oParser = new parserFormula( "OCT2HEX(\"4000000001\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2HEX(\"4000000001\")" );
        strictEqual( oParser.calculate().getValue(), "FFE0000001");

        oParser = new parserFormula( "OCT2HEX(\"7777777777\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2HEX(\"7777777777\")" );
        strictEqual( oParser.calculate().getValue(), "FFFFFFFFFF");

        oParser = new parserFormula( "OCT2HEX(\"0\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2HEX(\"0\")" );
        strictEqual( oParser.calculate().getValue(), "0");

        oParser = new parserFormula( "OCT2HEX(\"1\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2HEX(\"1\")" );
        strictEqual( oParser.calculate().getValue(), "1");

        oParser = new parserFormula( "OCT2HEX(\"3777777776\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2HEX(\"3777777776\")" );
        strictEqual( oParser.calculate().getValue(), "1FFFFFFE");

        oParser = new parserFormula( "OCT2HEX(\"3777777777\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2HEX(\"3777777777\")" );
        strictEqual( oParser.calculate().getValue(), "1FFFFFFF");

        oParser = new parserFormula( "OCT2HEX(\"52\", 2)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2HEX(\"52\",2)" );
        strictEqual( oParser.calculate().getValue(), "2A");

        oParser = new parserFormula( "OCT2HEX(\"52\", 4)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2HEX(\"52\",4)" );
        strictEqual( oParser.calculate().getValue(), "002A");

        oParser = new parserFormula( "OCT2HEX(\"Hello World!\", 4)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2HEX(\"Hello World!\",4)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!");

        oParser = new parserFormula( "OCT2HEX(\"52\", -4)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2HEX(\"52\",-4)" );
        strictEqual( oParser.calculate().getValue(), "#NUM!");

        oParser = new parserFormula( "OCT2HEX(\"52\", \"Hello World!\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "OCT2HEX(\"52\",\"Hello World!\")" );
        strictEqual( oParser.calculate().getValue(), "#VALUE!");

Alexander.Trofimov's avatar
Alexander.Trofimov committed
6300
    });
6301

Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328
    test( "Test: \"COMPLEX\"", function () {

        oParser = new parserFormula( "COMPLEX(-3.5,19.6)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "COMPLEX(-3.5,19.6)" );
        strictEqual( oParser.calculate().getValue(), "-3.5+19.6i");

        oParser = new parserFormula( "COMPLEX(3.5,-19.6,\"j\")", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "COMPLEX(3.5,-19.6,\"j\")" );
        strictEqual( oParser.calculate().getValue(), "3.5-19.6j");

        oParser = new parserFormula( "COMPLEX(3.5,0)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "COMPLEX(3.5,0)" );
        strictEqual( oParser.calculate().getValue(), "3.5");

        oParser = new parserFormula( "COMPLEX(0,2.4)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "COMPLEX(0,2.4)" );
        strictEqual( oParser.calculate().getValue(), "2.4i");

        oParser = new parserFormula( "COMPLEX(0,0)", "A2", ws );
        ok( oParser.parse() );
        ok( oParser.assemble() == "COMPLEX(0,0)" );
        strictEqual( oParser.calculate().getValue(), "0");

Alexander.Trofimov's avatar
Alexander.Trofimov committed
6329
    });
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348

    test( "Test: \"DELTA\"", function () {

        oParser = new parserFormula( "DELTA(10.5,10.5)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1);

        oParser = new parserFormula( "DELTA(10.5,10.6)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 0);

        oParser = new parserFormula( "DELTA(10.5)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 0);

        oParser = new parserFormula( "DELTA(0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1);

Alexander.Trofimov's avatar
Alexander.Trofimov committed
6349
    });
Dmitry.Shahtanov's avatar
Dmitry.Shahtanov committed
6350

6351 6352 6353 6354
    test( "Test: \"ERF\"", function () {

        oParser = new parserFormula( "ERF(1.234,4.5432)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
fix:  
Dmitry.Shahtanov committed
6355
        strictEqual( oParser.calculate().getValue().toFixed(14)-0, 0.08096058291050978.toFixed(14)-0 );
6356 6357 6358

        oParser = new parserFormula( "ERF(1)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
fix:  
Dmitry.Shahtanov committed
6359
        strictEqual( oParser.calculate().getValue().toFixed(14)-0, 0.8427007929497149.toFixed(14)-0 );
6360 6361 6362

        oParser = new parserFormula( "ERF(0,1.345)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
fix:  
Dmitry.Shahtanov committed
6363
        strictEqual( oParser.calculate().getValue().toFixed(14)-0, 0.9428441710878559.toFixed(14)-0 );
6364 6365 6366

        oParser = new parserFormula( "ERF(1.234)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
fix:  
Dmitry.Shahtanov committed
6367
        strictEqual( oParser.calculate().getValue().toFixed(14)-0, 0.9190394169576684.toFixed(14)-0 );
6368

Alexander.Trofimov's avatar
Alexander.Trofimov committed
6369
    });
6370 6371 6372 6373 6374

    test( "Test: \"ERFC\"", function () {

        oParser = new parserFormula( "ERFC(1.234)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
fix:  
Dmitry.Shahtanov committed
6375
        strictEqual( oParser.calculate().getValue().toFixed(14)-0, 0.08096058304233157.toFixed(14)-0 );
6376 6377 6378

        oParser = new parserFormula( "ERFC(1)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
fix:  
Dmitry.Shahtanov committed
6379
        strictEqual( oParser.calculate().getValue().toFixed(14)-0, 0.15729920705028513.toFixed(14)-0 );
6380 6381 6382 6383 6384 6385 6386

        oParser = new parserFormula( "ERFC(0)", "A2", ws );
        ok( oParser.parse() );
        strictEqual( oParser.calculate().getValue(), 1 );

        oParser = new parserFormula( "ERFC(-1)", "A2", ws );
        ok( oParser.parse() );
Dmitry.Shahtanov's avatar
fix:  
Dmitry.Shahtanov committed
6387
        strictEqual( oParser.calculate().getValue().toFixed(14)-0, 1.8427007929497148.toFixed(14)-0 );
6388

Alexander.Trofimov's avatar
Alexander.Trofimov committed
6389
    });
6390

6391
} );