@@ -4,21 +4,17 @@ const assert = require('assert');
4
4
5
5
// testing buffer write functions
6
6
7
+ const outOfRange = / ^ R a n g e E r r o r : (?: I n d e x ) ? o u t o f r a n g e (?: i n d e x ) ? $ / ;
8
+
7
9
function write ( funx , args , result , res ) {
8
10
{
9
11
const buf = Buffer . alloc ( 9 ) ;
10
12
assert . strictEqual ( buf [ funx ] ( ...args ) , result ) ;
11
13
assert . deepStrictEqual ( buf , res ) ;
12
14
}
13
15
14
- {
15
- const invalidArgs = Array . from ( args ) ;
16
- invalidArgs [ 1 ] = - 1 ;
17
- assert . throws (
18
- ( ) => Buffer . alloc ( 9 ) [ funx ] ( ...invalidArgs ) ,
19
- / ^ R a n g e E r r o r : (?: I n d e x ) ? o u t o f r a n g e (?: i n d e x ) ? $ /
20
- ) ;
21
- }
16
+ writeInvalidOffset ( - 1 ) ;
17
+ writeInvalidOffset ( 9 ) ;
22
18
23
19
{
24
20
const error = / I n t / . test ( funx ) ?
@@ -37,6 +33,15 @@ function write(funx, args, result, res) {
37
33
assert . deepStrictEqual ( buf2 , res ) ;
38
34
}
39
35
36
+ function writeInvalidOffset ( offset ) {
37
+ const newArgs = Array . from ( args ) ;
38
+ newArgs [ 1 ] = offset ;
39
+ assert . throws ( ( ) => Buffer . alloc ( 9 ) [ funx ] ( ...newArgs ) , outOfRange ) ;
40
+
41
+ const buf = Buffer . alloc ( 9 ) ;
42
+ buf [ funx ] ( ...newArgs , true ) ;
43
+ assert . deepStrictEqual ( buf , Buffer . alloc ( 9 ) ) ;
44
+ }
40
45
}
41
46
42
47
write ( 'writeInt8' , [ 1 , 0 ] , 1 , Buffer . from ( [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ) ;
@@ -57,3 +62,45 @@ write('writeDoubleBE', [1, 1], 9, Buffer.from([0, 63, 240, 0, 0, 0, 0, 0, 0]));
57
62
write ( 'writeDoubleLE' , [ 1 , 1 ] , 9 , Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 240 , 63 ] ) ) ;
58
63
write ( 'writeFloatBE' , [ 1 , 1 ] , 5 , Buffer . from ( [ 0 , 63 , 128 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ) ;
59
64
write ( 'writeFloatLE' , [ 1 , 1 ] , 5 , Buffer . from ( [ 0 , 0 , 0 , 128 , 63 , 0 , 0 , 0 , 0 ] ) ) ;
65
+
66
+ function writePartial ( funx , args , result , res ) {
67
+ assert . throws ( ( ) => Buffer . alloc ( 9 ) [ funx ] ( ...args ) , outOfRange ) ;
68
+ const buf = Buffer . alloc ( 9 ) ;
69
+ assert . strictEqual ( buf [ funx ] ( ...args , true ) , result ) ;
70
+ assert . deepStrictEqual ( buf , res ) ;
71
+ }
72
+
73
+ // Test partial writes (cases where the buffer isn't large enough to hold the
74
+ // entire value, but is large enough to hold parts of it).
75
+ writePartial ( 'writeIntBE' , [ 0x0eadbeef , 6 , 4 ] , 10 ,
76
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0x0e , 0xad , 0xbe ] ) ) ;
77
+ writePartial ( 'writeIntLE' , [ 0x0eadbeef , 6 , 4 ] , 10 ,
78
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0xef , 0xbe , 0xad ] ) ) ;
79
+ writePartial ( 'writeInt16BE' , [ 0x1234 , 8 ] , 10 ,
80
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0x12 ] ) ) ;
81
+ writePartial ( 'writeInt16LE' , [ 0x1234 , 8 ] , 10 ,
82
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0x34 ] ) ) ;
83
+ writePartial ( 'writeInt32BE' , [ 0x0eadbeef , 6 ] , 10 ,
84
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0x0e , 0xad , 0xbe ] ) ) ;
85
+ writePartial ( 'writeInt32LE' , [ 0x0eadbeef , 6 ] , 10 ,
86
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0xef , 0xbe , 0xad ] ) ) ;
87
+ writePartial ( 'writeUIntBE' , [ 0xdeadbeef , 6 , 4 ] , 10 ,
88
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0xde , 0xad , 0xbe ] ) ) ;
89
+ writePartial ( 'writeUIntLE' , [ 0xdeadbeef , 6 , 4 ] , 10 ,
90
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0xef , 0xbe , 0xad ] ) ) ;
91
+ writePartial ( 'writeUInt16BE' , [ 0x1234 , 8 ] , 10 ,
92
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0x12 ] ) ) ;
93
+ writePartial ( 'writeUInt16LE' , [ 0x1234 , 8 ] , 10 ,
94
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0x34 ] ) ) ;
95
+ writePartial ( 'writeUInt32BE' , [ 0xdeadbeef , 6 ] , 10 ,
96
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0xde , 0xad , 0xbe ] ) ) ;
97
+ writePartial ( 'writeUInt32LE' , [ 0xdeadbeef , 6 ] , 10 ,
98
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0xef , 0xbe , 0xad ] ) ) ;
99
+ writePartial ( 'writeDoubleBE' , [ 1 , 2 ] , 10 ,
100
+ Buffer . from ( [ 0 , 0 , 63 , 240 , 0 , 0 , 0 , 0 , 0 ] ) ) ;
101
+ writePartial ( 'writeDoubleLE' , [ 1 , 2 ] , 10 ,
102
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 240 ] ) ) ;
103
+ writePartial ( 'writeFloatBE' , [ 1 , 6 ] , 10 ,
104
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 63 , 128 , 0 ] ) ) ;
105
+ writePartial ( 'writeFloatLE' , [ 1 , 6 ] , 10 ,
106
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 128 ] ) ) ;
0 commit comments