11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciif (!common.hasIntl)
61cb0ef41Sopenharmony_ci  common.skip('missing Intl');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst buffer = require('buffer');
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst orig = Buffer.from('těst ☕', 'utf8');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// Test Transcoding
131cb0ef41Sopenharmony_ciconst tests = {
141cb0ef41Sopenharmony_ci  'latin1': [0x74, 0x3f, 0x73, 0x74, 0x20, 0x3f],
151cb0ef41Sopenharmony_ci  'ascii': [0x74, 0x3f, 0x73, 0x74, 0x20, 0x3f],
161cb0ef41Sopenharmony_ci  'ucs2': [0x74, 0x00, 0x1b, 0x01, 0x73,
171cb0ef41Sopenharmony_ci           0x00, 0x74, 0x00, 0x20, 0x00,
181cb0ef41Sopenharmony_ci           0x15, 0x26]
191cb0ef41Sopenharmony_ci};
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cifor (const test in tests) {
221cb0ef41Sopenharmony_ci  const dest = buffer.transcode(orig, 'utf8', test);
231cb0ef41Sopenharmony_ci  assert.strictEqual(dest.length, tests[test].length, `utf8->${test} length`);
241cb0ef41Sopenharmony_ci  for (let n = 0; n < tests[test].length; n++)
251cb0ef41Sopenharmony_ci    assert.strictEqual(dest[n], tests[test][n], `utf8->${test} char ${n}`);
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci{
291cb0ef41Sopenharmony_ci  const dest = buffer.transcode(Buffer.from(tests.ucs2), 'ucs2', 'utf8');
301cb0ef41Sopenharmony_ci  assert.strictEqual(dest.toString(), orig.toString());
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci{
341cb0ef41Sopenharmony_ci  const utf8 = Buffer.from('€'.repeat(4000), 'utf8');
351cb0ef41Sopenharmony_ci  const ucs2 = Buffer.from('€'.repeat(4000), 'ucs2');
361cb0ef41Sopenharmony_ci  const utf8_to_ucs2 = buffer.transcode(utf8, 'utf8', 'ucs2');
371cb0ef41Sopenharmony_ci  const ucs2_to_utf8 = buffer.transcode(ucs2, 'ucs2', 'utf8');
381cb0ef41Sopenharmony_ci  assert.deepStrictEqual(utf8, ucs2_to_utf8);
391cb0ef41Sopenharmony_ci  assert.deepStrictEqual(ucs2, utf8_to_ucs2);
401cb0ef41Sopenharmony_ci  assert.strictEqual(ucs2_to_utf8.toString('utf8'),
411cb0ef41Sopenharmony_ci                     utf8_to_ucs2.toString('ucs2'));
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciassert.throws(
451cb0ef41Sopenharmony_ci  () => buffer.transcode(null, 'utf8', 'ascii'),
461cb0ef41Sopenharmony_ci  {
471cb0ef41Sopenharmony_ci    name: 'TypeError',
481cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
491cb0ef41Sopenharmony_ci    message: 'The "source" argument must be an instance of Buffer ' +
501cb0ef41Sopenharmony_ci             'or Uint8Array. Received null'
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciassert.throws(
551cb0ef41Sopenharmony_ci  () => buffer.transcode(Buffer.from('a'), 'b', 'utf8'),
561cb0ef41Sopenharmony_ci  /^Error: Unable to transcode Buffer \[U_ILLEGAL_ARGUMENT_ERROR\]/
571cb0ef41Sopenharmony_ci);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ciassert.throws(
601cb0ef41Sopenharmony_ci  () => buffer.transcode(Buffer.from('a'), 'uf8', 'b'),
611cb0ef41Sopenharmony_ci  /^Error: Unable to transcode Buffer \[U_ILLEGAL_ARGUMENT_ERROR\]$/
621cb0ef41Sopenharmony_ci);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ciassert.deepStrictEqual(
651cb0ef41Sopenharmony_ci  buffer.transcode(Buffer.from('hi', 'ascii'), 'ascii', 'utf16le'),
661cb0ef41Sopenharmony_ci  Buffer.from('hi', 'utf16le'));
671cb0ef41Sopenharmony_ciassert.deepStrictEqual(
681cb0ef41Sopenharmony_ci  buffer.transcode(Buffer.from('hi', 'latin1'), 'latin1', 'utf16le'),
691cb0ef41Sopenharmony_ci  Buffer.from('hi', 'utf16le'));
701cb0ef41Sopenharmony_ciassert.deepStrictEqual(
711cb0ef41Sopenharmony_ci  buffer.transcode(Buffer.from('hä', 'latin1'), 'latin1', 'utf16le'),
721cb0ef41Sopenharmony_ci  Buffer.from('hä', 'utf16le'));
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci// Test that Uint8Array arguments are okay.
751cb0ef41Sopenharmony_ci{
761cb0ef41Sopenharmony_ci  const uint8array = new Uint8Array([...Buffer.from('hä', 'latin1')]);
771cb0ef41Sopenharmony_ci  assert.deepStrictEqual(
781cb0ef41Sopenharmony_ci    buffer.transcode(uint8array, 'latin1', 'utf16le'),
791cb0ef41Sopenharmony_ci    Buffer.from('hä', 'utf16le'));
801cb0ef41Sopenharmony_ci}
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci{
831cb0ef41Sopenharmony_ci  const dest = buffer.transcode(new Uint8Array(), 'utf8', 'latin1');
841cb0ef41Sopenharmony_ci  assert.strictEqual(dest.length, 0);
851cb0ef41Sopenharmony_ci}
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci// Test that it doesn't crash
881cb0ef41Sopenharmony_ci{
891cb0ef41Sopenharmony_ci  buffer.transcode(new buffer.SlowBuffer(1), 'utf16le', 'ucs2');
901cb0ef41Sopenharmony_ci}
91