logigate/tests/wide.test.ts

121 lines
4.9 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 { nandCounts, truthTable } from '../app/lib/logic/analyze'
import { compile } from '../app/lib/logic/compile'
import { shapeOf } from '../app/lib/logic/geometry'
import { createProject, lookup } from '../app/lib/logic/ops'
import { Simulator } from '../app/lib/logic/sim'
import type { ComponentDef } from '../app/lib/logic/types'
import { gateFamily, NAND_BASE, wideGate } from '../app/lib/logic/wide'
const FNS: Record<string, (bits: number[]) => number> = {
'ex-and': b => +b.every(Boolean),
'ex-or': b => +b.some(Boolean),
'ex-xor': b => b.reduce((x, y) => x ^ y, 0),
'ex-nor': b => +!b.some(Boolean),
[NAND_BASE]: b => +!b.every(Boolean),
}
describe('gates with more inputs', () => {
it('knows which gates can widen', () => {
const p = createProject({ examples: true })
const get = lookup(p)
expect(gateFamily(get, 'ex-and')).toBe('chain')
expect(gateFamily(get, 'ex-or')).toBe('chain')
expect(gateFamily(get, 'ex-xor')).toBe('chain')
expect(gateFamily(get, NAND_BASE)).toBe('inverted')
expect(gateFamily(get, 'ex-nor')).toBe('inverted')
expect(gateFamily(get, 'ex-xnor')).toBeNull()
expect(gateFamily(get, 'ex-mux')).toBeNull()
expect(gateFamily(get, 'ex-not')).toBeNull()
})
for (const [base, fn] of Object.entries(FNS)) {
it(`builds ${base} with 3 to 9 inputs that does the right thing`, () => {
const p = createProject({ examples: true })
for (const n of [3, 4, 5, 8, 9]) {
const target = wideGate(p, base, n)
if (target?.kind !== 'chip') throw new Error('no wide gate')
const t = truthTable(lookup(p), target.chip)
if ('error' in t) throw new Error(t.error)
expect(t.inputs).toHaveLength(n)
for (const r of t.rows) expect(r.outs[0], `${base} ×${n} ${r.ins.join('')}`).toBe(fn(r.ins))
}
})
}
it('counts honestly and reuses what it built', () => {
const p = createProject({ examples: true })
const counts = () => nandCounts(p.components)
const and4 = wideGate(p, 'ex-and', 4)!
expect(wideGate(p, 'ex-and', 4)).toEqual(and4)
if (and4.kind !== 'chip') throw new Error()
expect(counts().get(and4.chip)).toBe(3 * counts().get('ex-and')!)
// NAND with 3 inputs: an AND of two (NAND + NAND as NOT), then a NAND.
const nand3 = wideGate(p, NAND_BASE, 3)!
if (nand3.kind !== 'chip') throw new Error()
expect(counts().get(nand3.chip)).toBe(3)
expect(wideGate(p, 'ex-and', 2)).toEqual({ kind: 'chip', chip: 'ex-and' })
expect(wideGate(p, NAND_BASE, 2)).toEqual({ kind: 'nand' })
expect(wideGate(p, 'ex-and', 17)).toBeNull()
expect(wideGate(p, 'ex-xnor', 3)).toBeNull()
})
it('draws a wide AND as an AND symbol with all its pins', () => {
const p = createProject({ examples: true })
const t = wideGate(p, 'ex-and', 6)!
if (t.kind !== 'chip') throw new Error()
const s = shapeOf({ id: 'g', kind: 'chip', chip: t.chip, x: 0, y: 0 }, lookup(p))
expect(s.gate).toBe('and')
expect(s.inputs).toHaveLength(6)
})
})
describe('pass-through lamps and outputs', () => {
it('a lamp passes its signal on', () => {
const p = createProject({ examples: false })
const def: ComponentDef = {
id: 'r',
name: 'R',
symbol: 'box',
nodes: [
{ id: 's', kind: 'input', x: 0, y: 0 },
{ id: 'l1', kind: 'lamp', x: 5, y: 0 },
{ id: 'l2', kind: 'lamp', x: 10, y: 0 },
],
wires: [
{ id: 'w1', from: { node: 's', pin: 'q' }, to: { node: 'l1', pin: 'a' } },
{ id: 'w2', from: { node: 'l1', pin: 'q' }, to: { node: 'l2', pin: 'a' } },
],
}
p.components.push(def)
const sim = new Simulator(compile(lookup(p), 'r'))
sim.setSource('s', true)
sim.run()
expect([sim.pin('l1', 'a'), sim.pin('l1', 'q'), sim.pin('l2', 'a')]).toEqual([1, 1, 1])
})
it('an output pin inside a component also feeds parts after it', () => {
const p = createProject({ examples: true })
const inner: ComponentDef = {
id: 'in',
name: 'In',
symbol: 'box',
nodes: [
{ id: 'a', kind: 'input', x: 0, y: 0, label: 'A' },
{ id: 'q', kind: 'output', x: 6, y: 0, label: 'Q' },
{ id: 'not', kind: 'chip', chip: 'ex-not', x: 12, y: 0 },
{ id: 'nq', kind: 'output', x: 20, y: 0, label: 'NQ' },
],
wires: [
{ id: 'w1', from: { node: 'a', pin: 'q' }, to: { node: 'q', pin: 'a' } },
{ id: 'w2', from: { node: 'q', pin: 'q' }, to: { node: 'not', pin: p.components.find(d => d.id === 'ex-not')!.nodes.find(n => n.kind === 'input')!.id } },
{ id: 'w3', from: { node: 'not', pin: p.components.find(d => d.id === 'ex-not')!.nodes.find(n => n.kind === 'output')!.id }, to: { node: 'nq', pin: 'a' } },
],
}
p.components.push(inner)
const t = truthTable(lookup(p), 'in')
if ('error' in t) throw new Error(t.error)
expect(t.rows.map(r => r.outs.join(''))).toEqual(['01', '10'])
})
})