github.com/nuvolaris/goja@v0.0.0-20230825100449-967811910c6d/builtin_typedarrays_test.go (about) 1 package goja 2 3 import ( 4 "testing" 5 ) 6 7 /* 8 func TestArrayBufferNew(t *testing.T) { 9 const SCRIPT = ` 10 var b = new ArrayBuffer(16); 11 b.byteLength; 12 ` 13 14 testScript(SCRIPT, intToValue(16), t) 15 } 16 */ 17 18 func TestArrayBufferSetUint32(t *testing.T) { 19 vm := New() 20 b := vm._newArrayBuffer(vm.global.ArrayBufferPrototype, nil) 21 b.data = make([]byte, 4) 22 b.setUint32(0, 0xCAFEBABE, bigEndian) 23 24 i := b.getUint32(0, bigEndian) 25 if i != 0xCAFEBABE { 26 t.Fatal(i) 27 } 28 i = b.getUint32(0, littleEndian) 29 if i != 0xBEBAFECA { 30 t.Fatal(i) 31 } 32 33 b.setUint32(0, 0xBEBAFECA, littleEndian) 34 i = b.getUint32(0, bigEndian) 35 if i != 0xCAFEBABE { 36 t.Fatal(i) 37 } 38 } 39 40 func TestArrayBufferSetInt32(t *testing.T) { 41 vm := New() 42 b := vm._newArrayBuffer(vm.global.ArrayBufferPrototype, nil) 43 b.data = make([]byte, 4) 44 b.setInt32(0, -42, littleEndian) 45 if v := b.getInt32(0, littleEndian); v != -42 { 46 t.Fatal(v) 47 } 48 49 b.setInt32(0, -42, bigEndian) 50 if v := b.getInt32(0, bigEndian); v != -42 { 51 t.Fatal(v) 52 } 53 } 54 55 func TestNewUint8Array(t *testing.T) { 56 const SCRIPT = ` 57 var a = new Uint8Array(1); 58 a[0] = 42; 59 a.byteLength === 1 && a.length === 1 && a[0] === 42; 60 ` 61 62 testScript(SCRIPT, valueTrue, t) 63 } 64 65 func TestNewUint16Array(t *testing.T) { 66 const SCRIPT = ` 67 var a = new Uint16Array(1); 68 a[0] = 42; 69 a.byteLength === 2 && a.length === 1 && a[0] === 42; 70 ` 71 72 testScript(SCRIPT, valueTrue, t) 73 } 74 75 func TestTypedArraysSpeciesConstructor(t *testing.T) { 76 const SCRIPT = ` 77 'use strict'; 78 function MyArray() { 79 var NewTarget = this.__proto__.constructor; 80 return Reflect.construct(Uint16Array, arguments, NewTarget); 81 } 82 MyArray.prototype = Object.create(Uint16Array.prototype, { 83 constructor: { 84 value: MyArray, 85 writable: true, 86 configurable: true 87 } 88 }); 89 var a = new MyArray(1); 90 Object.defineProperty(MyArray, Symbol.species, {value: Uint8Array, configurable: true}); 91 a[0] = 32767; 92 var b = a.filter(function() { 93 return true; 94 }); 95 if (a[0] !== 32767) { 96 throw new Error("a[0]=" + a[0]); 97 } 98 if (!(b instanceof Uint8Array)) { 99 throw new Error("b instanceof Uint8Array"); 100 } 101 if (b[0] != 255) { 102 throw new Error("b[0]=" + b[0]); 103 } 104 ` 105 106 testScript(SCRIPT, _undefined, t) 107 } 108 109 func TestTypedArrayFromArrayBuffer(t *testing.T) { 110 const SCRIPT = ` 111 var buf = new ArrayBuffer(2); 112 var a16 = new Uint16Array(buf); 113 if (!(a16 instanceof Uint16Array)) { 114 throw new Error("a16 is not an instance"); 115 } 116 if (a16.buffer !== buf) { 117 throw new Error("a16.buffer !== buf"); 118 } 119 if (a16.length !== 1) { 120 throw new Error("a16.length=" + a16.length); 121 } 122 var a8 = new Uint8Array(buf); 123 a8.fill(0xAA); 124 if (a16[0] !== 0xAAAA) { 125 throw new Error("a16[0]=" + a16[0]); 126 } 127 ` 128 129 testScript(SCRIPT, _undefined, t) 130 } 131 132 func TestTypedArraySetOverlapDifSize(t *testing.T) { 133 const SCRIPT = ` 134 var buf = new ArrayBuffer(4); 135 var src = new Uint8Array(buf, 1, 2); 136 src[0] = 1; 137 src[1] = 2; 138 var dst = new Uint16Array(buf); 139 dst.set(src); 140 if (dst[0] !== 1 || dst[1] !== 2) { 141 throw new Error("dst: " + dst.join(",")); 142 } 143 ` 144 testScript(SCRIPT, _undefined, t) 145 } 146 147 func TestTypedArraySetOverlapDifSize2(t *testing.T) { 148 const SCRIPT = ` 149 var buf = new ArrayBuffer(4); 150 var src = new Uint8Array(buf, 0, 2); 151 src[0] = 1; 152 src[1] = 2; 153 var dst = new Uint16Array(buf); 154 dst.set(src); 155 if (dst[0] !== 1 || dst[1] !== 2) { 156 throw new Error("dst: " + dst.join(",")); 157 } 158 ` 159 testScript(SCRIPT, _undefined, t) 160 } 161 162 func TestTypedArraySetOverlapDifSize3(t *testing.T) { 163 const SCRIPT = ` 164 var buf = new ArrayBuffer(8); 165 var src = new Uint8Array(buf, 2, 4); 166 src[0] = 1; 167 src[1] = 2; 168 src[2] = 3; 169 src[3] = 4; 170 var dst = new Uint16Array(buf); 171 dst.set(src); 172 if (dst[0] !== 1 || dst[1] !== 2 || dst[2] !== 3 || dst[3] !== 4) { 173 throw new Error("dst: " + dst.join(",")); 174 } 175 ` 176 testScript(SCRIPT, _undefined, t) 177 } 178 179 func TestTypedArraySetOverlapDifSize4(t *testing.T) { 180 const SCRIPT = ` 181 var buf = new ArrayBuffer(10); 182 var dst = new Uint8Array(buf, 2, 5); 183 var src = new Uint16Array(buf); 184 src[0] = 1; 185 src[1] = 2; 186 src[2] = 3; 187 src[3] = 4; 188 src[4] = 5; 189 dst.set(src); 190 if (dst[0] !== 1 || dst[1] !== 2 || dst[2] !== 3 || dst[3] !== 4 || dst[4] !== 5) { 191 throw new Error("dst: " + dst.join(",")); 192 } 193 ` 194 testScript(SCRIPT, _undefined, t) 195 } 196 197 func TestTypedArraySetNoOverlapDifSizeForward(t *testing.T) { 198 const SCRIPT = ` 199 var buf = new ArrayBuffer(10); 200 var dst = new Uint8Array(buf, 7, 2); 201 var src = new Uint16Array(buf, 0, 2); 202 src[0] = 1; 203 src[1] = 2; 204 dst.set(src); 205 if (dst[0] !== 1 || dst[1] !== 2 || src[0] !== 1 || src[1] !== 2) { 206 throw new Error("dst: " + dst.join(",")); 207 } 208 ` 209 testScript(SCRIPT, _undefined, t) 210 } 211 212 func TestTypedArraySetNoOverlapDifSizeBackward(t *testing.T) { 213 const SCRIPT = ` 214 var buf = new ArrayBuffer(10); 215 var dst = new Uint8Array(buf, 0, 2); 216 var src = new Uint16Array(buf, 6, 2); 217 src[0] = 1; 218 src[1] = 2; 219 dst.set(src); 220 if (dst[0] !== 1 || dst[1] !== 2 || src[0] !== 1 || src[1] !== 2) { 221 throw new Error("dst: " + dst.join(",")); 222 } 223 ` 224 testScript(SCRIPT, _undefined, t) 225 } 226 227 func TestTypedArraySetNoOverlapDifSizeDifBuffers(t *testing.T) { 228 const SCRIPT = ` 229 var dstBuf = new ArrayBuffer(1024); 230 var dst = new Uint8Array(dstBuf, 0, 2); 231 var src = new Uint16Array(2); 232 src[0] = 1; 233 src[1] = 2; 234 dst.set(src); 235 if (dst[0] !== 1 || dst[1] !== 2 || src[0] !== 1 || src[1] !== 2) { 236 throw new Error("dst: " + dst.join(",")); 237 } 238 ` 239 testScript(SCRIPT, _undefined, t) 240 } 241 242 func TestTypedArraySliceSameType(t *testing.T) { 243 const SCRIPT = ` 244 var src = Uint8Array.of(1,2,3,4); 245 var dst = src.slice(1, 3); 246 if (dst.length !== 2 || dst[0] !== 2 || dst[1] !== 3) { 247 throw new Error("dst: " + dst.join(",")); 248 } 249 ` 250 testScript(SCRIPT, _undefined, t) 251 } 252 253 func TestTypedArraySliceDifType(t *testing.T) { 254 const SCRIPT = ` 255 var src = Uint8Array.of(1,2,3,4); 256 Object.defineProperty(Uint8Array, Symbol.species, {value: Uint16Array, configurable: true}); 257 var dst = src.slice(1, 3); 258 if (!(dst instanceof Uint16Array)) { 259 throw new Error("wrong dst type: " + dst); 260 } 261 if (dst.length !== 2 || dst[0] !== 2 || dst[1] !== 3) { 262 throw new Error("dst: " + dst.join(",")); 263 } 264 ` 265 testScript(SCRIPT, _undefined, t) 266 } 267 268 func TestTypedArraySortComparatorReturnValueFloats(t *testing.T) { 269 const SCRIPT = ` 270 var a = Float64Array.of( 271 5.97, 272 9.91, 273 4.13, 274 9.28, 275 3.29 276 ); 277 a.sort( function(a, b) { return a - b; } ); 278 for (var i = 1; i < a.length; i++) { 279 if (a[i] < a[i-1]) { 280 throw new Error("Array is not sorted: " + a); 281 } 282 } 283 ` 284 testScript(SCRIPT, _undefined, t) 285 } 286 287 func TestTypedArraySortComparatorReturnValueNegZero(t *testing.T) { 288 const SCRIPT = ` 289 var a = new Uint8Array([2, 1]); 290 a.sort( function(a, b) { return a > b ? 0 : -0; } ); 291 for (var i = 1; i < a.length; i++) { 292 if (a[i] < a[i-1]) { 293 throw new Error("Array is not sorted: " + a); 294 } 295 } 296 ` 297 testScript(SCRIPT, _undefined, t) 298 } 299 300 func TestInt32ArrayNegativeIndex(t *testing.T) { 301 const SCRIPT = ` 302 new Int32Array()[-1] === undefined; 303 ` 304 305 testScript(SCRIPT, valueTrue, t) 306 } 307 308 func TestTypedArrayDeleteUnconfigurable(t *testing.T) { 309 const SCRIPT = ` 310 try { 311 (function() { 312 'use strict'; 313 delete Uint8Array.prototype.BYTES_PER_ELEMENT; 314 })(); 315 } catch(e) { 316 if (!(e instanceof TypeError)) { 317 throw e; 318 } 319 if (!e.message.startsWith("Cannot delete property")) { 320 throw e; 321 } 322 } 323 ` 324 325 testScript(SCRIPT, _undefined, t) 326 }