// Copyright 2008 Google Inc. All Rights Reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following // disclaimer in the documentation and/or other materials provided // with the distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. /** * @fileoverview Test concat on small and large arrays */ var poses = [140, 4000000000]; while (pos = poses.shift()) { var a = new Array(pos); assertEquals(pos, a.length); a.push('foo'); assertEquals(pos + 1, a.length); var b = ['bar']; var c = a.concat(b); assertEquals(pos + 2, c.length); assertEquals("undefined", typeof(c[pos - 1])); assertEquals("foo", c[pos]); assertEquals("bar", c[pos + 1]); // Can we fool the system by putting a number in a string? var onetwofour = "124"; a[onetwofour] = 'doo'; assertEquals(a[124], 'doo'); c = a.concat(b); assertEquals(c[124], 'doo'); // If we put a number in the prototype, then the spec says it should be // copied on concat. Array.prototype["123"] = 'baz'; assertEquals(a[123], 'baz'); c = a.concat(b); assertEquals(pos + 2, c.length); assertEquals("baz", c[123]); assertEquals("undefined", typeof(c[pos - 1])); assertEquals("foo", c[pos]); assertEquals("bar", c[pos + 1]); // When we take the number off the prototype it disappears from a, but // the concat put it in c itself. Array.prototype["123"] = undefined; assertEquals("undefined", typeof(a[123])); assertEquals("baz", c[123]); // Non-numeric properties on the prototype or the array shouldn't get // copied. Array.prototype.moe = 'joe'; a.ben = 'jerry'; assertEquals(a["moe"], 'joe'); assertEquals(a["ben"], 'jerry'); c = a.concat(b); // ben was not copied assertEquals("undefined", typeof(c.ben)); // moe was not copied, but we can see it through the prototype assertEquals("joe", c.moe); // When we take moe off the prototype it disappears from all arrays. Array.prototype.moe = undefined; assertEquals("undefined", typeof(c.moe)); // Negative indeces don't get concated. a[-1] = 'minus1'; assertEquals("minus1", a[-1]); assertEquals("undefined", typeof(a[0xffffffff])); c = a.concat(b); assertEquals("undefined", typeof(c[-1])); assertEquals("undefined", typeof(c[0xffffffff])); assertEquals(c.length, a.length + 1); } a = []; c = a.concat('Hello'); assertEquals(1, c.length); assertEquals("Hello", c[0]); assertEquals("Hello", c.toString());