|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 5 | + |
| 6 | +vi.mock('../src/themer/themer', () => ({ |
| 7 | + colors: { |
| 8 | + 'stroke': '#000000', |
| 9 | + 'fill': '#FFFFFF', |
| 10 | + 'hover_select': '#CCCCCC', |
| 11 | + 'text': '#000000' |
| 12 | + }, |
| 13 | + updateThemeForStyle: vi.fn(), |
| 14 | + getThemeCardSvg: vi.fn() |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock('../src/engine', () => ({ |
| 18 | + scheduleUpdate: vi.fn(), |
| 19 | + wireToBeCheckedSet: vi.fn(), |
| 20 | + updateCanvasSet: vi.fn(), |
| 21 | + forceResetNodesSet: vi.fn(), |
| 22 | + updateSimulationSet: vi.fn(), |
| 23 | + renderCanvas: vi.fn(), |
| 24 | + canvasMessageData: {}, |
| 25 | + errorDetectedGet: vi.fn().mockReturnValue(false), |
| 26 | + errorDetectedSet: vi.fn() |
| 27 | +})); |
| 28 | + |
| 29 | +vi.mock('../src/ux', () => ({ |
| 30 | + fillSubcircuitElements: vi.fn() |
| 31 | +})); |
| 32 | + |
| 33 | +vi.mock('../src/modules', () => ({ |
| 34 | + changeInputSize: vi.fn(), |
| 35 | + default: {} |
| 36 | +})); |
| 37 | + |
| 38 | +vi.mock('../src/subcircuit', () => ({ |
| 39 | + default: class SubCircuit {} |
| 40 | +})); |
| 41 | + |
| 42 | +vi.mock('../src/data/load', () => ({ |
| 43 | + loadScope: vi.fn(), |
| 44 | + default: vi.fn() |
| 45 | +})); |
| 46 | + |
| 47 | +vi.mock('../src/data', () => ({ |
| 48 | + default: { |
| 49 | + save: vi.fn(), |
| 50 | + load: vi.fn(), |
| 51 | + createSaveAsImgPrompt: vi.fn(), |
| 52 | + clearProject: vi.fn(), |
| 53 | + newProject: vi.fn(), |
| 54 | + saveOffline: vi.fn(), |
| 55 | + createOpenLocalPrompt: vi.fn(), |
| 56 | + recoverProject: vi.fn(), |
| 57 | + createSubCircuitPrompt: vi.fn(), |
| 58 | + createCombinationalAnalysisPrompt: vi.fn(), |
| 59 | + fullViewOption: vi.fn(), |
| 60 | + colorThemes: vi.fn(), |
| 61 | + showTourGuide: vi.fn(), |
| 62 | + newVerilogModule: vi.fn(), |
| 63 | + generateVerilog: vi.fn(), |
| 64 | + bitconverter: vi.fn(), |
| 65 | + createNewCircuitScope: vi.fn(), |
| 66 | + customShortcut: vi.fn(), |
| 67 | + ExportProject: {}, |
| 68 | + ImportProject: {} |
| 69 | + } |
| 70 | +})); |
| 71 | + |
| 72 | +vi.mock('../src/combinationalAnalysis', () => ({ |
| 73 | + performCombinationalAnalysis: vi.fn(), |
| 74 | + GenerateCircuit: vi.fn(), |
| 75 | + createBooleanPrompt: vi.fn() |
| 76 | +})); |
| 77 | + |
| 78 | +vi.mock('../src/layoutMode', () => ({ |
| 79 | + layoutModeGet: vi.fn().mockReturnValue(false), |
| 80 | + layoutModeSet: vi.fn(), |
| 81 | + tempBuffer: {}, |
| 82 | + determineLabel: vi.fn(), |
| 83 | + paneLayout: vi.fn(), |
| 84 | + layoutUpdate: vi.fn() |
| 85 | +})); |
| 86 | + |
| 87 | +import ParityGenerator from '../src/modules/ParityGenerator'; |
| 88 | +import { simulationArea } from '../src/simulationArea'; |
| 89 | + |
| 90 | +describe('ParityGenerator', () => { |
| 91 | + let parityGen; |
| 92 | + |
| 93 | + beforeEach(() => { |
| 94 | + global.globalScope = { |
| 95 | + ParityGenerator: [], |
| 96 | + scale: 1, |
| 97 | + ox: 0, |
| 98 | + oy: 0, |
| 99 | + allNodes: [], |
| 100 | + nodes: [] |
| 101 | + }; |
| 102 | + |
| 103 | + simulationArea.simulationQueue = { |
| 104 | + add: vi.fn() |
| 105 | + }; |
| 106 | + simulationArea.context = { |
| 107 | + beginPath: vi.fn(), |
| 108 | + fillStyle: '', |
| 109 | + textAlign: '', |
| 110 | + fill: vi.fn(), |
| 111 | + stroke: vi.fn(), |
| 112 | + moveTo: vi.fn(), |
| 113 | + lineTo: vi.fn(), |
| 114 | + bezierCurveTo: vi.fn(), |
| 115 | + closePath: vi.fn(), |
| 116 | + arc: vi.fn(), |
| 117 | + fillText: vi.fn(), |
| 118 | + measureText: vi.fn(() => ({ width: 0 })) |
| 119 | + }; |
| 120 | + }); |
| 121 | + |
| 122 | + it('should initialize with 3 inputs by default', () => { |
| 123 | + parityGen = new ParityGenerator(0, 0, global.globalScope, 'RIGHT', 3); |
| 124 | + expect(parityGen.inputSize).toBe(3); |
| 125 | + expect(parityGen.inp.length).toBe(3); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should calculate parity correctly (Odd 1s -> 1)', () => { |
| 129 | + parityGen = new ParityGenerator(0, 0, global.globalScope, 'RIGHT', 3); |
| 130 | + parityGen.inp[0].value = 1; |
| 131 | + parityGen.inp[1].value = 0; |
| 132 | + parityGen.inp[2].value = 0; |
| 133 | + parityGen.resolve(); |
| 134 | + expect(parityGen.output1.value).toBe(1); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should calculate parity correctly (Even 1s -> 0)', () => { |
| 138 | + parityGen = new ParityGenerator(0, 0, global.globalScope, 'RIGHT', 3); |
| 139 | + parityGen.inp[0].value = 1; |
| 140 | + parityGen.inp[1].value = 1; |
| 141 | + parityGen.inp[2].value = 0; |
| 142 | + parityGen.resolve(); |
| 143 | + expect(parityGen.output1.value).toBe(0); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should work with 4 inputs', () => { |
| 147 | + parityGen = new ParityGenerator(0, 0, global.globalScope, 'RIGHT', 4); |
| 148 | + expect(parityGen.inputSize).toBe(4); |
| 149 | + expect(parityGen.inp.length).toBe(4); |
| 150 | + |
| 151 | + parityGen.inp[0].value = 1; |
| 152 | + parityGen.inp[1].value = 1; |
| 153 | + parityGen.inp[2].value = 1; |
| 154 | + parityGen.inp[3].value = 0; |
| 155 | + parityGen.resolve(); |
| 156 | + expect(parityGen.output1.value).toBe(1); |
| 157 | + |
| 158 | + parityGen.inp[3].value = 1; |
| 159 | + parityGen.resolve(); |
| 160 | + expect(parityGen.output1.value).toBe(0); |
| 161 | + }); |
| 162 | +}); |
0 commit comments