11cb0ef41Sopenharmony_ci// META: title=Encoding API: Basics
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_citest(function() {
41cb0ef41Sopenharmony_ci    assert_equals((new TextEncoder).encoding, 'utf-8', 'default encoding is utf-8');
51cb0ef41Sopenharmony_ci    assert_equals((new TextDecoder).encoding, 'utf-8', 'default encoding is utf-8');
61cb0ef41Sopenharmony_ci}, 'Default encodings');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_citest(function() {
91cb0ef41Sopenharmony_ci    assert_array_equals(new TextEncoder().encode(), [], 'input default should be empty string')
101cb0ef41Sopenharmony_ci    assert_array_equals(new TextEncoder().encode(undefined), [], 'input default should be empty string')
111cb0ef41Sopenharmony_ci}, 'Default inputs');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cifunction testDecodeSample(encoding, string, bytes) {
151cb0ef41Sopenharmony_ci  test(function() {
161cb0ef41Sopenharmony_ci    assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes)), string);
171cb0ef41Sopenharmony_ci    assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer), string);
181cb0ef41Sopenharmony_ci  }, 'Decode sample: ' + encoding);
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci// z (ASCII U+007A), cent (Latin-1 U+00A2), CJK water (BMP U+6C34),
221cb0ef41Sopenharmony_ci// G-Clef (non-BMP U+1D11E), PUA (BMP U+F8FF), PUA (non-BMP U+10FFFD)
231cb0ef41Sopenharmony_ci// byte-swapped BOM (non-character U+FFFE)
241cb0ef41Sopenharmony_civar sample = 'z\xA2\u6C34\uD834\uDD1E\uF8FF\uDBFF\uDFFD\uFFFE';
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_citest(function() {
271cb0ef41Sopenharmony_ci  var encoding = 'utf-8';
281cb0ef41Sopenharmony_ci  var string = sample;
291cb0ef41Sopenharmony_ci  var bytes = [0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4, 0xF0, 0x9D, 0x84, 0x9E, 0xEF, 0xA3, 0xBF, 0xF4, 0x8F, 0xBF, 0xBD, 0xEF, 0xBF, 0xBE];
301cb0ef41Sopenharmony_ci  var encoded = new TextEncoder().encode(string);
311cb0ef41Sopenharmony_ci  assert_array_equals([].slice.call(encoded), bytes);
321cb0ef41Sopenharmony_ci  assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes)), string);
331cb0ef41Sopenharmony_ci  assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer), string);
341cb0ef41Sopenharmony_ci}, 'Encode/decode round trip: utf-8');
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_citestDecodeSample(
371cb0ef41Sopenharmony_ci  'utf-16le',
381cb0ef41Sopenharmony_ci  sample,
391cb0ef41Sopenharmony_ci  [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8, 0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF]
401cb0ef41Sopenharmony_ci);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_citestDecodeSample(
431cb0ef41Sopenharmony_ci  'utf-16be',
441cb0ef41Sopenharmony_ci  sample,
451cb0ef41Sopenharmony_ci  [0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34, 0xD8, 0x34, 0xDD, 0x1E, 0xF8, 0xFF, 0xDB, 0xFF, 0xDF, 0xFD, 0xFF, 0xFE]
461cb0ef41Sopenharmony_ci);
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_citestDecodeSample(
491cb0ef41Sopenharmony_ci  'utf-16',
501cb0ef41Sopenharmony_ci  sample,
511cb0ef41Sopenharmony_ci  [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8, 0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF]
521cb0ef41Sopenharmony_ci);
53