hdl-js
Version:
Hardware definition language (HDL) and Hardware simulator
79 lines (63 loc) • 1.72 kB
JavaScript
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
;
var assert = require('assert');
var Pin = require('./Pin');
var _require = require('./Clock'),
SystemClock = _require.SystemClock;
/**
* Evaluates the gate logic on the truth table input.
*
* Example for `And` gate:
*
* [
* {a: 0, b: 0, out: 0},
* {a: 0, b: 1, out: 0},
* {a: 1, b: 0, out: 0},
* {a: 1, b: 1, out: 1},
* ]
*
* Throws if some `out` is not evaluate to the expected value.
*/
function testTruthTable(table, gate) {
SystemClock.reset();
var _gate$execOnData = gate.execOnData(table),
result = _gate$execOnData.result;
assert.deepEqual(table, result);
}
/**
* Automatically tests a gate based on it spec.
*/
function autoTestGate(GateClass) {
var spec = GateClass.Spec;
expect(spec.name).toBe(GateClass.name);
var createPins = function createPins(pinNames) {
return pinNames.map(function (pinName) {
var name = void 0,
size = 1;
if (typeof pinName === 'string') {
name = pinName;
} else {
name = pinName.name;
size = pinName.size;
}
return new Pin({ name: name, size: size });
});
};
var inputPins = createPins(spec.inputPins);
var outputPins = createPins(spec.outputPins);
var gate = new GateClass({
inputPins: inputPins,
outputPins: outputPins
});
expect(gate.getName()).toBe(GateClass.name);
expect(gate.getInputPins()).toEqual(inputPins);
expect(gate.getOutputPins()).toEqual(outputPins);
testTruthTable(spec.truthTable, gate);
}
module.exports = {
autoTestGate: autoTestGate,
testTruthTable: testTruthTable
};