Projekt

Obecné

Profil

Stáhnout (3.4 KB) Statistiky
| Větev: | Revize:
1
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
2
var B = require('../').Buffer
3
var test = require('tape')
4
var isnan = require('is-nan')
5

    
6
test('buffer.write string should get parsed as number', function (t) {
7
  var b = new B(64)
8
  b.writeUInt16LE('1003', 0)
9
  t.equal(b.readUInt16LE(0), 1003)
10
  t.end()
11
})
12

    
13
test('buffer.writeUInt8 a fractional number will get Math.floored', function (t) {
14
  // Some extra work is necessary to make this test pass with the Object implementation
15

    
16
  var b = new B(1)
17
  b.writeInt8(5.5, 0)
18
  t.equal(b[0], 5)
19
  t.end()
20
})
21

    
22
test('writeUint8 with a negative number throws', function (t) {
23
  var buf = new B(1)
24

    
25
  t.throws(function () {
26
    buf.writeUInt8(-3, 0)
27
  })
28

    
29
  t.end()
30
})
31

    
32
test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) {
33
  t.plan(2 * (2 * 2 * 2 + 2))
34
  var hex = [
35
    '03', '0300', '0003', '03000000', '00000003',
36
    'fd', 'fdff', 'fffd', 'fdffffff', 'fffffffd'
37
  ]
38
  var reads = [ 3, 3, 3, 3, 3, -3, -3, -3, -3, -3 ]
39
  var xs = ['UInt', 'Int']
40
  var ys = [8, 16, 32]
41
  for (var i = 0; i < xs.length; i++) {
42
    var x = xs[i]
43
    for (var j = 0; j < ys.length; j++) {
44
      var y = ys[j]
45
      var endianesses = (y === 8) ? [''] : ['LE', 'BE']
46
      for (var k = 0; k < endianesses.length; k++) {
47
        var z = endianesses[k]
48

    
49
        var v1 = new B(y / 8)
50
        var writefn = 'write' + x + y + z
51
        var val = (x === 'Int') ? -3 : 3
52
        v1[writefn](val, 0)
53
        t.equal(
54
          v1.toString('hex'),
55
          hex.shift()
56
        )
57
        var readfn = 'read' + x + y + z
58
        t.equal(
59
          v1[readfn](0),
60
          reads.shift()
61
        )
62
      }
63
    }
64
  }
65
  t.end()
66
})
67

    
68
test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) {
69
  if (!B.TYPED_ARRAY_SUPPORT) {
70
    t.pass('object impl: skipping overflow test')
71
    t.end()
72
    return
73
  }
74

    
75
  t.plan(3 * (2 * 2 * 2 + 2))
76
  var hex = [
77
    '', '03', '00', '030000', '000000',
78
    '', 'fd', 'ff', 'fdffff', 'ffffff'
79
  ]
80
  var reads = [
81
    undefined, 3, 0, NaN, 0,
82
    undefined, 253, -256, 16777213, -256
83
  ]
84
  var xs = ['UInt', 'Int']
85
  var ys = [8, 16, 32]
86
  for (var i = 0; i < xs.length; i++) {
87
    var x = xs[i]
88
    for (var j = 0; j < ys.length; j++) {
89
      var y = ys[j]
90
      var endianesses = (y === 8) ? [''] : ['LE', 'BE']
91
      for (var k = 0; k < endianesses.length; k++) {
92
        var z = endianesses[k]
93

    
94
        var v1 = new B(y / 8 - 1)
95
        var next = new B(4)
96
        next.writeUInt32BE(0, 0)
97
        var writefn = 'write' + x + y + z
98
        var val = (x === 'Int') ? -3 : 3
99
        v1[writefn](val, 0, true)
100
        t.equal(
101
          v1.toString('hex'),
102
          hex.shift()
103
        )
104
        // check that nothing leaked to next buffer.
105
        t.equal(next.readUInt32BE(0), 0)
106
        // check that no bytes are read from next buffer.
107
        next.writeInt32BE(~0, 0)
108
        var readfn = 'read' + x + y + z
109
        var r = reads.shift()
110
        if (isnan(r)) t.pass('equal')
111
        else t.equal(v1[readfn](0, true), r)
112
      }
113
    }
114
  }
115
  t.end()
116
})
117
test('large values do not improperly roll over (ref #80)', function (t) {
118
  var nums = [-25589992, -633756690, -898146932]
119
  var out = new B(12)
120
  out.fill(0)
121
  out.writeInt32BE(nums[0], 0)
122
  var newNum = out.readInt32BE(0)
123
  t.equal(nums[0], newNum)
124
  out.writeInt32BE(nums[1], 4)
125
  newNum = out.readInt32BE(4)
126
  t.equal(nums[1], newNum)
127
  out.writeInt32BE(nums[2], 8)
128
  newNum = out.readInt32BE(8)
129
  t.equal(nums[2], newNum)
130
  t.end()
131
})
(12-12/13)