Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
1
var B = require('../').Buffer
2
var test = require('tape')
3
if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false
4
5
test('convert to Uint8Array in modern browsers', function (t) {
6
if (B.TYPED_ARRAY_SUPPORT) {
7
var buf = new B([1, 2])
8
var uint8array = new Uint8Array(buf.buffer)
9
t.ok(uint8array instanceof Uint8Array)
10
t.equal(uint8array[0], 1)
11
t.equal(uint8array[1], 2)
12
} else {
13
t.pass('object impl: skipping test')
14
}
15
t.end()
16
})
17
18
test('indexes from a string', function (t) {
19
var buf = new B('abc')
20
t.equal(buf[0], 97)
21
t.equal(buf[1], 98)
22
t.equal(buf[2], 99)
23
t.end()
24
})
25
26
test('indexes from an array', function (t) {
27
var buf = new B([ 97, 98, 99 ])
28
t.equal(buf[0], 97)
29
t.equal(buf[1], 98)
30
t.equal(buf[2], 99)
31
t.end()
32
})
33
34
test('setting index value should modify buffer contents', function (t) {
35
var buf = new B([ 97, 98, 99 ])
36
t.equal(buf[2], 99)
37
t.equal(buf.toString(), 'abc')
38
39
buf[2] += 10
40
t.equal(buf[2], 109)
41
t.equal(buf.toString(), 'abm')
42
t.end()
43
})
44
45
test('storing negative number should cast to unsigned', function (t) {
46
var buf = new B(1)
47
48
if (B.TYPED_ARRAY_SUPPORT) {
49
// This does not work with the object implementation -- nothing we can do!
50
buf[0] = -3
51
t.equal(buf[0], 253)
52
}
53
54
buf = new B(1)
55
buf.writeInt8(-3, 0)
56
t.equal(buf[0], 253)
57
58
t.end()
59
})
60
61
// TODO: test write negative with
62
63