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 |
(...skipping 12 matching lines...) Expand all Loading... |
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 | 30 // ArrayBuffer |
31 | 31 |
32 function TestByteLength(param, expectedByteLength) { | 32 function TestByteLength(param, expectedByteLength) { |
33 var ab = new __ArrayBuffer(param); | 33 var ab = new ArrayBuffer(param); |
34 assertSame(expectedByteLength, ab.byteLength); | 34 assertSame(expectedByteLength, ab.byteLength); |
35 } | 35 } |
36 | 36 |
37 function TestArrayBufferCreation() { | 37 function TestArrayBufferCreation() { |
38 TestByteLength(1, 1); | 38 TestByteLength(1, 1); |
39 TestByteLength(256, 256); | 39 TestByteLength(256, 256); |
40 TestByteLength(-10, 0); | 40 TestByteLength(-10, 0); |
41 TestByteLength(2.567, 2); | 41 TestByteLength(2.567, 2); |
42 TestByteLength(-2.567, 0); | 42 TestByteLength(-2.567, 0); |
43 | 43 |
44 TestByteLength("abc", 0); | 44 TestByteLength("abc", 0); |
45 | 45 |
46 TestByteLength(0, 0); | 46 TestByteLength(0, 0); |
47 | 47 |
48 /* TODO[dslomov]: Reenable the test | 48 /* TODO[dslomov]: Reenable the test |
49 assertThrows(function() { | 49 assertThrows(function() { |
50 var ab1 = new __ArrayBuffer(0xFFFFFFFFFFFF) | 50 var ab1 = new ArrayBuffer(0xFFFFFFFFFFFF) |
51 }, RangeError); | 51 }, RangeError); |
52 */ | 52 */ |
53 | 53 |
54 var ab = new __ArrayBuffer(); | 54 var ab = new ArrayBuffer(); |
55 assertSame(0, ab.byteLength); | 55 assertSame(0, ab.byteLength); |
56 } | 56 } |
57 | 57 |
58 TestArrayBufferCreation(); | 58 TestArrayBufferCreation(); |
59 | 59 |
60 function TestByteLengthNotWritable() { | 60 function TestByteLengthNotWritable() { |
61 var ab = new __ArrayBuffer(1024); | 61 var ab = new ArrayBuffer(1024); |
62 assertSame(1024, ab.byteLength); | 62 assertSame(1024, ab.byteLength); |
63 | 63 |
64 assertThrows(function() { "use strict"; ab.byteLength = 42; }, TypeError); | 64 assertThrows(function() { "use strict"; ab.byteLength = 42; }, TypeError); |
65 } | 65 } |
66 | 66 |
67 TestByteLengthNotWritable(); | 67 TestByteLengthNotWritable(); |
68 | 68 |
69 function TestSlice(expectedResultLen, initialLen, start, end) { | 69 function TestSlice(expectedResultLen, initialLen, start, end) { |
70 var ab = new __ArrayBuffer(initialLen); | 70 var ab = new ArrayBuffer(initialLen); |
71 var slice = ab.slice(start, end); | 71 var slice = ab.slice(start, end); |
72 assertSame(expectedResultLen, slice.byteLength); | 72 assertSame(expectedResultLen, slice.byteLength); |
73 } | 73 } |
74 | 74 |
75 function TestArrayBufferSlice() { | 75 function TestArrayBufferSlice() { |
76 var ab = new __ArrayBuffer(1024); | 76 var ab = new ArrayBuffer(1024); |
77 var ab1 = ab.slice(512, 1024); | 77 var ab1 = ab.slice(512, 1024); |
78 assertSame(512, ab1.byteLength); | 78 assertSame(512, ab1.byteLength); |
79 | 79 |
80 TestSlice(512, 1024, 512, 1024); | 80 TestSlice(512, 1024, 512, 1024); |
81 TestSlice(512, 1024, 512); | 81 TestSlice(512, 1024, 512); |
82 | 82 |
83 TestSlice(0, 0, 1, 20); | 83 TestSlice(0, 0, 1, 20); |
84 TestSlice(100, 100, 0, 100); | 84 TestSlice(100, 100, 0, 100); |
85 TestSlice(100, 100, 0, 1000); | 85 TestSlice(100, 100, 0, 1000); |
86 TestSlice(0, 100, 5, 1); | 86 TestSlice(0, 100, 5, 1); |
(...skipping 17 matching lines...) Expand all Loading... |
104 | 104 |
105 TestSlice(10, 100, 90); | 105 TestSlice(10, 100, 90); |
106 TestSlice(10, 100, -10); | 106 TestSlice(10, 100, -10); |
107 } | 107 } |
108 | 108 |
109 TestArrayBufferSlice(); | 109 TestArrayBufferSlice(); |
110 | 110 |
111 // Typed arrays | 111 // Typed arrays |
112 | 112 |
113 function TestTypedArray(proto, elementSize, typicalElement) { | 113 function TestTypedArray(proto, elementSize, typicalElement) { |
114 var ab = new __ArrayBuffer(256*elementSize); | 114 var ab = new ArrayBuffer(256*elementSize); |
115 | 115 |
116 var a1 = new proto(ab, 128*elementSize, 128); | 116 var a1 = new proto(ab, 128*elementSize, 128); |
117 assertSame(ab, a1.buffer); | 117 assertSame(ab, a1.buffer); |
118 assertSame(elementSize, a1.BYTES_PER_ELEMENT); | 118 assertSame(elementSize, a1.BYTES_PER_ELEMENT); |
119 assertSame(128, a1.length); | 119 assertSame(128, a1.length); |
120 assertSame(128*elementSize, a1.byteLength); | 120 assertSame(128*elementSize, a1.byteLength); |
121 assertSame(128*elementSize, a1.byteOffset); | 121 assertSame(128*elementSize, a1.byteOffset); |
122 | 122 |
123 | 123 |
124 var a2 = new proto(ab, 64*elementSize, 128); | 124 var a2 = new proto(ab, 64*elementSize, 128); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 168 |
169 for (i = 128; i < 256; i++) { | 169 for (i = 128; i < 256; i++) { |
170 assertSame(typicalElement, a4[i]); | 170 assertSame(typicalElement, a4[i]); |
171 } | 171 } |
172 | 172 |
173 assertThrows(function () { new proto(ab, 256*elementSize); }, RangeError); | 173 assertThrows(function () { new proto(ab, 256*elementSize); }, RangeError); |
174 | 174 |
175 if (elementSize !== 1) { | 175 if (elementSize !== 1) { |
176 assertThrows(function() { new proto(ab, 128*elementSize - 1, 10); }, | 176 assertThrows(function() { new proto(ab, 128*elementSize - 1, 10); }, |
177 RangeError); | 177 RangeError); |
178 var unalignedArrayBuffer = new __ArrayBuffer(10*elementSize + 1); | 178 var unalignedArrayBuffer = new ArrayBuffer(10*elementSize + 1); |
179 var goodArray = new proto(unalignedArrayBuffer, 0, 10); | 179 var goodArray = new proto(unalignedArrayBuffer, 0, 10); |
180 assertSame(10, goodArray.length); | 180 assertSame(10, goodArray.length); |
181 assertSame(10*elementSize, goodArray.byteLength); | 181 assertSame(10*elementSize, goodArray.byteLength); |
182 assertThrows(function() { new proto(unalignedArrayBuffer)}, RangeError); | 182 assertThrows(function() { new proto(unalignedArrayBuffer)}, RangeError); |
183 assertThrows(function() { new proto(unalignedArrayBuffer, 5*elementSize)}, | 183 assertThrows(function() { new proto(unalignedArrayBuffer, 5*elementSize)}, |
184 RangeError); | 184 RangeError); |
185 } | 185 } |
186 | 186 |
187 } | 187 } |
188 | 188 |
189 TestTypedArray(__Uint8Array, 1, 0xFF); | 189 TestTypedArray(Uint8Array, 1, 0xFF); |
190 TestTypedArray(__Int8Array, 1, -0x7F); | 190 TestTypedArray(Int8Array, 1, -0x7F); |
191 TestTypedArray(__Uint16Array, 2, 0xFFFF); | 191 TestTypedArray(Uint16Array, 2, 0xFFFF); |
192 TestTypedArray(__Int16Array, 2, -0x7FFF); | 192 TestTypedArray(Int16Array, 2, -0x7FFF); |
193 TestTypedArray(__Uint32Array, 4, 0xFFFFFFFF); | 193 TestTypedArray(Uint32Array, 4, 0xFFFFFFFF); |
194 TestTypedArray(__Int32Array, 4, -0x7FFFFFFF); | 194 TestTypedArray(Int32Array, 4, -0x7FFFFFFF); |
195 TestTypedArray(__Float32Array, 4, 0.5); | 195 TestTypedArray(Float32Array, 4, 0.5); |
196 TestTypedArray(__Float64Array, 8, 0.5); | 196 TestTypedArray(Float64Array, 8, 0.5); |
197 | 197 |
198 | 198 |
199 // General tests for properties | 199 // General tests for properties |
200 | 200 |
201 // Test property attribute [[Enumerable]] | 201 // Test property attribute [[Enumerable]] |
202 function TestEnumerable(func, obj) { | 202 function TestEnumerable(func, obj) { |
203 function props(x) { | 203 function props(x) { |
204 var array = []; | 204 var array = []; |
205 for (var p in x) array.push(p); | 205 for (var p in x) array.push(p); |
206 return array.sort(); | 206 return array.sort(); |
207 } | 207 } |
208 assertArrayEquals([], props(func)); | 208 assertArrayEquals([], props(func)); |
209 assertArrayEquals([], props(func.prototype)); | 209 assertArrayEquals([], props(func.prototype)); |
210 if (obj) | 210 if (obj) |
211 assertArrayEquals([], props(obj)); | 211 assertArrayEquals([], props(obj)); |
212 } | 212 } |
213 TestEnumerable(__ArrayBuffer, new __ArrayBuffer()); | 213 TestEnumerable(ArrayBuffer, new ArrayBuffer()); |
214 TestEnumerable(__Uint8Array); | 214 TestEnumerable(Uint8Array); |
215 | 215 |
216 | 216 |
217 // Test arbitrary properties on ArrayBuffer | 217 // Test arbitrary properties on ArrayBuffer |
218 function TestArbitrary(m) { | 218 function TestArbitrary(m) { |
219 function TestProperty(map, property, value) { | 219 function TestProperty(map, property, value) { |
220 map[property] = value; | 220 map[property] = value; |
221 assertEquals(value, map[property]); | 221 assertEquals(value, map[property]); |
222 } | 222 } |
223 for (var i = 0; i < 20; i++) { | 223 for (var i = 0; i < 20; i++) { |
224 TestProperty(m, i, 'val' + i); | 224 TestProperty(m, i, 'val' + i); |
225 TestProperty(m, 'foo' + i, 'bar' + i); | 225 TestProperty(m, 'foo' + i, 'bar' + i); |
226 } | 226 } |
227 } | 227 } |
228 TestArbitrary(new __ArrayBuffer(256)); | 228 TestArbitrary(new ArrayBuffer(256)); |
229 | 229 |
230 // Test direct constructor call | 230 // Test direct constructor call |
231 assertTrue(__ArrayBuffer() instanceof __ArrayBuffer); | 231 assertTrue(ArrayBuffer() instanceof ArrayBuffer); |
OLD | NEW |