11cb0ef41Sopenharmony_ci// META: title=Encoding API: End-of-file
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_citest(function() {
41cb0ef41Sopenharmony_ci    [
51cb0ef41Sopenharmony_ci        {encoding: 'utf-8', sequence: [0xC0]},
61cb0ef41Sopenharmony_ci        {encoding: 'utf-16le', sequence: [0x00]},
71cb0ef41Sopenharmony_ci        {encoding: 'utf-16be', sequence: [0x00]}
81cb0ef41Sopenharmony_ci    ].forEach(function(testCase) {
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci        assert_throws_js(TypeError, function() {
111cb0ef41Sopenharmony_ci            var decoder = new TextDecoder(testCase.encoding, {fatal: true});
121cb0ef41Sopenharmony_ci            decoder.decode(new Uint8Array(testCase.sequence));
131cb0ef41Sopenharmony_ci        }, 'Unterminated ' + testCase.encoding + ' sequence should throw if fatal flag is set');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci        assert_equals(
161cb0ef41Sopenharmony_ci            new TextDecoder(testCase.encoding).decode(new Uint8Array([testCase.sequence])),
171cb0ef41Sopenharmony_ci            '\uFFFD',
181cb0ef41Sopenharmony_ci            'Unterminated UTF-8 sequence should emit replacement character if fatal flag is unset');
191cb0ef41Sopenharmony_ci    });
201cb0ef41Sopenharmony_ci}, 'Fatal flag, non-streaming cases');
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_citest(function() {
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci    var decoder = new TextDecoder('utf-16le', {fatal: true});
251cb0ef41Sopenharmony_ci    var odd = new Uint8Array([0x00]);
261cb0ef41Sopenharmony_ci    var even = new Uint8Array([0x00, 0x00]);
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci    assert_equals(decoder.decode(odd, {stream: true}), '');
291cb0ef41Sopenharmony_ci    assert_equals(decoder.decode(odd), '\u0000');
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    assert_throws_js(TypeError, function() {
321cb0ef41Sopenharmony_ci        decoder.decode(even, {stream: true});
331cb0ef41Sopenharmony_ci        decoder.decode(odd)
341cb0ef41Sopenharmony_ci    });
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci    assert_throws_js(TypeError, function() {
371cb0ef41Sopenharmony_ci        decoder.decode(odd, {stream: true});
381cb0ef41Sopenharmony_ci        decoder.decode(even);
391cb0ef41Sopenharmony_ci    });
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    assert_equals(decoder.decode(even, {stream: true}), '\u0000');
421cb0ef41Sopenharmony_ci    assert_equals(decoder.decode(even), '\u0000');
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci}, 'Fatal flag, streaming cases');
45