logigate/tests/bench.test.ts

98 lines
5.1 KiB
TypeScript
Raw Permalink 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.

import { describe, expect, it } from 'vitest'
import { benchFor, checkCols, combinationRows, driveCols, groupPins, parseCell, parseTicks, recordExpected, runBench, upgradeBenches } from '../app/lib/logic/bench'
import { benchLayout } from '../app/lib/logic/benchLayout'
import { compile } from '../app/lib/logic/compile'
import { shapeOf } from '../app/lib/logic/geometry'
import { createProject, lookup, uid } from '../app/lib/logic/ops'
import type { CircuitNode, ComponentDef, Project } from '../app/lib/logic/types'
/** A circuit with one placed example component and a bench wired to all its pins. */
function rig(chipId: string): { p: Project; def: ComponentDef; bench: CircuitNode } {
const p = createProject({ examples: true })
const bench: CircuitNode = { id: 'tb', kind: 'bench', x: 0, y: 0, bench: { cols: [], rows: [] } }
const chip: CircuitNode = { id: 'u', kind: 'chip', chip: chipId, x: 20, y: 0 }
const def: ComponentDef = { id: 'rig', name: 'Rig', symbol: 'box', nodes: [bench, chip], wires: [] }
p.components.push(def)
for (const w of benchFor(def, bench, chip, lookup(p)(chipId)!)) def.wires.push({ id: uid('w'), ...w })
return { p, def, bench }
}
const col = (b: CircuitNode, name: string) => b.bench!.cols!.find(c => c.name === name)!.id
describe('test bench cells', () => {
it('A0…An make one group; other pins stay single', () => {
expect(groupPins([{ key: 'a1', label: 'A1' }, { key: 'a0', label: 'A0' }, { key: 'c', label: 'Cin' }, { key: 'x2', label: 'X2' }])).toEqual([
{ name: 'A', keys: ['a0', 'a1'] },
{ name: 'Cin', keys: ['c'] },
{ name: 'X2', keys: ['x2'] },
])
})
it('numbers in three bases, P, don’t care, what does not fit; ticks', () => {
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('x', 1, false)).toBeNull()
expect(parseCell('16', 4, true)).toHaveProperty('error')
expect(parseCell('P', 1, false)).toHaveProperty('error')
expect(parseTicks('≤100')).toEqual({ n: 100, wait: true })
expect(parseTicks('soon')).toHaveProperty('error')
})
})
describe('a wired test bench', () => {
it('has pins: check columns on the left, drive columns on the right', () => {
const { p, bench } = rig('ex-ha')
const shape = shapeOf(bench, lookup(p))
expect(shape.inputs.map(x => x.label)).toEqual(checkCols(bench.bench).map(c => c.name))
expect(shape.outputs.map(x => x.label)).toEqual(driveCols(bench.bench).map(c => c.name))
expect(shape.outputs.every(x => x.dx === benchLayout(bench).w)).toBe(true)
const nl = compile(lookup(p), 'rig')
expect(nl.sources.size).toBe(shape.outputs.length)
})
it('a truth table: every combination, taken, passing; a wrong row fails', () => {
const { p, bench } = rig('ex-ha')
const rows = combinationRows(bench.bench!)
if ('error' in rows) throw new Error(rows.error)
expect(rows.length).toBe(4)
bench.bench!.rows = rows
bench.bench!.rows = recordExpected(runBench(lookup(p), 'rig', 'tb'), rows)
// Half adder, A B → S C: 1 + 1 is S 0, C 1.
const last = bench.bench!.rows[3]!
expect([last.expect![col(bench, 'S')], last.expect![col(bench, 'C')]]).toEqual(['0', '1'])
expect(runBench(lookup(p), 'rig', 'tb')).toMatchObject({ pass: 4, fail: 0 })
last.expect![col(bench, 'C')] = '0'
const r = runBench(lookup(p), 'rig', 'tb')
expect(r.fail).toBe(1)
expect(r.rows[3]).toMatchObject({ ok: false })
})
it('a counter: pulses, ticks, and waiting until it gets there', () => {
const { p, bench } = rig('ex-cnt4')
const clk = col(bench, 'CLK')
const q = col(bench, 'Q')
bench.bench!.rows = [{ set: { [clk]: 'P' } }, { set: { [clk]: 'P' }, ticks: '2' }, { set: { [clk]: 'P' }, ticks: '≤50', expect: { [q]: '9' } }]
const r = runBench(lookup(p), 'rig', 'tb')
expect(r.rows[1]!.got[q]).toBe(3)
expect(r.rows[2]).toMatchObject({ ok: true, ticks: 6 })
bench.bench!.rows = [{ set: { [clk]: 'P' }, ticks: '≤3', expect: { [q]: '9' } }]
expect(runBench(lookup(p), 'rig', 'tb').rows[0]).toMatchObject({ ok: false, error: 'Not within 3 ticks' })
bench.bench!.rows = [{ set: { [clk]: 'P' }, ticks: '100' }]
expect(runBench(lookup(p), 'rig', 'tb', 10).error).toMatch(/Stopped after 10 ticks/)
})
it('a bench from the first version (docked, keyed by names) gets columns and wires', () => {
const p = createProject({ examples: true })
const chip: CircuitNode = { id: 'u', kind: 'chip', chip: 'ex-ha', x: 20, y: 0 }
const old: CircuitNode = { id: 'tb', kind: 'bench', x: 0, y: 0, bench: { on: 'u', rows: [{ set: { A: '1', B: '1' }, expect: { S: '0', C: '1' } }] } }
const def: ComponentDef = { id: 'rig', name: 'Rig', symbol: 'box', nodes: [old, chip], wires: [] }
p.components.push(def)
upgradeBenches(def, lookup(p))
expect(old.bench!.on).toBeUndefined()
expect(old.bench!.cols!.map(c => `${c.dir}:${c.name}`)).toEqual(['drive:A', 'drive:B', 'check:S', 'check:C'])
expect(def.wires.length).toBe(4)
expect(runBench(lookup(p), 'rig', 'tb')).toMatchObject({ pass: 1, fail: 0 })
})
})