@@ -11,6 +11,105 @@ import { strictEqual } from "node:assert";
1111
1212const { MAX_STRING_LENGTH } = constants ;
1313
14+ Deno . test ( "[node/buffer] UTF-16LE encoding preserves every code unit" , ( ) => {
15+ const aliases = [ "utf16le" , "utf-16le" , "ucs2" , "ucs-2" ] as const ;
16+ for ( let start = 0 ; start < 65536 ; start += 1024 ) {
17+ const units = Array . from ( { length : 1024 } , ( _ , i ) => start + i ) ;
18+ const text = String . fromCharCode ( ...units ) ;
19+ const expected = Buffer . alloc ( units . length * 2 ) ;
20+ for ( let i = 0 ; i < units . length ; i ++ ) {
21+ expected [ i * 2 ] = units [ i ] & 255 ;
22+ expected [ i * 2 + 1 ] = units [ i ] >>> 8 ;
23+ }
24+ for ( const encoding of aliases ) {
25+ assertEquals ( Buffer . from ( text , encoding ) , expected ) ;
26+ }
27+ }
28+ } ) ;
29+
30+ Deno . test ( "[node/buffer] UTF-16LE writes respect byte limits and slice boundaries" , ( ) => {
31+ for (
32+ const text of [
33+ "" ,
34+ "ASCII" ,
35+ "café" ,
36+ "漢字" ,
37+ "A\0B" ,
38+ "\ud800" ,
39+ "\udc00" ,
40+ "😀X" ,
41+ ]
42+ ) {
43+ for ( let viewOffset = 0 ; viewOffset <= 2 ; viewOffset ++ ) {
44+ for ( let offset = 0 ; offset <= 3 ; offset ++ ) {
45+ for ( let length = 0 ; length <= text . length * 2 + 3 ; length ++ ) {
46+ const backing = Buffer . alloc ( viewOffset + offset + length + 3 , 0xaa ) ;
47+ const target = backing . subarray ( viewOffset , backing . length - 2 ) ;
48+ const expected = Buffer . alloc ( backing . length , 0xaa ) ;
49+ const units = Math . min ( text . length , Math . floor ( length / 2 ) ) ;
50+ for ( let i = 0 ; i < units ; i ++ ) {
51+ const code = text . charCodeAt ( i ) ;
52+ expected [ viewOffset + offset + i * 2 ] = code & 255 ;
53+ expected [ viewOffset + offset + i * 2 + 1 ] = code >>> 8 ;
54+ }
55+ assertEquals (
56+ target . write ( text , offset , length , "utf16le" ) ,
57+ units * 2 ,
58+ ) ;
59+ assertEquals ( backing , expected ) ;
60+ }
61+ }
62+ }
63+ }
64+ } ) ;
65+
66+ Deno . test ( "[node/buffer] UTF-16LE handles large and unaligned writes" , ( ) => {
67+ const text = "ASCII café 漢字 😀\ud800\0" . repeat ( 4096 ) ;
68+ const expected = Buffer . alloc ( text . length * 2 ) ;
69+ for ( let i = 0 ; i < text . length ; i ++ ) {
70+ expected [ i * 2 ] = text . charCodeAt ( i ) & 255 ;
71+ expected [ i * 2 + 1 ] = text . charCodeAt ( i ) >>> 8 ;
72+ }
73+ assertEquals ( Buffer . from ( text , "utf16le" ) , expected ) ;
74+ // Exercise multiple native scratch-buffer iterations and a final short chunk.
75+ for ( const offset of [ 0 , 1 , 2 , 3 ] ) {
76+ const backing = Buffer . alloc ( expected . length + offset + 1 , 0xaa ) ;
77+ assertEquals ( backing . write ( text , offset , "utf16le" ) , expected . length ) ;
78+ assertEquals ( backing . subarray ( offset , offset + expected . length ) , expected ) ;
79+ assertEquals ( backing . subarray ( 0 , offset ) , Buffer . alloc ( offset , 0xaa ) ) ;
80+ assertEquals ( backing [ backing . length - 1 ] , 0xaa ) ;
81+ }
82+ } ) ;
83+
84+ Deno . test ( "[node/buffer] UTF-16LE writes into shared backing stores" , ( ) => {
85+ const backing = new SharedArrayBuffer ( 16 ) ;
86+ const all = Buffer . from ( backing ) ;
87+ all . fill ( 0xaa ) ;
88+ const target = Buffer . from ( backing , 1 , 9 ) ;
89+ assertEquals ( target . write ( "A😀\ud800" , "utf16le" ) , 8 ) ;
90+ assertEquals (
91+ [ ...all ] ,
92+ [
93+ 0xaa ,
94+ 0x41 ,
95+ 0 ,
96+ 0x3d ,
97+ 0xd8 ,
98+ 0 ,
99+ 0xde ,
100+ 0 ,
101+ 0xd8 ,
102+ 0xaa ,
103+ 0xaa ,
104+ 0xaa ,
105+ 0xaa ,
106+ 0xaa ,
107+ 0xaa ,
108+ 0xaa ,
109+ ] ,
110+ ) ;
111+ } ) ;
112+
14113Deno . test ( {
15114 name : "[node/buffer] alloc fails if size is not a number" ,
16115 fn ( ) {
0 commit comments