logigate/tests/plugs.test.ts

231 lines
10 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { bundleWires, cablePanel, plugInto, syncCables } from '../app/lib/logic/cables'
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'])
// The names take a column on the cable side: the wires keep their place.
const s = shapeOf(res.split, lookup(p))
expect(s.outputs.every(o => o.dx === 1)).toBe(true)
expect(s.inputs[0]!.dx).toBeLessThan(0)
expect(s.body.x).toBe(s.inputs[0]!.dx)
// Plugged in, the bundle leaves the names to the component.
expect(shapeOf(res.bundle, lookup(p)).body.w).toBe(1)
})
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('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)
})
})