Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hdl-js

Package Overview
Dependencies
Maintainers
1
Versions
72
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hdl-js - npm Package Compare versions

Comparing version 0.0.6 to 0.0.7

src/util/__tests__/typed-numbers-test.js

2

package.json
{
"name": "hdl-js",
"version": "0.0.6",
"version": "0.0.7",
"license": "MIT",

@@ -5,0 +5,0 @@ "description": "Hardware definition language (HDL) and Hardware simulator",

@@ -12,2 +12,4 @@ /**

const {int16} = require('../../../util/typed-numbers');
// Inputs.

@@ -82,3 +84,3 @@ const a = new Pin({name: 'a', value: 1});

expect(result).toEqual(data);
expect(result).toEqual([{a: 1, b: 1, out: 1}]);
expect(conflicts.length).toBe(0);

@@ -126,9 +128,11 @@

expect(conflicts.length).toBe(1);
expect(conflicts[0]).toEqual({row: 0, pins: {out: ~0b0000000000000000}});
expect(conflicts[0])
.toEqual({row: 0, pins: {out: int16(0b1111111111111111)}});
// Sets the outputs, no conflicts.
data = [{in: '0000000000000000', out: ~0b0000000000000000}];
data = [{in: '0000000000000000', out: int16(0b1111111111111111)}];
({result, conflicts} = not16.execOnData(data));
expect(result).toEqual(data);
expect(result)
.toEqual([{in: 0, out: int16(0b1111111111111111)}]);
expect(conflicts.length).toBe(0);

@@ -135,0 +139,0 @@ });

@@ -13,9 +13,9 @@ /**

it('Pin interface', () => {
const a = new Pin({name: 'a', value: true});
const a = new Pin({name: 'a', value: 1});
expect(a.getName()).toBe('a');
expect(a.getValue()).toBe(true);
expect(a.getValue()).toBe(1);
a.setValue(false);
expect(a.getValue()).toBe(false);
a.setValue(0);
expect(a.getValue()).toBe(0);
});

@@ -29,2 +29,11 @@

it('string value converted to number', () => {
let a = new Pin({name: 'a', value: '1'});
expect(a.getValue()).toBe(1);
a = new Pin({name: 'a', value: 1});
a.setValue('1');
expect(a.getValue()).toBe(1);
});
});

@@ -80,2 +80,16 @@ /**

it('set value', () => {
const a16 = new PinBus({
name: 'a',
size: 16,
});
// Number value.
a16.setValue(0b0101010101010101);
expect(a16.getValue()).toBe(0b0101010101010101);
a16.setValue('0101010101010101');
expect(a16.getValue()).toBe(0b0101010101010101);
});
});

@@ -9,7 +9,5 @@ /**

const Not16 = require('../Not16');
const PinBus = require('../../PinBus');
const GateTestUtil = require('../../gate-test-util');
describe('Not16', () => {
it('Not16 interface', () => {

@@ -19,46 +17,2 @@ expect(() => GateTestUtil.autoTestGate(Not16))

});
it('Not16 manual string table', () => {
// Input.
const in16 = new PinBus({name: 'in', size: 16});
// Output.
const out16 = new PinBus({name: 'out', size: 16});
const not16 = new Not16({
inputPins: [in16],
outputPins: [out16],
});
// Since ~ operator inverts all bits, we use bit strings here
// for comparison of the result.
//
// The (~0b0000000000000000).toString(2) is not `0b1111111111111111`,
// but just '-1' in JS, so we have to do:
//
// (~0b0000000000000000 >>> 0).toString(2).slice(16)
//
// to get the bit string.
//
// For this test override `getValue` of the output.
//
out16.getValue = function() {
// The this._value >>> 0 gives 32 bit value,
// so do slice(16) to get needed 16 bits.
return (this._value >>> 0).toString(2).slice(16);
};
let truthTable = [
{in: 0b0000000000000000, out: '1111111111111111'},
{in: 0b1111111111111111, out: '0000000000000000'},
{in: 0b1010101010101010, out: '0101010101010101'},
{in: 0b0011110011000011, out: '1100001100111100'},
{in: 0b0001001000110100, out: '1110110111001011'},
];
expect(() => GateTestUtil.testTruthTable(truthTable, not16))
.not.toThrow();
});
});

@@ -10,2 +10,4 @@ /**

const {int16} = require('../../../util/typed-numbers');
/**

@@ -17,8 +19,8 @@ * Canonical truth table for the `And16` gate.

const TRUTH_TABLE = [
{a: 0b0000000000000000, b: 0b0000000000000000, out: 0b0000000000000000},
{a: 0b0000000000000000, b: 0b1111111111111111, out: 0b0000000000000000},
{a: 0b1111111111111111, b: 0b1111111111111111, out: 0b1111111111111111},
{a: 0b1010101010101010, b: 0b0101010101010101, out: 0b0000000000000000},
{a: 0b0011110011000011, b: 0b0000111111110000, out: 0b0000110011000000},
{a: 0b0001001000110100, b: 0b1001100001110110, out: 0b0001000000110100},
{a: int16(0b0000000000000000), b: int16(0b0000000000000000), out: int16(0b0000000000000000)},
{a: int16(0b0000000000000000), b: int16(0b1111111111111111), out: int16(0b0000000000000000)},
{a: int16(0b1111111111111111), b: int16(0b1111111111111111), out: int16(0b1111111111111111)},
{a: int16(0b1010101010101010), b: int16(0b0101010101010101), out: int16(0b0000000000000000)},
{a: int16(0b0011110011000011), b: int16(0b0000111111110000), out: int16(0b0000110011000000)},
{a: int16(0b0001001000110100), b: int16(0b1001100001110110), out: int16(0b0001000000110100)},
];

@@ -25,0 +27,0 @@

@@ -10,2 +10,4 @@ /**

const {int16} = require('../../../util/typed-numbers');
/**

@@ -15,7 +17,7 @@ * Canonical truth table for the `Not16` gate.

const TRUTH_TABLE = [
{in: 0b0000000000000000, out: ~0b0000000000000000},
{in: 0b1111111111111111, out: ~0b1111111111111111},
{in: 0b1010101010101010, out: ~0b1010101010101010},
{in: 0b0011110011000011, out: ~0b0011110011000011},
{in: 0b0001001000110100, out: ~0b0001001000110100},
{in: int16(0b0000000000000000), out: int16(0b1111111111111111)},
{in: int16(0b1111111111111111), out: int16(0b0000000000000000)},
{in: int16(0b1010101010101010), out: int16(0b0101010101010101)},
{in: int16(0b0011110011000011), out: int16(0b1100001100111100)},
{in: int16(0b0001001000110100), out: int16(0b01110110111001011)},
];

@@ -22,0 +24,0 @@

@@ -10,2 +10,4 @@ /**

const {int16} = require('../../../util/typed-numbers');
/**

@@ -17,8 +19,8 @@ * Canonical truth table for the `Or16` gate.

const TRUTH_TABLE = [
{a: 0b0000000000000000, b: 0b0000000000000000, out: 0b0000000000000000},
{a: 0b0000000000000000, b: 0b1111111111111111, out: 0b1111111111111111},
{a: 0b1111111111111111, b: 0b1111111111111111, out: 0b1111111111111111},
{a: 0b1010101010101010, b: 0b0101010101010101, out: 0b1111111111111111},
{a: 0b0011110011000011, b: 0b0000111111110000, out: 0b0011111111110011},
{a: 0b0001001000110100, b: 0b1001100001110110, out: 0b1001101001110110},
{a: int16(0b0000000000000000), b: int16(0b0000000000000000), out: int16(0b0000000000000000)},
{a: int16(0b0000000000000000), b: int16(0b1111111111111111), out: int16(0b1111111111111111)},
{a: int16(0b1111111111111111), b: int16(0b1111111111111111), out: int16(0b1111111111111111)},
{a: int16(0b1010101010101010), b: int16(0b0101010101010101), out: int16(0b1111111111111111)},
{a: int16(0b0011110011000011), b: int16(0b0000111111110000), out: int16(0b0011111111110011)},
{a: int16(0b0001001000110100), b: int16(0b1001100001110110), out: int16(0b1001101001110110)},
];

@@ -51,3 +53,4 @@

// In JS implemenation doesn't differ from the simple `Or` gate.
this.getOutputPins()[0].setValue(a | b);
// Use 16-bit values with 0xFFFF mask.
this.getOutputPins()[0].setValue((a | b) & 0xFFFF);
}

@@ -54,0 +57,0 @@ }

@@ -13,2 +13,4 @@ /**

const {int16} = require('../../util/typed-numbers');
/**

@@ -132,31 +134,12 @@ * Abstract gate class, base for `BuiltInGate`, and `CompositeGate`.

for (const pinName in this._namesToPinsMap) {
const pin = this.getPin(pinName);
const expectedValue = int16(row[pinName]);
const actualValue = this.getPin(pinName).getValue();
const expectedValue = row[pinName];
const actualValue = pin.getValue();
outputRow[pinName] = actualValue;
//console.log({pinName, expectedValue, actualValue});
// If the (output) pin is provided, validate it.
if (row.hasOwnProperty(pinName)) {
let expectedStrnig;
let actualString;
if (pin.constructor === Pin) {
expectedStrnig = expectedValue.toString();
actualString = actualValue.toString();
} else {
expectedStrnig = typeof expectedValue === 'number'
? (expectedValue >>> 0).toString(2).slice(16)
: expectedValue;
// For PinBus compare bit-strings.
actualString = typeof actualValue === 'number'
? (actualValue >>> 0).toString(2).slice(16)
: actualValue;
}
if (expectedStrnig !== actualString) {
conflictsForRow[pinName] = actualValue;
}
if (row.hasOwnProperty(pinName) && expectedValue !== actualValue) {
conflictsForRow[pinName] = actualValue;
}

@@ -163,0 +146,0 @@ }

@@ -14,2 +14,7 @@ /**

this._name = name;
if (typeof value === 'string') {
value = Number.parseInt(value, 2);
}
this._value = value;

@@ -36,2 +41,5 @@ }

setValue(value) {
if (typeof value === 'string') {
value = Number.parseInt(value, 2);
}
return this._value = value;

@@ -38,0 +46,0 @@ }

@@ -17,4 +17,9 @@ /**

constructor({name, size, value = null}) {
super({name, value});
super({name, value: new Int16Array([value])});
// Call explicitly `setValue` to handle bit-strings.
if (value) {
this.setValue(value);
}
if (!size) {

@@ -35,2 +40,20 @@ throw new TypeError(`PinBus "${name}": "size" argument is required.`);

/**
* Override `setValue` to handle both, bit-strings: '0000000000000000'
* raw binary numbers: 0b0000000000000000.
*/
setValue(value) {
if (typeof value === 'string') {
value = Number.parseInt(value, 2);
}
this._value[0] = value;
}
/**
* Returns value of this pin bus.
*/
getValue() {
return this._value[0];
}
/**
* Updates the value of a particular bit in this bus.

@@ -43,3 +66,3 @@ */

if (value === 1) {
this._value |= (1 << index);
this._value[0] |= (1 << index);
return;

@@ -49,3 +72,3 @@ }

// Set 0 ("clear").
this._value &= ~(1 << index);
this._value[0] &= ~(1 << index);
}

@@ -58,3 +81,3 @@

this._checkIndex(index);
return (this._value >> index) & 1;
return (this._value[0] >> index) & 1;
}

@@ -68,3 +91,3 @@

this._checkIndex(to);
return (this._value >> from) & ((1 << (to + 1 - from)) - 1);
return (this._value[0] >> from) & ((1 << (to + 1 - from)) - 1);
}

@@ -71,0 +94,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc