#!/usr/bin/env runawk

#use "alt_join.awk"
#use "alt_assert.awk"

BEGIN {
	# simple tests for join_keys and join_values
	hash ["apple"]     = "red"
	hash ["bilberry"]  = "dark blue"
	hash ["raspberry"] = "crimson"
	hash ["currant"]   = "black"

	keys1 = join_keys(hash, " ")
	keys2 = join_keys(hash, "")
	keys3 = join_keys(hash, (SUBSEP SUBSEP))

	values1 = join_values(hash, " ")
	values2 = join_values(hash, "")
	values3 = join_values(hash, (SUBSEP SUBSEP))

	print "keys1=" keys1 > "/dev/stderr"
	print "keys2=" keys2 > "/dev/stderr"
	print "keys3=" keys3 > "/dev/stderr"

	print "values1=" values1 > "/dev/stderr"
	print "values2=" values2 > "/dev/stderr"
	print "values3=" values3 > "/dev/stderr"

	assert(length(keys1) == 32)
	assert(length(keys2) == 29)
	assert(length(keys3) == 35)

	assert(length(values1) == 27)
	assert(length(values2) == 24)
	assert(length(values3) == 30)

	# tests for empty hashes
	keys_empty   = join_keys(empty_hash, "qqq")
	values_empty = join_values(empty_hash, "qqq")

	print "keys_empty=" keys_empty     > "/dev/stderr"
	print "values_empty=" values_empty > "/dev/stderr"

	assert(keys_empty == "")
	assert(values_empty == "")

	# test for the case when key == "" or value == ""
	hash_foo [""] = "good"
	hash_foo ["bad"] = ""

	keys_foo   = join_keys(hash_foo, " ")
	values_foo = join_values(hash_foo, " ")

	print "keys_foo=" keys_foo     > "/dev/stderr"
	print "values_foo=" values_foo > "/dev/stderr"

	assert(4 == length(keys_foo))
	assert(5 == length(values_foo))

	# test for join_by_numkeys
	arr [0] = "00"
	arr [1] = "11"
	arr [2] = "22"
	arr [4] = "44"
	arr [5] = "55"
	arr [6] = ""
	arr [7] = ""

	values01 = join_by_numkeys(arr, " ", 0, 1)
	print "values01=" values01 > "/dev/stderr"
	assert(values01 == "00 11");

	values02 = join_by_numkeys(arr, " ", 0)
	print "values02=" values02 > "/dev/stderr"
	assert(values02 == "00 11 22");

	values12 = join_by_numkeys(arr, "+")
	print "values12=" values12 > "/dev/stderr"
	assert(values12 == "11+22");

	values9 = join_by_numkeys(arr, "+", 9)
	print "values9=" values9 > "/dev/stderr"
	assert(values9 == "");

	values2 = join_by_numkeys(arr, "+", 2)
	print "values2=" values2 > "/dev/stderr"
	assert(values2 == "22");

	values46 = join_by_numkeys(arr, "", 4, 7)
	print "values46=" values46 > "/dev/stderr"
	assert(values46 == "4455");

	values6 = join_by_numkeys(arr, "!", 6)
	print "values6=" values6 > "/dev/stderr"
	assert(values6 == "!");

	print "succeeded"

	exit 0
}
