import { describe, expect, it } from 'vitest' import { benchColumns, combinationRows, groupPins, parseCell, parseTicks, recordExpected, runBench } from '../app/lib/logic/bench' import { createProject, lookup } from '../app/lib/logic/ops' import type { Project } from '../app/lib/logic/types' /** Examples: a half adder, and the 4-bit counter (CLK, RST in; Q0–Q3 out). */ function project(): Project { return createProject({ examples: true }) } describe('test bench columns and cells', () => { it('A0…An make one number column; other pins stay single', () => { const cols = groupPins([ { key: 'a1', label: 'A1' }, { key: 'a0', label: 'A0' }, { key: 'c', label: 'Cin' }, { key: 'x2', label: 'X2' }, ]) expect(cols).toEqual([ { name: 'A', keys: ['a0', 'a1'] }, { name: 'Cin', keys: ['c'] }, { name: 'X2', keys: ['x2'] }, ]) }) it('cells: numbers in three bases, P, don’t care, and what does not fit', () => { expect(parseCell('12', 4, true)).toBe(12) expect(parseCell('0x0C', 4, true)).toBe(12) expect(parseCell('0b1100', 4, true)).toBe(12) expect(parseCell('-1', 4, false)).toBe(15) expect(parseCell('p', 1, true)).toBe('P') expect(parseCell('', 1, false)).toBeNull() expect(parseCell('x', 1, false)).toBeNull() expect(parseCell('16', 4, true)).toHaveProperty('error') expect(parseCell('P', 1, false)).toHaveProperty('error') expect(parseTicks('5')).toEqual({ n: 5, wait: false }) expect(parseTicks('≤100')).toEqual({ n: 100, wait: true }) expect(parseTicks('<= 7')).toEqual({ n: 7, wait: true }) expect(parseTicks('soon')).toHaveProperty('error') }) }) describe('running a test bench', () => { it('a truth table: every combination, recorded, then passing; a wrong row fails', () => { const p = project() const ha = p.components.find(c => c.id === 'ex-ha')! const rows = combinationRows(ha) if ('error' in rows) throw new Error(rows.error) expect(rows.length).toBe(4) const filled = recordExpected(lookup(p), ha.id, rows) const r = runBench(lookup(p), ha.id, filled) expect(r).toMatchObject({ pass: 4, fail: 0 }) const cols = benchColumns(ha) const outName = cols.outs[0]!.name // Filled cells stay: taking again changes nothing. expect(recordExpected(lookup(p), ha.id, filled)).toEqual(filled) filled[3] = { ...filled[3], expect: { ...filled[3]!.expect, [outName]: filled[3]!.expect![outName] === '1' ? '0' : '1' } } const r2 = runBench(lookup(p), ha.id, filled) expect(r2.fail).toBe(1) expect(r2.rows[3]!.ok).toBe(false) }) it('a counter: pulses, ticks, and waiting until it gets there', () => { const p = project() const counter = p.components.find(c => c.id === 'ex-cnt4')! const { ins, outs } = benchColumns(counter) const clk = ins.find(c => /clk/i.test(c.name))!.name const rst = ins.find(c => /r/i.test(c.name) && c.name !== clk)?.name const q = outs[0]! const rows = [ { set: { [clk]: 'P', ...(rst ? { [rst]: '0' } : {}) } }, { set: { [clk]: 'P' }, ticks: '2' }, { set: { [clk]: 'P' }, ticks: '≤50', expect: { [q.name]: '9' } }, ] const r = runBench(lookup(p), counter.id, rows) expect(q.keys.length).toBe(4) expect(r.rows[0]!.ticks).toBe(1) expect(r.rows[1]!.ticks).toBe(2) expect(r.rows[1]!.got[q.name]).toBe(3) expect(r.rows[2]).toMatchObject({ ok: true, ticks: 6 }) // Too few ticks to get there. const r2 = runBench(lookup(p), counter.id, [{ set: { [clk]: 'P' }, ticks: '≤3', expect: { [q.name]: '9' } }]) expect(r2.rows[0]).toMatchObject({ ok: false, error: 'Not within 3 ticks' }) }) it('stops a run that goes on too long, and says why rows are broken', () => { const p = project() const counter = p.components.find(c => c.id === 'ex-cnt4')! const clk = benchColumns(counter).ins.find(c => /clk/i.test(c.name))!.name const r = runBench(lookup(p), counter.id, [{ set: { [clk]: 'P' }, ticks: '100' }], 10) expect(r.error).toMatch(/Stopped after 10 ticks/) const r2 = runBench(lookup(p), counter.id, [{ set: { [clk]: 'nope' } }]) expect(r2.rows[0]!.error).toMatch(/not a number/) }) })