#!/usr/bin/env qore
# -*- mode: qore; indent-tabs-mode: nil -*-

%new-style
%enable-all-warnings
%require-types
%strict-args

%requires ../../../../qlib/Util.qm
%requires ../../../../qlib/QUnit.qm

%exec-class ReplaceTest

public class ReplaceTest inherits QUnit::Test {
    constructor() : Test("ReplaceTest", "1.0") {
        addTestCase("replace() tests", \replaceTest());

        # Return for compatibility with test harness that checks return value.
        set_return_value(main());
    }

    replaceTest() {
        assertEq("", replace("", "", ""));
        assertEq("", replace("", "", "a"));
        assertEq("", replace("", "a", "a"));
        assertEq("", replace("", "a", ""));
        assertEq("", replace("", "bc", "xy"));
        assertEq("", replace("", "a", "xy"));
        assertEq("", replace("", "ab", "x"));
        assertEq("", replace("", "ab", "x", 1));
        assertEq("", replace("", "ab", "x", 1, 2));
        assertEq("", replace("", "ab", "x", -1, 2));
        assertEq("", replace("", "ab", "x", -5));
        assertEq("", replace("", "ab", "x", 1, -1));
        assertEq("", replace("", "ab", "x", 1, -5));
        assertEq("", replace("", "ab", "x", -1, -1));
        assertEq("", replace("", "ab", "x", -1, -5));
        assertEq("", replace("", "ab", "x", 0, -5));
        assertEq("", replace("", "ab", "x", 1, 0));

        assertEq("ab", replace("abc", "c", ""));
        assertEq("bc", replace("abc", "a", ""));
        assertEq("abxyz", replace("abc", "c", "xyz"));
        assertEq("xyzbc", replace("abc", "a", "xyz"));

        assertEq("axyd", replace("abcd", "bc", "xy"));
        assertEq("axyd 123 uxyp", replace("abcd 123 ubcp", "bc", "xy"));
        assertEq("qaz", replace("qaz", "\0", "A"));
        assertEq("qaz", replace("qaz", "\0xyz", "A"));
        assertEq("qaz", replace("qaz", "", "A"));
        assertEq("abx", replace("abc", "c", "x", 2));
        assertEq("abxd", replace("abcd", "c", "x", 2, 2));
        assertEq("abcx", replace("abcd", "d", "x", 3, 3));
        assertEq("abxd", replace("abcd", "c", "x", -2, -2));
        assertEq("abcx", replace("abcd", "d", "x", -1, -1));
        assertEq("abxd", replace("abcd", "c", "x", -2, 3));
        assertEq("abcx", replace("abcd", "d", "x", -1, 3));
        assertEq("abxd", replace("abcd", "c", "x", -2, 5));
        assertEq("abcx", replace("abcd", "d", "x", -1, 5));
        assertEq("abcd", replace("abcd", "1", "2"));
        assertEq("abcd", replace("abcd", "1", "2", 0, -1));
        assertEq("abcd", replace("abcd", "1", "2", 9));
        assertEq("abcd", replace("abcd", "1", "2", 9, 12));
        assertEq("abcd", replace("abcd", "1", "2", -20, 12));
        assertEq("arší", replace("prší", "p", "a"));
        assertEq("prší", replace("prší", "p", "a", -2));
        assertEq("arší", replace("prší", "p", "a", -4));
        assertEq("V Praze arší.", replace("V Praze prší.", "p", "a", -5));
    }
}
