Skip to content

Commit c98302e

Browse files
committed
🎉
1 parent 928069f commit c98302e

File tree

2 files changed

+66
-25
lines changed

2 files changed

+66
-25
lines changed

ctest.sh

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,30 @@
66
# The argument is the name of the program to test.
77
# Example: $ ../../../ctest.sh myprogram.c
88

9+
# Define some colors for output
910
RED="\e[31m"
1011
GREEN="\e[32m"
11-
BLUE="\e[34m"
12-
CYANBACK="\e[46m"
12+
BCYAN="\e[96m"
1313
MAGENTA="\e[35m"
14-
BOLDBLUE="\e[1;34m"
14+
BBLUE="\e[1;34m"
1515
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+
1633
PASSED=0
1734

1835
if [ $# -eq 0 ]; then
@@ -49,14 +66,22 @@ if [ $LEN -eq 0 ]; then
4966
exit 1
5067
fi
5168

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
5478
echo
5579

5680
# Compare the output of the program with the expected output
5781
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 "----------")
6085
else echo -e "Test ${INFILE:8}\n---------- "; fi
6186
OUTFILE="${INFILE%.*}.out"
6287
if [ ! -f "$OUTFILE" ]; then
@@ -65,24 +90,40 @@ for INFILE in "${INFILES[@]}"; do
6590
else
6691
OUTPUT=$(./a.out < "$INFILE")
6792
DIF="$(diff -Z "$OUTFILE" <(echo "$OUTPUT"))"
68-
EXPECTED=$(cat "$OUTFILE")
6993
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
75113
else
76-
if [ -t 1 ]; then echo -e "${GREEN}PASSED!${ENDCOLOR}\n"
114+
if [ -t 1 ]; then
115+
echo -e $(green "PASSED!")
116+
echo
77117
else echo -e "PASSED!\n"; fi
78118
PASSED=$((PASSED + 1))
79119
fi
80120
fi
81121
done
82122

83123
# 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 "------------- ")
86127
else echo -e "Valgrind test\n------------- "; fi
87128

88129
if ! [ -x "$(command -v valgrind)" ]; then
@@ -92,13 +133,13 @@ else
92133
CHECK1=$(echo "$TEST" | grep -c "in use at exit: 0 bytes in 0 blocks")
93134
CHECK2=$(echo "$TEST" | grep -c "0 errors from 0 contexts")
94135
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!")
96137
else echo -e "PASSED!"; fi
97138
PASSED=$((PASSED + 1))
98139
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
102143
else echo -e "Test failed."
103144
if [ $CHECK1 -eq 0 ]; then echo -e "Not all memory freed."; fi
104145
if [ $CHECK2 -eq 0 ]; then echo -e "Memory errors detected."; fi
@@ -111,15 +152,15 @@ LEN=$((LEN + 1)) # Add 1 for valgrind test
111152

112153
# Print final result
113154
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! \(ᵔᵕᵔ)/")
115156
else echo "All tests passed!"; fi
116157
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...! (◎_◎)")
119160
else echo -e "Passed $PASSED out of $LEN tests."; fi
120161
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+
(._.)")
123164
else echo "Passed $PASSED out of $LEN tests."; fi
124165
fi
125166
echo

example.jpg

105 KB
Loading

0 commit comments

Comments
 (0)