1
|
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
|
2
|
var B = require('../').Buffer
|
3
|
var test = require('tape')
|
4
|
|
5
|
test('instanceof Buffer', function (t) {
|
6
|
var buf = new B([1, 2])
|
7
|
t.ok(buf instanceof B)
|
8
|
t.end()
|
9
|
})
|
10
|
|
11
|
test('convert to Uint8Array in modern browsers', function (t) {
|
12
|
if (B.TYPED_ARRAY_SUPPORT) {
|
13
|
var buf = new B([1, 2])
|
14
|
var uint8array = new Uint8Array(buf.buffer)
|
15
|
t.ok(uint8array instanceof Uint8Array)
|
16
|
t.equal(uint8array[0], 1)
|
17
|
t.equal(uint8array[1], 2)
|
18
|
} else {
|
19
|
t.pass('object impl: skipping test')
|
20
|
}
|
21
|
t.end()
|
22
|
})
|
23
|
|
24
|
test('indexes from a string', function (t) {
|
25
|
var buf = new B('abc')
|
26
|
t.equal(buf[0], 97)
|
27
|
t.equal(buf[1], 98)
|
28
|
t.equal(buf[2], 99)
|
29
|
t.end()
|
30
|
})
|
31
|
|
32
|
test('indexes from an array', function (t) {
|
33
|
var buf = new B([ 97, 98, 99 ])
|
34
|
t.equal(buf[0], 97)
|
35
|
t.equal(buf[1], 98)
|
36
|
t.equal(buf[2], 99)
|
37
|
t.end()
|
38
|
})
|
39
|
|
40
|
test('setting index value should modify buffer contents', function (t) {
|
41
|
var buf = new B([ 97, 98, 99 ])
|
42
|
t.equal(buf[2], 99)
|
43
|
t.equal(buf.toString(), 'abc')
|
44
|
|
45
|
buf[2] += 10
|
46
|
t.equal(buf[2], 109)
|
47
|
t.equal(buf.toString(), 'abm')
|
48
|
t.end()
|
49
|
})
|
50
|
|
51
|
test('storing negative number should cast to unsigned', function (t) {
|
52
|
var buf = new B(1)
|
53
|
|
54
|
if (B.TYPED_ARRAY_SUPPORT) {
|
55
|
// This does not work with the object implementation -- nothing we can do!
|
56
|
buf[0] = -3
|
57
|
t.equal(buf[0], 253)
|
58
|
}
|
59
|
|
60
|
buf = new B(1)
|
61
|
buf.writeInt8(-3, 0)
|
62
|
t.equal(buf[0], 253)
|
63
|
|
64
|
t.end()
|
65
|
})
|
66
|
|
67
|
test('test that memory is copied from array-like', function (t) {
|
68
|
if (B.TYPED_ARRAY_SUPPORT) {
|
69
|
var u = new Uint8Array(4)
|
70
|
var b = new B(u)
|
71
|
b[0] = 1
|
72
|
b[1] = 2
|
73
|
b[2] = 3
|
74
|
b[3] = 4
|
75
|
|
76
|
t.equal(u[0], 0)
|
77
|
t.equal(u[1], 0)
|
78
|
t.equal(u[2], 0)
|
79
|
t.equal(u[3], 0)
|
80
|
} else {
|
81
|
t.pass('object impl: skipping test')
|
82
|
}
|
83
|
|
84
|
t.end()
|
85
|
})
|