Skip to content

Commit

Permalink
Add compiled tests, fix bitfields
Browse files Browse the repository at this point in the history
  • Loading branch information
Karang committed May 19, 2020
1 parent 2b365d2 commit d390ba0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class Compiler {
addNativeType (type, fn) {
this.primitiveTypes[type] = `native.${type}`
this.native[type] = fn
this.types[type] = 'native'
}

/**
Expand Down Expand Up @@ -419,6 +420,7 @@ class SizeOfCompiler extends Compiler {
} else {
this.native[type] = fn
}
this.types[type] = 'native'
}

compileType (type) {
Expand Down
13 changes: 7 additions & 6 deletions src/datatypes/compiler-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ module.exports = {
}],
'bitfield': ['parametrizable', (compiler, values) => {
let code = ''
const totalBits = values.reduce((acc, { size }) => acc + size, 0)
if (totalBits % 8 !== 0) throw new Error('Bitfield: The sum of the sizes must be a multiple of 8')
const totalBytes = totalBits / 8
const totalBytes = Math.ceil(values.reduce((acc, { size }) => acc + size, 0) / 8)
code += `if ( offset + ${totalBytes} > buffer.length) { throw new PartialReadError() }\n`

let names = []
Expand Down Expand Up @@ -112,6 +110,11 @@ module.exports = {
}
}
}
if (bits !== 0) {
code += 'buffer[offset++] = ' + toWrite + ' << ' + (8 - bits) + '\n'
bits = 0
toWrite = ''
}
code += 'return offset'
return compiler.wrapCode(code)
}],
Expand Down Expand Up @@ -144,9 +147,7 @@ module.exports = {
return compiler.wrapCode(code)
}],
'bitfield': ['parametrizable', (compiler, values) => {
const totalBits = values.reduce((acc, { size }) => acc + size, 0)
if (totalBits % 8 !== 0) throw new Error('Bitfield: The sum of the sizes must be a multiple of 8')
const totalBytes = totalBits / 8
const totalBytes = Math.ceil(values.reduce((acc, { size }) => acc + size, 0) / 8)
return `${totalBytes}`
}],
'mapper': ['parametrizable', (compiler, mapper) => {
Expand Down
21 changes: 19 additions & 2 deletions test/dataTypes/datatypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const Validator = require('jsonschema').Validator
const v = new Validator()
const assert = require('assert')

const testData = require('./prepareTests').testData
const proto = require('./prepareTests').proto
const { testData, proto, compiledProto } = require('./prepareTests')

function testValue (type, value, buffer) {
it('writes', function () {
Expand All @@ -17,6 +16,14 @@ function testValue (type, value, buffer) {
if (value === null) { assert.ok(actualResult.data === undefined) } else { expect(actualResult.data).to.deep.equal(value) }
expect(actualResult.metadata.size).to.deep.equal(buffer.length)
})
it('writes (compiled)', function () {
expect(compiledProto.createPacketBuffer(type, value)).to.deep.equal(buffer)
})
it('reads (compiled)', function () {
const actualResult = compiledProto.parsePacketBuffer(type, buffer)
if (value === null) { assert.ok(actualResult.data === undefined) } else { expect(actualResult.data).to.deep.equal(value) }
expect(actualResult.metadata.size).to.deep.equal(buffer.length)
})
}

function testType (type, values) {
Expand All @@ -42,6 +49,16 @@ function testType (type, values) {
}
throw Error('no PartialReadError thrown')
})

it('reads 0 bytes and throw a PartialReadError (compiled)', () => {
try {
compiledProto.parsePacketBuffer(type, Buffer.alloc(0))
} catch (e) {
if (!e.partialReadError) { throw e }
return
}
throw Error('no PartialReadError thrown')
})
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/dataTypes/prepareTests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const ProtoDef = require('protodef').ProtoDef
const { ProtoDefCompiler } = require('protodef').Compiler

const proto = new ProtoDef()
const compiler = new ProtoDefCompiler()

const testData = [
{
Expand Down Expand Up @@ -41,6 +43,9 @@ testData.forEach(tests => {
test.subtypes.forEach((subtype, i) => {
const type = test.type + '_' + i
proto.addType(type, subtype.type)
let types = {}
types[type] = subtype.type
compiler.addTypesToCompile(types)

subtype.values = transformValues(test.type, subtype.values)
subtype.type = type
Expand All @@ -54,5 +59,6 @@ testData.forEach(tests => {
})
})

module.exports.compiledProto = compiler.compileProtoDefSync()
module.exports.testData = testData
module.exports.proto = proto

0 comments on commit d390ba0

Please sign in to comment.