|
'use strict'; |
|
|
|
var test = require('tape'); |
|
var DynamicTypedarray = require('./'); |
|
|
|
test('DynamicTypedarray', function (t) { |
|
t.test('constructing by dtype string', function (t) { |
|
t.equal((new DynamicTypedarray('float64')).dtype, 'float64'); |
|
t.equal((new DynamicTypedarray('float32')).dtype, 'float32'); |
|
t.equal((new DynamicTypedarray('int32')).dtype, 'int32'); |
|
t.equal((new DynamicTypedarray('int16')).dtype, 'int16'); |
|
t.equal((new DynamicTypedarray('int8')).dtype, 'int8'); |
|
t.equal((new DynamicTypedarray('uint32')).dtype, 'uint32'); |
|
t.equal((new DynamicTypedarray('uint16')).dtype, 'uint16'); |
|
t.equal((new DynamicTypedarray('uint8')).dtype, 'uint8'); |
|
t.equal((new DynamicTypedarray('uint8_clamped')).dtype, 'uint8_clamped'); |
|
|
|
t.throws(function () { |
|
var x = new DynamicTypedarray('foo'); |
|
x.push(1); |
|
}, /invalid dtype string, "foo"/); |
|
|
|
t.end(); |
|
}); |
|
|
|
t.test('constructing by view array', function (t) { |
|
t.equal((new DynamicTypedarray(new Float64Array(1))).dtype, 'float64'); |
|
t.equal((new DynamicTypedarray(new Float32Array(1))).dtype, 'float32'); |
|
t.equal((new DynamicTypedarray(new Int32Array(1))).dtype, 'int32'); |
|
t.equal((new DynamicTypedarray(new Int16Array(1))).dtype, 'int16'); |
|
t.equal((new DynamicTypedarray(new Int8Array(1))).dtype, 'int8'); |
|
t.equal((new DynamicTypedarray(new Uint32Array(1))).dtype, 'uint32'); |
|
t.equal((new DynamicTypedarray(new Uint16Array(1))).dtype, 'uint16'); |
|
t.equal((new DynamicTypedarray(new Uint8Array(1))).dtype, 'uint8'); |
|
t.equal((new DynamicTypedarray(new Uint8ClampedArray(1))).dtype, 'uint8_clamped'); |
|
t.equal((new DynamicTypedarray([1])).dtype, 'float64'); |
|
|
|
t.throws(function () { |
|
var x = new DynamicTypedarray({}); |
|
x.push(1); |
|
}, /Unexpected view type, "object"/); |
|
|
|
t.end(); |
|
}); |
|
|
|
t.test('constructing by size', function (t) { |
|
t.equal((new DynamicTypedarray(7)).dtype, 'float64'); |
|
t.equal((new DynamicTypedarray(7, 'float32')).dtype, 'float32'); |
|
t.equal((new DynamicTypedarray(7)).view.length, 7); |
|
t.end(); |
|
}); |
|
|
|
t.test('constructing by view and dtype string', function (t) { |
|
t.equal((new DynamicTypedarray([1], 'float64')).dtype, 'float64'); |
|
t.equal((new DynamicTypedarray([1], 'float32')).dtype, 'float32'); |
|
t.equal((new DynamicTypedarray([1], 'int32')).dtype, 'int32'); |
|
t.equal((new DynamicTypedarray([1], 'int16')).dtype, 'int16'); |
|
t.equal((new DynamicTypedarray([1], 'int8')).dtype, 'int8'); |
|
t.equal((new DynamicTypedarray([1], 'uint32')).dtype, 'uint32'); |
|
t.equal((new DynamicTypedarray([1], 'uint16')).dtype, 'uint16'); |
|
t.equal((new DynamicTypedarray([1], 'uint8')).dtype, 'uint8'); |
|
t.equal((new DynamicTypedarray([1], 'uint8_clamped')).dtype, 'uint8_clamped'); |
|
|
|
t.end(); |
|
}); |
|
|
|
t.test('initialization with Array view', function (t) { |
|
var x = new DynamicTypedarray([1, 2, 3]); |
|
t.equal(x.view[0], 1); |
|
t.equal(x.view[1], 2); |
|
t.equal(x.view[2], 3); |
|
t.end(); |
|
}); |
|
|
|
t.test('initialization with typed array view', function (t) { |
|
var x = new DynamicTypedarray(new Float32Array([1.5, 2.5, 3.5])); |
|
t.equal(x.view[0], 1.5); |
|
t.equal(x.view[1], 2.5); |
|
t.equal(x.view[2], 3.5); |
|
t.end(); |
|
}); |
|
|
|
t.test('casts typed array input to type', function (t) { |
|
var input = new Float32Array([1.5, 2.5, 3.5]); |
|
var x = new DynamicTypedarray(input, 'uint8'); |
|
t.equal(x.view[0], 1); |
|
t.equal(x.view[1], 2); |
|
t.equal(x.view[2], 3); |
|
x.view[0] = 5; |
|
x.view[1] = 6; |
|
x.view[2] = 7; |
|
t.deepEqual(input, new Float32Array([1.5, 2.5, 3.5])); |
|
t.end(); |
|
}); |
|
|
|
t.test('casts array input to type', function (t) { |
|
var input = [1.5, 2.5, 3.5]; |
|
var x = new DynamicTypedarray(input, 'uint8'); |
|
t.equal(x.view[0], 1); |
|
t.equal(x.view[1], 2); |
|
t.equal(x.view[2], 3); |
|
t.deepEqual(input, [1.5, 2.5, 3.5]); |
|
t.end(); |
|
}); |
|
|
|
t.test('.setLength() (expansion)', function (t) { |
|
var x = new DynamicTypedarray([0, 1, 2, 3, 4]); |
|
|
|
t.equal(x.buffer.length, 8); |
|
t.equal(x.view.length, 5); |
|
|
|
x.length = 7; |
|
|
|
t.equal(x.buffer.length, 8); |
|
t.equal(x.view.length, 7); |
|
|
|
t.deepEqual(x.view, new Float32Array([0, 1, 2, 3, 4, 0, 0])); |
|
|
|
x.length = 15; |
|
|
|
t.equal(x.buffer.length, 16); |
|
t.equal(x.view.length, 15); |
|
|
|
t.deepEqual(x.view, new Float32Array([0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])); |
|
|
|
t.end(); |
|
}); |
|
|
|
t.test('++, --, += for the length', function (t) { |
|
var x = new DynamicTypedarray(4); |
|
|
|
t.equal(x.view.length, 4); |
|
t.equal(x.buffer.length, 4); |
|
|
|
x.length++; |
|
|
|
t.equal(x.view.length, 5); |
|
t.equal(x.buffer.length, 8); |
|
|
|
x.length--; |
|
|
|
t.equal(x.view.length, 4); |
|
t.equal(x.buffer.length, 8); |
|
|
|
x.length += 8; |
|
|
|
t.equal(x.view.length, 12); |
|
t.equal(x.buffer.length, 16); |
|
|
|
t.end(); |
|
}); |
|
|
|
t.test('.setLength() (truncation)', function (t) { |
|
var x = new DynamicTypedarray([0, 1, 2, 3, 4, 5]); |
|
|
|
t.equal(x.buffer.length, 8); |
|
t.equal(x.view.length, 6); |
|
|
|
x.length = 5; |
|
|
|
t.equal(x.buffer.length, 8); |
|
t.equal(x.length, 5); |
|
t.equal(x.view.length, 5); |
|
|
|
t.deepEqual(x.view, new Float32Array([0, 1, 2, 3, 4])); |
|
|
|
x.length = 3; |
|
|
|
t.equal(x.buffer.length, 4); |
|
t.equal(x.view.length, 3); |
|
|
|
t.deepEqual(x.view, new Float32Array([0, 1, 2])); |
|
|
|
t.end(); |
|
}); |
|
|
|
t.test('.push()', function (t) { |
|
var x = new DynamicTypedarray(); |
|
|
|
t.equal(x.view.length, 0); |
|
t.equal(x.buffer.length, 1); |
|
|
|
var returnValue = x.push(5); |
|
|
|
t.equal(returnValue, 1); |
|
t.equal(x.buffer.length, 1); |
|
t.deepEqual(x.view, new Float64Array([5])); |
|
|
|
returnValue = x.push(1, 2, 3); |
|
|
|
t.equal(returnValue, 4); |
|
t.equal(x.buffer.length, 4); |
|
t.deepEqual(x.view, new Float64Array([5, 1, 2, 3])); |
|
|
|
returnValue = x.push(5, 4); |
|
|
|
t.equal(returnValue, 6); |
|
t.equal(x.buffer.length, 8); |
|
t.deepEqual(x.view, new Float64Array([5, 1, 2, 3, 5, 4])); |
|
|
|
returnValue = x.push(6); |
|
|
|
t.equal(returnValue, 7); |
|
t.equal(x.buffer.length, 8); |
|
t.deepEqual(x.view, new Float64Array([5, 1, 2, 3, 5, 4, 6])); |
|
|
|
t.end(); |
|
}); |
|
|
|
t.test('.pushArray()', function (t) { |
|
var x = new DynamicTypedarray(); |
|
|
|
t.equal(x.view.length, 0); |
|
t.equal(x.buffer.length, 1); |
|
|
|
var returnValue = x.pushArray([5]); |
|
|
|
t.equal(returnValue, 1); |
|
t.equal(x.buffer.length, 1); |
|
t.deepEqual(x.view, new Float64Array([5])); |
|
|
|
returnValue = x.pushArray([1, 2, 3]); |
|
|
|
t.equal(returnValue, 4); |
|
t.equal(x.buffer.length, 4); |
|
t.deepEqual(x.view, new Float64Array([5, 1, 2, 3])); |
|
|
|
returnValue = x.pushArray([5, 4]); |
|
|
|
t.equal(returnValue, 6); |
|
t.equal(x.buffer.length, 8); |
|
t.deepEqual(x.view, new Float64Array([5, 1, 2, 3, 5, 4])); |
|
|
|
returnValue = x.pushArray([6]); |
|
|
|
t.equal(returnValue, 7); |
|
t.equal(x.buffer.length, 8); |
|
t.deepEqual(x.view, new Float64Array([5, 1, 2, 3, 5, 4, 6])); |
|
|
|
t.end(); |
|
}); |
|
|
|
t.test('.ensureCapacity()', function (t) { |
|
var x = new DynamicTypedarray([1, 2, 3]); |
|
|
|
t.equal(x.view.length, 3); |
|
t.equal(x.buffer.length, 4); |
|
|
|
x.ensureCapacity(7); |
|
|
|
t.equal(x.view.length, 3); |
|
t.equal(x.buffer.length, 8); |
|
|
|
t.deepEqual(x.view, new Float64Array([1, 2, 3])); |
|
|
|
t.end(); |
|
}); |
|
|
|
t.end(); |
|
}); |