Skip to content

Commit 87a5842

Browse files
committed
Add unit tests
1 parent 4423cf3 commit 87a5842

File tree

5 files changed

+907
-2
lines changed

5 files changed

+907
-2
lines changed

SConstruct

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fsenv
33

44
DIRECTORIES = [
55
'src',
6+
'test',
67
'components/rotatable' ]
78

89
TARGET_DEFINES = {

scripts/run-unittests.sh

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,114 @@
11
#!/usr/bin/env bash
22

3-
# all's well that ends well
4-
exit 0
3+
main () {
4+
cd "$(dirname "$(realpath "$0")")/.."
5+
if [ -n "$FSARCHS" ]; then
6+
local archs=()
7+
IFS=, read -ra archs <<< "$FSARCHS"
8+
for arch in "${archs[@]}" ; do
9+
run-tests "$arch" "$@"
10+
done
11+
else
12+
local os=$(uname -m -s)
13+
case $os in
14+
"Darwin arm64")
15+
run-tests darwin "$@";;
16+
"Darwin x86_64")
17+
run-tests darwin "$@";;
18+
"FreeBSD amd64")
19+
run-tests freebsd_amd64 "$@";;
20+
"Linux i686")
21+
run-tests linux32 "$@";;
22+
"Linux x86_64")
23+
run-tests linux64 "$@";;
24+
"Linux aarch64")
25+
run-tests linux_arm64 "$@";;
26+
"OpenBSD amd64")
27+
run-tests openbsd_amd64 "$@";;
28+
*)
29+
echo "$0: Unknown OS architecture: $os" >&2
30+
exit 1
31+
esac
32+
fi
33+
}
34+
35+
realpath () {
36+
if [ -x /bin/realpath ]; then
37+
/bin/realpath "$@"
38+
else
39+
# reimplementation of "readlink -fv" for OSX
40+
python -c "import os.path, sys; print(os.path.realpath(sys.argv[1]))" \
41+
"$1"
42+
fi
43+
}
44+
45+
run-tests () {
46+
local arch=$1
47+
shift &&
48+
echo &&
49+
echo Start Tests on $arch &&
50+
echo &&
51+
rm -rf stage/$arch/workdir &&
52+
mkdir stage/$arch/workdir &&
53+
if [ "$arch" = openbsd_amd64 ]; then
54+
stage/$arch/build/test/testrot stage/$arch/workdir
55+
return
56+
fi
57+
# The generated .gcda and .gcno files are not rewritten on
58+
# rebuild, which leads to errors and/or bad stats. I don't know a
59+
# better way around the problem but to get rid of the whole target
60+
# directory each time:
61+
rm -rf stage/$arch/test &&
62+
mkdir -p stage/$arch/test/gcov &&
63+
if ! FSCCFLAGS="-fprofile-arcs -ftest-coverage -O0" \
64+
FSLINKFLAGS="-fprofile-arcs" \
65+
${SCONS:-scons} builddir=test "$@"; then
66+
echo "Did you forget to specify prefix=<prefix> to $0?" >&2
67+
false
68+
fi &&
69+
stage/$arch/test/test/testrot stage/$arch/workdir &&
70+
test-coverage $arch
71+
}
72+
73+
test-coverage () {
74+
local arch=$1
75+
echo &&
76+
echo Test Coverage &&
77+
echo ============= &&
78+
echo &&
79+
find src -name \*.c |
80+
while read src; do
81+
${GCOV:-gcov} -p -o "stage/$arch/test/$(dirname "$src")" "$src" || exit
82+
done >stage/$arch/test/gcov/gcov.out 2>stage/$arch/test/gcov/gcov.err &&
83+
pretty-print-out <stage/$arch/test/gcov/gcov.out &&
84+
pretty-print-err <stage/$arch/test/gcov/gcov.err &&
85+
mv *.gcov stage/$arch/test/gcov/ &&
86+
echo
87+
}
88+
89+
pretty-print-out () {
90+
while read line1; do
91+
read line2
92+
read line3
93+
read line4
94+
f=$(sed "s/^File .\\([^']*\\)'$/\\1/" <<<"$line1")
95+
if [[ "$f" =~ \.h$ ]]; then
96+
continue
97+
fi
98+
case "$line2" in
99+
"No executable lines")
100+
;;
101+
"Lines executed:"*)
102+
p=$(sed 's/Lines executed:\([0-9.]*\)% .*$/\1/' <<<"$line2")
103+
printf "%6s%% %s\n" "$p" "$f"
104+
;;
105+
esac
106+
done
107+
}
108+
109+
pretty-print-err () {
110+
grep 'gcda:cannot open data file' |
111+
sed 's!^stage/[^/]*/test/\([^:]*\).gcda:cannot open data file.*! 0.00% \1.c!'
112+
}
113+
114+
main "$@"

test/SConscript

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Import('env')
2+
3+
env['CCFLAGS'] += ' -O0'
4+
5+
env['CPPPATH'] = [
6+
'../components/rotatable/include'
7+
]
8+
9+
env['LIBPATH'] = [
10+
'../src'
11+
]
12+
13+
env['LIBS'] = [
14+
'rotatable'
15+
]
16+
17+
env.ParseConfig(env['CONFIG_PARSER'])
18+
19+
env.Program('testrot',
20+
[ 'testrot.c' ])

0 commit comments

Comments
 (0)