#!/usr/bin/env runawk

#use "multisub.awk"
#use "alt_assert.awk"

function test (ideal, input, repls, num, keep,
       res)
{
#	print "ideal=" ideal > "/dev/stderr"
	res = multisub(input, repls, keep)
#	print "res=" res > "/dev/stderr"
	if (ideal != res){
		msg = ("test " num " failed")
		msg = msg "\n ideal=\"" ideal "\""
		msg = msg "\n res  =\"" res "\""
		abort(msg)
	}
#	print "test " num " succeeded"
}

BEGIN {
	test("osb", "[", "[:osb", -1)
	test("csb", "]", "]:csb", -2)
	test("", "", "", 1)
	test("aaa", "aaa", "", 2)
	test("aaa", "aaa", "b:c", 3)
	test("dc", "bbb", "b:c   bb:d", 4)
	test("e", "bbb", "b:c   bbb:e   bb:d", 5)
	test("Bd", "BBBA", "ABB:c   BBA:d", 6)
	test("ccBBde", "ABBABBBBBBAAB", "ABB:c   BBA:d   AB:e", 7)
	test("kABBA(acca)BC(saab)", "kabba(acca)bc(saab)", "a:A   b:B   c:C   [(][^()]*[)]:&", 7, "&")

	if (!__buggy_mawk)
		test("aaa\nbbb\nccc\tddd\aeee\a", "aaa\\nbbb\\nccc\\tddd\\aeee\\a", "\\t:\t   \\a:\a   \\n:\n", 7)

	test("question_question_osb_osb_csb_csb_ocb_ocb_ccb_ccb_or_or_orb_orb_crb_crb_star_star_plus_plus_dot_power_power_dollar_backslash_",
		"??[[]]{{}}||(())**++.^^$\\", "$:dollar_   ^:power_   .:dot_   +:plus_   *:star_   ):crb_   (:orb_   |:or_   }:ccb_   {:ocb_   [:osb_   ]:csb_   ?:question_   \\:backslash_", 8)

	exit 0
}
