UNPKG

meoser

Version:

meos protocol engine

55 lines (52 loc) 1.85 kB
import { calcFrameCheckSum, calcFileCheckSum, stringToAsciiCode, intArrayToHexArray } from '../src/util'; import chai from 'chai'; const expect = chai.expect; describe('test checkFrameSum', function() { it('should get correct frame check sum', function() { var data = [0x01, 0x02, 0x03, 0x04]; let sum = calcFrameCheckSum(data); expect(sum).to.equal(10); }); it('should get correct ascii code from content', function() { var content = "makeblock_rgbled().set_color(10,10,10)"; let content_bytes = stringToAsciiCode(content); let result = "6d 61 6b 65 62 6c 6f 63 6b 5f 72 67 62 6c 65 64 28 29 2e 73 65 74 5f 63 6f 6c 6f 72 28 31 30 2c 31 30 2c 31 30 29"; let cal_result = intArrayToHexArray(content_bytes).join(" "); expect(cal_result).to.equal(result); }); }); const checkFileSumCase = [{ desc: 'should get correct file check sum: 1bytes', data: [0x01], expect: 0x01000000 }, { desc: 'should get correct file check sum: 4bytes', data: [0x01, 0x02, 0x03, 0x04], expect: 0x01020304 }, { desc: 'should get correct file check sum: 5bytes', data: [0x01, 0x02, 0x03, 0x04, 0x01], expect: 0x00020304 }, { desc: 'should get correct file check sum: 8bytes', data: [0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04], expect: 0x00000000 }, { desc: 'should get correct file check sum: 9bytes', data: [0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0xfa], expect: 0xfa000000 }] describe('test checkFileSum', function() { for (let case_ of checkFileSumCase) { it(case_.desc, function() { let sum = calcFileCheckSum(case_.data); let rchecksum = parseInt(intArrayToHexArray(sum).join(""), 16); expect(rchecksum).to.equal(case_.expect); }); } });