Skip to content

Commit bfa4fb1

Browse files
committed
Add test cases for UTF16-LE encoding
1 parent c47dfed commit bfa4fb1

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

example/src/tests/utils/encoding_tests.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,84 @@ test(
578578
},
579579
);
580580

581+
// --- UTF-16LE ---
582+
583+
test(SUITE, '[Node.js] Roundtrips ASCII text through utf16le encoding.', () => {
584+
const str = 'foo';
585+
const ab = stringToBuffer(str, 'utf16le');
586+
expect(bufferToString(ab, 'utf16le')).to.equal(str);
587+
});
588+
589+
test(
590+
SUITE,
591+
'Roundtrips UTF-16LE text containing an unpaired high surrogate.',
592+
() => {
593+
const str = 'A\uD83DB';
594+
const ab = stringToBuffer(str, 'utf16le');
595+
expect(toU8(ab)).to.deep.equal(
596+
new Uint8Array([0x41, 0x00, 0x3d, 0xd8, 0x42, 0x00]),
597+
);
598+
expect(bufferToString(ab, 'utf16le')).to.equal(str);
599+
},
600+
);
601+
602+
test(
603+
SUITE,
604+
'Roundtrips UTF-16LE text containing an unpaired low surrogate.',
605+
() => {
606+
const str = 'A\uDC00B';
607+
const ab = stringToBuffer(str, 'utf16le');
608+
expect(toU8(ab)).to.deep.equal(
609+
new Uint8Array([0x41, 0x00, 0x00, 0xdc, 0x42, 0x00]),
610+
);
611+
expect(bufferToString(ab, 'utf16le')).to.equal(str);
612+
},
613+
);
614+
615+
test(SUITE, '[Node.js] UTF-16LE encoding of "über"', () => {
616+
expect(toU8(stringToBuffer('über', 'utf16le'))).to.deep.equal(
617+
new Uint8Array([252, 0, 98, 0, 101, 0, 114, 0]),
618+
);
619+
});
620+
621+
test(SUITE, '[Node.js] UTF-16LE encoding of "привет"', () => {
622+
const encoded = toU8(stringToBuffer('привет', 'utf16le'));
623+
expect(encoded).to.deep.equal(
624+
new Uint8Array([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]),
625+
);
626+
expect(bufferToString(encoded.buffer as ArrayBuffer, 'utf16le')).to.equal(
627+
'привет',
628+
);
629+
});
630+
631+
test(SUITE, '[Node.js] UTF-16LE encoding of Thumbs up sign (U+1F44D)', () => {
632+
expect(toU8(stringToBuffer('\uD83D\uDC4D', 'utf16le'))).to.deep.equal(
633+
new Uint8Array([0x3d, 0xd8, 0x4d, 0xdc]),
634+
);
635+
});
636+
637+
test(SUITE, '[Node.js] Decodes UTF-16LE bytes back to Japanese text.', () => {
638+
const bytes = new Uint8Array([
639+
0x42, 0x30, 0x44, 0x30, 0x46, 0x30, 0x48, 0x30, 0x4a, 0x30,
640+
]);
641+
expect(bufferToString(bytes.buffer as ArrayBuffer, 'utf16le')).to.equal(
642+
'あいうえお',
643+
);
644+
});
645+
646+
test(
647+
SUITE,
648+
'[Node.js] Decodes UTF-16LE bytes correctly from a sliced buffer starting at byte offset 1.',
649+
() => {
650+
const bytes = new Uint8Array([
651+
0xff, 0x42, 0x30, 0x44, 0x30, 0x46, 0x30, 0x48, 0x30, 0x4a, 0x30,
652+
]);
653+
expect(
654+
bufferToString(bytes.slice(1).buffer as ArrayBuffer, 'utf16le'),
655+
).to.equal('あいうえお');
656+
},
657+
);
658+
581659
// --- Latin1 / Binary ---
582660

583661
test(

0 commit comments

Comments
 (0)