11cb0ef41Sopenharmony_ci// META: title=Encoding API: Invalid UTF-16 surrogates with UTF-8 encoding 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_civar badStrings = [ 41cb0ef41Sopenharmony_ci { 51cb0ef41Sopenharmony_ci input: 'abc123', 61cb0ef41Sopenharmony_ci expected: [0x61, 0x62, 0x63, 0x31, 0x32, 0x33], 71cb0ef41Sopenharmony_ci decoded: 'abc123', 81cb0ef41Sopenharmony_ci name: 'Sanity check' 91cb0ef41Sopenharmony_ci }, 101cb0ef41Sopenharmony_ci { 111cb0ef41Sopenharmony_ci input: '\uD800', 121cb0ef41Sopenharmony_ci expected: [0xef, 0xbf, 0xbd], 131cb0ef41Sopenharmony_ci decoded: '\uFFFD', 141cb0ef41Sopenharmony_ci name: 'Surrogate half (low)' 151cb0ef41Sopenharmony_ci }, 161cb0ef41Sopenharmony_ci { 171cb0ef41Sopenharmony_ci input: '\uDC00', 181cb0ef41Sopenharmony_ci expected: [0xef, 0xbf, 0xbd], 191cb0ef41Sopenharmony_ci decoded: '\uFFFD', 201cb0ef41Sopenharmony_ci name: 'Surrogate half (high)' 211cb0ef41Sopenharmony_ci }, 221cb0ef41Sopenharmony_ci { 231cb0ef41Sopenharmony_ci input: 'abc\uD800123', 241cb0ef41Sopenharmony_ci expected: [0x61, 0x62, 0x63, 0xef, 0xbf, 0xbd, 0x31, 0x32, 0x33], 251cb0ef41Sopenharmony_ci decoded: 'abc\uFFFD123', 261cb0ef41Sopenharmony_ci name: 'Surrogate half (low), in a string' 271cb0ef41Sopenharmony_ci }, 281cb0ef41Sopenharmony_ci { 291cb0ef41Sopenharmony_ci input: 'abc\uDC00123', 301cb0ef41Sopenharmony_ci expected: [0x61, 0x62, 0x63, 0xef, 0xbf, 0xbd, 0x31, 0x32, 0x33], 311cb0ef41Sopenharmony_ci decoded: 'abc\uFFFD123', 321cb0ef41Sopenharmony_ci name: 'Surrogate half (high), in a string' 331cb0ef41Sopenharmony_ci }, 341cb0ef41Sopenharmony_ci { 351cb0ef41Sopenharmony_ci input: '\uDC00\uD800', 361cb0ef41Sopenharmony_ci expected: [0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd], 371cb0ef41Sopenharmony_ci decoded: '\uFFFD\uFFFD', 381cb0ef41Sopenharmony_ci name: 'Wrong order' 391cb0ef41Sopenharmony_ci } 401cb0ef41Sopenharmony_ci]; 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_cibadStrings.forEach(function(t) { 431cb0ef41Sopenharmony_ci test(function() { 441cb0ef41Sopenharmony_ci var encoded = new TextEncoder().encode(t.input); 451cb0ef41Sopenharmony_ci assert_array_equals([].slice.call(encoded), t.expected); 461cb0ef41Sopenharmony_ci assert_equals(new TextDecoder('utf-8').decode(encoded), t.decoded); 471cb0ef41Sopenharmony_ci }, 'Invalid surrogates encoded into UTF-8: ' + t.name); 481cb0ef41Sopenharmony_ci}); 49