1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2/* ***** BEGIN LICENSE BLOCK ***** 3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 4 * 5 * The contents of this file are subject to the Mozilla Public License Version 6 * 1.1 (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * http://www.mozilla.org/MPL/ 9 * 10 * Software distributed under the License is distributed on an "AS IS" basis, 11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 12 * for the specific language governing rights and limitations under the 13 * License. 14 * 15 * The Original Code is JavaScript Engine testing utilities. 16 * 17 * The Initial Developer of the Original Code is 18 * Mozilla Foundation. 19 * Portions created by the Initial Developer are Copyright (C) 2005 20 * the Initial Developer. All Rights Reserved. 21 * 22 * Contributor(s): 23 * 24 * Alternatively, the contents of this file may be used under the terms of 25 * either the GNU General Public License Version 2 or later (the "GPL"), or 26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 27 * in which case the provisions of the GPL or the LGPL are applicable instead 28 * of those above. If you wish to allow use of your version of this file only 29 * under the terms of either the GPL or the LGPL, and not to allow others to 30 * use your version of this file under the terms of the MPL, indicate your 31 * decision by deleting the provisions above and replace them with the notice 32 * and other provisions required by the GPL or the LGPL. If you do not delete 33 * the provisions above, a recipient may use your version of this file under 34 * the terms of any one of the MPL, the GPL or the LGPL. 35 * 36 * ***** END LICENSE BLOCK ***** */ 37//----------------------------------------------------------------------------- 38var bug = 304828; 39var summary = 'Array Generic Methods'; 40var actual = ''; 41var expect = ''; 42printBugNumber (bug); 43printStatus (summary); 44 45var value; 46 47// use Array methods on a String 48// join 49value = '123'; 50expect = '1,2,3'; 51try 52{ 53 actual = Array.prototype.join.call(value); 54} 55catch(e) 56{ 57 actual = e + ''; 58} 59reportCompare(expect, actual, summary + ': join'); 60 61// reverse 62value = '123'; 63expect = 'TypeError: Attempted to assign to readonly property.'; 64try 65{ 66 actual = Array.prototype.reverse.call(value) + ''; 67} 68catch(e) 69{ 70 actual = e + ''; 71} 72reportCompare(expect, actual, summary + ': reverse'); 73 74// sort 75value = 'cba'; 76expect = 'TypeError: Attempted to assign to readonly property.'; 77try 78{ 79 actual = Array.prototype.sort.call(value) + ''; 80} 81catch(e) 82{ 83 actual = e + ''; 84} 85reportCompare(expect, actual, summary + ': sort'); 86 87// push 88value = 'abc'; 89expect = 6; 90try 91{ 92 actual = Array.prototype.push.call(value, 'd', 'e', 'f'); 93} 94catch(e) 95{ 96 actual = e + ''; 97} 98reportCompare(expect, actual, summary + ': push'); 99reportCompare('abc', value, summary + ': push'); 100 101// pop 102value = 'abc'; 103expect = 'TypeError: Unable to delete property.'; 104try 105{ 106 actual = Array.prototype.pop.call(value); 107} 108catch(e) 109{ 110 actual = e + ''; 111} 112reportCompare(expect, actual, summary + ': pop'); 113reportCompare('abc', value, summary + ': pop'); 114 115// unshift 116value = 'def'; 117expect = "TypeError: Attempted to assign to readonly property."; 118try 119{ 120 actual = Array.prototype.unshift.call(value, 'a', 'b', 'c'); 121} 122catch(e) 123{ 124 actual = e + ''; 125} 126reportCompare(expect, actual, summary + ': unshift'); 127reportCompare('def', value, summary + ': unshift'); 128 129// shift 130value = 'abc'; 131expect = 'TypeError: Attempted to assign to readonly property.'; 132try 133{ 134 actual = Array.prototype.shift.call(value); 135} 136catch(e) 137{ 138 actual = e + ''; 139} 140reportCompare(expect, actual, summary + ': shift'); 141reportCompare('abc', value, summary + ': shift'); 142 143// splice 144value = 'abc'; 145expect = 'TypeError: Attempted to assign to readonly property.'; 146try 147{ 148 actual = Array.prototype.splice.call(value, 1, 1) + ''; 149} 150catch(e) 151{ 152 actual = e + ''; 153} 154reportCompare(expect, actual, summary + ': splice'); 155 156// concat 157value = 'abc'; 158expect = 'abc,d,e,f'; 159try 160{ 161 actual = Array.prototype.concat.call(value, 'd', 'e', 'f') + ''; 162} 163catch(e) 164{ 165 actual = e + ''; 166} 167reportCompare(expect, actual, summary + ': concat'); 168 169// slice 170value = 'abc'; 171expect = 'b'; 172try 173{ 174 actual = Array.prototype.slice.call(value, 1, 2) + ''; 175} 176catch(e) 177{ 178 actual = e + ''; 179} 180reportCompare(expect, actual, summary + ': slice'); 181 182// indexOf 183value = 'abc'; 184expect = 1; 185try 186{ 187 actual = Array.prototype.indexOf.call(value, 'b'); 188} 189catch(e) 190{ 191 actual = e + ''; 192} 193reportCompare(expect, actual, summary + ': indexOf'); 194 195// lastIndexOf 196value = 'abcabc'; 197expect = 4; 198try 199{ 200 actual = Array.prototype.lastIndexOf.call(value, 'b'); 201} 202catch(e) 203{ 204 actual = e + ''; 205} 206reportCompare(expect, actual, summary + ': lastIndexOf'); 207 208// forEach 209value = 'abc'; 210expect = 'ABC'; 211actual = ''; 212try 213{ 214 Array.prototype.forEach.call(value, 215 function (v, index, array) 216 {actual += array[index].toUpperCase();}); 217} 218catch(e) 219{ 220 actual = e + ''; 221} 222reportCompare(expect, actual, summary + ': forEach'); 223 224// map 225value = 'abc'; 226expect = 'A,B,C'; 227try 228{ 229 actual = Array.prototype.map.call(value, 230 function (v, index, array) 231 {return v.toUpperCase();}) + ''; 232} 233catch(e) 234{ 235 actual = e + ''; 236} 237reportCompare(expect, actual, summary + ': map'); 238 239// filter 240value = '1234567890'; 241expect = '2,4,6,8,0'; 242try 243{ 244 actual = Array.prototype.filter.call(value, 245 function (v, index, array) 246 {return array[index] % 2 == 0;}) + ''; 247} 248catch(e) 249{ 250 actual = e + ''; 251} 252reportCompare(expect, actual, summary + ': filter'); 253 254// every 255value = '1234567890'; 256expect = false; 257try 258{ 259 actual = Array.prototype.every.call(value, 260 function (v, index, array) 261 {return array[index] % 2 == 0;}); 262} 263catch(e) 264{ 265 actual = e + ''; 266} 267reportCompare(expect, actual, summary + ': every'); 268 269 270 271