| 1 |
#!/usr/bin/env runawk |
| 2 |
|
| 3 |
#use "alt_join.awk" |
| 4 |
#use "alt_assert.awk" |
| 5 |
|
| 6 |
BEGIN { |
| 7 |
# simple tests for join_keys and join_values |
| 8 |
hash ["apple"] = "red" |
| 9 |
hash ["bilberry"] = "dark blue" |
| 10 |
hash ["raspberry"] = "crimson" |
| 11 |
hash ["currant"] = "black" |
| 12 |
|
| 13 |
keys1 = join_keys(hash, " ") |
| 14 |
keys2 = join_keys(hash, "") |
| 15 |
keys3 = join_keys(hash, (SUBSEP SUBSEP)) |
| 16 |
|
| 17 |
values1 = join_values(hash, " ") |
| 18 |
values2 = join_values(hash, "") |
| 19 |
values3 = join_values(hash, (SUBSEP SUBSEP)) |
| 20 |
|
| 21 |
print "keys1=" keys1 > "/dev/stderr" |
| 22 |
print "keys2=" keys2 > "/dev/stderr" |
| 23 |
print "keys3=" keys3 > "/dev/stderr" |
| 24 |
|
| 25 |
print "values1=" values1 > "/dev/stderr" |
| 26 |
print "values2=" values2 > "/dev/stderr" |
| 27 |
print "values3=" values3 > "/dev/stderr" |
| 28 |
|
| 29 |
assert(length(keys1) == 32) |
| 30 |
assert(length(keys2) == 29) |
| 31 |
assert(length(keys3) == 35) |
| 32 |
|
| 33 |
assert(length(values1) == 27) |
| 34 |
assert(length(values2) == 24) |
| 35 |
assert(length(values3) == 30) |
| 36 |
|
| 37 |
# tests for empty hashes |
| 38 |
keys_empty = join_keys(empty_hash, "qqq") |
| 39 |
values_empty = join_values(empty_hash, "qqq") |
| 40 |
|
| 41 |
print "keys_empty=" keys_empty > "/dev/stderr" |
| 42 |
print "values_empty=" values_empty > "/dev/stderr" |
| 43 |
|
| 44 |
assert(keys_empty == "") |
| 45 |
assert(values_empty == "") |
| 46 |
|
| 47 |
# test for the case when key == "" or value == "" |
| 48 |
hash_foo [""] = "good" |
| 49 |
hash_foo ["bad"] = "" |
| 50 |
|
| 51 |
keys_foo = join_keys(hash_foo, " ") |
| 52 |
values_foo = join_values(hash_foo, " ") |
| 53 |
|
| 54 |
print "keys_foo=" keys_foo > "/dev/stderr" |
| 55 |
print "values_foo=" values_foo > "/dev/stderr" |
| 56 |
|
| 57 |
assert(4 == length(keys_foo)) |
| 58 |
assert(5 == length(values_foo)) |
| 59 |
|
| 60 |
# test for join_by_numkeys |
| 61 |
arr [0] = "00" |
| 62 |
arr [1] = "11" |
| 63 |
arr [2] = "22" |
| 64 |
arr [4] = "44" |
| 65 |
arr [5] = "55" |
| 66 |
arr [6] = "" |
| 67 |
arr [7] = "" |
| 68 |
|
| 69 |
values01 = join_by_numkeys(arr, " ", 0, 1) |
| 70 |
print "values01=" values01 > "/dev/stderr" |
| 71 |
assert(values01 == "00 11"); |
| 72 |
|
| 73 |
values02 = join_by_numkeys(arr, " ", 0) |
| 74 |
print "values02=" values02 > "/dev/stderr" |
| 75 |
assert(values02 == "00 11 22"); |
| 76 |
|
| 77 |
values12 = join_by_numkeys(arr, "+") |
| 78 |
print "values12=" values12 > "/dev/stderr" |
| 79 |
assert(values12 == "11+22"); |
| 80 |
|
| 81 |
values9 = join_by_numkeys(arr, "+", 9) |
| 82 |
print "values9=" values9 > "/dev/stderr" |
| 83 |
assert(values9 == ""); |
| 84 |
|
| 85 |
values2 = join_by_numkeys(arr, "+", 2) |
| 86 |
print "values2=" values2 > "/dev/stderr" |
| 87 |
assert(values2 == "22"); |
| 88 |
|
| 89 |
values46 = join_by_numkeys(arr, "", 4, 7) |
| 90 |
print "values46=" values46 > "/dev/stderr" |
| 91 |
assert(values46 == "4455"); |
| 92 |
|
| 93 |
values6 = join_by_numkeys(arr, "!", 6) |
| 94 |
print "values6=" values6 > "/dev/stderr" |
| 95 |
assert(values6 == "!"); |
| 96 |
|
| 97 |
print "succeeded" |
| 98 |
|
| 99 |
exit 0 |
| 100 |
} |