import { test } from 'node:test'; import assert from 'node:assert/strict'; import { holeVsUndefined, lengthMutation, run, sparseFacts, typedVsSparse, } from './main.js'; test('稀疏数组:length 为 3,索引 1 是洞', () => { const s = sparseFacts(); assert.equal(s.length, 3); assert.equal(s.holeAt1, true); assert.deepEqual(s.keys, ['0', '2']); }); test('map 保留洞与 length,forEach 只访问存在的元素', () => { const s = sparseFacts(); assert.equal(s.mappedLength, 3); assert.deepEqual(s.mappedKeys, ['0', '2']); assert.equal(s.forEachCount, 2); assert.deepEqual(s.filtered, [1, 3]); }); test('展开与 Array.from 把洞暴露出来', () => { const s = sparseFacts(); assert.deepEqual(s.spread, [1, undefined, 3]); assert.equal(s.spreadHasUndefined, true); assert.deepEqual(s.arrayFromFill, [1, 0, 3]); assert.equal(s.json, '[1,null,3]'); }); test('length 截断会丢元素,扩展会造洞', () => { assert.deepEqual(lengthMutation(), { afterTruncate: [1], afterExtend: [1, undefined, undefined, undefined], holeAt3: true, }); }); test('new Array(3) 全是洞,字面量 undefined 不是洞', () => { assert.deepEqual(holeVsUndefined(), { holesKeys: [], undefsKeys: ['0', '1', '2'], holesLength: 3, undefsLength: 3, }); }); test('TypedArray:零填充、定长、不是 Array、没有 push', () => { assert.deepEqual(typedVsSparse(), { typedKeys: ['0', '1', '2'], sparseKeys: [], typedValues: [0, 0, 0], sparseFirst: undefined, isArray: false, isView: true, pushError: 'TypeError', byteLength: 12, }); }); test('run 输出 13 行场景与两条说明', () => { const out = []; const rows = run((s) => out.push(s)); assert.equal(rows.length, 13); assert.equal(out.length, 16); assert.match(out[1], /稀疏数组 length => 3/); });