19258b6bc66e09368ada54001f619d53b4fc976d5ager@chromium.org// Copyright 2008 the V8 project authors. All rights reserved.
29a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Redistribution and use in source and binary forms, with or without
39a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// modification, are permitted provided that the following conditions are
49a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// met:
59a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//
69a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//     * Redistributions of source code must retain the above copyright
79a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       notice, this list of conditions and the following disclaimer.
89a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//     * Redistributions in binary form must reproduce the above
99a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       copyright notice, this list of conditions and the following
109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       disclaimer in the documentation and/or other materials provided
119a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       with the distribution.
129a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//     * Neither the name of Google Inc. nor the names of its
139a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       contributors may be used to endorse or promote products derived
149a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       from this software without specific prior written permission.
159a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//
169a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
179a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
189a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
199a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
209a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
219a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
229a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
269a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
289a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Test that array join calls toString on subarrays.
299a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comvar a = [[1,2],3,4,[5,6]];
309ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('1,2345,6', a.join(''));
319a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals('1,2*3*4*5,6', a.join('*'));
329ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('1,2**3**4**5,6', a.join('**'));
339ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('1,2****3****4****5,6', a.join('****'));
349ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('1,2********3********4********5,6', a.join('********'));
359ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('1,2**********3**********4**********5,6', a.join('**********'));
369a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
379a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Create a cycle.
389a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.coma.push(a);
399ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('1,2345,6', a.join(''));
409a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals('1,2*3*4*5,6*', a.join('*'));
419ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('1,2**3**4**5,6**', a.join('**'));
429ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('1,2****3****4****5,6****', a.join('****'));
439ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('1,2********3********4********5,6********', a.join('********'));
449ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('1,2**********3**********4**********5,6**********', a.join('**********'));
459a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
469a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Replace array.prototype.toString.
478e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orgvar oldToString = Array.prototype.toString;
488e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orgArray.prototype.toString = function() { return "array"; };
499ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('array34arrayarray', a.join(''));
509a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals('array*3*4*array*array', a.join('*'));
519ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('array**3**4**array**array', a.join('**'));
529ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('array****3****4****array****array', a.join('****'));
539ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('array********3********4********array********array', a.join('********'));
549ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('array**********3**********4**********array**********array', a.join('**********'));
559a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
568e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orgArray.prototype.toString = function() { throw 42; };
579ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertThrows("a.join('')");
589a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertThrows("a.join('*')");
599ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertThrows("a.join('**')");
609ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertThrows("a.join('****')");
619ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertThrows("a.join('********')");
629ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertThrows("a.join('**********')");
639a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
648e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orgArray.prototype.toString = function() { return "array"; };
659ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('array34arrayarray', a.join(''));
669a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals('array*3*4*array*array', a.join('*'));
679ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('array**3**4**array**array', a.join('**'));
689ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('array****3****4****array****array', a.join('****'));
699ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('array********3********4********array********array', a.join('********'));
709ee27ae43ab88e9bb6417ff9e27af64e0cf13729ager@chromium.orgassertEquals('array**********3**********4**********array**********array', a.join('**********'));
719a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
728e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.org// Restore original toString.
738e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orgdelete Array.prototype.toString;
748e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orgif (Array.prototype.toString != oldToString) {
758e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.org  Array.prototype.toString = oldToString;
768e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.org}
778e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.org
78ab7dad4f999df008b590c74c2fe3d2e2c67ef7ffjkummerow@chromium.orgvar a = new Array(123123);
79ab7dad4f999df008b590c74c2fe3d2e2c67ef7ffjkummerow@chromium.orgassertEquals(123122, String(a).length);
80ab7dad4f999df008b590c74c2fe3d2e2c67ef7ffjkummerow@chromium.orgassertEquals(123122, a.join(",").length);
81ab7dad4f999df008b590c74c2fe3d2e2c67ef7ffjkummerow@chromium.orgassertEquals(246244, a.join("oo").length);
828e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.org
838e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orga = new Array(Math.pow(2,32) - 1);  // Max length.
848e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orgassertEquals("", a.join(""));
858e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orga[123123123] = "o";
868e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orga[1255215215] = "p";
878e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orgassertEquals("op", a.join(""));
888e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.org
898e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orga = new Array(100001);
908e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orgfor (var i = 0; i < a.length; i++) a[i] = undefined;
918e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orga[5] = "ab";
928e8294a88dc7d58f579aee0ba08c19fc8a616e2dsgjesse@chromium.orga[90000] = "cd";
93ab7dad4f999df008b590c74c2fe3d2e2c67ef7ffjkummerow@chromium.orgassertEquals("abcd", a.join(""));  // Must not throw.
94