nv-buf-resizable-ring 1.0.0
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-ring.js +153 -0
- package/TEST/index.html +105614 -0
- package/TEST/perf-ring.js +69 -0
- package/TEST/perf-ring2.js +57 -0
- package/index.js +171 -0
- package/package.json +11 -0
- package/ring.js +314 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
const _ring = require("../ring");
|
|
2
|
+
{
|
|
3
|
+
// 创建一个逻辑容量为 8 的 Uint8 Ring
|
|
4
|
+
const ring = _ring.creat_u8ring(8);
|
|
5
|
+
const target = ring["#target"] || ring; // 兼容 Proxy 模式
|
|
6
|
+
|
|
7
|
+
console.log("--- 1. 基础填充测试 ---");
|
|
8
|
+
ring.apend(new Uint8Array([1, 2, 3, 4]));
|
|
9
|
+
console.log("Size after 4 bytes:", ring.size()); // 4
|
|
10
|
+
console.log("Buffer content:", Array.from({length: ring.size()}, (_, i) => ring.get(i))); // [1, 2, 3, 4]
|
|
11
|
+
|
|
12
|
+
console.log("\n--- 2. 物理回绕测试 ---");
|
|
13
|
+
ring.dloc(2); // 释放 [1, 2],此时 si=2, ei=4
|
|
14
|
+
ring.apend(new Uint8Array([5, 6, 7, 8]));
|
|
15
|
+
// 此时 si=2, ei 应该回绕了。总 size: (4-2) + 4 = 6
|
|
16
|
+
console.log("Size after wrap:", ring.size()); // 6
|
|
17
|
+
console.log("Data at logical index 0:", ring.get(0)); // 3 (原先的索引2)
|
|
18
|
+
console.log("Logical full data:", Array.from({length: ring.size()}, (_, i) => ring.get(i))); // [3, 4, 5, 6, 7, 8]
|
|
19
|
+
|
|
20
|
+
console.log("\n--- 3. 扩容测试 ---");
|
|
21
|
+
// 当前 size 为 6,再加 5 个字节,总计 11,超过容量 8
|
|
22
|
+
ring.apend(new Uint8Array([9, 10, 11, 12, 13]));
|
|
23
|
+
console.log("Size after expansion:", ring.size()); // 11
|
|
24
|
+
console.log("Buffer length (physical):", target.buf.length); // 应该是 8*2+1 = 17 或更大
|
|
25
|
+
console.log("All data after expansion:", Array.from({length: ring.size()}, (_, i) => ring.get(i)));
|
|
26
|
+
// 应完整保留顺序: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
|
|
27
|
+
|
|
28
|
+
console.log("\n--- 4. Set/Get 边界与 Proxy 测试 ---");
|
|
29
|
+
const px = _ring.creat_u8px(8);
|
|
30
|
+
px.apend(new Uint8Array([100, 101]));
|
|
31
|
+
console.log("px[0]:", px[0]); // 100
|
|
32
|
+
px[1] = 200; // 修改
|
|
33
|
+
console.log("px[1] after set:", px[1]); // 200
|
|
34
|
+
px[2] = 102; // 触发 set 中的 offset === size() 自动追加
|
|
35
|
+
console.log("px size after auto-append:", px.size()); // 3
|
|
36
|
+
}
|
|
37
|
+
// ANSI Colors
|
|
38
|
+
const C = {
|
|
39
|
+
R: "\x1b[31m", G: "\x1b[32m", Y: "\x1b[33m",
|
|
40
|
+
B: "\x1b[34m", M: "\x1b[35m", C: "\x1b[36m", W: "\x1b[0m"
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const ring = _ring.creat_u8ring(8);
|
|
44
|
+
const target = ring["#target"] || ring;
|
|
45
|
+
|
|
46
|
+
function report(action, expected, actual, extra = "") {
|
|
47
|
+
const isOk = JSON.stringify(expected) === JSON.stringify(actual);
|
|
48
|
+
console.log(`${C.W}[${action}]`);
|
|
49
|
+
console.log(` Expected: ${C.G}${JSON.stringify(expected)}${C.W}`);
|
|
50
|
+
console.log(` Actual: ${isOk ? C.G : C.R}${JSON.stringify(actual)}${C.W}`);
|
|
51
|
+
console.log(` Status: ${C.C}si:${target.si}, ei:${target.ei}, size:${ring.size()}${C.W} ${extra}`);
|
|
52
|
+
console.log("-".repeat(40));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
console.log(`${C.M}=== ResizableRingBuffer Deep Inspection ===${C.W}\n`);
|
|
56
|
+
|
|
57
|
+
// 1. 初始填充
|
|
58
|
+
ring.apend(new Uint8Array([10, 20, 30, 40]));
|
|
59
|
+
report("Append 4b", [10, 20, 30, 40], Array.from(ring));
|
|
60
|
+
|
|
61
|
+
// 2. 制造回绕
|
|
62
|
+
ring.dloc(3); // 剩 [40]
|
|
63
|
+
ring.apend(new Uint8Array([50, 60, 70, 80])); // 此时逻辑 0 位物理在后面,其余在前面
|
|
64
|
+
report("Wrap Move", [40, 50, 60, 70, 80], Array.from(ring), "(Physical Wrap Triggered)");
|
|
65
|
+
|
|
66
|
+
// 3. 测试 for_each 逻辑与物理索引映射
|
|
67
|
+
const trace = [];
|
|
68
|
+
ring.for_each((v, i, p) => {
|
|
69
|
+
trace.push({v, i, p});
|
|
70
|
+
return false; // don't break
|
|
71
|
+
});
|
|
72
|
+
console.log(`${C.Y}[ForEach Mapping Inspection]${C.W}`);
|
|
73
|
+
trace.forEach(t => {
|
|
74
|
+
console.log(` Logic[${t.i}] -> ${C.B}Phys[${t.p}]${C.W} : Value(${t.v})`);
|
|
75
|
+
});
|
|
76
|
+
console.log("-".repeat(40));
|
|
77
|
+
|
|
78
|
+
// 4. 扩容压力测试
|
|
79
|
+
const bigData = [90, 100, 110, 120, 130];
|
|
80
|
+
ring.apend(new Uint8Array(bigData));
|
|
81
|
+
report("Expansion", [40, 50, 60, 70, 80, 90, 100, 110, 120, 130], Array.from(ring));
|
|
82
|
+
|
|
83
|
+
// 5. Iterator 测试
|
|
84
|
+
const iterResult = [];
|
|
85
|
+
for(let v of ring) iterResult.push(v);
|
|
86
|
+
report("Symbol.iterator", [40, 50, 60, 70, 80, 90, 100, 110, 120, 130], iterResult);
|
|
87
|
+
|
|
88
|
+
{
|
|
89
|
+
const C = {
|
|
90
|
+
R: "\x1b[31m", G: "\x1b[32m", Y: "\x1b[33m",
|
|
91
|
+
B: "\x1b[34m", M: "\x1b[35m", C: "\x1b[36m",
|
|
92
|
+
GR: "\x1b[90m", W: "\x1b[0m", INV: "\x1b[7m"
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
function dump_physical(ring) {
|
|
96
|
+
const target = ring["#target"] || ring;
|
|
97
|
+
const buf = target.buf;
|
|
98
|
+
const { si, ei } = target;
|
|
99
|
+
|
|
100
|
+
let parts = [];
|
|
101
|
+
for (let i = 0; i < buf.length; i++) {
|
|
102
|
+
let val = buf[i].toString().padStart(2, '0');
|
|
103
|
+
let style = "";
|
|
104
|
+
|
|
105
|
+
// 标记指针位置
|
|
106
|
+
if (i === si && i === ei) style = C.R + C.INV; // 重合(理论上只有reset或极小容量出现)
|
|
107
|
+
else if (i === si) style = C.G + C.INV; // si 起点
|
|
108
|
+
else if (i === ei) style = C.Y + C.INV; // ei 终点
|
|
109
|
+
|
|
110
|
+
// 标记数据有效区
|
|
111
|
+
let is_used = false;
|
|
112
|
+
if (si <= ei) {
|
|
113
|
+
is_used = i >= si && i < ei;
|
|
114
|
+
} else {
|
|
115
|
+
is_used = i >= si || i < ei;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!style && !is_used) style = C.GR; // 无效区变灰
|
|
119
|
+
parts.push(`${style}${val}${C.W}`);
|
|
120
|
+
}
|
|
121
|
+
return `[ ${parts.join(" ")} ]`;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function report(ring, action, expected) {
|
|
125
|
+
const actual = Array.from(ring);
|
|
126
|
+
const isOk = JSON.stringify(expected) === JSON.stringify(actual);
|
|
127
|
+
const target = ring["#target"] || ring;
|
|
128
|
+
|
|
129
|
+
console.log(`\n${C.M}>> ACTION: ${action}${C.W}`);
|
|
130
|
+
console.log(`${C.Y}Logic Exp: ${JSON.stringify(expected)}${C.W}`);
|
|
131
|
+
console.log(`${C.G}Logic Act: ${isOk ? C.G : C.R}${JSON.stringify(actual)}${C.W}`);
|
|
132
|
+
console.log(`${C.C}Phys Info: si=${target.si}, ei=${target.ei}, len=${target.buf.length}${C.W}`);
|
|
133
|
+
console.log(`${C.B}Raw Buf: ${dump_physical(ring)}${C.W}`);
|
|
134
|
+
console.log(`${C.GR}${"-".repeat(60)}${C.W}`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 开始测试
|
|
138
|
+
const ring = _ring.creat_u8px(8); // 物理长度 9
|
|
139
|
+
|
|
140
|
+
report(ring, "Initial", []);
|
|
141
|
+
|
|
142
|
+
ring.apend(new Uint8Array([1, 2, 3]));
|
|
143
|
+
report(ring, "Append 3b", [1, 2, 3]);
|
|
144
|
+
|
|
145
|
+
ring.dloc(2);
|
|
146
|
+
report(ring, "Dloc 2b (si move)", [3]);
|
|
147
|
+
|
|
148
|
+
ring.apend(new Uint8Array([4, 5, 6, 7, 8]));
|
|
149
|
+
report(ring, "Append 5b (wrap around)", [3, 4, 5, 6, 7, 8]);
|
|
150
|
+
|
|
151
|
+
ring.apend(new Uint8Array([9, 10])); // 触发扩容
|
|
152
|
+
report(ring, "Append 2b (expansion)", [3, 4, 5, 6, 7, 8, 9, 10]);
|
|
153
|
+
}
|