-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathlist_ops.bats
229 lines (199 loc) · 5.78 KB
/
list_ops.bats
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
#!/usr/bin/env bats
load bats-extra
# local version: 2.4.0.0
bash_version=$((10 * BASH_VERSINFO[0] + BASH_VERSINFO[1]))
if (( bash_version < 43 )); then
echo "This exercise requires at least bash version 4.3" >&2
exit 4
fi
## append entries to a list and return the new list
setup() { source list_ops.sh; }
@test "append empty lists" {
#[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list1=()
list2=()
list::append list1 "${list2[@]}"
assert_equal "${#list1[@]}" 0
assert_equal "${list1[*]}" ""
}
@test "append list to empty list" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list1=()
list2=(1 2 3 4)
list::append list1 "${list2[@]}"
assert_equal "${#list1[@]}" 4
assert_equal "${list1[*]}" "1 2 3 4"
}
@test "append empty list to list" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list1=(1 2 3 4)
list2=()
list::append list1 "${list2[@]}"
assert_equal "${#list1[@]}" 4
assert_equal "${list1[*]}" "1 2 3 4"
}
@test "append non-empty lists" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
l1=(1 2)
l2=(2 3 4 5)
list::append l1 "${l2[@]}"
assert_equal "${#l1[@]}" 6
assert_equal "${l1[*]}" "1 2 2 3 4 5"
}
## concatenate a list of lists
# N/A: bash arrays are strictly one-dimensional
## filter list returning only values that satisfy the filter function
@test "filter empty list" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=()
result=()
isOdd () { (( $1 % 2 == 1 )); }
list::filter isOdd list result
assert_equal "${#result[@]}" 0
assert_equal "${result[*]}" ""
}
@test "filter non-empty list" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=(1 2 3 4 5)
result=()
isOdd () { (( $1 % 2 == 1 )); }
list::filter isOdd list result
assert_equal "${#result[@]}" 3
assert_equal "${result[*]}" "1 3 5"
}
## returns the length of a list
# N/A: bash array length syntax covers it: ${#ary[@]}
## map: a list of elements whose values equal the list value
## transformed by the mapping function
@test "map empty list" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=()
result=()
incr () { echo $(( $1 + 1 )); }
list::map incr list result
assert_equal "${#result[@]}" ${#list[@]}
assert_equal "${result[*]}" ""
}
@test "map non-empty list" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=(1 3 5 7)
result=()
incr () { echo $(( $1 + 1 )); }
list::map incr list result
assert_equal "${#result[@]}" ${#list[@]}
assert_equal "${result[*]}" "2 4 6 8"
}
## folds (reduces) the given list from the left with a function
@test "foldl empty list" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=()
mult () {
local acc=$1 elem=$2
echo $(( elem * acc ))
}
result=$(list::foldl mult 2 list)
assert_equal "$result" "2"
}
@test "foldl direction independent function applied to non-empty list" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=(1 2 3 4)
add () {
local acc=$1 elem=$2
echo $(( elem + acc ))
}
result=$(list::foldl add 5 list)
assert_equal "$result" "15"
}
@test "foldl direction dependent function applied to non-empty list" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=(1 2 3 4)
# For this test, we need a div function that performs
# floating point arithmetic to preserve fractions.
div () {
local acc=$1 elem=$2
echo "$elem / $acc" | bc -l
}
answer=$(list::foldl div 24 list)
result=$(printf '%.1f' "$answer")
assert_equal "$result" "64.0"
}
# track-specific test
@test "foldl not just numbers" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=(H e l l o " " W o r l d "!")
concat () {
local acc=$1 elem=$2
echo "${acc}${elem}"
}
result=$(list::foldl concat "" list)
assert_equal "$result" 'Hello World!'
}
## folds (reduces) the given list from the right with a function
# Note the order of the arguments to the given functions!
@test "foldr empty list" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=()
mult () {
local elem=$1 acc=$2
echo $(( elem * acc ))
}
result=$(list::foldr mult 2 list)
assert_equal "$result" "2"
}
@test "foldr direction independent function applied to non-empty list" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=(1 2 3 4)
add () {
local elem=$1 acc=$2
echo $(( elem + acc ))
}
result=$(list::foldr add 5 list)
assert_equal "$result" "15"
}
@test "foldr direction dependent function applied to non-empty list" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=(1 2 3 4)
div () {
local elem=$1 acc=$2
echo "$elem / $acc" | bc -l
}
answer=$(list::foldr div 24 list)
result=$(printf '%.1f' "$answer")
assert_equal "$result" "9.0"
}
# track-specific test
@test "foldr not just numbers" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=(H e l l o " " W o r l d "!")
concat () {
local elem=$1 acc=$2
echo "${acc}${elem}"
}
result=$(list::foldr concat "" list)
assert_equal "$result" '!dlroW olleH'
}
## reverse the elements of the list
@test "reverse empty list" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=()
result=()
list::reverse list result
assert_equal "${#result[@]}" ${#list[@]}
assert_equal "${result[*]}" ""
}
@test "reverse non-empty list" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=(1 3 5 7)
result=()
list::reverse list result
assert_equal "${#result[@]}" ${#list[@]}
assert_equal "${result[*]}" "7 5 3 1"
}
@test "reverse with special characters" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
list=("R*" "l*")
result=()
list::reverse list result
assert_equal "${#result[@]}" ${#list[@]}
assert_equal "${result[*]}" "l* R*"
}