-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtester.sh
More file actions
executable file
·237 lines (206 loc) · 3.79 KB
/
tester.sh
File metadata and controls
executable file
·237 lines (206 loc) · 3.79 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
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
#!/bin/bash
# Load config
. config.env
# Compile push_swap
make -C $PS_LOCATION/ > /dev/null
# Setting the program location.
PUSH_SWAP="$PS_LOCATION/push_swap"
# Checking required files.
if ! test -f $PUSH_SWAP; then
echo $PUSH_SWAP does not exist!
exit
elif ! test -f $CHECKER; then
echo $CHECKER does not exist!
exit
fi
### Functions ###
orange() {
local orange_color='\033[38;5;214m' # ANSI escape code for orange color
local reset_color='\033[0m' # ANSI escape code to reset color
echo
echo -e "${orange_color}$1${reset_color}"
}
# Function to print OK in green
print_ok() {
echo $1 -e "[\033[32mOK\033[0m] "
}
# Function to print KO in red and failed numbers
print_ko() {
echo $2 -e "[\033[31mKO\033[0m] "
echo "Failed numbers: \"$1\"" >> "$LOGFILE"
if $PIPELINE; then
exit 1
fi
}
err_test() {
RES=$($PUSH_SWAP $1 2>&1)
if [ "$RES" != "Error" ]; then
print_ko "$1"
fi
print_ok $2
}
evaluate() {
if [ "$1" != "OK" ]; then
print_ko "$2" $3
fi
print_ok $3
}
# base test give "<number array>" as input add -n for no newline
base_test() {
RES=$($PUSH_SWAP $1 | $CHECKER $1)
if [ "$RES" != "OK" ]; then
print_ko "$1"
else
print_ok $2
fi
}
# Test loop for 10 times
do_tests() {
for ((i=1; i<=$1; i++)); do
ARG="$(ruby -e "puts (1..$2).to_a.shuffle.join(' ')")"
RES=$($PUSH_SWAP $ARG | $CHECKER $ARG)
if [ "$RES" != "OK" ]; then
print_ko "$ARG"
elif [ i != $1 ]; then
print_ok -n
fi
done
print_ok $3
}
info() {
echo "INFO: $1"
}
operations_benchmark() {
if ! $BENCHMARK; then
return
fi
if [ $# -eq 1 ]; then
LOOPS=1
else
LOOPS=$2
fi
MAX=0
for ((k=1; k<=$LOOPS; k++)); do
ARG="$(ruby -e "puts (1..$1).to_a.shuffle.join(' ')")"
RES=$($PUSH_SWAP $ARG | wc -l)
if [ $RES -gt $MAX ]; then
MAX=$RES
fi
done
echo "Max operations for $1 at $LOOPS tries: $MAX"
}
low_numbers() {
for ((j=1; j<=6; j++)); do
test_and_benchmark $j
done
}
test_and_benchmark() {
orange "Tests for $1 numbers"
do_tests 5 $1
operations_benchmark $1 $ROUNDS
}
random() {
orange "Random tests"
do_tests 1 42 -n
do_tests 1 139 -n
do_tests 1 64 -n
do_tests 1 33 -n
do_tests 1 92 -n
do_tests 1 99 -n
do_tests 1 512 -n
do_tests 1 477
}
input() {
# do the false input tests
orange "False input tests"
err_test "1 2 3 4 4" -n
err_test "1a 3 9" -n
err_test "a b c" -n
err_test "-1 12 one 42" -n
err_test "+0 -0"
# perfectly fine input
orange "Perfectly fine input"
base_test "-1 22 3"
# base_test "wrong"
}
timed() {
# hand sorting impossible? try 10.000 args
ARG="$(ruby -e "puts (1..10000).to_a.shuffle.join(' ')")"
orange "Now we will test your program with 10000 arguments"
# precision
RES=$($PUSH_SWAP $ARG | $CHECKER $ARG)
evaluate "$RES" "-10000 args"
# operations
operations_benchmark 10000
# time
echo -n "time: "
time $PUSH_SWAP $ARG > /dev/null
}
## Full
full_test() {
low_numbers
test_and_benchmark 100
test_and_benchmark 500
random
input
timed
}
### Read options ###
check_array=()
PIPELINE=false
BENCHMARK=false
TIMED=false
RANDOM_CHECK=false
INPUT=false
CHECK=false
LOW=false
EXIT=false
while getopts "ptriba:c:" option; do
case $option in
p)
PIPELINE=true ;; # Kill process when a fault is found.
b)
BENCHMARK=true
;;
a)
echo $OPTARG
ROUNDS=$OPTARG ;;
t)
TIMED=true ;;
r)
RANDOM_CHECK=true ;;
i)
INPUT=true ;;
c)
check_array+=($OPTARG)
CHECK=true ;;
l)
LOW=true ;;
\?)
EXIT=true ;;
esac
done
if $EXIT; then
echo Invalid option.
exit 2
fi
echo Starting tests..
if ! $LOW && ! $INPUT && ! $RANDOM_CHECK && ! $TIMED && ! $CHECK; then
full_test
else
if $LOW; then
low_numbers
fi
if $RANDOM_CHECK; then
random
fi
if $INPUT; then
input
fi
if $TIMED; then
timed
fi
for element in "${check_array[@]}"; do
test_and_benchmark $element
done
fi