You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.3 KiB
51 lines
1.3 KiB
'use strict' |
|
|
|
var Buffer = require('safe-buffer').Buffer |
|
var test = require('tape').test |
|
var msgpack = require('../') |
|
var bl = require('bl') |
|
|
|
test('encoding/decoding 8-bits integers', function (t) { |
|
var encoder = msgpack() |
|
var allNum = [] |
|
|
|
for (var i = 128; i < 256; i++) { |
|
allNum.push(i) |
|
} |
|
|
|
allNum.forEach(function (num) { |
|
t.test('encoding ' + num, function (t) { |
|
var buf = encoder.encode(num) |
|
t.equal(buf.length, 2, 'must have 2 bytes') |
|
t.equal(buf[0], 0xcc, 'must have the proper header') |
|
t.equal(buf[1], num, 'must decode correctly') |
|
t.end() |
|
}) |
|
|
|
t.test('decoding ' + num, function (t) { |
|
var buf = Buffer.from([0xcc, num]) |
|
t.equal(encoder.decode(buf), num, 'must decode correctly') |
|
t.end() |
|
}) |
|
|
|
t.test('mirror test ' + num, function (t) { |
|
t.equal(encoder.decode(encoder.encode(num)), num, 'must stay the same') |
|
t.end() |
|
}) |
|
}) |
|
|
|
t.end() |
|
}) |
|
|
|
test('decoding an incomplete 8-bits unsigned integer', function (t) { |
|
var encoder = msgpack() |
|
var buf = Buffer.allocUnsafe(1) |
|
buf[0] = 0xcc |
|
buf = bl().append(buf) |
|
var origLength = buf.length |
|
t.throws(function () { |
|
encoder.decode(buf) |
|
}, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') |
|
t.equals(buf.length, origLength, 'must not consume any byte') |
|
t.end() |
|
})
|
|
|