Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/core/LinearLayout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ describe('LinearLayout', () => {
})
})

describe('fromBitMatrix', () => {
it('should reconstruct a 1D identity layout', () => {
const matrix = [
[1, 0],
[0, 1],
]
const layout = LinearLayout.fromBitMatrix(
matrix,
[{ name: 'in', size: 4 }],
[{ name: 'out', size: 4 }]
)

for (let i = 0; i < 4; i++) {
expect(layout.apply({ in: i }).out).toBe(i)
}
})
})

describe('XOR linearity', () => {
it('should satisfy L(a⊕b) = L(a)⊕L(b)', () => {
const layout = LinearLayout.identity1D(8, 'in', 'out')
Expand Down Expand Up @@ -215,6 +233,36 @@ describe('LinearLayout', () => {
}
}
})

it('should report non-invertible layouts', () => {
const nonSquare = new LinearLayout(
[
['in', [[1, 0]]],
],
[
['out0', 4],
['out1', 4],
]
)

expect(nonSquare.isInvertible()).toBe(false)
expect(() => nonSquare.invert()).toThrowError()
})

it('should reject layouts where output bits are zero but inputs are not', () => {
const broadcast = LinearLayout.zeros1D(8, 'reg', 'zero')
expect(broadcast.isInvertible()).toBe(false)
expect(() => broadcast.invert()).toThrowError(/square and surjective/i)
})

it('should invert layouts whose input/output spaces collapse to a single value', () => {
const trivial = LinearLayout.identity1D(1, 'in', 'out')
expect(trivial.isInvertible()).toBe(true)
const inverted = trivial.invert()
expect(inverted.apply({ out: 0 }).in).toBe(0)
expect(inverted.getOutDimNames()).toEqual(['in'])
expect(inverted.getInDimNames()).toEqual(['out'])
})
})

describe('ensureNotSmallerThan', () => {
Expand Down
Loading