-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-wasm.js
More file actions
263 lines (227 loc) · 7.33 KB
/
test-wasm.js
File metadata and controls
263 lines (227 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* Test WASM module correctness against JS implementation.
*/
'use strict';
const baoWasm = require('./bao-wasm.js');
const baoJs = require('./bao.js');
function toHex(arr) {
return Array.from(arr).map(b => b.toString(16).padStart(2, '0')).join('');
}
function arraysEqual(a, b) {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
async function runTests() {
console.log('Initializing WASM...');
const wasmAvailable = await baoWasm.initWasm();
console.log('WASM available:', wasmAvailable);
if (!wasmAvailable) {
console.log('WASM not available, tests will use JS fallback');
}
let passed = 0;
let failed = 0;
// Test 1: Empty chunk CV
console.log('\n--- Test 1: Empty chunk CV ---');
{
const jsResult = baoJs.chunkCV(new Uint8Array(0), 0, true);
const wasmResult = baoWasm.chunkCV(new Uint8Array(0), 0, true);
console.log('JS: ', toHex(jsResult));
console.log('WASM:', toHex(wasmResult));
if (arraysEqual(jsResult, wasmResult)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL: Results differ');
failed++;
}
}
// Test 2: Single byte chunk CV
console.log('\n--- Test 2: Single byte chunk CV ---');
{
const data = new Uint8Array([0x42]);
const jsResult = baoJs.chunkCV(data, 0, true);
const wasmResult = baoWasm.chunkCV(data, 0, true);
console.log('JS: ', toHex(jsResult));
console.log('WASM:', toHex(wasmResult));
if (arraysEqual(jsResult, wasmResult)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL: Results differ');
failed++;
}
}
// Test 3: Full chunk (1024 bytes)
console.log('\n--- Test 3: Full 1024-byte chunk CV ---');
{
const data = new Uint8Array(1024);
for (let i = 0; i < 1024; i++) data[i] = i & 0xff;
const jsResult = baoJs.chunkCV(data, 0, false);
const wasmResult = baoWasm.chunkCV(data, 0, false);
console.log('JS: ', toHex(jsResult));
console.log('WASM:', toHex(wasmResult));
if (arraysEqual(jsResult, wasmResult)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL: Results differ');
failed++;
}
}
// Test 4: Chunk with non-zero index
console.log('\n--- Test 4: Chunk with index=5 ---');
{
const data = new Uint8Array(1024).fill(0xaa);
const jsResult = baoJs.chunkCV(data, 5, false);
const wasmResult = baoWasm.chunkCV(data, 5, false);
console.log('JS: ', toHex(jsResult));
console.log('WASM:', toHex(wasmResult));
if (arraysEqual(jsResult, wasmResult)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL: Results differ');
failed++;
}
}
// Test 5: Parent CV
console.log('\n--- Test 5: Parent CV ---');
{
const leftCV = new Uint8Array(32);
const rightCV = new Uint8Array(32);
for (let i = 0; i < 32; i++) {
leftCV[i] = i;
rightCV[i] = 255 - i;
}
const jsResult = baoJs.parentCV(leftCV, rightCV, false);
const wasmResult = baoWasm.parentCV(leftCV, rightCV, false);
console.log('JS: ', toHex(jsResult));
console.log('WASM:', toHex(wasmResult));
if (arraysEqual(jsResult, wasmResult)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL: Results differ');
failed++;
}
}
// Test 6: Parent CV as root
console.log('\n--- Test 6: Parent CV (root) ---');
{
const leftCV = new Uint8Array(32).fill(0x11);
const rightCV = new Uint8Array(32).fill(0x22);
const jsResult = baoJs.parentCV(leftCV, rightCV, true);
const wasmResult = baoWasm.parentCV(leftCV, rightCV, true);
console.log('JS: ', toHex(jsResult));
console.log('WASM:', toHex(wasmResult));
if (arraysEqual(jsResult, wasmResult)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL: Results differ');
failed++;
}
}
// Test 7: Batch chunk CVs
console.log('\n--- Test 7: Batch chunk CVs (4 chunks) ---');
{
const data = new Uint8Array(4 * 1024);
for (let i = 0; i < data.length; i++) data[i] = (i * 7) & 0xff;
// JS sequential
const jsCVs = [];
for (let i = 0; i < 4; i++) {
const chunk = data.subarray(i * 1024, (i + 1) * 1024);
jsCVs.push(baoJs.chunkCV(chunk, i, false));
}
// WASM batch
const wasmCVs = baoWasm.batchChunkCVs(data, 0, 4);
let allMatch = true;
for (let i = 0; i < 4; i++) {
console.log(`Chunk ${i} JS: `, toHex(jsCVs[i]));
console.log(`Chunk ${i} WASM:`, toHex(wasmCVs[i]));
if (!arraysEqual(jsCVs[i], wasmCVs[i])) {
allMatch = false;
}
}
if (allMatch) {
console.log('PASS');
passed++;
} else {
console.log('FAIL: Some chunks differ');
failed++;
}
}
// Test 8: Bao encode comparison
console.log('\n--- Test 8: Bao encode (2KB) ---');
{
const data = new Uint8Array(2048);
for (let i = 0; i < data.length; i++) data[i] = (i * 13) & 0xff;
const jsResult = baoJs.baoEncode(data, false);
const wasmResult = baoWasm.baoEncodeWasm(data, false);
console.log('JS hash: ', toHex(jsResult.hash));
console.log('WASM hash:', toHex(wasmResult.hash));
const hashMatch = arraysEqual(jsResult.hash, wasmResult.hash);
const encodedMatch = arraysEqual(jsResult.encoded, wasmResult.encoded);
console.log('Hash match:', hashMatch);
console.log('Encoded match:', encodedMatch);
console.log('Encoded length JS:', jsResult.encoded.length, 'WASM:', wasmResult.encoded.length);
if (hashMatch && encodedMatch) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
failed++;
}
}
// Test 9: Bao encode outboard
console.log('\n--- Test 9: Bao encode outboard (4KB) ---');
{
const data = new Uint8Array(4096);
for (let i = 0; i < data.length; i++) data[i] = (i * 17) & 0xff;
const jsResult = baoJs.baoEncode(data, true);
const wasmResult = baoWasm.baoEncodeWasm(data, true);
console.log('JS hash: ', toHex(jsResult.hash));
console.log('WASM hash:', toHex(wasmResult.hash));
const hashMatch = arraysEqual(jsResult.hash, wasmResult.hash);
const encodedMatch = arraysEqual(jsResult.encoded, wasmResult.encoded);
console.log('Hash match:', hashMatch);
console.log('Encoded match:', encodedMatch);
if (hashMatch && encodedMatch) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
failed++;
}
}
// Test 10: Large encode
console.log('\n--- Test 10: Bao encode (64KB) ---');
{
const data = new Uint8Array(64 * 1024);
for (let i = 0; i < data.length; i++) data[i] = (i * 19) & 0xff;
const jsResult = baoJs.baoEncode(data, false);
const wasmResult = baoWasm.baoEncodeWasm(data, false);
console.log('JS hash: ', toHex(jsResult.hash));
console.log('WASM hash:', toHex(wasmResult.hash));
const hashMatch = arraysEqual(jsResult.hash, wasmResult.hash);
if (hashMatch) {
console.log('PASS');
passed++;
} else {
console.log('FAIL: Hash mismatch');
failed++;
}
}
console.log('\n========================================');
console.log(`Results: ${passed} passed, ${failed} failed`);
return failed === 0;
}
runTests().then(success => {
process.exit(success ? 0 : 1);
}).catch(err => {
console.error('Test error:', err);
process.exit(1);
});