logigate/tests/plugs.test.ts

356 lines
16 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.

import { describe, expect, it } from 'vitest'
import { bundleWires, cableFromPins, cableIntoPins, cablePanel, nameTags, pinGroups, plugInto, plugLayout, syncCables } from '../app/lib/logic/cables'
import { dropLinks, dropPins } from '../app/lib/logic/fit'
import { compile, rowSource } from '../app/lib/logic/compile'
import { chipPins, panelOffsets, pinPos, shapeOf } from '../app/lib/logic/geometry'
import { copySelection, createProject, lookup, pasteClip } from '../app/lib/logic/ops'
import { circuitRoutes } from '../app/lib/logic/routing'
import { Simulator } from '../app/lib/logic/sim'
import type { CircuitNode, ComponentDef, Project } from '../app/lib/logic/types'
/** Switches A0..A3 and B0..B3 wired to an Adder 4, its outputs to lamps. */
function adder(): { p: Project; def: ComponentDef; ins: string[]; outs: string[] } {
const p = createProject({ examples: true })
const pins = chipPins(lookup(p)('ex-add4')!)
const ins = pins.inputs.map(x => x.key)
const outs = pins.outputs.map(x => x.key)
const def: ComponentDef = {
id: 'root',
name: 'Root',
symbol: 'box',
nodes: [
{ id: 'add', kind: 'chip', chip: 'ex-add4', x: 20, y: 0 },
...pins.inputs.map((x, i) => ({ id: `s${i}`, kind: 'input' as const, x: 0, y: i * 3, label: x.label })),
...pins.outputs.map((_, i) => ({ id: `l${i}`, kind: 'lamp' as const, x: 50, y: 20 + i * 2 })),
],
wires: [
...ins.map((k, i) => ({ id: `wi${i}`, from: { node: `s${i}`, pin: 'q' }, to: { node: 'add', pin: k } })),
...outs.map((k, i) => ({ id: `wo${i}`, from: { node: 'add', pin: k }, to: { node: `l${i}`, pin: 'a' } })),
],
}
p.components.push(def)
return { p, def, ins, outs }
}
function sums(p: Project, def: ComponentDef): string[] {
const out: string[] = []
for (const [a, b] of [[3, 5], [15, 1], [9, 9], [0, 0]] as const) {
const s = new Simulator(compile(lookup(p), def.id))
for (let i = 0; i < 4; i++) {
s.setSource(`s${i}`, ((a >> i) & 1) === 1)
s.setSource(`s${i + 4}`, ((b >> i) & 1) === 1)
}
s.run()
out.push([0, 1, 2, 3, 4].map(i => s.pin(`l${i}`, 'a')).join(''))
}
return out
}
const node = (def: ComponentDef, id: string) => def.nodes.find(n => n.id === id)!
/** Where each wire on the single-wire side of a cable end meets the part it is plugged into. */
function touches(p: Project, def: ComponentDef, end: CircuitNode): boolean {
const get = lookup(p)
const wires = end.kind === 'bundle' ? def.wires.filter(w => w.to.node === end.id) : def.wires.filter(w => w.from.node === end.id)
return wires.every(w => {
const a = pinPos(node(def, w.from.node), shapeOf(node(def, w.from.node), get), w.from.pin, 'out')!
const b = pinPos(node(def, w.to.node), shapeOf(node(def, w.to.node), get), w.to.pin, 'in')!
return a.x === b.x && a.y === b.y
})
}
describe('plugged cable ends', () => {
it('bundling wires into one part plugs the split straight onto its pins', () => {
const { p, def } = adder()
const before = sums(p, def)
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
if (!res.ok) throw new Error(res.error)
expect(res.split.plug).toBe('add')
expect(res.bundle.plug).toBeUndefined()
expect(touches(p, def, res.split)).toBe(true)
expect(res.split.x).toBe(19)
expect(sums(p, def)).toEqual(before)
// The plugged wires have no length, so there is nothing to draw.
const routes = circuitRoutes(def, new Map(def.nodes.map(n => [n.id, shapeOf(n, lookup(p))])))
const plugged = new Set(def.wires.filter(w => w.from.node === res.split.id).map(w => w.id))
for (const r of routes.filter(r => plugged.has(r.id))) expect(r.grid).toHaveLength(2)
})
it('a bundle plugs onto the outputs, and the free split names its wires after them', () => {
const { p, def } = adder()
const res = bundleWires(def, ['wo0', 'wo1', 'wo2', 'wo3', 'wo4'], lookup(p))
if (!res.ok) throw new Error(res.error)
expect(res.bundle.plug).toBe('add')
syncCables(p)
expect(res.split.names).toEqual(['S0', 'S1', 'S2', 'S3', 'Cout'])
// Names never change the shape: the pins stay where they were.
const s = shapeOf(res.split, lookup(p))
expect(s.body).toEqual({ x: 0, y: -0.5, w: 1, h: 5 })
expect(s.outputs.every(o => o.dx === 1)).toBe(true)
// Plugged in, the bundle leaves the names to the component.
expect(nameTags(def, res.bundle, lookup(p))).toEqual([])
})
it('a free end shows only the names the part at the other end does not show already', () => {
const { p, def } = adder()
const get = lookup(p)
const res = bundleWires(def, ['wo0', 'wo1', 'wo2', 'wo3', 'wo4'], get)
if (!res.ok) throw new Error(res.error)
syncCables(p)
// Lamps without a name show the wire's name, dimmed.
expect(nameTags(def, res.split, get)).toEqual(['', '', '', '', ''])
// A lamp with another name, and a NAND gate, don't.
node(def, 'l1').label = 'Bit 1'
def.nodes.push({ id: 'g', kind: 'nand', x: 60, y: 30 })
def.wires.find(w => w.from.node === res.split.id && w.from.pin === 'b4')!.to = { node: 'g', pin: 'a' }
expect(nameTags(def, res.split, get)).toEqual(['', 'S1', '', '', 'Cout'])
})
it('a bundle fed by switches of the same names shows none', () => {
const { p, def } = adder()
const get = lookup(p)
const ins = chipPins(get('ex-add4')!).inputs
def.nodes.push({ id: 'b', kind: 'bundle', bits: 4, x: 8, y: 2 })
def.wires = def.wires.filter(w => !['wi4', 'wi5', 'wi6', 'wi7'].includes(w.id))
for (const i of [0, 1, 2, 3]) def.wires.push({ id: `f${i}`, from: { node: `s${i + 4}`, pin: 'q' }, to: { node: 'b', pin: `b${i}` } })
syncCables(p)
expect(node(def, 'b').names).toEqual(ins.slice(4, 8).map(x => x.label))
expect(nameTags(def, node(def, 'b'), get)).toEqual(['', '', '', ''])
})
it('names come from what drives the wires, else from what they drive', () => {
const { p, def } = adder()
for (const i of [0, 1, 2, 3]) delete node(def, `s${i}`).label
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
if (!res.ok) throw new Error(res.error)
syncCables(p)
expect(res.bundle.names).toEqual(['A0', 'A1', 'A2', 'A3'])
node(def, 's1').label = 'X'
syncCables(p)
expect(res.bundle.names).toEqual(['A0', 'X', 'A2', 'A3'])
})
it('follows its part, and comes loose when the pins no longer line up', () => {
const { p, def, ins } = adder()
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
if (!res.ok) throw new Error(res.error)
const chip = node(def, 'add')
chip.x += 7
chip.y += 3
syncCables(p)
expect(touches(p, def, res.split)).toBe(true)
// Rewire one wire to a pin further down: no longer in the cable's order on one edge.
def.wires.find(w => w.from.node === res.split.id && w.from.pin === 'b0')!.to.pin = ins[8]!
syncCables(p)
expect(res.split.plug).toBeUndefined()
})
it('a free end dropped with its pins on a part plugs in', () => {
const { p, def } = adder()
const res = bundleWires(def, ['wo0', 'wo1', 'wo2', 'wo3', 'wo4'], lookup(p))
if (!res.ok) throw new Error(res.error)
const { x, y } = res.bundle
delete res.bundle.plug
res.bundle.x += 3
syncCables(p)
expect(res.bundle.plug).toBeUndefined()
res.bundle.x = x
res.bundle.y = y
syncCables(p)
expect(res.bundle.plug).toBe('add')
})
it('one wire landing on a part does not plug the whole end in', () => {
const { p, def } = adder()
const res = bundleWires(def, ['wo0', 'wo1', 'wo2', 'wo3', 'wo4'], lookup(p))
if (!res.ok) throw new Error(res.error)
// Only the last wire runs on, to a lamp sitting right on its pin.
def.wires = def.wires.filter(w => w.from.node !== res.split.id || w.from.pin === 'b4')
const lamp = node(def, 'l4')
const pin = shapeOf(res.split, lookup(p)).outputs[4]!
lamp.x = res.split.x + pin.dx
lamp.y = res.split.y + pin.dy
syncCables(p)
expect(touches(p, def, res.split)).toBe(true)
expect(res.split.plug).toBeUndefined()
})
it('takes the spacing of pins further apart', () => {
const p = createProject({ examples: true })
const get = lookup(p)
const def: ComponentDef = {
id: 'root',
name: 'Root',
symbol: 'box',
nodes: [
{ id: 'g', kind: 'nand', x: 10, y: 0 },
{ id: 'b', kind: 'split', bits: 2, x: 0, y: 0 },
],
wires: [
{ id: 'w0', from: { node: 'b', pin: 'b0' }, to: { node: 'g', pin: 'a' } },
{ id: 'w1', from: { node: 'b', pin: 'b1' }, to: { node: 'g', pin: 'b' } },
],
}
p.components.push(def)
const split = node(def, 'b')
expect(plugInto(def, split, 'g', get)).toBe(true)
const gate = shapeOf(node(def, 'g'), get).inputs
expect(split.pitch).toBe(gate[1]!.dy - gate[0]!.dy)
expect(touches(p, def, split)).toBe(true)
})
})
describe('input panels for cables', () => {
it('a bundle gets a panel with a switch per wire, plugged in and named', () => {
const { p, def } = adder()
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
if (!res.ok) throw new Error(res.error)
// Take the switches away: the bundle has nothing driving it.
def.nodes = def.nodes.filter(n => !['s0', 's1', 's2', 's3'].includes(n.id))
def.wires = def.wires.filter(w => !['s0', 's1', 's2', 's3'].includes(w.from.node))
syncCables(p)
const panel = cablePanel(def, res.bundle.id, lookup(p))!
syncCables(p)
expect(panel.rows!.map(r => r.name)).toEqual(['A0', 'A1', 'A2', 'A3'])
expect(res.bundle.plug).toBe(panel.id)
expect(touches(p, def, res.bundle)).toBe(true)
// Flipping row 2 sets A2.
const s = new Simulator(compile(lookup(p), def.id))
s.setSource(rowSource(panel.id, panel.rows![2]!.id), true)
s.run()
expect([0, 1, 2, 3].map(i => s.pin(`l${i}`, 'a')).join('')).toBe('0010')
})
it('a bundle plugged into a panel takes the spacing of its rows', () => {
const { p, def } = adder()
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
if (!res.ok) throw new Error(res.error)
def.nodes = def.nodes.filter(n => !['s0', 's1', 's2', 's3'].includes(n.id))
def.wires = def.wires.filter(w => !['s0', 's1', 's2', 's3'].includes(w.from.node))
const panel = cablePanel(def, res.bundle.id, lookup(p))!
// A gap of two rows above the third row.
const at = panelOffsets(panel.rows!)
panel.rows!.forEach((r, i) => (r.dy = at[i]! + (i >= 2 ? 2 : 0)))
syncCables(p)
expect(res.bundle.offsets).toEqual([0, 1, 4, 5])
expect(touches(p, def, res.bundle)).toBe(true)
})
})
describe('copying plugged ends', () => {
it('a copied end plugs into the copy of its part, or into nothing', () => {
const { p, def } = adder()
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
if (!res.ok) throw new Error(res.error)
const both = pasteClip(def, copySelection(def, new Set(['add', res.split.id])), 0, 30)
const copy = both.map(id => node(def, id)).find(n => n.kind === 'split')!
const chip = both.map(id => node(def, id)).find(n => n.kind === 'chip')!
expect(copy.plug).toBe(chip.id)
const alone = pasteClip(def, copySelection(def, new Set([res.split.id])), 0, 60)
expect(node(def, alone[0]!).plug).toBeUndefined()
})
})
describe('cables saved before plugs existed', () => {
it('stay where they are and compute the same; only names are added', () => {
const { p, def } = adder()
const before = sums(p, def)
def.nodes.push({ id: 'b', kind: 'bundle', bits: 4, x: 8, y: 2 }, { id: 'sp', kind: 'split', bits: 4, x: 16, y: 2 })
def.wires = def.wires.filter(w => !['wi0', 'wi1', 'wi2', 'wi3'].includes(w.id))
const ins = chipPins(lookup(p)('ex-add4')!).inputs
for (const i of [0, 1, 2, 3]) {
def.wires.push({ id: `f${i}`, from: { node: `s${i}`, pin: 'q' }, to: { node: 'b', pin: `b${i}` } })
def.wires.push({ id: `t${i}`, from: { node: 'sp', pin: `b${i}` }, to: { node: 'add', pin: ins[i]!.key } })
}
def.wires.push({ id: 'c', from: { node: 'b', pin: 'q' }, to: { node: 'sp', pin: 'a' } })
syncCables(p)
expect([node(def, 'b').x, node(def, 'b').y, node(def, 'sp').x, node(def, 'sp').y]).toEqual([8, 2, 16, 2])
expect(node(def, 'b').plug ?? node(def, 'sp').plug).toBeUndefined()
expect(node(def, 'sp').names).toEqual(['A0', 'A1', 'A2', 'A3'])
expect(sums(p, def)).toEqual(before)
})
})
describe('plugging in more often', () => {
it('outputs of one part bundled to scattered targets: the bundle plugs into the part', () => {
const { p, def, outs } = adder()
// Lamps in reverse order: wire order follows the Adder's pins, not the lamps.
for (const i of [0, 1, 2, 3, 4]) node(def, `l${i}`).y = 40 - i * 3
const res = bundleWires(def, outs.map((_, i) => `wo${i}`), lookup(p))
if (!res.ok) throw new Error(res.error)
expect(res.bundle.plug).toBe('add')
expect(touches(p, def, res.bundle)).toBe(true)
})
it('an end with only some of its wires on a part still plugs in', () => {
const { p, def, ins } = adder()
def.nodes.push({ id: 'sp', kind: 'split', bits: 5, x: 0, y: 30 })
for (const i of [0, 1, 2, 3]) def.wires.push({ id: `t${i}`, from: { node: 'sp', pin: `b${i}` }, to: { node: 'add', pin: ins[i]! } })
const sp = node(def, 'sp')
const lay = plugLayout(def, sp, node(def, 'add'), lookup(p))!
sp.x = lay.x
sp.y = lay.y
syncCables(p)
expect(sp.plug).toBe('add')
})
})
describe('a cable straight off a part', () => {
it('offers numbered groups of pins and all of them', () => {
const groups = pinGroups(['A0', 'A1', 'A2', 'A3', 'B0', 'B1', 'B2', 'B3', 'Cin'].map(l => ({ key: l, label: l })))
expect(groups.map(g => g.label)).toEqual(['A0–A3', 'B0–B3', 'all 9'])
const outs = pinGroups(['S0', 'S1', 'S2', 'S3', 'Cout'].map(l => ({ key: l, label: l })), k => k !== 'S2')
expect(outs.map(g => g.label)).toEqual(['S0–S1', 'all 4 (S0–Cout)'])
})
it('from outputs: a bundle plugged on the pins, a free split with their names, then dropped onto the next part', () => {
const { p, def, outs } = adder()
const get = lookup(p)
// Take the lamps away: the outputs are free.
def.wires = def.wires.filter(w => w.from.node !== 'add')
const res = cableFromPins(def, 'add', outs.slice(0, 4), get)
if (!res.ok) throw new Error(res.error)
expect(res.bundle.plug).toBe('add')
expect(touches(p, def, res.bundle)).toBe(true)
syncCables(p)
expect(res.split.names).toEqual(['S0', 'S1', 'S2', 'S3'])
expect(res.split.x).toBeGreaterThan(res.bundle.x)
// A second adder: drop the split right onto its A inputs.
def.nodes.push({ id: 'add2', kind: 'chip', chip: 'ex-add4', x: 60, y: 0 })
const lay = (() => {
const s = shapeOf(node(def, 'add2'), get).inputs[0]!
return { x: 60 + s.dx - 1, y: s.dy }
})()
res.split.x = lay.x
res.split.y = lay.y
const moved = dropPins(def, [res.split], n => shapeOf(n, get))
const still = dropPins(def, def.nodes.filter(n => n !== res.split), n => shapeOf(n, get))
const drop = dropLinks(moved, still, 0)!
expect(drop.links).toHaveLength(4)
def.wires.push(...drop.links.map((l, i) => ({ id: `d${i}`, ...l })))
syncCables(p)
expect(res.split.plug).toBe('add2')
// A0 of the first adder set: S0 = 1 arrives at A0 of the second, whose S0 shows it.
def.nodes.push({ id: 'out', kind: 'lamp', x: 80, y: 0 })
def.wires.push({ id: 'o', from: { node: 'add2', pin: outs[0]! }, to: { node: 'out', pin: 'a' } })
const s = new Simulator(compile(get, def.id))
s.setSource('s0', true)
s.run()
expect(s.pin('out', 'a')).toBe(1)
})
it('into inputs: a split plugged on the free pins, a free bundle behind it', () => {
const { p, def, ins } = adder()
def.wires = def.wires.filter(w => w.to.node !== 'add')
const res = cableIntoPins(def, 'add', ins.slice(4, 8), lookup(p))
if (!res.ok) throw new Error(res.error)
expect(res.split.plug).toBe('add')
expect(touches(p, def, res.split)).toBe(true)
expect(res.bundle.x).toBeLessThan(res.split.x)
syncCables(p)
expect(res.bundle.names).toEqual(['B0', 'B1', 'B2', 'B3'])
// Those inputs are taken now: no second cable into them.
expect(cableIntoPins(def, 'add', ins.slice(4, 8), lookup(p)).ok).toBe(false)
})
})