-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester
executable file
·573 lines (555 loc) · 13.5 KB
/
tester
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#!/bin/sh
# TO DO
# salvare bene le 'abc'
# How to generate an array of prandom numbers
# abc=$(shuf -i 0-200000000 -n $how_many_numbers)
# DESCRIPTION
# this function changes the color of the number of moves
ft_set_color()
{
# set variables
local stack_size=$1
local moves=$2
if [ $stack_size -eq 100 ] && [ $moves -lt 701 ] || [ $stack_size -eq 500 ] && [ $moves -lt 5501 ]
then
MC=$GREEN
elif [ $stack_size -eq 100 ] && [ $moves -lt 901 ] || [ $stack_size -eq 500 ] && [ $moves -lt 7001 ]
then
MC=$CYAN
elif [ $stack_size -eq 100 ] && [ $moves -lt 1101 ] || [ $stack_size -eq 500 ] && [ $moves -lt 8501 ]
then
MC=$YELLOW
elif [ $stack_size -eq 100 ] && [ $moves -lt 1301 ] || [ $stack_size -eq 500 ] && [ $moves -lt 10001 ]
then
MC=$ORANGE
elif [ $stack_size -eq 100 ] && [ $moves -lt 1501 ] || [ $stack_size -eq 500 ] && [ $moves -lt 11501 ]
then
MC=$RED
elif [ $stack_size -eq 3 ] && [ $moves -lt 3 ] || [ $stack_size -eq 5 ] && [ $moves -lt 13 ]
then
MC=$GREEN
elif [ $stack_size -eq 3 ] || [ $stack_size -eq 5 ]
then
MC=$RED
else
MC=$WHITE
fi
}
ft_check_bonus()
{
local tests=0
local ok=0
local ko=0
# TEST 1
out1=$($C ciao come stai > out.log 2>&1; cat out.log)
out2=$($CP ciao come stai > out.log 2>&1; cat out.log | head -c 5)
if [ "$out1" = "$out2" ]
then
ok=$((ok + 1))
rm out.log
else
ko=$((ko + 1))
printf "$RED TEST 1 fail:$RESET '$CP ciao come stai'\n"
fi
tests=$((tests + 1))
# TEST 2
out1=$($C 1 2 3 "4 1 6" > out.log 2>&1; cat out.log)
out2=$($CP 1 2 3 "4 1 6" > out.log 2>&1; cat out.log | head -c 5)
if [ "$out1" = "$out2" ]
then
ok=$((ok + 1))
rm out.log
else
ko=$((ko + 1))
printf "$RED TEST 2 fail:$RESET '$CP 1 2 3 \"4 1 6\"'\n"
fi
tests=$((tests + 1))
# TEST 3
out1=$(echo "ra" | $C 3 1 2 > out.log 2>&1; cat out.log)
out2=$(echo "ra" | $CP 3 1 2 > out.log 2>&1; cat out.log | head -c 5)
if [ "$out1" = "$out2" ]
then
ok=$((ok + 1))
rm out.log
else
ko=$((ko + 1))
printf "$RED TEST 3 fail:$RESET 'echo "ra" | $CP 3 1 2'\n"
fi
tests=$((tests + 1))
# TEST 4
out1=$(echo "sa" | $C 3 1 2 > out.log 2>&1; cat out.log)
out2=$(echo "sa" | $CP 3 1 2 > out.log 2>&1; cat out.log | head -c 5)
if [ "$out1" = "$out2" ]
then
ok=$((ok + 1))
rm out.log
else
ko=$((ko + 1))
printf "$RED TEST 4 fail:$RESET '"sa" | $CP 3 1 2'\n"
fi
tests=$((tests + 1))
# TEST 5
out1=$($C > out.log 2>&1; cat out.log)
out2=$($CP > out.log 2>&1; cat out.log | head -c 5)
if [ "$out1" = "$out2" ]
then
ok=$((ok + 1))
rm out.log
else
ko=$((ko + 1))
printf "$RED TEST 5 fail:$RESET '$CP'\n"
fi
tests=$((tests + 1))
# TEST 6
out1=$(printf "" | $C 1 2 3 > out.log 2>&1; cat out.log)
out2=$(printf "" | $CP 1 2 3 > out.log 2>&1; cat out.log | head -c 5)
if [ "$out1" = "$out2" ]
then
ok=$((ok + 1))
rm out.log
else
ko=$((ko + 1))
printf "$RED TEST 6 fail:$RESET 'printf \"\" | $CP 1 2 3'\n"
fi
tests=$((tests + 1))
# TEST 7
out1=$(printf "" | $C 3 1 2 > out.log 2>&1; cat out.log)
out2=$(printf "" | $CP 3 1 2 > out.log 2>&1; cat out.log | head -c 5)
if [ "$out1" = "$out2" ]
then
ok=$((ok + 1))
rm out.log
else
ko=$((ko + 1))
printf "$RED TEST 7 fail:$RESET 'printf \"\" | $CP 3 1 2'\n"
fi
tests=$((tests + 1))
# TEST 8
out1=$(printf "" | $C "" 1 > out.log 2>&1; cat out.log)
out2=$(printf "" | $CP "" 1 > out.log 2>&1; cat out.log | head -c 5)
if [ "$out1" = "$out2" ]
then
ok=$((ok + 1))
rm out.log
else
ko=$((ko + 1))
printf "$RED TEST 8 fail:$RESET 'printf \"\" | $CP \"\" 1'\n"
fi
tests=$((tests + 1))
# TEST 9
out1=$($P 3 1 2 | $C 3 1 2 > out.log 2>&1; cat out.log)
out2=$($P 3 1 2 | $CP 3 1 2 > out.log 2>&1; cat out.log | head -c 5)
if [ "$out1" = "$out2" ]
then
ok=$((ok + 1))
rm out.log
else
ko=$((ko + 1))
printf "$RED TEST 9 fail:$RESET '$P 3 1 2 | $CP 3 1 2'\n"
fi
tests=$((tests + 1))
# TEST 10
out1=$($P 3 1 2 4 6 5 7 9 8 | $C 3 1 2 4 6 5 7 9 8 > out.log 2>&1; cat out.log)
out2=$($P 3 1 2 4 6 5 7 9 8 | $CP 3 1 2 4 6 5 7 9 8 > out.log 2>&1; cat out.log | head -c 5)
if [ "$out1" = "$out2" ]
then
ok=$((ok + 1))
rm out.log
else
ko=$((ko + 1))
printf "$RED TEST 10 fail:$RESET '$P 3 1 2 4 6 5 7 9 8 | $CP 3 1 2 4 6 5 7 9 8'\n"
fi
tests=$((tests + 1))
# TEST 11
out1=$($P 3 1 2 4 6 5 7 8 9 | $C 3 1 2 4 6 5 7 9 8 > out.log 2>&1; cat out.log)
out2=$($P 3 1 2 4 6 5 7 8 9 | $CP 3 1 2 4 6 5 7 9 8 > out.log 2>&1; cat out.log | head -c 5)
if [ "$out1" = "$out2" ]
then
ok=$((ok + 1))
rm out.log
else
ko=$((ko + 1))
printf "$RED TEST 11 fail:$RESET '$P 3 1 2 4 6 5 7 8 9 | $CP 3 1 2 4 6 5 7 9 8'\n"
fi
tests=$((tests + 1))
printf "$PURPLE Tests: $WHITE$tests$PURPLE ok: $GREEN$ok$RESET"
if [ $ko -gt 0 ]
then
printf "$PURPLE ko: $RED$ko$RESET\n"
else
printf "\n"
fi
}
ft_check_mandatory()
{
for i in 3 5 100 500
do
tests=0
ok=0
ko=0
max=0
if [ $i -eq 3 ]
then
nums=15
elif [ $i -eq 5 ]
then
nums=50
elif [ $i -eq 100 ]
then
nums=25
else
nums=10
fi
for _ in $(seq 1 $nums)
do
abc=$(python3 generator.py $i)
echo "$abc\n" >> data.log
output=$($P $abc | $C $abc)
tests=$((tests + 1))
if [ $output = "OK" ]
then
ok=$((ok + 1))
moves=$($P $abc | wc -l | awk '{print $1}')
if [ $moves -gt $max ]
then
max=$moves
fi
elif [ $output = "KO" ]
then
ko=$((ko + 1))
fi
done
if [ $ok -eq $tests ]
then
ft_set_color $i $max
fi
if [ $ko -gt 0 ]
then
ko="$PURPLE KO: $RED$ko$RESET"
else
ko=""
fi
printf "$PURPLE Tests: $WHITE$tests$RESET$PURPLE Size: $CYAN$i$RESET$PURPLE OK: $MC$ok$RESET$ko\n"
done
}
ft_check_custom()
{
local stack_size=$1
local test=$2
tests=0
ok=0
ko=0
max=0
rm data.log 2>/dev/null
for _ in $(seq 1 $test)
do
abc=$(python3 generator.py $stack_size)
echo "$abc\n" >> data.log
output=$($P $abc | $C $abc)
tests=$((tests + 1))
if [ $output = "OK" ]
then
ok=$((ok + 1))
move=$($P $abc | wc -l | awk '{print $1}')
if [ $move -gt $max ]
then
max=$move
fi
elif [ $output = "KO" ]
then
ko=$((ko + 1))
fi
done
if [ $ok -eq $tests ]
then
ft_set_color $stack_size $max
fi
if [ $ko -gt 0 ]
then
ko="$PURPLE KO: $RED$ko$RESET"
else
ko=""
fi
printf "$PURPLE Tests: $WHITE$tests$RESET$PURPLE Size: $CYAN$stack_size$RESET$PURPLE OK: $MC$ok$RESET$ko$PURPLE Max: $MC$max$RESET\n"
}
ft_check_range ()
{
local start=$1
local end=$2
local tests=$3
for size in $(seq $start $end)
do
ft_check_custom $size $tests
done
}
ft_check_executables()
{
# check executables existence
if ! [ -e $P ]
then
printf "'$P' executable not found\n"
printf "Try with 'make -C ..'\n"
exit 1
elif ! [ -e $C ]
then
printf "'$C' executable not found\n"
printf "Try cloning the repository again\n"
exit 1
elif [ $1 -eq 2 ] && ! [ -e $CP ]
then
printf "'$CP' executable not found\n"
printf "Try with 'make bonus -C ..'\n"
exit 1
fi
# check executables permissions
if [ "$(uname)" = "Linux" ] && ! [ $(stat -c %A $P | tr -cd x | wc -c) -eq 3 ]
then
printf "'$P' executable must have execution permissions\n"
printf "Try with 'chmod +x $P'\n"
exit 1
elif ! [ "$(uname)" = "Linux" ] && ! [ $(stat $P | awk '{print $3}' | tr -cd x | wc -c) -eq 3 ]
then
printf "'$P' executable must have execution permissions\n"
printf "Try with 'chmod +x $P'\n"
exit 1
elif [ "$(uname)" = "Linux" ] && ! [ $(stat -c %A $C | tr -cd x | wc -c) -eq 3 ]
then
printf "'$C' executable must have execution permissions\n"
printf "Try with 'chmod +x $C'\n"
exit 1
elif ! [ "$(uname)" = "Linux" ] && ! [ $(stat $C | awk '{print $3}' | tr -cd x | wc -c) -eq 3 ]
then
printf "'$C' executable must have execution permissions\n"
printf "Try with 'chmod +x $C'\n"
exit 1
elif [ "$(uname)" = "Linux" ] && [ $1 -eq 2 ] && ! [ $(stat -c %A $CP | tr -cd x | wc -c) -eq 3 ]
then
printf "'$CP' executable must have execution permissions\n"
printf "Try with 'chmod +x $CP'\n"
exit 1
elif ! [ "$(uname)" = "Linux" ] && [ $1 -eq 2 ] && ! [ $(stat $CP | awk '{print $3}' | tr -cd x | wc -c) -eq 3 ]
then
printf "'$CP' executable must have execution permissions\n"
printf "Try with 'chmod +x $CP'\n"
exit 1
fi
}
# blue cyan yellow orange red
# 38;5;21 38;5;81 38;5;226 38;5;202 38;5;124
P="./../push_swap"
CP="./../checker"
# set checker_name
if [ $(uname) = "Linux" ]
then
C="./checker_linux"
else
C="./checker_Mac"
fi
# check shell that executed the script
shell_name=$(ps -p $$ -o command | tail -1 | awk '{print $1}')
slashes=$(($(echo $shell_name | tr -cd / | wc -c | awk '{print $1}') + 1))
shell_name=$(echo $shell_name | cut -d/ -f $slashes)
# set colors
if [ $shell_name = "sh" ] || [ $shell_name = "zsh" ]
then
BLUE="\033[38;5;21m"
CYAN="\033[38;5;81m"
GREEN="\033[38;5;40m"
ORANGE="\033[38;5;202m"
PURPLE="\033[38;5;105m"
RED="\033[38;5;124m"
WHITE="\033[38;5;231m"
YELLOW="\033[38;5;226m"
RESET="\033[0m"
MC="\033[37m"
else
BLUE=""
CYAN=""
GREEN=""
ORANGE=""
PURPLE=""
RED=""
WHITE=""
YELLOW=""
RESET=""
MC=""
fi
# check flags
case $1 in
-h|--help)
printf "PUSH_SWAP_TESTER\n"
printf "This is a simple script that tests your push_swap program.\n"
printf "It's easy to use and works on Linux and MacOS.\n"
printf "\n"
printf "OPTIONS\n"
printf " ./tester m checks for the mandatory part.\n"
printf " ./tester b checks for the bonus part.\n"
printf " ./tester a checks for both, mandatory and bonus.\n"
printf " ./tester c allows you to do custom tests.\n"
printf " ./tester r allows you to do custom tests in a range of numbers.\n"
printf " -h, --help display this help and exit.\n"
printf "\n"
printf "OUTPUT\n"
printf "Tests: [1] Size: [2] OK: [3] KO: [4]\n"
printf "1) is the the numbers of tests executed.\n"
printf "2) is the size of the stack used in those tests.\n"
printf "3) is the number of the successfull tests and if you pass all of them then\n"
printf " this number will be colored on the base of the evaluation sheet.\n"
printf "4) is the number of the failed tests (if there aren't, you won't see this number).\n"
printf "\n"
printf "The colors that ok can be are these:\n"
printf "$RED RED$RESET it's for grade 1/5\n"
printf "$ORANGE ORANGE$RESET it's for grade 2/5\n"
printf "$YELLOW YELLOW$RESET it's for grade 3/5\n"
printf "$CYAN BLUE$RESET it's for grade 4/5\n"
printf "$GREEN GREEN$RESET it's for grade 5/5\n"
printf "$WHITE WHITE$RESET it's for everything that's not on the evaluation sheet.\n"
printf "\n"
printf "EXAMPLES\n"
printf "./tester c 500 30\n"
printf " $PURPLE Tests:$WHITE 30$PURPLE Size:$CYAN 500$PURPLE OK:$YELLOW 30$PURPLE Max:$YELLOW 8643$RESET\n"
printf "\n"
printf "AUTHOR\n"
printf "Written by adi-stef aka Aldisti <https://github.com/Aldisti>.\n"
printf "\n"
printf "REPORTING BUGS\n"
printf "To report bugs you can write me on slack: adi-stef.\n"
exit 0
;;
m)
shift
if ! [ $# -eq 0 ]
then
printf "'m' option doesn't expect any arguments\n"
exit 1
fi
ft_check_executables 1
# do mandatory
ft_check_mandatory
;;
b)
shift
if ! [ $# -eq 0 ]
then
printf "'b' option doesn't expect any arguments\n"
exit 1
fi
ft_check_executables 2
# do bonus
ft_check_bonus
;;
a)
shift
if ! [ $# -eq 0 ]
then
printf "'a' option doesn't expect any arguments\n"
exit 1
fi
# do both
ft_check_executables 2
ft_check_mandatory
ft_check_bonus
;;
c)
shift
if ! [ $# -eq 2 ]
then
printf "'c' option expects only 2 arguments\n"
exit 1
fi
if [ "$1" = "" ] || [ "$2" = "" ]
then
printf "You have to insert 2 numbers as argument\n"
printf "Command sould be of this type:\n"
printf "./tester c [stack_size] [number_of_tests]\n"
exit 1
fi
[ -n "$1" ] && [ "$1" -eq "$1" ] 2>/dev/null
if ! [ $? -eq 0 ]
then
printf "Input arguments must be integers\n"
exit 1
fi
[ -n "$2" ] && [ "$2" -eq "$2" ] 2>/dev/null
if ! [ $? -eq 0 ]
then
printf "Input arguments must be integers\n"
exit 1
fi
if [ $1 -lt 0 ] || [ $2 -lt 0 ]
then
printf "Input arguments must be greater than zero\n"
exit 1
fi
# do custom
ft_check_custom $1 $2
;;
r)
shift
if ! [ $# -eq 3 ]
then
printf "'r' option expects only 2 arguments\n"
exit 1
fi
if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]
then
printf "You have to insert 2 numbers as argument\n"
printf "Command sould be of this type:\n"
printf "./tester r [stack_size] [number_of_tests]\n"
exit 1
fi
[ -n "$1" ] && [ "$1" -eq "$1" ] 2>/dev/null
if ! [ $? -eq 0 ]
then
printf "Input arguments must be integers\n"
exit 1
fi
[ -n "$2" ] && [ "$2" -eq "$2" ] 2>/dev/null
if ! [ $? -eq 0 ]
then
printf "Input arguments must be integers\n"
exit 1
fi
[ -n "$3" ] && [ "$3" -eq "$3" ] 2>/dev/null
if ! [ $? -eq 0 ]
then
printf "Input arguments must be integers\n"
exit 1
fi
if [ $1 -lt 0 ] || [ $2 -lt 0 ] || [ $3 -lt 0 ]
then
printf "Input arguments must be greater than zero\n"
exit 1
fi
# do custom range
ft_check_range $1 $2 $3
;;
l)
shift
if ! [ $# -eq 0 ]
then
printf "'l' option doesn't expect any arguments\n"
exit 1
fi
printf "$RED RED$RESET is for grade 1/5\n"
printf "$ORANGE ORANGE$RESET is for grade 2/5\n"
printf "$YELLOW YELLOW$RESET is for grade 3/5\n"
printf "$CYAN BLUE$RESET is for grade 4/5\n"
printf "$GREEN GREEN$RESET is for grade 5/5\n"
exit 0
;;
-*)
printf "'$1': invalid option\n"
printf "Try with './tester --help' for more information\n"
exit 1
;;
"")
printf "Try with './tester --help' for more information\n"
exit 1
;;
*)
printf "'$1': bad argument\n"
printf "Try with './tester --help' for more information\n"
exit 1
;;
esac