Blame view

node_modules/msgpack5/test/16-bits-signed-integers.js 1.37 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  'use strict'
  
  var test = require('tape').test
  var msgpack = require('../')
  var bl = require('bl')
  
  test('encoding/decoding 16-bits big-endian signed integers', function (t) {
    var encoder = msgpack()
    var allNum = []
    var i
  
    for (i = 129; i < 32768; i += 1423) {
      allNum.push(-i)
    }
  
    allNum.push(-32768)
  
    allNum.forEach(function (num) {
      t.test('encoding ' + num, function (t) {
        var buf = encoder.encode(num)
        t.equal(buf.length, 3, 'must have 3 bytes')
        t.equal(buf[0], 0xd1, 'must have the proper header')
        t.equal(buf.readInt16BE(1), num, 'must decode correctly')
        t.end()
      })
  
      t.test('decoding ' + num, function (t) {
        var buf = new Buffer(3)
        buf[0] = 0xd1
        buf.writeInt16BE(num, 1)
        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 16-bits big-endian integer', function (t) {
    var encoder = msgpack()
    var buf = new Buffer(2)
    buf[0] = 0xd1
    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()
  })