// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

function assertThrows(s) {
    try {
        decodeURI(s);
    } catch (e) {
        if (!(e instanceof URIError)) {
            throw "decodeURI('" + s + "') threw " + e + " instead of URIError";
        }
        return;
    }
    throw "Excpected decodeURI('" + s + "') to throw URIError.";
}

function assertEquals(a, b) {
    if (decodeURI(a) !== b)
        throw "Expected decodeURI('" + a + "') to be equal to " + b;
}

//Phase1: overlong sequences
assertThrows("%C1%BF");
assertThrows("%E0%9F%BF");
assertThrows("%F0%88%80%80");
assertThrows("%F0%8F%BF%BF");
assertThrows("%F0%80%C0%80%80");
assertThrows("%F8%84%8F%BF%BF");
assertThrows("%FC%80%84%8F%BF%BF");

assertThrows("%C1%BF");
assertThrows("%E0%9F%BF");

//Phase 2:Out of Unicode range

assertThrows("%F4%90%80%80%80");
assertThrows("%F8%84%90%80%80");
assertThrows("%FC%80%84%90%80%80");

assertThrows("%ED%A0%80");
assertThrows("%ED%AF%BF");
assertThrows("%ED%B0%80");
assertThrows("%ED%BF%BF");


//Phase 3:Valid sequences must be decoded correctly

assertEquals("%7F", "\x7f");
assertEquals("%C2%80", "\x80");
assertEquals("%E0%A0%80", "\u0800");
assertEquals("%F0%90%80%80", "\uD800\uDC00");
assertEquals("%F4%8F%BF%BF", "\uDBFF\uDFFF");

"success"
