-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrevline.bats
More file actions
executable file
·69 lines (62 loc) · 1.73 KB
/
revline.bats
File metadata and controls
executable file
·69 lines (62 loc) · 1.73 KB
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
#!/usr/bin/env bats
#The revline function accepts as input an argument that
#is a sentence. It reverses the words in the sentence so that
#the last word in the input is the first word in the output,
#the second to last word in the input is the second word in
#the output, etc. In addition, it reverses
#the characters in each word.
#If the number of arguments is not 1, the function returns 1.
#Otherwise, the function returns 0.
revline() {
if [ "$#" -ne 1 ]; then return 1
fi
echo "$1" | tr " " "\n" > words.txt
declare -a arr
count=0
for ln in `cat words.txt`; do
count=$((count + 1))
arr[count]="$ln"
done
result=""
for ((i = $count; i >= 0; i --)); do
result=$result" "$(revword "${arr[i]}")
done
echo "$result" | sed -r 's/^ //g;s/ $//g'
return 0
}
revword()
{
echo "$1" | sed -r 's/./&\n/g' > temp.txt
declare -a arr
count=0
for ln in `cat temp.txt`; do
arr[count]="$ln"
count=$((count + 1))
done
result=""
for((i = $count; i >= 0; i --)); do
result=$result$(echo "${arr[i]}")
done
echo "$result"
}
#test on no arguments; should return 1
@test "revline" {
#revline with no argument
run revline
#assertions
[ $status -eq 1 ]
}
@test "revline hi there cs5521" {
#revline with argument
run revline 'hi there cs5521'
echo $output
[ "$output" == "1255sc ereht ih" ]
[ $status -eq 0 ]
}
@test "revline how is it going" {
#revline with argument
run revline 'how is it going'
echo $output
[ "$output" == "gniog ti si woh" ]
[ $status -eq 0 ]
}