From 774b2a39b2d75c1b424606b4a127f737bef82500 Mon Sep 17 00:00:00 2001 From: kevinnls <57634663+kevinnls@users.noreply.github.com> Date: Mon, 29 Mar 2021 20:19:50 +0530 Subject: [PATCH] init coding and test thing --- dev/.gitignore | 1 + dev/test | 112 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 24 +++++++++++ 3 files changed, 137 insertions(+) create mode 100644 dev/.gitignore create mode 100755 dev/test create mode 100644 src/main.c diff --git a/dev/.gitignore b/dev/.gitignore new file mode 100644 index 0000000..1079bf8 --- /dev/null +++ b/dev/.gitignore @@ -0,0 +1 @@ +tests.d diff --git a/dev/test b/dev/test new file mode 100755 index 0000000..3819d89 --- /dev/null +++ b/dev/test @@ -0,0 +1,112 @@ +#!/bin/bash + +set -e + +# set -x for debugging if DEBUG is set +function debug() { set -xv; } +${DEBUG+debug} + +# declare project info variables +declare -l project_lang project_name project_version; + +project_name='stdouterr' +project_version='v0.1.0-beta' +project_lang='C' + +project_root="$( cd .. ; pwd )" + +test_dir="${project_root}/dev/tests.d" +[[ ! -d "${test_dir}" ]] && mkdir -p "${test_dir}" +src_dir="${project_root}/src" + +c_compilers=('gcc') +cpp_compilers=('g++') + +_oname="${project_name:0:5}-${project_version}~test" +oname="$test_dir/${output:-${_oname}}" +_iname="${src_dir}/main.c" +iname="${input:-${_iname}}" + +_compileargs=( ) + + +# add args from env or set args based on env +# script version of -o retained unless overridden by env +if [[ -n "${compile_args}" ]]; then + if [[ ! "${compile_args}" =~ /*-o/ ]]; then + compile_args+=( -o "${oname}" ) + fi +else + compile_args=( "${_compileargs[@]}" -o "${oname}" ) +fi + +compile_args+=( "${compile_aargs[@]}" ) + + +case "${project_lang}" in + 'c') + compilers=( "${c_compilers[@]}" ) + ;; + 'cpp') + compilers=( "${cpp_compilers[@]}" ) + ;; + *) + err_unknown_lang + ;; +esac + +list_compilers(){ + for compiler in "${compilers[@]}"; do + echo -e " - ${compiler}\\n" + done +} + +err_no_compilers(){ + 1>&2 echo "ERR: no supported compiler found for ${project_lang}" + 1>&2 echo "install one of:" + 1>&2 list_compilers + exit 127 +} + +check_compilers(){ + for compiler in "${compilers[@]}"; do + compiler_path=$(command -v "${compiler}" 2>/dev/null) + if [[ -n "${compiler_path}" ]]; then + echo "found compiler: ${compiler}" + compile(){ + echo "=== compiling ===" + echo + "${compiler_path}" "${compile_args[@]}" "${iname}" + if [[ $? -eq 0 ]]; then + echo + echo "=== compiled! ===" + return 0 + else + echo + echo "=== compile failed! ===" + return 1 + fi + } + else + continue + fi + done + { command -v compile ; } &>/dev/null \ + || err_no_compilers +} + +test(){ + echo "=== start of test ===" + echo + "${oname}" + echo + echo + echo "=== end of test ===" +} + +check_compilers \ + && compile \ + && echo $? \ + && test "$@" \ + || exit 1 + diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..d91a6e6 --- /dev/null +++ b/src/main.c @@ -0,0 +1,24 @@ +#include + +char *output = "This is output", *error = "This is an error message"; +int printOut = 0, printErr = 0; + +void write_stdout(){ + fprintf(stdout, "%s\n", output); +} + +void write_stderr(){ + fprintf(stderr, "%s\n", error); +} + +int main(int argc, char const *argv[]) { + + if(argc > 1){ + //parse the arguments + } + + write_stdout(); + write_stderr(); + + return 0; +}