提交 f3667e32 编写于 作者: M mahaifeng

[array-buffer]添加slice测试

上级 f36cdd44
...@@ -34,72 +34,87 @@ export class TDataView { ...@@ -34,72 +34,87 @@ export class TDataView {
} }
setInt8() { setInt8() {
// #TEST DataView.setInt8,DataView.getInt8
// Create an ArrayBuffer with a size in bytes // Create an ArrayBuffer with a size in bytes
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let view = new DataView(buffer); let view = new DataView(buffer);
view.setInt8(1, 127); // Max signed 8-bit integer view.setInt8(1, 127); // Max signed 8-bit integer
expect(view.getInt8(1)).toEqual(127); expect(view.getInt8(1)).toEqual(127);
} // #END
}
setFloat32() { setFloat32() {
console.log('setFloat32', 'start') // #TEST DataView.setFloat32,DataView.getFloat32
// Create an ArrayBuffer with a size in bytes // Create an ArrayBuffer with a size in bytes
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let view = new DataView(buffer); let view = new DataView(buffer);
view.setFloat32(1,3); view.setFloat32(1, 3);
console.log('setFloat32',view.getFloat32(1))
expect(this.numberEquals(3, view.getFloat32(1))).toEqual(true); expect(this.numberEquals(3, view.getFloat32(1))).toEqual(true);
} // #END
}
setFloat64() { setFloat64() {
// #TEST DataView.setFloat64,DataView.getFloat64
// Create an ArrayBuffer with a size in bytes // Create an ArrayBuffer with a size in bytes
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let view = new DataView(buffer); let view = new DataView(buffer);
view.setFloat64(1, Math.PI); view.setFloat64(1, Math.PI);
expect(this.numberEquals(3.141592653589793, view.getFloat64(1))).toEqual(true); expect(this.numberEquals(3.141592653589793, view.getFloat64(1))).toEqual(true);
} // #END
}
setInt16() { setInt16() {
// #TEST DataView.setInt16,DataView.getInt16
// Create an ArrayBuffer with a size in bytes // Create an ArrayBuffer with a size in bytes
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let view = new DataView(buffer); let view = new DataView(buffer);
view.setInt16(1, 32767); // Max signed 16-bit integer view.setInt16(1, 32767); // Max signed 16-bit integer
expect(view.getInt16(1)).toEqual(32767); expect(view.getInt16(1)).toEqual(32767);
} // #END
}
setInt32() { setInt32() {
// #TEST DataView.setInt32,DataView.getInt32
// Create an ArrayBuffer with a size in bytes // Create an ArrayBuffer with a size in bytes
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let view = new DataView(buffer); let view = new DataView(buffer);
view.setInt32(1, 2147483647); // Max signed 32-bit integer view.setInt32(1, 2147483647); // Max signed 32-bit integer
expect(this.numberEquals(2147483647, view.getInt32(1))).toEqual(true); expect(this.numberEquals(2147483647, view.getInt32(1))).toEqual(true);
} // #END
}
setUint16() { setUint16() {
// #TEST DataView.setUint16,DataView.getUint16
// Create an ArrayBuffer with a size in bytes // Create an ArrayBuffer with a size in bytes
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let view = new DataView(buffer); let view = new DataView(buffer);
view.setUint16(1, 65535); // Max unsigned 16-bit integer view.setUint16(1, 65535); // Max unsigned 16-bit integer
expect(this.numberEquals(65535, view.getUint16(1))).toEqual(true); expect(this.numberEquals(65535, view.getUint16(1))).toEqual(true);
// #END
} }
setUint32() { setUint32() {
// #TEST DataView.setUint32,DataView.getUint32
// Create an ArrayBuffer with a size in bytes // Create an ArrayBuffer with a size in bytes
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let view = new DataView(buffer); let view = new DataView(buffer);
view.setUint32(1, 4294967295); // Max unsigned 32-bit integer view.setUint32(1, 4294967295); // Max unsigned 32-bit integer
expect(this.numberEquals(4294967295, view.getUint32(1))).toEqual(true); expect(this.numberEquals(4294967295, view.getUint32(1))).toEqual(true);
} // #END
}
setUint8() { setUint8() {
// #TEST DataView.setUint8,DataView.getUint8
// Create an ArrayBuffer with a size in bytes // Create an ArrayBuffer with a size in bytes
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let view = new DataView(buffer); let view = new DataView(buffer);
view.setUint8(1, 255); // Max unsigned 8-bit integer view.setUint8(1, 255); // Max unsigned 8-bit integer
expect(this.numberEquals(255, view.getUint8(1))).toEqual(true); expect(this.numberEquals(255, view.getUint8(1))).toEqual(true);
// #END
} }
numberEquals(a: number, b: number): Boolean {
numberEquals(a : number, b : number) : Boolean {
return a == b return a == b
} }
...@@ -128,7 +143,7 @@ export class TDataView { ...@@ -128,7 +143,7 @@ export class TDataView {
this.doFloatIterations(false); this.doFloatIterations(false);
} }
doIterations(littleEndian: boolean, dataView: boolean) { doIterations(littleEndian : boolean, dataView : boolean) {
var buffer = this.makeBuffer(1000, littleEndian); var buffer = this.makeBuffer(1000, littleEndian);
var iterations = 10; var iterations = 10;
if (dataView) { if (dataView) {
...@@ -140,7 +155,7 @@ export class TDataView { ...@@ -140,7 +155,7 @@ export class TDataView {
} }
} }
makeBuffer(size: number, littleEndian: boolean): ArrayBuffer { makeBuffer(size : number, littleEndian : boolean) : ArrayBuffer {
var buffer = new ArrayBuffer(1000 * 14); var buffer = new ArrayBuffer(1000 * 14);
var view = new DataView(buffer); var view = new DataView(buffer);
for (var i = 0; i < 1000; ++i) { for (var i = 0; i < 1000; ++i) {
...@@ -154,7 +169,7 @@ export class TDataView { ...@@ -154,7 +169,7 @@ export class TDataView {
return buffer; return buffer;
} }
doOneIterationDV(buffer: ArrayBuffer, littleEndian: boolean) { doOneIterationDV(buffer : ArrayBuffer, littleEndian : boolean) {
var xor = 0; var xor = 0;
var view = new DataView(buffer); var view = new DataView(buffer);
for (var i = 0; i < view.byteLength; i += 14) { for (var i = 0; i < view.byteLength; i += 14) {
...@@ -168,7 +183,7 @@ export class TDataView { ...@@ -168,7 +183,7 @@ export class TDataView {
expect(xor).toEqual(0); expect(xor).toEqual(0);
} }
doOneIterationJS(buffer: ArrayBuffer, littleEndian: boolean) { doOneIterationJS(buffer : ArrayBuffer, littleEndian : boolean) {
var xor = 0; var xor = 0;
if (littleEndian) { if (littleEndian) {
var reader = new LittleEndian(buffer); var reader = new LittleEndian(buffer);
...@@ -195,7 +210,7 @@ export class TDataView { ...@@ -195,7 +210,7 @@ export class TDataView {
} }
doFloatIterations(dataView: boolean) { doFloatIterations(dataView : boolean) {
var buffer = this.makeFloatBuffer(); var buffer = this.makeFloatBuffer();
var iterations = 10; var iterations = 10;
if (dataView) { if (dataView) {
...@@ -207,7 +222,7 @@ export class TDataView { ...@@ -207,7 +222,7 @@ export class TDataView {
} }
} }
makeFloatBuffer(): ArrayBuffer { makeFloatBuffer() : ArrayBuffer {
var buffer = new ArrayBuffer(1000 * 16); var buffer = new ArrayBuffer(1000 * 16);
var view = new DataView(buffer); var view = new DataView(buffer);
for (var i = 0; i < 1000; i++) { for (var i = 0; i < 1000; i++) {
...@@ -218,7 +233,7 @@ export class TDataView { ...@@ -218,7 +233,7 @@ export class TDataView {
return buffer; return buffer;
} }
doOneFloatIterationDV(buffer: ArrayBuffer) { doOneFloatIterationDV(buffer : ArrayBuffer) {
var sum = 0; var sum = 0;
var view = new DataView(buffer); var view = new DataView(buffer);
for (var i = 0; i < view.byteLength; i += 16) { for (var i = 0; i < view.byteLength; i += 16) {
...@@ -226,10 +241,10 @@ export class TDataView { ...@@ -226,10 +241,10 @@ export class TDataView {
sum += view.getFloat32(i + 8); sum += view.getFloat32(i + 8);
sum += view.getFloat32(i + 12); sum += view.getFloat32(i + 12);
} }
expect(sum).toEqual(23634.413356150446); expect(sum).toEqual(23634.413356150446);
} }
doOneFloatIterationJS(buffer: ArrayBuffer) { doOneFloatIterationJS(buffer : ArrayBuffer) {
var sum = 0; var sum = 0;
var float32array = new Float32Array(buffer); var float32array = new Float32Array(buffer);
var float64array = new Float64Array(buffer); var float64array = new Float64Array(buffer);
...@@ -241,32 +256,32 @@ export class TDataView { ...@@ -241,32 +256,32 @@ export class TDataView {
} }
} }
class BigEndian { class BigEndian {
private uint8View_: Uint8Array; private uint8View_ : Uint8Array;
private int8View_: Int8Array; private int8View_ : Int8Array;
constructor(buffer: ArrayBuffer, ) { constructor(buffer : ArrayBuffer,) {
const offset = 0; const offset = 0;
this.uint8View_ = new Uint8Array(buffer, offset); this.uint8View_ = new Uint8Array(buffer, offset);
this.int8View_ = new Int8Array(buffer, offset); this.int8View_ = new Int8Array(buffer, offset);
} }
getInt8(byteOffset: number): number { getInt8(byteOffset : number) : number {
return this.int8View_[byteOffset]; return this.int8View_[byteOffset];
} }
getUint8(byteOffset: number): number { getUint8(byteOffset : number) : number {
return this.uint8View_[byteOffset]; return this.uint8View_[byteOffset];
} }
getInt16(byteOffset: number): number { getInt16(byteOffset : number) : number {
return this.uint8View_[byteOffset + 1] | (this.int8View_[byteOffset] << 8); return this.uint8View_[byteOffset + 1] | (this.int8View_[byteOffset] << 8);
} }
getUint16(byteOffset: number): number { getUint16(byteOffset : number) : number {
return this.uint8View_[byteOffset + 1] | (this.uint8View_[byteOffset] << 8); return this.uint8View_[byteOffset + 1] | (this.uint8View_[byteOffset] << 8);
} }
getInt32(byteOffset: number): number { getInt32(byteOffset : number) : number {
return ( return (
this.uint8View_[byteOffset + 3] | this.uint8View_[byteOffset + 3] |
(this.uint8View_[byteOffset + 2] << 8) | (this.uint8View_[byteOffset + 2] << 8) |
...@@ -275,7 +290,7 @@ class BigEndian { ...@@ -275,7 +290,7 @@ class BigEndian {
); );
} }
getUint32(byteOffset: number): number { getUint32(byteOffset : number) : number {
return ( return (
this.uint8View_[byteOffset + 3] + this.uint8View_[byteOffset + 3] +
(this.uint8View_[byteOffset + 2] << 8) + (this.uint8View_[byteOffset + 2] << 8) +
...@@ -286,32 +301,32 @@ class BigEndian { ...@@ -286,32 +301,32 @@ class BigEndian {
} }
class LittleEndian { class LittleEndian {
private uint8View_: Uint8Array; private uint8View_ : Uint8Array;
private int8View_: Int8Array; private int8View_ : Int8Array;
constructor(buffer: ArrayBuffer) { constructor(buffer : ArrayBuffer) {
const offset = 0; const offset = 0;
this.uint8View_ = new Uint8Array(buffer, offset); this.uint8View_ = new Uint8Array(buffer, offset);
this.int8View_ = new Int8Array(buffer, offset); this.int8View_ = new Int8Array(buffer, offset);
} }
getInt8(byteOffset: number): number { getInt8(byteOffset : number) : number {
return this.int8View_[byteOffset]; return this.int8View_[byteOffset];
} }
getUint8(byteOffset: number): number { getUint8(byteOffset : number) : number {
return this.uint8View_[byteOffset]; return this.uint8View_[byteOffset];
} }
getInt16(byteOffset: number): number { getInt16(byteOffset : number) : number {
return this.uint8View_[byteOffset] | (this.int8View_[byteOffset + 1] << 8); return this.uint8View_[byteOffset] | (this.int8View_[byteOffset + 1] << 8);
} }
getUint16(byteOffset: number): number { getUint16(byteOffset : number) : number {
return this.uint8View_[byteOffset] | (this.uint8View_[byteOffset + 1] << 8); return this.uint8View_[byteOffset] | (this.uint8View_[byteOffset + 1] << 8);
} }
getInt32(byteOffset: number): number { getInt32(byteOffset : number) : number {
return ( return (
this.uint8View_[byteOffset] | this.uint8View_[byteOffset] |
(this.uint8View_[byteOffset + 1] << 8) | (this.uint8View_[byteOffset + 1] << 8) |
...@@ -320,7 +335,7 @@ class LittleEndian { ...@@ -320,7 +335,7 @@ class LittleEndian {
); );
} }
getUint32(byteOffset: number): number { getUint32(byteOffset : number) : number {
return ( return (
this.uint8View_[byteOffset] + this.uint8View_[byteOffset] +
(this.uint8View_[byteOffset + 1] << 8) + (this.uint8View_[byteOffset + 1] << 8) +
......
...@@ -304,7 +304,8 @@ export class TFloat64Array { ...@@ -304,7 +304,8 @@ export class TFloat64Array {
} }
arrayBufferSlice() { arrayBufferSlice() {
// #TEST Float64Array with ArrayBuffer.slice
// #TEST ArrayBuffer.slice
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let float64 = new Float64Array(buffer); let float64 = new Float64Array(buffer);
float64[1] = 42; float64[1] = 42;
...@@ -314,6 +315,7 @@ export class TFloat64Array { ...@@ -314,6 +315,7 @@ export class TFloat64Array {
let sliced = new Float64Array(res); let sliced = new Float64Array(res);
expect(sliced[0]).toEqual(42); expect(sliced[0]).toEqual(42);
// #END // #END
} }
// #endif // #endif
......
...@@ -279,6 +279,7 @@ export class TInt16Array { ...@@ -279,6 +279,7 @@ export class TInt16Array {
} }
arrayBufferSlice() { arrayBufferSlice() {
// #TEST ArrayBuffer.slice with Int16Array // #TEST ArrayBuffer.slice with Int16Array
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let int16 = new Int16Array(buffer); let int16 = new Int16Array(buffer);
...@@ -289,6 +290,7 @@ export class TInt16Array { ...@@ -289,6 +290,7 @@ export class TInt16Array {
let sliced = new Int16Array(res); let sliced = new Int16Array(res);
expect(sliced[0]).toEqual(42); expect(sliced[0]).toEqual(42);
// #END // #END
} }
// #endif // #endif
......
...@@ -298,6 +298,7 @@ export class TInt32Array { ...@@ -298,6 +298,7 @@ export class TInt32Array {
} }
arrayBufferSlice() { arrayBufferSlice() {
// #TEST ArrayBuffer.slice // #TEST ArrayBuffer.slice
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let int32 = new Int32Array(buffer); let int32 = new Int32Array(buffer);
...@@ -308,6 +309,7 @@ export class TInt32Array { ...@@ -308,6 +309,7 @@ export class TInt32Array {
let sliced = new Int32Array(res); let sliced = new Int32Array(res);
expect(sliced[1]).toEqual(42); expect(sliced[1]).toEqual(42);
// #END // #END
} }
// #endif // #endif
......
...@@ -294,7 +294,8 @@ export class TInt8Array { ...@@ -294,7 +294,8 @@ export class TInt8Array {
} }
arrayBufferSlice() { arrayBufferSlice() {
// #TEST ArrayBuffer.slice with Int8Array
// #TEST ArrayBuffer.slice
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let int8 = new Int8Array(buffer); let int8 = new Int8Array(buffer);
int8[4] = 42; int8[4] = 42;
...@@ -304,6 +305,7 @@ export class TInt8Array { ...@@ -304,6 +305,7 @@ export class TInt8Array {
let sliced = new Int8Array(res); let sliced = new Int8Array(res);
expect(sliced[0]).toEqual(42); expect(sliced[0]).toEqual(42);
// #END // #END
} }
// #endif // #endif
......
...@@ -293,7 +293,8 @@ export class TUint8Array { ...@@ -293,7 +293,8 @@ export class TUint8Array {
} }
arrayBufferSlice() { arrayBufferSlice() {
// #TEST ArrayBuffer.slice with Uint8Array
// #TEST ArrayBuffer.slice
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let uint8 = new Uint8Array(buffer); let uint8 = new Uint8Array(buffer);
uint8[4] = 42; uint8[4] = 42;
...@@ -303,6 +304,7 @@ export class TUint8Array { ...@@ -303,6 +304,7 @@ export class TUint8Array {
let sliced = new Uint8Array(res); let sliced = new Uint8Array(res);
expect(sliced[0]).toEqual(42); expect(sliced[0]).toEqual(42);
// #END // #END
} }
// #endif // #endif
......
...@@ -291,7 +291,8 @@ export class TUint8ClampedArray { ...@@ -291,7 +291,8 @@ export class TUint8ClampedArray {
} }
arrayBufferSlice() { arrayBufferSlice() {
// #TEST ArrayBuffer.slice with Uint8ClampedArray
// #TEST ArrayBuffer.slice
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let uint8Clamped = new Uint8ClampedArray(buffer); let uint8Clamped = new Uint8ClampedArray(buffer);
uint8Clamped[4] = 42; uint8Clamped[4] = 42;
......
...@@ -305,6 +305,7 @@ export class TUint16Array { ...@@ -305,6 +305,7 @@ export class TUint16Array {
} }
arrayBufferSlice() { arrayBufferSlice() {
// #TEST Uint16Array.arrayBufferSlice // #TEST Uint16Array.arrayBufferSlice
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let uint16 = new Uint16Array(buffer); let uint16 = new Uint16Array(buffer);
...@@ -315,6 +316,7 @@ export class TUint16Array { ...@@ -315,6 +316,7 @@ export class TUint16Array {
let sliced = new Uint16Array(res); let sliced = new Uint16Array(res);
expect(sliced[0]).toEqual(42); expect(sliced[0]).toEqual(42);
// #END // #END
} }
// #endif // #endif
......
...@@ -299,6 +299,7 @@ export class TUint32Array { ...@@ -299,6 +299,7 @@ export class TUint32Array {
} }
arrayBufferSlice() { arrayBufferSlice() {
// #TEST ArrayBuffer.slice // #TEST ArrayBuffer.slice
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let uint32 = new Uint32Array(buffer); let uint32 = new Uint32Array(buffer);
...@@ -309,6 +310,7 @@ export class TUint32Array { ...@@ -309,6 +310,7 @@ export class TUint32Array {
let sliced = new Uint32Array(res); let sliced = new Uint32Array(res);
expect(sliced[1]).toEqual(42); expect(sliced[1]).toEqual(42);
// #END // #END
} }
// #endif // #endif
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册