tizen beta release
[framework/web/webkit-efl.git] / LayoutTests / fast / js / script-tests / array-holes.js
1 description("This tests that arrays have holes that you can see the prototype through, not just missing values.");
2
3 function isHole(array, index)
4 {
5     if (index >= array.length)
6         return "bad index: past length";
7     // Check if we can see through the hole into another room.
8     Array.prototype[index] = "room";
9     var isHole = array[index] == "room";
10     delete Array.prototype[index];
11     return isHole;
12 }
13
14 function showHoles(array)
15 {
16     var string = "[";
17     for (i = 0; i < array.length; ++i) {
18         if (i)
19             string += ", ";
20         if (isHole(array, i))
21             string += "hole";
22         else
23             string += array[i];
24     }
25     string += "]";
26     return string;
27 }
28
29 function returnTrue()
30 {
31     return true;
32 }
33
34 var a;
35
36 function addToArray(arg)
37 {
38     a.push(arg);
39 }
40
41 function addToArrayReturnFalse(arg)
42 {
43     a.push(arg);
44     return false;
45 }
46
47 function addToArrayReturnTrue(arg)
48 {
49     a.push(arg);
50     return true;
51 }
52
53 shouldBe("var a = []; a.length = 1; showHoles(a)", "'[hole]'");
54 shouldBe("var a = []; a[0] = undefined; showHoles(a)", "'[undefined]'");
55 shouldBe("var a = []; a[0] = undefined; delete a[0]; showHoles(a)", "'[hole]'");
56
57 shouldBe("showHoles([0, , 2])", "'[0, hole, 2]'");
58 shouldBe("showHoles([0, 1, ,])", "'[0, 1, hole]'");
59 shouldBe("showHoles([0, , 2].concat([3, , 5]))", "'[0, hole, 2, 3, hole, 5]'");
60 shouldBe("showHoles([0, , 2, 3].reverse())", "'[3, 2, hole, 0]'");
61 shouldBe("a = [0, , 2, 3]; a.shift(); showHoles(a)", "'[hole, 2, 3]'");
62 shouldBe("showHoles([0, , 2, 3].slice(0, 3))", "'[0, hole, 2]'");
63 shouldBe("showHoles([0, , 2, 3].sort())", "'[0, 2, 3, hole]'");
64 shouldBe("showHoles([0, undefined, 2, 3].sort())", "'[0, 2, 3, undefined]'");
65 shouldBe("a = [0, , 2, 3]; a.splice(2, 3, 5, 6); showHoles(a)", "'[0, hole, 5, 6]'");
66 shouldBe("a = [0, , 2, 3]; a.unshift(4); showHoles(a)", "'[4, 0, hole, 2, 3]'");
67 shouldBe("showHoles([0, , 2, 3].filter(returnTrue))", "'[0, 2, 3]'");
68 shouldBe("showHoles([0, undefined, 2, 3].filter(returnTrue))", "'[0, undefined, 2, 3]'");
69 shouldBe("showHoles([0, , 2, 3].map(returnTrue))", "'[true, hole, true, true]'");
70 shouldBe("showHoles([0, undefined, 2, 3].map(returnTrue))", "'[true, true, true, true]'");
71 shouldBe("a = []; [0, , 2, 3].every(addToArrayReturnTrue); showHoles(a)", "'[0, 2, 3]'");
72 shouldBe("a = []; [0, undefined, 2, 3].every(addToArrayReturnTrue); showHoles(a)", "'[0, undefined, 2, 3]'");
73 shouldBe("a = []; [0, , 2, 3].forEach(addToArray); showHoles(a)", "'[0, 2, 3]'");
74 shouldBe("a = []; [0, undefined, 2, 3].forEach(addToArray); showHoles(a)", "'[0, undefined, 2, 3]'");
75 shouldBe("a = []; [0, , 2, 3].some(addToArrayReturnFalse); showHoles(a)", "'[0, 2, 3]'");
76 shouldBe("a = []; [0, undefined, 2, 3].some(addToArrayReturnFalse); showHoles(a)", "'[0, undefined, 2, 3]'");
77 shouldBe("[0, , 2, 3].indexOf()", "-1");
78 shouldBe("[0, undefined, 2, 3].indexOf()", "1");
79 shouldBe("[0, , 2, 3].lastIndexOf()", "-1");
80 shouldBe("[0, undefined, 2, 3].lastIndexOf()", "1");
81
82 Object.prototype[1] = "peekaboo";
83
84 shouldBe("showHoles([0, , 2])", "'[0, hole, 2]'");
85 shouldBe("showHoles([0, 1, ,])", "'[0, 1, hole]'");
86 shouldBe("showHoles([0, , 2].concat([3, , 5]))", "'[0, peekaboo, 2, 3, peekaboo, 5]'");
87 shouldBe("showHoles([0, , 2, 3].reverse())", "'[3, 2, peekaboo, 0]'");
88 shouldBe("a = [0, , 2, 3]; a.shift(); showHoles(a)", "'[peekaboo, 2, 3]'");
89 shouldBe("showHoles([0, , 2, 3].slice(0, 3))", "'[0, peekaboo, 2]'");
90 shouldBe("showHoles([0, , 2, 3].sort())", "'[0, 2, 3, hole]'");
91 shouldBe("showHoles([0, undefined, 2, 3].sort())", "'[0, 2, 3, undefined]'");
92 shouldBe("a = [0, , 2, 3]; a.splice(2, 3, 5, 6); showHoles(a)", "'[0, hole, 5, 6]'");
93 shouldBe("a = [0, , 2, 3]; a.unshift(4); showHoles(a)", "'[4, 0, peekaboo, 2, 3]'");
94 shouldBe("showHoles([0, , 2, 3].filter(returnTrue))", "'[0, peekaboo, 2, 3]'");
95 shouldBe("showHoles([0, undefined, 2, 3].filter(returnTrue))", "'[0, undefined, 2, 3]'");
96 shouldBe("showHoles([0, , 2, 3].map(returnTrue))", "'[true, true, true, true]'");
97 shouldBe("showHoles([0, undefined, 2, 3].map(returnTrue))", "'[true, true, true, true]'");
98 shouldBe("a = []; [0, , 2, 3].every(addToArrayReturnTrue); showHoles(a)", "'[0, peekaboo, 2, 3]'");
99 shouldBe("a = []; [0, undefined, 2, 3].every(addToArrayReturnTrue); showHoles(a)", "'[0, undefined, 2, 3]'");
100 shouldBe("a = []; [0, , 2, 3].forEach(addToArray); showHoles(a)", "'[0, peekaboo, 2, 3]'");
101 shouldBe("a = []; [0, undefined, 2, 3].forEach(addToArray); showHoles(a)", "'[0, undefined, 2, 3]'");
102 shouldBe("a = []; [0, , 2, 3].some(addToArrayReturnFalse); showHoles(a)", "'[0, peekaboo, 2, 3]'");
103 shouldBe("a = []; [0, undefined, 2, 3].some(addToArrayReturnFalse); showHoles(a)", "'[0, undefined, 2, 3]'");
104 shouldBe("[0, , 2, 3].indexOf()", "-1");
105 shouldBe("[0, , 2, 3].indexOf('peekaboo')", "1");
106 shouldBe("[0, undefined, 2, 3].indexOf()", "1");
107 shouldBe("[0, , 2, 3].lastIndexOf()", "-1");
108 shouldBe("[0, , 2, 3].lastIndexOf('peekaboo')", "1");
109 shouldBe("[0, undefined, 2, 3].lastIndexOf()", "1");
110
111 delete Object.prototype[1];