6
6
# The argument is the name of the program to test.
7
7
# Example: $ ../../../ctest.sh myprogram.c
8
8
9
+ # Define some colors for output
9
10
RED=" \e[31m"
10
11
GREEN=" \e[32m"
11
- BLUE=" \e[34m"
12
- CYANBACK=" \e[46m"
12
+ BCYAN=" \e[96m"
13
13
MAGENTA=" \e[35m"
14
- BOLDBLUE =" \e[1;34m"
14
+ BBLUE =" \e[1;34m"
15
15
ENDCOLOR=" \e[0m"
16
+
17
+ function bcyan {
18
+ printf " ${BCYAN} $@ ${ENDCOLOR} "
19
+ }
20
+ function bblue {
21
+ printf " ${BBLUE} $@ ${ENDCOLOR} "
22
+ }
23
+ function red {
24
+ printf " ${RED} $@ ${ENDCOLOR} "
25
+ }
26
+ function green {
27
+ printf " ${GREEN} $@ ${ENDCOLOR} "
28
+ }
29
+ function magenta {
30
+ printf " ${MAGENTA} $@ ${ENDCOLOR} "
31
+ }
32
+
16
33
PASSED=0
17
34
18
35
if [ $# -eq 0 ]; then
@@ -49,14 +66,22 @@ if [ $LEN -eq 0 ]; then
49
66
exit 1
50
67
fi
51
68
52
- if [ -t 1 ]; then echo -e " ${CYANBACK} ==:: TEST RESULTS ::== ${ENDCOLOR} "
53
- else echo " ==:: TEST RESULTS ::==" ; fi
69
+ if [ -t 1 ]; then
70
+ echo -e $( bcyan " ┌──────────────────────────┐" )
71
+ echo -e " $( bcyan " │" ) $( bblue " TEST RESULTS" ) $( bcyan " │" ) "
72
+ echo -e $( bcyan " └──────────────────────────┘" )
73
+ else
74
+ echo -e " ┌──────────────────────────┐"
75
+ echo -e " │ TEST RESULTS │"
76
+ echo -e " └──────────────────────────┘"
77
+ fi
54
78
echo
55
79
56
80
# Compare the output of the program with the expected output
57
81
for INFILE in " ${INFILES[@]} " ; do
58
- if [ -t 1 ]; then echo -e " ${BOLDBLUE} Test ${INFILE: 8} ${ENDCOLOR} "
59
- echo -e " ${BOLDBLUE} ---------- ${ENDCOLOR} "
82
+ if [ -t 1 ]; then
83
+ echo -e $( bblue " Test ${INFILE: 8} " )
84
+ echo -e $( bblue " ----------" )
60
85
else echo -e " Test ${INFILE: 8} \n---------- " ; fi
61
86
OUTFILE=" ${INFILE% .* } .out"
62
87
if [ ! -f " $OUTFILE " ]; then
@@ -65,24 +90,40 @@ for INFILE in "${INFILES[@]}"; do
65
90
else
66
91
OUTPUT=$( ./a.out < " $INFILE " )
67
92
DIF=" $( diff -Z " $OUTFILE " <( echo " $OUTPUT " ) ) "
68
- EXPECTED=$( cat " $OUTFILE " )
69
93
if [ -n " $DIF " ]; then
70
- if [ -t 1 ]; then echo -e " ${RED} Test failed.${ENDCOLOR} "
71
- echo -e " Expected:\n ${GREEN}${EXPECTED// $' \n ' / $' \n ' }${ENDCOLOR} "
72
- echo -e " \nActual:\n ${RED}${OUTPUT// $' \n ' / $' \n ' }${ENDCOLOR} \n"
73
- else echo -e " Test failed.\nExpected:\n ${EXPECTED// $' \n ' / $' \n ' } "
74
- echo -e " \nActual:\n ${OUTPUT// $' \n ' / $' \n ' } \n" ; fi
94
+ if [ -t 1 ]; then echo -e $( red " Test failed." )
95
+ else echo -e " Test failed." ; fi
96
+ I=0; J=0
97
+ echo " $DIF " | while [[ $I -lt 6 ]] && read -r line; do
98
+ if [[ $line == " <" * ]]; then
99
+ if [ -t 1 ]; then echo -e " $( green " $line " ) "
100
+ else echo -e " $line " ; fi
101
+ J=$(( J+ 1 ))
102
+ elif [[ $line == " >" * ]]; then
103
+ if [ -t 1 ]; then echo -e " $( red " $line " ) "
104
+ else echo -e " $line " ; fi
105
+ I=$(( I+ 1 ))
106
+ elif [[ $line == * " c" * ]]; then
107
+ OUT=$( echo " $line " | cut -d " c" -f 1)
108
+ OUT=$( echo " $OUT " | sed ' s/,/-/g' )
109
+ echo -e " \n line " $OUT " "
110
+ fi
111
+ done
112
+ echo
75
113
else
76
- if [ -t 1 ]; then echo -e " ${GREEN} PASSED!${ENDCOLOR} \n"
114
+ if [ -t 1 ]; then
115
+ echo -e $( green " PASSED!" )
116
+ echo
77
117
else echo -e " PASSED!\n" ; fi
78
118
PASSED=$(( PASSED + 1 ))
79
119
fi
80
120
fi
81
121
done
82
122
83
123
# Check for memory issues with valgrind
84
- if [ -t 1 ]; then echo -e " ${BOLDBLUE} Valgrind test ${ENDCOLOR} "
85
- echo -e " ${BOLDBLUE} ------------- ${ENDCOLOR} "
124
+ if [ -t 1 ]; then
125
+ echo -e $( bblue " Valgrind test" )
126
+ echo -e $( bblue " ------------- " )
86
127
else echo -e " Valgrind test\n------------- " ; fi
87
128
88
129
if ! [ -x " $( command -v valgrind) " ]; then
@@ -92,13 +133,13 @@ else
92
133
CHECK1=$( echo " $TEST " | grep -c " in use at exit: 0 bytes in 0 blocks" )
93
134
CHECK2=$( echo " $TEST " | grep -c " 0 errors from 0 contexts" )
94
135
if [[ $CHECK1 -ne 0 && $CHECK2 -ne 0 ]]; then
95
- if [ -t 1 ]; then echo -e " ${GREEN} PASSED!${ENDCOLOR} "
136
+ if [ -t 1 ]; then echo -e $( green " PASSED!" )
96
137
else echo -e " PASSED!" ; fi
97
138
PASSED=$(( PASSED + 1 ))
98
139
else
99
- if [ -t 1 ]; then echo -e " ${RED} Test failed."
100
- if [ $CHECK1 -eq 0 ]; then echo -e " Not all memory freed." ; fi
101
- if [ $CHECK2 -eq 0 ]; then echo -e " Memory errors detected.${ENDCOLOR} " ; fi
140
+ if [ -t 1 ]; then echo -e $( red " Test failed." )
141
+ if [ $CHECK1 -eq 0 ]; then echo -e $( red " Not all memory freed." ) ; fi
142
+ if [ $CHECK2 -eq 0 ]; then echo -e $( red " Memory errors detected." ) ; fi
102
143
else echo -e " Test failed."
103
144
if [ $CHECK1 -eq 0 ]; then echo -e " Not all memory freed." ; fi
104
145
if [ $CHECK2 -eq 0 ]; then echo -e " Memory errors detected." ; fi
@@ -111,15 +152,15 @@ LEN=$((LEN + 1)) # Add 1 for valgrind test
111
152
112
153
# Print final result
113
154
if [ $PASSED -eq $LEN ]; then
114
- if [ -t 1 ]; then echo -e " ${GREEN} You have passed all tests! \(ᵔᵕᵔ)/${ENDCOLOR} "
155
+ if [ -t 1 ]; then echo -e $( green " You have passed all tests! \(ᵔᵕᵔ)/" )
115
156
else echo " All tests passed!" ; fi
116
157
elif [ $PASSED -eq $(( $LEN - 1 )) ]; then
117
- if [ -t 1 ]; then echo -e " ${MAGENTA} You have passed $PASSED out of $LEN tests."
118
- echo -e " Almost there...! (◎_◎)${ENDCOLOR} "
158
+ if [ -t 1 ]; then echo -e $( magenta " You have passed $PASSED out of $LEN tests." )
159
+ echo -e $( magenta " Almost there...! (◎_◎)" )
119
160
else echo -e " Passed $PASSED out of $LEN tests." ; fi
120
161
else
121
- if [ -t 1 ]; then echo -e " ${MAGENTA} You have passed $PASSED out of $LEN tests.
122
- (._.)${ENDCOLOR} "
162
+ if [ -t 1 ]; then echo -e $( magenta " You have passed $PASSED out of $LEN tests.
163
+ (._.)" )
123
164
else echo " Passed $PASSED out of $LEN tests." ; fi
124
165
fi
125
166
echo
0 commit comments