Spaces:
Running
Running
| import { describe, it, expect } from 'vitest'; | |
| import { BchCodec } from '../core/bch.js'; | |
| describe('BCH', () => { | |
| const bch63 = new BchCodec({ n: 63, k: 36, t: 5, m: 6 }); | |
| it('should encode and decode without errors (63,36,5)', () => { | |
| const message = new Uint8Array(36); | |
| for (let i = 0; i < 36; i++) message[i] = Math.random() > 0.5 ? 1 : 0; | |
| const codeword = bch63.encode(message); | |
| expect(codeword.length).toBe(63); | |
| const decoded = bch63.decode(codeword); | |
| expect(decoded).not.toBeNull(); | |
| expect(Array.from(decoded!)).toEqual(Array.from(message)); | |
| }); | |
| it('should correct up to t=5 errors (63,36,5)', () => { | |
| const message = new Uint8Array(36); | |
| for (let i = 0; i < 36; i++) message[i] = Math.random() > 0.5 ? 1 : 0; | |
| const codeword = bch63.encode(message); | |
| // Introduce 5 errors at random positions | |
| const errorPositions = new Set<number>(); | |
| while (errorPositions.size < 5) { | |
| errorPositions.add(Math.floor(Math.random() * 63)); | |
| } | |
| const corrupted = new Uint8Array(codeword); | |
| for (const pos of errorPositions) { | |
| corrupted[pos] ^= 1; | |
| } | |
| const decoded = bch63.decode(corrupted); | |
| expect(decoded).not.toBeNull(); | |
| expect(Array.from(decoded!)).toEqual(Array.from(message)); | |
| }); | |
| it('should handle zero message', () => { | |
| const message = new Uint8Array(36); // all zeros | |
| const codeword = bch63.encode(message); | |
| const decoded = bch63.decode(codeword); | |
| expect(decoded).not.toBeNull(); | |
| expect(Array.from(decoded!)).toEqual(Array.from(message)); | |
| }); | |
| }); | |