提交 9b3de1ef 编写于 作者: M mahaifeng

[array-buffer]文档添加console 输出

上级 b13b17fd
...@@ -39,8 +39,10 @@ export class TDataView { ...@@ -39,8 +39,10 @@ export class TDataView {
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); let ret = view.getInt8(1)
console.log(ret)//127
// #END // #END
expect(ret).toEqual(127);
} }
setFloat32() { setFloat32() {
...@@ -49,8 +51,10 @@ export class TDataView { ...@@ -49,8 +51,10 @@ export class TDataView {
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);
expect(this.numberEquals(3, view.getFloat32(1))).toEqual(true); let ret = view.getFloat32(1);
console.log(ret); // 3
// #END // #END
expect(this.numberEquals(3, ret)).toEqual(true);
} }
setFloat64() { setFloat64() {
...@@ -59,8 +63,10 @@ export class TDataView { ...@@ -59,8 +63,10 @@ export class TDataView {
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); let ret = view.getFloat64(1);
console.log(ret); // 3.141592653589793
// #END // #END
expect(this.numberEquals(3.141592653589793, ret)).toEqual(true);
} }
setInt16() { setInt16() {
...@@ -69,18 +75,23 @@ export class TDataView { ...@@ -69,18 +75,23 @@ export class TDataView {
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); let ret = view.getInt16(1);
console.log(ret); // 32767
// #END // #END
expect(ret).toEqual(32767);
} }
setInt32() { setInt32() {
// #TEST DataView.setInt32,DataView.getInt32 // #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); let ret = view.getInt32(1);
console.log(ret); // 2147483647
// #END // #END
expect(this.numberEquals(2147483647, ret)).toEqual(true);
} }
setUint16() { setUint16() {
...@@ -89,8 +100,10 @@ export class TDataView { ...@@ -89,8 +100,10 @@ export class TDataView {
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); let ret = view.getUint16(1);
console.log(ret); // 65535
// #END // #END
expect(this.numberEquals(65535, ret)).toEqual(true);
} }
setUint32() { setUint32() {
...@@ -99,8 +112,10 @@ export class TDataView { ...@@ -99,8 +112,10 @@ export class TDataView {
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); let ret = view.getUint32(1);
console.log(ret); // 4294967295
// #END // #END
expect(this.numberEquals(4294967295, ret)).toEqual(true);
} }
setUint8() { setUint8() {
...@@ -109,8 +124,10 @@ export class TDataView { ...@@ -109,8 +124,10 @@ export class TDataView {
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); let ret = view.getUint8(1);
console.log(ret); // 255
// #END // #END
expect(this.numberEquals(255, ret)).toEqual(true);
} }
......
...@@ -73,20 +73,23 @@ export class TFloat32Array { ...@@ -73,20 +73,23 @@ export class TFloat32Array {
testSet() { testSet() {
// #TEST Float32Array.set // #TEST Float32Array.set
let float32 = new Float32Array(8); let float32 = new Float32Array(8);
var array = [1, 2, 3] var array = [1, 2, 3];
float32.set(array, 1); float32.set(array, 1);
expect(float32.toString()).toEqual("0,1,2,3,0,0,0,0"); console.log(float32.toString()); // 0,1,2,3,0,0,0,0
let src = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]; let src = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4];
let typed_dest = new Float32Array(16); let typed_dest = new Float32Array(16);
typed_dest.set(src); typed_dest.set(src);
expect(typed_dest.toString()).toEqual("1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4"); console.log(typed_dest.toString()); // 1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4
// expect(typed_dest.toString()).toEqual("1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4");
let typed_src = new Float32Array(src); let typed_src = new Float32Array(src);
typed_dest = new Float32Array(16); typed_dest = new Float32Array(16);
typed_dest.set(typed_src); typed_dest.set(typed_src);
expect(typed_dest.toString()).toEqual("1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4"); console.log(typed_dest.toString()); // 1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4
// #END // #END
expect(float32.toString()).toEqual("0,1,2,3,0,0,0,0");
expect(typed_dest.toString()).toEqual("1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4");
} }
testCopyWith() { testCopyWith() {
...@@ -94,62 +97,78 @@ export class TFloat32Array { ...@@ -94,62 +97,78 @@ export class TFloat32Array {
let float32 = new Float32Array(8); let float32 = new Float32Array(8);
float32.set([1, 2, 3], 1); float32.set([1, 2, 3], 1);
float32.copyWithin(3, 0, 3); float32.copyWithin(3, 0, 3);
expect(float32.toString()).toEqual("0,1,2,0,1,2,0,0"); console.log(float32.toString()); // 0,1,2,0,1,2,0,0
// #END // #END
expect(float32.toString()).toEqual("0,1,2,0,1,2,0,0");
} }
testEvery() { testEvery() {
// #TEST Float32Array.every // #TEST Float32Array.every
let result = new Float32Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Float32Array) : boolean => value < 40); let result = new Float32Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Float32Array) : boolean => value < 40);
expect(result).toEqual(false); console.log(result); // false
// #END // #END
expect(result).toEqual(false);
} }
testFill() { testFill() {
// #TEST Float32Array.fill // #TEST Float32Array.fill
let float32 = new Float32Array([1, 2, 3]).fill(4); let float32_t1 = new Float32Array([1, 2, 3]).fill(4);
expect(float32.toString()).toEqual("4,4,4"); console.log(float32_t1.toString()); // 4,4,4
float32 = new Float32Array([1, 2, 3]).fill(4, 1); let float32_t2 = new Float32Array([1, 2, 3]).fill(4, 1);
expect(float32.toString()).toEqual("1,4,4"); console.log(float32_t2.toString()); // 1,4,4
float32 = new Float32Array([1, 2, 3]).fill(4, 1, 2);
expect(float32.toString()).toEqual("1,4,3");
float32 = new Float32Array([1, 2, 3]).fill(4, 1, 1); let float32_t3 = new Float32Array([1, 2, 3]).fill(4, 1, 2);
expect(float32.toString()).toEqual("1,2,3"); console.log(float32_t3.toString()); // 1,4,3
float32 = new Float32Array([1, 2, 3]).fill(4, -3, -2); let float32_t4 = new Float32Array([1, 2, 3]).fill(4, 1, 1);
expect(float32.toString()).toEqual("4,2,3"); console.log(float32_t4.toString()); // 1,2,3
let float32 = new Float32Array([1, 2, 3]).fill(4, -3, -2);
console.log(float32.toString()); // 4,2,3
// #END // #END
expect(float32_t4.toString()).toEqual("1,2,3");
expect(float32_t3.toString()).toEqual("1,4,3");
expect(float32_t2.toString()).toEqual("1,4,4");
expect(float32_t1.toString()).toEqual("4,4,4");
expect(float32.toString()).toEqual("4,2,3");
} }
testFilter() { testFilter() {
// #TEST Float32Array.filter // #TEST Float32Array.filter
let float32 = new Float32Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Float32Array) : boolean => value >= 10); let float32 = new Float32Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Float32Array) : boolean => value >= 10);
expect(float32.toString()).toEqual("12,44"); console.log(float32.toString()); // 12,44
// #END // #END
expect(float32.toString()).toEqual("12,44");
} }
find() { find() {
// #TEST Float32Array.find // #TEST Float32Array.find
let float32 = new Float32Array([4, 5, 8, 12]); let float32 = new Float32Array([4, 5, 8, 12]);
let res = float32.find((value : number, _ : number, _a : Float32Array) : boolean => value > 5); let res = float32.find((value : number, _ : number, _a : Float32Array) : boolean => value > 5);
expect(res).toEqual(8); console.log(res); // 8
// #END // #END
expect(res).toEqual(8);
} }
findIndex() { findIndex() {
// #TEST Float32Array.findIndex // #TEST Float32Array.findIndex
let float32 = new Float32Array([4, 6, 8, 12]); let float32 = new Float32Array([4, 6, 8, 12]);
let res = float32.findIndex((value : number, _ : number, _a : Float32Array) : boolean => value > 100); let res1 = float32.findIndex((value : number, _ : number, _a : Float32Array) : boolean => value > 100);
expect(res).toEqual(-1); console.log(res1); // -1
// #END
let ufloat32 = new Float32Array([4, 6, 7, 120]); let ufloat32 = new Float32Array([4, 6, 7, 120]);
res = ufloat32.findIndex((value : number, _ : number, _a : Float32Array) : boolean => value > 100); let res = ufloat32.findIndex((value : number, _ : number, _a : Float32Array) : boolean => value > 100);
expect(res).toEqual(3); console.log(res); // 3
// #END // #END
expect(res).toEqual(3);
expect(res1).toEqual(-1);
} }
foreach() { foreach() {
...@@ -164,139 +183,170 @@ export class TFloat32Array { ...@@ -164,139 +183,170 @@ export class TFloat32Array {
// #TEST Float32Array.entries // #TEST Float32Array.entries
let arr = new Float32Array([10, 20, 30, 40, 50]); let arr = new Float32Array([10, 20, 30, 40, 50]);
let entries = arr.entries(); let entries = arr.entries();
expect(entries.next().value[1]).toEqual(10); let value1 = entries.next().value[1]
expect(entries.next().value[1]).toEqual(20); let value2 = entries.next().value[1]
console.log(value1); // 10
console.log(value2); // 20
// #END // #END
expect(value1).toEqual(10);
expect(value2).toEqual(20);
} }
includes() { includes() {
// #TEST Float32Array.includes // #TEST Float32Array.includes
let float32 = new Float32Array([1, 2, 3]); let float32 = new Float32Array([1, 2, 3]);
let res = float32.includes(2.0); let res1 = float32.includes(2.0);
expect(res).toEqual(true); console.log(res1); // true
let res2 = float32.includes(4.0);
res = float32.includes(4.0); console.log(res2); // false
expect(res).toEqual(false); let res = float32.includes(3.0, 3);
console.log(res); // false
res = float32.includes(3.0, 3);
expect(res).toEqual(false);
// #END // #END
expect(res2).toEqual(false);
expect(res1).toEqual(true);
expect(res).toEqual(false);
} }
indexOf() { indexOf() {
// #TEST Float32Array.indexOf // #TEST Float32Array.indexOf
let float32 = new Float32Array([2, 5, 9]); let float32 = new Float32Array([2, 5, 9]);
let res = float32.indexOf(2); let res = float32.indexOf(2);
expect(res).toEqual(0); console.log(res); // 0
// #END
// expect(res).toEqual(0);
res = float32.indexOf(7); // res = float32.indexOf(7);
expect(res).toEqual(-1); // console.log(res); // -1
// expect(res).toEqual(-1);
res = float32.indexOf(9, 2); // res = float32.indexOf(9, 2);
expect(res).toEqual(2); // console.log(res); // 2
// expect(res).toEqual(2);
res = float32.indexOf(2, -1); // res = float32.indexOf(2, -1);
expect(res).toEqual(-1); // console.log(res); // -1
// expect(res).toEqual(-1);
// res = float32.indexOf(2, -3);
// console.log(res); // 0
res = float32.indexOf(2, -3);
expect(res).toEqual(0); expect(res).toEqual(0);
// #END
} }
join() { join() {
// #TEST Float32Array.join // #TEST Float32Array.join
let float32 = new Float32Array([1, 2, 3]); let float32 = new Float32Array([1, 2, 3]);
let res = float32.join(); let res = float32.join();
console.log(res); // 1,2,3
// #END
expect(res).toEqual("1,2,3"); expect(res).toEqual("1,2,3");
res = float32.join(" / "); // res = float32.join(" / ");
expect(res).toEqual("1 / 2 / 3"); // console.log(res); // 1 / 2 / 3
// expect(res).toEqual("1 / 2 / 3");
res = float32.join(""); // res = float32.join("");
expect(res).toEqual("123"); // console.log(res); // 123
// #END // expect(res).toEqual("123");
} }
keys() { keys() {
// #TEST Float32Array.keys // #TEST Float32Array.keys
let arr = new Float32Array([10, 20, 30, 40, 50]); let arr = new Float32Array([10, 20, 30, 40, 50]);
let keys = arr.keys(); let keys = arr.keys();
expect(keys.next().value).toEqual(0); let ret = keys.next().value
expect(keys.next().value).toEqual(1); console.log(ret); // 0
expect(keys.next().value).toEqual(2);
expect(keys.next().value).toEqual(3);
expect(keys.next().value).toEqual(4);
// #END // #END
expect(ret).toEqual(0);
// console.log(keys.next().value); // 1
// expect(keys.next().value).toEqual(1);
// console.log(keys.next().value); // 2
// expect(keys.next().value).toEqual(2);
// console.log(keys.next().value); // 3
// expect(keys.next().value).toEqual(3);
// console.log(keys.next().value); // 4
// expect(keys.next().value).toEqual(4);
} }
map() { map() {
// #TEST Float32Array.map // #TEST Float32Array.map
let numbers = new Float32Array([1, 4, 9]); let numbers = new Float32Array([1, 4, 9]);
let doubles = numbers.map((value : number, _ : number, _a : Float32Array) : number => value * 2); let doubles = numbers.map((value : number, _ : number, _a : Float32Array) : number => value * 2);
expect(numbers.toString()).toEqual("1,4,9"); console.log(doubles.toString()); // 2,8,18
expect(doubles.toString()).toEqual("2,8,18");
// #END // #END
expect(doubles.toString()).toEqual("2,8,18");
} }
reduce() { reduce() {
// #TEST Float32Array.reduce // #TEST Float32Array.reduce
let total = new Float32Array([0, 1, 2, 3]); let total = new Float32Array([0, 1, 2, 3]);
let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number => accumulator + currentValue); let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number => accumulator + currentValue);
console.log(res); // 6
// #END
expect(res).toEqual(6); expect(res).toEqual(6);
total = new Float32Array([0, 1, 2, 3]); // total = new Float32Array([0, 1, 2, 3]);
res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number => accumulator + currentValue, 8); // res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number => accumulator + currentValue, 8);
expect(res).toEqual(14); // console.log(res); // 14
// #END // expect(res).toEqual(14);
} }
reduceRight() { reduceRight() {
// #TEST Float32Array.reduceRight // #TEST Float32Array.reduceRight
let total = new Float32Array([0, 1, 2, 3]); let total = new Float32Array([0, 1, 2, 3]);
let res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number => accumulator + currentValue); let res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number => accumulator + currentValue);
console.log(res); // 6
// #END
expect(res).toEqual(6); expect(res).toEqual(6);
total = new Float32Array([0, 1, 2, 3]); // total = new Float32Array([0, 1, 2, 3]);
res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number => accumulator + currentValue, 8); // res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number => accumulator + currentValue, 8);
expect(res).toEqual(14); // console.log(res); // 14
// #END // // #END
// expect(res).toEqual(14);
} }
reverse() { reverse() {
// #TEST Float32Array.reverse // #TEST Float32Array.reverse
let float32 = new Float32Array([1, 2, 3]); let float32 = new Float32Array([1, 2, 3]);
float32.reverse(); float32.reverse();
expect(float32.toString()).toEqual("3,2,1"); console.log(float32.toString()); // 3,2,1
// #END // #END
expect(float32.toString()).toEqual("3,2,1");
} }
slice() { slice() {
// #TEST Float32Array.slice // #TEST Float32Array.slice
let float32 = new Float32Array([1, 2, 3]); let float32 = new Float32Array([1, 2, 3]);
let res = float32.slice(1); let res = float32.slice(1);
console.log(res.toString()); // 2,3
// #END
expect(res.toString()).toEqual("2,3"); expect(res.toString()).toEqual("2,3");
res = float32.slice(2); // res = float32.slice(2);
expect(res.toString()).toEqual("3"); // console.log(res.toString()); // 3
// expect(res.toString()).toEqual("3");
res = float32.slice(-2); // res = float32.slice(-2);
expect(res.toString()).toEqual("2,3"); // console.log(res.toString()); // 2,3
// expect(res.toString()).toEqual("2,3");
res = float32.slice(0, 1); // res = float32.slice(0, 1);
expect(res.toString()).toEqual("1"); // console.log(res.toString()); // 1
// #END // expect(res.toString()).toEqual("1");
} }
sort() { sort() {
// #TEST Float32Array.sort // #TEST Float32Array.sort
let numbers = new Float32Array([40, 1, 5]); let numbers = new Float32Array([40, 1, 5]);
numbers.sort(); numbers.sort();
console.log(numbers.toString()); // 1,5,40
// #END
expect(numbers.toString()).toEqual("1,5,40"); expect(numbers.toString()).toEqual("1,5,40");
numbers.sort((a, b) : number => a - b); // numbers.sort((a, b) : number => a - b);
expect(numbers.toString()).toEqual("1,5,40"); // console.log(numbers.toString()); // 1,5,40
// #END // expect(numbers.toString()).toEqual("1,5,40");
} }
subarray() { subarray() {
...@@ -304,21 +354,26 @@ export class TFloat32Array { ...@@ -304,21 +354,26 @@ export class TFloat32Array {
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let float32 = new Float32Array(buffer); let float32 = new Float32Array(buffer);
float32.set([1, 2, 3]); float32.set([1, 2, 3]);
console.log(float32.toString()); // 1,2,3,0
// #END
expect(float32.toString()).toEqual("1,2,3,0"); expect(float32.toString()).toEqual("1,2,3,0");
let sub = float32.subarray(0, 4); // let sub = float32.subarray(0, 4);
expect(sub.toString()).toEqual("1,2,3,0"); // console.log(sub.toString()); // 1,2,3,0
// #END // expect(sub.toString()).toEqual("1,2,3,0");
} }
values() { values() {
// #TEST Float32Array.values // #TEST Float32Array.values
let arr = new Float32Array([1, 2, 3]); let arr = new Float32Array([1, 2, 3]);
let values = arr.values(); let values = arr.values().next().value;
expect(values.next().value).toEqual(1); console.log(values); // 1
expect(values.next().value).toEqual(2);
expect(values.next().value).toEqual(3);
// #END // #END
expect(values).toEqual(1);
// console.log(values.next().value); // 2
// expect(values.next().value).toEqual(2);
// console.log(values.next().value); // 3
// expect(values.next().value).toEqual(3);
} }
arrayBufferSlice() { arrayBufferSlice() {
...@@ -326,12 +381,14 @@ export class TFloat32Array { ...@@ -326,12 +381,14 @@ export class TFloat32Array {
let buffer = new ArrayBuffer(16); let buffer = new ArrayBuffer(16);
let float32 = new Float32Array(buffer); let float32 = new Float32Array(buffer);
float32[3] = 42; float32[3] = 42;
console.log(float32.toString()); // 0,0,0,42
// #END
expect(float32.toString()).toEqual("0,0,0,42"); expect(float32.toString()).toEqual("0,0,0,42");
let res = buffer.slice(8); // let res = buffer.slice(8);
let sliced = new Float32Array(res); // let sliced = new Float32Array(res);
expect(sliced[1]).toEqual(42); // console.log(sliced[1]); // 42
// #END // expect(sliced[1]).toEqual(42);
} }
testSome() { testSome() {
...@@ -339,15 +396,22 @@ export class TFloat32Array { ...@@ -339,15 +396,22 @@ export class TFloat32Array {
const float32 = new Float32Array([-10, 20, -30, 40, -50]); const float32 = new Float32Array([-10, 20, -30, 40, -50]);
const positives = new Float32Array([10, 20, 30, 40, 50]); const positives = new Float32Array([10, 20, 30, 40, 50]);
expect(float32.some((element : number, index : number, array : Float32Array) : boolean => console.log(float32.some((element : number, index : number, array : Float32Array) : boolean =>
element < 0 element < 0
)).toEqual(true); )); // true
console.log(positives.some((element : number, index : number, array : Float32Array) : boolean =>
element < 0
)); // false
// #END
expect(positives.some((element : number, index : number, array : Float32Array) : boolean => expect(positives.some((element : number, index : number, array : Float32Array) : boolean =>
element < 0 element < 0
)).toEqual(false); )).toEqual(false);
// #END
expect(float32.some((element : number, index : number, array : Float32Array) : boolean =>
element < 0
)).toEqual(true);
} }
// #endif // #endif
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册