@@ -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
if ( ! / I n t / . test ( funx ) ) {
24
20
assert . throws (
@@ -33,6 +29,15 @@ function write(funx, args, result, res) {
33
29
assert . deepStrictEqual ( buf2 , res ) ;
34
30
}
35
31
32
+ function writeInvalidOffset ( offset ) {
33
+ const newArgs = Array . from ( args ) ;
34
+ newArgs [ 1 ] = offset ;
35
+ assert . throws ( ( ) => Buffer . alloc ( 9 ) [ funx ] ( ...newArgs ) , outOfRange ) ;
36
+
37
+ const buf = Buffer . alloc ( 9 ) ;
38
+ buf [ funx ] ( ...newArgs , true ) ;
39
+ assert . deepStrictEqual ( buf , Buffer . alloc ( 9 ) ) ;
40
+ }
36
41
}
37
42
38
43
write ( 'writeInt8' , [ 1 , 0 ] , 1 , Buffer . from ( [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ) ;
@@ -53,3 +58,45 @@ write('writeDoubleBE', [1, 1], 9, Buffer.from([0, 63, 240, 0, 0, 0, 0, 0, 0]));
53
58
write ( 'writeDoubleLE' , [ 1 , 1 ] , 9 , Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 240 , 63 ] ) ) ;
54
59
write ( 'writeFloatBE' , [ 1 , 1 ] , 5 , Buffer . from ( [ 0 , 63 , 128 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ) ;
55
60
write ( 'writeFloatLE' , [ 1 , 1 ] , 5 , Buffer . from ( [ 0 , 0 , 0 , 128 , 63 , 0 , 0 , 0 , 0 ] ) ) ;
61
+
62
+ function writePartial ( funx , args , result , res ) {
63
+ assert . throws ( ( ) => Buffer . alloc ( 9 ) [ funx ] ( ...args ) , outOfRange ) ;
64
+ const buf = Buffer . alloc ( 9 ) ;
65
+ assert . strictEqual ( buf [ funx ] ( ...args , true ) , result ) ;
66
+ assert . deepStrictEqual ( buf , res ) ;
67
+ }
68
+
69
+ // Test partial writes (cases where the buffer isn't large enough to hold the
70
+ // entire value, but is large enough to hold parts of it).
71
+ writePartial ( 'writeIntBE' , [ 0x0eadbeef , 6 , 4 ] , 10 ,
72
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0x0e , 0xad , 0xbe ] ) ) ;
73
+ writePartial ( 'writeIntLE' , [ 0x0eadbeef , 6 , 4 ] , 10 ,
74
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0xef , 0xbe , 0xad ] ) ) ;
75
+ writePartial ( 'writeInt16BE' , [ 0x1234 , 8 ] , 10 ,
76
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0x12 ] ) ) ;
77
+ writePartial ( 'writeInt16LE' , [ 0x1234 , 8 ] , 10 ,
78
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0x34 ] ) ) ;
79
+ writePartial ( 'writeInt32BE' , [ 0x0eadbeef , 6 ] , 10 ,
80
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0x0e , 0xad , 0xbe ] ) ) ;
81
+ writePartial ( 'writeInt32LE' , [ 0x0eadbeef , 6 ] , 10 ,
82
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0xef , 0xbe , 0xad ] ) ) ;
83
+ writePartial ( 'writeUIntBE' , [ 0xdeadbeef , 6 , 4 ] , 10 ,
84
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0xde , 0xad , 0xbe ] ) ) ;
85
+ writePartial ( 'writeUIntLE' , [ 0xdeadbeef , 6 , 4 ] , 10 ,
86
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0xef , 0xbe , 0xad ] ) ) ;
87
+ writePartial ( 'writeUInt16BE' , [ 0x1234 , 8 ] , 10 ,
88
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0x12 ] ) ) ;
89
+ writePartial ( 'writeUInt16LE' , [ 0x1234 , 8 ] , 10 ,
90
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0x34 ] ) ) ;
91
+ writePartial ( 'writeUInt32BE' , [ 0xdeadbeef , 6 ] , 10 ,
92
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0xde , 0xad , 0xbe ] ) ) ;
93
+ writePartial ( 'writeUInt32LE' , [ 0xdeadbeef , 6 ] , 10 ,
94
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0xef , 0xbe , 0xad ] ) ) ;
95
+ writePartial ( 'writeDoubleBE' , [ 1 , 2 ] , 10 ,
96
+ Buffer . from ( [ 0 , 0 , 63 , 240 , 0 , 0 , 0 , 0 , 0 ] ) ) ;
97
+ writePartial ( 'writeDoubleLE' , [ 1 , 2 ] , 10 ,
98
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 240 ] ) ) ;
99
+ writePartial ( 'writeFloatBE' , [ 1 , 6 ] , 10 ,
100
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 63 , 128 , 0 ] ) ) ;
101
+ writePartial ( 'writeFloatLE' , [ 1 , 6 ] , 10 ,
102
+ Buffer . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 128 ] ) ) ;
0 commit comments