nv-facutil-slct-ta 1.0.0 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/TEST/check.js +180 -0
- package/index.js +143 -5
- package/package.json +1 -1
package/TEST/check.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
const assert = require("assert");
|
|
2
|
+
|
|
3
|
+
// ====== 你的 cp 函数 ======
|
|
4
|
+
|
|
5
|
+
const AB_CLSES = [ArrayBuffer];
|
|
6
|
+
if (globalThis.SharedArrayBuffer !== undefined) {
|
|
7
|
+
AB_CLSES.push(globalThis.SharedArrayBuffer)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const TA_CLSES = [
|
|
11
|
+
Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array, BigUint64Array,
|
|
12
|
+
Int8Array, Int16Array, Int32Array, BigInt64Array,
|
|
13
|
+
Float32Array, Float64Array
|
|
14
|
+
];
|
|
15
|
+
if (globalThis.Float16Array !== undefined) {
|
|
16
|
+
TA_CLSES.push(globalThis.Float16Array)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const VWLIKE_CLSES = [DataView];
|
|
20
|
+
if (globalThis.Buffer !== undefined) { VWLIKE_CLSES.push(globalThis.Buffer) }
|
|
21
|
+
if (globalThis.Blob !== undefined) { VWLIKE_CLSES.push(globalThis.Blob) }
|
|
22
|
+
if (globalThis.File !== undefined) { VWLIKE_CLSES.push(globalThis.File) }
|
|
23
|
+
|
|
24
|
+
const BUFLIKE_CLSES = [...AB_CLSES, ...TA_CLSES, ...VWLIKE_CLSES];
|
|
25
|
+
|
|
26
|
+
function cp(o) {
|
|
27
|
+
var Cls = o.constructor;
|
|
28
|
+
if (TA_CLSES.includes(Cls) || Cls === DataView) {
|
|
29
|
+
var ab = o.buffer.slice(o.byteOffset, o.byteOffset + o.byteLength)
|
|
30
|
+
return new Cls(ab);
|
|
31
|
+
} else if (Cls === globalThis.Buffer) {
|
|
32
|
+
return globalThis?.Buffer?.from(o);
|
|
33
|
+
} else {
|
|
34
|
+
return o.slice(0)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ====== 测试工具 ======
|
|
39
|
+
|
|
40
|
+
function eq_buf(a, b) {
|
|
41
|
+
assert.strictEqual(a.byteLength, b.byteLength);
|
|
42
|
+
const ua = new Uint8Array(a);
|
|
43
|
+
const ub = new Uint8Array(b);
|
|
44
|
+
for (let i = 0; i < ua.length; i++) {
|
|
45
|
+
assert.strictEqual(ua[i], ub[i]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function deep_test(orig, copy) {
|
|
50
|
+
|
|
51
|
+
assert.notStrictEqual(orig, copy);
|
|
52
|
+
|
|
53
|
+
if (orig instanceof ArrayBuffer || orig instanceof SharedArrayBuffer) {
|
|
54
|
+
eq_buf(orig, copy);
|
|
55
|
+
}
|
|
56
|
+
else if (ArrayBuffer.isView(orig)) {
|
|
57
|
+
assert.strictEqual(orig.length, copy.length);
|
|
58
|
+
for (let i = 0; i < orig.length; i++) {
|
|
59
|
+
assert.strictEqual(orig[i], copy[i]);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else if (orig instanceof Buffer) {
|
|
63
|
+
assert.strictEqual(orig.equals(copy), true);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ====== TypedArray tests ======
|
|
68
|
+
|
|
69
|
+
for (let Cls of TA_CLSES) {
|
|
70
|
+
|
|
71
|
+
const ta = new Cls(8);
|
|
72
|
+
|
|
73
|
+
for (let i = 0; i < ta.length; i++) {
|
|
74
|
+
if(Cls === BigInt64Array || Cls === BigUint64Array) {ta[i] = BigInt(i + 1);} else {ta[i] = i + 1;}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const c = cp(ta);
|
|
78
|
+
|
|
79
|
+
deep_test(ta, c);
|
|
80
|
+
|
|
81
|
+
ta[0] = (Cls === BigInt64Array || Cls === BigUint64Array)?99n:99;
|
|
82
|
+
|
|
83
|
+
assert.notStrictEqual(ta[0], c[0]);
|
|
84
|
+
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ====== offset typedarray ======
|
|
88
|
+
|
|
89
|
+
const buf = new ArrayBuffer(32);
|
|
90
|
+
const ta = new Uint16Array(buf, 8, 4);
|
|
91
|
+
|
|
92
|
+
for (let i = 0; i < ta.length; i++) {
|
|
93
|
+
ta[i] = i + 10;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const ta_copy = cp(ta);
|
|
97
|
+
|
|
98
|
+
deep_test(ta, ta_copy);
|
|
99
|
+
|
|
100
|
+
ta[0] = 999;
|
|
101
|
+
|
|
102
|
+
assert.notStrictEqual(ta[0], ta_copy[0]);
|
|
103
|
+
|
|
104
|
+
// ====== DataView ======
|
|
105
|
+
|
|
106
|
+
const ab = new ArrayBuffer(16);
|
|
107
|
+
const dv = new DataView(ab);
|
|
108
|
+
|
|
109
|
+
dv.setUint32(0, 123456);
|
|
110
|
+
|
|
111
|
+
const dv2 = cp(dv);
|
|
112
|
+
|
|
113
|
+
assert.notStrictEqual(dv, dv2);
|
|
114
|
+
assert.strictEqual(dv2.getUint32(0), 123456);
|
|
115
|
+
|
|
116
|
+
dv.setUint32(0, 9);
|
|
117
|
+
|
|
118
|
+
assert.notStrictEqual(dv.getUint32(0), dv2.getUint32(0));
|
|
119
|
+
|
|
120
|
+
// ====== ArrayBuffer ======
|
|
121
|
+
|
|
122
|
+
const ab1 = new ArrayBuffer(8);
|
|
123
|
+
const v1 = new Uint8Array(ab1);
|
|
124
|
+
v1[0] = 42;
|
|
125
|
+
|
|
126
|
+
const ab2 = cp(ab1);
|
|
127
|
+
|
|
128
|
+
eq_buf(ab1, ab2);
|
|
129
|
+
|
|
130
|
+
v1[0] = 1;
|
|
131
|
+
|
|
132
|
+
assert.notStrictEqual(new Uint8Array(ab2)[0], v1[0]);
|
|
133
|
+
|
|
134
|
+
// ====== Buffer (Node) ======
|
|
135
|
+
|
|
136
|
+
if (globalThis.Buffer) {
|
|
137
|
+
|
|
138
|
+
const b = Buffer.from([1, 2, 3, 4]);
|
|
139
|
+
|
|
140
|
+
const b2 = cp(b);
|
|
141
|
+
|
|
142
|
+
assert.notStrictEqual(b, b2);
|
|
143
|
+
|
|
144
|
+
assert.strictEqual(b.equals(b2), true);
|
|
145
|
+
|
|
146
|
+
b[0] = 9;
|
|
147
|
+
|
|
148
|
+
assert.notStrictEqual(b[0], b2[0]);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ====== Blob ======
|
|
152
|
+
|
|
153
|
+
if (globalThis.Blob) {
|
|
154
|
+
|
|
155
|
+
const blob = new Blob(["hello"]);
|
|
156
|
+
|
|
157
|
+
const blob2 = cp(blob);
|
|
158
|
+
|
|
159
|
+
assert.notStrictEqual(blob, blob2);
|
|
160
|
+
|
|
161
|
+
assert.strictEqual(blob.size, blob2.size);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ====== File (browser) ======
|
|
165
|
+
|
|
166
|
+
if (globalThis.File) {
|
|
167
|
+
|
|
168
|
+
const file = new File(["abc"], "a.txt");
|
|
169
|
+
|
|
170
|
+
const file2 = cp(file);
|
|
171
|
+
|
|
172
|
+
assert.notStrictEqual(file, file2);
|
|
173
|
+
|
|
174
|
+
assert.strictEqual(file.size, file2.size);
|
|
175
|
+
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
console.log("ALL TEST PASS");
|
|
179
|
+
|
|
180
|
+
|
package/index.js
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
function check_sz_and_algn(sz,algnsz) {
|
|
2
|
+
if(sz<algnsz) {
|
|
3
|
+
return [false,`size(${sz}) MUST >= algnsz(${algnsz})`]
|
|
4
|
+
} else if(sz % algnsz !== 0) {
|
|
5
|
+
return [false,`size(${sz}) % algnsz(${algnsz}) = ${sz % algnsz} MUST == 0`]
|
|
6
|
+
} else {
|
|
7
|
+
return [true,null]
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
|
|
1
12
|
function calc_algn_si(si,algnsz) {
|
|
2
13
|
var r = si % algnsz;
|
|
3
14
|
if(r === 0) {
|
|
@@ -32,10 +43,10 @@ function get_ta_cls_with_max_cnt(cnt,using_u64_when_gt_safe_f64_int=false) {
|
|
|
32
43
|
}
|
|
33
44
|
|
|
34
45
|
const DUMP_ONE_BYTE_TYPE_MP = {
|
|
35
|
-
0:
|
|
36
|
-
1:
|
|
37
|
-
2:
|
|
38
|
-
3:
|
|
46
|
+
0: Uint8Array,
|
|
47
|
+
1: Uint16Array,
|
|
48
|
+
2: Uint32Array,
|
|
49
|
+
3: BigUint64Array, // js version NOT supported
|
|
39
50
|
255: Float64Array,
|
|
40
51
|
}
|
|
41
52
|
|
|
@@ -63,12 +74,139 @@ function get_display_name_prefix(TypedArrayClass) {
|
|
|
63
74
|
return "Large";
|
|
64
75
|
}
|
|
65
76
|
|
|
77
|
+
const BRIEF_MIRR = new Map();
|
|
78
|
+
BRIEF_MIRR.set("u8a",Uint8Array);
|
|
79
|
+
BRIEF_MIRR.set("u8ca",Uint8ClampedArray);
|
|
80
|
+
BRIEF_MIRR.set("u16a",Uint16Array);
|
|
81
|
+
BRIEF_MIRR.set("u32a",Uint16Array);
|
|
82
|
+
BRIEF_MIRR.set("u64a",BigUint64Array);
|
|
83
|
+
BRIEF_MIRR.set("i8a", Int8Array);
|
|
84
|
+
BRIEF_MIRR.set("i16a", Int16Array);
|
|
85
|
+
BRIEF_MIRR.set("i32a", Int32Array);
|
|
86
|
+
BRIEF_MIRR.set("i64a", BigInt64Array);
|
|
87
|
+
BRIEF_MIRR.set("f32a", Float32Array);
|
|
88
|
+
BRIEF_MIRR.set("f64a", Float64Array);
|
|
89
|
+
if (globalThis.Float16Array !== undefined) {BRIEF_MIRR.set("f16a", globalThis.Float16Array);}
|
|
90
|
+
BRIEF_MIRR.set("dv", DataView);
|
|
91
|
+
if (globalThis.Buffer !== undefined) {BRIEF_MIRR.set("buf", globalThis.Buffer);}
|
|
92
|
+
if (globalThis.Blob !== undefined) {BRIEF_MIRR.set("blob", globalThis.Blob);}
|
|
93
|
+
if (globalThis.File !== undefined) {BRIEF_MIRR.set("file", globalThis.File);}
|
|
94
|
+
BRIEF_MIRR.set(Uint8Array,"u8a");
|
|
95
|
+
BRIEF_MIRR.set(Uint8ClampedArray, "u8ca");
|
|
96
|
+
BRIEF_MIRR.set(Uint16Array, "u16a");
|
|
97
|
+
BRIEF_MIRR.set(Uint32Array, "u32a");
|
|
98
|
+
BRIEF_MIRR.set(BigUint64Array, "u64a");
|
|
99
|
+
BRIEF_MIRR.set(Int8Array, "i8a");
|
|
100
|
+
BRIEF_MIRR.set(Int16Array, "i16a");
|
|
101
|
+
BRIEF_MIRR.set(Int32Array, "i32a");
|
|
102
|
+
BRIEF_MIRR.set(BigInt64Array, "i64a");
|
|
103
|
+
BRIEF_MIRR.set(Float32Array, "f32a");
|
|
104
|
+
BRIEF_MIRR.set(Float64Array, "f64a");
|
|
105
|
+
|
|
106
|
+
if (globalThis.Float16Array !== undefined) {
|
|
107
|
+
BRIEF_MIRR.set(globalThis.Float16Array, "f16a");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
BRIEF_MIRR.set(DataView, "dv");
|
|
111
|
+
|
|
112
|
+
if (globalThis.Buffer !== undefined) {
|
|
113
|
+
BRIEF_MIRR.set(globalThis.Buffer, "buf");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (globalThis.Blob !== undefined) {
|
|
117
|
+
BRIEF_MIRR.set(globalThis.Blob, "blob");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (globalThis.File !== undefined) {
|
|
121
|
+
BRIEF_MIRR.set(globalThis.File, "file");
|
|
122
|
+
}
|
|
123
|
+
function get_brief_name(Cls) {return BRIEF_MIRR.get(Cls)}
|
|
124
|
+
function get_cls_from_brief_name(brief) {return BRIEF_MIRR.get(brief.toLowerCase())}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
////
|
|
129
|
+
const AB_CLSES = [ArrayBuffer];
|
|
130
|
+
if(globalThis.SharedArrayBuffer!== undefined) { AB_CLSES.push(globalThis.SharedArrayBuffer)}
|
|
131
|
+
|
|
132
|
+
const TA_CLSES = [
|
|
133
|
+
Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array,BigUint64Array,
|
|
134
|
+
Int8Array, Int16Array,Int32Array,BigInt64Array,
|
|
135
|
+
Float32Array,Float64Array
|
|
136
|
+
];
|
|
137
|
+
if(globalThis.Float16Array!== undefined) { TA_CLSES.push(globalThis.Float16Array)}
|
|
138
|
+
|
|
139
|
+
const VWLIKE_CLSES = [DataView];
|
|
140
|
+
if(globalThis.Buffer!== undefined) { VWLIKE_CLSES.push(globalThis.Buffer)}
|
|
141
|
+
if(globalThis.Blob!== undefined) { VWLIKE_CLSES.push(globalThis.Blob)}
|
|
142
|
+
if(globalThis.File!== undefined) { VWLIKE_CLSES.push(globalThis.File)}
|
|
143
|
+
|
|
144
|
+
const BUFLIKE_CLSES = [...AB_CLSES,...TA_CLSES,...VWLIKE_CLSES];
|
|
145
|
+
|
|
146
|
+
function cp(o) {
|
|
147
|
+
var Cls = o.constructor;
|
|
148
|
+
if(Cls === DataView ) {
|
|
149
|
+
var ab = o.buffer.slice(o.byteOffset,o.byteOffset+o.byteLength)
|
|
150
|
+
return new Cls(ab);
|
|
151
|
+
} else if(Cls === globalThis.Buffer) {
|
|
152
|
+
return globalThis?.Buffer?.from(o);
|
|
153
|
+
} else {
|
|
154
|
+
return o.slice(0)
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function setv(ta,i,v) {
|
|
159
|
+
if(ta.constructor.BYTES_PER_ELEMENT === 8 && ta.constructor !== Float64Array) {
|
|
160
|
+
ta[i] = BigInt(v)
|
|
161
|
+
} else {
|
|
162
|
+
ta[i] = v;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function is_readonly(o) {
|
|
167
|
+
if(globalThis.Blob!== undefined && o.constructor === globalThis.Blob) { return true}
|
|
168
|
+
if(globalThis.File!== undefined && o.constructor === globalThis.File) { return true}
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function creat_debug_view(o) {
|
|
173
|
+
var ab = o.buffer; var offset = o.byteOffset; var sz = o.byteLength;
|
|
174
|
+
var view = {};
|
|
175
|
+
view.u8a = new Uint8Array(ab,offset,sz);
|
|
176
|
+
view.u8ca = new Uint8ClampedArray(ab,offset,sz);
|
|
177
|
+
view.u16a = new Uint16Array(ab,offset,sz/2);
|
|
178
|
+
view.u32a = new Uint32Array(ab,offset,sz/4);
|
|
179
|
+
view.u64a = new BigUint64Array(ab,offset,sz/8);
|
|
180
|
+
view.i8a = new Int8Array(ab,offset,sz);
|
|
181
|
+
view.i16a = new Int16Array(ab,offset,sz/2);
|
|
182
|
+
view.i32a = new Int32Array(ab,offset,sz/4);
|
|
183
|
+
view.i64a = new BigInt64Array(ab,offset,sz/8);
|
|
184
|
+
if (globalThis.Float16Array !== undefined) {view.f16a=new Float16Array(ab,offset,sz/2);;}
|
|
185
|
+
view.f32a = new Float32Array(ab,offset,sz/4);
|
|
186
|
+
view.f64a = new Float64Array(ab,offset,sz/8);
|
|
187
|
+
view.dv = new DataView(ab,offset,sz);
|
|
188
|
+
return view;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
|
|
66
192
|
module.exports = {
|
|
193
|
+
check_sz_and_algn,
|
|
67
194
|
calc_algn_si,
|
|
68
195
|
calc_max_incr_cnt,
|
|
69
196
|
get_ta_cls_with_max_cnt,
|
|
70
197
|
get_cls_from_dump_fst_byte,
|
|
71
198
|
get_fst_byte_for_dump,
|
|
72
199
|
set_fst_byte_for_dump,
|
|
73
|
-
|
|
200
|
+
////
|
|
201
|
+
BRIEF_MIRR,
|
|
202
|
+
get_display_name_prefix,
|
|
203
|
+
get_brief_name,get_cls_from_brief_name,
|
|
204
|
+
////
|
|
205
|
+
AB_CLSES,TA_CLSES,VWLIKE_CLSES,BUFLIKE_CLSES,
|
|
206
|
+
////
|
|
207
|
+
cp,
|
|
208
|
+
setv,
|
|
209
|
+
is_readonly,
|
|
210
|
+
////
|
|
211
|
+
creat_debug_view
|
|
74
212
|
}
|