OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Flags: --harmony-typed-arrays | 28 // Flags: --harmony-typed-arrays |
29 | 29 |
| 30 // ArrayBuffer |
| 31 |
30 function TestByteLength(param, expectedByteLength) { | 32 function TestByteLength(param, expectedByteLength) { |
31 var ab = new __ArrayBuffer(param); | 33 var ab = new __ArrayBuffer(param); |
32 assertSame(expectedByteLength, ab.byteLength); | 34 assertSame(expectedByteLength, ab.byteLength); |
33 } | 35 } |
34 | 36 |
35 function TestArrayBufferCreation() { | 37 function TestArrayBufferCreation() { |
36 TestByteLength(1, 1); | 38 TestByteLength(1, 1); |
37 TestByteLength(256, 256); | 39 TestByteLength(256, 256); |
38 TestByteLength(-10, 0); | 40 TestByteLength(-10, 0); |
39 TestByteLength(2.567, 2); | 41 TestByteLength(2.567, 2); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 TestSlice(10, 100, 0.01, 10.01); | 99 TestSlice(10, 100, 0.01, 10.01); |
98 TestSlice(10, 100, 0.01, 10.96); | 100 TestSlice(10, 100, 0.01, 10.96); |
99 | 101 |
100 | 102 |
101 TestSlice(10, 100, 90); | 103 TestSlice(10, 100, 90); |
102 TestSlice(10, 100, -10); | 104 TestSlice(10, 100, -10); |
103 } | 105 } |
104 | 106 |
105 TestArrayBufferSlice(); | 107 TestArrayBufferSlice(); |
106 | 108 |
| 109 // Typed arrays |
| 110 |
| 111 function TestTypedArray(proto, elementSize, typicalElement) { |
| 112 var ab = new __ArrayBuffer(256*elementSize); |
| 113 |
| 114 var a1 = new proto(ab, 128*elementSize, 128); |
| 115 assertSame(ab, a1.buffer); |
| 116 assertSame(elementSize, a1.BYTES_PER_ELEMENT); |
| 117 assertSame(128, a1.length); |
| 118 assertSame(128*elementSize, a1.byteLength); |
| 119 assertSame(128*elementSize, a1.byteOffset); |
| 120 |
| 121 |
| 122 var a2 = new proto(ab, 64*elementSize, 128); |
| 123 assertSame(ab, a2.buffer); |
| 124 assertSame(elementSize, a2.BYTES_PER_ELEMENT); |
| 125 assertSame(128, a2.length); |
| 126 assertSame(128*elementSize, a2.byteLength); |
| 127 assertSame(64*elementSize, a2.byteOffset); |
| 128 |
| 129 var a3 = new proto(ab, 192*elementSize); |
| 130 assertSame(ab, a3.buffer); |
| 131 assertSame(64, a3.length); |
| 132 assertSame(64*elementSize, a3.byteLength); |
| 133 assertSame(192*elementSize, a3.byteOffset); |
| 134 |
| 135 var a4 = new proto(ab); |
| 136 assertSame(ab, a4.buffer); |
| 137 assertSame(256, a4.length); |
| 138 assertSame(256*elementSize, a4.byteLength); |
| 139 assertSame(0, a4.byteOffset); |
| 140 |
| 141 |
| 142 var i; |
| 143 for (i = 0; i < 128; i++) { |
| 144 a1[i] = typicalElement; |
| 145 } |
| 146 |
| 147 for (i = 0; i < 128; i++) { |
| 148 assertSame(typicalElement, a1[i]); |
| 149 } |
| 150 |
| 151 for (i = 0; i < 64; i++) { |
| 152 assertSame(0, a2[i]); |
| 153 } |
| 154 |
| 155 for (i = 64; i < 128; i++) { |
| 156 assertSame(typicalElement, a2[i]); |
| 157 } |
| 158 |
| 159 for (i = 0; i < 64; i++) { |
| 160 assertSame(typicalElement, a3[i]); |
| 161 } |
| 162 |
| 163 for (i = 0; i < 128; i++) { |
| 164 assertSame(0, a4[i]); |
| 165 } |
| 166 |
| 167 for (i = 128; i < 256; i++) { |
| 168 assertSame(typicalElement, a4[i]); |
| 169 } |
| 170 |
| 171 assertThrows(function () { new proto(ab, 256*elementSize); }, RangeError); |
| 172 |
| 173 if (elementSize !== 1) { |
| 174 assertThrows(function() { new proto(ab, 128*elementSize - 1, 10); }, |
| 175 RangeError); |
| 176 var unalignedArrayBuffer = new __ArrayBuffer(10*elementSize + 1); |
| 177 var goodArray = new proto(unalignedArrayBuffer, 0, 10); |
| 178 assertSame(10, goodArray.length); |
| 179 assertSame(10*elementSize, goodArray.byteLength); |
| 180 assertThrows(function() { new proto(unalignedArrayBuffer)}, RangeError); |
| 181 assertThrows(function() { new proto(unalignedArrayBuffer, 5*elementSize)}, |
| 182 RangeError); |
| 183 } |
| 184 |
| 185 } |
| 186 |
| 187 TestTypedArray(__Uint8Array, 1, 0xFF); |
| 188 TestTypedArray(__Int8Array, 1, -0x7F); |
| 189 TestTypedArray(__Uint16Array, 2, 0xFFFF); |
| 190 TestTypedArray(__Int16Array, 2, -0x7FFF); |
| 191 TestTypedArray(__Uint32Array, 4, 0xFFFFFFFF); |
| 192 TestTypedArray(__Int32Array, 4, -0x7FFFFFFF); |
| 193 TestTypedArray(__Float32Array, 4, 0.5); |
| 194 TestTypedArray(__Float64Array, 8, 0.5); |
| 195 |
| 196 |
| 197 // General tests for properties |
| 198 |
107 // Test property attribute [[Enumerable]] | 199 // Test property attribute [[Enumerable]] |
108 function TestEnumerable(func) { | 200 function TestEnumerable(func, obj) { |
109 function props(x) { | 201 function props(x) { |
110 var array = []; | 202 var array = []; |
111 for (var p in x) array.push(p); | 203 for (var p in x) array.push(p); |
112 return array.sort(); | 204 return array.sort(); |
113 } | 205 } |
114 assertArrayEquals([], props(func)); | 206 assertArrayEquals([], props(func)); |
115 assertArrayEquals([], props(func.prototype)); | 207 assertArrayEquals([], props(func.prototype)); |
116 assertArrayEquals([], props(new func())); | 208 if (obj) |
| 209 assertArrayEquals([], props(obj)); |
117 } | 210 } |
118 TestEnumerable(__ArrayBuffer); | 211 TestEnumerable(__ArrayBuffer, new __ArrayBuffer()); |
| 212 TestEnumerable(__Uint8Array); |
119 | 213 |
120 | 214 |
121 // Test arbitrary properties on ArrayBuffer | 215 // Test arbitrary properties on ArrayBuffer |
122 function TestArbitrary(m) { | 216 function TestArbitrary(m) { |
123 function TestProperty(map, property, value) { | 217 function TestProperty(map, property, value) { |
124 map[property] = value; | 218 map[property] = value; |
125 assertEquals(value, map[property]); | 219 assertEquals(value, map[property]); |
126 } | 220 } |
127 for (var i = 0; i < 20; i++) { | 221 for (var i = 0; i < 20; i++) { |
128 TestProperty(m, i, 'val' + i); | 222 TestProperty(m, i, 'val' + i); |
129 TestProperty(m, 'foo' + i, 'bar' + i); | 223 TestProperty(m, 'foo' + i, 'bar' + i); |
130 } | 224 } |
131 } | 225 } |
132 TestArbitrary(new __ArrayBuffer(256)); | 226 TestArbitrary(new __ArrayBuffer(256)); |
133 | 227 |
134 | |
135 // Test direct constructor call | 228 // Test direct constructor call |
136 assertTrue(__ArrayBuffer() instanceof __ArrayBuffer); | 229 assertTrue(__ArrayBuffer() instanceof __ArrayBuffer); |
OLD | NEW |