UNPKG

nes-emu

Version:

A NES emulator

76 lines (75 loc) 3.59 kB
"use strict"; var _Byte = _interopRequireDefault(require("./Byte")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const should = require("chai").Should(); describe("helpers", () => { describe("byte", () => { it("can create a number from a signed byte", () => { _Byte.default.toNumber(0b11111011).should.equal(-5); _Byte.default.toNumber(0b00000101).should.equal(5); _Byte.default.toNumber(127).should.equal(127); _Byte.default.toNumber(128).should.equal(-128); }); it("can create a signed byte from a number", () => { _Byte.default.toSignedByte(-5).should.equal(0b11111011); _Byte.default.toSignedByte(5).should.equal(0b00000101); _Byte.default.toSignedByte(127).should.equal(127); _Byte.default.toSignedByte(-128).should.equal(128); }); it("can determine if a value has overflow", () => { _Byte.default.hasOverflow(258).should.equal(true); _Byte.default.hasOverflow(255).should.equal(false); _Byte.default.hasOverflow(25).should.equal(false); _Byte.default.hasOverflow(300).should.equal(true); }); it("can determine if a value is positive or negative", () => { _Byte.default.isPositive(0xfe).should.equal(false); _Byte.default.isNegative(0xfe).should.equal(true); _Byte.default.isPositive(0x10).should.equal(true); _Byte.default.isNegative(0x10).should.equal(false); }); it("can negate numbers using two's complement", () => { const negative112 = _Byte.default.toSignedByte(-112); _Byte.default.negate(112).should.equal(negative112); _Byte.default.negate(negative112).should.equal(112); }); it("can force numbers to 8-bit", () => { _Byte.default.force8Bit(0b110000000).should.equal(0b10000000); _Byte.default.force8Bit(-2).should.equal(254); }); it("can force numbers to 16-bit", () => { _Byte.default.force16Bit(65537).should.equal(1); _Byte.default.force16Bit(-2).should.equal(65534); }); it("can return a bit from a number", () => { _Byte.default.getBit(2, 1).should.equal(1); _Byte.default.getBit(2, 0).should.equal(0); _Byte.default.getBit(0b100101, 0).should.equal(1); _Byte.default.getBit(0b100101, 1).should.equal(0); _Byte.default.getBit(0b100101, 2).should.equal(1); _Byte.default.getBit(0b100101, 3).should.equal(0); _Byte.default.getBit(0b100101, 4).should.equal(0); _Byte.default.getBit(0b100101, 5).should.equal(1); _Byte.default.getBit(0b100101, 6).should.equal(0); }); it("can return a sub-number from a number", () => { _Byte.default.getBits(0b00001110, 1, 3).should.equal(0b111); _Byte.default.getBits(0b00001110, 0, 3).should.equal(0b110); _Byte.default.getBits(0b00001110, 0, 2).should.equal(0b10); _Byte.default.getBits(0b00001110, 2, 2).should.equal(0b011); _Byte.default.getBits(0b00001110, 3, 2).should.equal(0b01); }); it("can insert a sub-number inside a byte", () => { _Byte.default.setBits(0b00001110, 2, 2, 0b10).should.equal(0b00001010); _Byte.default.setBits(0b00001110, 2, 2, 0b01).should.equal(0b00000110); _Byte.default.setBits(0b00001110, 2, 5, 0b10101).should.equal(0b01010110); }); it("can split 16-bit numbers", () => { _Byte.default.highPartOf(0xfe20).should.equal(0xfe); _Byte.default.lowPartOf(0xfe20).should.equal(0x20); }); it("can create 16-bit numbers from two bytes", () => { _Byte.default.to16Bit(0xfe, 0x12).should.equal(0xfe12); }); }); });