couple of new example scripts and unit tests.
authorChris Koeritz <fred@gruntose.com>
Sun, 6 Oct 2013 23:13:52 +0000 (18:13 -0500)
committerChris Koeritz <fred@gruntose.com>
Sun, 6 Oct 2013 23:13:52 +0000 (18:13 -0500)
examples/bashisms/it_is_possible_to_cleanly_copy_array_elems.txt [new file with mode: 0644]
scripts/unit_test/array_sifter_test.sh [new file with mode: 0644]
scripts/unit_test/test_assoc_array.sh [new file with mode: 0644]

diff --git a/examples/bashisms/it_is_possible_to_cleanly_copy_array_elems.txt b/examples/bashisms/it_is_possible_to_cleanly_copy_array_elems.txt
new file mode 100644 (file)
index 0000000..8be73c9
--- /dev/null
@@ -0,0 +1,16 @@
+
+
+# when the array elements have spaces, it is still possible to get them back right.
+
+
+a=("ted row" "boon" "moopy dongle")
+echo ${#a[@]}
+# 3
+borg=( "${a[@]}" )
+echo ${#borg[@]}
+# 3
+
+# normally the setting of borg would not preserve the elements with spaces in them as separate.
+# but by using the @ to get all the array members and the spaces around the reference, you get
+# the list back properly.
+
diff --git a/scripts/unit_test/array_sifter_test.sh b/scripts/unit_test/array_sifter_test.sh
new file mode 100644 (file)
index 0000000..26173f1
--- /dev/null
@@ -0,0 +1,47 @@
+#!/bin/bash
+#
+# tests the array sifter methods.
+
+source $FEISTY_MEOW_SCRIPTS/core/array_sifter.sh
+
+#demo 1 & 2 for test presence.
+declare -a my_array=(peanuts sauce fish basil)
+
+test1=basil
+test_presence my_array $test1
+if [ $? != 0 ]; then
+  echo "test1 did not find flag, but should have."
+else
+  echo "test1 found expected flag."
+fi
+
+test2=spoo
+test_presence my_array $test2
+if [ $? != 0 ]; then
+  echo "test2 did not find flag, which is correct."
+else
+  echo "test2 found flag when should not have."
+fi
+
+#############################################
+
+#demo3 for sifting...
+
+declare -a sift_list=(ontrack selenium aggressive)
+declare -a stripping=(selenium)
+declare -a store_list=()
+
+sift_array "sift_list" "stripping" "store_list" 
+if [ ${store_list[0]} == "selenium" ]; then
+  echo "test3 found expected content in storage list."
+else
+  echo "test3 was missing expected content in storage list."
+fi
+echo sift is now ${sift_list[*]}
+if [ "${sift_list[*]}" == "ontrack aggressive" ]; then
+  echo "test3 found expected content in sifting list."
+else
+  echo "test3 was missing expected content in sifting list."
+fi
+
+
diff --git a/scripts/unit_test/test_assoc_array.sh b/scripts/unit_test/test_assoc_array.sh
new file mode 100644 (file)
index 0000000..49cf6ec
--- /dev/null
@@ -0,0 +1,65 @@
+#!/bin/bash
+
+##############
+
+# simple examples...
+
+declare -A snuggles
+  # make an associative array
+
+snuggles=([book]=petunia "[muffets]=glasgow robbery")
+  # keys: book and muffets
+  # values: (second part)
+
+snuggles+=([morgower]=flimshaw)
+  # adding entries to it.
+
+echo ${!snuggles[x]}
+  # show index x's key.
+
+echo ${snuggles[x]}
+  # show index x's value.
+
+##############
+
+# excellent code from:
+# http://blog.spencertipping.com/2009/08/constant-time-associative-arrays-in-bash
+typeset -a table_keys
+typeset -a table_values
+function index_of_key () {
+  initial=$(($(echo $1 | md5sum | cut -c 18-32 | awk '{print "0x"$1}')))
+  while [[ ${table_keys[$initial]} && ${table_keys[$initial]} != $1 ]]; do
+    initial=$((initial + 1))
+  done
+  echo -n $initial
+}
+function associate () {
+  index=$(index_of_key $1)
+  table_keys[$index]=$1
+  table_values[$index]=$2
+  echo -n $2
+}
+function lookup () {
+  index=$(index_of_key $1)
+  echo -n ${table_values[$index]}
+}
+echo Associating foo with bar and bif with baz
+associate foo bar && echo
+associate bif baz && echo
+echo -n Looking up foo:
+lookup foo && echo
+echo -n Looking up bif:
+lookup bif && echo
+echo -n Looking up bar:
+lookup bar && echo
+
+##############
+