Files
MetonaSqlark/tests/engine/crc32.test.ts
T
thzxx 334067d89e
CI / test (18.x) (push) Successful in 10m10s
CI / test (20.x) (push) Successful in 10m10s
CI / test (22.x) (push) Successful in 10m6s
CI / e2e (push) Successful in 9m51s
CI / test (24.x) (push) Successful in 10m28s
release: v0.5.1 — 存储后端生产级硬化(CRC-32/全库加密/WAL分片/页面化存储/多标签页锁/e2e)+ 深度审查修复(假实现接线/死代码清理)
2026-08-10 12:07:00 +08:00

90 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* AriaEngine CRC32 单元测试
*/
import { crc32, crc32Continue } from '../../src/engine/aria/crc32';
const enc = new TextEncoder();
function bytes(s: string): Uint8Array {
return enc.encode(s);
}
describe('CRC32', () => {
test('标准测试向量:空串 = 0', () => {
expect(crc32(new Uint8Array(0))).toBe(0x00000000);
});
test('标准测试向量:123456789 = 0xCBF43926IEEE 802.3 参考值)', () => {
expect(crc32(bytes('123456789'))).toBe(0xcbf43926);
});
test('标准测试向量:The quick brown fox... = 0x414FA339', () => {
expect(crc32(bytes('The quick brown fox jumps over the lazy dog'))).toBe(0x414fa339);
});
test('确定性:相同输入多次计算一致', () => {
const data = bytes('metona-sqlark aria engine');
const a = crc32(data);
const b = crc32(data);
expect(a).toBe(b);
});
test('区分性:不同输入不同校验和', () => {
expect(crc32(bytes('hello'))).not.toBe(crc32(bytes('hellp')));
expect(crc32(bytes('abc'))).not.toBe(crc32(bytes('abd')));
});
test('单字节翻转可被检测', () => {
const original = bytes('metona-sqlark aria engine');
const flipped = new Uint8Array(original);
flipped[10] ^= 0xff;
expect(crc32(original)).not.toBe(crc32(flipped));
});
test('分段计算与一次性计算一致(crc32Continue', () => {
const data = bytes('The quick brown fox jumps over the lazy dog');
const whole = crc32(data);
// 按 1 字节分段
let seg = 0;
for (let i = 0; i < data.length; i++) {
seg = crc32Continue(seg, data.subarray(i, i + 1));
}
expect(seg).toBe(whole);
// 按 5 字节分段
let seg2 = 0;
for (let i = 0; i < data.length; i += 5) {
seg2 = crc32Continue(seg2, data.subarray(i, i + 5));
}
expect(seg2).toBe(whole);
});
test('seed 参数与 crc32Continue 等价', () => {
const data = bytes('metona-sqlark aria engine');
const head = data.subarray(0, 7);
const tail = data.subarray(7);
const whole = crc32(data);
const split = crc32(tail, crc32(head));
expect(split).toBe(whole);
});
test('中文 UTF-8 字节正确参与计算', () => {
const data = bytes('玥玥的测试数据:大段中文内容');
expect(crc32(data)).toBe(crc32(new Uint8Array(data)));
});
test('大缓冲区(1MB 随机字节)稳定计算', () => {
const data = new Uint8Array(1024 * 1024);
let x = 12345;
for (let i = 0; i < data.length; i++) {
x = (x * 1103515245 + 12345) & 0x7fffffff;
data[i] = x & 0xff;
}
const a = crc32(data);
const b = crc32(data);
expect(a).toBe(b);
expect(a).not.toBe(0);
});
});