commit a248f8335bd063066b61a9a7f6a1140d45f7c94c Author: Timo Schmidt Date: Tue Mar 28 06:13:35 2023 +0200 extract_tests.sh diff --git a/extract_tests.sh b/extract_tests.sh new file mode 100755 index 0000000..db48ff3 --- /dev/null +++ b/extract_tests.sh @@ -0,0 +1,39 @@ +#!/bin/sh + +wd="/nfs/homes/tischmid/repos/piscine" +if [ ! "$(pwd)" = "${wd}" ]; then + echo "# Your current working directory is:" + pwd + echo "# But you must be in:" + echo "${wd}" +fi + +out_dir="./timo_moulinette" +count="0" + +for file in $(find . | grep -v .git | grep '^\./c[[:digit:]][[:digit:]].*.c$'); do + comment_start="$(( $(grep -n -m 1 '////' "${file}" | cut -f1 -d:) + 1))" || { echo "Could not extract comment start. Exiting"; exit 1; } + comment_end="$(( $(grep -n -m 2 '////' "${file}" | tail -n1 | cut -f1 -d:) - 1))" || { echo "Could not extract comment end. Exiting"; exit 2; } + + if [ "${comment_start}" -lt 1 ]; then + echo "Comment start ${comment_start} is smaller than 1. Exiting" + continue + elif [ "${comment_end}" -lt 1 ]; then + echo "Comment start ${comment_end} is smaller than 1. Exiting" + continue + elif [ "${comment_end}" -le "${comment_start}" ]; then + echo "Comment start ${comment_end} is smaller than or equal to comment end ${comment_start}. Exiting" + continue + elif [ "$(( ${comment_end} + 2 ))" -eq "${comment_start}" ]; then + echo "Comment start ${comment_end} is 2 less than comment start ${comment_start}, which is probably an error. Exiting" + continue + fi + + out_dirpath="${out_dir}/$(dirname "${file}")" + out_filepath="${out_dir}/$(echo "${file}" | sed 's/\.c$/_test.c/g')" + mkdir -p "${out_dirpath}" || { echo "Could not create dir ${out_filepath}. Exiting"; exit 3; } + sed -n "${comment_start},${comment_end}p" "${file}" > "${out_filepath}" + count="$((count + 1))" +done + +printf '\033[32m%s\033[m\n' "Done, processed ${count} files"