module Misc_Helpers {

modulepar {
	charstring mp_osmo_repo := "nightly";
}

/* Test the Osmocom repository (nightly, latest, 2021q4, ...), from which the
 * SUT is running (OS#4878). Usage examples:
 * 	if (Misc_Helpers.f_osmo_repo_is("nightly")) {
 * 		...
 * 	}
 * 	if (Misc_Helpers.f_osmo_repo_is(("nightly", "2021q4"))) {
 * 		...
 * 	}
 */
function f_osmo_repo_is(template charstring ver) return boolean {
	return match(mp_osmo_repo, ver);
}

/* Try to properly shutdown a testcase.
 * The reliable method to stop a testcase without running into dynamic
 * testcase errors due to unconnected ports receiving messages is to call
 * all component.stop before terminating. However, this can only be called
 * from the mtc! So in case we want to terminate from a component that is not
 * the mtc we try to do the next best thing which is calling mtc.stop and
 * hoping for the best.
 */
function f_shutdown(charstring file, integer line, verdicttype verdict := none,
			charstring text := "") {
	if (verdict != none) {
		text := file & ":" & int2str(line) & " : " & text
		setverdict(verdict, text);
	}

	log("Stopping testcase execution from ", file, ":", line)
	if (self == mtc) {
		/* Properly stop all ports before disconnecting them. This avoids
		 * running into the dynamic testcase error due to messages arriving on
		 * unconnected ports. */
		all component.stop;
	}
	mtc.stop
}

function f_addr_is_ipv6(charstring addr) return boolean {
	for (var integer i := 0; i < lengthof(addr); i := i + 1) {
		if (addr[i] == ":") {
			return true;
		}
	}
	return false;
}

}
