-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest
executable file
·153 lines (117 loc) · 3.99 KB
/
test
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
#! /bin/sh
# Test suite for array
set -e
. "`pwd`/array"
# test array_indexof
A=`array -n ' bob' ross '' khan ross '/\#$^' ro`
idx=`printf '%s\n' "$A" | array_indexof -n` || { echo 'Index of -n failed' >&2; exit 1; }
if [ "$idx" -ne 0 ] ; then
echo "Index of -n, expected 0, got $idx" >&2
exit 1
fi
idx=`printf '%s\n' "$A" | array_indexof khan` || { echo 'Index of khan failed' >&2; exit 1; }
if [ "$idx" -ne 4 ] ; then
echo "Index of khan, expected 4, got $idx" >&2
exit 1
fi
if printf '%s\n' "$A" | array_indexof tim > /dev/null 2>&1 ; then
echo "Index of tim should fail, got `printf '%s\n' "$A" | array_indexof tim`" >&2
exit 1
fi
idx=`printf '%s\n' "$A" | array_indexof ''` || { echo 'Index of failed' >&2; exit 1; }
if [ "$idx" -ne 3 ] ; then
echo "Index of : expected 3, got $idx" >&2
exit 1
fi
idx=`printf '%s\n' "$A" | array_indexof ross` || { echo 'Index of ross failed' >&2; exit 1; }
if [ "$idx" -ne 2 ] ; then
echo "Index of ross: expected 2, got $idx" >&2
exit 1
fi
idx=`printf '%s\n' "$A" | array_indexof '/\#$^'` || { echo 'Index of /\#$^ failed' >&2; exit 1; }
if [ "$idx" -ne 6 ] ; then
echo "Index of /\#$^: expected 6, got $idx" >&2
exit 1
fi
idx=`printf '%s\n' "$A" | array_indexof ro` || { echo 'Index of ro failed' >&2; exit 1; }
if [ "$idx" -ne 7 ] ; then
echo "Index of ro: expected 7, got $idx" >&2
exit 1
fi
idx=`printf '%s\n' "$A" | array_indexof ' bob'` || { echo 'Index of bob failed' >&2; exit 1; }
if [ "$idx" -ne 1 ] ; then
echo "Index of bob: expected 1, got $idx" >&2
exit 1
fi
# test array_nth
A=`array -e ' bob' ross '' khan ross '/\#$^' ro`
val=`printf '%s\n' "$A" | array_nth 3` || { echo 'array_nth 3 failed' >&2; exit 1; }
if [ "$val" != '' ] ; then
echo "array_nth that should return the empty string, didn't." >&2
exit 1
fi
# test array_element_encode, array_element_decode
E='look
ma
three lines and a %'
expect_encoded_E='look%0Ama%0Athree lines and a %25'
really_encoded_E=`printf '%s\n' "$E" | array_element_encode`
if [ "$expect_encoded_E" != "$really_encoded_E" ]; then
echo "array_element_encode doesn't do the right thing" >&2
echo " wanted: $expect_encoded_E" >&2
echo " but got: $really_encoded_E" >&2
exit 1
fi
really_decoded_E=`echo "$really_encoded_E" | array_element_decode`
if [ "$E" != "$really_decoded_E" ]; then
echo "array_element_decode doesn't do the right thing" >&2
echo " wanted: $E" >&2
echo " but got: $really_decoded_E" >&2
exit 1
fi
A1=`array "$E"`
if [ "`printf '%s\n' "$A1" | array_nth 0`" != "$E" ]; then
echo "array_nth cannot handle encoded elements" >&2
exit 1
fi
if [ "`printf '%s\n' "$A1" | array_indexof "$E"`" -ne 0 ]; then
echo "array_indexof cannot handle encoded elements" >&2
exit 1
fi
# test array_append
A=`array_append "$A" next`
idx=`printf '%s\n' "$A" | array_indexof next` || { echo 'Index of next failed' >&2; exit 1; }
if [ "$idx" -ne 8 ] ; then
echo "Index of next: expected 8, got $idx" >&2
exit 1
fi
idx=`printf '%s\n' "$A" | array_indexof ' bob'` || { echo 'Index of bob failed' >&2; exit 1; }
if [ "$idx" -ne 1 ] ; then
echo "Index of bob: expected 1, got $idx" >&2
exit 1
fi
# test array_len
len=`printf '%s\n' "$A" | array_len` || { echo 'array_len failed' >&2; exit 1; }
if [ "$len" -ne 9 ] ; then
echo "array_len: expected 9, got $len" >&2
exit 1
fi
# test array_remove
A=`printf '%s\n' "$A" | array_remove next`
if printf '%s\n' "$A" | array_indexof next > /dev/null 2>&1; then
echo "Index of next should fail, got `printf '%s\n' "$A" | array_indexof next`" >&2
exit 1
fi
A=`array first second third first`
A=`printf '%s\n' "$A" | array_remove first`
idx=`printf '%s\n' "$A" | array_indexof first` || { echo 'Index of failed' >&2; exit 1; }
if [ "$idx" -ne 2 ] ; then
echo "Index of first: expected 2, got $idx" >&2
exit 1
fi
A=`printf '%s\n' "$A" | array_remove first`
if printf '%s\n' "$A" | array_indexof first > /dev/null 2>&1; then
echo "Index of first should fail, got `printf '%s\n' "$A" | array_indexof first`" >&2
exit 1
fi
echo 'Tests successful'