comparison tests/farray-alist.t @ 604:45c47bc1f7d2

farray.sh: Moved all currently existing tests for alists into cram tests. Removed existing tests from farray.sh -- and put them into farray-alist.t.
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 18 Sep 2024 23:08:06 +0200
parents
children 96366e2075fe
comparison
equal deleted inserted replaced
603:d4e8474ecc8b 604:45c47bc1f7d2
1 Basic tests of farray.sh's falist_XXX functions
2
3 Shell is /bin/sh.
4
5
6 Setup
7 =====
8
9 $ set -u
10 $ . "${TESTDIR}/testsetup.sh"
11 $ _p_datadir="${TESTDIR}/../share/local-bsdtools"
12 $ . "${_p_datadir}/farray.sh"
13
14
15 Basic Creation and Destruction
16 ==============================
17
18 Create an empty alist
19
20 $ falist_create LIST
21 $ falist_length _i LIST
22 $ echo "$_i"
23 0
24 $ test "${_i}" -eq 0
25 $ falist_print_length LIST
26 0 (no-eol)
27
28 $ falist_debug LIST
29 DEBUG: alist `LIST' has length 0
30
31 $ falist_destroy LIST
32 $ ( falist_destroy LIST )
33 ERROR: falist `LIST' not created properly: token empty
34 [1]
35
36 $ check_no_alist_artifacts
37
38
39 Clear
40 =====
41
42 $ falist_create LIST
43 $ falist_set LIST K1 V1
44 $ falist_set LIST K2 V2
45 $ falist_debug LIST
46 DEBUG: alist `LIST' has length 2
47 DEBUG: `K1' -> `V1'
48 DEBUG: `K2' -> `V2'
49 $ falist_length _i LIST
50 $ echo "$_i"
51 2
52 $ falist_print_length LIST
53 2 (no-eol)
54
55 $ falist_clear LIST
56 $ falist_length _i LIST
57 $ echo "$_i"
58 0
59 $ falist_print_length LIST
60 0 (no-eol)
61
62 $ falist_destroy LIST
63 $ check_no_alist_artifacts
64
65
66 Get / Set / Contains
67 ====================
68
69 $ falist_create LIST
70 $ falist_set LIST K1 V1
71 $ falist_set LIST K2 V2
72 $ falist_debug LIST
73 DEBUG: alist `LIST' has length 2
74 DEBUG: `K1' -> `V1'
75 DEBUG: `K2' -> `V2'
76 $ falist_length _i LIST
77 $ echo "$_i"
78 2
79 $ falist_print_length LIST
80 2 (no-eol)
81
82 $ falist_set LIST K2 "V2 2"
83 $ falist_set LIST K3 $'" 111222333" \\\'444555 ' # '
84 $ falist_debug LIST
85 DEBUG: alist `LIST' has length 3
86 DEBUG: `K1' -> `V1'
87 DEBUG: `K2' -> `V2 2'
88 DEBUG: `K3' -> `" 111222333" \'444555 '
89 $ falist_length _i LIST
90 $ echo "$_i"
91 3
92 $ falist_print_length LIST
93 3 (no-eol)
94
95 $ falist_contains LIST K1
96 $ falist_contains LIST K
97 [1]
98 $ falist_get _var LIST K2
99 $ echo "$_var"
100 V2 2
101 $ falist_tryget _var LIST K1
102 $ echo "$_var"
103 V1
104 $ falist_tryget _i LIST K
105 [1]
106 $ _var="$(falist_print_length NON_EXISTING_LIST)"
107 ERROR: falist `NON_EXISTING_LIST' not created properly: token empty
108 $ echo "${_var}"
109 -1
110
111 $ falist_destroy LIST
112 $ check_no_alist_artifacts
113
114
115 Iteration
116 =========
117
118 ITERATE (manual indexing)
119
120 $ falist_create LIST
121 $ falist_set LIST K1 V1
122 $ falist_set LIST K2 "V2 2"
123 $ falist_set LIST K3 $'" 111222333" \\\'444555 ' # '
124
125 Iteration by indexing key and values separately
126
127 $ _i=1
128 > while falist_tryget_key_at_index _k LIST ${_i}; do
129 > # cannot fail under "normal" circumstances
130 > falist_tryget_value_at_index _v LIST ${_i}
131 > printf " KEY: \`%s', VAL: \`%s'\\n" "${_k}" "${_v}"
132 > _i=$((_i + 1))
133 > done
134 KEY: `K1', VAL: `V1'
135 KEY: `K2', VAL: `V2 2'
136 KEY: `K3', VAL: `" 111222333" \'444555 '
137
138 ITERATE (manual indexing over items)
139
140 $ _i=1
141 > while falist_tryget_item_at_index _k _v LIST ${_i}; do
142 > printf " KEY: \`%s', VAL: \`%s'\\n" "${_k}" "${_v}"
143 > _i=$((_i + 1))
144 > done
145 KEY: `K1', VAL: `V1'
146 KEY: `K2', VAL: `V2 2'
147 KEY: `K3', VAL: `" 111222333" \'444555 '
148
149 ITERATE (for each)
150
151 $ falist_for_each LIST $'printf "EACH: %s key \\`%s\\\', value \\`%s\\\' at idx %d\\n"' # `
152 EACH: LIST key `K1', value `V1' at idx 1
153 EACH: LIST key `K2', value `V2 2' at idx 2
154 EACH: LIST key `K3', value `" 111222333" \'444555 ' at idx 3
155
156 $ falist_clear LIST
157 $ falist_destroy LIST
158 $ falist_destroy LIST
159 ERROR: falist `LIST' not created properly: token empty
160 [1]
161
162
163 Deletion of keys
164 ================
165
166 $ falist_create LIST
167 $ falist_set LIST 'key 1' 'value 1'
168 $ falist_set LIST 'key 2' 'value 2'
169 $ falist_set LIST 'key 3' 'value 3'
170 $ falist_set LIST 'key 4' 'value 4'
171 $ falist_set LIST 'key 5' 'value 5'
172 $ falist_trydel LIST 'key 1'
173 $ falist_trydel LIST 'key 5'
174 $ falist_trydel LIST 'key 3'
175 $ falist_trydel LIST "non-existing key"
176 [1]
177 $ falist_print_length LIST
178 2 (no-eol)
179 $ falist_get _var LIST 'key 2'
180 $ printf '%s' "$_var"
181 value 2 (no-eol)
182 $ falist_get _var LIST 'key 4'
183 $ printf '%s' "$_var"
184 value 4 (no-eol)
185
186 $ falist_destroy LIST
187
188 $ check_no_alist_artifacts