FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/test/parallel/test-buffer-arraybuffer.js at master · DynamicAnt/node · GitHub
DynamicAnt
/
node
Public
forked from
nodejs/node
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
node
/
test
/
parallel
/
test-buffer-arraybuffer.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
150 lines (127 loc) · 4.14 KB
Breadcrumbs
node
/
test
/
parallel
/
test-buffer-arraybuffer.js
Copy path
File metadata and controls
150 lines (127 loc) · 4.14 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict'
;
const
common
=
require
(
'../common'
)
;
const
assert
=
require
(
'assert'
)
;
const
LENGTH
=
16
;
const
ab
=
new
ArrayBuffer
(
LENGTH
)
;
const
dv
=
new
DataView
(
ab
)
;
const
ui
=
new
Uint8Array
(
ab
)
;
const
buf
=
Buffer
.
from
(
ab
)
;
assert
.
ok
(
buf
instanceof
Buffer
)
;
assert
.
strictEqual
(
buf
.
parent
,
buf
.
buffer
)
;
assert
.
strictEqual
(
buf
.
buffer
,
ab
)
;
assert
.
strictEqual
(
buf
.
length
,
ab
.
byteLength
)
;
buf
.
fill
(
0xC
)
;
for
(
let
i
=
0
;
i
<
LENGTH
;
i
++
)
{
assert
.
strictEqual
(
ui
[
i
]
,
0xC
)
;
ui
[
i
]
=
0xF
;
assert
.
strictEqual
(
buf
[
i
]
,
0xF
)
;
}
buf
.
writeUInt32LE
(
0xF00
,
0
)
;
buf
.
writeUInt32BE
(
0xB47
,
4
)
;
buf
.
writeDoubleLE
(
3.1415
,
8
)
;
assert
.
strictEqual
(
dv
.
getUint32
(
0
,
true
)
,
0xF00
)
;
assert
.
strictEqual
(
dv
.
getUint32
(
4
)
,
0xB47
)
;
assert
.
strictEqual
(
dv
.
getFloat64
(
8
,
true
)
,
3.1415
)
;
// Now test protecting users from doing stupid things
assert
.
throws
(
function
(
)
{
function
AB
(
)
{
}
Object
.
setPrototypeOf
(
AB
,
ArrayBuffer
)
;
Object
.
setPrototypeOf
(
AB
.
prototype
,
ArrayBuffer
.
prototype
)
;
Buffer
.
from
(
new
AB
(
)
)
;
}
,
TypeError
)
;
// write{Double,Float}{LE,BE} with noAssert should not crash, cf. #3766
const
b
=
Buffer
.
allocUnsafe
(
1
)
;
b
.
writeFloatLE
(
11.11
,
0
,
true
)
;
b
.
writeFloatBE
(
11.11
,
0
,
true
)
;
b
.
writeDoubleLE
(
11.11
,
0
,
true
)
;
b
.
writeDoubleBE
(
11.11
,
0
,
true
)
;
// Test the byteOffset and length arguments
{
const
ab
=
new
Uint8Array
(
5
)
;
ab
[
0
]
=
1
;
ab
[
1
]
=
2
;
ab
[
2
]
=
3
;
ab
[
3
]
=
4
;
ab
[
4
]
=
5
;
const
buf
=
Buffer
.
from
(
ab
.
buffer
,
1
,
3
)
;
assert
.
strictEqual
(
buf
.
length
,
3
)
;
assert
.
strictEqual
(
buf
[
0
]
,
2
)
;
assert
.
strictEqual
(
buf
[
1
]
,
3
)
;
assert
.
strictEqual
(
buf
[
2
]
,
4
)
;
buf
[
0
]
=
9
;
assert
.
strictEqual
(
ab
[
1
]
,
9
)
;
assert
.
throws
(
(
)
=>
Buffer
.
from
(
ab
.
buffer
,
6
)
,
common
.
expectsError
(
{
code
:
'ERR_BUFFER_OUT_OF_BOUNDS'
,
type
:
RangeError
,
message
:
'"offset" is outside of buffer bounds'
}
)
)
;
assert
.
throws
(
(
)
=>
Buffer
.
from
(
ab
.
buffer
,
3
,
6
)
,
common
.
expectsError
(
{
code
:
'ERR_BUFFER_OUT_OF_BOUNDS'
,
type
:
RangeError
,
message
:
'"length" is outside of buffer bounds'
}
)
)
;
}
// Test the deprecated Buffer() version also
{
const
ab
=
new
Uint8Array
(
5
)
;
ab
[
0
]
=
1
;
ab
[
1
]
=
2
;
ab
[
2
]
=
3
;
ab
[
3
]
=
4
;
ab
[
4
]
=
5
;
const
buf
=
Buffer
(
ab
.
buffer
,
1
,
3
)
;
assert
.
strictEqual
(
buf
.
length
,
3
)
;
assert
.
strictEqual
(
buf
[
0
]
,
2
)
;
assert
.
strictEqual
(
buf
[
1
]
,
3
)
;
assert
.
strictEqual
(
buf
[
2
]
,
4
)
;
buf
[
0
]
=
9
;
assert
.
strictEqual
(
ab
[
1
]
,
9
)
;
assert
.
throws
(
(
)
=>
Buffer
(
ab
.
buffer
,
6
)
,
common
.
expectsError
(
{
code
:
'ERR_BUFFER_OUT_OF_BOUNDS'
,
type
:
RangeError
,
message
:
'"offset" is outside of buffer bounds'
}
)
)
;
assert
.
throws
(
(
)
=>
Buffer
(
ab
.
buffer
,
3
,
6
)
,
common
.
expectsError
(
{
code
:
'ERR_BUFFER_OUT_OF_BOUNDS'
,
type
:
RangeError
,
message
:
'"length" is outside of buffer bounds'
}
)
)
;
}
{
// If byteOffset is not numeric, it defaults to 0.
const
ab
=
new
ArrayBuffer
(
10
)
;
const
expected
=
Buffer
.
from
(
ab
,
0
)
;
assert
.
deepStrictEqual
(
Buffer
.
from
(
ab
,
'fhqwhgads'
)
,
expected
)
;
assert
.
deepStrictEqual
(
Buffer
.
from
(
ab
,
NaN
)
,
expected
)
;
assert
.
deepStrictEqual
(
Buffer
.
from
(
ab
,
{
}
)
,
expected
)
;
assert
.
deepStrictEqual
(
Buffer
.
from
(
ab
,
[
]
)
,
expected
)
;
// If byteOffset can be converted to a number, it will be.
assert
.
deepStrictEqual
(
Buffer
.
from
(
ab
,
[
1
]
)
,
Buffer
.
from
(
ab
,
1
)
)
;
// If byteOffset is Infinity, throw.
assert
.
throws
(
(
)
=>
{
Buffer
.
from
(
ab
,
Infinity
)
;
}
,
common
.
expectsError
(
{
code
:
'ERR_BUFFER_OUT_OF_BOUNDS'
,
type
:
RangeError
,
message
:
'"offset" is outside of buffer bounds'
}
)
)
;
}
{
// If length is not numeric, it defaults to 0.
const
ab
=
new
ArrayBuffer
(
10
)
;
const
expected
=
Buffer
.
from
(
ab
,
0
,
0
)
;
assert
.
deepStrictEqual
(
Buffer
.
from
(
ab
,
0
,
'fhqwhgads'
)
,
expected
)
;
assert
.
deepStrictEqual
(
Buffer
.
from
(
ab
,
0
,
NaN
)
,
expected
)
;
assert
.
deepStrictEqual
(
Buffer
.
from
(
ab
,
0
,
{
}
)
,
expected
)
;
assert
.
deepStrictEqual
(
Buffer
.
from
(
ab
,
0
,
[
]
)
,
expected
)
;
// If length can be converted to a number, it will be.
assert
.
deepStrictEqual
(
Buffer
.
from
(
ab
,
0
,
[
1
]
)
,
Buffer
.
from
(
ab
,
0
,
1
)
)
;
//If length is Infinity, throw.
assert
.
throws
(
(
)
=>
{
Buffer
.
from
(
ab
,
0
,
Infinity
)
;
}
,
common
.
expectsError
(
{
code
:
'ERR_BUFFER_OUT_OF_BOUNDS'
,
type
:
RangeError
,
message
:
'"length" is outside of buffer bounds'
}
)
)
;
}
Back
|
FazBrowse Home
|
New Git URL