jlaskey@3: /* jlaskey@7: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. jlaskey@3: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jlaskey@3: * jlaskey@3: * This code is free software; you can redistribute it and/or modify it jlaskey@3: * under the terms of the GNU General Public License version 2 only, as jlaskey@3: * published by the Free Software Foundation. jlaskey@3: * jlaskey@3: * This code is distributed in the hope that it will be useful, but WITHOUT jlaskey@3: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jlaskey@3: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jlaskey@3: * version 2 for more details (a copy is included in the LICENSE file that jlaskey@3: * accompanied this code). jlaskey@3: * jlaskey@3: * You should have received a copy of the GNU General Public License version jlaskey@3: * 2 along with this work; if not, write to the Free Software Foundation, jlaskey@3: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jlaskey@3: * jlaskey@3: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jlaskey@3: * or visit www.oracle.com if you need additional information or have any jlaskey@3: * questions. jlaskey@3: */ jlaskey@3: jlaskey@3: /* jlaskey@3: * NASHORN-377: Typed arrays. jlaskey@3: * jlaskey@3: * @test jlaskey@3: * @run jlaskey@3: */ jlaskey@3: jlaskey@3: var types = [Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array]; jlaskey@3: jlaskey@3: //--------------------------------------------------------------------------- jlaskey@3: // utility functions jlaskey@3: //--------------------------------------------------------------------------- jlaskey@3: function tohex(d, w) { jlaskey@3: var hex = Number(d).toString(16); jlaskey@3: var pad = (w ? w : 8) - hex.length; jlaskey@3: hex = "00000000".substr(0, pad) + hex; jlaskey@3: return hex; jlaskey@3: } jlaskey@3: jlaskey@3: function arrstr(a, n, w) { jlaskey@3: var s = ""; jlaskey@3: if (typeof n == "undefined") n = a.length; sundar@284: if (typeof w == "undefined") w = a.constructor.BYTES_PER_ELEMENT * 2; jlaskey@3: for (var i = 0; i < n; i++) { jlaskey@3: s += tohex(a[i], w); jlaskey@3: } jlaskey@3: return s; jlaskey@3: } jlaskey@3: function bufstr(b) { jlaskey@3: if (b.buffer !== undefined) { jlaskey@3: b = b.buffer; jlaskey@3: } jlaskey@3: return arrstr(new Uint8Array(b)); jlaskey@3: } jlaskey@3: jlaskey@3: function assertFail(f) { jlaskey@3: try { jlaskey@3: f(); jlaskey@3: } catch (e) { jlaskey@3: //print(e); jlaskey@3: return; jlaskey@3: } jlaskey@3: throw "assertion failed: expected exception"; jlaskey@3: } jlaskey@3: jlaskey@3: function assertTrue(f) { jlaskey@3: if (f() !== true) throw "assertion failed: " + f; jlaskey@3: } jlaskey@3: jlaskey@3: function isUndefined(x) { jlaskey@3: return typeof x === "undefined"; jlaskey@3: } jlaskey@3: jlaskey@3: function fillArray(a, start) { jlaskey@3: if (typeof start == "undefined") start = 1; jlaskey@3: for (var i = 0; i < a.length; i++) { jlaskey@3: a[i] = i + start; jlaskey@3: } jlaskey@3: return a; jlaskey@3: } jlaskey@3: jlaskey@3: //--------------------------------------------------------------------------- jlaskey@3: // tests jlaskey@3: //--------------------------------------------------------------------------- jlaskey@3: (function() { jlaskey@3: var b = new ArrayBuffer(8); jlaskey@3: var i8 = new Int8Array(b); jlaskey@3: print(i8.buffer.byteLength, b.byteLength, i8.buffer === b, b.length); jlaskey@3: print(b, i8.buffer, i8); jlaskey@3: })(); jlaskey@3: jlaskey@3: (function test_attributes() { jlaskey@3: var b = new ArrayBuffer(8); jlaskey@3: for (var i in types) { jlaskey@3: var x = new types[i](b); sundar@284: print(x.byteOffset, x.byteLength, x.length, x.constructor.BYTES_PER_ELEMENT); jlaskey@3: assertTrue(function(){ return x.constructor === types[i] }); jlaskey@3: } jlaskey@3: })(); jlaskey@3: jlaskey@3: (function() { jlaskey@3: var b = new ArrayBuffer(8); jlaskey@3: var i8 = new Int8Array(b); jlaskey@3: fillArray(i8, 0x70); jlaskey@3: jlaskey@3: var i8_2 = new Int8Array(b, 2); jlaskey@3: var i8_2_4 = new Uint8Array(b, 2, 4); jlaskey@3: jlaskey@3: i8_2_4[3] = 0x80; jlaskey@3: jlaskey@3: print(arrstr(i8, 8, 2) + " " + bufstr(i8)); jlaskey@3: print(arrstr(i8_2, 6) + " " + i8_2.byteOffset + " " + i8_2.byteLength); jlaskey@3: print(arrstr(i8_2_4, 4) + " " + i8_2_4.byteOffset + " " + i8_2_4.byteLength); jlaskey@3: jlaskey@3: var i8_1_5 = i8.subarray(1, 5); jlaskey@3: i8_2_4.subarray(1, 5); jlaskey@3: print(arrstr(i8_1_5, 4) + " " + i8_1_5.byteOffset + " " + i8_1_5.byteLength); jlaskey@3: jlaskey@3: print(bufstr(b.slice(1,7))); jlaskey@3: })(); jlaskey@3: jlaskey@3: (function() { jlaskey@3: var b = new ArrayBuffer(8); jlaskey@3: fillArray(new Int8Array(b), 0x70); jlaskey@3: new Int8Array(b)[5] = 0x80; jlaskey@3: jlaskey@3: var i32 = new Int32Array(b); jlaskey@3: var u32 = new Uint32Array(b); jlaskey@3: print(arrstr(i32), i32[0], i32[1]); jlaskey@3: i32[1] = 0xfefdfcfb; jlaskey@3: print(arrstr(i32), i32[0], i32[1]); jlaskey@3: print(arrstr(u32), u32[0], u32[1]); jlaskey@3: jlaskey@3: var pi = 3.1415926; jlaskey@3: var f32 = new Float32Array(b); jlaskey@3: var f64 = new Float64Array(b); jlaskey@3: f32[0] = pi; jlaskey@3: print(bufstr(b), f32.length); jlaskey@3: f64[0] = pi; jlaskey@3: print(bufstr(b), f64.length); jlaskey@3: print(arrstr(u32), u32[0], u32[1]); jlaskey@3: jlaskey@3: var d = new Int32Array(3); jlaskey@3: d.set(i32,1); jlaskey@3: print(bufstr(d)); jlaskey@3: jlaskey@3: var s = new Int16Array(b); jlaskey@3: var t = new Uint16Array(b); jlaskey@3: print(arrstr(s), arrstr(t)); jlaskey@3: s[0] = -1; s[1] = 0x80; jlaskey@3: print(arrstr(s), arrstr(t)); jlaskey@3: })(); jlaskey@3: jlaskey@3: (function enumerate_properties() { jlaskey@3: var i8 = new Int8Array(new ArrayBuffer(8)); jlaskey@3: var s = ""; for (var i in i8) { s += i + " "; } print(s.trim()); jlaskey@3: })(); jlaskey@3: jlaskey@3: // check that ScriptObject fallback is still working jlaskey@3: // DISABLED because correct behavior is unclear jlaskey@3: (function() { jlaskey@3: // NB: firefox will never set any out-of-bounds or non-array values although it does get both from prototype. jlaskey@3: var z = new Uint8Array(4); jlaskey@3: z["asdf"] = "asdf"; print(z["asdf"]); jlaskey@3: z[0x100000000] = "asdf"; print(z[0x100000000]); jlaskey@3: z[-1] = "asdf"; print(z[-1]); jlaskey@3: jlaskey@3: // v8 and nashorn disagree on out-of-bounds uint32 indices: v8 won't go to the prototype. jlaskey@3: z[0xf0000000] = "asdf"; print(z[0xf0000000]); jlaskey@3: z[0xffffffff] = "asdf"; print(z[0xffffffff]); jlaskey@3: z[0x70000000] = "asdf"; print(z[0x70000000]); jlaskey@3: jlaskey@3: // this will work in firefox and nashorn (not in v8). jlaskey@3: Uint8Array.prototype[4] = "asdf"; print(z[4]); jlaskey@3: }); jlaskey@3: jlaskey@3: (function test_exceptions() { jlaskey@3: assertFail(function() { new Int32Array(new ArrayBuffer(7)); }); jlaskey@3: assertFail(function() { new Int32Array(new ArrayBuffer(8), 0, 4); }); jlaskey@3: assertFail(function() { new Int32Array(new ArrayBuffer(8),-1, 2); }); jlaskey@3: assertFail(function() { new Int32Array(new ArrayBuffer(8), 0,-1); }); jlaskey@3: })(); jlaskey@3: jlaskey@3: (function test_subarray() { jlaskey@3: var x = fillArray(new Int8Array(8)); jlaskey@3: print(arrstr(x)); jlaskey@3: print("subarray(2,4)=" + arrstr(x.subarray(2, 4)), "subarray(-6,-4)=" + arrstr(x.subarray(-6, -4))); // negative index refers from the end of the array jlaskey@3: print(arrstr(x.subarray(-10, -2))); // negative index clamped to 0 jlaskey@3: assertTrue(function(){ return arrstr(x.subarray(6, 4)) === ""; }); // negative length clamped to 0 jlaskey@3: print(arrstr(x.subarray(1,-1).subarray(1,-1)), arrstr(x.subarray(1,-1).subarray(1,-1).subarray(1,-1))); // subarray of subarray jlaskey@3: })(); jlaskey@3: jlaskey@3: (function test_slice() { jlaskey@3: var b = ArrayBuffer(16); jlaskey@3: fillArray(new Int8Array(b)); jlaskey@3: print(bufstr(b)); jlaskey@3: print("slice(4,8)=" + bufstr(b.slice(4, 8)), "slice(-8,-4)=" + bufstr(b.slice(-8, -4))); // negative index refers from the end of the array jlaskey@3: print(bufstr(b.slice(-20, -4))); // negative index clamped to 0 jlaskey@3: assertTrue(function(){ return bufstr(b.slice(8, 4)) === ""; }); // negative length clamped to 0 jlaskey@3: print(arrstr(new Int16Array(b.slice(1,-1).slice(2,-1).slice(1,-2).slice(1,-1)))); // slice of slice jlaskey@3: })(); jlaskey@3: jlaskey@3: (function test_clamped() { jlaskey@3: var a = new Uint8ClampedArray(10); jlaskey@3: a[0] = -17; // clamped to 0 jlaskey@3: a[1] = 4711; // clamped to 255 jlaskey@3: a[2] = 17.5; // clamped to 18 jlaskey@3: a[3] = 16.5; // clamped to 16 jlaskey@3: a[4] = 255.9; // clamped to 255 jlaskey@3: a[5] = Infinity; // clamped to 255 jlaskey@3: a[6] = -Infinity; // clamped to 0 jlaskey@3: a[7] = NaN; // 0 jlaskey@3: assertTrue(function(){ return a[0] === 0 && a[1] === 255 && a[2] === 18 && a[3] === 16 && a[4] === 255 && a[5] === 255 && a[6] === 0 && a[7] === 0; }); jlaskey@3: })(); jlaskey@3: jlaskey@3: (function test_out_of_bounds() { jlaskey@3: var a = new Int32Array(10); jlaskey@3: a[10] = 10; jlaskey@3: a[100] = 100; jlaskey@3: a[1000] = 1000; jlaskey@3: assertTrue(function(){ return isUndefined(a[10]) && isUndefined(a[11]) && isUndefined(a[100]) && isUndefined(a[123]) && isUndefined(a[1000]); }); jlaskey@3: })(); jlaskey@3: