-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpe
More file actions
141 lines (100 loc) · 2.5 KB
/
pe
File metadata and controls
141 lines (100 loc) · 2.5 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
#!/bin/bash
# Program Name: pe
# Title: Program Evaluator
# Purpose: Evalute the program for given input and compare the results with the required output. This script is written
# keeping Compettive Programming Competitions in mind.
#-------------------------------------------------------------------------------------------------------------------------
# Colors
red='\033[37;41m'
brown='\033[37;43m'
green='\033[37;42m'
# Variables
program=
input=
output=
# Creating tmp directory in $HOME if it does not exists
[[ -d $HOME/tmp ]] || mkdir $HOME/tmp
# Creating tmp file in $HOME/tmp directory
programOutput=$(mktemp $HOME/tmp/pe.$$.XXXXXXXXXXXXXXXXXX)
#Functions
usage () {
cat <<- eof
Program Name: pe
Title: Program Evaluator
Usage: pe [ -i ] PROGRAM INPUT_FILE OUTPUT_FILE
Purpose: Evalute the program for given input and compare the results with the required output. This script is written
keeping Competitive Programming Competitions in mind.
Options:
-i Show incorrect results only
-h Show help
eof
}
# debug function
log () {
echo "Debug: $1"
}
# validating correct number of positional parameters and assigning values
if [[ "$1" == "-h" ]]; then
usage
exit 0;
fi
if [[ "$#" == "4" ]]; then
if [[ "$1" == "-i" ]]; then
incorrectOnly=1
else
usage 1>&2
exit 1
fi
program=$2
input=$3
output=$4
elif [[ "$#" == "3" ]]; then
program=$1
input=$2
output=$3
else
usage 1>&2
exit 1
fi
# validating variables and their respective permissions
if [[ ! -x $program ]]; then
echo "pe: Cannot execute '$program' or file does not exists" 1>&2
exit 2
fi
if [[ ! -r $input ]]; then
echo "pe: Cannot read '$input' or file does not exists" 1>&2
exit 3
fi
if [[ ! -r $output ]]; then
echo "pe: Cannot read '$output' or file does not exists" 1>&2
exit 4
fi
if [[ ! -w $programOutput ]]; then
##log "cant create tmp output file"
exit -1
fi
# Executing the PROGRAM while redirecting input and output to respective files
$program < $input > $programOutput
# comparing output files and displaying output
lines=$(wc -l $input)
lines=${lines%%" $input"}
echo -e "\n${brown}Input\tOutput\tProgram Output\033[0m"
for (( n = 0; n < $lines; ++n));
do
IFS='' read -r i <&3
IFS='' read -r o <&4
IFS='' read -r po <&5
color=
if [[ "${o}" == "${po}" ]]; then
color=$green
if [[ $incorrectOnly == 1 ]]; then
continue
fi
else
color=$red
fi
printf "${color}%5s\t%6s\t%14s\033[0m\n" $i $o $po
done 3<$input 4<$output 5<$programOutput
echo -e "\033[0m"
# Removing temp file
rm $progamOutput