changeset 726:23f6d2993fa2

farray.sh: Test the debug output and the destruction of complex objects. BUGS: No formal ownership management is done yet. It is planned to implement resource management with reference counting.
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 06 Oct 2024 16:19:45 +0200
parents 33293795caa6
children 35c29e9919ba
files share/local-bsdtools/farray.sh tests/farray-alist.t tests/farray-array.t tests/farray-object.t
diffstat 4 files changed, 199 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/farray.sh	Sun Oct 06 05:56:27 2024 +0200
+++ b/share/local-bsdtools/farray.sh	Sun Oct 06 16:19:45 2024 +0200
@@ -1091,15 +1091,30 @@
 #: Args:
 #:   $1 (str): The name of the existing array.
 #:
+#: Other Parameters:
+#:   --unset-only: If given then no recursive deletion of other complex
+#:                 objects are done.
+#:
 farray_clear() {
-    local __farr_name
-
-    local __farr_token __farr_gvrname __farr_len __farr_idx
+    local __farr_name  __farr_opt_unset_only
+
+    local __farr_token __farr_gvrname __farr_len __farr_idx __farr_del_value
+
+    __farr_opt_unset_only="${1-}"
+    if [ "${__farr_opt_unset_only}" = '--unset-only' ] ; then
+        shift
+    else
+        __farr_opt_unset_only=''
+    fi
 
     _farr_array_get_meta "$@"
 
     __farr_idx=1
     while [ ${__farr_idx} -le ${__farr_len} ]; do
+        if [ -n "${__farr_opt_unset_only}" ] ; then
+            eval __farr_del_value=\"\$\{${__farr_gvrname}_${__farr_idx}\}\"
+            _farr_destroy_object "${__farr_del_value}"
+        fi
 	eval unset ${__farr_gvrname}_${__farr_idx}
 	__farr_idx=$((__farr_idx + 1))
     done
@@ -1123,14 +1138,15 @@
     local __farr_name
 
     local __farr_token __farr_gvrname __farr_len
-    local __farr_idx
-
-    if ! _farr_array_tryget_meta "$@" ; then
-        return 1
-    fi
+    local __farr_idx __farr_del_value
+
+    _farr_array_tryget_meta "$@" || return 1
+
     # Remove "storage"
     __farr_idx=1
     while [ ${__farr_idx} -le ${__farr_len} ]; do
+        eval __farr_del_value=\"\$\{${__farr_gvrname}_${__farr_idx}\}\"
+        _farr_destroy_object "${__farr_del_value}"
 	eval unset ${__farr_gvrname}_${__farr_idx}
 	__farr_idx=$((__farr_idx + 1))
     done
@@ -1555,7 +1571,21 @@
 #:   $4: The current indentation string
 #:
 _farr_debug_print_value() {
-    printf "%sDEBUG:     %s: \`%s'\\n" "$4" "$2" "$3" 1>&2
+    case "$3" in
+        "${_farr_array_token_prefix}"*)
+            printf "%sDEBUG:     %s: -->\\n" "$4" "$2" 1>&2
+            _farr_array_debug "$4    " "$3"
+            # let the return value pass through
+            ;;
+        "${_farr_alist_token_prefix}"*)
+            printf "%sDEBUG:     %s: -->\\n" "$4" "$2" 1>&2
+            _farr_alist_debug "$4    " "$3"
+            # let the return value pass through
+            ;;
+        *)
+            printf "%sDEBUG:     %s: \`%s'\\n" "$4" "$2" "$3" 1>&2
+            ;;
+    esac
     return 0
 }
 
@@ -1806,17 +1836,34 @@
 #: Args:
 #:   $1 (str): The name of the existing alist.
 #:
+#: Other Parameters:
+#:   --unset-only: If given then no recursive deletion of other complex
+#:                 objects are done.
+#:
 falist_clear() {
-    local __farr_name
+    local __farr_name __farr_opt_unset_only
 
     local __farr_token __farr_objname __farr_keyname __farr_valname __farr_len
-    local __farr_idx
+    local __farr_idx __farr_del_value
+
+    __farr_opt_unset_only="${1-}"
+    if [ "${__farr_opt_unset_only}" = '--unset-only' ] ; then
+        shift
+    else
+        __farr_opt_unset_only=''
+    fi
 
     _farr_alist_get_meta "$@"
 
     # Remove "storage"
     __farr_idx=1
     while [ ${__farr_idx} -le ${__farr_len} ]; do
+        if [ -n "${__farr_opt_unset_only}" ] ; then
+            eval __farr_del_value=\"\$\{${__farr_valname}_${__farr_idx}\}\"
+            _farr_destroy_object "${__farr_del_value}"
+            eval __farr_del_value=\"\$\{${__farr_keyname}_${__farr_idx}\}\"
+            _farr_destroy_object "${__farr_del_value}"
+        fi
         eval unset ${__farr_valname}_${__farr_idx}
         eval unset ${__farr_keyname}_${__farr_idx}
         __farr_idx=$((__farr_idx + 1))
@@ -1842,15 +1889,17 @@
     local __farr_name
 
     local __farr_token __farr_objname __farr_keyname __farr_valname __farr_len
-    local __farr_idx
-
-    if ! _farr_alist_tryget_meta "$@" ; then
-        return 1
-    fi
+    local __farr_idx __farr_del_value
+
+    _farr_alist_tryget_meta "$@" || return 1
 
     # Remove "storage"
     __farr_idx=1
     while [ ${__farr_idx} -le ${__farr_len} ]; do
+        eval __farr_del_value=\"\$\{${__farr_valname}_${__farr_idx}\}\"
+        _farr_destroy_object "${__farr_del_value}"
+        eval __farr_del_value=\"\$\{${__farr_keyname}_${__farr_idx}\}\"
+        _farr_destroy_object "${__farr_del_value}"
         eval unset ${__farr_valname}_${__farr_idx}
         eval unset ${__farr_keyname}_${__farr_idx}
         __farr_idx=$((__farr_idx + 1))
@@ -1884,6 +1933,10 @@
     # Remove "storage"
     __farr_idx=1
     while [ ${__farr_idx} -le ${__farr_len} ]; do
+        eval __farr_del_value=\"\$\{${__farr_valname}_${__farr_idx}\}\"
+        _farr_destroy_object "${__farr_del_value}"
+        eval __farr_del_value=\"\$\{${__farr_keyname}_${__farr_idx}\}\"
+        _farr_destroy_object "${__farr_del_value}"
         eval unset ${__farr_valname}_${__farr_idx}
         eval unset ${__farr_keyname}_${__farr_idx}
         __farr_idx=$((__farr_idx + 1))
@@ -2859,7 +2912,19 @@
         if [ \( -n "${__farr_el_key}" \) -a \( -n "${__farr_el_val}" \) ]; then
             eval __farr_el_key=\"\$\{${__farr_keyname}_${__farr_idx}\}\"
             eval __farr_el_val=\"\$\{${__farr_valname}_${__farr_idx}\}\"
-            printf "%sDEBUG:     \`%s' -> \`%s'\\n" "${__farr_debug_indent}" "${__farr_el_key}" "${__farr_el_val}" 1>&2
+            case "${__farr_el_val}" in
+                "${_farr_array_token_prefix}"*)
+                    printf "%sDEBUG:     \`%s': -->\\n" "${__farr_debug_indent}" "${__farr_el_key}" 1>&2
+                    _farr_array_debug "${__farr_debug_indent}    " "${__farr_el_val}"
+                    ;;
+                "${_farr_alist_token_prefix}"*)
+                    printf "%sDEBUG:     \`%s': -->\\n" "${__farr_debug_indent}" "${__farr_el_key}" 1>&2
+                    _farr_alist_debug "${__farr_debug_indent}    " "${__farr_el_val}"
+                    ;;
+                *)
+                    printf "%sDEBUG:     \`%s' -> \`%s'\\n" "${__farr_debug_indent}" "${__farr_el_key}" "${__farr_el_val}" 1>&2
+                    ;;
+            esac
         fi
         __farr_idx=$((__farr_idx + 1))
     done
--- a/tests/farray-alist.t	Sun Oct 06 05:56:27 2024 +0200
+++ b/tests/farray-alist.t	Sun Oct 06 16:19:45 2024 +0200
@@ -428,3 +428,22 @@
   $ LIST=''
   $ _farr_destroy_object "$LIST"
   $ check_no_alist_artifacts
+
+
+Complex Debug
+=============
+
+  $ falist_create LIST
+  $ falist_set LIST K1 V1
+  $ falist_create ITEM1 K11 V11 K22 V22
+# This also transfers ownership
+  $ falist_set LIST K2 "$ITEM1"
+  $ falist_debug LIST
+  DEBUG: alist `LIST' has length 2
+  DEBUG:     `K1' -> `V1'
+  DEBUG:     `K2': -->
+      DEBUG: alist with token `[a-f0-9]+' has length 2 (re)
+      DEBUG:     `K11' -> `V11'
+      DEBUG:     `K22' -> `V22'
+  $ falist_destroy LIST
+  $ check_no_alist_artifacts
--- a/tests/farray-array.t	Sun Oct 06 05:56:27 2024 +0200
+++ b/tests/farray-array.t	Sun Oct 06 16:19:45 2024 +0200
@@ -1151,3 +1151,24 @@
   $ TEST=''
   $ _farr_destroy_object "$TEST"
   $ check_no_array_artifacts
+
+
+Complex Debug
+=============
+
+  $ farray_create TEST i1 i2
+  $ farray_create ITEM1 i11 i22
+This also transfers ownership
+  $ farray_append TEST "$ITEM1"
+  $ farray_debug TEST
+  DEBUG: array `TEST' has length 3
+  DEBUG:   its contents:
+  DEBUG:     1: `i1'
+  DEBUG:     2: `i2'
+  DEBUG:     3: -->
+      DEBUG: array with token `[a-f0-9]+' has length 2 (re)
+      DEBUG:   its contents:
+      DEBUG:     1: `i11'
+      DEBUG:     2: `i22'
+  $ farray_destroy TEST
+  $ check_no_array_artifacts
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/farray-object.t	Sun Oct 06 16:19:45 2024 +0200
@@ -0,0 +1,77 @@
+Complex object tests of farray.sh
+
+Shell is /bin/sh.
+
+
+Setup
+=====
+
+  $ set -u
+  $ . "${TESTDIR}/testsetup.sh"
+  $ _p_datadir="${TESTDIR}/../share/local-bsdtools"
+  $ . "${_p_datadir}/farray.sh"
+
+
+Array
+=====
+
+  $ farray_create ARRAY1 i1 i2
+  $ farray_create ITEM1 i11 i22
+This also transfers ownership
+  $ farray_append ARRAY1 "$ITEM1"
+  $ ITEM1=''
+  $ falist_create LIST2 k1 v1 k2 v2 k3 v3
+This also transfers ownership
+  $ farray_append ARRAY1 "$LIST2"
+  $ LIST2=''
+  $ farray_debug ARRAY1
+  DEBUG: array `ARRAY1' has length 4
+  DEBUG:   its contents:
+  DEBUG:     1: `i1'
+  DEBUG:     2: `i2'
+  DEBUG:     3: -->
+      DEBUG: array with token `[a-f0-9]+' has length 2 (re)
+      DEBUG:   its contents:
+      DEBUG:     1: `i11'
+      DEBUG:     2: `i22'
+  DEBUG:     4: -->
+      DEBUG: alist with token `[a-f0-9]+' has length 3 (re)
+      DEBUG:     `k1' -> `v1'
+      DEBUG:     `k2' -> `v2'
+      DEBUG:     `k3' -> `v3'
+  $ farray_destroy ARRAY1
+  $ check_no_array_artifacts
+  $ check_no_alist_artifacts
+
+
+AList
+=====
+
+  $ falist_create LIST1 k1 v1
+  $ falist_create ITEM1 k11 v11 k22 v22 k33 v33 k44 v44
+This also transfers ownership
+  $ falist_set LIST1 k2 "$ITEM1"
+  $ ITEM1=''
+  $ farray_create ARRAY2 a1 a2 a3
+This also transfers ownership
+  $ falist_set LIST1 k3 "$ARRAY2"
+  $ ARRAY2=''
+  $ falist_debug LIST1
+  DEBUG: alist `LIST1' has length 3
+  DEBUG:     `k1' -> `v1'
+  DEBUG:     `k2': -->
+      DEBUG: alist with token `[a-f0-9]+' has length 4 (re)
+      DEBUG:     `k11' -> `v11'
+      DEBUG:     `k22' -> `v22'
+      DEBUG:     `k33' -> `v33'
+      DEBUG:     `k44' -> `v44'
+  DEBUG:     `k3': -->
+      DEBUG: array with token `[a-f0-9]+' has length 3 (re)
+      DEBUG:   its contents:
+      DEBUG:     1: `a1'
+      DEBUG:     2: `a2'
+      DEBUG:     3: `a3'
+
+  $ falist_destroy LIST1
+  $ check_no_array_artifacts
+  $ check_no_alist_artifacts