A test bench: docks to a placed component, a table of tests (number columns for A0..An, P pulses, ticks or wait-until), every combination, take what it does; runs after each change

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-28 13:12:43 +01:00
parent 2be845030e
commit 45c37bef43
15 changed files with 876 additions and 1 deletions

View File

@ -6082,3 +6082,194 @@ button.tile:hover {
white-space: pre;
pointer-events: none;
}
/* Test bench */
.node .bench-body {
fill: #141c24;
stroke: #3b4554;
stroke-dasharray: 4 3;
}
.node .bench-body.pass {
stroke: var(--on);
stroke-dasharray: none;
}
.node .bench-body.fail {
stroke: var(--danger);
stroke-dasharray: none;
}
.node .bench-title {
fill: var(--text);
font-size: 10px;
font-weight: 600;
pointer-events: none;
}
.node .bench-line {
fill: var(--muted);
font-size: 10px;
pointer-events: none;
}
.node .bench-line.pass {
fill: var(--on);
}
.node .bench-line.fail {
fill: var(--danger);
}
.modal.bench {
width: min(1100px, 100%);
}
.modal.bench code {
font-family: var(--font-mono);
font-size: 11px;
color: var(--text);
}
.bench-bar {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 6px;
margin: 10px 0 8px;
}
.bench-target {
display: flex;
align-items: center;
gap: 8px;
font-size: 13px;
font-weight: 600;
}
.bench-summary {
margin-bottom: 6px;
font-size: 13px;
font-weight: 600;
}
.bench-summary.pass {
color: var(--on);
}
.bench-summary.fail {
color: var(--danger);
}
.bench-table-wrap {
max-height: 55vh;
overflow: auto;
border: 1px solid var(--border);
border-radius: 6px;
}
.bench-table {
border-collapse: collapse;
font-size: 12px;
}
.bench-table th {
position: sticky;
top: 0;
z-index: 1;
padding: 4px 4px;
background: var(--panel);
color: var(--muted);
font-weight: 600;
text-align: left;
white-space: nowrap;
}
.bench-table th small {
margin-left: 2px;
font-size: 9px;
opacity: 0.7;
}
.bench-table th.out,
.bench-table td.out {
background: rgb(61 220 132 / 0.04);
}
.bench-table td {
position: relative;
padding: 1px 3px;
border-top: 1px solid rgb(37 46 58 / 0.6);
vertical-align: middle;
}
.bench-table tr.fail td {
background: rgb(255 107 107 / 0.06);
}
.bench-num {
width: 32px;
color: var(--muted);
text-align: right;
}
.bench-arrow {
width: 22px;
text-align: center;
color: var(--muted);
}
.bench-cell {
width: 64px;
padding: 3px 5px;
background: transparent;
border: 1px solid transparent;
border-radius: 4px;
font-family: var(--font-mono);
font-size: 12px;
}
.bench-cell:hover {
border-color: var(--border);
}
.bench-cell:focus {
border-color: var(--accent);
background: var(--panel-2);
outline: none;
}
.bench-cell::placeholder {
color: #4a5566;
}
.bench-got {
display: block;
padding: 0 5px 2px;
color: var(--danger);
font-family: var(--font-mono);
font-size: 10px;
}
.bench-actions {
white-space: nowrap;
text-align: right;
}
.bench-err {
display: inline-block;
max-width: 240px;
overflow: hidden;
text-overflow: ellipsis;
vertical-align: middle;
color: var(--danger);
font-size: 11px;
}
.bench-add {
margin-top: 8px;
}
.bench-empty {
margin-top: 12px;
}

View File

@ -86,5 +86,6 @@ watch(
<EditorLibraryPublishDialog v-if="ed.dialog.value === 'library-publish'" />
<EditorConvertDialog v-if="ed.dialog.value === 'convert'" />
<EditorAsmDialog v-if="ed.dialog.value === 'asm'" />
<EditorBenchDialog v-if="ed.benchOpen.value" />
</div>
</template>

View File

@ -0,0 +1,164 @@
<script setup lang="ts">
// The tests of a test bench: a row per test, the component's inputs on the
// left, what its outputs should show on the right, ticks to give it, and
// how each row went. They run again after every change.
import { benchColumns, cellOf, combinationRows, recordExpected } from '~/lib/logic/bench'
import type { BenchRow } from '~/lib/logic/types'
const ed = useEditor()
const close = () => (ed.benchOpen.value = null)
const node = computed(() => ed.def.value.nodes.find(n => n.id === ed.benchOpen.value && n.kind === 'bench'))
watch(node, n => !n && close())
const target = computed(() => (node.value ? ed.benchTarget(node.value) : undefined))
const cols = computed(() => (target.value ? benchColumns(target.value.def) : { ins: [], outs: [] }))
const rows = computed(() => node.value?.bench?.rows ?? [])
const result = computed(() => (node.value ? ed.benchResults.value.get(node.value.id) : undefined))
/** Components in this circuit it can be docked to. */
const chips = computed(() =>
ed.def.value.nodes
.filter(n => n.kind === 'chip' && n.chip)
.map(n => ({ id: n.id, name: `${ed.getDef.value(n.chip!)?.name ?? 'Missing'}${n.label?.trim() && !n.autoLabel ? ` · ${n.label.trim()}` : ''}` })),
)
function onKey(e: KeyboardEvent) {
if (e.key === 'Escape') close()
}
onMounted(() => {
window.addEventListener('keydown', onKey)
if (node.value && !result.value) ed.runBenchNow(node.value.id)
})
onBeforeUnmount(() => window.removeEventListener('keydown', onKey))
const id = () => node.value!.id
const dock = (chipId: string) => ed.dockBench(id(), chipId || null)
function setCell(i: number, side: 'set' | 'expect', name: string, v: string) {
ed.setBench(id(), b => {
const row = b.rows[i]
if (!row) return
const cells = { ...(row[side] ?? {}) }
if (v.trim()) cells[name] = v.trim()
else delete cells[name]
if (Object.keys(cells).length) row[side] = cells
else delete row[side]
})
}
function setTicks(i: number, v: string) {
ed.setBench(id(), b => {
const row = b.rows[i]
if (!row) return
if (v.trim()) row.ticks = v.trim()
else delete row.ticks
})
}
function addRow(at = rows.value.length) {
// A new row starts as a copy of the one above, without its checks.
const above = rows.value[at - 1]
const row: BenchRow = above?.set ? { set: { ...above.set } } : {}
ed.setBench(id(), b => b.rows.splice(at, 0, row))
}
const removeRow = (i: number) => ed.setBench(id(), b => b.rows.splice(i, 1))
function combinations() {
const t = target.value
if (!t) return
const r = combinationRows(t.def)
if ('error' in r) return ed.notify(r.error, 'error')
ed.setBench(id(), b => (b.rows = r))
ed.notify(`${r.length} rows: one for every input combination. Fill in what it should show, or take what it does now.`)
}
function takeExpected() {
const t = target.value
if (!t || !rows.value.length) return
const filled = recordExpected(ed.getDef.value, t.def.id, rows.value)
ed.setBench(id(), b => (b.rows = filled))
ed.notify('Expected outputs set to what it shows now: the tests keep it that way')
}
/** The value a failing output cell showed, when it differs. */
function gotNote(i: number, name: string): string {
const r = result.value?.rows[i]
if (!r || r.ok !== false) return ''
const want = cellOf(rows.value[i]!, 'expect', name)
const got = r.got[name]
if (got === undefined || !want) return ''
return String(got) === want || (/^0[xb]/i.test(want) && Number(want) === got) ? '' : `is ${got}`
}
</script>
<template>
<div class="modal-backdrop" @click.self="close">
<div class="modal bench" role="dialog" aria-labelledby="bench-title">
<div class="flex items-start justify-between gap-4">
<h2 id="bench-title">Tests</h2>
<button class="tb-btn" title="Close (Esc)" @click="close"><Icon name="lucide:x" class="size-4" /></button>
</div>
<p class="hint">
Each row sets the inputs, gives it ticks, then checks the outputs. It runs on its own from all zeros; what it holds carries to the next row.
Inputs: a number (12, 0x0C, 0b1100), <code>P</code> for a clock pulse, empty to leave it. Outputs: what it should show, empty for any.
Ticks: <code>5</code> pulses (the P inputs and its own clocks), or <code>≤1000</code> to wait until the outputs are right.
</p>
<div class="bench-bar">
<label class="bench-target">
<span>Tests</span>
<select class="tb-select" :value="node?.bench?.on ?? ''" @change="dock(($event.target as HTMLSelectElement).value)">
<option value="">nothing yet</option>
<option v-for="c in chips" :key="c.id" :value="c.id">{{ c.name }}</option>
</select>
</label>
<span class="grow" />
<button class="btn-sm" :disabled="!target" title="A row for every combination of the inputs" @click="combinations"><Icon name="lucide:grid-3x3" class="size-3.5" /> Every combination</button>
<button class="btn-sm" :disabled="!target || !rows.length" title="Expected outputs: what it shows now" @click="takeExpected"><Icon name="lucide:camera" class="size-3.5" /> Take what it does</button>
<button class="btn-sm" :disabled="!target || !rows.length" title="Run again, longer runs allowed" @click="node && ed.runBenchNow(node.id, true)"><Icon name="lucide:play" class="size-3.5" /> Run</button>
</div>
<p v-if="!target" class="hint bench-empty">Pick the component it tests above, or drop the bench on one.</p>
<template v-else>
<p v-if="result" class="bench-summary" :class="result.fail || result.error ? 'fail' : result.pass ? 'pass' : ''">
<template v-if="result.error">{{ result.error }}. </template>
<template v-if="result.fail">{{ result.fail }} failing, {{ result.pass }} pass</template>
<template v-else-if="result.pass">All {{ result.pass }} pass</template>
<template v-else-if="!result.error">Nothing checked yet</template>
</p>
<div class="bench-table-wrap">
<table class="bench-table">
<thead>
<tr>
<th class="bench-num">#</th>
<th v-for="c in cols.ins" :key="`i-${c.name}`" :title="c.keys.length > 1 ? `${c.keys.length} bits` : ''">{{ c.name }}<small v-if="c.keys.length > 1">{{ c.keys.length }}</small></th>
<th class="bench-arrow">→</th>
<th v-for="c in cols.outs" :key="`o-${c.name}`" class="out" :title="c.keys.length > 1 ? `${c.keys.length} bits` : ''">{{ c.name }}<small v-if="c.keys.length > 1">{{ c.keys.length }}</small></th>
<th>Ticks</th>
<th />
</tr>
</thead>
<tbody>
<tr v-for="(row, i) in rows" :key="i" :class="result?.rows[i]?.ok === false ? 'fail' : result?.rows[i]?.ok ? 'pass' : ''">
<td class="bench-num tabular-nums">{{ i + 1 }}</td>
<td v-for="c in cols.ins" :key="`i-${c.name}`">
<input :value="cellOf(row, 'set', c.name)" class="bench-cell" spellcheck="false" @change="setCell(i, 'set', c.name, ($event.target as HTMLInputElement).value)" />
</td>
<td class="bench-arrow" :title="result?.rows[i]?.error ?? ''">
<Icon v-if="result?.rows[i]?.ok === false" name="lucide:x" class="size-3.5 text-(--danger)" />
<Icon v-else-if="result?.rows[i]?.ok" name="lucide:check" class="size-3.5 text-(--on)" />
</td>
<td v-for="c in cols.outs" :key="`o-${c.name}`" class="out">
<input :value="cellOf(row, 'expect', c.name)" class="bench-cell" spellcheck="false" :placeholder="String(result?.rows[i]?.got[c.name] ?? '')" @change="setCell(i, 'expect', c.name, ($event.target as HTMLInputElement).value)" />
<span v-if="gotNote(i, c.name)" class="bench-got">{{ gotNote(i, c.name) }}</span>
</td>
<td>
<input :value="row.ticks ?? ''" class="bench-cell ticks" spellcheck="false" :placeholder="result?.rows[i]?.ticks ? String(result.rows[i]!.ticks) : ''" @change="setTicks(i, ($event.target as HTMLInputElement).value)" />
</td>
<td class="bench-actions">
<span v-if="result?.rows[i]?.error" class="bench-err" :title="result.rows[i]!.error">{{ result.rows[i]!.error }}</span>
<button class="icon-btn" title="New row below" @click="addRow(i + 1)"><Icon name="lucide:plus" class="size-3.5" /></button>
<button class="icon-btn" title="Remove this row" @click="removeRow(i)"><Icon name="lucide:trash-2" class="size-3.5" /></button>
</td>
</tr>
</tbody>
</table>
</div>
<button class="btn-sm bench-add" @click="addRow()"><Icon name="lucide:plus" class="size-3.5" /> Row</button>
</template>
</div>
</div>
</template>

View File

@ -1247,6 +1247,7 @@ function onPointerUp(e: PointerEvent) {
function onDoubleClick(node: CircuitNode) {
if (node.kind === 'chip' && node.chip) ed.open(node.chip)
else if (node.kind === 'bench') ed.benchOpen.value = node.id
else if (['input', 'output', 'lamp', 'button', 'note', 'hex', 'panel', 'outpanel', 'tag', 'ref'].includes(node.kind)) {
ed.selection.value = [node.id]
ed.labelFocus.value++
@ -1281,6 +1282,7 @@ function nudge(dx: number, dy: number) {
function onKey(e: KeyboardEvent) {
const el = e.target as HTMLElement
if (el.closest?.('input, textarea, select, [contenteditable]')) return
if (ed.benchOpen.value) return
if (ed.dialog.value || ed.showHelp.value) {
if (e.key === 'Escape') {
ed.dialog.value = null

View File

@ -172,6 +172,21 @@ const faceHex = (inner: string) => sevenSegment([0, 1, 2, 3].reduce((n, i) => n
/** The colour of an RGB pixel inside this chip ('' while off). */
const facePx = (inner: string) => rgbHex(fv(inner, 'r'), fv(inner, 'g'), fv(inner, 'b'))
const rgbNow = computed(() => (props.node.rgb ? rgbHex(v('r'), v('g'), v('b')) : ''))
// Test bench: its target and the last run.
const benchSum = computed(() => {
if (props.node.kind !== 'bench') return { title: '', line: '', state: '' }
const target = ed.benchTarget(props.node)
const title = target ? `Tests: ${target.def.name}` : 'Test bench'
const rows = props.node.bench?.rows.length ?? 0
const r = props.ghost ? undefined : ed.benchResults.value.get(props.node.id)
if (!target) return { title, line: 'drop on a component', state: 'idle' }
if (!rows) return { title, line: 'no tests yet: double-click', state: 'idle' }
if (!r) return { title, line: `${rows} tests`, state: 'idle' }
if (r.error && !r.fail) return { title, line: r.error, state: 'fail' }
if (r.fail) return { title, line: `✗ ${r.fail} failing, ${r.pass} pass`, state: 'fail' }
return { title, line: r.pass ? `✓ all ${r.pass} pass` : `${rows} rows, nothing checked`, state: r.pass ? 'pass' : 'idle' }
})
// Terminal and keyboard: what the store keeps for this part.
const TERM_CHAR = 6.6
const termLines = computed(() => {
@ -421,6 +436,13 @@ const title = computed(() => {
<text v-if="!lines.length" :x="(b.w * G) / 2" :y="G + 3.5" text-anchor="middle" class="pin-label">{{ node.conn && !connType ? 'type missing' : 'no lines' }}</text>
</template>
<!-- Test bench: how the tests of the component it is docked to went -->
<template v-else-if="node.kind === 'bench'">
<rect x="0" :y="b.y * G" :width="b.w * G" :height="b.h * G" rx="4" class="body bench-body" :class="benchSum.state" />
<text :x="0.5 * G" :y="0.15 * G" class="bench-title">{{ benchSum.title }}</text>
<text :x="0.5 * G" :y="1.05 * G" class="bench-line" :class="benchSum.state">{{ benchSum.line }}</text>
</template>
<!-- Terminal: a text screen the circuit writes to -->
<template v-else-if="node.kind === 'terminal'">
<rect x="0" :y="b.y * G" :width="b.w * G" :height="b.h * G" rx="3" class="body display" />

View File

@ -27,6 +27,7 @@ import { baseOf, MAX_GATE_INPUTS, NAND_BASE, wideGate } from '~/lib/logic/wide'
import { CHALLENGES, challengeSteps, type Challenge } from '~/lib/logic/challenges'
import { probeValue } from '~/lib/logic/probes'
import { bitsCode, codeBits, DATA_KEYS, KEY_QUEUE, putChar } from '~/lib/logic/devices'
import { AUTO_TICKS, HAND_TICKS, runBench, type BenchResult } from '~/lib/logic/bench'
import { EXAMPLE as ASM_EXAMPLE, loadIntoPanels } from '~/lib/asm'
import { latchedTo, latchInto, latchTarget, syncLatches } from '~/lib/logic/latch'
import { cleanBundle, importBundle, importProblem, LIBRARY_LIMITS, libraryBundle, ownedEntry, publishProblem, type LibraryBundle, type LibraryMeta } from '~/lib/logic/library'
@ -83,7 +84,7 @@ import {
type AccountUser,
type ProjectMeta,
} from '~/lib/logic/storage'
import type { AsmDoc, CircuitNode, ComponentDef, ConnectorType, HeldState, LampColor, LibraryOrigin, NodeKind, Probe, PanelControl, PanelRow, PinRef, Project, PuzzleSpec, WireColor } from '~/lib/logic/types'
import type { AsmDoc, Bench, CircuitNode, ComponentDef, ConnectorType, HeldState, LampColor, LibraryOrigin, NodeKind, Probe, PanelControl, PanelRow, PinRef, Project, PuzzleSpec, WireColor } from '~/lib/logic/types'
/** A challenge check plus how many transistors the solution uses. */
/** How everyone did on a puzzle, from the server. */
@ -454,6 +455,7 @@ function create() {
}
}
}
scheduleBenches()
devTerms = d.nodes.filter(n => n.kind === 'terminal').map(n => n.id)
devKbs = d.nodes.filter(n => n.kind === 'keyboard').map(n => n.id)
if (!devKbs.includes(typingInto.value ?? '')) typingInto.value = null
@ -908,6 +910,12 @@ function create() {
if (p.kind === 'ref') node.label = def.value.nodes.filter(n => n.kind === 'tag' && n.label?.trim()).at(-1)?.label?.trim() ?? ''
if (p.kind === 'panel' || p.kind === 'outpanel') node.rows = newPanelRows()
if (p.kind === 'bundle' || p.kind === 'split') node.bits = 4
if (p.kind === 'bench') {
node.bench = { rows: [] }
// Dropped on (or next to) a component: docks below it.
const chip = benchSpot(x, y)
if (chip) dockAt(node, chip)
}
// A new pixel is like the one placed last: building a colour screen, every pixel comes in colour.
if (p.kind === 'pixel' && def.value.nodes.findLast(n => n.kind === 'pixel')?.rgb) node.rgb = true
def.value.nodes.push(node)
@ -1088,6 +1096,7 @@ function create() {
for (let size = -1; size !== set.size; ) {
size = set.size
for (const p of [...dockedPanels(def.value, set), ...pluggedInto(def.value, set), ...latchedTo(def.value, set)]) set.add(p.id)
for (const n of def.value.nodes) if (n.bench?.on && set.has(n.bench.on)) set.add(n.id)
}
return set
}
@ -1434,6 +1443,88 @@ function create() {
commit()
}
// ── Test benches ──────────────────────────────────────────────────
/** Results of the benches in the open circuit, by bench id; not saved. */
const benchResults = shallowRef(new Map<string, BenchResult>())
/** The bench whose tests are open in the dialog. */
const benchOpen = ref<string | null>(null)
let benchTimer: ReturnType<typeof setTimeout> | undefined
/** The component a bench at this spot docks to: one whose box is under it or within two squares. */
function benchSpot(x: number, y: number): CircuitNode | undefined {
let best: CircuitNode | undefined
let bestD = 2.01
for (const n of def.value.nodes) {
if (n.kind !== 'chip') continue
const b = shapeOf(n, getDef.value).body
const dx = Math.max(n.x + b.x - x, 0, x - (n.x + b.x + b.w))
const dy = Math.max(n.y + b.y - y, 0, y - (n.y + b.y + b.h))
const d = Math.max(dx, dy)
if (d < bestD) {
best = n
bestD = d
}
}
return best
}
/** Docks a bench to a component, just below it. */
function dockAt(bench: CircuitNode, chip: CircuitNode) {
const b = shapeOf(chip, getDef.value).body
bench.bench = { rows: bench.bench?.rows ?? [], on: chip.id }
bench.x = chip.x + b.x
bench.y = Math.ceil(chip.y + b.y + b.h + 1.5)
}
/** Docks a bench to a component of this circuit (or undocks it) and runs its tests. */
function dockBench(id: string, chipId: string | null) {
const n = def.value.nodes.find(x => x.id === id)
const chip = chipId ? def.value.nodes.find(x => x.id === chipId && x.kind === 'chip') : undefined
if (!n?.bench) return
if (chip) dockAt(n, chip)
else delete n.bench.on
commit(false)
scheduleBenches()
}
/** The component a bench tests, if it is docked to one that is still here. */
function benchTarget(bench: CircuitNode): { node: CircuitNode; def: ComponentDef } | undefined {
const node = bench.bench?.on ? def.value.nodes.find(n => n.id === bench.bench!.on) : undefined
const d = node?.kind === 'chip' && node.chip ? getDef.value(node.chip) : undefined
return node && d ? { node, def: d } : undefined
}
/** Runs a bench's tests now; by hand they may run longer. */
function runBenchNow(id: string, byHand = false) {
const n = def.value.nodes.find(x => x.id === id)
if (!n?.bench) return
const t = benchTarget(n)
const res: BenchResult = t
? runBench(getDef.value, t.def.id, n.bench.rows, byHand ? HAND_TICKS : AUTO_TICKS)
: { rows: [], pass: 0, fail: 0, error: 'Dock it to a component: drop it on one, or pick one in its tests' }
benchResults.value.set(id, res)
benchResults.value = new Map(benchResults.value)
if (byHand) play(res.fail || res.error ? 'error' : 'connect')
}
/** Runs every bench of the open circuit a moment after the last change. */
function scheduleBenches() {
clearTimeout(benchTimer)
if (!def.value.nodes.some(n => n.kind === 'bench')) return
benchTimer = setTimeout(() => {
for (const n of def.value.nodes) if (n.kind === 'bench') runBenchNow(n.id)
}, 300)
}
/** Changes a bench's tests (an undo step) and runs them again. */
function setBench(id: string, change: (b: Bench) => void) {
const n = def.value.nodes.find(x => x.id === id)
if (!n?.bench) return
change(n.bench)
commit(false)
scheduleBenches()
}
// ── Assembler ─────────────────────────────────────────────────────
/** The assembler's texts, kept in the project (not undo steps). */
const asmDoc = computed<AsmDoc>(() => project.value.asm ?? { ...ASM_EXAMPLE })
@ -3007,6 +3098,12 @@ function create() {
setRowsType,
setScreen,
setRgb,
benchResults,
benchOpen,
benchTarget,
runBenchNow,
setBench,
dockBench,
screens,
keyQueues,
devVersion,

View File

@ -147,6 +147,12 @@ export function useMenu() {
{ label: 'Design its box', icon: 'lucide:pen-tool', run: () => ed.designFace(n.chip!) },
)
}
if (n.kind === 'bench') {
out.push(
{ label: 'Edit the tests', icon: 'lucide:table', keys: 'Double-click', run: () => (ed.benchOpen.value = n.id) },
{ label: 'Run the tests', icon: 'lucide:play', run: () => ed.runBenchNow(n.id, true) },
)
}
if (n.kind === 'terminal') out.push({ label: 'Clear the screen', icon: 'lucide:eraser', run: () => ed.clearScreen(n.id) })
if (n.kind === 'keyboard') {
out.push({ label: ed.typingInto.value === n.id ? 'Stop typing' : 'Type into it', icon: 'lucide:keyboard', keys: 'Click', run: () => ed.typeInto(n.id) })

View File

@ -55,6 +55,18 @@ export const BASIC_GROUPS: { name: string; parts: BasicPart[] }[] = [
{ kind: 'ref', name: 'Use signal', icon: 'lucide:tag', hint: 'The signal of the "Send signal" with the same name, anywhere in this circuit.', aka: 'label net reference' },
],
},
{
name: 'Testing',
parts: [
{
kind: 'bench',
name: 'Test bench',
icon: 'lucide:flask-conical',
hint: 'Drop it on a component: a table of tests for it. Every combination, clock pulses, or ticks until the outputs are right.',
aka: 'test truth table check verify unit',
},
],
},
{
name: 'Notes',
parts: [{ kind: 'note', name: 'Note', icon: 'lucide:sticky-note', key: 'T', hint: 'Text label for your drawing.', aka: 'text comment' }],

240
app/lib/logic/bench.ts Normal file
View File

@ -0,0 +1,240 @@
// Test benches: a table of tests docked to a placed component. Each row sets
// its inputs, gives it clock ticks if asked, and checks its outputs. The
// component runs on its own (no wires to the circuit around it), from all
// zeros, its state carried from row to row, so a row can test memory, a
// counter, or a whole computer running until it is done.
//
// Pins A0, A1, … make one column A that takes a number. An input cell is a
// number (12, 0x0C, 0b1100), P for a clock pulse, or empty to leave it as
// it was. An output cell is the number it should show, empty for any.
// Ticks: N pulses (the P inputs and the component's own clocks) before the
// check; ≤N gives up to N, checking after each, until the outputs match.
import { compile } from './compile'
import { chipPins, type DefLookup } from './geometry'
import { Simulator } from './sim'
import type { Bench, BenchRow, ComponentDef } from './types'
export interface BenchColumn {
name: string
/** Pin keys, bit 0 first. */
keys: string[]
}
export interface RowResult {
/** True all checks hold, false one does not, null nothing to check. */
ok: boolean | null
/** Outputs after the row, by column. */
got: Record<string, number>
/** Ticks the row ran. */
ticks: number
error?: string
}
export interface BenchResult {
rows: RowResult[]
pass: number
fail: number
error?: string
}
const SETTLE = 2_000_000
/** Ticks a run gives in all, at most (a run in the editor stays short; by hand it may go longer). */
export const AUTO_TICKS = 20_000
export const HAND_TICKS = 500_000
/** Rows "every combination" makes, at most (10 input bits). */
export const MAX_COMBO_BITS = 10
const SERIES = /^(.*?)(\d+)$/
/** Pins as columns: A0…An (numbered from 0, none missing) make one column A, any other pin is its own. */
export function groupPins(pins: { key: string; label: string }[]): BenchColumn[] {
const series = new Map<string, Map<number, string>>()
for (const p of pins) {
const m = SERIES.exec(p.label.trim())
if (!m || !m[1]) continue
const s = series.get(m[1]) ?? new Map<number, string>()
s.set(Number(m[2]), p.key)
series.set(m[1], s)
}
const whole = new Set([...series.entries()].filter(([, s]) => s.size >= 2 && [...s.keys()].every(i => i < s.size)).map(([name]) => name))
const out: BenchColumn[] = []
const done = new Set<string>()
for (const p of pins) {
const m = SERIES.exec(p.label.trim())
const name = m?.[1]
if (name && whole.has(name)) {
if (done.has(name)) continue
done.add(name)
const s = series.get(name)!
out.push({ name, keys: [...s.keys()].sort((a, b) => a - b).map(i => s.get(i)!) })
} else out.push({ name: p.label.trim() || p.key, keys: [p.key] })
}
return out
}
export function benchColumns(def: ComponentDef): { ins: BenchColumn[]; outs: BenchColumn[] } {
const pins = chipPins(def)
return { ins: groupPins(pins.inputs), outs: groupPins(pins.outputs) }
}
/** A cell as a number, P (inputs only), or null when empty or a don't-care. */
export function parseCell(s: string | undefined, bits: number, input: boolean): number | 'P' | null | { error: string } {
const t = (s ?? '').trim()
if (!t || t === '-' || t.toLowerCase() === 'x') return null
if (input && /^(p|↑)$/i.test(t)) return 'P'
let v: number
if (/^\d+$/.test(t)) v = parseInt(t, 10)
else if (/^0x[0-9a-f]+$/i.test(t)) v = parseInt(t.slice(2), 16)
else if (/^0b[01]+$/i.test(t)) v = parseInt(t.slice(2), 2)
else if (/^-\d+$/.test(t)) v = 2 ** bits + parseInt(t, 10)
else return { error: `"${t}" is not a number${input ? ' or P' : ''}` }
if (v < 0 || v >= 2 ** bits) return { error: `${t} does not fit in ${bits} bit${bits === 1 ? '' : 's'}` }
return v
}
/** Ticks of a row: N, or ≤N (also <=N) to wait for the outputs. Empty: one pulse when the row has a P, else none. */
export function parseTicks(s: string | undefined): { n: number; wait: boolean } | null | { error: string } {
const t = (s ?? '').trim()
if (!t) return null
const m = /^(≤|<=|<|max\s*|up\s*to\s*)?\s*(\d+)$/i.exec(t)
if (!m) return { error: `Ticks "${t}": a number, or ≤ and a number` }
const n = parseInt(m[2]!, 10)
if (n > HAND_TICKS) return { error: `Ticks: at most ${HAND_TICKS}` }
return { n, wait: !!m[1] }
}
/** Runs a bench's rows on the component, in order. */
export function runBench(getDef: DefLookup, defId: string, rows: BenchRow[], maxTicks = AUTO_TICKS): BenchResult {
const def = getDef(defId)
const res: BenchResult = { rows: [], pass: 0, fail: 0 }
if (!def) return { ...res, error: 'The component it tests no longer exists' }
const cols = benchColumns(def)
const sim = new Simulator(compile(getDef, defId))
sim.run(SETTLE)
const clocks = sim.nl.clocks.length > 0
let left = maxTicks
const read = (c: BenchColumn) => c.keys.reduce((v, k, i) => v + (sim.output(k) ? 2 ** i : 0), 0)
for (const row of rows) {
const out: RowResult = { ok: null, got: {}, ticks: 0 }
res.rows.push(out)
const fail = (error: string) => {
out.ok = false
out.error = error
res.fail++
}
// Inputs, pulses held low for now.
const pulses: BenchColumn[] = []
let bad = ''
for (const c of cols.ins) {
const v = parseCell(row.set?.[c.name], c.keys.length, true)
if (v && typeof v === 'object') bad ||= `${c.name}: ${v.error}`
else if (v === 'P') {
pulses.push(c)
c.keys.forEach(k => sim.setSource(k, false))
} else if (v !== null) c.keys.forEach((k, i) => sim.setSource(k, Math.floor(v / 2 ** i) % 2 === 1))
}
const checks: { c: BenchColumn; want: number }[] = []
for (const c of cols.outs) {
const v = parseCell(row.expect?.[c.name], c.keys.length, false)
if (v && typeof v === 'object') bad ||= `${c.name}: ${v.error}`
else if (typeof v === 'number') checks.push({ c, want: v })
}
const ticks = parseTicks(row.ticks)
if (ticks && 'error' in ticks) bad ||= ticks.error
if (bad) {
fail(bad)
continue
}
const spec = (ticks as { n: number; wait: boolean } | null) ?? { n: pulses.length ? 1 : 0, wait: false }
if (spec.n && !pulses.length && !clocks) {
fail('Ticks, but nothing to tick: give a clock input P, or a clock inside')
continue
}
if (!sim.run(SETTLE)) {
fail('It oscillates and never settles')
continue
}
const matches = () => checks.every(({ c, want }) => read(c) === want)
const tick = (): boolean => {
for (const c of pulses) c.keys.forEach(k => sim.setSource(k, true))
if (clocks) sim.setClock(1)
if (!sim.run(SETTLE)) return false
for (const c of pulses) c.keys.forEach(k => sim.setSource(k, false))
if (clocks) sim.setClock(0)
return sim.run(SETTLE)
}
let stopped = ''
for (let t = 0; t < spec.n; t++) {
if (spec.wait && checks.length && matches()) break
if (left-- <= 0) {
stopped = `Stopped after ${maxTicks.toLocaleString('en')} ticks in all`
break
}
if (!tick()) {
stopped = 'It oscillates and never settles'
break
}
out.ticks++
}
for (const c of cols.outs) out.got[c.name] = read(c)
if (stopped) {
fail(stopped)
if (left <= 0) {
res.error = stopped
break
}
continue
}
if (!checks.length) continue
out.ok = matches()
if (out.ok) res.pass++
else {
res.fail++
if (spec.wait) out.error = `Not within ${spec.n} ticks`
}
}
return res
}
/** Every combination of the inputs, counting up (the first column the highest), outputs left to fill. */
export function combinationRows(def: ComponentDef): BenchRow[] | { error: string } {
const { ins } = benchColumns(def)
const bits = ins.reduce((n, c) => n + c.keys.length, 0)
if (!ins.length) return { error: 'It has no inputs' }
if (bits > MAX_COMBO_BITS) return { error: `${bits} input bits are ${2 ** bits} combinations: at most ${MAX_COMBO_BITS} bits` }
const rows: BenchRow[] = []
for (let i = 0; i < 2 ** bits; i++) {
let rest = i
const set: Record<string, string> = {}
for (let k = ins.length - 1; k >= 0; k--) {
const c = ins[k]!
const n = 2 ** c.keys.length
set[c.name] = String(rest % n)
rest = Math.floor(rest / n)
}
rows.push({ set })
}
return rows
}
/**
* The rows with what the component shows now in their empty output cells
* (to keep it working the same). Outputs already filled in stay as they are,
* so a row that waits for them waits the same.
*/
export function recordExpected(getDef: DefLookup, defId: string, rows: BenchRow[]): BenchRow[] {
const r = runBench(getDef, defId, rows, HAND_TICKS)
return rows.map((row, i) => {
const got = r.rows[i]?.got
if (!got || (r.rows[i]?.error && r.rows[i]?.ok !== false)) return row
const expect = { ...row.expect }
for (const [k, v] of Object.entries(got)) if (!expect[k]?.trim()) expect[k] = String(v)
return { ...row, expect }
})
}
/** A bench cell's value for a column, by name. */
export const cellOf = (row: BenchRow, side: 'set' | 'expect', name: string) => row[side]?.[name] ?? ''
export type { Bench, BenchRow }

View File

@ -727,6 +727,8 @@ const TERMINAL_SHAPE: Shape = {
outputs: [],
}
const BENCH_SHAPE: Shape = { body: { x: 0, y: -0.5, w: 9, h: 2 }, inputs: [], outputs: [] }
const KEYBOARD_SHAPE: Shape = {
body: { x: 0, y: -0.5, w: 7, h: 9 },
inputs: [{ key: 'next', label: 'NEXT', dx: 0, dy: 8 }],
@ -831,6 +833,8 @@ export function shapeOf(node: CircuitNode, getDef: DefLookup): Shape {
return node.rgb ? PIXEL_RGB_SHAPE : PIXEL_SHAPE
case 'terminal':
return TERMINAL_SHAPE
case 'bench':
return BENCH_SHAPE
case 'keyboard':
return KEYBOARD_SHAPE
case 'outpanel':

View File

@ -264,6 +264,11 @@ function redock(nodes: CircuitNode[], ids: Map<string, string>) {
if (to) n[k] = to
else delete n[k]
}
if (n.bench?.on) {
const to = ids.get(n.bench.on)
if (to) n.bench.on = to
else delete n.bench.on
}
if (!n.attach) continue
const to = ids.get(n.attach)
if (to) n.attach = to

View File

@ -30,6 +30,7 @@ export const KIND_NAMES: Partial<Record<NodeKind, string>> = {
ref: 'Use signal',
terminal: 'Terminal',
keyboard: 'Keyboard',
bench: 'Test bench',
}
/** Challenges each one builds on. */

View File

@ -9,6 +9,7 @@ import {
PANEL_CONTROLS,
WIRE_COLORS,
type AsmDoc,
type Bench,
type CircuitNode,
type ComponentDef,
type ConnectorType,
@ -48,6 +49,21 @@ function cleanNode(n: CircuitNode) {
if (n.inner !== undefined && n.inner !== true) delete n.inner
if (n.screen !== undefined && typeof n.screen !== 'string') delete n.screen
if (n.rgb !== undefined && n.rgb !== true) delete n.rgb
if (n.bench !== undefined) {
const b = n.bench as Partial<Bench>
const cells = (r: unknown) => (r && typeof r === 'object' && !Array.isArray(r) ? Object.fromEntries(Object.entries(r).filter(([, v]) => typeof v === 'string').slice(0, 256)) : undefined)
if (!b || typeof b !== 'object' || !Array.isArray(b.rows)) delete n.bench
else {
n.bench = {
...(typeof b.on === 'string' ? { on: b.on } : {}),
rows: b.rows.slice(0, 4096).filter(r => r && typeof r === 'object').map(r => ({
...(cells(r.set) ? { set: cells(r.set) } : {}),
...(cells(r.expect) ? { expect: cells(r.expect) } : {}),
...(typeof r.ticks === 'string' ? { ticks: r.ticks } : {}),
})),
}
}
}
}
const MAX_ROWS = 256

View File

@ -24,6 +24,7 @@ export type BuiltinKind =
| 'ref' // carries the signal of the tag with the same name in this circuit, without a wire
| 'terminal' // a text screen: writes the character on D0–D7 at a rising WR, clears at a rising CLR
| 'keyboard' // the keys you type: the first waiting one on D0–D7, RDY while one waits, a rising NEXT takes it
| 'bench' // a table of tests for the placed component it is docked to (no pins)
export type NodeKind = BuiltinKind | 'chip'
@ -78,10 +79,28 @@ export interface CircuitNode {
screen?: string
/** Pixel with three inputs, red, green and blue: eight colours. */
rgb?: boolean
/** Test bench: its tests and the component it tests. */
bench?: Bench
/** Component: its connector ports (port node ids inside) shown split on this box, with a pin per line to wire by hand. */
splitPorts?: string[]
}
/** A test bench (see lib/logic/bench). */
export interface Bench {
/** The placed component it is docked to (a chip node in the same circuit). */
on?: string
rows: BenchRow[]
}
export interface BenchRow {
/** Input cells by column name: a number, P for a pulse; empty leaves it. */
set?: Record<string, string>
/** Output cells by column name: the number it should show; empty for any. */
expect?: Record<string, string>
/** N ticks before the check, or ≤N to wait up to N. */
ticks?: string
}
export const PANEL_CONTROLS = ['switch', 'button', 'clock'] as const
export type PanelControl = (typeof PANEL_CONTROLS)[number]

95
tests/bench.test.ts Normal file
View File

@ -0,0 +1,95 @@
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/)
})
})