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