Browse Source

More work on utils/bcd and added unit tests.

master
Bryan Miller 5 years ago
parent
commit
d443ff4c3a
2 changed files with 48 additions and 5 deletions
  1. +13
    -5
      src/utils/bcd.js
  2. +35
    -0
      test/unit.src.utils.bcd.spec.js

+ 13
- 5
src/utils/bcd.js View File

@@ -9,16 +9,15 @@ function isValid(bcd, n){
return true;
}

function dec2BCD(b, n){
function int2BCD(b, n){
;
}

function BCD2Dec(bcd, n){
// TODO: Fix bug!
function BCD2Int(bcd, n){
let v = 0
for (let i=0; i < n; i++){
bcd = bcd >> (i*4);
v += (bcd & 0x0F) * Math.pow(10,i);
let d = bcd >> (i*4);
v += (d & 0x0F) * Math.pow(10,i);
}
return v;
}
@@ -37,3 +36,12 @@ function add(a, b, n){
return v;
}


var BCD = Object.freeze({
isValid: isValid,
int2BCD: int2BCD,
BCD2Int: BCD2Int,
add: add
});

module.exports = BCD;

+ 35
- 0
test/unit.src.utils.bcd.spec.js View File

@@ -0,0 +1,35 @@
const expect = require('chai').expect;
const BCD = require('../src/utils/bcd.js');



describe("BCD Utils Tests...", function(){
it(".isValid()", function(){
expect(BCD.isValid(0x01, 2)).to.be.true;
expect(BCD.isValid(0x78, 2)).to.be.true;
expect(BCD.isValid(0xF4, 2)).to.be.false;
expect(BCD.isValid(0xF4, 1)).to.be.true;
});

it(".int2BCD()");

it(".BCD2Int()", function(){
expect(BCD.BCD2Int(0x10, 2)).to.equal(10);
expect(BCD.BCD2Int(0x15, 1)).to.equal(5);
expect(BCD.BCD2Int(0x144, 3)).to.equal(144);
expect(BCD.BCD2Int(0x144, 2)).to.equal(44);
expect(BCD.BCD2Int(0x12, 3)).to.equal(12);
});

it(".add()", function(){
expect(BCD.add(0x05, 0x11, 2)).to.equal(0x16);
expect(BCD.add(0x5, 0x11, 2)).to.equal(0x16);
expect(BCD.add(0x111, 0x12, 3)).to.equal(0x123);
expect(BCD.add(0x90, 0x11, 3)).to.equal(0x101);
expect(BCD.add(0x90, 0x11, 2)).to.equal(0x01);
});
});





Loading…
Cancel
Save