michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * michael@0: * Date: 14 Mar 2003 michael@0: * SUMMARY: Testing left-associativity of the + operator michael@0: * michael@0: * See ECMA-262 Ed.3, Section 11.6.1, "The Addition operator" michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=196290 michael@0: * michael@0: * The upshot: |a + b + c| should always equal |(a + b) + c| michael@0: * michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = 196290; michael@0: var summary = 'Testing left-associativity of the + operator'; michael@0: var status = ''; michael@0: var statusitems = []; michael@0: var actual = ''; michael@0: var actualvalues = []; michael@0: var expect= ''; michael@0: var expectedvalues = []; michael@0: michael@0: michael@0: status = inSection(1); michael@0: actual = 1 + 1 + 'px'; michael@0: expect = '2px'; michael@0: addThis(); michael@0: michael@0: status = inSection(2); michael@0: actual = 'px' + 1 + 1; michael@0: expect = 'px11'; michael@0: addThis(); michael@0: michael@0: status = inSection(3); michael@0: actual = 1 + 1 + 1 + 'px'; michael@0: expect = '3px'; michael@0: addThis(); michael@0: michael@0: status = inSection(4); michael@0: actual = 1 + 1 + 'a' + 1 + 1 + 'b'; michael@0: expect = '2a11b'; michael@0: addThis(); michael@0: michael@0: /* michael@0: * The next sections test the + operator via eval() michael@0: */ michael@0: status = inSection(5); michael@0: actual = sumThese(1, 1, 'a', 1, 1, 'b'); michael@0: expect = '2a11b'; michael@0: addThis(); michael@0: michael@0: status = inSection(6); michael@0: actual = sumThese(new Number(1), new Number(1), 'a'); michael@0: expect = '2a'; michael@0: addThis(); michael@0: michael@0: status = inSection(7); michael@0: actual = sumThese('a', new Number(1), new Number(1)); michael@0: expect = 'a11'; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: michael@0: function addThis() michael@0: { michael@0: statusitems[UBound] = status; michael@0: actualvalues[UBound] = actual; michael@0: expectedvalues[UBound] = expect; michael@0: UBound++; michael@0: } michael@0: michael@0: /* michael@0: * Applies the + operator to the provided arguments via eval(). michael@0: * michael@0: * Form an eval string of the form 'arg1 + arg2 + arg3', but michael@0: * remember to add double-quotes inside the eval string around michael@0: * any argument that is of string type. For example, suppose the michael@0: * arguments were 11, 'a', 22. Then the eval string should be michael@0: * michael@0: * arg1 + quoteThis(arg2) + arg3 michael@0: * michael@0: * If we didn't put double-quotes around the string argument, michael@0: * we'd get this for an eval string: michael@0: * michael@0: * '11 + a + 22' michael@0: * michael@0: * If we eval() this, we get 'ReferenceError: a is not defined'. michael@0: * With proper quoting, we get eval('11 + "a" + 22') as desired. michael@0: */ michael@0: function sumThese() michael@0: { michael@0: var sEval = ''; michael@0: var arg; michael@0: var i; michael@0: michael@0: var L = arguments.length; michael@0: for (i=0; i