test/script/basic/NASHORN-377.js

Wed, 22 May 2013 19:33:08 +0530

author
sundar
date
Wed, 22 May 2013 19:33:08 +0530
changeset 284
66685c69bdb3
parent 7
5a1b0714df0e
child 952
6d5471a497fb
child 962
ac62e33a99b0
permissions
-rw-r--r--

8014735: Typed Array, BYTES_PER_ELEMENT should be a class property
Reviewed-by: lagergren, jlaskey

jlaskey@3 1 /*
jlaskey@7 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
jlaskey@3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jlaskey@3 4 *
jlaskey@3 5 * This code is free software; you can redistribute it and/or modify it
jlaskey@3 6 * under the terms of the GNU General Public License version 2 only, as
jlaskey@3 7 * published by the Free Software Foundation.
jlaskey@3 8 *
jlaskey@3 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jlaskey@3 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlaskey@3 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jlaskey@3 12 * version 2 for more details (a copy is included in the LICENSE file that
jlaskey@3 13 * accompanied this code).
jlaskey@3 14 *
jlaskey@3 15 * You should have received a copy of the GNU General Public License version
jlaskey@3 16 * 2 along with this work; if not, write to the Free Software Foundation,
jlaskey@3 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jlaskey@3 18 *
jlaskey@3 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jlaskey@3 20 * or visit www.oracle.com if you need additional information or have any
jlaskey@3 21 * questions.
jlaskey@3 22 */
jlaskey@3 23
jlaskey@3 24 /*
jlaskey@3 25 * NASHORN-377: Typed arrays.
jlaskey@3 26 *
jlaskey@3 27 * @test
jlaskey@3 28 * @run
jlaskey@3 29 */
jlaskey@3 30
jlaskey@3 31 var types = [Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array];
jlaskey@3 32
jlaskey@3 33 //---------------------------------------------------------------------------
jlaskey@3 34 // utility functions
jlaskey@3 35 //---------------------------------------------------------------------------
jlaskey@3 36 function tohex(d, w) {
jlaskey@3 37 var hex = Number(d).toString(16);
jlaskey@3 38 var pad = (w ? w : 8) - hex.length;
jlaskey@3 39 hex = "00000000".substr(0, pad) + hex;
jlaskey@3 40 return hex;
jlaskey@3 41 }
jlaskey@3 42
jlaskey@3 43 function arrstr(a, n, w) {
jlaskey@3 44 var s = "";
jlaskey@3 45 if (typeof n == "undefined") n = a.length;
sundar@284 46 if (typeof w == "undefined") w = a.constructor.BYTES_PER_ELEMENT * 2;
jlaskey@3 47 for (var i = 0; i < n; i++) {
jlaskey@3 48 s += tohex(a[i], w);
jlaskey@3 49 }
jlaskey@3 50 return s;
jlaskey@3 51 }
jlaskey@3 52 function bufstr(b) {
jlaskey@3 53 if (b.buffer !== undefined) {
jlaskey@3 54 b = b.buffer;
jlaskey@3 55 }
jlaskey@3 56 return arrstr(new Uint8Array(b));
jlaskey@3 57 }
jlaskey@3 58
jlaskey@3 59 function assertFail(f) {
jlaskey@3 60 try {
jlaskey@3 61 f();
jlaskey@3 62 } catch (e) {
jlaskey@3 63 //print(e);
jlaskey@3 64 return;
jlaskey@3 65 }
jlaskey@3 66 throw "assertion failed: expected exception";
jlaskey@3 67 }
jlaskey@3 68
jlaskey@3 69 function assertTrue(f) {
jlaskey@3 70 if (f() !== true) throw "assertion failed: " + f;
jlaskey@3 71 }
jlaskey@3 72
jlaskey@3 73 function isUndefined(x) {
jlaskey@3 74 return typeof x === "undefined";
jlaskey@3 75 }
jlaskey@3 76
jlaskey@3 77 function fillArray(a, start) {
jlaskey@3 78 if (typeof start == "undefined") start = 1;
jlaskey@3 79 for (var i = 0; i < a.length; i++) {
jlaskey@3 80 a[i] = i + start;
jlaskey@3 81 }
jlaskey@3 82 return a;
jlaskey@3 83 }
jlaskey@3 84
jlaskey@3 85 //---------------------------------------------------------------------------
jlaskey@3 86 // tests
jlaskey@3 87 //---------------------------------------------------------------------------
jlaskey@3 88 (function() {
jlaskey@3 89 var b = new ArrayBuffer(8);
jlaskey@3 90 var i8 = new Int8Array(b);
jlaskey@3 91 print(i8.buffer.byteLength, b.byteLength, i8.buffer === b, b.length);
jlaskey@3 92 print(b, i8.buffer, i8);
jlaskey@3 93 })();
jlaskey@3 94
jlaskey@3 95 (function test_attributes() {
jlaskey@3 96 var b = new ArrayBuffer(8);
jlaskey@3 97 for (var i in types) {
jlaskey@3 98 var x = new types[i](b);
sundar@284 99 print(x.byteOffset, x.byteLength, x.length, x.constructor.BYTES_PER_ELEMENT);
jlaskey@3 100 assertTrue(function(){ return x.constructor === types[i] });
jlaskey@3 101 }
jlaskey@3 102 })();
jlaskey@3 103
jlaskey@3 104 (function() {
jlaskey@3 105 var b = new ArrayBuffer(8);
jlaskey@3 106 var i8 = new Int8Array(b);
jlaskey@3 107 fillArray(i8, 0x70);
jlaskey@3 108
jlaskey@3 109 var i8_2 = new Int8Array(b, 2);
jlaskey@3 110 var i8_2_4 = new Uint8Array(b, 2, 4);
jlaskey@3 111
jlaskey@3 112 i8_2_4[3] = 0x80;
jlaskey@3 113
jlaskey@3 114 print(arrstr(i8, 8, 2) + " " + bufstr(i8));
jlaskey@3 115 print(arrstr(i8_2, 6) + " " + i8_2.byteOffset + " " + i8_2.byteLength);
jlaskey@3 116 print(arrstr(i8_2_4, 4) + " " + i8_2_4.byteOffset + " " + i8_2_4.byteLength);
jlaskey@3 117
jlaskey@3 118 var i8_1_5 = i8.subarray(1, 5);
jlaskey@3 119 i8_2_4.subarray(1, 5);
jlaskey@3 120 print(arrstr(i8_1_5, 4) + " " + i8_1_5.byteOffset + " " + i8_1_5.byteLength);
jlaskey@3 121
jlaskey@3 122 print(bufstr(b.slice(1,7)));
jlaskey@3 123 })();
jlaskey@3 124
jlaskey@3 125 (function() {
jlaskey@3 126 var b = new ArrayBuffer(8);
jlaskey@3 127 fillArray(new Int8Array(b), 0x70);
jlaskey@3 128 new Int8Array(b)[5] = 0x80;
jlaskey@3 129
jlaskey@3 130 var i32 = new Int32Array(b);
jlaskey@3 131 var u32 = new Uint32Array(b);
jlaskey@3 132 print(arrstr(i32), i32[0], i32[1]);
jlaskey@3 133 i32[1] = 0xfefdfcfb;
jlaskey@3 134 print(arrstr(i32), i32[0], i32[1]);
jlaskey@3 135 print(arrstr(u32), u32[0], u32[1]);
jlaskey@3 136
jlaskey@3 137 var pi = 3.1415926;
jlaskey@3 138 var f32 = new Float32Array(b);
jlaskey@3 139 var f64 = new Float64Array(b);
jlaskey@3 140 f32[0] = pi;
jlaskey@3 141 print(bufstr(b), f32.length);
jlaskey@3 142 f64[0] = pi;
jlaskey@3 143 print(bufstr(b), f64.length);
jlaskey@3 144 print(arrstr(u32), u32[0], u32[1]);
jlaskey@3 145
jlaskey@3 146 var d = new Int32Array(3);
jlaskey@3 147 d.set(i32,1);
jlaskey@3 148 print(bufstr(d));
jlaskey@3 149
jlaskey@3 150 var s = new Int16Array(b);
jlaskey@3 151 var t = new Uint16Array(b);
jlaskey@3 152 print(arrstr(s), arrstr(t));
jlaskey@3 153 s[0] = -1; s[1] = 0x80;
jlaskey@3 154 print(arrstr(s), arrstr(t));
jlaskey@3 155 })();
jlaskey@3 156
jlaskey@3 157 (function enumerate_properties() {
jlaskey@3 158 var i8 = new Int8Array(new ArrayBuffer(8));
jlaskey@3 159 var s = ""; for (var i in i8) { s += i + " "; } print(s.trim());
jlaskey@3 160 })();
jlaskey@3 161
jlaskey@3 162 // check that ScriptObject fallback is still working
jlaskey@3 163 // DISABLED because correct behavior is unclear
jlaskey@3 164 (function() {
jlaskey@3 165 // NB: firefox will never set any out-of-bounds or non-array values although it does get both from prototype.
jlaskey@3 166 var z = new Uint8Array(4);
jlaskey@3 167 z["asdf"] = "asdf"; print(z["asdf"]);
jlaskey@3 168 z[0x100000000] = "asdf"; print(z[0x100000000]);
jlaskey@3 169 z[-1] = "asdf"; print(z[-1]);
jlaskey@3 170
jlaskey@3 171 // v8 and nashorn disagree on out-of-bounds uint32 indices: v8 won't go to the prototype.
jlaskey@3 172 z[0xf0000000] = "asdf"; print(z[0xf0000000]);
jlaskey@3 173 z[0xffffffff] = "asdf"; print(z[0xffffffff]);
jlaskey@3 174 z[0x70000000] = "asdf"; print(z[0x70000000]);
jlaskey@3 175
jlaskey@3 176 // this will work in firefox and nashorn (not in v8).
jlaskey@3 177 Uint8Array.prototype[4] = "asdf"; print(z[4]);
jlaskey@3 178 });
jlaskey@3 179
jlaskey@3 180 (function test_exceptions() {
jlaskey@3 181 assertFail(function() { new Int32Array(new ArrayBuffer(7)); });
jlaskey@3 182 assertFail(function() { new Int32Array(new ArrayBuffer(8), 0, 4); });
jlaskey@3 183 assertFail(function() { new Int32Array(new ArrayBuffer(8),-1, 2); });
jlaskey@3 184 assertFail(function() { new Int32Array(new ArrayBuffer(8), 0,-1); });
jlaskey@3 185 })();
jlaskey@3 186
jlaskey@3 187 (function test_subarray() {
jlaskey@3 188 var x = fillArray(new Int8Array(8));
jlaskey@3 189 print(arrstr(x));
jlaskey@3 190 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 191 print(arrstr(x.subarray(-10, -2))); // negative index clamped to 0
jlaskey@3 192 assertTrue(function(){ return arrstr(x.subarray(6, 4)) === ""; }); // negative length clamped to 0
jlaskey@3 193 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 194 })();
jlaskey@3 195
jlaskey@3 196 (function test_slice() {
jlaskey@3 197 var b = ArrayBuffer(16);
jlaskey@3 198 fillArray(new Int8Array(b));
jlaskey@3 199 print(bufstr(b));
jlaskey@3 200 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 201 print(bufstr(b.slice(-20, -4))); // negative index clamped to 0
jlaskey@3 202 assertTrue(function(){ return bufstr(b.slice(8, 4)) === ""; }); // negative length clamped to 0
jlaskey@3 203 print(arrstr(new Int16Array(b.slice(1,-1).slice(2,-1).slice(1,-2).slice(1,-1)))); // slice of slice
jlaskey@3 204 })();
jlaskey@3 205
jlaskey@3 206 (function test_clamped() {
jlaskey@3 207 var a = new Uint8ClampedArray(10);
jlaskey@3 208 a[0] = -17; // clamped to 0
jlaskey@3 209 a[1] = 4711; // clamped to 255
jlaskey@3 210 a[2] = 17.5; // clamped to 18
jlaskey@3 211 a[3] = 16.5; // clamped to 16
jlaskey@3 212 a[4] = 255.9; // clamped to 255
jlaskey@3 213 a[5] = Infinity; // clamped to 255
jlaskey@3 214 a[6] = -Infinity; // clamped to 0
jlaskey@3 215 a[7] = NaN; // 0
jlaskey@3 216 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 217 })();
jlaskey@3 218
jlaskey@3 219 (function test_out_of_bounds() {
jlaskey@3 220 var a = new Int32Array(10);
jlaskey@3 221 a[10] = 10;
jlaskey@3 222 a[100] = 100;
jlaskey@3 223 a[1000] = 1000;
jlaskey@3 224 assertTrue(function(){ return isUndefined(a[10]) && isUndefined(a[11]) && isUndefined(a[100]) && isUndefined(a[123]) && isUndefined(a[1000]); });
jlaskey@3 225 })();
jlaskey@3 226

mercurial