The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / test / mjsunit / array-concat.js @ 40c0f755

History | View | Annotate | Download (4.25 KB)

1
// Copyright 2008 the V8 project authors. All rights reserved.
2
// Redistribution and use in source and binary forms, with or without
3
// modification, are permitted provided that the following conditions are
4
// met:
5
//
6
//     * Redistributions of source code must retain the above copyright
7
//       notice, this list of conditions and the following disclaimer.
8
//     * Redistributions in binary form must reproduce the above
9
//       copyright notice, this list of conditions and the following
10
//       disclaimer in the documentation and/or other materials provided
11
//       with the distribution.
12
//     * Neither the name of Google Inc. nor the names of its
13
//       contributors may be used to endorse or promote products derived
14
//       from this software without specific prior written permission.
15
//
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27

    
28
/**
29
 * @fileoverview Test concat on small and large arrays
30
 */
31

    
32
var poses = [140, 4000000000];
33
while (pos = poses.shift()) {
34
  var a = new Array(pos);
35
  assertEquals(pos, a.length);
36
  a.push('foo');
37
  assertEquals(pos + 1, a.length);
38
  var b = ['bar'];
39
  var c = a.concat(b);
40
  assertEquals(pos + 2, c.length);
41
  assertEquals("undefined", typeof(c[pos - 1]));
42
  assertEquals("foo", c[pos]);
43
  assertEquals("bar", c[pos + 1]);
44

    
45
  // Can we fool the system by putting a number in a string?
46
  var onetwofour = "124";
47
  a[onetwofour] = 'doo';
48
  assertEquals(a[124], 'doo');
49
  c = a.concat(b);
50
  assertEquals(c[124], 'doo');
51

    
52
  // If we put a number in the prototype, then the spec says it should be
53
  // copied on concat.
54
  Array.prototype["123"] = 'baz';
55
  assertEquals(a[123], 'baz');
56

    
57
  c = a.concat(b);
58
  assertEquals(pos + 2, c.length);
59
  assertEquals("baz", c[123]);
60
  assertEquals("undefined", typeof(c[pos - 1]));
61
  assertEquals("foo", c[pos]);
62
  assertEquals("bar", c[pos + 1]);
63

    
64
  // When we take the number off the prototype it disappears from a, but
65
  // the concat put it in c itself.
66
  Array.prototype["123"] = undefined;
67
  assertEquals("undefined", typeof(a[123]));
68
  assertEquals("baz", c[123]);
69

    
70
  // If the element of prototype is shadowed, the element on the instance
71
  // should be copied, but not the one on the prototype.
72
  Array.prototype[123] = 'baz';
73
  a[123] = 'xyz';
74
  assertEquals('xyz', a[123]);
75
  c = a.concat(b);
76
  assertEquals('xyz', c[123]);
77

    
78
  // Non-numeric properties on the prototype or the array shouldn't get
79
  // copied.
80
  Array.prototype.moe = 'joe';
81
  a.ben = 'jerry';
82
  assertEquals(a["moe"], 'joe');
83
  assertEquals(a["ben"], 'jerry');
84
  c = a.concat(b);
85
  // ben was not copied
86
  assertEquals("undefined", typeof(c.ben));
87
  // moe was not copied, but we can see it through the prototype
88
  assertEquals("joe", c.moe);
89

    
90
  // When we take moe off the prototype it disappears from all arrays.
91
  Array.prototype.moe = undefined;
92
  assertEquals("undefined", typeof(c.moe));
93

    
94
  // Negative indeces don't get concated.
95
  a[-1] = 'minus1';
96
  assertEquals("minus1", a[-1]);
97
  assertEquals("undefined", typeof(a[0xffffffff]));
98
  c = a.concat(b);
99
  assertEquals("undefined", typeof(c[-1]));
100
  assertEquals("undefined", typeof(c[0xffffffff]));
101
  assertEquals(c.length, a.length + 1);
102

    
103
}
104

    
105
a = [];
106
c = a.concat('Hello');
107
assertEquals(1, c.length);
108
assertEquals("Hello", c[0]);
109
assertEquals("Hello", c.toString());
110

    
111
// Check that concat preserves holes.
112
var holey = [void 0,'a',,'c'].concat(['d',,'f',[0,,2],void 0])
113
assertEquals(9, holey.length);  // hole in embedded array is ignored
114
for (var i = 0; i < holey.length; i++) {
115
  if (i == 2 || i == 5) {
116
    assertFalse(i in holey);
117
  } else {
118
    assertTrue(i in holey);
119
  }
120
}