<meta charset="utf-8">
<div id="container"></div>
<pre id="console" style="visibility: hidden;"></pre>
<script>
    function log(message)
    {
        document.getElementById("console").appendChild(document.createTextNode(message + "\n"));
    }

    function testFindString(text, target, options, expectedRanges)
    {
        log("Searching for \u2018" + target + "\u2019 " + (text.length <= 64 ? "in \u2018" + text + "\u2019 " : "in long string ") + "with options [" + options.join(", ") + "]:");

        var container = document.getElementById("container");
        container.innerText = text;
        document.body.offsetTop;
        var selection = getSelection();
        selection.empty();

        var expectedRange;
        while (expectedRange = expectedRanges.shift()) {
            var found = testRunner.findString(target, options);
            if (found) {
                var actualRange = [selection.baseOffset, selection.extentOffset];
                if (expectedRange[0] !== actualRange[0] || expectedRange[1] !== actualRange[1])
                    log("FAIL: Expected a match at " + expectedRange + " but got a match at " + actualRange + " instead.");
                else
                    log("PASS: Got a match at " + expectedRange + " as expected.");
            } else if (expectedRange.length)
                log("FAIL: Expected " + expectedRange + " but got no match.");
            else
                log("PASS: Got no match as expected.");
        }
        container.innerText = "";
        log("");
    }

    testRunner.dumpAsText();

    testFindString("Lorem ipsum dolor sit amet", "o", [], [[1, 2], [13, 14], [15, 16], []]);
    testFindString("Lorem ipsum dolor sit amet", "o", ["WrapAround"], [[1, 2], [13, 14], [15, 16], [1, 2]]);
    testFindString("Lorem ipsum dolor sit amet", "o", ["Backwards"], [[15, 16], [13, 14], [1, 2], []]);
    testFindString("Lorem ipsum dolor sit amet", "o", ["Backwards", "WrapAround"], [[15, 16], [13, 14], [1, 2], [15, 16]]);
    testFindString("Lorem ipsum dolor sit amet", "O", [], [[]]);
    testFindString("Lorem ipsum dolor sit amet", "O", ["CaseInsensitive"], [[1, 2], [13, 14], [15, 16]]);

    testFindString("insurmountable mountain", "mount", [], [[5, 10], [15, 20], []]);
    testFindString("insurmountable mountain", "mount", ["AtWordStarts"], [[15, 20], []]);

    testFindString("cocoa", "co", [], [[0, 2], [2, 4], []]);
    testFindString("cocoa", "co", ["AtWordStarts"], [[0, 2], []]);

    testFindString("webkit.org", "org", ["AtWordStarts"], [[]]);
    testFindString("webkit.org", ".org", ["AtWordStarts"], [[6, 10], []]);

    testFindString("webkit.org", "rg", ["AtWordStarts", "TreatMedialCapitalAsWordStart"], [[]]);
    testFindString("webkit.org", "org", ["AtWordStarts", "TreatMedialCapitalAsWordStart"], [[7, 10], []]);
    testFindString("webkit.org", ".org", ["AtWordStarts", "TreatMedialCapitalAsWordStart"], [[6, 10], []]);
    testFindString("webkit.org", "t.org", ["AtWordStarts", "TreatMedialCapitalAsWordStart"], [[]]);

    testFindString("WebKit", "it", ["AtWordStarts", "TreatMedialCapitalAsWordStart"], [[]]);
    testFindString("WebKit", "Kit", ["AtWordStarts", "TreatMedialCapitalAsWordStart"], [[3, 6], []]);
    testFindString("WebKit", "bKit", ["AtWordStarts", "TreatMedialCapitalAsWordStart"], [[]]);

    testFindString("XMLHTTPRequest", "equest", ["AtWordStarts", "TreatMedialCapitalAsWordStart"], [[]]);
    testFindString("XMLHTTPRequest", "Request", ["AtWordStarts", "TreatMedialCapitalAsWordStart"], [[7, 14], []]);
    testFindString("XMLHTTPRequest", "PRequest", ["AtWordStarts", "TreatMedialCapitalAsWordStart"], [[]]);

    testFindString("LP64", "64", ["AtWordStarts"], [[]]);
    testFindString("LP64", "4", ["AtWordStarts", "TreatMedialCapitalAsWordStart"], [[]]);
    testFindString("LP64", "64", ["AtWordStarts", "TreatMedialCapitalAsWordStart"], [[2, 4], []]);
    testFindString("LP64", "P64", ["AtWordStarts", "TreatMedialCapitalAsWordStart"], [[]]);

    testFindString("\u8d77\u52d5\u6226\u58eb", "\u52d5\u6226\u58eb", ["AtWordStarts"], [[1, 4], []]);
    testFindString("\u8d77\u52d5\u6226\u58eb", "\u6226\u58eb", ["AtWordStarts"], [[2, 4], []]);
    testFindString("\u8d77\u52d5\u6226\u58eb", "\u58eb", ["AtWordStarts"], [[3, 4], []]);

    const searchBufferSize = 8192;
    const searchBufferOverlapSize = searchBufferSize / 4;
    const searchBufferUnoverlappedSize = searchBufferSize - searchBufferOverlapSize;
    var bufferSizedString = "X";
    while (bufferSizedString.length < searchBufferSize)
        bufferSizedString += bufferSizedString;
    bufferSizedString = bufferSizedString.substring(0, searchBufferSize);

    testFindString(bufferSizedString.substring(0, searchBufferUnoverlappedSize - 2) + " ba a" + bufferSizedString, "a", ["AtWordStarts"], [[searchBufferUnoverlappedSize + 2, searchBufferUnoverlappedSize + 3], []]);

    var thaiWords = [
        "\u0e01\u0e23",
        "\u0e1b\u0e39\u0e40\u0e25",
        "\u0e01\u0e0a",
        "\u0e01\u0e0a\u0e01\u0e23", // thaiWords[2] + thaiWords[0]
        "\u0e01\u0e23\u0e01\u0e0a", // thaiWords[0] + thaiWords[2]
        "\u0e1a\u0e07\u0e01\u0e0a", // ends with thaiWords[2]
    ];

    testFindString(thaiWords.join(""), thaiWords[0], [], [[0, 2], [10, 12], [12, 14], []]);
    testFindString(thaiWords.join(""), thaiWords[0], ["AtWordStarts"], [[0, 2], [12, 14], []]);

    testFindString(thaiWords.join(""), thaiWords[2], [], [[6, 8], [8, 10], [14, 16], [18, 20], []]);
    testFindString(thaiWords.join(""), thaiWords[2], ["AtWordStarts"], [[6, 8], [8, 10], []]);

    testFindString(bufferSizedString.substring(0, searchBufferUnoverlappedSize) + thaiWords.join("") + bufferSizedString, thaiWords[0], [], [[searchBufferUnoverlappedSize, searchBufferUnoverlappedSize + 2], [searchBufferUnoverlappedSize + 10, searchBufferUnoverlappedSize + 12], [searchBufferUnoverlappedSize + 12, searchBufferUnoverlappedSize + 14], []]);
    testFindString(bufferSizedString.substring(0, searchBufferUnoverlappedSize) + thaiWords.join("") + bufferSizedString, thaiWords[0], ["AtWordStarts"], [[searchBufferUnoverlappedSize + 12, searchBufferUnoverlappedSize + 14], []]);
    testFindString(bufferSizedString.substring(0, searchBufferUnoverlappedSize - 1) + " " + thaiWords.join("") + bufferSizedString, thaiWords[0], ["AtWordStarts"], [[searchBufferUnoverlappedSize, searchBufferUnoverlappedSize + 2], [searchBufferUnoverlappedSize + 12, searchBufferUnoverlappedSize + 14], []]);
    testFindString(bufferSizedString.substring(0, searchBufferUnoverlappedSize - 3) + " " + thaiWords[4] + bufferSizedString, thaiWords[2], ["AtWordStarts"], [[]]);

    testFindString("Spaces, the final frontier", " ", ["AtWordStarts"], [[7, 8], [11, 12], [17, 18], []]);
    testFindString("Use an @import rule", "@", ["AtWordStarts"], [[7, 8], []]);
    testFindString("If ((x + 5) * 2) = 14, then x = 2", "(x", ["AtWordStarts"], [[4, 6], []]);

    document.getElementById("console").style.removeProperty("visibility");
</script>
