NASHORN-377.js revision 2:da1e581c933b
1124035Sgrog/* 2124035Sgrog * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. 3124035Sgrog * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4124035Sgrog * 5124035Sgrog * This code is free software; you can redistribute it and/or modify it 6124035Sgrog * under the terms of the GNU General Public License version 2 only, as 7124035Sgrog * published by the Free Software Foundation. 8124035Sgrog * 9124035Sgrog * This code is distributed in the hope that it will be useful, but WITHOUT 10124035Sgrog * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11124035Sgrog * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12124035Sgrog * version 2 for more details (a copy is included in the LICENSE file that 13124035Sgrog * accompanied this code). 14124035Sgrog * 15124035Sgrog * You should have received a copy of the GNU General Public License version 16124035Sgrog * 2 along with this work; if not, write to the Free Software Foundation, 17124035Sgrog * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18124035Sgrog * 19124035Sgrog * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20124035Sgrog * or visit www.oracle.com if you need additional information or have any 21124035Sgrog * questions. 22124035Sgrog */ 23124035Sgrog 24124035Sgrog/* 25123980Sgrog * NASHORN-377: Typed arrays. 26124035Sgrog * 27141519Sbrueffer * @test 28123980Sgrog * @run 29123980Sgrog */ 30123980Sgrog 31123980Sgrogvar types = [Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array]; 32123980Sgrog 33123980Sgrog//--------------------------------------------------------------------------- 34124035Sgrog// utility functions 35124035Sgrog//--------------------------------------------------------------------------- 36123980Sgrogfunction tohex(d, w) { 37123980Sgrog var hex = Number(d).toString(16); 38123980Sgrog var pad = (w ? w : 8) - hex.length; 39124035Sgrog hex = "00000000".substr(0, pad) + hex; 40123980Sgrog return hex; 41123980Sgrog} 42123980Sgrog 43124035Sgrogfunction arrstr(a, n, w) { 44124035Sgrog var s = ""; 45124035Sgrog if (typeof n == "undefined") n = a.length; 46123980Sgrog if (typeof w == "undefined") w = a.BYTES_PER_ELEMENT * 2; 47124064Sgrog for (var i = 0; i < n; i++) { 48124064Sgrog s += tohex(a[i], w); 49124064Sgrog } 50124064Sgrog return s; 51123980Sgrog} 52124035Sgrogfunction bufstr(b) { 53124035Sgrog if (b.buffer !== undefined) { 54123980Sgrog b = b.buffer; 55124064Sgrog } 56124064Sgrog return arrstr(new Uint8Array(b)); 57124064Sgrog} 58124064Sgrog 59123980Sgrogfunction assertFail(f) { 60123980Sgrog try { 61123980Sgrog f(); 62123980Sgrog } catch (e) { 63123980Sgrog //print(e); 64123980Sgrog return; 65123980Sgrog } 66123980Sgrog throw "assertion failed: expected exception"; 67123980Sgrog} 68123980Sgrog 69123980Sgrogfunction assertTrue(f) { 70123980Sgrog if (f() !== true) throw "assertion failed: " + f; 71123980Sgrog} 72123980Sgrog 73124035Sgrogfunction isUndefined(x) { 74124035Sgrog return typeof x === "undefined"; 75124035Sgrog} 76123980Sgrog 77123980Sgrogfunction fillArray(a, start) { 78123980Sgrog if (typeof start == "undefined") start = 1; 79124035Sgrog for (var i = 0; i < a.length; i++) { 80124035Sgrog a[i] = i + start; 81124035Sgrog } 82123980Sgrog return a; 83123980Sgrog} 84124064Sgrog 85124035Sgrog//--------------------------------------------------------------------------- 86123980Sgrog// tests 87123980Sgrog//--------------------------------------------------------------------------- 88124035Sgrog(function() { 89124035Sgrog var b = new ArrayBuffer(8); 90123980Sgrog var i8 = new Int8Array(b); 91123980Sgrog print(i8.buffer.byteLength, b.byteLength, i8.buffer === b, b.length); 92123980Sgrog print(b, i8.buffer, i8); 93124035Sgrog})(); 94124035Sgrog 95124035Sgrog(function test_attributes() { 96123980Sgrog var b = new ArrayBuffer(8); 97123980Sgrog for (var i in types) { 98123980Sgrog var x = new types[i](b); 99123980Sgrog print(x.byteOffset, x.byteLength, x.length, x.BYTES_PER_ELEMENT); 100124035Sgrog assertTrue(function(){ return x.constructor === types[i] }); 101123980Sgrog } 102124035Sgrog})(); 103124035Sgrog 104124035Sgrog(function() { 105124064Sgrog var b = new ArrayBuffer(8); 106124234Sgrog var i8 = new Int8Array(b); 107123980Sgrog fillArray(i8, 0x70); 108123980Sgrog 109123980Sgrog var i8_2 = new Int8Array(b, 2); 110124035Sgrog var i8_2_4 = new Uint8Array(b, 2, 4); 111124035Sgrog 112123980Sgrog i8_2_4[3] = 0x80; 113124064Sgrog 114124035Sgrog print(arrstr(i8, 8, 2) + " " + bufstr(i8)); 115124035Sgrog print(arrstr(i8_2, 6) + " " + i8_2.byteOffset + " " + i8_2.byteLength); 116124035Sgrog print(arrstr(i8_2_4, 4) + " " + i8_2_4.byteOffset + " " + i8_2_4.byteLength); 117124035Sgrog 118124035Sgrog var i8_1_5 = i8.subarray(1, 5); 119124010Sgrog i8_2_4.subarray(1, 5); 120123980Sgrog print(arrstr(i8_1_5, 4) + " " + i8_1_5.byteOffset + " " + i8_1_5.byteLength); 121124010Sgrog 122124035Sgrog print(bufstr(b.slice(1,7))); 123123980Sgrog})(); 124124035Sgrog 125123980Sgrog(function() { 126123980Sgrog var b = new ArrayBuffer(8); 127124035Sgrog fillArray(new Int8Array(b), 0x70); 128124064Sgrog new Int8Array(b)[5] = 0x80; 129124035Sgrog 130123980Sgrog var i32 = new Int32Array(b); 131123980Sgrog var u32 = new Uint32Array(b); 132123980Sgrog print(arrstr(i32), i32[0], i32[1]); 133124035Sgrog i32[1] = 0xfefdfcfb; 134124035Sgrog print(arrstr(i32), i32[0], i32[1]); 135123980Sgrog print(arrstr(u32), u32[0], u32[1]); 136124035Sgrog 137123980Sgrog var pi = 3.1415926; 138124010Sgrog var f32 = new Float32Array(b); 139124035Sgrog var f64 = new Float64Array(b); 140124064Sgrog f32[0] = pi; 141123980Sgrog print(bufstr(b), f32.length); 142124010Sgrog f64[0] = pi; 143124035Sgrog print(bufstr(b), f64.length); 144123980Sgrog print(arrstr(u32), u32[0], u32[1]); 145123980Sgrog 146124035Sgrog var d = new Int32Array(3); 147123980Sgrog d.set(i32,1); 148124035Sgrog print(bufstr(d)); 149124035Sgrog 150124035Sgrog var s = new Int16Array(b); 151124035Sgrog var t = new Uint16Array(b); 152124010Sgrog print(arrstr(s), arrstr(t)); 153124035Sgrog s[0] = -1; s[1] = 0x80; 154124010Sgrog print(arrstr(s), arrstr(t)); 155124035Sgrog})(); 156124035Sgrog 157124035Sgrog(function enumerate_properties() { 158124035Sgrog var i8 = new Int8Array(new ArrayBuffer(8)); 159124035Sgrog var s = ""; for (var i in i8) { s += i + " "; } print(s.trim()); 160124035Sgrog})(); 161124035Sgrog 162124035Sgrog// check that ScriptObject fallback is still working 163123980Sgrog// DISABLED because correct behavior is unclear 164123980Sgrog(function() { 165123980Sgrog // NB: firefox will never set any out-of-bounds or non-array values although it does get both from prototype. 166124035Sgrog var z = new Uint8Array(4); 167124010Sgrog z["asdf"] = "asdf"; print(z["asdf"]); 168124035Sgrog z[0x100000000] = "asdf"; print(z[0x100000000]); 169123980Sgrog z[-1] = "asdf"; print(z[-1]); 170123980Sgrog 171123980Sgrog // v8 and nashorn disagree on out-of-bounds uint32 indices: v8 won't go to the prototype. 172124035Sgrog z[0xf0000000] = "asdf"; print(z[0xf0000000]); 173123980Sgrog z[0xffffffff] = "asdf"; print(z[0xffffffff]); 174123980Sgrog z[0x70000000] = "asdf"; print(z[0x70000000]); 175123980Sgrog 176123980Sgrog // this will work in firefox and nashorn (not in v8). 177123980Sgrog Uint8Array.prototype[4] = "asdf"; print(z[4]); 178124035Sgrog}); 179123980Sgrog 180123980Sgrog(function test_exceptions() { 181124035Sgrog assertFail(function() { new Int32Array(new ArrayBuffer(7)); }); 182123980Sgrog assertFail(function() { new Int32Array(new ArrayBuffer(8), 0, 4); }); 183124010Sgrog assertFail(function() { new Int32Array(new ArrayBuffer(8),-1, 2); }); 184124010Sgrog assertFail(function() { new Int32Array(new ArrayBuffer(8), 0,-1); }); 185124035Sgrog})(); 186124010Sgrog 187124035Sgrog(function test_subarray() { 188124035Sgrog var x = fillArray(new Int8Array(8)); 189124035Sgrog print(arrstr(x)); 190124035Sgrog 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 191124010Sgrog print(arrstr(x.subarray(-10, -2))); // negative index clamped to 0 192124010Sgrog assertTrue(function(){ return arrstr(x.subarray(6, 4)) === ""; }); // negative length clamped to 0 193124035Sgrog print(arrstr(x.subarray(1,-1).subarray(1,-1)), arrstr(x.subarray(1,-1).subarray(1,-1).subarray(1,-1))); // subarray of subarray 194123980Sgrog})(); 195123980Sgrog 196124035Sgrog(function test_slice() { 197124035Sgrog var b = ArrayBuffer(16); 198124010Sgrog fillArray(new Int8Array(b)); 199124035Sgrog print(bufstr(b)); 200124035Sgrog 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 201123980Sgrog print(bufstr(b.slice(-20, -4))); // negative index clamped to 0 202123980Sgrog assertTrue(function(){ return bufstr(b.slice(8, 4)) === ""; }); // negative length clamped to 0 203123980Sgrog print(arrstr(new Int16Array(b.slice(1,-1).slice(2,-1).slice(1,-2).slice(1,-1)))); // slice of slice 204123980Sgrog})(); 205123980Sgrog 206123980Sgrog(function test_clamped() { 207123980Sgrog var a = new Uint8ClampedArray(10); 208123980Sgrog a[0] = -17; // clamped to 0 209123980Sgrog a[1] = 4711; // clamped to 255 210123980Sgrog a[2] = 17.5; // clamped to 18 211123982Sgrog a[3] = 16.5; // clamped to 16 212124035Sgrog a[4] = 255.9; // clamped to 255 213124035Sgrog a[5] = Infinity; // clamped to 255 214123980Sgrog a[6] = -Infinity; // clamped to 0 215123980Sgrog a[7] = NaN; // 0 216123980Sgrog 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; }); 217123980Sgrog})(); 218123980Sgrog 219123980Sgrog(function test_out_of_bounds() { 220123980Sgrog var a = new Int32Array(10); 221124035Sgrog a[10] = 10; 222123980Sgrog a[100] = 100; 223124035Sgrog a[1000] = 1000; 224123980Sgrog assertTrue(function(){ return isUndefined(a[10]) && isUndefined(a[11]) && isUndefined(a[100]) && isUndefined(a[123]) && isUndefined(a[1000]); }); 225124035Sgrog})(); 226124010Sgrog 227124035Sgrog