-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_forward.sh
executable file
·153 lines (117 loc) · 3.68 KB
/
test_forward.sh
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
#!/bin/bash
CC="gcc"
CFLAGS="-std=c17 -Wall -Wextra -Wno-implicit-fallthrough -g"
CFALGS_WRAP="-Wl,--wrap=malloc -Wl,--wrap=calloc -Wl,--wrap=realloc
-Wl,--wrap=reallocarray -Wl,--wrap=free -Wl,--wrap=strdup -Wl,--wrap=strndup"
VALGRIND_FLAGS="--leak-check=full --show-leak-kinds=all
--errors-for-leak-kinds=all --quiet"
TEST_DIR="testy_forward"
BOLD=$(tput bold)
NORMAL=$(tput sgr0)
C_RED="\033[0;31m"
C_DEFAULT="\033[0m"
SKIP_CMAKE_FLAG=""
RUN_VALGRIND_FLAG=0
# Function definitions
print_usage() {
header="Usage: $0 [options] <path/to/src>"
options="Options:
-h show this help message
-c remove test files
-m skip cmake tests
-v run tests with valgrind"
printf "%s\n%s\n" "$header" "$options"
}
clean() {
printf "Removing test files...\n"
FILES=$(find $TEST_DIR -type f -name "*.o")
rm -f $FILES
}
test_cmake() {
# Test CMake Release
mkdir tmp_cmake_$1_forward_test
cd tmp_cmake_$1_forward_test || exit
cmake -D CMAKE_BUILD_TYPE=$1 ../$SRC_DIR/.. > /dev/null
make > /dev/null
printf "[OK] TEST CMAKE $1\n"
./phone_forward >temp_message
make clean > temp_message
rm temp_message
cd ..
rm -r -f tmp_cmake_$1_forward_test
}
# Check flags
while getopts 'chsvm' FLAG; do
case "${FLAG}" in
c) clean; exit 0;;
h) print_usage; exit 0;;
m) SKIP_CMAKE_FLAG="m";;
v) RUN_VALGRIND_FLAG=1;;
*) print_usage; exit 1;;
esac
done
# Check number of arguments
[[ $# < 1 ]] && print_usage && exit 1
SRC_DIR=${@:$#}
# Notify user if skipping tests
[ "$SKIP_CMAKE_FLAG" == "m" ] && printf "Skipping tests for cmake\n"
[ $RUN_VALGRIND_FLAG == 1 ] && printf "Running tests with valgrind\n"
# Check if SRC_DIR exists
[ ! -d "$SRC_DIR" ] && printf "Directory %s does not exist!\n" "$SRC_DIR" && exit 1
##############################################################################
# cmake/doc tests #
##############################################################################
[ "$SKIP_CMAKE_FLAG" == "m" ] || test_cmake ''
[ "$SKIP_CMAKE_FLAG" == "m" ] || test_cmake 'Release'
[ "$SKIP_CMAKE_FLAG" == "m" ] || test_cmake 'Test'
# Compile and run tests
SRC_FILES=()
for SRC_FILE in $SRC_DIR/*; do
if [ -d $SRC_FILE ]
then
for SUB_SRC_FILE in $SRC_FILE/*.c; do
SRC_FILES+="${SUB_SRC_FILE} "
done
elif [[ $SRC_FILE == *.c ]]
then
SRC_FILE_NAME=$(basename -- "$SRC_FILE")
if [[ "$SRC_FILE_NAME" != "phone_forward_example.c" && "$SRC_FILE_NAME" != "phone_forward_tests.c" ]]
then
SRC_FILES+="${SRC_FILE} "
fi
fi
done
TEST_FILES=$(find $TEST_DIR -type f -name "*.c")
for TEST in $TEST_FILES
do
echo -e "\n${BOLD}========= Running test ${TEST} =========${NORMAL}\n"
echo -n "Compiling... "
$CC $CFLAGS -o ${TEST%.c}.o $TEST $SRC_FILES >/dev/null
[ "$?" -ne 0 ] && printf "${C_RED}Compilation error${C_DEFAULT}\n" && exit 1
echo -e "done"
if [[ $RUN_VALGRIND_FLAG == 1 ]]
then
valgrind $VALGRIND_FLAGS ./${TEST%.c}.o
else
time ./${TEST%.c}.o
fi
done
##############################################################################
# Instrumented tests #
##############################################################################
INSTR_OUT="$TEST_DIR/instrumented.o"
INSTR_IN="$TEST_DIR/official_tests.c"
if [ -f $INSTR_IN ]
then
echo -e "\n${BOLD}========= Running instrumented =========${NORMAL}\n"
echo -n "Compiling... "
$CC $CFLAGS $CFALGS_WRAP -o $INSTR_OUT $INSTR_IN $SRC_FILES >/dev/null
[ "$?" -ne 0 ] && printf "${C_RED}Compilation error${C_DEFAULT}\n" && exit 1
echo -e "done"
if [[ $RUN_VALGRIND_FLAG == 1 ]]
then
valgrind $VALGRIND_FLAGS ./$INSTR_OUT
else
time ./$INSTR_OUT
fi
fi