1// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1.  Redistributions of source code must retain the above copyright
8//     notice, this list of conditions and the following disclaimer.
9// 2.  Redistributions in binary form must reproduce the above copyright
10//     notice, this list of conditions and the following disclaimer in the
11//     documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24description(
25"This test checks that toString() round-trip on a function that has a array with elision does not remove a comma."
26);
27
28function f1() {
29    return [,];
30}
31
32function f2() {
33    return [1];
34}
35
36function f3() {
37    return [1,];
38}
39
40// this is the first testcase that proves that trailing
41// elision comma is not removed
42function f4() {
43    return [1,,];
44}
45
46function f5() {
47    return [1,,,];
48}
49function f6() {
50    return [1,,,4];
51}
52
53function f7() {
54    return [,2,];
55}
56
57function f8() {
58    return [,2,,];
59}
60
61function f9() {
62    return [,2,,,5];
63}
64
65function f10() {
66    return [,,3,,,];
67}
68
69function f11() {
70    return [,,3,,,6];
71}
72
73function f12() {
74    return [,undefined];
75}
76
77function f13() {
78    return [,undefined,];
79}
80
81function f14() {
82    return [,undefined,,];
83}
84
85function f15() {
86    return [,,];
87}
88
89function f16() {
90    return [,,,];
91}
92
93shouldBe("typeof undefined", "'undefined'");
94
95unevalf = function(x) { return '(' + x.toString() + ')'; };
96
97function testToStringAndLength(fn, length, lastElement)
98{
99    // check that array length is correct
100    shouldBe(""+ fn +"().length", "" + length);
101
102    // check that last element is what it is supposed to be
103    shouldBe(""+ fn +"()[" + length +"-1]", "" + lastElement);
104
105    // check that toString result evaluates to code that can be evaluated
106    // and that toString doesn't remove the trailing elision comma.
107    shouldBe("unevalf(eval(unevalf("+fn+")))", "unevalf(" + fn + ")");
108
109    // check that toString()ed functions should retain semantics
110
111    shouldBe("eval(unevalf("+fn+"))().length", ""+length);
112    shouldBe("eval(unevalf("+fn+"))()[" + length +"-1]", ""+lastElement);
113}
114
115
116testToStringAndLength("f1", 1);
117testToStringAndLength("f2", 1, 1);
118testToStringAndLength("f3", 1,1);
119testToStringAndLength("f4", 2);
120testToStringAndLength("f5", 3);
121testToStringAndLength("f6", 4, 4);
122testToStringAndLength("f7", 2, 2);
123testToStringAndLength("f8", 3);
124testToStringAndLength("f9", 5, 5);
125testToStringAndLength("f10", 5);
126testToStringAndLength("f11", 6, 6);
127testToStringAndLength("f12", 2);
128testToStringAndLength("f13", 2);
129testToStringAndLength("f14", 3);
130testToStringAndLength("f15", 2);
131testToStringAndLength("f16", 3);
132