2017-06-28 04:33:03 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
const make = require('../make')
|
|
|
|
const random = require('../random')
|
|
|
|
|
|
|
|
class typed extends make {
|
|
|
|
static byte (limit = null) {
|
2017-06-28 21:20:45 +00:00
|
|
|
// [-128, 127]
|
2017-06-28 04:33:03 +00:00
|
|
|
let value = (limit !== null) ? random.number(limit) : random.number(129)
|
|
|
|
value = random.chance(10) ? -value : value
|
2018-05-21 15:50:44 +00:00
|
|
|
return `new Uint8Array([${value}])[0]`
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static octet (limit = null) {
|
2017-06-28 04:33:03 +00:00
|
|
|
// [0, 255]
|
|
|
|
let value = (limit !== null) ? random.number(limit) : random.number(256)
|
2018-05-21 15:50:44 +00:00
|
|
|
return `new Int8Array([${value}])[0]`
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static short (limit = null) {
|
2017-06-28 21:20:45 +00:00
|
|
|
// [-32768, 32767]
|
2017-06-28 04:33:03 +00:00
|
|
|
let value = (limit !== null) ? random.number(limit) : random.number(32769)
|
|
|
|
value = random.chance(10) ? -value : value
|
2018-05-21 15:50:44 +00:00
|
|
|
return `new Int16Array([${value}])[0]`
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsignedShort (limit = null) {
|
2017-06-28 04:33:03 +00:00
|
|
|
// [0, 65535]
|
|
|
|
let value = (limit !== null) ? random.number(limit) : random.number(65535)
|
2018-05-21 15:50:44 +00:00
|
|
|
return `new Uint16Array([${value}])[0]`
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static long (limit = null) {
|
2017-06-28 21:20:45 +00:00
|
|
|
// [-2147483648, 2147483647]
|
2017-06-28 04:33:03 +00:00
|
|
|
let value = (limit !== null) ? random.number(limit) : random.number(2147483649)
|
|
|
|
value = random.chance(10) ? -value : value
|
2018-05-21 15:50:44 +00:00
|
|
|
return `new Int32Array([${value}])[0]`
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsignedLong (limit = null) {
|
2017-06-28 04:33:03 +00:00
|
|
|
// [0, 4294967295]
|
|
|
|
let value = (limit !== null) ? random.number(limit) : random.number(4294967296)
|
2018-05-21 15:50:44 +00:00
|
|
|
return `new Uint32Array([${value}])[0]`
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static float (limit = null) {
|
2017-06-28 04:33:03 +00:00
|
|
|
let base = (limit !== null) ? random.number(limit) : random.number()
|
2017-06-28 19:01:19 +00:00
|
|
|
let value = random.chance(10) ? -(base + random.float()) : (base + random.float())
|
2018-05-21 15:50:44 +00:00
|
|
|
return `new Float32Array([${value}])[0]`
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static unrestrictedFloat (limit = null) {
|
2017-06-28 04:33:03 +00:00
|
|
|
if (random.chance(100)) {
|
|
|
|
return random.pick([NaN, +Infinity, -Infinity])
|
|
|
|
} else {
|
|
|
|
let base = (limit !== null) ? random.number(limit) : random.number()
|
2018-05-21 15:50:44 +00:00
|
|
|
return `new Float32Array([${base + random.float()}])[0]`
|
2017-06-28 04:33:03 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static double (limit = null) {
|
2017-06-28 04:33:03 +00:00
|
|
|
let base = (limit !== null) ? random.number(limit) : random.number()
|
2017-06-28 19:01:19 +00:00
|
|
|
let value = random.chance(10) ? -(base + random.float()) : (base + random.float())
|
2018-05-21 15:50:44 +00:00
|
|
|
return `new Float64Array([${value}])[0]`
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static unrestrictedDouble (limit = null) {
|
2017-06-28 04:33:03 +00:00
|
|
|
if (random.chance(100)) {
|
|
|
|
return random.pick([NaN, +Infinity, -Infinity])
|
|
|
|
} else {
|
|
|
|
let base = (limit !== null) ? random.number(limit) : random.number()
|
2017-06-28 19:01:19 +00:00
|
|
|
let value = random.chance(10) ? -(base + random.float()) : (base + random.float())
|
2018-05-21 15:50:44 +00:00
|
|
|
return `new Float64Array([${value}])[0]`
|
2017-06-28 04:33:03 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static any () {
|
2017-09-13 20:31:04 +00:00
|
|
|
return random.choose([
|
2018-04-06 22:15:55 +00:00
|
|
|
[1, [typed.byte, typed.octet]],
|
|
|
|
[1, [typed.short, typed.unsignedShort]],
|
|
|
|
[1, [typed.long, typed.unsignedLong]],
|
|
|
|
[1, [typed.float, typed.unrestrictedFloat]],
|
2018-08-20 16:21:37 +00:00
|
|
|
[1, [typed.double, typed.unrestrictedDouble]]
|
2017-06-28 04:33:03 +00:00
|
|
|
])
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static arrayBuffer (byteLength = null) {
|
2018-04-06 22:15:55 +00:00
|
|
|
let length = (byteLength !== null) ? byteLength : typed.unsignedShort()
|
2018-05-21 15:50:44 +00:00
|
|
|
return `new ArrayBuffer(${length})`
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static dataView (byteLength = null) {
|
2018-04-06 22:15:55 +00:00
|
|
|
let length = (byteLength !== null) ? byteLength : typed.unsignedShort()
|
2018-05-21 15:50:44 +00:00
|
|
|
return `new DataView(${typed.arrayBuffer(length)})`
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static typedArray (byteLength = null) {
|
2018-04-06 22:15:55 +00:00
|
|
|
let length = (byteLength !== null) ? byteLength : typed.unsignedShort()
|
2017-09-13 18:57:21 +00:00
|
|
|
let arrType = random.item([
|
|
|
|
'Int8', 'Uint8', 'Uint8Clamped',
|
|
|
|
'Int16', 'Uint16',
|
|
|
|
'Int32', 'Uint32',
|
2017-09-13 19:04:58 +00:00
|
|
|
'Float32', 'Float64'
|
2017-09-13 18:57:21 +00:00
|
|
|
])
|
2018-05-21 15:50:44 +00:00
|
|
|
let method = `new ${arrType}Array`
|
2017-09-13 18:57:21 +00:00
|
|
|
switch (random.number(16)) {
|
|
|
|
case 0:
|
2018-05-21 15:50:44 +00:00
|
|
|
return `${method}()`
|
2017-09-13 18:57:21 +00:00
|
|
|
case 1:
|
2018-05-21 15:50:44 +00:00
|
|
|
return `${method}(${this.typedArray()})`
|
2017-09-13 18:57:21 +00:00
|
|
|
default:
|
2018-05-21 15:50:44 +00:00
|
|
|
return `${method}(${length})`
|
2017-09-13 18:57:21 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bufferSource () {
|
2017-09-13 20:44:14 +00:00
|
|
|
switch (random.number(4)) {
|
|
|
|
case 0:
|
2018-04-06 22:15:55 +00:00
|
|
|
return typed.arrayBuffer()
|
2017-09-13 20:44:14 +00:00
|
|
|
case 1:
|
2018-04-06 22:15:55 +00:00
|
|
|
return typed.dataView()
|
2017-09-13 20:44:14 +00:00
|
|
|
default:
|
2018-04-06 22:15:55 +00:00
|
|
|
return typed.typedArray()
|
2017-09-13 20:44:14 +00:00
|
|
|
}
|
2017-06-28 04:33:03 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
|
|
|
|
module.exports = typed
|