1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_3/Expressions/11.6.1-1.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,142 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * 1.11 + * Date: 14 Mar 2003 1.12 + * SUMMARY: Testing left-associativity of the + operator 1.13 + * 1.14 + * See ECMA-262 Ed.3, Section 11.6.1, "The Addition operator" 1.15 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=196290 1.16 + * 1.17 + * The upshot: |a + b + c| should always equal |(a + b) + c| 1.18 + * 1.19 + */ 1.20 +//----------------------------------------------------------------------------- 1.21 +var UBound = 0; 1.22 +var BUGNUMBER = 196290; 1.23 +var summary = 'Testing left-associativity of the + operator'; 1.24 +var status = ''; 1.25 +var statusitems = []; 1.26 +var actual = ''; 1.27 +var actualvalues = []; 1.28 +var expect= ''; 1.29 +var expectedvalues = []; 1.30 + 1.31 + 1.32 +status = inSection(1); 1.33 +actual = 1 + 1 + 'px'; 1.34 +expect = '2px'; 1.35 +addThis(); 1.36 + 1.37 +status = inSection(2); 1.38 +actual = 'px' + 1 + 1; 1.39 +expect = 'px11'; 1.40 +addThis(); 1.41 + 1.42 +status = inSection(3); 1.43 +actual = 1 + 1 + 1 + 'px'; 1.44 +expect = '3px'; 1.45 +addThis(); 1.46 + 1.47 +status = inSection(4); 1.48 +actual = 1 + 1 + 'a' + 1 + 1 + 'b'; 1.49 +expect = '2a11b'; 1.50 +addThis(); 1.51 + 1.52 +/* 1.53 + * The next sections test the + operator via eval() 1.54 + */ 1.55 +status = inSection(5); 1.56 +actual = sumThese(1, 1, 'a', 1, 1, 'b'); 1.57 +expect = '2a11b'; 1.58 +addThis(); 1.59 + 1.60 +status = inSection(6); 1.61 +actual = sumThese(new Number(1), new Number(1), 'a'); 1.62 +expect = '2a'; 1.63 +addThis(); 1.64 + 1.65 +status = inSection(7); 1.66 +actual = sumThese('a', new Number(1), new Number(1)); 1.67 +expect = 'a11'; 1.68 +addThis(); 1.69 + 1.70 + 1.71 + 1.72 +//----------------------------------------------------------------------------- 1.73 +test(); 1.74 +//----------------------------------------------------------------------------- 1.75 + 1.76 + 1.77 + 1.78 +function addThis() 1.79 +{ 1.80 + statusitems[UBound] = status; 1.81 + actualvalues[UBound] = actual; 1.82 + expectedvalues[UBound] = expect; 1.83 + UBound++; 1.84 +} 1.85 + 1.86 +/* 1.87 + * Applies the + operator to the provided arguments via eval(). 1.88 + * 1.89 + * Form an eval string of the form 'arg1 + arg2 + arg3', but 1.90 + * remember to add double-quotes inside the eval string around 1.91 + * any argument that is of string type. For example, suppose the 1.92 + * arguments were 11, 'a', 22. Then the eval string should be 1.93 + * 1.94 + * arg1 + quoteThis(arg2) + arg3 1.95 + * 1.96 + * If we didn't put double-quotes around the string argument, 1.97 + * we'd get this for an eval string: 1.98 + * 1.99 + * '11 + a + 22' 1.100 + * 1.101 + * If we eval() this, we get 'ReferenceError: a is not defined'. 1.102 + * With proper quoting, we get eval('11 + "a" + 22') as desired. 1.103 + */ 1.104 +function sumThese() 1.105 +{ 1.106 + var sEval = ''; 1.107 + var arg; 1.108 + var i; 1.109 + 1.110 + var L = arguments.length; 1.111 + for (i=0; i<L; i++) 1.112 + { 1.113 + arg = arguments[i]; 1.114 + if (typeof arg === 'string') 1.115 + arg = quoteThis(arg); 1.116 + 1.117 + if (i < L-1) 1.118 + sEval += arg + ' + '; 1.119 + else 1.120 + sEval += arg; 1.121 + } 1.122 + 1.123 + return eval(sEval); 1.124 +} 1.125 + 1.126 + 1.127 +function quoteThis(x) 1.128 +{ 1.129 + return '"' + x + '"'; 1.130 +} 1.131 + 1.132 + 1.133 +function test() 1.134 +{ 1.135 + enterFunc('test'); 1.136 + printBugNumber(BUGNUMBER); 1.137 + printStatus(summary); 1.138 + 1.139 + for (var i=0; i<UBound; i++) 1.140 + { 1.141 + reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 1.142 + } 1.143 + 1.144 + exitFunc ('test'); 1.145 +}