-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtest-list-utils.sh
executable file
·467 lines (404 loc) · 11.6 KB
/
test-list-utils.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/env bash
# test-list-utils.sh
#
# Copyright 2006-2022, Alan K. Stebbens <[email protected]>
#
# Test module for list-utils.sh
#
export PATH=.:$HOME/lib:$PATH
source list-utils.sh
source test-utils.sh
test_01_list_add() {
start_test
list1=()
list_add list1 foo
check_item list1 0 foo "list_add failed"
check_size list1 1 "list_add size test failed"
list_add list1 bar
check_size list1 2
check_item list1 0 foo
check_item list1 1 bar
end_test
}
test_02_list_sort() {
start_test
list2=()
words="now is the time for all good men to come to the aid of their country"
list_add list2 $words
check_size list2 16
sort_list list2
check_size list2 16
sorted_words="`sort_str \"$words\"`"
sorted_words2=`join_list list2 nowrap ' '`
check_equal "$sorted_words" "$sorted_words2"
check_unequal "$words" "$sorted_words2"
end_test
}
test_03_list_add_once() {
start_test
tlist=()
words="now is the time for all good men to come to the aid of their country"
list_add_once tlist $words
check_size tlist 14
list_add_once tlist $words
check_size tlist 14
end_test
}
test_04_list_insert() {
start_test
tlist=()
list_add tlist banana cherry
check_size tlist 2
list_insert tlist apple
check_size tlist 3
check_item tlist 0 apple
check_item tlist 1 banana
check_item tlist 2 cherry
end_test
}
test_05_list_insert_once() {
start_test
tlist=()
list_add_once tlist banana cherry
check_size tlist 2
list_insert_once tlist apple
check_size tlist 3
check_item tlist 0 apple
check_item tlist 1 banana
check_item tlist 2 cherry
list_insert_once tlist apple banana cherry
check_size tlist 3
list_insert_once tlist aardvark
check_size tlist 4
check_item tlist 0 aardvark
check_item tlist 1 apple
end_test
}
test_06_in_list() {
start_test
tlist=()
tlist2=()
list_add_once tlist foo bar baf
list_add_once tlist2 "foo bar" "bif baf" "+" "-" "/" "*"
check_size tlist 3
check_true "in_list tlist foo"
check_false "in_list tlist foo2"
check_true "in_list tlist -all foo bar"
check_false "in_list tlist -any foo1 bar2"
check_true "in_list tlist -all foo bar baf"
check_true "in_list tlist -any foo1 baf bar2"
check_false "in_list tlist -all foo bar baf gonzo"
check_true "in_list tlist foo1 baf bar2"
check_true "in_list tlist -any foo1 baf bar2"
check_false "in_list tlist -all foo1 baf bar2"
check_false "in_list tlist foo1 baf2 bar2"
# check weird matches
check_true "in_list tlist2 '+'"
check_false "in_list tlist2 '?'"
check_false "in_list tlist2 'bif'"
check_true "in_list tlist2 'bif baf'"
end_test
}
test_07_lookup_list() {
start_test
tlist1=( now is the 'time' 'for' all good men to come to the aid of their country )
item=`lookup_list tlist1 now`
code=$?
check_eq $code 0
check_equal "$item" 'now'
item=`lookup_list tlist1 'time'`
code=$?
check_eq $code 0
check_equal $item 'time'
item=`lookup_list tlist1 notfound`
code=$?
check_eq $code 1
check_equal "$item" ''
item=`lookup_list tlist1 the`
code=$?
check_eq $code 2
check_equal "$item" ''
item=`lookup_list tlist1 to`
code=$?
check_eq $code 2
check_equal "$item" ''
item=`lookup_list tlist1 goo`
code=$?
check_eq $code 0
check_equal "$item" 'good'
item=`lookup_list tlist1 go`
code=$?
check_eq $code 0
check_equal "$item" 'good'
item=`lookup_list tlist1 t`
code=$?
check_eq $code 2
check_equal "$item" ''
end_test
}
test_08_grep_list() {
start_test
tlist1=( now is the 'time' 'for' all good men to come to the aid of their country )
items=( `grep_list tlist1 now` ) # 1
code=$?
check_eq $code 0
check_size items 1
check_item_equal items 0 'now'
items=( `grep_list tlist1 'time'` ) # 2
code=$?
check_eq $code 0
check_size items 1
check_item_equal items 0 'time'
items=( `grep_list tlist1 notfound` ) # 3
code=$?
check_eq $code 1
check_size items 0
items=( `grep_list tlist1 the` ) # 4
code=$?
check_eq $code 0
check_size items 3
check_item_equal items 0 'the'
check_item_equal items 1 'the'
check_item_equal items 2 'their'
items=( `grep_list tlist1 to` ) # 5
code=$?
check_eq $code 0
check_size items 2
check_item_equal items 0 'to'
check_item_equal items 1 'to'
items=( `grep_list tlist1 goo` ) # 6
code=$?
check_eq $code 0
check_size items 1
check_item_equal items 0 'good'
items=( `grep_list tlist1 go` ) # 7
code=$?
check_eq $code 0
check_size items 1
check_item_equal items 0 'good'
items=( `grep_list tlist1 t` ) # 8
code=$?
check_eq $code 0
check_size items 7
check_item_equal items 0 'the'
check_item_equal items 1 'time'
check_item_equal items 2 'to'
check_item_equal items 3 'to'
check_item_equal items 4 'the'
check_item_equal items 5 'their'
check_item_equal items 6 'country'
items=( `grep_list tlist1 e` )
code=$?
check_eq $code 0
check_size items 6
check_item_equal items 0 'the'
check_item_equal items 1 'time'
check_item_equal items 2 'men'
check_item_equal items 3 'come'
check_item_equal items 4 'the'
check_item_equal items 5 'their'
end_test
}
test_09_push_pop_list() {
start_test
stack=()
check_size stack 0
push_list stack fubar
check_size stack 1
push_list stack tarfu
check_size stack 2
push_list stack snafu
check_size stack 3
pop_list stack
check_size stack 2
check_equal "$item" 'snafu'
pop_list stack
check_size stack 1
check_equal "$item" 'tarfu'
pop_list stack
check_size stack 0
check_equal "$item" 'fubar'
pop_list stack
code=$?
check_eq $code 1
check_equal "$item" ''
end_test
}
test_10_print_list() {
start_test
words=(
apple banana cherry dog elephant fox giraffe hawk indigo manzana milk november
october december january february march april may june july august
)
check_output plout1 "print_list words"
check_output plout2 "print_list words i=1"
check_output plout3 "print_list words i=2 c=4"
check_output plout4 "print_list words i=3 c=3"
check_output plout5 "print_list words i=1 c=2"
end_test
}
test_10a_print_list() {
start_test
workfiles=( '.environment*' '.cshrc*' '.aliases*' '.prompts*' '.bashrc*' '.inputrc'
'.profile' '.vim*' '.git-bash-prompt' 'src/github/aks/maximum-awesome' )
check_output plouta1 "print_list workfiles"
check_output plouta2 "print_list workfiles i=1"
check_output plouta3 "print_list workfiles i=1 c=2"
check_output plouta4 "print_list workfiles i=1 c=3"
check_output plouta5 "print_list workfiles i=2 c=1"
check_output plouta6 "print_list workfiles i=4 c=1"
end_test
}
test_11_join_list() {
start_test
list_init tlist
list_add tlist apple banana cherry
check_size tlist 3
check_output join_list_3 "join_list tlist"
check_output join_list_3sp "join_list tlist ' '"
check_output join_list_3tab "join_list tlist TAB"
check_output join_list_3and "join_list tlist AND"
check_output join_list_3or "join_list tlist OR"
check_output join_list_3nl "join_list tlist NL"
words=(
apple banana cherry dog elephant fox giraffe hawk indigo manzana milk november
october december january february march april may june july august
)
check_output join_list_words "join_list words"
check_output join_list_words_nowrap "join_list words NOWRAP"
end_test
}
test_12_map_list() {
start_test
words=( now is the 'time' 'for' all good men to come to the aid of their country )
num_words=`list_size words`
items=( `map_list words 'echo ${#item}'` )
check_size items $num_words
check_item_equal items 0 3
check_item_equal items 1 2
check_item_equal items 2 3
check_item_equal items 3 4
check_item_equal items 4 3
check_item_equal items 5 3
check_item_equal items 14 5
check_item_equal items 15 7
end_test
}
test_13_map_list_func_expr() {
start_test
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
words=( now is the 'time' 'for' all good men to come to the aid of their country )
num_words=`list_size words`
function count_vowels() {
local word="$1"
echo "$word" | sed -Ee $'s/(.)/\\1\\\n/g' | egrep -c '[aeiouy]'
}
items=( `map_list words count_vowels` )
check_size items $num_words
check_item_equal items 0 1
check_item_equal items 1 1
check_item_equal items 2 1
check_item_equal items 3 2
check_item_equal items 4 1
check_item_equal items 5 1
check_item_equal items 6 2
check_item_equal items 7 1
check_item_equal items 8 1
check_item_equal items 9 2
check_item_equal items 10 1
check_item_equal items 12 2
check_item_equal items 14 2
check_item_equal items 15 3
end_test
}
test_14_map_list_joinstr() {
start_test
words=( now is the "time" "for" all good men to come to the aid of their country )
gen_echo() { echo "unset \"$1\"" ; }
check_output map_list_joinstr "map_list words gen_echo \"\$CHAR_NL\""
end_test
}
test_16_map_list_exprs() {
start_test
numbers=( 0 1 2 3 4 5 6 7 8 9 )
squares=( `list_map numbers '( x * x )'` )
check_size numbers `list_size numbers`
check_item_equal squares 2 4
check_item_equal squares 3 9
check_item_equal squares 4 16
check_item_equal squares 5 25
check_item_equal squares 6 36
check_item_equal squares 7 49
check_item_equal squares 8 64
check_item_equal squares 9 81
end_test
}
test_18_reductions() {
start_test
words=( now is the 'time' 'for' all good men to come to the aid of their country )
num_words=`list_size words`
items=( `map_list words 'echo ${#item}'` )
count_chars_orig=`echo "${words[@]}" | wc -c`
count_chars=$(( `sum_list items` + num_words ))
check_eq $count_chars $count_chars_orig "sum_list count error; got $count_chars; should be $count_chars_orig"
min=`min_list items`
check_eq $min 2 "min test failed; got $min, should be 2"
max=`max_list items`
check_eq $max 7 "max test failed; got $max, should be 7"
avg=`avg_list items`
check_eq $avg 3 "avg test failed; got $avg, should be 3"
end_test
}
test_19_list_help_func() {
start_test
check_output list_help "list_help"
check_output list_init_help "list_init"
check_output list_init_nohelp "list_init foo"
check_output list_add_help "list_add"
check_output list_add_nohelp "list_add foo bar"
check_output list_add_once_help "list_add_once"
check_output list_get_help "list_get"
check_output list_get2_help "list_get nolist"
check_output list_item_help "list_item"
check_output list_item2_help "list_item nolist"
check_output list_push_help "list_push"
check_output list_push2_help "list_push nolist"
end_test
}
# test to make sure IFS is not changed by list_items and list_join, which alters it
test_20_list_IFS_check() {
start_test
local saveIFS="$IFS"
list_init foo
list_add foo now is the 'time' 'for' all good men to come to the aid
local tmpfile="/tmp/foo.$$"
list_items foo >$tmpfile
check_equal "$IFS" "$saveIFS"
join_list foo >$tmpfile
check_equal "$IFS" "$saveIFS"
end_test
}
# test the list_remove function
test_21_list_remove() {
start_test
list_init words
list_add words now is the 'time' 'for' all good men to come to the aid of their country
check_size words 16
list_remove words 'is'
# now the time for all good men to come to the aid of their country
check_size words 15
check_item words 1 'the'
list_remove words 'the'
# now time for all good men to come to aid of their country
check_size words 13
check_item words 1 'time'
list_remove words 'to'
# now time for all good men come aid of their country
check_size words 11
check_item words 6 'come'
end_test
}
init_tests "$@"
run_tests
summarize_tests
exit