diff --git a/.gitignore b/.gitignore deleted file mode 100644 index c6127b3..0000000 --- a/.gitignore +++ /dev/null @@ -1,52 +0,0 @@ -# Prerequisites -*.d - -# Object files -*.o -*.ko -*.obj -*.elf - -# Linker output -*.ilk -*.map -*.exp - -# Precompiled Headers -*.gch -*.pch - -# Libraries -*.lib -*.a -*.la -*.lo - -# Shared objects (inc. Windows DLLs) -*.dll -*.so -*.so.* -*.dylib - -# Executables -*.exe -*.out -*.app -*.i*86 -*.x86_64 -*.hex - -# Debug files -*.dSYM/ -*.su -*.idb -*.pdb - -# Kernel Module Compile Results -*.mod* -*.cmd -.tmp_versions/ -modules.order -Module.symvers -Mkfile.old -dkms.conf diff --git a/Doxyfile b/Doxyfile deleted file mode 100644 index da6c3f6..0000000 --- a/Doxyfile +++ /dev/null @@ -1,319 +0,0 @@ -# Doxyfile 1.8.11 - -#--------------------------------------------------------------------------- -# Project related configuration options -#--------------------------------------------------------------------------- -DOXYFILE_ENCODING = UTF-8 -PROJECT_NAME = "SNP - Labs" -PROJECT_NUMBER = -PROJECT_BRIEF = -PROJECT_LOGO = -OUTPUT_DIRECTORY = -CREATE_SUBDIRS = NO -ALLOW_UNICODE_NAMES = NO -OUTPUT_LANGUAGE = English -BRIEF_MEMBER_DESC = YES -REPEAT_BRIEF = YES -ABBREVIATE_BRIEF = -ALWAYS_DETAILED_SEC = NO -INLINE_INHERITED_MEMB = NO -FULL_PATH_NAMES = YES -STRIP_FROM_PATH = -STRIP_FROM_INC_PATH = -SHORT_NAMES = NO -JAVADOC_AUTOBRIEF = NO -QT_AUTOBRIEF = NO -MULTILINE_CPP_IS_BRIEF = NO -INHERIT_DOCS = YES -SEPARATE_MEMBER_PAGES = NO -TAB_SIZE = 4 -ALIASES = -TCL_SUBST = -OPTIMIZE_OUTPUT_FOR_C = YES -OPTIMIZE_OUTPUT_JAVA = NO -OPTIMIZE_FOR_FORTRAN = NO -OPTIMIZE_OUTPUT_VHDL = NO -EXTENSION_MAPPING = -MARKDOWN_SUPPORT = YES -AUTOLINK_SUPPORT = YES -BUILTIN_STL_SUPPORT = NO -CPP_CLI_SUPPORT = NO -SIP_SUPPORT = NO -IDL_PROPERTY_SUPPORT = NO -DISTRIBUTE_GROUP_DOC = NO -GROUP_NESTED_COMPOUNDS = NO -SUBGROUPING = YES -INLINE_GROUPED_CLASSES = NO -INLINE_SIMPLE_STRUCTS = NO -TYPEDEF_HIDES_STRUCT = NO -LOOKUP_CACHE_SIZE = 0 -#--------------------------------------------------------------------------- -# Build related configuration options -#--------------------------------------------------------------------------- -EXTRACT_ALL = NO -EXTRACT_PRIVATE = NO -EXTRACT_PACKAGE = NO -EXTRACT_STATIC = NO -EXTRACT_LOCAL_CLASSES = YES -EXTRACT_LOCAL_METHODS = NO -EXTRACT_ANON_NSPACES = NO -HIDE_UNDOC_MEMBERS = NO -HIDE_UNDOC_CLASSES = NO -HIDE_FRIEND_COMPOUNDS = NO -HIDE_IN_BODY_DOCS = NO -INTERNAL_DOCS = NO -CASE_SENSE_NAMES = YES -HIDE_SCOPE_NAMES = NO -HIDE_COMPOUND_REFERENCE= NO -SHOW_INCLUDE_FILES = YES -SHOW_GROUPED_MEMB_INC = NO -FORCE_LOCAL_INCLUDES = NO -INLINE_INFO = YES -SORT_MEMBER_DOCS = YES -SORT_BRIEF_DOCS = NO -SORT_MEMBERS_CTORS_1ST = NO -SORT_GROUP_NAMES = NO -SORT_BY_SCOPE_NAME = NO -STRICT_PROTO_MATCHING = NO -GENERATE_TODOLIST = YES -GENERATE_TESTLIST = YES -GENERATE_BUGLIST = YES -GENERATE_DEPRECATEDLIST= YES -ENABLED_SECTIONS = -MAX_INITIALIZER_LINES = 30 -SHOW_USED_FILES = YES -SHOW_FILES = YES -SHOW_NAMESPACES = YES -FILE_VERSION_FILTER = -LAYOUT_FILE = -CITE_BIB_FILES = -#--------------------------------------------------------------------------- -# Configuration options related to warning and progress messages -#--------------------------------------------------------------------------- -QUIET = NO -WARNINGS = YES -WARN_IF_UNDOCUMENTED = YES -WARN_IF_DOC_ERROR = YES -WARN_NO_PARAMDOC = YES -WARN_AS_ERROR = NO -WARN_FORMAT = "$file:$line: $text" -WARN_LOGFILE = -#--------------------------------------------------------------------------- -# Configuration options related to the input files -#--------------------------------------------------------------------------- -INPUT = . -INPUT_ENCODING = UTF-8 -FILE_PATTERNS = *.h *.c *.dox -RECURSIVE = YES -EXCLUDE = -EXCLUDE_SYMLINKS = NO -EXCLUDE_PATTERNS = test*/* -EXCLUDE_SYMBOLS = -EXAMPLE_PATH = -EXAMPLE_PATTERNS = -EXAMPLE_RECURSIVE = NO -IMAGE_PATH = -INPUT_FILTER = -FILTER_PATTERNS = -FILTER_SOURCE_FILES = NO -FILTER_SOURCE_PATTERNS = -USE_MDFILE_AS_MAINPAGE = -#--------------------------------------------------------------------------- -# Configuration options related to source browsing -#--------------------------------------------------------------------------- -SOURCE_BROWSER = YES -INLINE_SOURCES = NO -STRIP_CODE_COMMENTS = NO -REFERENCED_BY_RELATION = NO -REFERENCES_RELATION = NO -REFERENCES_LINK_SOURCE = YES -SOURCE_TOOLTIPS = YES -USE_HTAGS = NO -VERBATIM_HEADERS = YES -CLANG_ASSISTED_PARSING = NO -CLANG_OPTIONS = -#--------------------------------------------------------------------------- -# Configuration options related to the alphabetical class index -#--------------------------------------------------------------------------- -ALPHABETICAL_INDEX = YES -COLS_IN_ALPHA_INDEX = 5 -IGNORE_PREFIX = -#--------------------------------------------------------------------------- -# Configuration options related to the HTML output -#--------------------------------------------------------------------------- -GENERATE_HTML = YES -HTML_OUTPUT = doc -HTML_FILE_EXTENSION = .html -HTML_HEADER = -HTML_FOOTER = -HTML_STYLESHEET = -HTML_EXTRA_STYLESHEET = -HTML_EXTRA_FILES = -HTML_COLORSTYLE_HUE = 220 -HTML_COLORSTYLE_SAT = 100 -HTML_COLORSTYLE_GAMMA = 80 -HTML_TIMESTAMP = NO -HTML_DYNAMIC_SECTIONS = NO -HTML_INDEX_NUM_ENTRIES = 100 -GENERATE_DOCSET = NO -DOCSET_FEEDNAME = "Doxygen generated docs" -DOCSET_BUNDLE_ID = org.doxygen.Project -DOCSET_PUBLISHER_ID = org.doxygen.Publisher -DOCSET_PUBLISHER_NAME = Publisher -GENERATE_HTMLHELP = NO -CHM_FILE = -HHC_LOCATION = -GENERATE_CHI = NO -CHM_INDEX_ENCODING = -BINARY_TOC = NO -TOC_EXPAND = NO -GENERATE_QHP = NO -QCH_FILE = -QHP_NAMESPACE = org.doxygen.Project -QHP_VIRTUAL_FOLDER = doc -QHP_CUST_FILTER_NAME = -QHP_CUST_FILTER_ATTRS = -QHP_SECT_FILTER_ATTRS = -QHG_LOCATION = -GENERATE_ECLIPSEHELP = NO -ECLIPSE_DOC_ID = org.doxygen.Project -DISABLE_INDEX = NO -GENERATE_TREEVIEW = NO -ENUM_VALUES_PER_LINE = 4 -TREEVIEW_WIDTH = 250 -EXT_LINKS_IN_WINDOW = NO -FORMULA_FONTSIZE = 10 -FORMULA_TRANSPARENT = YES -USE_MATHJAX = NO -MATHJAX_FORMAT = HTML-CSS -MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest -MATHJAX_EXTENSIONS = -MATHJAX_CODEFILE = -SEARCHENGINE = YES -SERVER_BASED_SEARCH = NO -EXTERNAL_SEARCH = NO -SEARCHENGINE_URL = -SEARCHDATA_FILE = searchdata.xml -EXTERNAL_SEARCH_ID = -EXTRA_SEARCH_MAPPINGS = -#--------------------------------------------------------------------------- -# Configuration options related to the LaTeX output -#--------------------------------------------------------------------------- -GENERATE_LATEX = NO -LATEX_OUTPUT = latex -LATEX_CMD_NAME = latex -MAKEINDEX_CMD_NAME = makeindex -COMPACT_LATEX = NO -PAPER_TYPE = a4 -EXTRA_PACKAGES = -LATEX_HEADER = -LATEX_FOOTER = -LATEX_EXTRA_STYLESHEET = -LATEX_EXTRA_FILES = -PDF_HYPERLINKS = YES -USE_PDFLATEX = YES -LATEX_BATCHMODE = NO -LATEX_HIDE_INDICES = NO -LATEX_SOURCE_CODE = NO -LATEX_BIB_STYLE = plain -LATEX_TIMESTAMP = NO -#--------------------------------------------------------------------------- -# Configuration options related to the RTF output -#--------------------------------------------------------------------------- -GENERATE_RTF = NO -RTF_OUTPUT = rtf -COMPACT_RTF = NO -RTF_HYPERLINKS = NO -RTF_STYLESHEET_FILE = -RTF_EXTENSIONS_FILE = -RTF_SOURCE_CODE = NO -#--------------------------------------------------------------------------- -# Configuration options related to the man page output -#--------------------------------------------------------------------------- -GENERATE_MAN = NO -MAN_OUTPUT = man -MAN_EXTENSION = .3 -MAN_SUBDIR = -MAN_LINKS = NO -#--------------------------------------------------------------------------- -# Configuration options related to the XML output -#--------------------------------------------------------------------------- -GENERATE_XML = NO -XML_OUTPUT = xml -XML_PROGRAMLISTING = YES -#--------------------------------------------------------------------------- -# Configuration options related to the DOCBOOK output -#--------------------------------------------------------------------------- -GENERATE_DOCBOOK = NO -DOCBOOK_OUTPUT = docbook -DOCBOOK_PROGRAMLISTING = NO -#--------------------------------------------------------------------------- -# Configuration options for the AutoGen Definitions output -#--------------------------------------------------------------------------- -GENERATE_AUTOGEN_DEF = NO -#--------------------------------------------------------------------------- -# Configuration options related to the Perl module output -#--------------------------------------------------------------------------- -GENERATE_PERLMOD = NO -PERLMOD_LATEX = NO -PERLMOD_PRETTY = YES -PERLMOD_MAKEVAR_PREFIX = -#--------------------------------------------------------------------------- -# Configuration options related to the preprocessor -#--------------------------------------------------------------------------- -ENABLE_PREPROCESSING = YES -MACRO_EXPANSION = NO -EXPAND_ONLY_PREDEF = NO -SEARCH_INCLUDES = YES -INCLUDE_PATH = -INCLUDE_FILE_PATTERNS = -PREDEFINED = -EXPAND_AS_DEFINED = -SKIP_FUNCTION_MACROS = YES -#--------------------------------------------------------------------------- -# Configuration options related to external references -#--------------------------------------------------------------------------- -TAGFILES = -GENERATE_TAGFILE = -ALLEXTERNALS = NO -EXTERNAL_GROUPS = YES -EXTERNAL_PAGES = YES -PERL_PATH = /usr/bin/perl -#--------------------------------------------------------------------------- -# Configuration options related to the dot tool -#--------------------------------------------------------------------------- -CLASS_DIAGRAMS = NO -MSCGEN_PATH = -DIA_PATH = -HIDE_UNDOC_RELATIONS = YES -HAVE_DOT = YES -DOT_NUM_THREADS = 0 -DOT_FONTNAME = Helvetica -DOT_FONTSIZE = 10 -DOT_FONTPATH = -CLASS_GRAPH = NO -COLLABORATION_GRAPH = NO -GROUP_GRAPHS = NO -UML_LOOK = NO -UML_LIMIT_NUM_FIELDS = 10 -TEMPLATE_RELATIONS = NO -INCLUDE_GRAPH = NO -INCLUDED_BY_GRAPH = NO -CALL_GRAPH = NO -CALLER_GRAPH = NO -GRAPHICAL_HIERARCHY = NO -DIRECTORY_GRAPH = NO -DOT_IMAGE_FORMAT = png -INTERACTIVE_SVG = NO -DOT_PATH = -DOTFILE_DIRS = -MSCFILE_DIRS = -DIAFILE_DIRS = -PLANTUML_JAR_PATH = -PLANTUML_INCLUDE_PATH = -DOT_GRAPH_MAX_NODES = 50 -MAX_DOT_GRAPH_DEPTH = 0 -DOT_TRANSPARENT = NO -DOT_MULTI_TARGETS = NO -GENERATE_LEGEND = NO -DOT_CLEANUP = NO diff --git a/Makefile b/Makefile deleted file mode 100644 index 50dbb63..0000000 --- a/Makefile +++ /dev/null @@ -1,32 +0,0 @@ -EMPTY := -SPACE := $(EMPTY) $(EMPTY) -NL := $(EMPTY)\\n$(EMPTY) - -LABS := $(sort $(wildcard P[0-9][0-9]*)) -EXAMPLE := $(if $(firstword $(LABS)),$(firstword $(LABS)),"Pxx") - -default: - @echo "**** SNP Labs ****" - @echo "$(subst $(SPACE),$(NL),$(LABS))" - @echo "" - @echo "**** Prerequisites ****" - @echo "1. Change into the testlib directory" - @echo " cd testlib" - @echo "2. Build and install the library, e.g." - @echo " make clean" - @echo " make default" - @echo " make test" - @echo " make install" - @echo " Caution: make sure the tests and installation does not produce any error." - @echo "" - @echo "**** How to build and run a lab? ****" - @echo "1. Change into the respective directory, e.g." - @echo " cd $(EXAMPLE)" - @echo "2. Build the lab, e.g." - @echo " make" - @echo " The resulting executable is located in the bin folder." - @echo "3. Build and run the tests, e.g." - @echo " make test" - @echo "Notes:" - @echo "- You may cleanup the builds, e.g." - @echo " make clean" diff --git a/P01_Bash/P01_Bash.pdf b/P01_Bash/P01_Bash.pdf deleted file mode 100644 index 8fc55ed..0000000 Binary files a/P01_Bash/P01_Bash.pdf and /dev/null differ diff --git a/P01_Bash/get-exec-list-arg.sh b/P01_Bash/get-exec-list-arg.sh deleted file mode 100755 index 59c83dd..0000000 --- a/P01_Bash/get-exec-list-arg.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash - -# produces a tabular output of all executables as found over the $PATH environment variable -# - output format: [1-based index of $PATH entry]:[$PATH entry]:[name of the executable] -# - e.g. 6:/bin:bash -# - the first argument (if given) is used as alternative to $PATH (e.g. for testing purposes) -# usage: ./get-exec-list-arg.sh # examines $PATH -# usage: ./get-exec-list-arg.sh "$PATH" # equivalent to the above call -# usage: ./get-exec-list-arg.sh ".:~/bin" # examines the current directory (.) and ~/bin - -# argument handling -path="$1" -[ -n "$path" ] || path="$PATH" - -# input-field-separator: tells the shell to split in the 'for' loop the $var by ":" -IFS=":" - -for p in $path -do - i=$((i+1)) - [ -n "$p" ] || p="." - if [ -d "$p" ] && [ -x "$p" ] - then - find -L "$p" -maxdepth 1 -type f -executable -printf "$i:%h:%f\n" 2>/dev/null - fi -done diff --git a/P01_Bash/tab2html.sh b/P01_Bash/tab2html.sh deleted file mode 100755 index e78218c..0000000 --- a/P01_Bash/tab2html.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash - -# produces a crude HTML table from a tabular file (given on stdin) with columns separated by ":" -# usage: tab2html < inbut.txt > output.html - -awk $* -- ' -BEGIN { - FS=":" # field-separator: which character separats fields in a record (i.e. in a line) - print "" - print "" - print " " - print " " - print " Tabular Data" - print " " - print " " - print "
" - print " " -} -{ - print " " - for (i = 1; i <= NF; i++) { print " " } - print " " -} -END { - print "
"$i"
" - print "
" - print " " - print "" -} -' - diff --git a/P01_Bash/tab2xml.sh b/P01_Bash/tab2xml.sh deleted file mode 100755 index a2efdd7..0000000 --- a/P01_Bash/tab2xml.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -# produces an XML file from a tabular file (given on stdin) with columns separated by ":" -# usage: tab2xml < inbut.txt > output.xml - -awk $* -- ' -BEGIN { - FS=":" # field-separator: which character separats fields in a record (i.e. in a line) - print "" - print "" -} -{ - print " " - for (i = 1; i <= NF; i++) { print " "$i"" } - print " " -} -END { - print "
" -} -' - diff --git a/P02_QR_Code_auf_Terminal/P02_QR_Code_auf_Terminal.pdf b/P02_QR_Code_auf_Terminal/P02_QR_Code_auf_Terminal.pdf deleted file mode 100644 index 06890f6..0000000 Binary files a/P02_QR_Code_auf_Terminal/P02_QR_Code_auf_Terminal.pdf and /dev/null differ diff --git a/P02_QR_Code_auf_Terminal/qr-code/Makefile b/P02_QR_Code_auf_Terminal/qr-code/Makefile deleted file mode 100644 index 81131f7..0000000 --- a/P02_QR_Code_auf_Terminal/qr-code/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -SNP_SHARED_MAKEFILE := $(if $(SNP_SHARED_MAKEFILE),$(SNP_SHARED_MAKEFILE),"~/snp/shared.mk") - -TARGET := bin/term-qr-code -SOURCES := src/main.c -TSTSOURCES := tests/tests.c - -include $(SNP_SHARED_MAKEFILE) diff --git a/P02_QR_Code_auf_Terminal/qr-code/mainpage.dox b/P02_QR_Code_auf_Terminal/qr-code/mainpage.dox deleted file mode 100644 index 2611637..0000000 --- a/P02_QR_Code_auf_Terminal/qr-code/mainpage.dox +++ /dev/null @@ -1,8 +0,0 @@ -/** - * @mainpage SNP - P02 QR Code - * - * @section Purpose - * - * This is a lab to display a QR code on the color terminal. - * - */ diff --git a/P02_QR_Code_auf_Terminal/qr-code/src/main.c b/P02_QR_Code_auf_Terminal/qr-code/src/main.c deleted file mode 100644 index dc0830c..0000000 --- a/P02_QR_Code_auf_Terminal/qr-code/src/main.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Lab P02 QR Code - * @remark prerequisite: sudo apt install qrencode - */ -#include -#include - -// define local macros -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - -/** - * @brief main function - * @returns always success (0) - */ -int main() -{ - // BEGIN-STUDENTS-TO-ADD-CODE - - - // END-STUDENTS-TO-ADD-CODE - return EXIT_SUCCESS; -} diff --git a/P02_QR_Code_auf_Terminal/qr-code/tests/snp.input b/P02_QR_Code_auf_Terminal/qr-code/tests/snp.input deleted file mode 100644 index a7922f3..0000000 --- a/P02_QR_Code_auf_Terminal/qr-code/tests/snp.input +++ /dev/null @@ -1,29 +0,0 @@ - - - - - ############## ######## ############## - ## ## ## ## ## - ## ###### ## #### ## ###### ## - ## ###### ## ###### ## ###### ## - ## ###### ## ## ## ## ###### ## - ## ## #### ## ## - ############## ## ## ## ############## - ###### - #### #### ## ######## ## ## - ## ## ## ## ## ## - ## #### #### #### ## ## ## - ## ######## ###### - ## #### #### ## #### ###### - ###### ## ## ## ## ## - ############## ###### #### - ## ## ## ## #### - ## ###### ## ############ ## ###### - ## ###### ## ## #### ## - ## ###### ## #### #### #### #### - ## ## ## #### ###### - ############## ############ ## ## - - - - diff --git a/P02_QR_Code_auf_Terminal/qr-code/tests/tests.c b/P02_QR_Code_auf_Terminal/qr-code/tests/tests.c deleted file mode 100644 index df95312..0000000 --- a/P02_QR_Code_auf_Terminal/qr-code/tests/tests.c +++ /dev/null @@ -1,146 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Test suite for the given package. - */ -#include -#include -#include -#include -#include "test_utils.h" - -#ifndef TARGET // must be given by the make file --> see test target -#error missing TARGET define -#endif - -/// @brief The name of the STDOUT text file. -#define OUTFILE "stdout.txt" -/// @brief The name of the STDERR text file. -#define ERRFILE "stderr.txt" -/// @brief The input data -#define SNP_INPUT "snp.input" - -// setup & cleanup -static int setup(void) -{ - remove_file_if_exists(OUTFILE); - remove_file_if_exists(ERRFILE); - return 0; // success -} - -static int teardown(void) -{ - // Do nothing. - // Especially: do not remove result files - they are removed in int setup(void) *before* running a test. - return 0; // success -} - - -/// @brief Reset -#define N "\033[0m\n" -/// @brief Black -#define X "\033[40m " -/// @brief White -#define _ "\033[47m " - -// tests -static void test_empty_input(void) -{ - // arrange - const char *out_txt[] = - { N - , N - }; - const char *err_txt[] = { NULL }; - // act - int exit_code = system(XSTR(TARGET) "" OUTFILE " 2>" ERRFILE); - // assert - CU_ASSERT_EQUAL(WEXITSTATUS(exit_code), EXIT_SUCCESS); - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); - assert_lines(ERRFILE, err_txt, sizeof(err_txt)/sizeof(*err_txt)); -} - -static void test_black_and_white_input(void) -{ - // arrange - const char *out_txt[] = - { N - , _ X _ N - , N - }; - const char *err_txt[] = { NULL }; - // act - int exit_code = system("echo ' # ' | " XSTR(TARGET) " 1>" OUTFILE " 2>" ERRFILE); - // assert - CU_ASSERT_EQUAL(WEXITSTATUS(exit_code), EXIT_SUCCESS); - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); - assert_lines(ERRFILE, err_txt, sizeof(err_txt)/sizeof(*err_txt)); -} - -static void test_snp_input(void) -{ - // arrange - const char *out_txt[] = - { N - , _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X X X X X X X X X X X X X _ _ X X X X X X X X _ _ _ _ X X X X X X X X X X X X X X _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ _ _ _ _ X X _ _ X X _ _ _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X _ _ X X X X X X _ _ X X _ _ _ _ X X X X _ _ _ _ _ _ X X _ _ X X X X X X _ _ X X _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X _ _ X X X X X X _ _ X X _ _ _ _ _ _ X X X X X X _ _ X X _ _ X X X X X X _ _ X X _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X _ _ X X X X X X _ _ X X _ _ _ _ _ _ X X _ _ X X _ _ X X _ _ X X X X X X _ _ X X _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ _ _ X X X X _ _ X X _ _ _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X X X X X X X X X X X X X _ _ X X _ _ X X _ _ X X _ _ X X X X X X X X X X X X X X _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ X X X X X X _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X X X _ _ X X X X _ _ X X _ _ _ _ X X X X X X X X _ _ X X _ _ _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ X X _ _ X X _ _ _ _ _ _ _ _ X X _ _ X X _ _ X X _ _ _ _ X X _ _ _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X _ _ X X X X _ _ X X X X _ _ X X X X _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ X X _ _ X X _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ _ _ _ _ _ _ _ _ X X X X X X X X _ _ _ _ X X X X X X _ _ _ _ _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ X X X X _ _ X X X X _ _ _ _ X X _ _ _ _ X X X X _ _ X X X X X X _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ X X X X X X _ _ X X _ _ X X _ _ X X _ _ X X _ _ X X _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X X X X X X X X X X X X X _ _ _ _ _ _ _ _ _ _ X X X X X X _ _ X X X X _ _ _ _ _ _ _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ X X _ _ X X X X _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X _ _ X X X X X X _ _ X X _ _ X X X X X X X X X X X X _ _ _ _ X X _ _ X X X X X X _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X _ _ X X X X X X _ _ X X _ _ X X _ _ _ _ _ _ X X X X _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X _ _ X X X X X X _ _ X X _ _ _ _ X X X X _ _ X X X X _ _ _ _ X X X X _ _ X X X X _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X _ _ _ _ _ _ _ _ _ _ X X _ _ X X _ _ _ _ _ _ _ _ _ _ X X X X _ _ X X X X X X _ _ _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ X X X X X X X X X X X X X X _ _ X X X X X X X X X X X X _ _ _ _ X X _ _ _ _ X X _ _ _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ N - , _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ N - , N - }; - const char *err_txt[] = { NULL }; - // act - int exit_code = system(XSTR(TARGET) "<" SNP_INPUT " 1>" OUTFILE " 2>" ERRFILE); - // assert - CU_ASSERT_EQUAL(WEXITSTATUS(exit_code), EXIT_SUCCESS); - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); - assert_lines(ERRFILE, err_txt, sizeof(err_txt)/sizeof(*err_txt)); -} - -/** - * @brief Registers and runs the tests. - * @returns success (0) or one of the CU_ErrorCode (>0) - */ -int main(void) -{ - // setup, run, teardown - TestMainBasic("lab test", setup, teardown - , test_empty_input - , test_black_and_white_input - , test_snp_input - ); - -} diff --git a/P03_Bounding_Box/P03_BoundingBox.pdf b/P03_Bounding_Box/P03_BoundingBox.pdf deleted file mode 100644 index f65aba0..0000000 Binary files a/P03_Bounding_Box/P03_BoundingBox.pdf and /dev/null differ diff --git a/P03_Bounding_Box/bounding-box/Makefile b/P03_Bounding_Box/bounding-box/Makefile deleted file mode 100644 index caa3e7e..0000000 --- a/P03_Bounding_Box/bounding-box/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -SNP_SHARED_MAKEFILE := $(if $(SNP_SHARED_MAKEFILE),$(SNP_SHARED_MAKEFILE),"~/snp/shared.mk") - -TARGET := bin/boundinbox -SOURCES := src/main.c -TSTSOURCES := tests/tests.c -LIBS := -lm - -include $(SNP_SHARED_MAKEFILE) diff --git a/P03_Bounding_Box/bounding-box/mainpage.dox b/P03_Bounding_Box/bounding-box/mainpage.dox deleted file mode 100644 index c142211..0000000 --- a/P03_Bounding_Box/bounding-box/mainpage.dox +++ /dev/null @@ -1,8 +0,0 @@ -/** - * @mainpage SNP - P03 Calculate bounding box - * - * @section Purpose - * - * This is a lab to calculate the bounding box of some triangle and to compare the boxes - * - */ diff --git a/P03_Bounding_Box/bounding-box/src/main.c b/P03_Bounding_Box/bounding-box/src/main.c deleted file mode 100644 index e66f245..0000000 --- a/P03_Bounding_Box/bounding-box/src/main.c +++ /dev/null @@ -1,209 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Lab P03 weekday - */ -#include -#include -#include -#include - - -/// @brief point of two coordinate axes -// BEGIN-STUDENTS-TO-ADD-CODE - - - -// END-STUDENTS-TO-ADD-CODE - -/// @brief box with an origin point and a dimension w and h -// BEGIN-STUDENTS-TO-ADD-CODE - - - -// END-STUDENTS-TO-ADD-CODE - -/// @brief triangle given by three points a, b, and c -// BEGIN-STUDENTS-TO-ADD-CODE - - - -// END-STUDENTS-TO-ADD-CODE - -/** - * @brief Compares two double values with a given hard coded tolerance. - * @param [in] a the first value - * @param [in] b the second value - * @returns 0 if fabs(a-b) <= tolerance, -1 if a < b, 1 otherwise - * @remark the tolerance is expected to be 0.05 (internally hard coded) - */ -// BEGIN-STUDENTS-TO-ADD-CODE - - - -// END-STUDENTS-TO-ADD-CODE - - -/** - * @brief Compares two box parameters for their area (w * h of the box). - * @param [in] a the first box - * @param [in] b the second box - * @returns the return value from compare_double() when given the areas of both boxes as parameter to compare_double() - */ -// BEGIN-STUDENTS-TO-ADD-CODE - - - -// END-STUDENTS-TO-ADD-CODE - - -/** - * @brief Compares the area of the parameter against 0. - * @param [in] box the box to check against area 0 - * @returns compare_double() == 0 with the boxes area and 0 as parameters - */ -// BEGIN-STUDENTS-TO-ADD-CODE - - - -// END-STUDENTS-TO-ADD-CODE - - -/** - * @brief Calculates the bounding box of a triangle - * @param [in] t the trinagle for which the baounding box is to be calculated - * @returns the bounding box of the triangle - * @remark calculates first the point with the minimum x and y of the triangle's points, - * plus as second point the one with the max coordinates of all points - * @remark the minial point is the origin of the bounding box, the width and hight is the x/y delta of the two points - * - */ -// BEGIN-STUDENTS-TO-ADD-CODE - - - -// END-STUDENTS-TO-ADD-CODE - - -// forward declaration of used functions in main() -static box_t get_match(box_t board, triangle_t t); -static triangle_t triangle_rotate(triangle_t t, double degree); -static void write_data(box_t board, triangle_t t, int degree); - - -/** - * @brief main function - * @param argc [in] number of entries in argv - * @param argv [in] program name plus command line arguments - * @returns returns success if valid date is given, failure otherwise - */ -int main(int argc, const char *argv[]) -{ - int x1, x2, x3, y1, y2, y3; - if (argc < 2 || sscanf(argv[1], "%d/%d-%d/%d-%d/%d", &x1, &y1, &x2, &y2, &x3, &y3) != 6) { - fprintf(stderr, "Usage: %s x1/y1-x2/y2-x3/y3\ne.g. 0/0-100/0-50/50\n", argv[0]); - return EXIT_FAILURE; - - } - - box_t board = { { 0, 0 }, 100, 200 }; - - // rotate the triangle in steps of 1 degree to find the "best position" of the triangle in the board - triangle_t t = { { x1, y1 }, { x2, y2 }, { x3, y3 } }; - int degree_best = -1; - box_t best; - for(int degree = 0; degree < 360; degree++) { - box_t match = get_match(board, triangle_rotate(t, degree)); - if (!is_zero_area(match)) { - if (degree_best == -1 || compare_area(match, best) < 0) { - degree_best = degree; - best = match; - } - } - } - - // write as tabular file - write_data(board, t, degree_best); - - return EXIT_SUCCESS; -} - - -/****************** internal functions ********************/ - -// forward declarations -static point_t point_rotate(point_t p, double degree); -static point_t point_move(point_t p, double dx, double dy); -static triangle_t triangle_move(triangle_t t, double dx, double dy); - - -static box_t get_match(box_t board, triangle_t t) -{ - box_t b = triangle_bounding_box(t); - if (compare_double(board.w, b.w) < 0) return (box_t){ board.p, 0, 0 }; - if (compare_double(board.h, b.h) < 0) return (box_t){ board.p, 0, 0 }; - return (box_t){ board.p, b.w, b.h }; -} - -static triangle_t triangle_rotate(triangle_t t, double degree) -{ - return (triangle_t){ point_rotate(t.a, degree), point_rotate(t.b, degree), point_rotate(t.c, degree) }; -} - -static triangle_t triangle_move(triangle_t t, double dx, double dy) -{ - return (triangle_t){ point_move(t.a, dx, dy), point_move(t.b, dx, dy), point_move(t.c, dx, dy) }; -} - -static point_t point_rotate(point_t p, double degree) -{ - double rad = fmod(degree, 360.0) * acos(-1.0) / 180.0; - double s = sin(rad); - double c = cos(rad); - - return (point_t){ c*p.x - s*p.y, s*p.x + c*p.y }; -} - -static point_t point_move(point_t p, double dx, double dy) -{ - return (point_t) { p.x+dx, p.y+dy }; -} - -static void write_data(box_t board, triangle_t t, int degree) -{ - double border = 10.0; - double gap = 2*border; - - // move board to origin - board.p.x = 0.0; - board.p.y = 0.0; - - // move original triangle to above the board - box_t tbb = triangle_bounding_box(t); - t = triangle_move(t, -tbb.p.x, -tbb.p.y + board.h + gap); - tbb.p.x = 0.0; - tbb.p.y = board.h + gap; - - // view box - box_t view = { { -border, -border }, fmax(board.w, tbb.w) + 2 * border, board.h + gap + tbb.h + 2 * border }; - printf("viewbox:%.1f:%.1f:%.1f:%.1f\n", view.p.x, view.p.y, view.w, view.h); - - printf("rect:%.1f:%.1f:%.1f:%.1f:%s\n", board.p.x, board.p.y, board.w, board.h, "gray"); - printf("polygon:%.1f:%.1f:%.1f:%.1f:%.1f:%.1f:%s\n", t.a.x, t.a.y, t.b.x, t.b.y, t.c.x, t.c.y, "green"); - - // there was a match, show it - if (degree >= 0) { - triangle_t rotated = triangle_rotate(t, degree); - box_t rbb = triangle_bounding_box(rotated); - t = triangle_move(rotated, -rbb.p.x, -rbb.p.y); // move to origin - printf("polygon:%.1f:%.1f:%.1f:%.1f:%.1f:%.1f:%s\n", t.a.x, t.a.y, t.b.x, t.b.y, t.c.x, t.c.y, "yellow"); - } -} diff --git a/P03_Bounding_Box/bounding-box/tab2svg.sh b/P03_Bounding_Box/bounding-box/tab2svg.sh deleted file mode 100755 index a74dbf6..0000000 --- a/P03_Bounding_Box/bounding-box/tab2svg.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash - -#!/bin/bash - -# produces a crude HTML embedded SVG drawing from a tabular file (given on stdin) with columns separated by ":" -# usage: tab2svg < inbut.txt > output.html - -awk $* -- ' -BEGIN { - FS=":" # field-separator: which character separats fields in a record (i.e. in a line) - print "" - print "" - print " " - print " SVG Data" - print " " - print " " -} -/^viewbox/ { - x = $2 - y = $3 - w = $4 - h = $5 -} -/^rect/ { - if (h > 0) { - printf " board = %dmm x %dmm\n

\n", $4, $5 - printf " \n", $4, x, y, w, h - h = 0 - } - printf " \n", $6, $2, $3, $4, $5 -} -/^polygon/ { - printf " \n", $8, $2, $3, $4, $5, $6, $7 -} -END { - print " " - print " " - print "" -} -' diff --git a/P03_Bounding_Box/bounding-box/tests/tests.c b/P03_Bounding_Box/bounding-box/tests/tests.c deleted file mode 100644 index 51844a0..0000000 --- a/P03_Bounding_Box/bounding-box/tests/tests.c +++ /dev/null @@ -1,109 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Test suite for the given package. - */ -#include -#include -#include -#include -#include -#include -#include "test_utils.h" - -#ifndef TARGET // must be given by the make file --> see test target -#error missing TARGET define -#endif - -/// @brief alias for EXIT_SUCCESS -#define OK EXIT_SUCCESS -/// @brief alias for EXIT_FAILURE -#define FAIL EXIT_FAILURE - -/// @brief The name of the STDOUT text file. -#define OUTFILE "stdout.txt" -/// @brief The name of the STDERR text file. -#define ERRFILE "stderr.txt" - -// setup & cleanup -static int setup(void) -{ - remove_file_if_exists(OUTFILE); - remove_file_if_exists(ERRFILE); - return 0; // success -} - -static int teardown(void) -{ - // Do nothing. - // Especially: do not remove result files - they are removed in int setup(void) *before* running a test. - return 0; // success -} - -// tests -static void test_no_match(void) -{ - // arrange - const char *out_txt[] = { - "viewbox:-10.0:-10.0:250.0:290.0\n", - "rect:0.0:0.0:100.0:200.0:gray\n", - "polygon:0.0:220.0:230.0:220.0:100.0:270.0:green\n", - }; - - // act & assert - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 0/0-230/0-100/50 >" OUTFILE " 2>" ERRFILE)), OK); - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); -} - -static void test_initial_match(void) -{ - // arrange - const char *out_txt[] = { - "viewbox:-10.0:-10.0:120.0:340.0\n", - "rect:0.0:0.0:100.0:200.0:gray\n", - "polygon:0.0:220.0:100.0:220.0:100.0:320.0:green\n", - "polygon:0.0:0.0:100.0:0.0:100.0:100.0:yellow\n", - }; - - // act & assert - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 0/0-100/0-100/100 >" OUTFILE " 2>" ERRFILE)), OK); - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); -} - -static void test_tight_match(void) -{ - // arrange - const char *out_txt[] = { - "viewbox:-10.0:-10.0:225.0:290.0\n", - "rect:0.0:0.0:100.0:200.0:gray\n", - "polygon:0.0:220.0:205.0:220.0:205.0:270.0:green\n", - "polygon:94.8:0.0:48.7:199.7:0.0:188.5:yellow\n", - }; - - // act & assert - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 0/0-205/0-205/50 >" OUTFILE " 2>" ERRFILE)), OK); - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); -} - - -/** - * @brief Registers and runs the tests. - * @returns success (0) or one of the CU_ErrorCode (>0) - */ -int main(void) -{ - // setup, run, teardown - TestMainBasic("lab test", setup, teardown - , test_no_match - , test_initial_match - , test_tight_match - ); -} diff --git a/P03_Bounding_Box/wochentag-berechnung/Makefile b/P03_Bounding_Box/wochentag-berechnung/Makefile deleted file mode 100644 index b3a1fea..0000000 --- a/P03_Bounding_Box/wochentag-berechnung/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -SNP_SHARED_MAKEFILE := $(if $(SNP_SHARED_MAKEFILE),$(SNP_SHARED_MAKEFILE),"~/snp/shared.mk") - -TARGET := bin/weekday -SOURCES := src/main.c -TSTSOURCES := tests/tests.c - -include $(SNP_SHARED_MAKEFILE) diff --git a/P03_Bounding_Box/wochentag-berechnung/mainpage.dox b/P03_Bounding_Box/wochentag-berechnung/mainpage.dox deleted file mode 100644 index 79fdc99..0000000 --- a/P03_Bounding_Box/wochentag-berechnung/mainpage.dox +++ /dev/null @@ -1,8 +0,0 @@ -/** - * @mainpage SNP - P03 Calculate the week day - * - * @section Purpose - * - * This is a lab to calculate the week day from a given Gregorian date. - * - */ diff --git a/P03_Bounding_Box/wochentag-berechnung/src/main.c b/P03_Bounding_Box/wochentag-berechnung/src/main.c deleted file mode 100644 index 65a0292..0000000 --- a/P03_Bounding_Box/wochentag-berechnung/src/main.c +++ /dev/null @@ -1,121 +0,0 @@ - /* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Lab P03 weekday - */ -#include -#include -#include - - -// *** TASK1: typedef enum types for month_t (Jan=1,...Dec} *** -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - - - -// *** TASK1: typedef struct for date_t *** -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - - - -// *** TASK2: typedef enum weekday_t (Sun=0, Mon, ...Sat) *** -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - -/** - * @brief TASK1: Checks if the given date is a leap year. - * @returns 0 = is not leap year, 1 = is leap year - */ -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - - -/** - * @brief TASK1: Calculates the length of the month given by the data parameter - * @returns 28, 29, 30, 31 if a valid month, else 0 - */ -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - -/** - * @brief TASK1: Checks if the given date is in the gregorian date range - * @returns 0 = no, 1 = yes - */ -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - - -/** - * @brief TASK1: Checks if the given date is a valid date. - * @returns 0 = is not valid date, 1 = is valid date - */ -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - - -/** - * @brief TASK2: calculated from a valid date the weekday - * @returns returns a weekday in the range Sun...Sat - */ -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - - - -/** - * @brief TASK2: print weekday as 3-letter abreviated English day name - */ -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - -/** - * @brief main function - * @param argc [in] number of entries in argv - * @param argv [in] program name plus command line arguments - * @returns returns success if valid date is given, failure otherwise - */ -int main(int argc, const char *argv[]) -{ - // TASK1: parse the mandatory argument into a date_t variable and check if the date is valid - // BEGIN-STUDENTS-TO-ADD-CODE - - - // END-STUDENTS-TO-ADD-CODE - - - // TASK2: calculate the weekday and print it in this format: YYYY-MM-DD is a Ddd\n, e.g. 2021-03-08 is a Mon\n - // BEGIN-STUDENTS-TO-ADD-CODE - - - // END-STUDENTS-TO-ADD-CODE - - return EXIT_SUCCESS; -} diff --git a/P03_Bounding_Box/wochentag-berechnung/tests/tests.c b/P03_Bounding_Box/wochentag-berechnung/tests/tests.c deleted file mode 100644 index 3a784db..0000000 --- a/P03_Bounding_Box/wochentag-berechnung/tests/tests.c +++ /dev/null @@ -1,196 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Test suite for the given package. - */ -#include -#include -#include -#include -#include -#include -#include "test_utils.h" - -#ifndef TARGET // must be given by the make file --> see test target -#error missing TARGET define -#endif - -/// @brief alias for EXIT_SUCCESS -#define OK EXIT_SUCCESS -/// @brief alias for EXIT_FAILURE -#define FAIL EXIT_FAILURE - -/// @brief The name of the STDOUT text file. -#define OUTFILE "stdout.txt" -/// @brief The name of the STDERR text file. -#define ERRFILE "stderr.txt" - -// setup & cleanup -static int setup(void) -{ - remove_file_if_exists(OUTFILE); - remove_file_if_exists(ERRFILE); - return 0; // success -} - -static int teardown(void) -{ - // Do nothing. - // Especially: do not remove result files - they are removed in int setup(void) *before* running a test. - return 0; // success -} - -static struct tm now() -{ - time_t t = time(NULL); - return *localtime(&t); -} - -static const char* weekday_name(int wday) -{ - assert(0 <= wday && wday <= 6); - static const char* days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; - return days[wday]; -} - -// tests -static void test_task1_fail_no_arg(void) -{ - // arrange & act & assert - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " >" OUTFILE " 2>" ERRFILE)), FAIL); -} - -static void test_task1_fail_not_gregorian(void) -{ - // arrange & act & assert - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 1291-08-01 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 1582-10-14 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 1583-10-14 >" OUTFILE " 2>" ERRFILE)), OK); -} - -static void test_task1_fail_month_out_of_range(void) -{ - // arrange & act & assert - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-13-13 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-00-13 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-12-13 >" OUTFILE " 2>" ERRFILE)), OK); -} - -static void test_task1_fail_day_out_of_range(void) -{ - // arrange & act & assert - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-01-32 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-01-31 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-02-30 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-02-29 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2019-02-29 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2019-02-28 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2019-02-29 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2019-02-28 >" OUTFILE " 2>" ERRFILE)), OK); -} - -static void test_task1_fail_leap_year(void) -{ - // arrange & act & assert - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 1900-02-29 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 1900-02-28 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2000-02-30 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2000-02-29 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2001-02-29 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2001-02-28 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2004-02-30 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2004-02-29 >" OUTFILE " 2>" ERRFILE)), OK); -} - -static void test_task1_valid_date(void) -{ - // arrange & act & assert - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-01-01 >" OUTFILE " 2>" ERRFILE)), OK); - - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-01-31 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-02-29 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-03-31 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-04-30 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-05-31 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-06-30 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-07-31 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-08-31 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-09-30 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-10-31 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-11-30 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-12-31 >" OUTFILE " 2>" ERRFILE)), OK); - - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-01-32 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-02-30 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-03-32 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-04-31 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-05-32 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-06-31 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-07-32 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-08-32 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-09-31 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-10-32 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-11-31 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 2020-12-32 >" OUTFILE " 2>" ERRFILE)), FAIL); -} - -static void test_task2_start_gregorian(void) -{ - // arrange & act & assert - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 1581-10-14 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 1581-10-15 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 1582-10-14 >" OUTFILE " 2>" ERRFILE)), FAIL); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 1582-10-15 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 1583-10-14 >" OUTFILE " 2>" ERRFILE)), OK); - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 1583-10-15 >" OUTFILE " 2>" ERRFILE)), OK); - - const char *out_txt[] = { "1582-10-15 is a Fri\n" }; - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " 1582-10-15 >" OUTFILE " 2>" ERRFILE)), OK); - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); -} - -static void test_task2_today(void) -{ - // arrange - const size_t SIZE = 1000; - char command[SIZE]; - struct tm t = now(); - snprintf(command, SIZE, "%s %04d-%02d-%02d 2>%s | tail -1 >%s", XSTR(TARGET), t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, ERRFILE, OUTFILE); - - char buffer[SIZE]; - snprintf(buffer, SIZE, "%04d-%02d-%02d is a %s\n", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, weekday_name(t.tm_wday)); - const char *out_txt[] = { buffer }; - const char *err_txt[] = { NULL }; - - // act & assert - CU_ASSERT_EQUAL(WEXITSTATUS(system(command)), OK); - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); - assert_lines(ERRFILE, err_txt, sizeof(err_txt)/sizeof(*err_txt)); -} - -/** - * @brief Registers and runs the tests. - * @returns success (0) or one of the CU_ErrorCode (>0) - */ -int main(void) -{ - // setup, run, teardown - TestMainBasic("lab test", setup, teardown - , test_task1_fail_no_arg - , test_task1_fail_not_gregorian - , test_task1_fail_month_out_of_range - , test_task1_fail_day_out_of_range - , test_task1_fail_leap_year - , test_task1_valid_date - , test_task2_start_gregorian - , test_task2_today - ); -} diff --git a/P04_Modularisieren_von_C_Code/P04_Modularisieren_von_C_Code.pdf b/P04_Modularisieren_von_C_Code/P04_Modularisieren_von_C_Code.pdf deleted file mode 100644 index 61c2206..0000000 Binary files a/P04_Modularisieren_von_C_Code/P04_Modularisieren_von_C_Code.pdf and /dev/null differ diff --git a/P04_Modularisieren_von_C_Code/show-dependencies/Makefile b/P04_Modularisieren_von_C_Code/show-dependencies/Makefile deleted file mode 100644 index df07b77..0000000 --- a/P04_Modularisieren_von_C_Code/show-dependencies/Makefile +++ /dev/null @@ -1,65 +0,0 @@ -SNP_SHARED_MAKEFILE := $(if $(SNP_SHARED_MAKEFILE),$(SNP_SHARED_MAKEFILE),"~/snp/shared.mk") - -TARGET := bin/dep2dot -# Add all additional c-files to the SOURCES variable -# BEGIN-STUDENTS-TO-ADD-CODE -SOURCES := src/main.c -# END-STUDENTS-TO-ADD-CODE -TSTSOURCES := tests/tests.c - -include $(SNP_SHARED_MAKEFILE) - - -# DEPFILES := ... define a list of png file names: %.c -> %.c.png -# BEGIN-STUDENTS-TO-ADD-CODE - - -# END-STUDENTS-TO-ADD-CODE - - - -# define dep target as .PHONEY -# BEGIN-STUDENTS-TO-ADD-CODE - - -# BEGIN-STUDENTS-TO-ADD-CODE - - - -# define dep target depending on FULLTARGET and DEPFILES above -# action: echo some text telling that the target is done using $@ - the echo command shall not be echoed before execution -# BEGIN-STUDENTS-TO-ADD-CODE - - -# BEGIN-STUDENTS-TO-ADD-CODE - - - -# define new suffix rule for %.png depending on %.dot -# action: dot -Tpng $< >$@ || $(RM) $@ -# BEGIN-STUDENTS-TO-ADD-CODE - - -# BEGIN-STUDENTS-TO-ADD-CODE - - - -# define new suffix rule for %.dot depending on %.dep -# action: call $(TARGET) $(@:.dot=) <$< >$@ || $(RM) $@ -# BEGIN-STUDENTS-TO-ADD-CODE - - -# BEGIN-STUDENTS-TO-ADD-CODE - - - -# converts any .c file into a .c.dep file by means of GCC -H switch -# note: it removes intermediate files which were created as side effect -%.c.dep: %.c - $(COMPILE.c) -H -o $@.x $< 2>$@ && $(RM) $@.x $@.d - - -# cleanup all results, including the ones od creating the dependencies -dep-clean: clean - $(RM) $(DEPFILES) $(wildcard src/*.dep src/*.dot) - diff --git a/P04_Modularisieren_von_C_Code/show-dependencies/mainpage.dox b/P04_Modularisieren_von_C_Code/show-dependencies/mainpage.dox deleted file mode 100644 index cd6b69a..0000000 --- a/P04_Modularisieren_von_C_Code/show-dependencies/mainpage.dox +++ /dev/null @@ -1,8 +0,0 @@ -/** - * @mainpage SNP - P04 Modularisation - * - * @section Purpose - * - * This is a lab for splitting functionality into multiple modules. - * - */ diff --git a/P04_Modularisieren_von_C_Code/show-dependencies/src/data.c b/P04_Modularisieren_von_C_Code/show-dependencies/src/data.c deleted file mode 100644 index a9f28f8..0000000 --- a/P04_Modularisieren_von_C_Code/show-dependencies/src/data.c +++ /dev/null @@ -1,149 +0,0 @@ -/** - * @file - * @brief Implementation of the dependency file access. - */ -#include "data.h" -#include "error.h" -#include -#include -#include -#include -#include - -#define MAX_PATH_LEN 512 ///< @brief Arbitrarily chosen maximum accepted path lenght. -#define MAX_LINE_LEN 512 ///< @brief Arbitrarily chosen maximum accepted line length -#define MAX_DIRS 64 ///< @brief Arbitrarily chosen maximum number of supported individual directories per dependency file. -#define MAX_FILES 256 ///< @brief Arbitrarily chosen maximum number of supported individual denendency entries. - -/** - * @brief Declaration of POSIX (but not C99) function. - * @param s [IN] The string to duplicate on the heap memory. - * @return The duplicated string. - * @remark Since the Makefile calls gcc with -std=c99, non-C99 POSIX and GNU extensions are excluded - the glibc, though, provides the function to the linker. - */ -char *strdup(const char *s); // not stdc99, but available in the glibc - -/** - * @brief Initialized the data structure before the data is to be read from th edependency file. - * @param data [INOUT] The address of the instance to initialize. - */ -static void init(data_t *data) -{ - assert(data); - memset(data, 0, sizeof(data_t)); - data->dirs = malloc(MAX_DIRS * sizeof(dir_t)); - if (!data->dirs) FATAL("no memory left"); - data->files = malloc(MAX_FILES * sizeof(file_t)); - if (!data->files) FATAL("no memory left"); -} - -/** - * @brief Updates the directory list with the given data. - * @param data [INOUT] The instance to update. - * @param path [IN] The file path of a dependency entry as given by the dependency file. - * @return The index of the directory entry (either an existing matching one or a newly added one). - * @remark Extracts the directory part by means of dirname() from the given path and looks up an existing entry or adds a new one. - */ -static size_t get_or_add_dir(data_t *data, const char *path) -{ - assert(data); - assert(path); - // The function dirname() gives no guarantee to not modify the parameter, therefore, need to produce a copy before calling dirname(). - // Likewise, the returned value may refer to the passed paremater, therefore, a copy is made from the return value. - char *dup = strdup(path); - if (!dup) FATAL("no memory left"); - char *name = strdup(dirname(dup)); - if (!name) FATAL("no memory left"); - free(dup); - - // search for a matching entry... - size_t i = 0; - while(i < data->n_dirs && strcmp(data->dirs[i].name, name) != 0) { - i++; - } - if (i >= MAX_DIRS) FATAL("too many directories"); - - if (i == data->n_dirs) { // no match found: add - // handover the allocated name to the owning new directory entry - dir_t dir = { .name = name }; - // append the new directory entry - data->dirs[data->n_dirs] = dir; - data->n_dirs++; - } else { - // release the name since match found, and therefore, no need to keep the allocated name anymore - free(name); - } - return i; -} - -/** - * @brief Add a file entry from the dependency file to the data structure. - * @param data [INOUT] The data container instance. - * @param path [IN] The path of one file entry from the dependency file. - * @param level [IN] The dependency level of the file entry from the dependency file. - * @remark The sequence of entries in the dependency file is relevant - it implies direct dependencies. - */ -static void add_file(data_t *data, const char *path, size_t level) -{ - assert(data); - assert(path); - // The function basename() gives no guarantee to not modify the parameter, therefore, need to produce a copy before calling basename(). - // Likewise, the returned value may refer to the passed paremater, therefore, a copy is made from the return value. - char *dup = strdup(path); - if (!dup) FATAL("no memory left"); - char *name = strdup(basename(dup)); - if (!name) FATAL("no memory left"); - free(dup); - - if (data->n_files >= MAX_FILES) FATAL("too many files"); - // produce a file entry - file_t file = { .name = name, .dir = get_or_add_dir(data, path), .level = level }; - data->files[data->n_files] = file; - data->n_files++; -} - -/** - * @brief Processes one dependency line of the dependency file. - * @param data [INOUT] The data container instance. - * @param line [IN] The line to parse and store in data. - */ -static void process_line(data_t *data, const char line[]) -{ - assert(data); - - size_t len = strlen(line); - assert(len > 0); - assert(line[0] == '.'); - - // read level - size_t i = strspn(line, "."); - size_t level = i; - // skip spaces - i += strspn(line+i, " \t"); - // take rest as path and add the file to the records - add_file(data, line+i, level); -} - -/* - * The public interface. - */ -const data_t data_read_all(const char *root) -{ - data_t data; - init(&data); - // add as first file the root for the given dependencies - add_file(&data, root, 0); - - char line[MAX_LINE_LEN] = { 0 }; - - // read all stdin line and only process dependency lines (those starting on a '.') - clearerr(stdin); - while(fgets(line, MAX_LINE_LEN, stdin)) { - size_t len = strlen(line); - if (len > 0 && line[len-1] == '\n' && line[0] == '.') { // only dependency lines - line[len-1] = '\0'; - process_line(&data, line); - } - } - return data; -} diff --git a/P04_Modularisieren_von_C_Code/show-dependencies/src/data.h b/P04_Modularisieren_von_C_Code/show-dependencies/src/data.h deleted file mode 100644 index 7a20b76..0000000 --- a/P04_Modularisieren_von_C_Code/show-dependencies/src/data.h +++ /dev/null @@ -1,66 +0,0 @@ -/** - * @file - * @brief Access to the GCC produced dependency data (via gcc -H command line option). - */ - -// begin of include guard -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - - -// includes which are needed in this header file -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - - - -/** - * @brief Directory container for file entries of the dependency file. - */ -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - - -/** - * @brief File container for the file entries of the dependency file. - */ -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - - - -/** - * @brief Overall container for all directories and all files from the dependency file. - */ -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - - - -/** - * @brief Entry function to read the deendency data from stdin. - * @param root [IN] The name of the root file (the deoendency file does not mention the root file, so, it has to be passed from outside). - * @return The container of the read data from stdin. See the documentation on gcc -H for details on the dependencies, etc. - */ -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE - - - -// end of include guard -// BEGIN-STUDENTS-TO-ADD-CODE - - -// END-STUDENTS-TO-ADD-CODE diff --git a/P04_Modularisieren_von_C_Code/show-dependencies/src/error.h b/P04_Modularisieren_von_C_Code/show-dependencies/src/error.h deleted file mode 100644 index 3a38ab6..0000000 --- a/P04_Modularisieren_von_C_Code/show-dependencies/src/error.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file - * @brief Error handling convenience functions. - */ -#ifndef _ERROR_H_ -#define _ERROR_H_ - -#include -#include - -/** - * @brief Prints the message to stderr and terminates with EXIT_FAILURE. - * @param MSG [IN] The "..." *string literal* to emit as error - no format parameters nor variables supported. - */ -#define FATAL(MSG) do { fprintf(stderr, "ERROR: %s\n", MSG); exit(EXIT_FAILURE); } while(0) - -#endif // _ERROR_H_ diff --git a/P04_Modularisieren_von_C_Code/show-dependencies/src/main.c b/P04_Modularisieren_von_C_Code/show-dependencies/src/main.c deleted file mode 100644 index 8cf35f9..0000000 --- a/P04_Modularisieren_von_C_Code/show-dependencies/src/main.c +++ /dev/null @@ -1,36 +0,0 @@ - /* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Lab P04 dep2dot - */ -#include -#include - -#include "error.h" -#include "data.h" -#include "output.h" - -/** - * @brief main function - * @param argc [in] number of entries in argv - * @param argv [in] program name plus command line arguments - * @returns returns success if valid date is given, failure otherwise - * @remark Prerequisit to convert the resulting DOT file on the shell: sodo apt install graphviz - * @remark Convert: gcc -H ... file.c ... 2>file.dep ; dep2dot file.c file.dot && dot -Tpng file.dot >file.png - */ -int main(int argc, const char *argv[]) -{ - if (argc < 2) FATAL("missing arguments\nusage: dep2dot file.c file.dot # from gcc -H ... file.c ... 2>file.dep\n"); - - output_dot(data_read_all(argv[1])); - - return EXIT_SUCCESS; -} diff --git a/P04_Modularisieren_von_C_Code/show-dependencies/src/output.c b/P04_Modularisieren_von_C_Code/show-dependencies/src/output.c deleted file mode 100644 index 088d984..0000000 --- a/P04_Modularisieren_von_C_Code/show-dependencies/src/output.c +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @file - * @brief Provides output functions for various file formats. - */ -#include "output.h" -#include -#include -#include - -/** - * @brief Writes the node name of the given file. - * @param file [IN] The file for which to write the node name. - * @remark The dependency data contain duplicates of file entries - the node name must be unique for the path and the *basename* of the files. - */ -static void print_node(file_t file) -{ - printf("\"%s (cluster_c%zd)\"", file.name, file.dir); -} - -/** - * @brief Recursively writes the individual direct dependencies for the file given by curr. - * @param files [IN] The array of all files - the sequence is relevant. - * @param len [IN] The lenght of the files array, i.e. the upper limit for curr values and the subsequent index values. - * @param curr [IN] The index into files for the current root for dependencies: curr -> x, curr -> y, ... - * @return Returns the index into files for the next file to process (i.e. curr value for the next call to this function). - * @remark For a given *curr* file, all following files are with depth level + 1 are direct include files. - * @remark All files with a higher level are *indirect* include files, thus *direct* includes from files processed by recursive calls. - * @remark The list of direct includes to the *curr* file terminates with a level equal of less the the *curr* one (or when the list is exchausted). - */ -static size_t dependencies(file_t files[], size_t len, size_t curr) -{ - assert(curr < len); - size_t level = files[curr].level; - size_t file = curr + 1; - while(file < len && files[file].level > level) { - if (files[file].level == level + 1) { - // Write to stdout " file -> include;\n" where file and include are the DOT node names of the respective files - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - - - // END-STUDENTS-TO-ADD-CODE - file = dependencies(files, len, file); - } else { - file++; - } - } - return file; -} - -/* - * Public interface - */ -void output_dot(const data_t data) -{ - printf("digraph dep {\n"); - // nodes - printf(" node [shape=box]\n"); - for (size_t file = 0; file < data.n_files; file++) { - // Write to stdout " file [label=\"name\"];\n" where file is the DOT node name and name is the file name - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - - // END-STUDENTS-TO-ADD-CODE - } - // directory clusters - for (size_t dir = 0; dir < data.n_dirs; dir++) { - printf(" subgraph cluster_c%zd {\n", dir); - printf(" label=\"%s\"; %s\n", data.dirs[dir].name, strncmp(data.dirs[dir].name, "/usr/", 5) == 0 ? "style=filled; color=lightgrey;" : "color=black;"); - for (size_t file = 0; file < data.n_files; file++) { - if (data.files[file].dir == dir) { - // Write to stdout " file;\n" where file is the DOT node name - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - - // END-STUDENTS-TO-ADD-CODE - } - } - printf(" }\n"); - } - - // dependencies - size_t curr = 0; - do { - curr = dependencies(data.files, data.n_files, curr); - } while(curr < data.n_files); - - printf("}\n"); -} - - diff --git a/P04_Modularisieren_von_C_Code/show-dependencies/src/output.h b/P04_Modularisieren_von_C_Code/show-dependencies/src/output.h deleted file mode 100644 index 21f46e3..0000000 --- a/P04_Modularisieren_von_C_Code/show-dependencies/src/output.h +++ /dev/null @@ -1,12 +0,0 @@ -/** - * @file - * @brief Provides output functions for various file formats. - */ -// define proper header file here, with include gaurd, etc. -// BEGIN-STUDENTS-TO-ADD-CODE - - - - - -// END-STUDENTS-TO-ADD-CODE diff --git a/P04_Modularisieren_von_C_Code/show-dependencies/tests/dep.input b/P04_Modularisieren_von_C_Code/show-dependencies/tests/dep.input deleted file mode 100644 index d21a54c..0000000 --- a/P04_Modularisieren_von_C_Code/show-dependencies/tests/dep.input +++ /dev/null @@ -1,7 +0,0 @@ -Test File -. dir1/h1_1 -.. dir1/h1_1_2 -. dir1/h1_2 -. dir2/h2_1 -.. dir1/h1_1 -Done diff --git a/P04_Modularisieren_von_C_Code/show-dependencies/tests/no_dep.input b/P04_Modularisieren_von_C_Code/show-dependencies/tests/no_dep.input deleted file mode 100644 index 6565e2d..0000000 --- a/P04_Modularisieren_von_C_Code/show-dependencies/tests/no_dep.input +++ /dev/null @@ -1,2 +0,0 @@ -Test File -Done diff --git a/P04_Modularisieren_von_C_Code/show-dependencies/tests/tests.c b/P04_Modularisieren_von_C_Code/show-dependencies/tests/tests.c deleted file mode 100644 index 119800f..0000000 --- a/P04_Modularisieren_von_C_Code/show-dependencies/tests/tests.c +++ /dev/null @@ -1,140 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Test suite for the given package. - */ -#include -#include -#include -#include -#include -#include -#include "test_utils.h" - -#ifndef TARGET // must be given by the make file --> see test target -#error missing TARGET define -#endif - -/// @brief alias for EXIT_SUCCESS -#define OK EXIT_SUCCESS -/// @brief alias for EXIT_FAILURE -#define FAIL EXIT_FAILURE - -/// @brief The name of the STDOUT text file. -#define OUTFILE "stdout.txt" -/// @brief The name of the STDERR text file. -#define ERRFILE "stderr.txt" - -/// @brief test data file -#define IN_NO_DEP "no_dep.input" -/// @brief test data file -#define IN_DEP "dep.input" - -// setup & cleanup -static int setup(void) -{ - remove_file_if_exists(OUTFILE); - remove_file_if_exists(ERRFILE); - return 0; // success -} - -static int teardown(void) -{ - // Do nothing. - // Especially: do not remove result files - they are removed in int setup(void) *before* running a test. - return 0; // success -} - -// tests -static void test_fail_no_arg(void) -{ - // arrange & act & assert - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " >" OUTFILE " 2>" ERRFILE)), FAIL); -} - -static void test_no_dep(void) -{ - // arrange - const char *out_txt[] = { - "digraph dep {\n", - " node [shape=box]\n", - " \"root (cluster_c0)\" [label=\"root\"];\n", - " subgraph cluster_c0 {\n", - " label=\".\"; color=black;\n", - " \"root (cluster_c0)\";\n", - " }\n", - "}\n", - }; - - // act & assert - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " root <" IN_NO_DEP " >" OUTFILE " 2>" ERRFILE)), OK); - - // assert - - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); -} - -static void test_dep(void) -{ - // arrange - const char *out_txt[] = { - "digraph dep {\n", - " node [shape=box]\n", - " \"root (cluster_c0)\" [label=\"root\"];\n", - " \"h1_1 (cluster_c1)\" [label=\"h1_1\"];\n", - " \"h1_1_2 (cluster_c1)\" [label=\"h1_1_2\"];\n", - " \"h1_2 (cluster_c1)\" [label=\"h1_2\"];\n", - " \"h2_1 (cluster_c2)\" [label=\"h2_1\"];\n", - " \"h1_1 (cluster_c1)\" [label=\"h1_1\"];\n", - " subgraph cluster_c0 {\n", - " label=\".\"; color=black;\n", - " \"root (cluster_c0)\";\n", - " }\n", - " subgraph cluster_c1 {\n", - " label=\"dir1\"; color=black;\n", - " \"h1_1 (cluster_c1)\";\n", - " \"h1_1_2 (cluster_c1)\";\n", - " \"h1_2 (cluster_c1)\";\n", - " \"h1_1 (cluster_c1)\";\n", - " }\n", - " subgraph cluster_c2 {\n", - " label=\"dir2\"; color=black;\n", - " \"h2_1 (cluster_c2)\";\n", - " }\n", - " \"root (cluster_c0)\" -> \"h1_1 (cluster_c1)\";\n", - " \"h1_1 (cluster_c1)\" -> \"h1_1_2 (cluster_c1)\";\n", - " \"root (cluster_c0)\" -> \"h1_2 (cluster_c1)\";\n", - " \"root (cluster_c0)\" -> \"h2_1 (cluster_c2)\";\n", - " \"h2_1 (cluster_c2)\" -> \"h1_1 (cluster_c1)\";\n", - "}\n", - }; - - // act & assert - CU_ASSERT_EQUAL(WEXITSTATUS(system(XSTR(TARGET) " root <" IN_DEP " >" OUTFILE " 2>" ERRFILE)), OK); - - // assert - - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); -} - -/** - * @brief Registers and runs the tests. - * @returns success (0) or one of the CU_ErrorCode (>0) - */ -int main(void) -{ - // setup, run, teardown - TestMainBasic("lab test", setup, teardown - , test_fail_no_arg - , test_no_dep - , test_dep - ); -} diff --git a/P05_TicTacToe/P05_TicTacToe.pdf b/P05_TicTacToe/P05_TicTacToe.pdf deleted file mode 100644 index 27488e6..0000000 Binary files a/P05_TicTacToe/P05_TicTacToe.pdf and /dev/null differ diff --git a/P05_TicTacToe/tic-tac-toe/Makefile b/P05_TicTacToe/tic-tac-toe/Makefile deleted file mode 100644 index a29984f..0000000 --- a/P05_TicTacToe/tic-tac-toe/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -SNP_SHARED_MAKEFILE := $(if $(SNP_SHARED_MAKEFILE),$(SNP_SHARED_MAKEFILE),"~/snp/shared.mk") - -TARGET := bin/tic-tac-toe -MODULES := src/model.c src/view.c src/control.c -SOURCES := src/main.c $(MODULES) -TSTSOURCES := tests/tests.c $(MODULES) - -include $(SNP_SHARED_MAKEFILE) - diff --git a/P05_TicTacToe/tic-tac-toe/mainpage.dox b/P05_TicTacToe/tic-tac-toe/mainpage.dox deleted file mode 100644 index b07e65d..0000000 --- a/P05_TicTacToe/tic-tac-toe/mainpage.dox +++ /dev/null @@ -1,8 +0,0 @@ -/** - * @mainpage SNP - P05 Tic Tac Toe Game - * - * @section Purpose - * - * This is a lab on usage of arrays. - * - */ diff --git a/P05_TicTacToe/tic-tac-toe/src/control.c b/P05_TicTacToe/tic-tac-toe/src/control.c deleted file mode 100644 index ccae643..0000000 --- a/P05_TicTacToe/tic-tac-toe/src/control.c +++ /dev/null @@ -1,154 +0,0 @@ -/** - * @file - * @brief Implementation - */ -#include "control.h" -#include "model.h" -#include - -/** - * @brief Conversion from control field number (1...9) to 0-based model position (row, col). - * @param cell [IN] Field number (1...9). - * @return Returns the position of the field. - * @remark Asserts proper field range. - */ -static model_pos_t get_pos(size_t cell) -{ - assert(1 <= cell && cell <= 9); - model_pos_t pos = { (cell - 1) / 3, (cell - 1) % 3 }; - return pos; -} - -/** - * @brief Conversion from control player to model state. - * @param player [IN] Control player value to convert. - * @return Returns the matching model state. - * @remark No assertion is done - defaults to model_state_none. - */ -static model_state_t get_state(control_player_t player) -{ - switch(player) { - case control_player_a: return model_state_a; - case control_player_b: return model_state_b; - default: return model_state_none; - } -} - -/** - * @brief Conversion from 0-based model position (row, col) to control field number (1...9). - * @param pos [IN] 0-based model position (row,col). - * @return The control filed number (1...9) - * @remark Asserts proper position range. - */ -static size_t get_cell(model_pos_t pos) -{ - assert(pos.row < 3); - assert(pos.col < 3); - return 1 + pos.row * 3 + pos.col; -} - -/** - * @brief Conversion from model state to control player. - * @param state [IN] Model state to convert - * @return Returns the matching control player value. - * @remark No assertion is done - defaults to control_no_player. - */ -static control_player_t get_player(model_state_t state) -{ - switch(state) { - case model_state_a: return control_player_a; - case model_state_b: return control_player_b; - default: return control_no_player; - } -} - -/** - * @brief Queries if a move is possible. - * @param instance [INOUT] The instance which holds the state. - * @return Returns 0 if no move is possible any more, otherwise 1. - */ -static int control_can_move(control_t *instance) -{ - assert(instance); - return model_can_move(instance->model); -} - -// public API function which is documented in the header file. -void control_init(control_t *instance, model_t *model) -{ - assert(instance); - assert(model); - instance->player = control_player_a; - instance->model = model; -} - -// public API function which is documented in the header file. -void control_move(control_t *instance, size_t cell) -{ - assert(instance); - if (model_move(instance->model, get_pos(cell), get_state(instance->player))) { - if (control_can_move(instance)) { - switch(instance->player) { - case control_player_a: - instance->player = control_player_b; - break; - case control_player_b: - instance->player = control_player_a; - break; - default: - break; - } - } else { - instance->player = control_no_player; - } - } -} - -// public API function which is documented in the header file. -control_player_t control_get_winner(control_t *instance) -{ - assert(instance); - return get_player(model_get_winner(instance->model)); -} - -// public API function which is documented in the header file. -control_player_t control_get_player(control_t *instance) -{ - assert(instance); - return instance->player; -} - -// public API function which is documented in the header file. -control_player_t control_get_state(control_t *instance, size_t cell) -{ - assert(instance); - return get_player(model_get_state(instance->model, get_pos(cell))); -} - -// public API function which is documented in the header file. -control_line_t control_get_win(control_t *instance) -{ - assert(instance); - if (control_get_winner(instance) == control_no_player) { - control_line_t no_win = { 0 }; - return no_win; - } - - model_line_t line = model_get_win_line(instance->model); - assert(line.dir != model_dir_none); - size_t start_cell = get_cell(line.start); - switch(line.dir) { - case model_dir_h: - return (control_line_t) { { start_cell, start_cell + 1, start_cell + 2 } }; - case model_dir_v: - return (control_line_t) { { start_cell, start_cell + 3, start_cell + 6 } }; - case model_dir_d: - if (start_cell == 1) { - return (control_line_t) { { start_cell, start_cell + 4, start_cell + 8 } }; - } else { - return (control_line_t) { { start_cell, start_cell + 2, start_cell + 6 } }; - } - default: - return (control_line_t) { { 1, 1, 1 } }; - } -} diff --git a/P05_TicTacToe/tic-tac-toe/src/control.h b/P05_TicTacToe/tic-tac-toe/src/control.h deleted file mode 100644 index 613bd8f..0000000 --- a/P05_TicTacToe/tic-tac-toe/src/control.h +++ /dev/null @@ -1,80 +0,0 @@ -/** - * @file - * @brief MVC - agent between model and view - */ -#ifndef _CONTROL_H_ -#define _CONTROL_H_ - -#include "model.h" - -/** - * @brief The selection of possible players. - */ -typedef enum { - control_no_player, ///< none of the players - control_player_a, ///< first player - control_player_b, ///< second player -} control_player_t; - -/** - * @brief Sequence of winning cell numbers in increasing cell numbers. - */ -typedef struct { - size_t line[3]; ///< the sequence of cells (1...9) or 0 in the first element if no win -} control_line_t; - -/** - * @brief The instance type. - */ -typedef struct { - control_player_t player; ///< the current player - model_t *model; ///< the reference to the model -} control_t; - -/** - * @brief Constructor: initialize the instance memory. - * @param instance [INOUT] The instance which holds the state. - * @param model [IN] Dependency Injection of the model instance. - */ -void control_init(control_t *instance, model_t *model); - -/** - * @brief Performs a move on the board. - * @param instance [INOUT] The instance which holds the state. - * @param cell [IN] The affected field (1...9) - * @remark Silently ignores a move if it is not allowed (e.g. if already completed or the field is already played, etc.). - */ -void control_move(control_t *instance, size_t cell); - -/** - * @brief Queries the winning player. - * @param instance [INOUT] The instance which holds the state. - * @returns Returns the winning player (if any). - */ -control_player_t control_get_winner(control_t *instance); - -/** - * @brief Queries the next player. - * @param instance [INOUT] The instance which holds the state. - * @returns Returns the next player (if any). - * @remark This is updated by the control_move() function. - */ -control_player_t control_get_player(control_t *instance); - -/** - * @brief Queries the state of a field. - * @param instance [INOUT] The instance which holds the state. - * @param cell [IN] The affected field of the board (1...9). - * @returns Returns the player which played this field (if any). - */ -control_player_t control_get_state(control_t *instance, size_t cell); - -/** - * @brief Gets the winning fields (if any). - * @param instance [INOUT] The instance which holds the state. - * @returns Returns the field numbers in increasing order (1...9) which win the game (if any). - * @remark If there is no winner (yet), the first entry in the result is 0. - */ -control_line_t control_get_win(control_t *instance); - -#endif // _CONTROL_H_ diff --git a/P05_TicTacToe/tic-tac-toe/src/main.c b/P05_TicTacToe/tic-tac-toe/src/main.c deleted file mode 100644 index 7e66b7e..0000000 --- a/P05_TicTacToe/tic-tac-toe/src/main.c +++ /dev/null @@ -1,39 +0,0 @@ - /* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Lab P04 dep2dot - */ -#include -#include - -#include "view.h" -#include "model.h" -#include "control.h" - -/** - * @brief main function - * @param argc [in] number of entries in argv - * @param argv [in] program name plus command line arguments - * @returns returns success if valid date is given, failure otherwise - */ -int main(int argc, const char *argv[]) -{ - view_t view; - control_t control; - model_t model; - - model_init(&model); - control_init(&control, &model); - view_init(&view, &control); - view_run(&view); - - return EXIT_SUCCESS; -} diff --git a/P05_TicTacToe/tic-tac-toe/src/model.c b/P05_TicTacToe/tic-tac-toe/src/model.c deleted file mode 100644 index fd673bc..0000000 --- a/P05_TicTacToe/tic-tac-toe/src/model.c +++ /dev/null @@ -1,165 +0,0 @@ -/** - * @file - * @brief Implementation - */ -#include "model.h" -#include - -/** - * @brief Asserts that the position is in range. - * @param [IN] The position to check. - */ -static void assert_pos(model_pos_t pos) -{ - assert(pos.row < MODEL_SIZE); - assert(pos.col < MODEL_SIZE); -} - -/** - * @brief Sets the field on the board to the given state. - * @param instance [INOUT] The instance which holds the state. - * @param pos [IN] The affected field. - * @param state [IN] The new state of the field. - */ -static void set_state(model_t *instance, model_pos_t pos, model_state_t state) -{ - assert_pos(pos); - - // Instructions to the students: - // set the field of the board to the new state - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - - - // END-STUDENTS-TO-ADD-CODE -} - -// public API function which is documented in the header file. -model_pos_t model_pos(size_t row, size_t col) -{ - return (model_pos_t){row, col}; -} - -// public API function which is documented in the header file. -void model_init(model_t *instance) -{ - assert(instance); - - // Instructions to the students: - // set all fields of the board to model_state_none - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - - - // END-STUDENTS-TO-ADD-CODE -} - -// public API function which is documented in the header file. -model_state_t model_get_state(model_t *instance, model_pos_t pos) -{ - assert(instance); - assert_pos(pos); - - // Instructions to the students: - // replace the stub implementation my access to the field at the given position. - // BEGIN-STUDENTS-TO-ADD-CODE - - - return model_state_none; // stub - - - // END-STUDENTS-TO-ADD-CODE -} - -// public API function which is documented in the header file. -model_line_t model_get_win_line(model_t *instance) -{ - assert(instance); - model_state_t anchor; - - // horizontal - for(size_t row = 0; row < MODEL_SIZE; row++) { - anchor = model_get_state(instance, model_pos(row, 0)); - if (anchor != model_state_none - && anchor == model_get_state(instance, model_pos(row, 1)) - && anchor == model_get_state(instance, model_pos(row, 2))) { - return (model_line_t) { model_dir_h, { row, 0 } }; - } - } - - // vertical - for(size_t col = 0; col < MODEL_SIZE; col++) { - anchor = model_get_state(instance, model_pos(0, col)); - if (anchor != model_state_none - && anchor == model_get_state(instance, model_pos(1, col)) - && anchor == model_get_state(instance, model_pos(2, col))) { - return (model_line_t) { model_dir_v, { 0, col } }; - } - - - } - - // diagonal - anchor = model_get_state(instance, model_pos(1, 1)); - if (anchor != model_state_none) { - if (anchor == model_get_state(instance, model_pos(0, 0)) && anchor == model_get_state(instance, model_pos(2, 2))) { - return (model_line_t) { model_dir_d, { 0, 0 } }; - } - if (anchor == model_get_state(instance, model_pos(2, 0)) && anchor == model_get_state(instance, model_pos(0, 2))) { - return (model_line_t) { model_dir_d, { 0, 2 } }; - } - } - - // fallback - return (model_line_t) { model_dir_none, { 0, 0 } }; -} - -// public API function which is documented in the header file. -model_state_t model_get_winner(model_t *instance) -{ - assert(instance); - model_line_t line = model_get_win_line(instance); - return line.dir == model_dir_none - ? model_state_none - : model_get_state(instance, model_pos(line.start.row, line.start.col)) - ; -} - -// public API function which is documented in the header file. -int model_can_move(model_t *instance) -{ - assert(instance); - if (model_get_winner(instance) == model_state_none) { - // Instructions to the students: - // scan all fields: return 1 with first field which equals model_state_none - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - - - - - // END-STUDENTS-TO-ADD-CODE - } - return 0; -} - -// public API function which is documented in the header file. -int model_move(model_t *instance, model_pos_t pos, model_state_t state) -{ - assert(instance); - assert_pos(pos); - if (model_get_state(instance, pos) == model_state_none && model_can_move(instance)) { - set_state(instance, pos, state); - return 1; - } - return 0; -} diff --git a/P05_TicTacToe/tic-tac-toe/src/model.h b/P05_TicTacToe/tic-tac-toe/src/model.h deleted file mode 100644 index 51e2678..0000000 --- a/P05_TicTacToe/tic-tac-toe/src/model.h +++ /dev/null @@ -1,108 +0,0 @@ -/** - * @file - * @brief MVC - Model instance - */ -#ifndef _MODEL_H_ -#define _MODEL_H_ - -#include - -#define MODEL_SIZE 3 ///< size of the game to avoid magic numbers in the code (not meant to modify) - -/** - * @brief The position on the board. - */ -typedef struct { - size_t row; ///< The row (0-based). - size_t col; ///< The column (0-based). -} model_pos_t; - -/** - * @brief Winner line direction - the winner line is given together with the start position. - */ -typedef enum { - model_dir_none, ///< no winner line - model_dir_h, ///< horizontal - model_dir_v, ///< vertical - model_dir_d, ///< diagonal -} model_dir_t; - -/** - * @brief The Winner line (if any). - */ -typedef struct { - model_dir_t dir; ///< the winner line direction (if any) - model_pos_t start; ///< the start position of the winner line -} model_line_t; - -/** - * @brief The state of a field. - */ -typedef enum { - model_state_none, ///< field available to play - model_state_a, ///< field already played - model_state_b, ///< field already played -} model_state_t; - -/** - * @brief The instance type. - */ -typedef struct { - model_state_t board[MODEL_SIZE][MODEL_SIZE]; ///< the play board -} model_t; - -/** - * @brief Convert to row and col to position. - * @param row [IN] position parameter - * @param col [IN] position parameter - * @return Returns the position given be row and col parameters. - */ -model_pos_t model_pos(size_t row, size_t col); - -/** - * @brief Constructor: initialize the instance memory. - * @param instance [INOUT] The instance which holds the state. - */ -void model_init(model_t *instance); - -/** - * @brief Queries the state of the given field. - * @param instance [INOUT] The instance which holds the state. - * @param pos [IN] The affected field. - * @return Returns the state of the field. - */ -model_state_t model_get_state(model_t *instance, model_pos_t pos); - -/** - * @brief Queries the winner (if any). - * @param instance [INOUT] The instance which holds the state. - * @return Returns the wining player or model_state_none if no winner (yet). - */ -model_state_t model_get_winner(model_t *instance); - -/** - * @brief Queries if a move is possible (i.e. not won yet and any field available?). - * @param instance [INOUT] The instance which holds the state. - * @return Returns 0 if no move possible, 1 otherwise. - */ -int model_can_move(model_t *instance); - -/** - * @brief Do a move if possible. - * @param instance [INOUT] The instance which holds the state. - * @param pos [IN] The field to play. - * @param state [IN] The new state (only model_state_a and model_state_b allowed). - * @return Returns if the attempt to move was successful. - * @remark Does only succeed if not yet won and if the field is available. - */ -int model_move(model_t *instance, model_pos_t pos, model_state_t state); - -/** - * @brief Gets the winner line (if any). - * @param instance [INOUT] The instance which holds the state. - * @returns The line which wins (if any). - * @remark The start position is 0/0, 1/0, 2/0 for horizontal, 0/0, 0/1, 0/2 for vertical, 0/0, 0/2 for diagonal. - */ -model_line_t model_get_win_line(model_t *instance); - -#endif // _MODEL_H_ diff --git a/P05_TicTacToe/tic-tac-toe/src/view.c b/P05_TicTacToe/tic-tac-toe/src/view.c deleted file mode 100644 index a248c49..0000000 --- a/P05_TicTacToe/tic-tac-toe/src/view.c +++ /dev/null @@ -1,255 +0,0 @@ -/** - * @file - * @brief Implementation - */ -#include "view.h" -#include "control.h" - -#include // assert() -#include // various i/o -#include // isdigit() -#include // STDIN_FILENO, isatty() -#include // tcgetattr(), tcsetattr() - -#define EXIT '0' ///< the UI exit request - -#define CLS "\033[2J" ///< ANSI termial CSI sequence for clear screen -#define AVAILABLE "\033[40m" ///< ANSI termial CSI sequence for available fields (black) -#define PLAYER_A "\033[42m" ///< ANSI termial CSI sequence for one player (green) -#define PLAYER_B "\033[41m" ///< ANSI termial CSI sequence for the other player (red) -#define GAP "\033[47m" ///< ANSI termial CSI sequence for boarder (white) -#define RESET "\033[0m" ///< ANSI termial CSI sequence to reset all settings - -#define CELL_WIDTH 10 ///< rendering parameter: columns per cell -#define CELL_HEIGHT 5 ///< rendering parameter: rows per cell -#define GAP_WIDTH 4 ///< rendering parameter: columns per border -#define GAP_HEIGHT 2 ///< rendering parameter: rows per boarder - -#define SIZE 3 ///< size of the game to avoid magic numbers in the code (not meant to modify) -#define CELLS (SIZE * SIZE) ///< size of the game to avoid magic numbers in the code (not meant to modify) - -/** - * @brief Position the cursor for further output. - * @param row [IN] position parameter - * @param col [IN] position parameter - */ -static void goto_pos(size_t row, size_t col) -{ - printf("\033[%zd;%zdH", row, col); -} - -/** - * @brief Displays a sequence of spaces at the given position in the given background color. - * @param row [IN] position parameter - * @param col [IN] position parameter - * @param width [IN] how many spaces to write - * @param color [IN] the format string before writing the spaces (intent: background color) - * @remark After writing the spaces, the format is reset. - */ -static size_t show_bar(size_t row, size_t col, size_t width, const char* color) -{ - goto_pos(row, col); - printf("%s", color); - for(size_t col = 0; col < width; col++) { - putchar(' '); - } - printf(RESET); - return col + width; -} - -/** - * @brief Displays a horizontal border over the whole board width. - * @param row [IN] position parameter - * @param col [IN] position parameter - */ -static size_t show_h_gap(size_t row, size_t col) { - for(size_t i = 0; i < GAP_HEIGHT; i++) { - show_bar(row+i, col, GAP_WIDTH + CELL_WIDTH + GAP_WIDTH, GAP); - } - return row + GAP_HEIGHT; -} - -/** - * @brief Writes for the call at position y/x the given number with the given background color. - * @param y [IN] position parameter: the upper left row of the cell - * @param x [IN] position parameter: the upper left column of the cell - * @param n [IN] the number to write as text - * @param color [IN] the format string before writing the text (intent: background color) - * @remark After writing the number, the format is reset. - */ -static void show_cell_nr(size_t y, size_t x, size_t n, const char *color) -{ - size_t cy = (y + y + CELL_HEIGHT)/2; - size_t cx = (x + x + CELL_WIDTH - 2)/2; - - goto_pos(cy, cx); - printf("%s", color); - printf("%2zd", n); - printf(RESET); -} - -/** - * @brief Renders the given cell with the given background color, including the surrounding border. - * @param n [IN] the cell number (0...CELLS-1) - * @param color [IN] the format string for the cell content (intent: background color) - * @remark After writing the number, the format is reset. - */ -static void show_cell(size_t n, const char *color) -{ - // goto upper-left corner of a cell (the cell starts with an upper and left gap) - size_t y = 1 + n / SIZE * (GAP_HEIGHT + CELL_HEIGHT); - size_t x = 1 + n % SIZE * (GAP_WIDTH + CELL_WIDTH); - - size_t row = show_h_gap(y, x); - for(size_t i = 0; i < CELL_HEIGHT; i++) { - size_t col = x; - col = show_bar(row, col, GAP_WIDTH, GAP); - col = show_bar(row, col, CELL_WIDTH, color); - col = show_bar(row, col, GAP_WIDTH, GAP); - row++; - } - row = show_h_gap(row, x); - show_cell_nr(y + GAP_HEIGHT, x + GAP_WIDTH, n + 1, color); - goto_pos(row, 0); -} - -/** - * @brief Renders the given player's name in the given background color. - * @param player [IN] the player to render (select the background color and the name of the player - * @remark After writing the content, the format is reset. - */ -static void print_player(control_player_t player) -{ - switch(player) { - case control_player_a: - printf(PLAYER_A); - printf("Player A"); - printf(RESET); - break; - case control_player_b: - printf(PLAYER_B); - printf("Player B"); - printf(RESET); - break; - default: - printf(RESET); - printf("none"); - break; - } -} - -/** - * @brief Displays a label followed by the given player. - * @param row [IN] position parameter - * @param col [IN] position parameter - * @param label [IN] the profixing label - * @param player [IN] the player to display - */ -static void show_player(size_t row, size_t col, const char *label, control_player_t player) -{ - goto_pos(row, col); - printf(RESET); - col += printf("%s", label); - goto_pos(row, col); - print_player(player); -} - -/** - * @brief Renders the winner and the next player. - * @param winner [IN] the winning player (if any) - * @param next [IN] the next player (if any) - */ -static void show_status(control_player_t winner, control_player_t next) -{ - size_t y = GAP_HEIGHT; - size_t x = SIZE * (GAP_WIDTH + CELL_WIDTH) + GAP_WIDTH + GAP_WIDTH; - size_t row = y; - size_t col = x; - - show_player(row, col, "Winner is: ", winner); - row += 2; - show_player(row, col, "Next player is: ", next); - row += 2; - row += 2; - goto_pos(row, col); - printf("0: exit"); - row += 2; - goto_pos(row, col); - printf("1..9: play field"); -} - -/** - * @brief Renders the board from the status given by the control instance. - * @param instance [IN] the instance which holds the control instance - */ -static void show(view_t *instance) -{ - assert(instance); - assert(instance->control); - puts(CLS); - show_status(control_get_winner(instance->control), control_get_player(instance->control)); - - for(size_t i = 0; i < CELLS; i++) { - const char *color = AVAILABLE; - switch(control_get_state(instance->control, i+1)) { - case control_player_a: - color = PLAYER_A; - break; - case control_player_b: - color = PLAYER_B; - break; - default: - break; - } - show_cell(i, color); - } -} - -/** - * @brief Processes the input and dsiplays the result. - * @param the instance which holds the control instance - */ -static void notifier_loop(view_t *instance) -{ - show(instance); - int c = getchar(); - while(c != EOF && c != EXIT) { - if (isdigit(c)) { - control_move(instance->control, c-'0'); - } - show(instance); - c = getchar(); - } -} - - -// public API function which is documented in the header file. -void view_init(view_t *instance, control_t *control) -{ - assert(instance); - assert(control); - instance->control = control; -} - -// public API function which is documented in the header file. -void view_run(view_t *instance) -{ - if (isatty(STDIN_FILENO)) { // in case of an interactive terminal, the exhoing and buffering is disabled - // declare non POSIX function, which is available in glibc, but not in strinct C99 mode - void cfmakeraw(struct termios *termios_p); - - // replace original tty IO state... - struct termios orig; - struct termios raw; - cfmakeraw(&raw); - tcgetattr(STDIN_FILENO, &orig); - tcsetattr(STDIN_FILENO, TCSANOW, &raw); - // ...do the work... - notifier_loop(instance); - // ...and finalle restore original tty IO state - tcsetattr(STDIN_FILENO, TCSANOW, &orig); - } else { // if not an interactive terminal, no tweaking with IO is done - notifier_loop(instance); - } -} - diff --git a/P05_TicTacToe/tic-tac-toe/src/view.h b/P05_TicTacToe/tic-tac-toe/src/view.h deleted file mode 100644 index 84cee90..0000000 --- a/P05_TicTacToe/tic-tac-toe/src/view.h +++ /dev/null @@ -1,31 +0,0 @@ -/** - * @file - * @brief MVC - View instance - */ -#ifndef _VIEW_H_ -#define _VIEW_H_ - -#include "control.h" - -/** - * @brief The instance type. - */ -typedef struct { - control_t *control; ///< the instance knows of the control instance -} view_t; - -/** - * @brief Constructor: initialize the instance memory. - * @param instance [INOUT] The instance which holds the state. - * @param control [IN] Dependency Injection of the control instance. - */ -void view_init(view_t *instance, control_t *control); - -/** - * @brief Starts the notifyer loop: accepts input and displays the results. - * @param instance [INOUT] The instance which holds the state. - * @remark Does only return when termination is requested through the UI. - */ -void view_run(view_t *instance); - -#endif // _VIEW_H_ diff --git a/P05_TicTacToe/tic-tac-toe/tests/tests.c b/P05_TicTacToe/tic-tac-toe/tests/tests.c deleted file mode 100644 index f2d15df..0000000 --- a/P05_TicTacToe/tic-tac-toe/tests/tests.c +++ /dev/null @@ -1,445 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Test suite for the given package. - */ -#include -#include -#include -#include -#include -#include -#include "test_utils.h" -#include "model.h" - -#ifndef TARGET // must be given by the make file --> see test target -#error missing TARGET define -#endif - -/// @brief alias for EXIT_SUCCESS -#define OK EXIT_SUCCESS -/// @brief alias for EXIT_FAILURE -#define FAIL EXIT_FAILURE - -/// @brief The name of the STDOUT text file. -#define OUTFILE "stdout.txt" -/// @brief The name of the STDERR text file. -#define ERRFILE "stderr.txt" - -#define TRACE_INDENT "\n " ///< allow for better stdout formatting in case of error - -// setup & cleanup -static int setup(void) -{ - remove_file_if_exists(OUTFILE); - remove_file_if_exists(ERRFILE); - return 0; // success -} - -static int teardown(void) -{ - // Do nothing. - // Especially: do not remove result files - they are removed in int setup(void) *before* running a test. - return 0; // success -} - -// test utils -static void init_model(model_t *instance, int act) -{ - if (act) printf(TRACE_INDENT "init_model:... "); - model_init(instance); - for(size_t row = 0; row < MODEL_SIZE; row++) { - for(size_t col = 0; col < MODEL_SIZE; col++) { - if (act) printf("%zd/%zd ", row, col); - CU_ASSERT_EQUAL_FATAL(instance->board[row][col], model_state_none); - } - } - if (act) printf(TRACE_INDENT); -} - -static void print_board(model_state_t board[MODEL_SIZE][MODEL_SIZE]) -{ - for(size_t row = 0; row < MODEL_SIZE; row++) { - printf("{ "); - for(size_t col = 0; col < MODEL_SIZE; col++) { - printf("%d ", board[row][col]); - } - printf("} "); - } -} - - -// tests -static void test_model_init(void) -{ - // check void model_init(model_t *instance); - - // arrange - model_t model; - // act & assert - init_model(&model, 1); -} - -static void test_model_get_state(void) -{ - // check: model_state_t model_get_state(model_t *instance, model_pos_t pos); - - { - // arrange - model_t model; - init_model(&model, 0); - // act & assert - printf(TRACE_INDENT "initial state:... "); - print_board(model.board); - for(size_t row = 0; row < MODEL_SIZE; row++) { - for(size_t col = 0; col < MODEL_SIZE; col++) { - printf("%zd/%zd ", row, col); - CU_ASSERT_EQUAL_FATAL(model_get_state(&model, model_pos(row, col)), model_state_none); - } - } - } - { - // arrange - static const model_state_t board[MODEL_SIZE][MODEL_SIZE] = { - { model_state_none, model_state_a, model_state_b }, - { model_state_a, model_state_b, model_state_none }, - { model_state_b, model_state_none, model_state_a }, - }; - model_t model; - init_model(&model, 0); - memcpy(model.board, board, sizeof(board)); - - // act & assert - printf(TRACE_INDENT "modified state:... "); - print_board(model.board); - for(size_t row = 0; row < MODEL_SIZE; row++) { - for(size_t col = 0; col < MODEL_SIZE; col++) { - printf("%zd/%zd ", row, col); - CU_ASSERT_EQUAL_FATAL(model_get_state(&model, model_pos(row, col)), board[row][col]); - } - } - } - printf(TRACE_INDENT); -} - -static void test_model_get_winner(void) -{ - // check: model_state_t model_get_winner(model_t *instance); - - { - // arrange - model_t model; - init_model(&model, 0); - // act & assert - printf(TRACE_INDENT "initial no winner:... "); - print_board(model.board); - CU_ASSERT_EQUAL_FATAL(model_get_winner(&model), model_state_none); - } - { - // arrange - static const model_state_t board[MODEL_SIZE][MODEL_SIZE] = { - { model_state_none, model_state_a, model_state_b }, - { model_state_a, model_state_b, model_state_none }, - { model_state_b, model_state_none, model_state_a }, - }; - model_t model; - init_model(&model, 0); - memcpy(model.board, board, sizeof(board)); - - // act & assert - printf(TRACE_INDENT "winner:... "); - print_board(model.board); - CU_ASSERT_EQUAL_FATAL(model_get_winner(&model), model_state_b); - } - printf(TRACE_INDENT); -} - -static void test_model_can_move(void) -{ - // check: int model_can_move(model_t *instance); - - { - // arrange - model_t model; - init_model(&model, 0); - // act & assert - printf(TRACE_INDENT "initial can move:... "); - print_board(model.board); - CU_ASSERT_EQUAL_FATAL(model_can_move(&model), 1); - } - { - // arrange - static const model_state_t board[MODEL_SIZE][MODEL_SIZE] = { - { model_state_none, model_state_a, model_state_a }, - { model_state_a, model_state_b, model_state_none }, - { model_state_b, model_state_none, model_state_b }, - }; - model_t model; - init_model(&model, 0); - memcpy(model.board, board, sizeof(board)); - - // act & assert - printf(TRACE_INDENT "can move while not yet done nor win:... "); - print_board(model.board); - CU_ASSERT_EQUAL_FATAL(model_can_move(&model), 1); - } - { - // arrange - static const model_state_t board[MODEL_SIZE][MODEL_SIZE] = { - { model_state_none, model_state_a, model_state_b }, - { model_state_a, model_state_b, model_state_none }, - { model_state_b, model_state_none, model_state_a }, - }; - model_t model; - init_model(&model, 0); - memcpy(model.board, board, sizeof(board)); - - // act & assert - printf(TRACE_INDENT "cannot move after win:... "); - print_board(model.board); - CU_ASSERT_EQUAL_FATAL(model_can_move(&model), 0); - } - { - // arrange - static const model_state_t board[MODEL_SIZE][MODEL_SIZE] = { - { model_state_b, model_state_a, model_state_a }, - { model_state_a, model_state_b, model_state_b }, - { model_state_b, model_state_a, model_state_a }, - }; - model_t model; - init_model(&model, 0); - memcpy(model.board, board, sizeof(board)); - - // act & assert - printf(TRACE_INDENT "cannot move when all done:... "); - print_board(model.board); - CU_ASSERT_EQUAL_FATAL(model_can_move(&model), 0); - } - printf(TRACE_INDENT); -} - -static void test_model_move(void) -{ - // check: int model_move(model_t *instance, model_pos_t pos, model_state_t state); - - { - // arrange - model_t model; - init_model(&model, 0); - // act & assert - printf(TRACE_INDENT "initial move:... "); - print_board(model.board); - model_pos_t pos_a = model_pos(0, 0); - printf("%zd/%zd ", pos_a.row, pos_a.col); - CU_ASSERT_EQUAL_FATAL(model_move(&model, pos_a, model_state_a), 1); - CU_ASSERT_EQUAL_FATAL(model_move(&model, pos_a, model_state_a), 0); - CU_ASSERT_EQUAL_FATAL(model_move(&model, pos_a, model_state_b), 0); - model_pos_t pos_b = model_pos(2, 2); - printf("%zd/%zd ", pos_b.row, pos_b.col); - CU_ASSERT_EQUAL_FATAL(model_move(&model, pos_b, model_state_b), 1); - CU_ASSERT_EQUAL_FATAL(model_move(&model, pos_b, model_state_b), 0); - CU_ASSERT_EQUAL_FATAL(model_move(&model, pos_b, model_state_a), 0); - } - { - // arrange - static const model_state_t board[MODEL_SIZE][MODEL_SIZE] = { - { model_state_none, model_state_a, model_state_a }, - { model_state_a, model_state_b, model_state_none }, - { model_state_b, model_state_none, model_state_b }, - }; - model_t model; - init_model(&model, 0); - memcpy(model.board, board, sizeof(board)); - - // act & assert - printf(TRACE_INDENT "can move while not yet done nor win:... "); - print_board(model.board); - model_pos_t pos = model_pos(2, 1); - CU_ASSERT_EQUAL_FATAL(model_move(&model, pos, model_state_a), 1); - CU_ASSERT_EQUAL_FATAL(model_move(&model, pos, model_state_a), 0); - CU_ASSERT_EQUAL_FATAL(model_move(&model, pos, model_state_b), 0); - } - { - // arrange - static const model_state_t board[MODEL_SIZE][MODEL_SIZE] = { - { model_state_none, model_state_a, model_state_b }, - { model_state_a, model_state_b, model_state_none }, - { model_state_b, model_state_none, model_state_a }, - }; - model_t model; - init_model(&model, 0); - memcpy(model.board, board, sizeof(board)); - - // act & assert - printf(TRACE_INDENT "cannot move after win:... "); - print_board(model.board); - model_pos_t pos = model_pos(2, 1); - CU_ASSERT_EQUAL_FATAL(model_move(&model, pos, model_state_a), 0); - CU_ASSERT_EQUAL_FATAL(model_move(&model, pos, model_state_b), 0); - } - { - // arrange - static const model_state_t board[MODEL_SIZE][MODEL_SIZE] = { - { model_state_b, model_state_a, model_state_a }, - { model_state_a, model_state_b, model_state_b }, - { model_state_b, model_state_a, model_state_a }, - }; - model_t model; - init_model(&model, 0); - memcpy(model.board, board, sizeof(board)); - - // act & assert - printf(TRACE_INDENT "cannot move when all done:... "); - print_board(model.board); - for(size_t row = 0; row < MODEL_SIZE; row++) { - for(size_t col = 0; col < MODEL_SIZE; col++) { - model_pos_t pos = model_pos(row, col); - printf("%zd/%zd ", row, col); - CU_ASSERT_EQUAL_FATAL(model_move(&model, pos, model_state_a), 0); - CU_ASSERT_EQUAL_FATAL(model_move(&model, pos, model_state_b), 0); - } - } - CU_ASSERT_EQUAL_FATAL(model_can_move(&model), 0); - } - printf(TRACE_INDENT); -} - -static void test_model_get_win_line(void) -{ - // check: model_line_t model_get_win_line(model_t *instance); - - { - // arrange - model_t model; - init_model(&model, 0); - // act & assert - printf(TRACE_INDENT "initial no winner:... "); - print_board(model.board); - model_line_t no_win = model_get_win_line(&model); - CU_ASSERT_EQUAL_FATAL(no_win.dir, model_dir_none); - } - { - // arrange - static const model_state_t board[MODEL_SIZE][MODEL_SIZE] = { - { model_state_none, model_state_a, model_state_a }, - { model_state_a, model_state_b, model_state_none }, - { model_state_b, model_state_none, model_state_b }, - }; - model_t model; - init_model(&model, 0); - memcpy(model.board, board, sizeof(board)); - - // act & assert - printf(TRACE_INDENT "no winner while not yet done nor win:... "); - print_board(model.board); - model_line_t no_win = model_get_win_line(&model); - CU_ASSERT_EQUAL_FATAL(no_win.dir, model_dir_none); - } - { - // arrange - static const model_state_t board[MODEL_SIZE][MODEL_SIZE] = { - { model_state_b, model_state_a, model_state_a }, - { model_state_a, model_state_b, model_state_b }, - { model_state_b, model_state_a, model_state_a }, - }; - model_t model; - init_model(&model, 0); - memcpy(model.board, board, sizeof(board)); - - // act & assert - printf(TRACE_INDENT "no winner when all done:... "); - print_board(model.board); - model_line_t no_win = model_get_win_line(&model); - CU_ASSERT_EQUAL_FATAL(no_win.dir, model_dir_none); - } - { - for(size_t row = 0; row < MODEL_SIZE; row++) { - // arrange - model_t model; - init_model(&model, 0); - for(size_t col = 0; col < MODEL_SIZE; col++) { - CU_ASSERT_EQUAL_FATAL(model_move(&model, model_pos(row, col), model_state_a), 1); - } - // act & assert - printf(TRACE_INDENT "row winner:... "); - print_board(model.board); - model_line_t win = model_get_win_line(&model); - CU_ASSERT_EQUAL_FATAL(win.dir, model_dir_h); - CU_ASSERT_EQUAL_FATAL(win.start.row, row); - CU_ASSERT_EQUAL_FATAL(win.start.col, 0); - } - } - { - for(size_t col = 0; col < MODEL_SIZE; col++) { - // arrange - model_t model; - init_model(&model, 0); - for(size_t row = 0; row < MODEL_SIZE; row++) { - CU_ASSERT_EQUAL_FATAL(model_move(&model, model_pos(row, col), model_state_a), 1); - } - // act & assert - printf(TRACE_INDENT "column winner:... "); - print_board(model.board); - model_line_t win = model_get_win_line(&model); - CU_ASSERT_EQUAL_FATAL(win.dir, model_dir_v); - CU_ASSERT_EQUAL_FATAL(win.start.row, 0); - CU_ASSERT_EQUAL_FATAL(win.start.col, col); - } - } - { - printf(TRACE_INDENT "diagonal left-right winner:... "); - // arrange - model_t model; - init_model(&model, 0); - for(size_t i = 0; i < MODEL_SIZE; i++) { - CU_ASSERT_EQUAL_FATAL(model_move(&model, model_pos(i, i), model_state_a), 1); - } - // act & assert - print_board(model.board); - model_line_t win = model_get_win_line(&model); - CU_ASSERT_EQUAL_FATAL(win.dir, model_dir_d); - CU_ASSERT_EQUAL_FATAL(win.start.row, 0); - CU_ASSERT_EQUAL_FATAL(win.start.col, 0); - } - { - printf(TRACE_INDENT "diagonal right-left winner:... "); - // arrange - model_t model; - init_model(&model, 0); - for(size_t i = 0; i < MODEL_SIZE; i++) { - CU_ASSERT_EQUAL_FATAL(model_move(&model, model_pos(MODEL_SIZE - 1 - i, i), model_state_a), 1); - } - // act & assert - print_board(model.board); - model_line_t win = model_get_win_line(&model); - CU_ASSERT_EQUAL_FATAL(win.dir, model_dir_d); - CU_ASSERT_EQUAL_FATAL(win.start.row, 0); - CU_ASSERT_EQUAL_FATAL(win.start.col, MODEL_SIZE - 1); - } - printf(TRACE_INDENT); -} - -/** - * @brief Registers and runs the tests. - * @returns success (0) or one of the CU_ErrorCode (>0) - */ -int main(void) -{ - // setup, run, teardown - TestMainBasic("lab test", setup, teardown - , test_model_init - , test_model_get_state - , test_model_get_winner - , test_model_can_move - , test_model_move - , test_model_get_win_line - ); -} diff --git a/P06_Integer_Rechner/P06_Integer_Rechner.pdf b/P06_Integer_Rechner/P06_Integer_Rechner.pdf deleted file mode 100644 index 03ce527..0000000 Binary files a/P06_Integer_Rechner/P06_Integer_Rechner.pdf and /dev/null differ diff --git a/P06_Integer_Rechner/integer-rechner/Makefile b/P06_Integer_Rechner/integer-rechner/Makefile deleted file mode 100644 index fcaf8fd..0000000 --- a/P06_Integer_Rechner/integer-rechner/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -SNP_SHARED_MAKEFILE := $(if $(SNP_SHARED_MAKEFILE),$(SNP_SHARED_MAKEFILE),"~/snp/shared.mk") - -TARGET := bin/integer-rechner -MODULES := src/calc.c src/scan.c src/stack.c src/eval.c src/error.c -SOURCES := src/main.c $(MODULES) -TSTSOURCES := tests/tests.c $(MODULES) - -include $(SNP_SHARED_MAKEFILE) - diff --git a/P06_Integer_Rechner/integer-rechner/mainpage.dox b/P06_Integer_Rechner/integer-rechner/mainpage.dox deleted file mode 100644 index 6325e98..0000000 --- a/P06_Integer_Rechner/integer-rechner/mainpage.dox +++ /dev/null @@ -1,8 +0,0 @@ -/** - * @mainpage SNP - P06 Integer Rechner - * - * @section Purpose - * - * This is a lab on usage of arrays. - * - */ diff --git a/P06_Integer_Rechner/integer-rechner/src/calc.c b/P06_Integer_Rechner/integer-rechner/src/calc.c deleted file mode 100644 index 70552f8..0000000 --- a/P06_Integer_Rechner/integer-rechner/src/calc.c +++ /dev/null @@ -1,168 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Lab implementation - */ -#include -#include -#include "calc.h" -#include "eval.h" -#include "error.h" - -/** - * @brief The details of the otherwise opaque type. - */ -struct calc { - scan_t *scan; ///< DI: the scanner (this instance does not take over ownership) - eval_t *eval; ///< DI: the evaluator (this instance does not take over ownership) -}; - -static void parse_expression(calc_t *instance); - -static eval_op_t unary_tk2op(int tk) { - switch(tk) { - case '~': return OP_INV; - case '+': return OP_NOP; - case '-': return OP_CHS; - default: - error_fatal("not supported unary op"); - return OP_NOP; - } -} - -static eval_op_t binary_tk2op(int tk) { - switch(tk) { - case '|': return OP_BIT_OR; - case '^': return OP_BIT_XOR; - case '&': return OP_BIT_AND; - case TK_LSHIFT: return OP_BIT_LEFT; - case TK_RSHIFT: return OP_BIT_RIGHT; - case '+': return OP_ADD; - case '-': return OP_SUB; - case '*': return OP_MUL; - case '/': return OP_DIV; - case '%': return OP_MOD; - default: - error_fatal("not supported binary op"); - return OP_NOP; - } -} - - -static int parse_optional_op(calc_t *instance, const int ops[]) -{ - assert(instance); - assert(ops); - const int *op = ops; - while(*op && *op != scan_curr_token(instance->scan)) op++; - if (*op) scan_next_token(instance->scan); - return *op; -} - -static void parse_unary_op(calc_t *instance, void (*parse)(calc_t *), const int ops[]) -{ - assert(instance); - int op = parse_optional_op(instance, ops); - if (op) { - parse_unary_op(instance, parse, ops); - eval_op(instance->eval, unary_tk2op(op)); - } - else { - (*parse)(instance); - } -} - -static void parse_binary_op(calc_t *instance, void (*parse)(calc_t *), const int ops[]) -{ - assert(instance); - (*parse)(instance); - int op = parse_optional_op(instance, ops); - while (op) { - (*parse)(instance); - eval_op(instance->eval, binary_tk2op(op)); - op = parse_optional_op(instance, ops); - } -} - -static void parse_nested_expression(calc_t *instance) -{ - assert(instance); - // printf("parse_nested\n"); - if ('(' != scan_curr_token(instance->scan)) error_fatal("number or '(' ... ')' expected"); - if (scan_next_token(instance->scan) == TK_EOT) error_fatal("expression expected"); - parse_expression(instance); - if (scan_curr_token(instance->scan) != ')') error_fatal("closing ')' expected"); - scan_next_token(instance->scan); -} - -static void parse_number(calc_t *instance) -{ - assert(instance); - // printf("parse_number\n"); - if (scan_curr_token(instance->scan) != TK_NUM) error_fatal("number expected"); - eval_number(instance->eval, scan_curr_value(instance->scan)); - scan_next_token(instance->scan); -} - -static void parse_primary(calc_t *instance) -{ - assert(instance); - // printf("parse_primary(%d)\n", scan_curr_token(instance->scan)); - int t = scan_curr_token(instance->scan); - if (t == TK_NUM) parse_number(instance); - else if (t == '(') parse_nested_expression(instance); - else error_fatal("number or '(' ... ')' expected"); -} - -static void parse_unary(calc_t *instance) { parse_unary_op(instance, parse_primary, (const int[]){ '+', '-', '~', 0 }); } - -static void parse_mul(calc_t *instance) { parse_binary_op(instance, parse_unary, (const int[]){ '*', '/', '%', 0 }); } - -static void parse_sum(calc_t *instance) { parse_binary_op(instance, parse_mul, (const int[]){ '+', '-', 0 }); } - -static void parse_bit_shift(calc_t *instance) { parse_binary_op(instance, parse_sum, (const int[]){ TK_LSHIFT, TK_RSHIFT, 0 }); } - -static void parse_bit_and(calc_t *instance) { parse_binary_op(instance, parse_bit_shift, (const int[]){ '&', 0 }); } - -static void parse_bit_xor(calc_t *instance) { parse_binary_op(instance, parse_bit_and, (const int[]){ '^', 0 }); } - -static void parse_bit_or(calc_t *instance) { parse_binary_op(instance, parse_bit_xor, (const int[]){ '|', 0 }); } - -static void parse_expression(calc_t *instance) { parse_bit_or(instance); } - - -calc_t *calc_new(scan_t *scan, eval_t *eval) -{ - assert(scan); - assert(eval); - calc_t *instance = malloc(sizeof(calc_t)); - if (!instance) error_fatal_errno("calc_new: instance"); - instance->scan = scan; - instance->eval = eval; - return instance; -} - -void calc_destroy(calc_t *instance) -{ - assert(instance); - free(instance); -} - -void calc_execute(calc_t *instance) -{ - assert(instance); - if (scan_next_token(instance->scan) == TK_EOT) error_fatal("missing expression text"); - parse_expression(instance); - // printf("excess = %d\n", scan_curr_token(instance->scan)); - if (scan_curr_token(instance->scan) != TK_EOT) error_fatal("excess expression text found"); - - eval_result(instance->eval); -} diff --git a/P06_Integer_Rechner/integer-rechner/src/calc.h b/P06_Integer_Rechner/integer-rechner/src/calc.h deleted file mode 100644 index 65635e8..0000000 --- a/P06_Integer_Rechner/integer-rechner/src/calc.h +++ /dev/null @@ -1,50 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Infix calculator - */ - -#ifndef _CALC_H_ -#define _CALC_H_ - -#include "scan.h" -#include "eval.h" - -/** - * @brief Opaque type - detailed in the implementation only. - */ -typedef struct calc calc_t; - -/** - * @brief Construcor. - * @param scan [IN] Dependency Injection: the serving scanner. - * @param eval [IN] Dependency Injection: the serving evaluator. - * @return The newly created and initialized instance - * @remark In case of error, the program terminates with EXIT_FAILURE and an appropriate error message. - * @remark Does not take over ownership of the injected dependencies. - */ -calc_t *calc_new(scan_t *scan, eval_t *eval); - -/** - * @brief Frees the instances memory and the data it owns. - * @param instance [INOUT] The affected instance. - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - */ -void calc_destroy(calc_t *instance); - -/** - * @brief Entry point for processing the expression from stdin. - * @param instance [INOUT] The affected instance. - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - */ -void calc_execute(calc_t *instance); - -#endif // _CALC_H_ diff --git a/P06_Integer_Rechner/integer-rechner/src/error.c b/P06_Integer_Rechner/integer-rechner/src/error.c deleted file mode 100644 index d91aa69..0000000 --- a/P06_Integer_Rechner/integer-rechner/src/error.c +++ /dev/null @@ -1,36 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Errors - */ -#include -#include -#include "error.h" - -static size_t context = 0; -void error_set_pos(size_t pos) -{ - context = pos; -} - -void error_fatal(const char message[]) -{ - fprintf(stderr, "ERROR: at or near position %zd: %s\n", context, message); - exit(EXIT_FAILURE); -} - -void error_fatal_errno(const char message[]) -{ - fprintf(stderr, "ERROR: at or near position %zd: ", context); - perror(message); - exit(EXIT_FAILURE); -} - diff --git a/P06_Integer_Rechner/integer-rechner/src/error.h b/P06_Integer_Rechner/integer-rechner/src/error.h deleted file mode 100644 index 744ebde..0000000 --- a/P06_Integer_Rechner/integer-rechner/src/error.h +++ /dev/null @@ -1,36 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Error handling - */ - -#ifndef _ERROR_H_ -#define _ERROR_H_ - -/** - * @brief Sets the context of the current token for potential error reports. - * @param pos [IN] The current input start position of the token which is currently processed. - */ -void error_set_pos(size_t pos); - -/** - * @brief Issues on stderr an error message and terminates the program with EXIT_FAILURE. - * @param message [IN] Context information. - */ -void error_fatal(const char message[]); - -/** - * @brief Issues on stderr an error message (includeing the errno text) and terminates the program with EXIT_FAILURE. - * @param message [IN] Context information. - */ -void error_fatal_errno(const char message[]); - -#endif // _ERROR_H_ diff --git a/P06_Integer_Rechner/integer-rechner/src/eval.c b/P06_Integer_Rechner/integer-rechner/src/eval.c deleted file mode 100644 index 9fce346..0000000 --- a/P06_Integer_Rechner/integer-rechner/src/eval.c +++ /dev/null @@ -1,244 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief RPN Evaluator - */ -#include -#include -#include -#include "eval.h" -#include "error.h" - -/** - * @brief The details of the otherwise opaque type. - */ -struct eval { - stack_t *stack; ///< DI: the stack (this instance does not take over ownership) - int verbose; ///< verbosity control (0 = silent, 1 = verbose) -}; - -static void write_op(FILE *fd, eval_op_t op) -{ - switch(op) { - case OP_NOP: fprintf(fd, "OP_NOP\n"); break; - case OP_PRINT_HEX: fprintf(fd, "OP_PRINT_HEX\n"); break; - case OP_PRINT_DEC: fprintf(fd, "OP_PRINT_DEC\n"); break; - case OP_PRINT_OCT: fprintf(fd, "OP_PRINT_OCT\n"); break; - case OP_PRINT_BIN: fprintf(fd, "OP_PRINT_BIN\n"); break; - case OP_BIT_OR: fprintf(fd, "OP_BIT_OR\n"); break; - case OP_BIT_XOR: fprintf(fd, "OP_BIT_XOR\n"); break; - case OP_BIT_AND: fprintf(fd, "OP_BIT_AND\n"); break; - case OP_BIT_LEFT: fprintf(fd, "OP_BIT_LEFT\n"); break; - case OP_BIT_RIGHT: fprintf(fd, "OP_BIT_RIGHT\n"); break; - case OP_ADD: fprintf(fd, "OP_ADD\n"); break; - case OP_SUB: fprintf(fd, "OP_SUB\n"); break; - case OP_MUL: fprintf(fd, "OP_MUL\n"); break; - case OP_DIV: fprintf(fd, "OP_DIV\n"); break; - case OP_MOD: fprintf(fd, "OP_MOD\n"); break; - case OP_CHS: fprintf(fd, "OP_CHS\n"); break; - case OP_INV: fprintf(fd, "OP_INV\n"); break; - } -} - -static void write_value(FILE *fd, eval_op_t op, unsigned int value) -{ - const unsigned int top_bit = 1 << (sizeof(unsigned int) * 8 - 1); - switch(op) { - case OP_PRINT_HEX: - fprintf(fd, "hex=0x%0*x\n", (int)(sizeof(unsigned int)*2), value); - break; - case OP_PRINT_DEC: - fprintf(fd, "dec=%d\n", value); - break; - case OP_PRINT_OCT: - fprintf(fd, "oct=0%0*o\n", (int)((sizeof(unsigned int)*8+2)/3), value); - break; - case OP_PRINT_BIN: - fprintf(fd, "bin=0b"); - for(size_t i = 0; i < sizeof(unsigned int)*8; i++) { - fputc((value & top_bit) ? '1' : '0', fd); - value <<= 1; - } - fputc('\n', fd); - break; - default: - break; - } -} - -static int eval_top(eval_t *instance, eval_op_t op) -{ - assert(instance); - assert(instance->stack); - - switch(op) { - case OP_NOP: - return 1; - case OP_PRINT_HEX: - case OP_PRINT_DEC: - case OP_PRINT_OCT: - case OP_PRINT_BIN: - write_value(stdout, op, stack_top(instance->stack)); - return 1; - default: - break; - } - return 0; -} - -static int eval_unary(eval_t *instance, eval_op_t op) -{ - assert(instance); - assert(instance->stack); - - unsigned int v; - switch(op) { - case OP_CHS: - v = stack_pop(instance->stack); - stack_push(instance->stack, -v); - return 1; - case OP_INV: - // 1. implement the ~ operator analogous to the - sign operator above - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - // END-STUDENTS-TO-ADD-CODE - default: - break; - } - return 0; -} - -static int eval_binary(eval_t *instance, eval_op_t op) -{ - assert(instance); - assert(instance->stack); - - unsigned int a; - unsigned int b; - - switch(op) { - case OP_ADD: - b = stack_pop(instance->stack); - a = stack_pop(instance->stack); - stack_push(instance->stack, a + b); - return 1; - case OP_SUB: - b = stack_pop(instance->stack); - a = stack_pop(instance->stack); - stack_push(instance->stack, a - b); - return 1; - case OP_MUL: - b = stack_pop(instance->stack); - a = stack_pop(instance->stack); - stack_push(instance->stack, a * b); - return 1; - case OP_DIV: - b = stack_pop(instance->stack); - a = stack_pop(instance->stack); - stack_push(instance->stack, a / b); - return 1; - case OP_MOD: - b = stack_pop(instance->stack); - a = stack_pop(instance->stack); - stack_push(instance->stack, a % b); - return 1; - case OP_BIT_OR: - // 1. implement the | operator analogous to the * operator above - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - // END-STUDENTS-TO-ADD-CODE - case OP_BIT_XOR: - // 1. implement the ^ operator analogous to the * operator above - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - // END-STUDENTS-TO-ADD-CODE - case OP_BIT_AND: - // 1. implement the & operator analogous to the * operator above - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - // END-STUDENTS-TO-ADD-CODE - case OP_BIT_LEFT: - // 1. implement the << operator analogous to the * operator above - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - // END-STUDENTS-TO-ADD-CODE - case OP_BIT_RIGHT: - // 1. implement the >> operator analogous to the * operator above - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - // END-STUDENTS-TO-ADD-CODE - default: - break; - } - return 0; -} - -eval_t *eval_new(stack_t *stack, int verbose) -{ - assert(stack); - eval_t *instance = malloc(sizeof(eval_t)); - if (!instance) error_fatal_errno("eval_new: instance"); - instance->stack = stack; - instance->verbose = verbose; - return instance; -} - -void eval_destroy(eval_t *instance) -{ - assert(instance); - free(instance); -} - -void eval_number(eval_t *instance, unsigned int value) -{ - assert(instance); - stack_push(instance->stack, value); - if (instance->verbose) write_value(stderr, OP_PRINT_HEX, value); -} - -void eval_op(eval_t *instance, eval_op_t op) -{ - if (eval_top(instance, op) || eval_unary(instance, op) || eval_binary(instance, op)) { - // successfully evaluated - if (instance->verbose) write_op(stderr, op); - } else { - error_fatal("eval_op: unknown op"); - } -} - -void eval_result(eval_t *instance) -{ - assert(instance); - unsigned int v = stack_top(instance->stack); - printf("--- RESULT ---\n"); - write_value(stdout, OP_PRINT_DEC, v); - write_value(stdout, OP_PRINT_HEX, v); - write_value(stdout, OP_PRINT_OCT, v); - write_value(stdout, OP_PRINT_BIN, v); -} diff --git a/P06_Integer_Rechner/integer-rechner/src/eval.h b/P06_Integer_Rechner/integer-rechner/src/eval.h deleted file mode 100644 index 91f24e4..0000000 --- a/P06_Integer_Rechner/integer-rechner/src/eval.h +++ /dev/null @@ -1,87 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief RPN Evaluator - */ -#ifndef _EVAL_H_ -#define _EVAL_H_ - -#include "stack.h" - -/** - * @brief All RPN Operators. - */ -typedef enum { - OP_NOP, ///< no operation - OP_PRINT_HEX, ///< print as hex - OP_PRINT_DEC, ///< print as dec - OP_PRINT_OCT, ///< print as oct - OP_PRINT_BIN, ///< print as bin - OP_BIT_OR, ///< unsigned integer bit or - OP_BIT_XOR, ///< unsigned integer bit xor - OP_BIT_AND, ///< unsigned integer bit and - OP_BIT_LEFT, ///< unsigned integer shift-left - OP_BIT_RIGHT, ///< unsigned integer shift-right - OP_ADD, ///< unsigned integer addition - OP_SUB, ///< unsigned integer subtraction - OP_MUL, ///< unsigned integer multiplication - OP_DIV, ///< unsigned integer division - OP_MOD, ///< unsigned integer modulo - OP_CHS, ///< unsigned integer twos-complement - OP_INV, ///< unsigned integer ones-complement -} eval_op_t; - -/** - * @brief Opaque type - detailed in the implementation only. - */ -typedef struct eval eval_t; - -/** - * @brief Construcor. - * @param stack [IN] Dependency Injection: the serving stack. - * @param verbose [IN] Cointrol debug output (stderr): 0 = no output, 1 = additional output. - * @return The newly created and initialized instance - * @remark In case of error, the program terminates with EXIT_FAILURE and an appropriate error message. - * @remark Does not take over ownership of the injected dependencies. - */ -eval_t *eval_new(stack_t *stack, int verbose); - -/** - * @brief Frees the instances memory and the data it owns. - * @param instance [INOUT] The affected instance. - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - */ -void eval_destroy(eval_t *instance); - -/** - * @brief Feeds the value in RPN sequence to the evaluator. - * @param instance [INOUT] The affected instance. - * @param value [IN] The value to stack on the RPN calculator. - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - */ -void eval_number(eval_t *instance, unsigned int value); - -/** - * @brief Feeds the operation in RPN sequence to the evaluator. - * @param instance [INOUT] The affected instance. - * @param op [IN] The operator to evaluate. - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - */ -void eval_op(eval_t *instance, eval_op_t op); - -/** - * @brief Prints the top entry of the stack. - * @param instance [INOUT] The affected instance. - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - */ -void eval_result(eval_t *instance); - -#endif // _EVAL_H_ diff --git a/P06_Integer_Rechner/integer-rechner/src/main.c b/P06_Integer_Rechner/integer-rechner/src/main.c deleted file mode 100644 index f39f52e..0000000 --- a/P06_Integer_Rechner/integer-rechner/src/main.c +++ /dev/null @@ -1,42 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Lab implementation - */ -#include -#include -#include "calc.h" - -/** - * @brief Main entry point. - * @param[in] argc The size of the argv array. - * @param[in] argv The command line arguments... - * @returns Returns EXIT_SUCCESS (=0) on success, EXIT_FAILURE (=1) there is an expression syntax error. - */ -int main(int argc, char* argv[]) -{ - int verbose = argc > 1 && strtol(argv[1], NULL, 10); - - const size_t stack_size = 10; - stack_t *stack = stack_new(stack_size); - eval_t *eval = eval_new(stack, verbose); - scan_t *scan = scan_new(); - calc_t *calc = calc_new(scan, eval); - - calc_execute(calc); - - calc_destroy(calc); - scan_destroy(scan); - eval_destroy(eval); - stack_destroy(stack); - - return EXIT_SUCCESS; -} diff --git a/P06_Integer_Rechner/integer-rechner/src/scan.c b/P06_Integer_Rechner/integer-rechner/src/scan.c deleted file mode 100644 index 4912526..0000000 --- a/P06_Integer_Rechner/integer-rechner/src/scan.c +++ /dev/null @@ -1,204 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Infix calculator tokenizer - */ - -#include -#include -#include -#include -#include -#include "scan.h" -#include "error.h" - -/** - * @brief The details of the otherwise opaque type. - */ -struct scan { - size_t count; ///< counts the read characters (used for error reporting) - int la; ///< look-ahead-1 character - int token; ///< the currently recognized token - unsigned int value; ///< the current TK_NUM value -}; - - -static int next_char(scan_t *instance) -{ - assert(instance); - instance->la = getc(stdin); - if (instance->la != EOF) instance->count++; - return instance->la; -} - -static int curr_char(scan_t *instance) -{ - assert(instance); - return instance->la; -} - -static int skip_chars(scan_t *instance) -{ - assert(instance); - while(instance->la != EOF && isspace(instance->la)) next_char(instance); - return instance->la; -} - -static int eof(scan_t *instance) -{ - assert(instance); - // printf("eof?\n"); - if (curr_char(instance) == EOF) { - instance->token = TK_EOT; - return 1; - } - return 0; -} - -static int op1(scan_t *instance, const char ops[]) -{ - assert(instance); - // printf("op1?\n"); - assert(ops); - if (strchr(ops, curr_char(instance))) { - instance->token = curr_char(instance); - next_char(instance); - return 1; - } - return 0; -} - -static int op2(scan_t *instance, const char *op, int t) -{ - assert(instance); - // printf("op2?\n"); - assert(op); - assert(strlen(op) == 2); - if (curr_char(instance) == op[0] && next_char(instance) == op[1]) { - instance->token = t; - next_char(instance); - return 1; - } - return 0; -} - -static int number(scan_t *instance) -{ - assert(instance); - // printf("number\n"); - int c = curr_char(instance); - if (c == '0') { - instance->value = 0; - instance->token = TK_NUM; - c = next_char(instance); - if (strchr("xX", c)) { - instance->token = TK_ERROR; - c = next_char(instance); - while(isxdigit(c) || c == '_') { - if (c != '_') { - instance->value *= 16; - instance->token = TK_NUM; - if (isdigit(c)) { - instance->value += c - '0'; - } else { - instance->value += 10 + c - (isupper(c) ? 'A' : 'a'); - } - } - c = next_char(instance); - } - } else if (strchr("bB", c)) { - instance->token = TK_ERROR; - c = next_char(instance); - while(strchr("01_", c)) { - if (c != '_') { - instance->value *= 2; - instance->value += c - '0'; - instance->token = TK_NUM; - } - c = next_char(instance); - } - } else if (strchr("01234567_", c)) { - while(strchr("01234567_", c)) { - if (c != '_') { - instance->value *= 8; - instance->value += c - '0'; - } - c = next_char(instance); - } - } - return 1; - } else if (isdigit(c)) { - instance->value = 0; - while(isdigit(c) || c == '_') { - if (c != '_') { - instance->value *= 10; - instance->value += c - '0'; - c = next_char(instance); - } - } - instance->token = TK_NUM; - return 1; - } - return 0; -} - - - -scan_t *scan_new() -{ - scan_t *instance = malloc(sizeof(scan_t)); - if (!instance) error_fatal_errno("scan_new: instance"); - instance->count = 0; - instance->la = EOF; - instance->token = TK_EOT; - instance->value = 0; - return instance; -} - -void scan_destroy(scan_t *instance) -{ - assert(instance); - free(instance); -} - -int scan_next_token(scan_t *instance) -{ - assert(instance); - // printf("next...\n"); - int c = curr_char(instance); - if (c == EOF) c = next_char(instance); // initial (or final) situation - c = skip_chars(instance); - error_set_pos(instance->count); - - instance->token = TK_ERROR; - instance->value = 0; - - if (eof(instance) || op1(instance, "+-*%/|^&~()") || op2(instance, "<<", TK_LSHIFT) || op2(instance, ">>", TK_RSHIFT) || number(instance)) { - // one of the above was found - skip_chars(instance); - } - - // printf("next = %d\n", token); - return instance->token; -} - -int scan_curr_token(scan_t *instance) -{ - assert(instance); - return instance->token; -} - -unsigned int scan_curr_value(scan_t *instance) -{ - assert(instance); - if (scan_curr_token(instance) != TK_NUM) error_fatal_errno("scan_curr_value: wrong token"); - return instance->value; -} diff --git a/P06_Integer_Rechner/integer-rechner/src/scan.h b/P06_Integer_Rechner/integer-rechner/src/scan.h deleted file mode 100644 index 46a50ac..0000000 --- a/P06_Integer_Rechner/integer-rechner/src/scan.h +++ /dev/null @@ -1,75 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Infix calculator tokenizer - */ - -#ifndef _SCAN_H_ -#define _SCAN_H_ - -/** - * @brief Opaque type - detailed in the implementation only. - */ -typedef struct scan scan_t; - -/** - * @brief Specific token enum values beside the ones provided as plain characters. - * @remark The last enum value must be below printable ascii code - */ -typedef enum { - TK_ERROR = 0, ///< error or end mark for arrays of tokens (must be 0) - TK_EOT, ///< end of text, i.e. when EOF is recognized - TK_LSHIFT, ///< left-shift operator (2-char token) - TK_RSHIFT, ///< right-shift operator (2-char token) - TK_NUM, ///< number token (e.g. 3, 0x1F, etc.) -} scan_tk_t; - -/** - * @brief Construcor. - * @return The newly created and initialized instance - * @remark In case of error, the program terminates with EXIT_FAILURE and an appropriate error message. - */ -scan_t *scan_new(); - -/** - * @brief Frees the instances memory and the data it owns. - * @param instance [INOUT] The affected instance. - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - */ -void scan_destroy(scan_t *instance); - -/** - * @brief Moves to the next token. - * @param instance [INOUT] The affected instance. - * @return Returns the next recognized token (single character token or scan_tk_t enum value). - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - * @remark If the token is a TK_NUM, the scan_curr_value() function returns the associated value. - */ -int scan_next_token(scan_t *instance); - -/** - * @brief Gets the token which mas found with the last scan_next_token() call. - * @param instance [INOUT] The affected instance. - * @return Returns the currently recognized token (single character token or scan_tk_t enum value). - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - */ -int scan_curr_token(scan_t *instance); - -/** - * @brief Gets the value associated with the TK_NUM token. - * @param instance [INOUT] The affected instance. - * @return Returns the value associated with the current TK_NUM. - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - * @remark This only gives valid values if the scan_curr_token() equals TK_NUM. - */ -unsigned int scan_curr_value(scan_t *instance); - -#endif // _SCAN_H_ diff --git a/P06_Integer_Rechner/integer-rechner/src/stack.c b/P06_Integer_Rechner/integer-rechner/src/stack.c deleted file mode 100644 index 7cf6e09..0000000 --- a/P06_Integer_Rechner/integer-rechner/src/stack.c +++ /dev/null @@ -1,132 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Stack - */ - -#include -#include -#include "stack.h" -#include "error.h" - -/** - * @brief The details of the otherwise opaque type. - */ -struct stack { - stack_value_t *stack; ///< the data container (the instance owns this) - stack_value_t *top; ///< the pointer into the container where the current top data resides (has to point initially to stack-1) - stack_value_t *full; ///< the pointer to the last entry of the conatainer -}; - -stack_t *stack_new(size_t max_elements) -{ - stack_t *instance = NULL; - // 1. allocate a stack_t instance on the heap and set the instance variable to it - // 2. call error_fatal_errno("stack_new: instance"); if failed to allocate the memory on the heap - // 3. allocate an array of max_elements value_t's on the heap and store its address in the stack member of the stack_t instance - // 4. call error_fatal_errno("stack_new: stack"); if failed to allocate the memory on the heap - // 5. set the top member of the stack_t instance to the address of the "element" before the first stack array element - // 6. set the full member of the stack_t instance to the address of the last element of the stack array - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - - - - - - - // END-STUDENTS-TO-ADD-CODE - return instance; -} - -void stack_destroy(stack_t *instance) -{ - assert(instance); - // 1. free the stack array - // 2. free the stack_t instance - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - // END-STUDENTS-TO-ADD-CODE -} - -void stack_push(stack_t *instance, stack_value_t value) -{ - assert(instance); - if (stack_is_full(instance)) error_fatal("stack_push: full"); - // 1. increment by one element the address stored in the top member of the stack_t instance - // 2. store the received value at the new top location - // BEGIN-STUDENTS-TO-ADD-CODE - - - - // END-STUDENTS-TO-ADD-CODE -} - -stack_value_t stack_pop(stack_t *instance) -{ - assert(instance); - if (stack_is_empty(instance)) error_fatal("stack_pop: empty"); - stack_value_t value = 0; - // 1. set the variable value to the value from the address the top member points to - // 2. decrement by one element the address stored in the top member of the stack_t instance - // BEGIN-STUDENTS-TO-ADD-CODE - - - - // END-STUDENTS-TO-ADD-CODE - return value; -} - -stack_value_t stack_top(stack_t *instance) -{ - assert(instance); - if (stack_is_empty(instance)) error_fatal("stack_top: empty"); - stack_value_t value = 0; - // 1. set the variable value to the value from the address the top member points to - // BEGIN-STUDENTS-TO-ADD-CODE - - - - // END-STUDENTS-TO-ADD-CODE - return value; -} - -int stack_is_empty(stack_t *instance) -{ - assert(instance); - int is_empty = 1; - // 1. set is_empty to 1 if the top equals the initial condition as done in stack_new(), otherwise to 0 - // BEGIN-STUDENTS-TO-ADD-CODE - - - - // END-STUDENTS-TO-ADD-CODE - return is_empty; -} - -int stack_is_full(stack_t *instance) -{ - assert(instance); - int is_full = 1; - // 1. set is_full to 1 if the top equals the full pointer as set in the stack_new() function, otherwise 0 - // BEGIN-STUDENTS-TO-ADD-CODE - - - - // END-STUDENTS-TO-ADD-CODE - return is_full; -} diff --git a/P06_Integer_Rechner/integer-rechner/src/stack.h b/P06_Integer_Rechner/integer-rechner/src/stack.h deleted file mode 100644 index f13d075..0000000 --- a/P06_Integer_Rechner/integer-rechner/src/stack.h +++ /dev/null @@ -1,86 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Stack - */ - -#ifndef _STACK_H_ -#define _STACK_H_ - -/** - * @brief Convenience alias. - */ -typedef unsigned int stack_value_t; - -/** - * @brief Opaque type - detailed in the implementation only. - */ -typedef struct stack stack_t; - -/** - * @brief Construcor. - * @param max_elements [IN] Gives the size of the stack. - * @return The newly created and initialized instance - * @remark In case of error, the program terminates with EXIT_FAILURE and an appropriate error message. - */ -stack_t *stack_new(size_t max_elements); - -/** - * @brief Frees the instances memory and the data it owns (i.e. the stack container). - * @param instance [INOUT] The affected instance. - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - */ -void stack_destroy(stack_t *instance); - -/** - * @brief Pushes the value to the stack. - * @param instance [INOUT] The affected instance. - * @param value [IN] The value to pus. - * @remark In case of error, the program terminates with EXIT_FAILURE and an appropriate error message. - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - */ -void stack_push(stack_t *instance, stack_value_t value); - -/** - * @brief Removes the top stack entry and returns it. - * @param instance [INOUT] The affected instance. - * @return Returns the top most entry. - * @remark In case of error, the program terminates with EXIT_FAILURE and an appropriate error message. - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - */ -stack_value_t stack_pop(stack_t *instance); - -/** - * @brief Gets the top stack entry. - * @param instance [INOUT] The affected instance. - * @return Returns the top most entry. - * @remark In case of error, the program terminates with EXIT_FAILURE and an appropriate error message. - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - */ -stack_value_t stack_top(stack_t *instance); - -/** - * @brief Queries if the stack is empty - i.e. if a pop operation is possible. - * @param instance [INOUT] The affected instance. - * @return Returns 1 if empry, 0 otherwise. - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - */ -int stack_is_empty(stack_t *instance); - -/** - * @brief Queries if the stack is full - i.e. if a push operation is possible. - * @param instance [INOUT] The affected instance. - * @return Returns 1 if full, 0 otherwise. - * @remark assert(instance) is performed, i.e. it is considered a programming error if instance is NULL. - */ -int stack_is_full(stack_t *instance); - -#endif // _STACK_H_ diff --git a/P06_Integer_Rechner/integer-rechner/tests/tests.c b/P06_Integer_Rechner/integer-rechner/tests/tests.c deleted file mode 100644 index 8883979..0000000 --- a/P06_Integer_Rechner/integer-rechner/tests/tests.c +++ /dev/null @@ -1,263 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Test suite for the given package. - */ -#include -#include -#include -#include -#include -#include -#include "test_utils.h" -#include "stack.h" - -#ifndef TARGET // must be given by the make file --> see test target -#error missing TARGET define -#endif - -/// @brief alias for EXIT_SUCCESS -#define OK EXIT_SUCCESS -/// @brief alias for EXIT_FAILURE -#define FAIL EXIT_FAILURE - -/// @brief The name of the STDOUT text file. -#define OUTFILE "stdout.txt" -/// @brief The name of the STDERR text file. -#define ERRFILE "stderr.txt" - -#define TRACE_INDENT "\n " ///< allow for better stdout formatting in case of error - -// setup & cleanup -static int setup(void) -{ - remove_file_if_exists(OUTFILE); - remove_file_if_exists(ERRFILE); - return 0; // success -} - -static int teardown(void) -{ - // Do nothing. - // Especially: do not remove result files - they are removed in int setup(void) *before* running a test. - return 0; // success -} - -// tests -static void test_stack_new(void) -{ - // arrange - const size_t size = 2; - // act - stack_t *instance = stack_new(size); - // assert - CU_ASSERT_PTR_NOT_NULL_FATAL(instance); - CU_ASSERT_EQUAL(stack_is_empty(instance), 1); - // cleanup - stack_destroy(instance); -} - -static void test_stack_push(void) -{ - // arrange - const size_t size = 2; - stack_t *instance = stack_new(size); - CU_ASSERT_PTR_NOT_NULL_FATAL(instance); - // act & assert - for(size_t i = 0; i < size; i++) { - stack_push(instance, i); - CU_ASSERT_EQUAL(stack_top(instance), i); - } - // cleanup - stack_destroy(instance); -} - -static void test_stack_top(void) -{ - // arrange - const size_t size = 2; - stack_t *instance = stack_new(size); - CU_ASSERT_PTR_NOT_NULL_FATAL(instance); - // act & assert - CU_ASSERT_EQUAL(stack_is_empty(instance), 1); - for(size_t i = 0; i < size; i++) { - CU_ASSERT_EQUAL(stack_is_full(instance), 0); - stack_push(instance, i); - CU_ASSERT_EQUAL(stack_is_empty(instance), 0); - CU_ASSERT_EQUAL(stack_top(instance), i); - } - CU_ASSERT_EQUAL(stack_is_full(instance), 1); - // cleanup - stack_destroy(instance); -} - -static void test_stack_pop(void) -{ - // arrange - const size_t size = 2; - stack_t *instance = stack_new(size); - CU_ASSERT_PTR_NOT_NULL_FATAL(instance); - for(size_t i = 0; i < size; i++) { - stack_push(instance, i); - } - // act & assert - for(size_t i = size; i > 0; i--) { - CU_ASSERT_EQUAL(stack_is_empty(instance), 0); - CU_ASSERT_EQUAL(stack_pop(instance), i-1); - CU_ASSERT_EQUAL(stack_is_full(instance), 0); - } - // cleanup - stack_destroy(instance); -} - -static void test_stack_is_empty(void) -{ - // arrange - const size_t size = 2; - stack_t *instance = stack_new(size); - CU_ASSERT_PTR_NOT_NULL_FATAL(instance); - // act & assert - CU_ASSERT_EQUAL(stack_is_empty(instance), 1); - for(size_t i = 0; i < size; i++) { - stack_push(instance, i); - CU_ASSERT_EQUAL(stack_is_empty(instance), 0); - } - for(size_t i = size; i > 0; i--) { - CU_ASSERT_EQUAL(stack_is_empty(instance), 0); - stack_pop(instance); - } - CU_ASSERT_EQUAL(stack_is_empty(instance), 1); - // cleanup - stack_destroy(instance); -} - -static void test_stack_is_full(void) -{ - // arrange - const size_t size = 2; - stack_t *instance = stack_new(size); - CU_ASSERT_PTR_NOT_NULL_FATAL(instance); - // act & assert - CU_ASSERT_EQUAL(stack_is_full(instance), 0); - for(size_t i = 0; i < size; i++) { - CU_ASSERT_EQUAL(stack_is_full(instance), 0); - stack_push(instance, i); - } - CU_ASSERT_EQUAL(stack_is_full(instance), 1); - for(size_t i = size; i > 0; i--) { - stack_pop(instance); - CU_ASSERT_EQUAL(stack_is_full(instance), 0); - } - CU_ASSERT_EQUAL(stack_is_full(instance), 0); - // cleanup - stack_destroy(instance); -} - -static void test_silent(void) -{ - const char *out_txt[] = { - "--- RESULT ---\n", - "dec=3\n", - "hex=0x00000003\n", - "oct=000000000003\n", - "bin=0b00000000000000000000000000000011\n", - }; - const char *err_txt[] = { - NULL, - }; - - // arrange & act & assert - CU_ASSERT_EQUAL_FATAL(WEXITSTATUS(system("echo '1 + 2' | " XSTR(TARGET) " 0 >" OUTFILE " 2>" ERRFILE)), OK); - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); - assert_lines(ERRFILE, err_txt, sizeof(err_txt)/sizeof(*err_txt)); - // arrange & act & assert - CU_ASSERT_EQUAL_FATAL(WEXITSTATUS(system("echo '1 + 2' | " XSTR(TARGET) " >" OUTFILE " 2>" ERRFILE)), OK); - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); - assert_lines(ERRFILE, err_txt, sizeof(err_txt)/sizeof(*err_txt)); -} - -static void test_verbose(void) -{ - const char *out_txt[] = { - "--- RESULT ---\n", - "dec=3\n", - "hex=0x00000003\n", - "oct=000000000003\n", - "bin=0b00000000000000000000000000000011\n", - }; - const char *err_txt[] = { - "hex=0x00000001\n", - "hex=0x00000002\n", - "OP_ADD\n", - }; - - - // arrange & act & assert - CU_ASSERT_EQUAL_FATAL(WEXITSTATUS(system("echo '1 + 2' | " XSTR(TARGET) " 1 >" OUTFILE " 2>" ERRFILE)), OK); - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); - assert_lines(ERRFILE, err_txt, sizeof(err_txt)/sizeof(*err_txt)); -} - -static void test_all(void) -{ - const char *out_txt[] = { - "--- RESULT ---\n", - "dec=8\n", - "hex=0x00000008\n", - "oct=000000000010\n", - "bin=0b00000000000000000000000000001000\n", - }; - const char *err_txt[] = { - NULL, - }; - - - // arrange & act & assert - CU_ASSERT_EQUAL_FATAL(WEXITSTATUS(system("echo '(8 | 7 ^ 5) & 4 << 3 + 2 * (~1+(1>>0))' | " XSTR(TARGET) " >" OUTFILE " 2>" ERRFILE)), OK); - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); - assert_lines(ERRFILE, err_txt, sizeof(err_txt)/sizeof(*err_txt)); -} - -static void test_error(void) -{ - const char *out_txt[] = { - NULL, - }; - const char *err_txt[] = { - "ERROR: at or near position 5: number or '(' ... ')' expected\n", - }; - - // arrange & act & assert - CU_ASSERT_EQUAL_FATAL(WEXITSTATUS(system("echo '1 + ' | " XSTR(TARGET) " >" OUTFILE " 2>" ERRFILE)), FAIL); - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); - assert_lines(ERRFILE, err_txt, sizeof(err_txt)/sizeof(*err_txt)); -} - -/** - * @brief Registers and runs the tests. - * @returns success (0) or one of the CU_ErrorCode (>0) - */ -int main(void) -{ - // setup, run, teardown - TestMainBasic("lab test", setup, teardown - , test_stack_new - , test_stack_push - , test_stack_top - , test_stack_pop - , test_stack_is_empty - , test_stack_is_full - , test_silent - , test_verbose - , test_all - , test_error - ); -} diff --git a/P07_Personen_Verwaltung_Linked_List/P07_Personen_Verwaltung_Linked_List.pdf b/P07_Personen_Verwaltung_Linked_List/P07_Personen_Verwaltung_Linked_List.pdf deleted file mode 100644 index 9cd3c97..0000000 Binary files a/P07_Personen_Verwaltung_Linked_List/P07_Personen_Verwaltung_Linked_List.pdf and /dev/null differ diff --git a/P07_Personen_Verwaltung_Linked_List/personen-verwaltung/Makefile b/P07_Personen_Verwaltung_Linked_List/personen-verwaltung/Makefile deleted file mode 100644 index 44f82b3..0000000 --- a/P07_Personen_Verwaltung_Linked_List/personen-verwaltung/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -SNP_SHARED_MAKEFILE := $(if $(SNP_SHARED_MAKEFILE),$(SNP_SHARED_MAKEFILE),"~/snp/shared.mk") - -TARGET := bin/personen-verwaltung -# BEGIN-STUDENTS-TO-ADD-CODE -MODULES := -# END-STUDENTS-TO-ADD-CODE -SOURCES := src/main.c $(MODULES) -TSTSOURCES := tests/tests.c $(MODULES) - - -include $(SNP_SHARED_MAKEFILE) - -# CFLAGS += -Werror diff --git a/P07_Personen_Verwaltung_Linked_List/personen-verwaltung/mainpage.dox b/P07_Personen_Verwaltung_Linked_List/personen-verwaltung/mainpage.dox deleted file mode 100644 index 05d1c72..0000000 --- a/P07_Personen_Verwaltung_Linked_List/personen-verwaltung/mainpage.dox +++ /dev/null @@ -1,8 +0,0 @@ -/** - * @mainpage SNP - P07 Linked List - * - * @section Purpose - * - * This is a lab on usage of arrays. - * - */ diff --git a/P07_Personen_Verwaltung_Linked_List/personen-verwaltung/src/main.c b/P07_Personen_Verwaltung_Linked_List/personen-verwaltung/src/main.c deleted file mode 100644 index 3dd772a..0000000 --- a/P07_Personen_Verwaltung_Linked_List/personen-verwaltung/src/main.c +++ /dev/null @@ -1,29 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Lab implementation - */ -#include -#include - -/** - * @brief Main entry point. - * @param[in] argc The size of the argv array. - * @param[in] argv The command line arguments... - * @returns Returns EXIT_SUCCESS (=0) on success, EXIT_FAILURE (=1) there is an expression syntax error. - */ -int main(int argc, char* argv[]) -{ - // BEGIN-STUDENTS-TO-ADD-CODE - - // END-STUDENTS-TO-ADD-CODE - return EXIT_SUCCESS; -} diff --git a/P07_Personen_Verwaltung_Linked_List/personen-verwaltung/tests/tests.c b/P07_Personen_Verwaltung_Linked_List/personen-verwaltung/tests/tests.c deleted file mode 100644 index 408182d..0000000 --- a/P07_Personen_Verwaltung_Linked_List/personen-verwaltung/tests/tests.c +++ /dev/null @@ -1,119 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Test suite for the given package. - */ -#include -#include -#include -#include -#include -#include -#include "test_utils.h" - -#ifndef TARGET // must be given by the make file --> see test target -#error missing TARGET define -#endif - -/// @brief alias for EXIT_SUCCESS -#define OK EXIT_SUCCESS -/// @brief alias for EXIT_FAILURE -#define FAIL EXIT_FAILURE - -/// @brief The name of the STDOUT text file. -#define OUTFILE "stdout.txt" -/// @brief The name of the STDERR text file. -#define ERRFILE "stderr.txt" - -#define TRACE_INDENT "\n " ///< allow for better stdout formatting in case of error - -// setup & cleanup -static int setup(void) -{ - remove_file_if_exists(OUTFILE); - remove_file_if_exists(ERRFILE); - return 0; // success -} - -static int teardown(void) -{ - // Do nothing. - // Especially: do not remove result files - they are removed in int setup(void) *before* running a test. - return 0; // success -} - -// tests -static void test_person_compare(void) -{ - // BEGIN-STUDENTS-TO-ADD-CODE - // arrange - - // act - CU_FAIL("missing test"); - - // assert - - // END-STUDENTS-TO-ADD-CODE -} - -static void test_list_insert(void) -{ - // BEGIN-STUDENTS-TO-ADD-CODE - // arrange - - // act - CU_FAIL("missing test"); - - // assert - - // END-STUDENTS-TO-ADD-CODE -} - -static void test_list_remove(void) -{ - // BEGIN-STUDENTS-TO-ADD-CODE - // arrange - - // act - CU_FAIL("missing test"); - - // assert - - // END-STUDENTS-TO-ADD-CODE -} - -static void test_list_clear(void) -{ - // BEGIN-STUDENTS-TO-ADD-CODE - // arrange - - // act - CU_FAIL("missing test"); - - // assert - - // END-STUDENTS-TO-ADD-CODE -} - -/** - * @brief Registers and runs the tests. - * @returns success (0) or one of the CU_ErrorCode (>0) - */ -int main(void) -{ - // setup, run, teardown - TestMainBasic("lab test", setup, teardown - , test_person_compare - , test_list_insert - , test_list_remove - , test_list_clear - ); -} diff --git a/P08_Auflisten_aller_PATH_Executables/P08_Auflisten_aller_PATH_Executables.pdf b/P08_Auflisten_aller_PATH_Executables/P08_Auflisten_aller_PATH_Executables.pdf deleted file mode 100644 index 3b43442..0000000 Binary files a/P08_Auflisten_aller_PATH_Executables/P08_Auflisten_aller_PATH_Executables.pdf and /dev/null differ diff --git a/P08_Auflisten_aller_PATH_Executables/c_get_exec_list/Makefile b/P08_Auflisten_aller_PATH_Executables/c_get_exec_list/Makefile deleted file mode 100644 index 916f2b4..0000000 --- a/P08_Auflisten_aller_PATH_Executables/c_get_exec_list/Makefile +++ /dev/null @@ -1,11 +0,0 @@ -SNP_SHARED_MAKEFILE := $(if $(SNP_SHARED_MAKEFILE),$(SNP_SHARED_MAKEFILE),"~/snp/shared.mk") - -TARGET := bin/c-get-exec-list -MODULES := -SOURCES := src/main.c $(MODULES) -TSTSOURCES := tests/tests.c $(MODULES) - - -include $(SNP_SHARED_MAKEFILE) - -# CFLAGS += -Werror diff --git a/P08_Auflisten_aller_PATH_Executables/c_get_exec_list/mainpage.dox b/P08_Auflisten_aller_PATH_Executables/c_get_exec_list/mainpage.dox deleted file mode 100644 index f7e791a..0000000 --- a/P08_Auflisten_aller_PATH_Executables/c_get_exec_list/mainpage.dox +++ /dev/null @@ -1,8 +0,0 @@ -/** - * @mainpage SNP - P08: C implementation of P01-Bash get_exec_list_arg.sh - * - * @section Purpose - * - * This is a lab on POSIX file and directory attribute access. - * - */ diff --git a/P08_Auflisten_aller_PATH_Executables/c_get_exec_list/src/main.c b/P08_Auflisten_aller_PATH_Executables/c_get_exec_list/src/main.c deleted file mode 100644 index 6ff1d9b..0000000 --- a/P08_Auflisten_aller_PATH_Executables/c_get_exec_list/src/main.c +++ /dev/null @@ -1,123 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Lab implementation - */ - -#include // getenv, malloc, free, EXIT_SUCCESS, NULL -#include // strchr -#include // printf, sprintf -#include // struct stat, S_ISDIR, S_ISREG -#include // stat, access -#include // opendir, closedir, readdir, DIR, struct dirent - -/** - * @brief Creates a copy of the string on the heap. - * @param s [IN] The original string. - * @return Returns the allocated copy or NULL if s is NULL or if malloc failed. - * @remark Suggestion: Use the functions malloc(), strcpy(). - */ -static char *malloc_copy(const char *s) -{ - char *p = NULL; - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - - // END-STUDENTS-TO-ADD-CODE - return p; -} - -/** - * @brief Replaces inplace in the buffer all occurances of sep by '\0'. - * @param buffer [INOUT] The string which gets searched and modified. - * @param sep [IN] The separator for which all instances in buffer get replaced by '\0'. - * @return Returns the number of fields which are separated by sep, or 0 if buffer is NULL. - * @remark Empty fields are allowed and to be supported, i.e. given by ":..." or by "...::..." or by "...:". - * @remark Suggestion: Use the function strchr() to search for the separator. - */ -static size_t split_buffer_inplace(char buffer[], char sep) -{ - size_t n = 0; - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - - - - - - - // END-STUDENTS-TO-ADD-CODE - return n; -} - -/** - * @brief If path is an executable directory, list all executable files located directly in that directory - each such file as i:path:file. - * @param i [IN] The number of the path to report. - * @param path [IN] The directory path to search for executables. - * @remark Suggestion: Use the function is_accessible_dir(), is_executable_file(), opendir(), readdir(), closedir(). - */ -static void list_executables(size_t i, const char *path) -{ - // bash: - // [ -n "$p" ] || p="." - // if [ -d "$p" ] && [ -x "$p" ] - // then - // find -L "$p" -maxdepth 1 -type f -executable -printf "$i:%h:%f\n" - // fi - const char *p = path && strlen(path) ? path : "."; // replace an empty path by "." as current directory to allow for appending /name - // BEGIN-STUDENTS-TO-ADD-CODE - - - - - - - - - - // END-STUDENTS-TO-ADD-CODE -} - -// # from lab P01_Bash -// paths="$1" -// [ -n "$paths" ] || paths="$PATH" -// -// # input-field-separator: tells the shell to split in the 'for' loop the $var by ":" -// IFS=":" -// -// for p in $paths -// do -// i=$((i+1)) -// [ -n "$p" ] || p="." -// if [ -d "$p" ] && [ -x "$p" ] -// then -// find -L "$p" -maxdepth 1 -type f -executable -printf "$i:%h:%f\n" 2>/dev/null -// fi -// done - -int main(int argc, const char *argv[]) -{ - char *paths = malloc_copy(argc > 1 && argv[1][0] ? argv[1] : getenv("PATH")); - size_t n = split_buffer_inplace(paths, ':'); // replaces all ':' by '\0' and returns the number of the resulting fields - char *p = paths; - for(size_t i = 1; i <= n; i++) { // 1...n - list_executables(i, p); - p += strlen(p) + 1; // readover the field and the trailing '\0' to get to the next field - } - free(paths); - return EXIT_SUCCESS; -} diff --git a/P08_Auflisten_aller_PATH_Executables/c_get_exec_list/tests/tests.c b/P08_Auflisten_aller_PATH_Executables/c_get_exec_list/tests/tests.c deleted file mode 100644 index 23b8812..0000000 --- a/P08_Auflisten_aller_PATH_Executables/c_get_exec_list/tests/tests.c +++ /dev/null @@ -1,89 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Test suite for the given package. - */ -#include -#include -#include -#include -#include -#include -#include "test_utils.h" - -#ifndef TARGET // must be given by the make file --> see test target -#error missing TARGET define -#endif - -/// @brief alias for EXIT_SUCCESS -#define OK EXIT_SUCCESS -/// @brief alias for EXIT_FAILURE -#define FAIL EXIT_FAILURE - -/// @brief The name of the STDOUT text file. -#define OUTFILE "stdout.txt" -/// @brief The name of the STDERR text file. -#define ERRFILE "stderr.txt" - -#define TRACE_INDENT "\n " ///< allow for better stdout formatting in case of error - -#define PATH_SIZE 256 - -// setup & cleanup -static int setup(void) -{ - remove_file_if_exists(OUTFILE); - remove_file_if_exists(ERRFILE); - return 0; // success -} - -static int teardown(void) -{ - // Do nothing. - // Especially: do not remove result files - they are removed in int setup(void) *before* running a test. - return 0; // success -} - -// tests -static void test_paths(void) -{ - // arrange - const char *name = strrchr(XSTR(TARGET), '/'); - name = name ? name+1 : XSTR(TARGET); - - char buf[PATH_SIZE] = { 0 }; - snprintf(buf, PATH_SIZE, "4:../bin:%s\n", name); - - const char *out_txt[] = - { "1:.:runtest\n", - "2::runtest\n", - buf - }; - const char *err_txt[] = { NULL }; - // act - int exit_code = system(XSTR(TARGET) " .::..:../bin 1>" OUTFILE " 2>" ERRFILE); - // assert - CU_ASSERT_EQUAL(WEXITSTATUS(exit_code), EXIT_SUCCESS); - assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt)); - assert_lines(ERRFILE, err_txt, sizeof(err_txt)/sizeof(*err_txt)); -} - -/** - * @brief Registers and runs the tests. - * @returns success (0) or one of the CU_ErrorCode (>0) - */ -int main(void) -{ - // setup, run, teardown - TestMainBasic("lab test", setup, teardown - , test_paths - ); -} diff --git a/P09_Prozesse_und_Threads/Daemon_Prozesse/Daemonizer.c b/P09_Prozesse_und_Threads/Daemon_Prozesse/Daemonizer.c deleted file mode 100644 index 5209c01..0000000 --- a/P09_Prozesse_und_Threads/Daemon_Prozesse/Daemonizer.c +++ /dev/null @@ -1,178 +0,0 @@ -/****************************************************************************** -* File: Daemonizer.c -* Original Autor: M. Thaler (Modul BSY) -* Aufgabe: Einen Daemon-Prozess erzeugen -******************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -//***************************************************************************** - -// Macro to write out all the PIDs... - -#define OutPutPIDs() /*printf("\nPID %d, PPID %d, GRP-ID %d\n", \ - getpid(), getppid(), getpgrp())*/ - - -//***************************************************************************** -// Function: Locks file with file descriptor fd -// Returns: 0 on success, -1 of file is already locked -// Exits: on fatal errors -//***************************************************************************** - -int lock(int fd) { - int retval, len; - struct flock lock; // data structure for file lock - char buffer[16]; - - // prepare lockfile - - lock.l_type = F_WRLCK; - lock.l_start = 0; - lock.l_whence = SEEK_SET; - lock.l_len = 0; - - retval = fcntl(fd, F_SETLK, &lock); // set file lock - if (retval < 0) { - if ((errno == EACCES) || (errno == EAGAIN)) { - return(-1); // Daemon already runs - } - else { - perror("fatal error when locking file"); - exit(-1); - } - } - - // empty the lockfile - - retval = ftruncate(fd, 0); - if (retval < 0) { - perror("fatal error when emptying lockfile"); - exit(-1); - } - - // write process ID to lockfile - - sprintf(buffer, "%d\n", getpid()); - len = strlen(buffer); - retval = write(fd, buffer, len) < len; - if (retval < 0) { - perror("fatal error when writing pid to lockfile"); - exit(-1); - } - - // set lockfile to close on exec - - retval = fcntl(fd, F_GETFD, 0); - if (retval < 0) { - perror("fatal error when reading lockfile flags"); - exit(-1); - } - retval = retval | FD_CLOEXEC; - retval = fcntl(fd, F_SETFD, retval); - if (retval < 0) { - perror("fatal error when setting lockfile flags"); - exit(-1); - } - return(0); -} - -//***************************************************************************** -// Function: Makes a deamon process and runs a daemon function -// Parameter: Daemon function -// data pointer to data to be passed to Daemonfunction -// LogFile, path of logfile, if NULL, no logfile is created -// LivDir, path, where daemon will live -// Returns: should not return -// Exits: if daemon is already runnung or on fatal errors -//***************************************************************************** - -int Daemonizer(void Daemon(void *), void *data, - const char *LockFile, const char *LogFile, const char *LivDir) { - - pid_t PID; - int fd, dummyfd, retval; - - // create a prozess and terminate parents -> parent is init - - OutPutPIDs(); - - PID = fork(); - if (PID < 0) { - perror("could not fork()"); - exit(-1); - } - else if (PID > 0) { - exit(0); // I have done my work an can exit - } - - // ---------------------------------------------------------------------- - // now I am a process detached from the parent - // an make the process -> a daemon process - - signal(SIGINT, SIG_IGN); // ignore CTRL-C - signal(SIGQUIT, SIG_IGN); // ignore quit - signal(SIGHUP, SIG_IGN); // ignore hangup of terminal - - OutPutPIDs(); - - setsid(); // make process session leader - // and processgroup leader - // no control terminal with pocess - OutPutPIDs(); - - chdir(LivDir); // change to secure directory - umask(0); // allow all access rights for files - - // set up lockfile, if required - - if (LockFile != NULL) { - fd = open(LockFile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | - S_IRGRP | S_IROTH); - if (fd < 0) { - perror("fatal error when opening lockfile"); - exit(-1); - } - retval = lock(fd); - if (retval < 0) { - printf("\n*** daemon is already running ***\n"); - exit(0); - } - } - - // last message from daemon - - printf("\n*** daemon starts with process id: %d ***\n",getpid()); - - // close "communication" to outer world and set up logging, if required - - close(1); // close stdout - close(2); // close stderr - if (LogFile != NULL) { // open log file on stdout - enum {UserWrite=0644}; - dummyfd = open(LogFile, O_CREAT | O_APPEND | O_WRONLY,UserWrite); - if (dummyfd < 0) { - perror("could not open log file"); - exit(-1); - } - dup(1); // connect stderr to logfile - } - close(0); // now close stdin - - // now start the daemon funktion - Daemon(data); - - // should not come here - - return(0); -} - -//***************************************************************************** diff --git a/P09_Prozesse_und_Threads/Daemon_Prozesse/Daemonizer.h b/P09_Prozesse_und_Threads/Daemon_Prozesse/Daemonizer.h deleted file mode 100644 index 3f03fab..0000000 --- a/P09_Prozesse_und_Threads/Daemon_Prozesse/Daemonizer.h +++ /dev/null @@ -1,15 +0,0 @@ -/********************************************************************* -* File: Daemonizer.h -* Original Autor: M. Thaler (Modul BSY) -* Aufgabe: einen Daemon-Prozess erzeugen -*********************************************************************/ - -#ifndef DAEMONIZER_H -#define DAEMONIZER_H - -int Daemonizer(void Daemon(void *), void *data, - const char *LockFile, const char *LogFile, const char *LivDir); - -#endif - -//------------------------------------------------------------------------ diff --git a/P09_Prozesse_und_Threads/Daemon_Prozesse/IPsockCom.c b/P09_Prozesse_und_Threads/Daemon_Prozesse/IPsockCom.c deleted file mode 100644 index af35c9f..0000000 --- a/P09_Prozesse_und_Threads/Daemon_Prozesse/IPsockCom.c +++ /dev/null @@ -1,188 +0,0 @@ -//***************************************************************************** -// ipCom.c IP Socket Functions -// Original Author: M. Thaler, M. Pellaton (Modul BSY) -//***************************************************************************** - -//***************************************************************************** -// system includes -//***************************************************************************** - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include - -//***************************************************************************** -// local definitions -//***************************************************************************** - -#include "IPsockCom.h" - -//***************************************************************************** -// Function: send data buffer to host "host" with port "port" -// Parameter: hostname or IP address in dot format -// port number -// buffer -// size of buffer -// Returns: number of characters read on success, -1 if connection failed -// buffer: time data -// -//***************************************************************************** - -int getTimeFromServer(char *host, int port, char *buffer, int bufferLen) { - - int sfd, sysRet, timeOut, retval; - char stringPort[8]; - struct addrinfo hints, *aiList, *aiPtr = NULL; - - if (strcmp(host, "") == 0) { - printf("Need hostname or IP address\n"); - return(-1); - } - - sprintf(stringPort, "%d", port); - - memset(&hints, '\0', sizeof(hints)); - //hints.ai_flags = AI_CANONNAME; - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - - sysRet = getaddrinfo(host, stringPort, &hints, &aiList); - if (sysRet != 0) { - printf("error getting network address for %s (%s)\n", - host, gai_strerror(sysRet)); - exit(-1); - } - - aiPtr = aiList; // search through list - while (aiPtr != 0) { - sfd = socket(aiPtr->ai_family, aiPtr->ai_socktype, aiPtr->ai_protocol); - if (sfd >= 0) { - timeOut = 100; - sysRet = 1; - while ((timeOut != 0) && (sysRet != 0)) { - sysRet = connect(sfd, aiPtr->ai_addr, aiPtr->ai_addrlen); - usleep(1000); - timeOut--; - } - if (sysRet == 0) - break; // connect successful - else - close(sfd); - } - aiPtr = aiPtr->ai_next; - } - freeaddrinfo(aiList); - if (aiPtr == NULL) { - printf("could not connect to %s\n", host); - retval = -1; - } - else - retval = 0; - - if (retval == 0) { - if (write(sfd, buffer, bufferLen) < 0) { - printf("error sending request to timer serveer\n"); - retval = -1; - } - else - retval = read(sfd, buffer, COM_BUF_SIZE); - } - close(sfd); - return(retval); -} - -//***************************************************************************** -// Function: starts "time" socket server -// Parameter: port number to listen to -// Returns: socket file descriptor if ok, -1 if error -// Exits: -//***************************************************************************** - -int StartTimeServer(int portNumber) { - - int sfd, sysRet, j; - char stringPort[8]; - struct addrinfo hints, *aiList, *aiPtr = NULL; - - sprintf(stringPort, "%d", portNumber); // portnumber to string - memset(&hints, '\0', sizeof(hints)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = 0; - hints.ai_flags = AI_PASSIVE; - - sysRet = getaddrinfo(NULL, stringPort, &hints, &aiList); - if (sysRet != 0) { - printf("error getting network address (%s)\n", gai_strerror(sysRet)); - exit(-1); - } - - aiPtr = aiList; // search through list - while (aiPtr != 0) { - sfd = socket(aiPtr->ai_family, aiPtr->ai_socktype, aiPtr->ai_protocol); - if (sfd >= 0) { - j = 1; - sysRet = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &j, sizeof(j)); - if (sysRet < 0) - perror("cannot set socket options"); - - if (bind(sfd, aiPtr->ai_addr, aiPtr->ai_addrlen) < 0) { - perror("bind failed "); - close(sfd); - exit(-1); - } - if (listen(sfd, 5) < 0) { - close(sfd); - perror("listen failed "); - exit(-1); - } - else - break; - } - aiPtr = aiPtr->ai_next; - } - freeaddrinfo(aiList); - if (aiPtr == NULL) { - printf("could not set up a socket server\n"); - exit(-1); - } - return(sfd); -} - -//***************************************************************************** -// Function: Reads data from client -// Parameter: socket file descriptor -// buffer to place data -// Returns: current socket descriptor on success, < 0 on failure -// Exits: none -//***************************************************************************** - - -int WaitForClient(int sfd, char *buffer) { - - int cfd, retval, addrlen; - struct sockaddr_in addr; - - addrlen = sizeof(struct sockaddr_in); - cfd = accept(sfd,(struct sockaddr *)&addr,(socklen_t *)&addrlen); - if (cfd >= 0) { - retval = read(cfd, buffer, COM_BUF_SIZE); - retval = cfd; - } - else - retval = cfd; - return(retval); -} - -//***************************************************************************** diff --git a/P09_Prozesse_und_Threads/Daemon_Prozesse/IPsockCom.h b/P09_Prozesse_und_Threads/Daemon_Prozesse/IPsockCom.h deleted file mode 100644 index 1fb8c83..0000000 --- a/P09_Prozesse_und_Threads/Daemon_Prozesse/IPsockCom.h +++ /dev/null @@ -1,20 +0,0 @@ -//***************************************************************************** -// ipCom.c IP Socket Functions -// Original Autor: M. Thaler, M. Pellaton (Modul BSY) -//***************************************************************************** - -#ifndef IP_COM_SOCKETS -#define IP_COM_SOCKETS - -#define COM_BUF_SIZE 512 - -#define PIDperror()\ - fprintf(stderr,"fatal error, daemon with PID %d: ",getpid()); - -int getTimeFromServer(char *host, int port, char *buffer, int bufferLen); -int StartTimeServer(int PortNumber); -int WaitForClient(int sfd, char *buffer); - -#endif - -//*************************************************************************** diff --git a/P09_Prozesse_und_Threads/Daemon_Prozesse/MrTimeDaemon.c b/P09_Prozesse_und_Threads/Daemon_Prozesse/MrTimeDaemon.c deleted file mode 100644 index f84926f..0000000 --- a/P09_Prozesse_und_Threads/Daemon_Prozesse/MrTimeDaemon.c +++ /dev/null @@ -1,44 +0,0 @@ -/********************************************************************* -* File: MrTimeDaemon.c -* Original Autor: M. Thaler (Modul BSY) -* Aufgabe: einen Daemon-Prozess erzeugen -*********************************************************************/ - -#include -#include -#include -#include -#include -#include - -#include "Daemonizer.h" -#include "TimeDaemon.h" - -#define STRING_LENGTH 128 - -//-------------------------------------------------------------------- - -int main(void) { - pid_t pid; - int status; - const char *lockfilePath = "/tmp/timeDaemon.lock"; - //const char *lockfilePath = NULL; - const char *logfilePath = "/tmp/timeDaemon.log"; - const char *livingPath = "/tmp"; - const char *myName = "I am Mr. Time Daemon on \n"; - - - if ((pid = fork()) == 0) - Daemonizer(TimeDaemon, (void *)myName, - lockfilePath, logfilePath, livingPath); - else { - assert(pid > 0); - wait(&status); // wait for Daemonizer to exit - // after having forked the "Daemon" - if (WEXITSTATUS(status) != 0) - printf("*** Daemonizer failed ***\n"); - } - -} - -//-------------------------------------------------------------------- diff --git a/P09_Prozesse_und_Threads/Daemon_Prozesse/PlapperMaul.c b/P09_Prozesse_und_Threads/Daemon_Prozesse/PlapperMaul.c deleted file mode 100644 index 2a2db97..0000000 --- a/P09_Prozesse_und_Threads/Daemon_Prozesse/PlapperMaul.c +++ /dev/null @@ -1,21 +0,0 @@ -/********************************************************************* -* File: PlapperMaul.c -* Original Autor: M. Thaler (Modul BSY) -* Aufgabe: plappern -*********************************************************************/ - -#include -#include -#include - -//-------------------------------------------------------------------- - -int main(void) { - - while (1) { - printf("Hallo, ich bins.... Pidi %d\n", getpid()); - usleep(500000); - } -} - -//-------------------------------------------------------------------- diff --git a/P09_Prozesse_und_Threads/Daemon_Prozesse/TimeDaemon.c b/P09_Prozesse_und_Threads/Daemon_Prozesse/TimeDaemon.c deleted file mode 100644 index fcbc568..0000000 --- a/P09_Prozesse_und_Threads/Daemon_Prozesse/TimeDaemon.c +++ /dev/null @@ -1,75 +0,0 @@ -/****************************************************************************** -* File: TimeDaemon.c -* Original Autor: M. Thaler (Modul BSY) -* Aufgabe: the daemon code -******************************************************************************/ - -//***************************************************************************** -// system includes -//***************************************************************************** - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -//***************************************************************************** -// local includes -//***************************************************************************** - -#include "TimeDaemon.h" -#include "TimeDaemonDefs.h" -#include "IPsockCom.h" - -//***************************************************************************** -// Function: TimeDeamon -// Parameter: data: expects here pointer to string -//***************************************************************************** - -void TimeDaemon(void *data) { - - TimeData tData; - char buffer[COM_BUF_SIZE]; - struct tm MyTime; - time_t ActualTime; - int sfd, cfd; - - - printf("%s\n", (char *)data); - - // start server - sfd = StartTimeServer(TIME_PORT); - if (sfd < 0) { - perror("could not start socket server"); - exit(-1); - } - while (1) { - - cfd = WaitForClient(sfd, buffer); - if ((strcmp(buffer, REQUEST_STRING) == 0) && (cfd >= 0)) { - - time(&ActualTime); - MyTime = *localtime(&ActualTime); - - tData.hours = MyTime.tm_hour; - tData.minutes = MyTime.tm_min; - tData.seconds = MyTime.tm_sec; - tData.day = MyTime.tm_mday; - tData.month = MyTime.tm_mon + 1; - tData.year = MyTime.tm_year + 1900; - gethostname(tData.servername, HOST_NAM_LEN); - write(cfd, (char *)(&tData), sizeof(tData)); - } - } - // if we should somehow come here (how ?) - close(sfd); - exit(0); -} - -//***************************************************************************** diff --git a/P09_Prozesse_und_Threads/Daemon_Prozesse/TimeDaemon.h b/P09_Prozesse_und_Threads/Daemon_Prozesse/TimeDaemon.h deleted file mode 100644 index ddc898a..0000000 --- a/P09_Prozesse_und_Threads/Daemon_Prozesse/TimeDaemon.h +++ /dev/null @@ -1,14 +0,0 @@ -/****************************************************************************** -* File: TimeDaemon.h -* Original Autor: M. Thaler (Modul BSY) -* Aufgabe: function prototype of time daemon -******************************************************************************/ - -#ifndef TIME_DAEMON -#define TIME_DAEMON - -void TimeDaemon(void *); - -#endif - -//***************************************************************************** diff --git a/P09_Prozesse_und_Threads/Daemon_Prozesse/TimeDaemonDefs.h b/P09_Prozesse_und_Threads/Daemon_Prozesse/TimeDaemonDefs.h deleted file mode 100644 index d29f19d..0000000 --- a/P09_Prozesse_und_Threads/Daemon_Prozesse/TimeDaemonDefs.h +++ /dev/null @@ -1,30 +0,0 @@ -/****************************************************************************** -* File: TimeDaemonDefs.h -* Original Autor: M. Thaler (Modul BSY) -* Aufgabe: Data Definitions for TimeDaemon -******************************************************************************/ - -#ifndef TIME_DAEMON_DEFS -#define TIME_DAEMON_DEFS - -#define HOST_NAM_LEN 32 - -#define TIME_PORT 65534 - -#define REQUEST_STRING "requestTimeFromServer" - -// data structure receiving from time daemon - -typedef struct { - int hours; - int minutes; - int seconds; - int day; - int month; - int year; - char servername[HOST_NAM_LEN]; -} TimeData, *TimeDataPtr; - -#endif - -//***************************************************************************** diff --git a/P09_Prozesse_und_Threads/Daemon_Prozesse/WhatsTheTimeMr.c b/P09_Prozesse_und_Threads/Daemon_Prozesse/WhatsTheTimeMr.c deleted file mode 100644 index 5180382..0000000 --- a/P09_Prozesse_und_Threads/Daemon_Prozesse/WhatsTheTimeMr.c +++ /dev/null @@ -1,66 +0,0 @@ -/****************************************************************************** -* File: WhatsTheTimeMr.c -* Original Autor: M. Thaler (Modul BSY) -* Aufgabe: Ask MrTimeDaemon for the time -******************************************************************************/ - -//***************************************************************************** -// system includes -//***************************************************************************** - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include // required by inet_aton - -//***************************************************************************** -// local includes -//***************************************************************************** - -#include "TimeDaemon.h" -#include "TimeDaemonDefs.h" -#include "IPsockCom.h" - -//***************************************************************************** -// Function: main() -// Parameter: hostname or IP address in dot format -//***************************************************************************** - -int main(int argc, char *argv[]) { - - char buffer[COM_BUF_SIZE]; - char hostname[64]; - TimeDataPtr tDataPtr; - int j; - - if (argc < 2) { - printf("*** no hostname or IP-address -> using localhost ***\n"); - strcpy(hostname, "localhost"); - } else { - strcpy(hostname, argv[1]); - } - - strcpy(buffer,REQUEST_STRING); - j = getTimeFromServer(hostname, TIME_PORT, buffer, sizeof(buffer)); - if (j < 0) - printf("no response from %s\n", argv[1]); - else { - tDataPtr = (TimeDataPtr)(buffer); - printf("\nIt's "); - printf("%02d:%02d:%02d", - tDataPtr->hours, tDataPtr->minutes,tDataPtr->seconds); - printf(" the %d.%d.%d on \"%s\"\n\n", - tDataPtr->day, tDataPtr->month, - tDataPtr->year, tDataPtr->servername); - } - exit(0); -} - -//***************************************************************************** - diff --git a/P09_Prozesse_und_Threads/Daemon_Prozesse/makefile b/P09_Prozesse_und_Threads/Daemon_Prozesse/makefile deleted file mode 100644 index 32af073..0000000 --- a/P09_Prozesse_und_Threads/Daemon_Prozesse/makefile +++ /dev/null @@ -1,35 +0,0 @@ -# ************************************************************* -# Original Autor: M. Thaler (Modul BSY) -# ************************************************************* - -CMP= gcc -CMPFLAGS= -Wall -g -std=gnu99 -EXENAMES= MrTimeDaemon.e -EXENAMEC= WhatsTheTimeMr.e -EXENAMEP= PlapperMaul.e -LIBNAME= - -compile: $(EXENAMES) $(EXENAMEC) $(EXENAMEP) - -$(EXENAMES): MrTimeDaemon.o IPsockCom.o Daemonizer.o TimeDaemon.o - $(CMP) $(CMPFLAGS) MrTimeDaemon.o IPsockCom.o Daemonizer.o TimeDaemon.o $(LIBNAME) -o $(EXENAMES) - -$(EXENAMEC): WhatsTheTimeMr.o IPsockCom.o - $(CMP) $(CMPFLAGS) WhatsTheTimeMr.o IPsockCom.o $(LIBNAME) -o $(EXENAMEC) - -$(EXENAMEP): PlapperMaul.o - $(CMP) $(CMPFLAGS) PlapperMaul.o $(LIBNAME) -o $(EXENAMEP) - -.c.o: - $(CMP) -c $(CMPFLAGS) $< - -.cc.o: - $(CMP) -c $(CMPFLAGS) $< - -clean: - rm -f $(EXENAMEC) $(EXENAMES) $(EXENAMEP) - rm -f *.o - -all: - @make clean - @make diff --git a/P09_Prozesse_und_Threads/P09_1_Prozesse_und_Threads.pdf b/P09_Prozesse_und_Threads/P09_1_Prozesse_und_Threads.pdf deleted file mode 100644 index 5d3c386..0000000 Binary files a/P09_Prozesse_und_Threads/P09_1_Prozesse_und_Threads.pdf and /dev/null differ diff --git a/P09_Prozesse_und_Threads/P09_2_Dämon_Prozesse.pdf b/P09_Prozesse_und_Threads/P09_2_Dämon_Prozesse.pdf deleted file mode 100644 index 78acc4d..0000000 Binary files a/P09_Prozesse_und_Threads/P09_2_Dämon_Prozesse.pdf and /dev/null differ diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe01/ProcA1.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe01/ProcA1.c deleted file mode 100644 index 9b37481..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe01/ProcA1.c +++ /dev/null @@ -1,53 +0,0 @@ -//*************************************************************************** -// File: ProcA1.c -// Original Author: M. Thaler (Modul BSY) -//*************************************************************************** - -//*************************************************************************** -// system includes -//*************************************************************************** - -#include -#include -#include -#include -#include -#include - -//*************************************************************************** -// Function: main(), parameter: none -//*************************************************************************** - -int main(void) { - - pid_t pid; - int status; - int i; - - i = 5; - - printf("\n\ni vor fork: %d\n\n", i); - - pid = fork(); - switch (pid) { - case -1: - perror("Could not fork"); - break; - case 0: - i++; - printf("\n... ich bin das Kind %d mit i=%d, ", getpid(),i); - printf("meine Eltern sind %d \n", getppid()); - break; - default: - i--; - printf("\n... wir sind die Eltern %d mit i=%d ", getpid(), i); - printf("und Kind %d,\n unsere Eltern sind %d\n", pid, getppid()); - wait(&status); - break; - } - printf("\n. . . . . und wer bin ich ?\n\n"); - exit(0); -} - -//*************************************************************************** - diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe01/makefile b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe01/makefile deleted file mode 100644 index fa951bf..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe01/makefile +++ /dev/null @@ -1,26 +0,0 @@ -# ************************************************************* -# Original Autor: M. Thaler (Modul BSY) -# ************************************************************* - -CMP= gcc -CMPFLAGS= -Wall -LDFLAGS= -EXENAM1= ProcA1.e -FNAM1= ProcA1 -LIBNAME= - -$(EXENAM1): $(FNAM1).o - $(CMP) $(FNAM1).o $(LIBNAME) $(LDFLAGS) -o $@ - -.c.o: - $(CMP) -c $(CMPFLAGS) $< - -.cc.o: - $(CMP) -c $(CMPFLAGS) $< - -clean: - rm -f *.o $(EXENAM1) - -all: - @make clean - @make diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe02/ChildProcA2.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe02/ChildProcA2.c deleted file mode 100644 index 470bcde..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe02/ChildProcA2.c +++ /dev/null @@ -1,38 +0,0 @@ -//*************************************************************************** -// File: ChildProcA3.c -// Original Author: M. Thaler (Modul BSY) -//*************************************************************************** - -//*************************************************************************** -// system includes -//*************************************************************************** - -#include -#include -#include -#include -#include -#include - -//*************************************************************************** -// Function: main(), parameter: arg[0]: Programmname, arg[1]: i -//*************************************************************************** - -int main(int argc, char *argv[]) { - - int i; - - if (argv[1] == NULL) { - printf("argument missing\n"); - exit(-1); - } - else - i = atoi(argv[1]); // convert string argv[1] to integer i - // argv[1] is a number passed to child - - printf("\n... ich bin das Kind %d mit i=%d, ", getpid(),i); - printf("meine Eltern sind %d \n", getppid()); - exit(0); -} - -//*************************************************************************** diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe02/ProcA2.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe02/ProcA2.c deleted file mode 100644 index 1120825..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe02/ProcA2.c +++ /dev/null @@ -1,54 +0,0 @@ -//*************************************************************************** -// File: ProcA3.c -// Original Author: M. Thaler (Modul BSY) -//*************************************************************************** - -//*************************************************************************** -// system includes -//*************************************************************************** - -#include -#include -#include -#include -#include -#include - -//*************************************************************************** -// Function: main(), parameter: none -//*************************************************************************** - -int main(void) { - - pid_t pid; - int status; - int i, retval; - char str[8]; - - i = 5; - - printf("\n\ni vor fork: %d\n\n", i); - - pid = fork(); - switch (pid) { - case -1: - perror("Could not fork"); - break; - case 0: - i++; - sprintf(str, "%d",i); // convert integer i to string str - retval = execl("./ChildProcA2.e", "ChildProcA2.e", str, NULL); - if (retval < 0) perror("\nexecl not successful"); - break; - default: - i--; - printf("\n... wir sind die Eltern %d mit i=%d ", getpid(), i); - printf("und Kind %d,\n unsere Eltern sind %d\n", pid, getppid()); - wait(&status); - break; - } - printf("\n. . . . . und wer bin ich ?\n\n"); - exit(0); -} - -//*************************************************************************** diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe02/makefile b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe02/makefile deleted file mode 100644 index 4d23435..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe02/makefile +++ /dev/null @@ -1,34 +0,0 @@ -# ************************************************************* -# Original Autor: M. Thaler (Modul BSY) -# ************************************************************* - -CMP= gcc -CMPFLAGS= -Wall -LDFLAGS= -EXENAME= ProcA2.e -FNAME= ProcA2 -EXENAMC= ChildProcA2.e -FNAMC= ChildProcA2 -LIBNAME= -LIBNAME= - -compile: $(EXENAME) $(EXENAMC) - -$(EXENAME): $(FNAME).o - $(CMP) $(FNAME).o $(LIBNAME) $(LDFLAGS) -o $@ - -$(EXENAMC): $(FNAMC).o - $(CMP) $(FNAMC).o $(LIBNAME) $(LDFLAGS) -o $@ - -.c.o: - $(CMP) -c $(CMPFLAGS) $< - -.cc.o: - $(CMP) -c $(CMPFLAGS) $< - -clean: - rm -f *.o $(EXENAME) $(EXENAMC) - -all: - @make clean - @make diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe03/ProcA3.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe03/ProcA3.c deleted file mode 100644 index 3c29605..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe03/ProcA3.c +++ /dev/null @@ -1,31 +0,0 @@ -//*************************************************************************** -// File: ProcA4.c -// Original Author: M. Thaler (Modul BSY) -//*************************************************************************** - -//*************************************************************************** -// system includes -//*************************************************************************** - -#include -#include -#include -#include -#include -#include - -//*************************************************************************** -// Function: main(), parameter: none -//*************************************************************************** - -int main(void) { - fork(); - fork(); - fork(); - fork(); - printf("PID: %d\t PPID: %d\n", getpid(), getppid()); - sleep(10); // keep processes in system to display their "stammbaum" - exit(0); -} - -//*************************************************************************** diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe03/makefile b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe03/makefile deleted file mode 100644 index af2c854..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe03/makefile +++ /dev/null @@ -1,27 +0,0 @@ -# ************************************************************* -# Original Autor: M. Thaler (Modul BSY) -# ************************************************************* - -CMP= gcc -CMPFLAGS= -Wall -LDFLAGS= -EXENAMES= ProcA3.e -FNAME= ProcA3 -LIBNAME= - -$(EXENAMES): $(FNAME).o - $(CMP) $(FNAME).o $(LIBNAME) $(LDFLAGS) -o $@ - -.c.o: - $(CMP) -c $(CMPFLAGS) $< - -.cc.o: - $(CMP) -c $(CMPFLAGS) $< - -clean: - rm -f *.o $(EXENAMES) - -all: - @make clean - @make - diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/ProcA4.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/ProcA4.c deleted file mode 100644 index 0554662..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/ProcA4.c +++ /dev/null @@ -1,61 +0,0 @@ -//*************************************************************************** -// File: ProcA5.c -// Original Author: M. Thaler (Modul BSY) -//*************************************************************************** - -//*************************************************************************** -// system includes -//*************************************************************************** - -#include -#include -#include -#include -#include -#include -#include - -#include "workerUtils.h" -#include "selectCPU.h" - -#define ITERATIONS 20 -#define WORK_HARD 2000000 - -//*************************************************************************** -// Function: main(), parameter: none -//*************************************************************************** - -int main(void) { - - pid_t pid; - int i; - - - pid = fork(); - selectCPU(0); // select CPU 0 - switch (pid) { - case -1: - perror("Could not fork"); - break; - case 0: - for (i = 0; i < ITERATIONS; i++) { - justWork(WORK_HARD); - printf("%d \t\tChild\n", i); - fflush(stdout); // force output - } - break; - default: - for (i = 0; i < ITERATIONS; i++) {; - justWork(WORK_HARD); - printf("%d \tMother\n", i); - fflush(stdout); // force output - } - } - printf("I go it ...\n"); - - if (pid > 0) // wait for child to terminate - waitpid(pid, NULL, 0); - - exit(0); -} -//*************************************************************************** diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/README_MAC b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/README_MAC deleted file mode 100644 index 84bd483..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/README_MAC +++ /dev/null @@ -1,2 +0,0 @@ - -setCPU() does not work on OSX diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/makefile b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/makefile deleted file mode 100644 index 8bc8dbd..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/makefile +++ /dev/null @@ -1,27 +0,0 @@ -# ************************************************************* -# Original Autor: M. Thaler (Modul BSY) -# ************************************************************* - -CMP= gcc -CMPFLAGS= -Wall -LDFLAGS= -EXENAM1= ProcA4.e -FNAM1= ProcA4.o workerUtils.o selectCPU.o -LIBNAME= - -$(EXENAM1): $(FNAM1) - $(CMP) $(FNAM1) $(LIBNAME) $(LDFLAGS) -o $@ - -.c.o: - $(CMP) -c $(CMPFLAGS) $< - -.cc.o: - $(CMP) -c $(CMPFLAGS) $< - -clean: - rm -f *.o $(EXENAM1) - -all: - @make clean - @make - diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/selectCPU.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/selectCPU.c deleted file mode 100644 index 8f5ddee..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/selectCPU.c +++ /dev/null @@ -1,41 +0,0 @@ -//*************************************************************************** -// File: setCPU.c -// Original Author: M. Thaler (Modul BSY) -//*************************************************************************** - -#define _GNU_SOURCE - -#include -#include -#include -#include - -#ifdef __linux -#include -#endif - -//****************************************************************************** - -#ifdef __linux - -void selectCPU(unsigned int n) { - cpu_set_t cpuset; - if (n >= sysconf(_SC_NPROCESSORS_ONLN)) { - printf("CPU %d not availble\n", n); - exit(0); - } - sched_getaffinity(0, sizeof(cpu_set_t), &cpuset); - CPU_ZERO(&cpuset); - CPU_SET(n, &cpuset); - sched_setaffinity(0, sizeof(cpu_set_t), &cpuset); -} -#endif - -#ifdef __APPLE__ -void selectCPU(unsigned int n) { - printf("Cannot set single CPU on OSX\n ... continue anyway"); -} -#endif - -//****************************************************************************** - diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/selectCPU.h b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/selectCPU.h deleted file mode 100644 index 87067b9..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/selectCPU.h +++ /dev/null @@ -1,15 +0,0 @@ -//******************************************************************* -// File: selectCPU.h -// Original Author: M. Thaler (Modul BSY) -//******************************************************************* - -#ifndef SET_CPUS_HEADER_FILE -#define SET_CPUS_HEADER_FILE - -//******************************************************************* - -void selectCPU(unsigned int n); - -//******************************************************************* - -#endif diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/workerUtils.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/workerUtils.c deleted file mode 100644 index 85368a0..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/workerUtils.c +++ /dev/null @@ -1,90 +0,0 @@ -//******************************************************************* -// File: workerUtils.c -// Original Author: M. Thaler (Modul BSY) -// Purpose: helper applications to consume cpu time -//******************************************************************* - -#include -#include -#include -#include -#include -#include -#include - -#include "workerUtils.h" - -//------------------------------------------------------------------------------ -#define MAX_CPUS 16 -//------------------------------------------------------------------------------ -pid_t worker[MAX_CPUS]; -int nCPUS; -//------------------------------------------------------------------------------ - -void launchWorkLoad() { - int i; - nCPUS = sysconf(_SC_NPROCESSORS_ONLN); // get number of available CPUs - nCPUS = (nCPUS > MAX_CPUS) ? MAX_CPUS : nCPUS; - for (i = 0; i < nCPUS; i++) // start workers -> random load - worker[i] = startWorker(); -} - -void stopWorkLoad() { - int i; - for (i = 0; i < nCPUS; i++) // stop worker - stopWorker(worker[i]); -} - - -void setRandom(void) { - srandom((unsigned int)time(NULL)); // new random sequence -} - -void justWork(unsigned int load) { - unsigned int j; - for (j = 0; j < load; j++) {}; // just work -} - -void workHard(unsigned int low, unsigned int high) { - double rv; - unsigned int us, j; - high = high - low; - rv = ((double)random())/RAND_MAX; // make random value (0..1) - us = low + (unsigned int)(rv * high); // between lower & higher limit - for (j = 0; j < us; j++) {}; // just work - setRandom(); // reseed random generator -} - -void randomSleep(unsigned int low, unsigned int high) { - double rv; - unsigned int us; - high = high - low; - rv = ((double)random())/RAND_MAX; // make random value (0..1) - us = low + (unsigned int)(rv * high); // between lower & higher limit - usleep(us*1000); -} - - -pid_t startWorker(void) { // make a hard working process - struct timeval tv; // limit run time to 60 secs - time_t st; - pid_t pid = fork(); - if (pid == 0) { // child: pid = 0 -> ifinite loop - gettimeofday(&tv, NULL); // take start time - st = tv.tv_sec; - while ((tv.tv_sec-st) < 60) { - workHard(50, 800); // work hard (random interval - gettimeofday(&tv, NULL); // between 50us and 800us) - } - - } - if (pid < 0) { // exit fork failed - printf("forking worker failed\n"); - exit(0); - } - return pid; // return pid if parent -} - -void stopWorker(pid_t worker) { - kill(worker, SIGKILL); // terminate worker process -} diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/workerUtils.h b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/workerUtils.h deleted file mode 100644 index ac5226f..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe04/workerUtils.h +++ /dev/null @@ -1,33 +0,0 @@ -//******************************************************************* -// File: workerUtils.h -// Original Author: M. Thaler (Modul BSY) -// Purpose: helper applications to consume cpu time -//******************************************************************* - -#include -#include -#include -#include -#include -#include -#include - -//------------------------------------------------------------------------------ -#define MAX_CPUS 16 -//------------------------------------------------------------------------------ - -void launchWorkLoad(); - -void stopWorkLoad(); - -void setRandom(void); - -void justWork(unsigned int load); - -void workHard(unsigned int low, unsigned int high); - -void randomSleep(unsigned int low, unsigned int high); - -pid_t startWorker(void); - -void stopWorker(pid_t worker); diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe05/ProcA5.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe05/ProcA5.c deleted file mode 100644 index a73c979..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe05/ProcA5.c +++ /dev/null @@ -1,51 +0,0 @@ -//*************************************************************************** -// File: ProcA6.c -// Original Author: M. Thaler (Modul BSY) -//*************************************************************************** - -//*************************************************************************** -// system includes -//*************************************************************************** - -#include -#include -#include -#include -#include -#include - -//*************************************************************************** -// Function: main(), parameter: none -//*************************************************************************** - -int main(void) { - - pid_t pid, id; - char buf[64]; - int i; - - pid = fork(); - switch (pid) { - case -1: - perror("Could not fork"); - break; - case 0: - printf("\n... ich bin das Kind %d\n", getpid()); - for (i = 0; i < 10; i++) { - usleep(500000); // slow down a bit - printf("Mein Elternprozess ist %d\n", id = getppid()); - } - printf("... so das wars\n"); - break; - default: - sleep(2); // terminate - exit(0); - break; - } - printf("\n\n*** and here my new parent ****\n\n"); - sprintf(buf, "ps -p %d", id); - system(buf); - exit(0); -} - -//*************************************************************************** diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe05/README b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe05/README deleted file mode 100644 index 23988f6..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe05/README +++ /dev/null @@ -1,3 +0,0 @@ -Reparenting -> new parent instead init (1): - -command prctl(PR_SET_CHILD_SUBREAPER, 1) diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe05/makefile b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe05/makefile deleted file mode 100644 index 00e05fc..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe05/makefile +++ /dev/null @@ -1,26 +0,0 @@ -# ************************************************************* -# Original Autor: M. Thaler (Modul BSY) -# ************************************************************* - -CMP= gcc -CMPFLAGS= -Wall -LDFLAGS= -EXENAMES= ProcA5.e -FNAME= ProcA5 -LIBNAME= - -$(EXENAMES): $(FNAME).o - $(CMP) $(FNAME).o $(LIBNAME) $(LDFLAGS) -o $@ - -.c.o: - $(CMP) -c $(CMPFLAGS) $< - -.cc.o: - $(CMP) -c $(CMPFLAGS) $< - -clean: - rm -f *.o $(EXENAMES) - -all: - @make clean - @make diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe06/ProcA6.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe06/ProcA6.c deleted file mode 100644 index f5bb59f..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe06/ProcA6.c +++ /dev/null @@ -1,48 +0,0 @@ -//*************************************************************************** -// File: ProcA7.c -// Original Author: M. Thaler (Modul BSY) -//*************************************************************************** - -//*************************************************************************** -// system includes -//*************************************************************************** - -#include -#include -#include -#include -#include -#include - -//*************************************************************************** -// Function: main(), parameter: none -//*************************************************************************** - -int main(void) { - - pid_t pid; - int j; - - for (j = 0; j < 3; j++) { // generate 3 processes - pid = fork(); - switch (pid) { - case -1: - perror("Could not fork"); - break; - case 0: - sleep(j+2); // process j sleeps for j+2 sec - exit(0); // then exits - break; - default: // parent - break; - } - } - sleep(8); // parent process sleeps for 6 sec - wait(NULL); // consult manual for "wait" - sleep(2); - wait(NULL); - sleep(2); - wait(NULL); - sleep(2); - exit(0); -} diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe06/makefile b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe06/makefile deleted file mode 100644 index f802333..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe06/makefile +++ /dev/null @@ -1,27 +0,0 @@ -# ************************************************************* -# Original Autor: M. Thaler (Modul BSY) -# ************************************************************* - -CMP= gcc -CMPFLAGS= -Wall -LDFLAGS= -EXENAMES= aaaa.e -FNAME= ProcA6 -LIBNAME= - -$(EXENAMES): $(FNAME).o - $(CMP) $(FNAME).o $(LIBNAME) $(LDFLAGS) -o $@ - -.c.o: - $(CMP) -c $(CMPFLAGS) $< - -.cc.o: - $(CMP) -c $(CMPFLAGS) $< - -clean: - rm -f *.o $(EXENAMES) - -all: - @make clean - @make - diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe06/mtop b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe06/mtop deleted file mode 100755 index 04949bb..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe06/mtop +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash -# M. Thaler, InIT/ZHAW 11/2014 -# Version v.fs20 - -if test "$1" = "" -then - cmd="-a" -else - cmd="-C $1" -fi - -forever="1" -while test "$forever" = "1" -do - clear - ps $cmd - sleep 1 -done - diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe07/ChildProcA7.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe07/ChildProcA7.c deleted file mode 100644 index 635d098..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe07/ChildProcA7.c +++ /dev/null @@ -1,50 +0,0 @@ -//*************************************************************************** -// File: ChildProcA8.c -// Original Author: M. Thaler (Modul BSY) -//*************************************************************************** - -//*************************************************************************** -// system includes -//*************************************************************************** - -#include -#include -#include -#include -#include -#include -#include - -//*************************************************************************** -// Function: main(), parameter: arg[0]: Programmname, arg[1]: i -//*************************************************************************** - -int main(int argc, char *argv[]) { - - int i = 0, *a = NULL; - - if (argc > 1) - i = atoi(argv[1]); // convert string argv[1] to integer i - // argv[1] is a number passed to child - - printf("\n*** I am the child having job nr. %d ***\n\n", i); - - switch(i) { - case 0: exit(0); // exit normally - break; - case 1: *a = i; // force segmentation error - break; - case 2: kill(getpid(), 30); // I send signal 30 to myself - break; - case 3: sleep(5); // sleep and wait for signal - break; - case 4: sleep(5); // just sleep - exit(222); // then exit - break; - default: - exit(-1); - } - exit(0); -} - -//*************************************************************************** diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe07/ProcA7.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe07/ProcA7.c deleted file mode 100644 index 06b8cfd..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe07/ProcA7.c +++ /dev/null @@ -1,65 +0,0 @@ -//*************************************************************************** -// File: ProcA8.c -// Original Author: M. Thaler (Modul BSY) -//*************************************************************************** - -//*************************************************************************** -// system includes -//*************************************************************************** - -#include -#include -#include -#include -#include -#include - -//*************************************************************************** -// Function: main(), parameter: none -//*************************************************************************** - -int main(int argc, char *argv[]) { - pid_t pid; - int status, retval, whatToDo = 0; - char str[8]; - - if (argc > 1) - whatToDo = atoi(argv[1]); // get job number for child - - pid = fork(); // fork child - switch (pid) { - case -1: - perror("Could not fork"); - break; - case 0: - sprintf(str, "%d",whatToDo); - retval = execl("./ChildProcA7.e", "ChildProcA7.e", str, NULL); - if (retval < 0) perror("\nexecl not successful"); - break; - default: - if (whatToDo <= 3) { - if (whatToDo == 3) { - sleep(1); - kill(pid, SIGABRT); // send signal SIGABTR to child - } - wait(&status); - if (WIFEXITED(status)) - printf("Child exits with status %d\n", WEXITSTATUS(status)); - if (WIFSIGNALED(status)) { - printf("Child exits on signal %d\n", WTERMSIG(status)); - printf("Child exits with core dump %d\n", WCOREDUMP(status)); - } - } else { - usleep(500*1000); - while (!waitpid(pid, &status, WNOHANG)) { - printf(". . . child is playing\n"); - usleep(500*1000); - } - printf("Child has exited with 'exit(%d)'\n", WEXITSTATUS(status)); - } - break; - } - exit(0); -} - -//*************************************************************************** diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe07/README b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe07/README deleted file mode 100644 index 9f27733..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe07/README +++ /dev/null @@ -1,21 +0,0 @@ - -Enable core dumps -> ulimit -c unlimited -Disable core dumps -> ulimit -c 0 - -Das core File lesen mit gdb: - - gdb -c core ChildProgA8.e - - -!!!! Wichtig !!!! - -Wenn sie ein Linux benutzen, das standardmaessig ABRT (Automatic Bug -Reporting Tool) verwendet (z.B. Fedora, Centos, etc.), muss -/proc/sys/kernel/core_pattern auf "core" gesetzt werden, damit ein -core File erzeugt wird - - echo core > /proc/sys/kernel/core_pattern - -Wenn ihr System SE-Linux verwendet (z.B. Fedora) , laesst sich core_pattern -nicht auf einfache Art setzen, verzichten sie in diesem Fall auf die -Erzeugung des "core" Files. diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe07/makefile b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe07/makefile deleted file mode 100644 index 6d8bc93..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe07/makefile +++ /dev/null @@ -1,34 +0,0 @@ -# ************************************************************* -# Original Autor: M. Thaler (Modul BSY) -# ************************************************************* - -CMP= gcc -CMPFLAGS= -Wall -g -LDFLAGS= -EXENAME= ProcA7.e -FNAME= ProcA7 -EXENAMC= ChildProcA7.e -FNAMC= ChildProcA7 -LIBNAME= -LIBNAME= - -compile: $(EXENAME) $(EXENAMC) - -$(EXENAME): $(FNAME).o - $(CMP) $(FNAME).o $(LIBNAME) $(LDFLAGS) -o $@ - -$(EXENAMC): $(FNAMC).o - $(CMP) $(FNAMC).o $(LIBNAME) $(LDFLAGS) -o $@ - -.c.o: - $(CMP) -c $(CMPFLAGS) $< - -.cc.o: - $(CMP) -c $(CMPFLAGS) $< - -clean: - rm -f core *.o $(EXENAME) $(EXENAMC) - -all: - @make clean - @make diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/ProcA8_1.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/ProcA8_1.c deleted file mode 100644 index 5fbe822..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/ProcA8_1.c +++ /dev/null @@ -1,45 +0,0 @@ -//*************************************************************************** -// File: ProcA9_1.c -// Original Author: M. Thaler (Modul BSY) -//*************************************************************************** - -//*************************************************************************** -// system includes -//*************************************************************************** - -#include -#include -#include -#include -#include -#include - -//*************************************************************************** -// Function: main(), parameter: none -//*************************************************************************** - -int main(void) { - - pid_t pid; - - printf("\n"); - printf("\nHallo, I am on the way to fork now, ......lo"); - - pid = fork(); - switch (pid) { - case -1: - perror("Could not fork"); - break; - case 0: - printf("ok: I am the child\n"); - break; - default: - printf("ok: I am the parent\n"); - break; - } - printf("\nclear ?\n\n"); - exit(0); -} - -//*************************************************************************** - diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/ProcA8_2.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/ProcA8_2.c deleted file mode 100644 index a3bbdff..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/ProcA8_2.c +++ /dev/null @@ -1,87 +0,0 @@ -//*************************************************************************** -// File: ProcA9_2.c -// Original Author: M. Thaler (Modul BSY) -//*************************************************************************** - -//*************************************************************************** -// system includes -//*************************************************************************** - -#include -#include -#include -#include -#include -#include -#include - -#define WAIT_TIME (200*1000) // 0.2 s with usleep - -// globaler array - -#define ARRAY_SIZE 8 -char GArray[ARRAY_SIZE][ARRAY_SIZE]; - -//*************************************************************************** -// Function: main(), parameter: none -//*************************************************************************** - -int main(void) { - - pid_t pid; - int i,j; - - // flip coin to select "child first" or "parent first" - struct timeval tv; - gettimeofday(&tv, NULL); - srandom(tv.tv_usec); // evaluate seed - int head = (int)(random()) >> 7; // flip coin - head &= 0x1; - - // fill global array with '-' and print array value - for (i = 0; i < ARRAY_SIZE; i++) { - for (j = 0; j < ARRAY_SIZE; j++) { - GArray[i][j] = '-'; - printf("%c ", GArray[i][j]); - } - printf("\n"); - } - fflush(stdout); - - pid = fork(); - switch (pid) { - case -1: - perror("Could not fork"); - break; - case 0: // --- child fills upper half of array with 'c' - if (head) usleep(WAIT_TIME); - for (i = ARRAY_SIZE / 2; i < ARRAY_SIZE; i++) - for (j = 0; j < ARRAY_SIZE; j++) - GArray[i][j] = 'c'; - break; - default: // --- parent fills lower half of array with 'p' - if (! head) usleep(WAIT_TIME); - for (i = 0; i < ARRAY_SIZE / 2; i++) - for (j = 0; j < ARRAY_SIZE; j++) - GArray[i][j] = 'p'; - break; - } - - if (pid == 0) - printf("\nKinderarray\n\n"); - else - printf("\nElternarray\n\n"); - - for (i = 0; i < ARRAY_SIZE; i++) { - for (j = 0; j < ARRAY_SIZE; j++) - printf("%c ", GArray[i][j]); - printf("\n"); - } - fflush(stdout); - - if (pid > 0) wait(NULL); - - exit(0); -} - -//*************************************************************************** diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/ProcA8_3.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/ProcA8_3.c deleted file mode 100644 index b2786bc..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/ProcA8_3.c +++ /dev/null @@ -1,74 +0,0 @@ -//*************************************************************************** -// File: ProcA9_3.c -// Original Author: M. Thaler (Modul BSY) -//*************************************************************************** - -//*************************************************************************** -// system includes -//*************************************************************************** - -#include -#include -#include -#include -#include -#include -#include - - -#include "workerUtils.h" - - -#define ANZAHL 15 -#define WORK_HARD 1000000 - -//***************************************************************************** -// Function: main(), parameter: none -//***************************************************************************** - -int main(void) { - - FILE *fdes; - pid_t pid; - int i; - - launchWorkLoad(); // start CPU load to force context switches - fdes = fopen("AnyOutPut.txt", "w"); - if (fdes == NULL) perror("Cannot open file"); - - usleep(500000); - - pid = fork(); - - switch (pid) { - case -1: - perror("Could not fork"); - break; - case 0: - for (i = 1; i <= ANZAHL; i++) { - fprintf(fdes, "Fritzli\t%d\n", i); - fflush(fdes); // make sure date is written to file - justWork(WORK_HARD); - } - break; - default: - for (i = 1; i <= ANZAHL; i++) { - fprintf(fdes, "Mami\t%d\n", i); - fflush(fdes); // make sure date is written to file - justWork(WORK_HARD); - } - fflush(stdout); - stopWorkLoad(); - break; - } - printf("We are done\n"); - if (pid > 0) { - waitpid(pid, NULL, 0); - printf("See file AnyOutPut.txt\n"); - } - fflush(stdout); - exit(0); -} - -//***************************************************************************** - diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/makefile b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/makefile deleted file mode 100644 index e5921ff..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/makefile +++ /dev/null @@ -1,38 +0,0 @@ -# ************************************************************* -# Original Autor: M. Thaler (Modul BSY) -# ************************************************************* - -CMP= gcc -std=gnu99 -CMPFLAGS= -Wall -LDFLAGS= -EXENAME1= ProcA8_1.e -FNAM1= ProcA8_1.o -EXENAME2= ProcA8_2.e -FNAM2= ProcA8_2.o -EXENAME3= ProcA8_3.e -FNAM3= ProcA8_3.o workerUtils.o -LIBNAME= - -compile: $(EXENAME1) $(EXENAME2) $(EXENAME3) - -$(EXENAME1): $(FNAM1) - $(CMP) $(CMPFLAGS) $(FNAM1) $(LIBNAME) $(LDFLAGS) -o $@ - -$(EXENAME2): $(FNAM2) - $(CMP) $(CMPFLAGS) $(FNAM2) $(LIBNAME) $(LDFLAGS) -o $@ - -$(EXENAME3): $(FNAM3) - $(CMP) $(CMPFLAGS) $(FNAM3) $(LIBNAME) $(LDFLAGS) -o $@ - -.c.o: - $(CMP) -c $(CMPFLAGS) $< - -.cc.o: - $(CMP) -c $(CMPFLAGS) $< - -clean: - rm -f *.o $(EXENAME1) $(EXENAME2) $(EXENAME3) - -all: - @make clean - @make diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/workerUtils.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/workerUtils.c deleted file mode 100644 index 1fb8a5d..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/workerUtils.c +++ /dev/null @@ -1,90 +0,0 @@ -//******************************************************************* -// File workerUtils.c -// Original Author: M. Thaler (Modul BSY) -// Purpose: helper applications to consume cpu time -//******************************************************************* - -#include -#include -#include -#include -#include -#include -#include - -#include "workerUtils.h" - -//------------------------------------------------------------------------------ -#define MAX_CPUS 16 -//------------------------------------------------------------------------------ -pid_t worker[MAX_CPUS]; -int nCPUS; -//------------------------------------------------------------------------------ - -void launchWorkLoad() { - int i; - nCPUS = sysconf(_SC_NPROCESSORS_ONLN); // get number of available CPUs - nCPUS = (nCPUS > MAX_CPUS) ? MAX_CPUS : nCPUS; - for (i = 0; i < nCPUS; i++) // start workers -> random load - worker[i] = startWorker(); -} - -void stopWorkLoad() { - int i; - for (i = 0; i < nCPUS; i++) // stop worker - stopWorker(worker[i]); -} - - -void setRandom(void) { - srandom((unsigned int)time(NULL)); // new random sequence -} - -void justWork(unsigned int load) { - unsigned int j; - for (j = 0; j < load; j++) {}; // just work -} - -void workHard(unsigned int low, unsigned int high) { - double rv; - unsigned int us, j; - high = high - low; - rv = ((double)random())/RAND_MAX; // make random value (0..1) - us = low + (unsigned int)(rv * high); // between lower & higher limit - for (j = 0; j < us; j++) {}; // just work - setRandom(); // reseed random generator -} - -void randomSleep(unsigned int low, unsigned int high) { - double rv; - unsigned int us; - high = high - low; - rv = ((double)random())/RAND_MAX; // make random value (0..1) - us = low + (unsigned int)(rv * high); // between lower & higher limit - usleep(us*1000); -} - - -pid_t startWorker(void) { // make a hard working process - struct timeval tv; // limit run time to 60 secs - time_t st; - pid_t pid = fork(); - if (pid == 0) { // child: pid = 0 -> ifinite loop - gettimeofday(&tv, NULL); // take start time - st = tv.tv_sec; - while ((tv.tv_sec-st) < 60) { - workHard(50, 800); // work hard (random interval - gettimeofday(&tv, NULL); // between 50us and 800us) - } - - } - if (pid < 0) { // exit fork failed - printf("forking worker failed\n"); - exit(0); - } - return pid; // return pid if parent -} - -void stopWorker(pid_t worker) { - kill(worker, SIGKILL); // terminate worker process -} diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/workerUtils.h b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/workerUtils.h deleted file mode 100644 index c944cda..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe08/workerUtils.h +++ /dev/null @@ -1,25 +0,0 @@ -//******************************************************************* -// File: workerUtils.h -// Original Author: M. Thaler (Modul BSY) -//******************************************************************* - -#ifndef WORKER_UTILS -#define WORKER_UTILS - -void launchWorkLoad(); - -void stopWorkLoad(); - -void setRandom(void); - -void justWork(unsigned int load); - -void workHard(unsigned int low, unsigned int high); - -void randomSleep(unsigned int low, unsigned int high); - -pid_t startWorker(void); - -void stopWorker(pid_t worker); - -#endif diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe09/ProcA9.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe09/ProcA9.c deleted file mode 100644 index ded9dad..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe09/ProcA9.c +++ /dev/null @@ -1,112 +0,0 @@ -//*************************************************************************** -// File: ProcA9.c -// Original Author: M. Thaler (Modul BSY) -//*************************************************************************** - -//*************************************************************************** -// system includes -//*************************************************************************** - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "selectCPU.h" - -//************************************************************************** -// global data -#define ARRAY_SIZE 8 -char GArray[ARRAY_SIZE][ARRAY_SIZE]; - -//************************************************************************** - -void *ThreadF(void *letter) { - int i,j; - int LowLim, HighLim; - char letr; - - letr = *(char *)letter; - if (letr == 'p') { // paremeter = p: fill lower half of array - LowLim = 0; HighLim = ARRAY_SIZE / 2; - } - else { // paremeter != p: fill upper half of array - LowLim = ARRAY_SIZE / 2; HighLim = ARRAY_SIZE; - } - - for (i = LowLim; i < HighLim; i++) { // fill own half - for (j = 0; j < ARRAY_SIZE; j++) - GArray[i][j] = letr; - } - - for (i = 0; i < ARRAY_SIZE; i++) { // print whole array - for (j = 0; j < ARRAY_SIZE; j++) - printf("%c ", GArray[i][j]); - printf("\n"); - } - printf("\n"); - fflush(stdout); - pthread_exit(0); -} - -//*************************************************************************** -// Function: main(), parameter: none -//*************************************************************************** - -int main(void) { - - pthread_t thread1, thread2; - int i,j, pthr; - char letter1, letter2; - - selectCPU(0); // run on CPU 0 - - // flip coin to select p or c first - struct timeval tv; - gettimeofday(&tv, NULL); - srandom(tv.tv_usec); // evaluate seed - int head = (int)(random()) >> 7; // flip coin - head &= 0x1; - if (head) { - letter1 = 'p'; - letter2 = 'c'; - } - else { - letter1 = 'c'; - letter2 = 'p'; - } - - for (i = 0; i < ARRAY_SIZE; i++) - for (j = 0; j < ARRAY_SIZE; j++) - GArray[i][j] = '-'; - - printf("\nArray vor Threads\n\n"); - for (i = 0; i < ARRAY_SIZE; i++) { - for (j = 0; j < ARRAY_SIZE; j++) - printf("%c ", GArray[i][j]); - printf("\n"); - } - printf("\n"); - - pthr = pthread_create(&thread1, NULL, ThreadF, (void *)&letter1); - if (pthr != 0) perror("Could not create thread"); - pthr = pthread_create(&thread2, NULL, ThreadF, (void *)&letter2); - if (pthr != 0) perror("Could not create thread"); - - pthread_join(thread1, NULL); - pthread_join(thread2, NULL); - - printf("\n... nach Threads\n"); - for (i = 0; i < ARRAY_SIZE; i++) { - for (j = 0; j < ARRAY_SIZE; j++) - printf("%c ", GArray[i][j]); - printf("\n"); - } -} - -//*************************************************************************** - diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe09/makefile b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe09/makefile deleted file mode 100644 index f29cbb6..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe09/makefile +++ /dev/null @@ -1,27 +0,0 @@ -# ************************************************************* -# Original Autor: M. Thaler (Modul BSY) -# ************************************************************* - -CMP= gcc -std=gnu99 -pthread -CMPFLAGS= -Wall -LDFLAGS= -EXENAME1= ProcA9.e -FNAM1= ProcA9.o selectCPU.o -LIBNAME= - -$(EXENAME1): $(FNAM1) - $(CMP) $(FNAM1) $(LDFLAGS) $(LIBNAME) -o $@ - -.c.o: - $(CMP) -c $(CMPFLAGS) $< - -.cc.o: - $(CMP) -c $(CMPFLAGS) $< - -clean: - rm -f *.o $(EXENAME1) - -all: - @make clean - @make - diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe09/selectCPU.c b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe09/selectCPU.c deleted file mode 100644 index ad67886..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe09/selectCPU.c +++ /dev/null @@ -1,41 +0,0 @@ -//****************************************************************************** -// File: setCPU.c -// Original Author: M. Thaler (Modul BSY) -//****************************************************************************** - -#define _GNU_SOURCE - -#include -#include -#include -#include - -#ifdef __linux -#include -#endif - -//****************************************************************************** - -#ifdef __linux - -void selectCPU(unsigned int n) { - cpu_set_t cpuset; - if (n >= sysconf(_SC_NPROCESSORS_ONLN)) { - printf("CPU %d not availble\n", n); - exit(0); - } - sched_getaffinity(0, sizeof(cpu_set_t), &cpuset); - CPU_ZERO(&cpuset); - CPU_SET(n, &cpuset); - sched_setaffinity(0, sizeof(cpu_set_t), &cpuset); -} -#endif - -#ifdef __APPLE__ -void selectCPU(unsigned int n) { - printf("Cannot set single CPU on OSX\n ... continue anyway"); -} -#endif - -//****************************************************************************** - diff --git a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe09/selectCPU.h b/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe09/selectCPU.h deleted file mode 100644 index 1079489..0000000 --- a/P09_Prozesse_und_Threads/Prozesse_und_Threads/Aufgabe09/selectCPU.h +++ /dev/null @@ -1,15 +0,0 @@ -//****************************************************************************** -// File: setCPU.h -// Original Author: M. Thaler (Modul BSY) -//****************************************************************************** - -#ifndef SET_CPUS_HEADER_FILE -#define SET_CPUS_HEADER_FILE - -//****************************************************************************** - -void selectCPU(unsigned int n); - -//****************************************************************************** - -#endif diff --git a/P10_Sync/P10_Sync.pdf b/P10_Sync/P10_Sync.pdf deleted file mode 100644 index b5ec1fb..0000000 Binary files a/P10_Sync/P10_Sync.pdf and /dev/null differ diff --git a/P10_Sync/Sync/advancedSequence/coffeeTeller.c b/P10_Sync/Sync/advancedSequence/coffeeTeller.c deleted file mode 100644 index 4b96adb..0000000 --- a/P10_Sync/Sync/advancedSequence/coffeeTeller.c +++ /dev/null @@ -1,46 +0,0 @@ -/******************************************************************************* -* File: coffeTeller.c -* Purpose: simple sequence with semaphores -* Course: bsy -* Author: M. Thaler, 2011 -* Revision: 5/2012, 7/2013 -* Version: v.fs20 -*******************************************************************************/ - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "commonDefs.h" - -//****************************************************************************** - -int main(void) { - - int i; - sem_t *coin, *coffee, *ready; - - // set up a semaphore - coin = sem_open(COIN_SEMAPHOR, 0); - coffee = sem_open(COFFEE_SEMAPHOR, 0); - ready = sem_open(READY_SEMAPHOR, 0); - - // start teller machine - printf("\nCoffee teller machine starting\n\n"); - - i = 0; - while (i < ITERS) { - printf("teller (%d): waiting for coin\n", i); - printf(" (%d): got coin\n", i); - printf(" (%d): dispense coffee\n", i); - i++; - } -} - -//****************************************************************************** diff --git a/P10_Sync/Sync/advancedSequence/commonDefs.h b/P10_Sync/Sync/advancedSequence/commonDefs.h deleted file mode 100644 index 597278d..0000000 --- a/P10_Sync/Sync/advancedSequence/commonDefs.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef MY_DEFINITIONS_HEADER -#define MY_DEFINITIONS_HEADER - -/******************************************************************************* -* File: commonDefs.h -* Purpose: common definitions -* Course: bsy -* Author: M. Thaler, 2011 -* Revision: 5/2012, 7/2013, 4/2014 -* Version: v.fs20 -*******************************************************************************/ - -#define MYTURN_SEMAPHOR "/my_semaphor_1_name_advaneced_seq" -#define COIN_SEMAPHOR "/my_semaphor_2_name_advaneced_seq" -#define COFFEE_SEMAPHOR "/my_semaphor_3_name_advaneced_seq" -#define READY_SEMAPHOR "/my_semaphor_4_name_advaneced_seq" - -#define ITERS (100*1000*1000) -#define CUSTOMERS 4 -#define NUM_COIN 3 - -//****************************************************************************** - -#define checkSem(X) {if (X == SEM_FAILED) {perror("sem_open"); exit(-1);}} - -#define drinkingCoffee(X) {usleep((((1+X)*rand())+100000)&0xFFFFF);} - -//****************************************************************************** - -#endif - diff --git a/P10_Sync/Sync/advancedSequence/customer.c b/P10_Sync/Sync/advancedSequence/customer.c deleted file mode 100644 index a4609e5..0000000 --- a/P10_Sync/Sync/advancedSequence/customer.c +++ /dev/null @@ -1,50 +0,0 @@ -/******************************************************************************* -* File: customer.c -* Purpose: simple sequence with semaphores -* Course: bsy -* Author: M. Thaler, 2011 -* Revision: 5/2012, 7/2013 -* Version: v.fs20 -*******************************************************************************/ - -#include -#include -#include - -#include -#include -#include - -#include "commonDefs.h" - -//****************************************************************************** - -int main(int argc, char *argv[]) { - - int i, myID; - sem_t *myTurn, *coin, *coffee, *ready; - - if (argc > 1) - myID = atoi(argv[1]); - else - myID = 0; - - // set up a semaphore - myTurn = sem_open(MYTURN_SEMAPHOR, 0); - coin = sem_open(COIN_SEMAPHOR, 0); - coffee = sem_open(COFFEE_SEMAPHOR, 0); - ready = sem_open(READY_SEMAPHOR, 0); - - // start customer - printf("Customer starting (%d)\n", myID); - - // now check the sum - for (i = 0; i < ITERS; i++) { - printf("\t\t\t\tcustomer(%d) put coin %d\n", myID, i); - printf("\t\t\t\tcustomer(%d) waiting for coffee %d\n", myID, i); - printf("\t\t\t\tcustomer(%d) got coffee %d\n", myID, i); - drinkingCoffee(myID); - } -} - -//****************************************************************************** diff --git a/P10_Sync/Sync/advancedSequence/makefile b/P10_Sync/Sync/advancedSequence/makefile deleted file mode 100644 index 2891669..0000000 --- a/P10_Sync/Sync/advancedSequence/makefile +++ /dev/null @@ -1,40 +0,0 @@ -# BSy M. Thaler -# Version v.fs20 - -CMP= gcc -std=gnu99 -CMPFLAGS= -Wall -g -LIB= -pthread -EXENAME0= startApp.e -EXENAME1= coffeeTeller.e -EXENAME2= customer.e - -doit: - @make --no-print-directory clean - @make --no-print-directory startApp - @make --no-print-directory coffeeTeller - @make --no-print-directory customer - -startApp: startApp.o - $(CMP) $(CMPFLAGS) startApp.o $(LIB) -o $(EXENAME0) - -coffeeTeller: coffeeTeller.o - $(CMP) $(CMPFLAGS) coffeeTeller.o $(LIB) -o $(EXENAME1) - -customer: customer.o - $(CMP) $(CMPFLAGS) customer.o $(LIB) -o $(EXENAME2) - -.c.o: - $(CMP) -c $(CMPFLAGS) $< - -.cc.o: - $(CMP) -c $(CMPFLAGS) $< - -all: - @make clean - make doit - -clean: - @rm -f *.e *.o - -purge: - @make clean diff --git a/P10_Sync/Sync/advancedSequence/startApp.c b/P10_Sync/Sync/advancedSequence/startApp.c deleted file mode 100644 index a1f3a6b..0000000 --- a/P10_Sync/Sync/advancedSequence/startApp.c +++ /dev/null @@ -1,72 +0,0 @@ -/******************************************************************************* -* File: startApp.c -* Purpose: ice cream teller, basic sequence -* Course: bsy -* Author: M. Thaler, 2011 -* Revision: 5/2012, 7/2013 -* Version: v.fs20 -*******************************************************************************/ - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "commonDefs.h" - -//****************************************************************************** - -int main(void) { - - int j; - char string[8]; - sem_t *access, *coin, *coffee, *ready; - pid_t tellerPID; - - sem_unlink(MYTURN_SEMAPHOR); // delete seamphor if it still exists - sem_unlink(COIN_SEMAPHOR); // delete seamphor if it still exists - sem_unlink(COFFEE_SEMAPHOR); // delete seamphor if it still exists - sem_unlink(READY_SEMAPHOR); // delete seamphor if it still exists - - // set up a semaphore (? -> initial value of semaphor) - // checkSem() -> macro defined in commonDefs.h - - /* - access = sem_open(MYTURN_SEMAPHOR, O_CREAT, 0700, ?); checkSem(access); - coin = sem_open(COIN_SEMAPHOR, O_CREAT, 0700, ?); checkSem(coin); - coffee = sem_open(COFFEE_SEMAPHOR, O_CREAT, 0700, ?); checkSem(coffee); - ready = sem_open(READY_SEMAPHOR, O_CREAT, 0700, ?); checkSem(ready); - */ - - // now that the resources are set up, the supervisor can be started - for (j = 1; j <= CUSTOMERS; j++) { - if (fork() == 0) { - sprintf(string, "%d", j); - execl("./customer.e", "customer.e", string, NULL); - printf("*** could not start customer.e ***\n"); - } - } - - if ((tellerPID = fork()) == 0) { - execl("./coffeeTeller.e", "coffeeTeller.e", "0", NULL); - printf("*** could not start coffeTeller ***\n"); - } - - waitpid(tellerPID, NULL, 0); - system("killall coffeeTeller.e"); - system("killall customer.e"); // kill all customers - - // clean up resources - sem_unlink(MYTURN_SEMAPHOR); - sem_unlink(COIN_SEMAPHOR); - sem_unlink(COFFEE_SEMAPHOR); - sem_unlink(READY_SEMAPHOR); - printf("\n"); -} - -//****************************************************************************** diff --git a/P10_Sync/Sync/banking/a1/banking.c b/P10_Sync/Sync/banking/a1/banking.c deleted file mode 100644 index fbf20c1..0000000 --- a/P10_Sync/Sync/banking/a1/banking.c +++ /dev/null @@ -1,103 +0,0 @@ -//****************************************************************************** -// Course: BSy -// File: banking.c -// Author: M. Thaler, ZHAW -// Purpose: locking mechanisms -// Version: v.fs20 -//****************************************************************************** - -#include -#include -#include -#include - -#include "banking.h" - -//****************************************************************************** - -typedef struct account_struct_ { - long int balance; - pthread_mutex_t acntLock; -} Account; - -typedef struct branch_struct { - Account *accounts; - pthread_mutex_t branchLock; -} Branch; - -//****************************************************************************** - -static Branch *Bank; -static int nBranches, nAccounts; - -//****************************************************************************** -// banking functions - -void makeBank(int num_branches, int num_accounts) { - nBranches = num_branches; - nAccounts = num_accounts; - Bank = (Branch *)malloc(nBranches * sizeof(Branch)); - - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - - for (int i = 0; i < nBranches; i++) { - Bank[i].accounts = (Account *)malloc(nAccounts * sizeof(Account)); - pthread_mutex_init(&(Bank[i].branchLock), &attr); - for (int j = 0; j < nAccounts; j++) { - Bank[i].accounts[j].balance = 0; - pthread_mutex_init((&(Bank[i].accounts[j].acntLock)), &attr); - } - } -} - -void deleteBank(void) { - for (int i = 0; i < nBranches; i++) - free(Bank[i].accounts); - free(Bank); - nBranches = nAccounts = 0; -} - -long int withdraw(int branchNr, int accountNr, long int value) { - int rv, tmp; - rv = 0; - tmp = Bank[branchNr].accounts[accountNr].balance - value; - if (tmp >= 0) { - Bank[branchNr].accounts[accountNr].balance = tmp; - rv = value; - } - return rv; -} - -void deposit(int branchNr, int accountNr, long int value) { - Bank[branchNr].accounts[accountNr].balance += value; -} - -void transfer(int fromB, int toB, int accountNr, long int value) { - int money = withdraw(fromB, accountNr, value); - if (money >= 0) - deposit(toB, accountNr, money); -} - -void checkAssets(void) { - static long assets = 0; - long sum = 0; - for (int i = 0; i < nBranches; i++) { - for (int j = 0; j < nAccounts; j++) { - sum += (long)Bank[i].accounts[j].balance; - } - } - if (assets == 0) { - assets = sum; - printf("Balance of accounts is: %ld\n", sum); - } - else { - if (sum != assets) - printf("Balance of accounts is: %ld ... not correct\n", sum); - else - printf("Balance of accounts is: %ld ... correct\n", assets); - } -} - -//****************************************************************************** - diff --git a/P10_Sync/Sync/banking/a1/banking.h b/P10_Sync/Sync/banking/a1/banking.h deleted file mode 100644 index 1b8ea98..0000000 --- a/P10_Sync/Sync/banking/a1/banking.h +++ /dev/null @@ -1,19 +0,0 @@ -//****************************************************************************** -// Course: BSy -// File: banking.h -// Author: M. Thaler, ZHAW -// Purpose: locking mechanisms -// Version: v.fs20 -//****************************************************************************** -// banking functions - -void makeBank(int num_branches, int num_accounts); -void deleteBank(void); - -long int withdraw(int branchNr, int accountNr, long int value) ; -void deposit(int branchNr, int accountNr, long int value); -void transfer(int fromB, int toB, int accountNr, long int value); -void checkAssets(void); - -//****************************************************************************** - diff --git a/P10_Sync/Sync/banking/a1/main.c b/P10_Sync/Sync/banking/a1/main.c deleted file mode 100644 index 4efe5c6..0000000 --- a/P10_Sync/Sync/banking/a1/main.c +++ /dev/null @@ -1,93 +0,0 @@ -//****************************************************************************** -// Course: BSy -// File: main.c -// Author: M. Thaler, ZHAW -// Purpose: locking mechanisms -// Version: v.fs20 -//****************************************************************************** - -#include -#include -#include - -#include "banking.h" -#include "mtimer.h" -#include "mrandom.h" - -//****************************************************************************** -// constant values - -#define MAX_THREADS 16 -#define NUM_THREADS 4 - -#define TRANSFERS (16*1024*1024) -#define ACCOUNTS (2048) -#define BRANCHES (128) - -//****************************************************************************** -// globals - -int nThreads; - -//****************************************************************************** -// pusher - -void *pusher(void *arg) { - int idx = (int)(long)(arg); - mrand_t rand; - unsigned int seed = 17*idx; - mrandInit(seed, &rand); - int account, from, to, val; - int count = TRANSFERS / nThreads; - for (int i = 0; i < count; i++) { - account = (int)(mrandUInt(&rand) % ACCOUNTS); - from = (int)(mrandUInt(&rand) % BRANCHES); - to = (int)(mrandUInt(&rand) % BRANCHES); - val = (int)(mrandRange(1000, 100000, &rand)); - val = withdraw(from, account, val); - if (val > 0) - deposit(to, account, val); - } -} - -//****************************************************************************** -// main program - -int main(int argc, char *argv[]) { - - gtimer_t timer; - mrand_t ranvar; - long assets; - - // thread id's - pthread_t th[MAX_THREADS]; - - // get number of threads or default - if (argc > 1) - nThreads = atoi(argv[1]); - else - nThreads = NUM_THREADS; - nThreads = (nThreads > MAX_THREADS) ? MAX_THREADS : nThreads; - - mrandInit((MAX_THREADS + 1)*333, &ranvar); - - printf("\nRunning %d threads\n", nThreads); - makeBank(BRANCHES, ACCOUNTS); - for (int i = 0; i < ACCOUNTS; i++) - deposit(0, i, mrandRange(10, 1000*1000, &ranvar)); - checkAssets(); - - startGTimer(timer); - // create threads and pass thread number - for (long i = 0; i < nThreads; i++) - pthread_create(&th[i], NULL, pusher, (void *)i); - // wait for threads to terminate - for (int i = 0; i < nThreads; i++) - pthread_join(th[i], NULL); - stopGTimer(timer); - - checkAssets(); - printGTime(timer); -} - -//****************************************************************************** diff --git a/P10_Sync/Sync/banking/a1/makefile b/P10_Sync/Sync/banking/a1/makefile deleted file mode 100644 index 889b1e5..0000000 --- a/P10_Sync/Sync/banking/a1/makefile +++ /dev/null @@ -1,32 +0,0 @@ -# --------------------------------------------------------------------------- -# Makefile -# Course: BSy -# Date: M. Thaler, 1/2016 -# File: makefile -# Version: v.fs20 -# --------------------------------------------------------------------------- -#macros - -CC = gcc -CFLGS = -std=gnu99 -LIBS = -pthread -OFILES = main.o banking.o -HFILES = banking.h - -main: $(OFILES) $(HFILES) - $(CC) $(CFLGS) $(LIBS) $(OFILES) -o $@.e - -.c.o: - $(CC) $(CFLGS) -c $< - -.cc.o: - $(CC) $(CFLGS) -c $< - -clean: - rm -f *.o *.e - @echo "directory cleaned" - -all: - @rm -f *.o - make --no-print-directory main -#----------------------------------------------------------------------------- diff --git a/P10_Sync/Sync/banking/a1/mrandom.h b/P10_Sync/Sync/banking/a1/mrandom.h deleted file mode 100644 index 3d91f4d..0000000 --- a/P10_Sync/Sync/banking/a1/mrandom.h +++ /dev/null @@ -1,74 +0,0 @@ -#ifndef MY_RANDOMGENERATOR_DEFINITIONS -#define MY_RANDOMGENERATOR_DEFINITIONS - -#include -#include - -//****************************************************************************** -// Course: BSy -// File: mrandom.h -// Author: M. Thaler, ZHAW -// Purpose: thread safe random functions (reentrant) -// Version: v.fs20 -//****************************************************************************** -// date type: mrand_t -// -// functions: -// - void mrandInit(mrand_t *mrt) -> initialize with fixed seed -// - unsigned int mrandUInt(mrand_t *mrt) -> unsigned random number -// - unsigned int mrandRange(int low, int high, mrand_t *mrt) -// -> random number within range -// -/******************************************************************************/ -// constanst for ecuyer generator: length approx 8 x 10^12 - -#define M1 2147483563 -#define M2 2147483399 -#define A1 40014 -#define A2 40692 -#define Q1 53668 -#define Q2 52774 -#define R1 12211 -#define R2 3791 -#define MRAND_MAX M1-1 - -/******************************************************************************/ -typedef struct mrand_t { unsigned int s1; \ - unsigned int s2; } mrand_t; - -void mrandInit(unsigned int seed, mrand_t *mrt) { - mrt->s1 = 33777 + seed * 777; - mrt->s2 = 9777572 + seed * 33775; -} - -unsigned int mrandUInt(mrand_t *mrt) { - unsigned int rv; - mrt->s1 = A1 * (mrt->s1 % Q1) - mrt->s1 * (mrt->s1/Q1); - if (mrt->s1 <= 0) mrt->s1 += M1; - mrt->s2 = A2 * (mrt->s2 % Q2) - mrt->s2 * (mrt->s2/Q2); - if (mrt->s2 <= 0) mrt->s2 += M2; - rv = (mrt->s1 + mrt->s2) % M1; - return rv; -} - -double mrandDouble(mrand_t *mrt) { - unsigned int rv; - double dv; - mrt->s1 = A1 * (mrt->s1 % Q1) - mrt->s1 * (mrt->s1/Q1); - if (mrt->s1 <= 0) mrt->s1 += M1; - mrt->s2 = A2 * (mrt->s2 % Q2) - mrt->s2 * (mrt->s2/Q2); - if (mrt->s2 <= 0) mrt->s2 += M2; - rv = (mrt->s1 + mrt->s2) % M1; - dv = (double)rv / ((double)(M1-1)); - return dv; -} - -unsigned int mrandRange(unsigned int low, unsigned int high, mrand_t *mrt) { - assert(low <= high); - double drv = mrandDouble(mrt); - unsigned int av = (unsigned int)(drv * (double)(high-low) + 0.5); - return low + av; -} - -/******************************************************************************/ -#endif diff --git a/P10_Sync/Sync/banking/a1/mtimer.h b/P10_Sync/Sync/banking/a1/mtimer.h deleted file mode 100644 index 63aa950..0000000 --- a/P10_Sync/Sync/banking/a1/mtimer.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef TIMER_MACRO_DEFINITIONS -#define TIMER_MACRO_DEFINITIONS - -/******************************************************************************/ -// Course: BSy -// File: mtimer.h -// Purpose: timer functions gettimeofday() -// Author: M. Thaler, ZHAW, 1/2016 -// Version: v.fs20 -/******************************************************************************/ -// gettimeofday() -// function: elapsed time: between start and stop -// data type: gtimer_t -// functions: startGTimer(gtimer_t tdata) -> start timer -// stopGTimer(gtimer_t tdata) -> stop timer -// double getWallGTime(gtimer_t tdata) -> get time in s -// printGTime(X) -> print time in s -// -// -> see also "man gettimeofday" -/******************************************************************************/ - -#include -#include -#include -#include - - -/******************************************************************************/ - -typedef struct gtimer_t { struct timeval sT; \ - struct timeval eT; } gtimer_t; - -#define startGTimer(X) gettimeofday(&X.sT, NULL) -#define stopGTimer(X) gettimeofday(&X.eT, NULL) -#define getGTime(X) ((double)(X.eT.tv_sec) - (double)(X.sT.tv_sec)) +\ - ((double)X.eT.tv_usec - (double)X.sT.tv_usec)/1e6 -#define printGTime(X) printf("Run time %3.2lfs\n", getGTime(X)) - -/******************************************************************************/ - -#endif - diff --git a/P10_Sync/Sync/banking/a1/startApp b/P10_Sync/Sync/banking/a1/startApp deleted file mode 100755 index 211c509..0000000 --- a/P10_Sync/Sync/banking/a1/startApp +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -./main.e 1 -./main.e 2 -./main.e 4 diff --git a/P10_Sync/Sync/banking/a2/banking.h b/P10_Sync/Sync/banking/a2/banking.h deleted file mode 100644 index 1b8ea98..0000000 --- a/P10_Sync/Sync/banking/a2/banking.h +++ /dev/null @@ -1,19 +0,0 @@ -//****************************************************************************** -// Course: BSy -// File: banking.h -// Author: M. Thaler, ZHAW -// Purpose: locking mechanisms -// Version: v.fs20 -//****************************************************************************** -// banking functions - -void makeBank(int num_branches, int num_accounts); -void deleteBank(void); - -long int withdraw(int branchNr, int accountNr, long int value) ; -void deposit(int branchNr, int accountNr, long int value); -void transfer(int fromB, int toB, int accountNr, long int value); -void checkAssets(void); - -//****************************************************************************** - diff --git a/P10_Sync/Sync/banking/a2/main.c b/P10_Sync/Sync/banking/a2/main.c deleted file mode 100644 index a7645e5..0000000 --- a/P10_Sync/Sync/banking/a2/main.c +++ /dev/null @@ -1,92 +0,0 @@ -//****************************************************************************** -// Course: BSy -// File: main.c -// Author: M. Thaler, ZHAW -// Purpose: locking mechanisms -// Version: v.fs20 -//****************************************************************************** - -#include -#include -#include - -#include "banking.h" -#include "mtimer.h" -#include "mrandom.h" - -//****************************************************************************** -// constant values - -#define MAX_THREADS 16 -#define NUM_THREADS 4 - -#define TRANSFERS (16*1024*1024) -#define ACCOUNTS (2048) -#define BRANCHES (128) - -//****************************************************************************** -// globals - -int nThreads; - -//****************************************************************************** -// pusher - -void *pusher(void *arg) { - int idx = (int)(long)(arg); - mrand_t rand; - unsigned int seed = 17*idx; - mrandInit(seed, &rand); - int account, from, to, val; - int count = TRANSFERS / nThreads; - for (int i = 0; i < count; i++) { - account = (int)(mrandUInt(&rand) % ACCOUNTS); - from = (int)(mrandUInt(&rand) % BRANCHES); - to = (int)(mrandUInt(&rand) % BRANCHES); - val = (int)(mrandRange(1000, 100000, &rand)); - val = withdraw(from, account, val); - deposit(to, account, val); - } -} - -//****************************************************************************** -// main program - -int main(int argc, char *argv[]) { - - gtimer_t timer; - mrand_t ranvar; - long assets; - - // thread id's - pthread_t th[MAX_THREADS]; - - // get number of threads or default - if (argc > 1) - nThreads = atoi(argv[1]); - else - nThreads = NUM_THREADS; - nThreads = (nThreads > MAX_THREADS) ? MAX_THREADS : nThreads; - - mrandInit((MAX_THREADS + 1)*333, &ranvar); - - printf("\nRunning %d threads\n", nThreads); - makeBank(BRANCHES, ACCOUNTS); - for (int i = 0; i < ACCOUNTS; i++) - deposit(0, i, mrandRange(10, 1000*1000, &ranvar)); - checkAssets(); - - startGTimer(timer); - // create threads and pass thread number - for (long i = 0; i < nThreads; i++) - pthread_create(&th[i], NULL, pusher, (void *)i); - // wait for threads to terminate - for (int i = 0; i < nThreads; i++) - pthread_join(th[i], NULL); - stopGTimer(timer); - - checkAssets(); - printGTime(timer); -} - -//****************************************************************************** diff --git a/P10_Sync/Sync/banking/a2/makefile b/P10_Sync/Sync/banking/a2/makefile deleted file mode 100644 index 889b1e5..0000000 --- a/P10_Sync/Sync/banking/a2/makefile +++ /dev/null @@ -1,32 +0,0 @@ -# --------------------------------------------------------------------------- -# Makefile -# Course: BSy -# Date: M. Thaler, 1/2016 -# File: makefile -# Version: v.fs20 -# --------------------------------------------------------------------------- -#macros - -CC = gcc -CFLGS = -std=gnu99 -LIBS = -pthread -OFILES = main.o banking.o -HFILES = banking.h - -main: $(OFILES) $(HFILES) - $(CC) $(CFLGS) $(LIBS) $(OFILES) -o $@.e - -.c.o: - $(CC) $(CFLGS) -c $< - -.cc.o: - $(CC) $(CFLGS) -c $< - -clean: - rm -f *.o *.e - @echo "directory cleaned" - -all: - @rm -f *.o - make --no-print-directory main -#----------------------------------------------------------------------------- diff --git a/P10_Sync/Sync/banking/a2/mrandom.h b/P10_Sync/Sync/banking/a2/mrandom.h deleted file mode 100644 index 3d91f4d..0000000 --- a/P10_Sync/Sync/banking/a2/mrandom.h +++ /dev/null @@ -1,74 +0,0 @@ -#ifndef MY_RANDOMGENERATOR_DEFINITIONS -#define MY_RANDOMGENERATOR_DEFINITIONS - -#include -#include - -//****************************************************************************** -// Course: BSy -// File: mrandom.h -// Author: M. Thaler, ZHAW -// Purpose: thread safe random functions (reentrant) -// Version: v.fs20 -//****************************************************************************** -// date type: mrand_t -// -// functions: -// - void mrandInit(mrand_t *mrt) -> initialize with fixed seed -// - unsigned int mrandUInt(mrand_t *mrt) -> unsigned random number -// - unsigned int mrandRange(int low, int high, mrand_t *mrt) -// -> random number within range -// -/******************************************************************************/ -// constanst for ecuyer generator: length approx 8 x 10^12 - -#define M1 2147483563 -#define M2 2147483399 -#define A1 40014 -#define A2 40692 -#define Q1 53668 -#define Q2 52774 -#define R1 12211 -#define R2 3791 -#define MRAND_MAX M1-1 - -/******************************************************************************/ -typedef struct mrand_t { unsigned int s1; \ - unsigned int s2; } mrand_t; - -void mrandInit(unsigned int seed, mrand_t *mrt) { - mrt->s1 = 33777 + seed * 777; - mrt->s2 = 9777572 + seed * 33775; -} - -unsigned int mrandUInt(mrand_t *mrt) { - unsigned int rv; - mrt->s1 = A1 * (mrt->s1 % Q1) - mrt->s1 * (mrt->s1/Q1); - if (mrt->s1 <= 0) mrt->s1 += M1; - mrt->s2 = A2 * (mrt->s2 % Q2) - mrt->s2 * (mrt->s2/Q2); - if (mrt->s2 <= 0) mrt->s2 += M2; - rv = (mrt->s1 + mrt->s2) % M1; - return rv; -} - -double mrandDouble(mrand_t *mrt) { - unsigned int rv; - double dv; - mrt->s1 = A1 * (mrt->s1 % Q1) - mrt->s1 * (mrt->s1/Q1); - if (mrt->s1 <= 0) mrt->s1 += M1; - mrt->s2 = A2 * (mrt->s2 % Q2) - mrt->s2 * (mrt->s2/Q2); - if (mrt->s2 <= 0) mrt->s2 += M2; - rv = (mrt->s1 + mrt->s2) % M1; - dv = (double)rv / ((double)(M1-1)); - return dv; -} - -unsigned int mrandRange(unsigned int low, unsigned int high, mrand_t *mrt) { - assert(low <= high); - double drv = mrandDouble(mrt); - unsigned int av = (unsigned int)(drv * (double)(high-low) + 0.5); - return low + av; -} - -/******************************************************************************/ -#endif diff --git a/P10_Sync/Sync/banking/a2/mtimer.h b/P10_Sync/Sync/banking/a2/mtimer.h deleted file mode 100644 index 63aa950..0000000 --- a/P10_Sync/Sync/banking/a2/mtimer.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef TIMER_MACRO_DEFINITIONS -#define TIMER_MACRO_DEFINITIONS - -/******************************************************************************/ -// Course: BSy -// File: mtimer.h -// Purpose: timer functions gettimeofday() -// Author: M. Thaler, ZHAW, 1/2016 -// Version: v.fs20 -/******************************************************************************/ -// gettimeofday() -// function: elapsed time: between start and stop -// data type: gtimer_t -// functions: startGTimer(gtimer_t tdata) -> start timer -// stopGTimer(gtimer_t tdata) -> stop timer -// double getWallGTime(gtimer_t tdata) -> get time in s -// printGTime(X) -> print time in s -// -// -> see also "man gettimeofday" -/******************************************************************************/ - -#include -#include -#include -#include - - -/******************************************************************************/ - -typedef struct gtimer_t { struct timeval sT; \ - struct timeval eT; } gtimer_t; - -#define startGTimer(X) gettimeofday(&X.sT, NULL) -#define stopGTimer(X) gettimeofday(&X.eT, NULL) -#define getGTime(X) ((double)(X.eT.tv_sec) - (double)(X.sT.tv_sec)) +\ - ((double)X.eT.tv_usec - (double)X.sT.tv_usec)/1e6 -#define printGTime(X) printf("Run time %3.2lfs\n", getGTime(X)) - -/******************************************************************************/ - -#endif - diff --git a/P10_Sync/Sync/banking/a2/startApp b/P10_Sync/Sync/banking/a2/startApp deleted file mode 100755 index 211c509..0000000 --- a/P10_Sync/Sync/banking/a2/startApp +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -./main.e 1 -./main.e 2 -./main.e 4 diff --git a/P10_Sync/Sync/banking/a3/banking.c b/P10_Sync/Sync/banking/a3/banking.c deleted file mode 100644 index e9694ce..0000000 --- a/P10_Sync/Sync/banking/a3/banking.c +++ /dev/null @@ -1,119 +0,0 @@ -//****************************************************************************** -// Course: BSy -// File: banking.c -// Author: M. Thaler, ZHAW -// Purpose: locking mechanisms -// Version: v.fs20 -//****************************************************************************** - -#include -#include -#include -#include - -#include "banking.h" - -//****************************************************************************** - -typedef struct account_struct_ { - long int balance; - pthread_mutex_t acntLock; -} Account; - -typedef struct branch_struct { - Account *accounts; - pthread_mutex_t branchLock; -} Branch; - -//****************************************************************************** - -static Branch *bank; -static int nBranches, nAccounts; - -//****************************************************************************** -// banking functions - -void makeBank(int num_branches, int num_accounts) { - nBranches = num_branches; - nAccounts = num_accounts; - bank = (Branch *)malloc(nBranches * sizeof(Branch)); - - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - //pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); - - for (int i = 0; i < nBranches; i++) { - bank[i].accounts = (Account *)malloc(nAccounts * sizeof(Account)); - for (int j = 0; j < nAccounts; j++) { - bank[i].accounts[j].balance = 0; - } - } -} - -void deletebank(void) { - for (int i = 0; i < nBranches; i++) - free(bank[i].accounts); - free(bank); - nBranches = nAccounts = 0; -} - -long int withdraw(int branchNr, int accountNr, long int value) { - int rv, tmp; - rv = 0; - tmp = bank[branchNr].accounts[accountNr].balance - value; - if (tmp >= 0) { - bank[branchNr].accounts[accountNr].balance = tmp; - rv = value; - }; - return rv; -} - -void deposit(int branchNr, int accountNr, long int value) { - bank[branchNr].accounts[accountNr].balance += value; -} - -void transfer(int fromB, int toB, int accountNr, long int value) { - int money = withdraw(fromB, accountNr, value); - deposit(toB, accountNr, money); -} - -void checkAssets(void) { - static long assets = 0; - long int sum = 0; - for (int i = 0; i < nBranches; i++) { - for (int j = 0; j < nAccounts; j++) { - sum += (long int)bank[i].accounts[j].balance; - } - } - if (assets == 0) { - assets = sum; - printf("Balance of accounts is: %ld\n", sum); - } - else { - if (sum != assets) { - printf("Balance of accounts is: %ld ... not correct\n", sum); - } - else - printf("Balance of accounts is: %ld ... correct\n", assets); - } -} - -int checkIBC(void) { - static long ibcError = 0; - long sum = 0; - for (int i = 0; i < nBranches; i++) { - pthread_mutex_lock(&bank[i].branchLock); - } - for (int i = 0; i < nBranches; i++) { - for (int j = 0; j < nAccounts; j++) { - sum += (long)bank[i].accounts[j].balance; - } - } - for (int i = 0; i < nBranches; i++) { - pthread_mutex_unlock(&bank[i].branchLock); - } - if (ibcError == 0) ibcError = sum; - return (ibcError != sum); -} -//****************************************************************************** - diff --git a/P10_Sync/Sync/banking/a3/banking.h b/P10_Sync/Sync/banking/a3/banking.h deleted file mode 100644 index e8a4017..0000000 --- a/P10_Sync/Sync/banking/a3/banking.h +++ /dev/null @@ -1,20 +0,0 @@ -//****************************************************************************** -// Course: BSy -// File: banking.h -// Author: M. Thaler, ZHAW -// Purpose: locking mechanisms -// Version: v.fs20 -//****************************************************************************** -// banking functions - -void makeBank(int num_branches, int num_accounts); -void deleteBank(void); - -long withdraw(int branch, int account, long int value) ; -void deposit(int branch, int account, long int value); -void transfer(int fromB, int toB, int account, long int value); -void checkAssets(void); -int checkIBC(void); - -//****************************************************************************** - diff --git a/P10_Sync/Sync/banking/a3/main.c b/P10_Sync/Sync/banking/a3/main.c deleted file mode 100644 index 623de36..0000000 --- a/P10_Sync/Sync/banking/a3/main.c +++ /dev/null @@ -1,107 +0,0 @@ -//****************************************************************************** -// Course: BSy -// File: main.c -// Author: M. Thaler, ZHAW -// Purpose: locking mechanisms -// Version: v.fs20 -//****************************************************************************** - -#include -#include -#include - -#include "banking.h" -#include "mtimer.h" -#include "mrandom.h" - -//****************************************************************************** -// constant values - -#define MAX_THREADS 16 -#define NUM_THREADS 5 - -#define TRANSFERS (16*1024*1024L) -#define ACCOUNTS (2048) -#define BRANCHES (128) - -//****************************************************************************** -// globals - -int nThreads; -int ibc = 0; - -//****************************************************************************** -// customers - -void *pusher(void *arg) { - int idx = (int)(long)(arg); - mrand_t rand; - unsigned int seed = 17*idx; - mrandInit(seed, &rand); - int account, from, to, val; - int count = TRANSFERS / nThreads; - for (int i = 0; i < count; i++) { - account = (int)(mrandUInt(&rand) % ACCOUNTS); - from = (int)(mrandUInt(&rand) % BRANCHES); - to = (int)(mrandUInt(&rand) % BRANCHES); - val = (int)(mrandRange(1000, 100000, &rand)); - transfer(from, to, account, val); - } -} - -void *checker(void *arg) { - for (int i = 0; i < 1000; i = i) { - ibc += checkIBC(); - usleep(100*1000); - } -} - -//****************************************************************************** -// main program - -int main(int argc, char *argv[]) { - - gtimer_t timer; - mrand_t ranvar; - long assets; - - // thread id's - pthread_t th[MAX_THREADS]; - - // get number of threads or default - if (argc > 1) - nThreads = atoi(argv[1]); - else - nThreads = NUM_THREADS; - nThreads = (nThreads > MAX_THREADS) ? MAX_THREADS : nThreads; - - mrandInit((MAX_THREADS + 1)*333, &ranvar); - - printf("\nRunning %d threads\n", nThreads); - makeBank(BRANCHES, ACCOUNTS); - for (int i = 0; i < ACCOUNTS; i++) - deposit(0, i, mrandRange(10, 1000*1000, &ranvar)); - checkAssets(); - - startGTimer(timer); - // create threads and pass thread number - pthread_create(&th[0], NULL, checker, (void *)0); - sleep(1); - for (long i = 1; i < nThreads; i++) - pthread_create(&th[i], NULL, pusher, (void *)i); - // wait for threads to terminate - for (int i = 1; i < nThreads; i++) - pthread_join(th[i], NULL); - stopGTimer(timer); - - checkAssets(); - printGTime(timer); - - if (ibc) - printf("\n\033[41mYou do not comply with the IBC rules \033[0m\n\n"); - else - printf("\n\033[42mYou do comply with the IBC rules \033[0m\n\n"); - -} - -//****************************************************************************** diff --git a/P10_Sync/Sync/banking/a3/makefile b/P10_Sync/Sync/banking/a3/makefile deleted file mode 100644 index 889b1e5..0000000 --- a/P10_Sync/Sync/banking/a3/makefile +++ /dev/null @@ -1,32 +0,0 @@ -# --------------------------------------------------------------------------- -# Makefile -# Course: BSy -# Date: M. Thaler, 1/2016 -# File: makefile -# Version: v.fs20 -# --------------------------------------------------------------------------- -#macros - -CC = gcc -CFLGS = -std=gnu99 -LIBS = -pthread -OFILES = main.o banking.o -HFILES = banking.h - -main: $(OFILES) $(HFILES) - $(CC) $(CFLGS) $(LIBS) $(OFILES) -o $@.e - -.c.o: - $(CC) $(CFLGS) -c $< - -.cc.o: - $(CC) $(CFLGS) -c $< - -clean: - rm -f *.o *.e - @echo "directory cleaned" - -all: - @rm -f *.o - make --no-print-directory main -#----------------------------------------------------------------------------- diff --git a/P10_Sync/Sync/banking/a3/mrandom.h b/P10_Sync/Sync/banking/a3/mrandom.h deleted file mode 100644 index 3d91f4d..0000000 --- a/P10_Sync/Sync/banking/a3/mrandom.h +++ /dev/null @@ -1,74 +0,0 @@ -#ifndef MY_RANDOMGENERATOR_DEFINITIONS -#define MY_RANDOMGENERATOR_DEFINITIONS - -#include -#include - -//****************************************************************************** -// Course: BSy -// File: mrandom.h -// Author: M. Thaler, ZHAW -// Purpose: thread safe random functions (reentrant) -// Version: v.fs20 -//****************************************************************************** -// date type: mrand_t -// -// functions: -// - void mrandInit(mrand_t *mrt) -> initialize with fixed seed -// - unsigned int mrandUInt(mrand_t *mrt) -> unsigned random number -// - unsigned int mrandRange(int low, int high, mrand_t *mrt) -// -> random number within range -// -/******************************************************************************/ -// constanst for ecuyer generator: length approx 8 x 10^12 - -#define M1 2147483563 -#define M2 2147483399 -#define A1 40014 -#define A2 40692 -#define Q1 53668 -#define Q2 52774 -#define R1 12211 -#define R2 3791 -#define MRAND_MAX M1-1 - -/******************************************************************************/ -typedef struct mrand_t { unsigned int s1; \ - unsigned int s2; } mrand_t; - -void mrandInit(unsigned int seed, mrand_t *mrt) { - mrt->s1 = 33777 + seed * 777; - mrt->s2 = 9777572 + seed * 33775; -} - -unsigned int mrandUInt(mrand_t *mrt) { - unsigned int rv; - mrt->s1 = A1 * (mrt->s1 % Q1) - mrt->s1 * (mrt->s1/Q1); - if (mrt->s1 <= 0) mrt->s1 += M1; - mrt->s2 = A2 * (mrt->s2 % Q2) - mrt->s2 * (mrt->s2/Q2); - if (mrt->s2 <= 0) mrt->s2 += M2; - rv = (mrt->s1 + mrt->s2) % M1; - return rv; -} - -double mrandDouble(mrand_t *mrt) { - unsigned int rv; - double dv; - mrt->s1 = A1 * (mrt->s1 % Q1) - mrt->s1 * (mrt->s1/Q1); - if (mrt->s1 <= 0) mrt->s1 += M1; - mrt->s2 = A2 * (mrt->s2 % Q2) - mrt->s2 * (mrt->s2/Q2); - if (mrt->s2 <= 0) mrt->s2 += M2; - rv = (mrt->s1 + mrt->s2) % M1; - dv = (double)rv / ((double)(M1-1)); - return dv; -} - -unsigned int mrandRange(unsigned int low, unsigned int high, mrand_t *mrt) { - assert(low <= high); - double drv = mrandDouble(mrt); - unsigned int av = (unsigned int)(drv * (double)(high-low) + 0.5); - return low + av; -} - -/******************************************************************************/ -#endif diff --git a/P10_Sync/Sync/banking/a3/mtimer.h b/P10_Sync/Sync/banking/a3/mtimer.h deleted file mode 100644 index 63aa950..0000000 --- a/P10_Sync/Sync/banking/a3/mtimer.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef TIMER_MACRO_DEFINITIONS -#define TIMER_MACRO_DEFINITIONS - -/******************************************************************************/ -// Course: BSy -// File: mtimer.h -// Purpose: timer functions gettimeofday() -// Author: M. Thaler, ZHAW, 1/2016 -// Version: v.fs20 -/******************************************************************************/ -// gettimeofday() -// function: elapsed time: between start and stop -// data type: gtimer_t -// functions: startGTimer(gtimer_t tdata) -> start timer -// stopGTimer(gtimer_t tdata) -> stop timer -// double getWallGTime(gtimer_t tdata) -> get time in s -// printGTime(X) -> print time in s -// -// -> see also "man gettimeofday" -/******************************************************************************/ - -#include -#include -#include -#include - - -/******************************************************************************/ - -typedef struct gtimer_t { struct timeval sT; \ - struct timeval eT; } gtimer_t; - -#define startGTimer(X) gettimeofday(&X.sT, NULL) -#define stopGTimer(X) gettimeofday(&X.eT, NULL) -#define getGTime(X) ((double)(X.eT.tv_sec) - (double)(X.sT.tv_sec)) +\ - ((double)X.eT.tv_usec - (double)X.sT.tv_usec)/1e6 -#define printGTime(X) printf("Run time %3.2lfs\n", getGTime(X)) - -/******************************************************************************/ - -#endif - diff --git a/P10_Sync/Sync/banking/a3/startApp b/P10_Sync/Sync/banking/a3/startApp deleted file mode 100755 index 211c509..0000000 --- a/P10_Sync/Sync/banking/a3/startApp +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -./main.e 1 -./main.e 2 -./main.e 4 diff --git a/P10_Sync/Sync/basicSequence/coffeeTeller.c b/P10_Sync/Sync/basicSequence/coffeeTeller.c deleted file mode 100644 index 4b96adb..0000000 --- a/P10_Sync/Sync/basicSequence/coffeeTeller.c +++ /dev/null @@ -1,46 +0,0 @@ -/******************************************************************************* -* File: coffeTeller.c -* Purpose: simple sequence with semaphores -* Course: bsy -* Author: M. Thaler, 2011 -* Revision: 5/2012, 7/2013 -* Version: v.fs20 -*******************************************************************************/ - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "commonDefs.h" - -//****************************************************************************** - -int main(void) { - - int i; - sem_t *coin, *coffee, *ready; - - // set up a semaphore - coin = sem_open(COIN_SEMAPHOR, 0); - coffee = sem_open(COFFEE_SEMAPHOR, 0); - ready = sem_open(READY_SEMAPHOR, 0); - - // start teller machine - printf("\nCoffee teller machine starting\n\n"); - - i = 0; - while (i < ITERS) { - printf("teller (%d): waiting for coin\n", i); - printf(" (%d): got coin\n", i); - printf(" (%d): dispense coffee\n", i); - i++; - } -} - -//****************************************************************************** diff --git a/P10_Sync/Sync/basicSequence/commonDefs.h b/P10_Sync/Sync/basicSequence/commonDefs.h deleted file mode 100644 index bf53a72..0000000 --- a/P10_Sync/Sync/basicSequence/commonDefs.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef MY_DEFINITIONS_HEADER -#define MY_DEFINITIONS_HEADER - -/******************************************************************************* -* File: commonDefs.h -* Purpose: common definitions -* Course: bsy -* Author: M. Thaler, 2011 -* Revision: 5/2012, 7/2013, 4/2014 -* Version: v.fs20 -*******************************************************************************/ - -#define MYTURN_SEMAPHOR "/my_semaphor_1_name_simple_seq" -#define COIN_SEMAPHOR "/my_semaphor_2_name_simple_seq" -#define COFFEE_SEMAPHOR "/my_semaphor_3_name_simple_seq" -#define READY_SEMAPHOR "/my_semaphor_4_name_simple_seq" - -#define ITERS (100*1000*1000) -#define CUSTOMERS 4 - -//****************************************************************************** - -#define checkSem(X) {if (X == SEM_FAILED) {perror("sem_open"); exit(-1);}} - -#define drinkingCoffee(X) {usleep((((1+X)*rand())+100000)&0xFFFFF);} - -//****************************************************************************** - -#endif - diff --git a/P10_Sync/Sync/basicSequence/customer.c b/P10_Sync/Sync/basicSequence/customer.c deleted file mode 100644 index a4609e5..0000000 --- a/P10_Sync/Sync/basicSequence/customer.c +++ /dev/null @@ -1,50 +0,0 @@ -/******************************************************************************* -* File: customer.c -* Purpose: simple sequence with semaphores -* Course: bsy -* Author: M. Thaler, 2011 -* Revision: 5/2012, 7/2013 -* Version: v.fs20 -*******************************************************************************/ - -#include -#include -#include - -#include -#include -#include - -#include "commonDefs.h" - -//****************************************************************************** - -int main(int argc, char *argv[]) { - - int i, myID; - sem_t *myTurn, *coin, *coffee, *ready; - - if (argc > 1) - myID = atoi(argv[1]); - else - myID = 0; - - // set up a semaphore - myTurn = sem_open(MYTURN_SEMAPHOR, 0); - coin = sem_open(COIN_SEMAPHOR, 0); - coffee = sem_open(COFFEE_SEMAPHOR, 0); - ready = sem_open(READY_SEMAPHOR, 0); - - // start customer - printf("Customer starting (%d)\n", myID); - - // now check the sum - for (i = 0; i < ITERS; i++) { - printf("\t\t\t\tcustomer(%d) put coin %d\n", myID, i); - printf("\t\t\t\tcustomer(%d) waiting for coffee %d\n", myID, i); - printf("\t\t\t\tcustomer(%d) got coffee %d\n", myID, i); - drinkingCoffee(myID); - } -} - -//****************************************************************************** diff --git a/P10_Sync/Sync/basicSequence/makefile b/P10_Sync/Sync/basicSequence/makefile deleted file mode 100644 index 4b2726a..0000000 --- a/P10_Sync/Sync/basicSequence/makefile +++ /dev/null @@ -1,40 +0,0 @@ -# Author M. Thaler InIT/ZHAW -# Version v.fs20 - -CMP= gcc -std=gnu99 -CMPFLAGS= -Wall -g -LIB= -pthread -EXENAME0= startApp.e -EXENAME1= coffeeTeller.e -EXENAME2= customer.e - -doit: - @make --no-print-directory clean - @make --no-print-directory startApp - @make --no-print-directory coffeeTeller - @make --no-print-directory customer - -startApp: startApp.o - $(CMP) $(CMPFLAGS) startApp.o $(LIB) -o $(EXENAME0) - -coffeeTeller: coffeeTeller.o - $(CMP) $(CMPFLAGS) coffeeTeller.o $(LIB) -o $(EXENAME1) - -customer: customer.o - $(CMP) $(CMPFLAGS) customer.o $(LIB) -o $(EXENAME2) - -.c.o: - $(CMP) -c $(CMPFLAGS) $< - -.cc.o: - $(CMP) -c $(CMPFLAGS) $< - -all: - @make clean - make doit - -clean: - @rm -f *.e *.o - -purge: - @make clean diff --git a/P10_Sync/Sync/basicSequence/startApp.c b/P10_Sync/Sync/basicSequence/startApp.c deleted file mode 100644 index 06717b6..0000000 --- a/P10_Sync/Sync/basicSequence/startApp.c +++ /dev/null @@ -1,72 +0,0 @@ -/******************************************************************************* -* File: startApp.c -* Purpose: ice cream teller, basic sequence -* Course: bsy -* Author: M. Thaler, 2011 -* Revision: 5/2012, 7/2013 -* Version: v.fs20 -*******************************************************************************/ - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "commonDefs.h" - -//****************************************************************************** - -int main(void) { - - int j; - char string[8]; - sem_t *myTurn, *coin, *coffee, *ready; - pid_t tellerPID; - - sem_unlink(MYTURN_SEMAPHOR); // delete seamphor if it still exists - sem_unlink(COIN_SEMAPHOR); // delete seamphor if it still exists - sem_unlink(COFFEE_SEMAPHOR); // delete seamphor if it still exists - sem_unlink(READY_SEMAPHOR); // delete seamphor if it still exists - - // set up a semaphore (? -> initial value of semaphor) - // checkSem() -> macro defined in commonDefs.h - - /* - myTurn = sem_open(MYTURN_SEMAPHOR, O_CREAT, 0700, ?); checkSem(myTurn); - coin = sem_open(COIN_SEMAPHOR, O_CREAT, 0700, ?); checkSem(coin); - coffee = sem_open(COFFEE_SEMAPHOR, O_CREAT, 0700, ?); checkSem(coffee); - ready = sem_open(READY_SEMAPHOR, O_CREAT, 0700, ?); checkSem(ready); - */ - - // now that the resources are set up, the supervisor can be started - for (j = 1; j <= CUSTOMERS; j++) { - if (fork() == 0) { - sprintf(string, "%d", j); - execl("./customer.e", "customer.e", string, NULL); - printf("*** could not start customer.e ***\n"); - } - } - - if ((tellerPID = fork()) == 0) { - execl("./coffeeTeller.e", "coffeeTeller.e", "0", NULL); - printf("*** could not start coffeTeller ***\n"); - } - - waitpid(tellerPID, NULL, 0); - system("killall coffeeTeller.e"); - system("killall customer.e"); // kill all customers - - // clean up resources - sem_unlink(MYTURN_SEMAPHOR); - sem_unlink(COIN_SEMAPHOR); - sem_unlink(COFFEE_SEMAPHOR); - sem_unlink(READY_SEMAPHOR); - printf("\n"); -} - -//****************************************************************************** diff --git a/P10_Sync/Sync/multiCPUs b/P10_Sync/Sync/multiCPUs deleted file mode 100755 index f7cd5bc..0000000 --- a/P10_Sync/Sync/multiCPUs +++ /dev/null @@ -1,56 +0,0 @@ -#!/bin/bash -#****************************************************************************** -# Purpose: turn off all CPU's but one (CPU 0), turn on all CPU's -# Linux only -# Author: M. Thaler, BSy 4/2014 - -# Usage: multiCPUs off: turn off all CPUs but one -# multiCPUs on: turn all CPUs on -# -# Version: v.fs20 -#****************************************************************************** - -theDir="/sys/devices/system/cpu" - -# check if sys exists -if test ! -d $theDir -then - echo "*** cannot set to single CPU ***" - exit 1 -fi - -# figure out what to do: multi CPUs off (single CPU) or all CPU's on -let status=1 -if test "$1" = "on" -then - let status=1 -else - if test "$1" = "off" - then - let status=0 - else - echo "*** what do you want: ... on or off? ***" - exit 1 - fi -fi - -# turn CPU's on or off - -cd $theDir - -CPUs=`ls -d cpu* | grep 'cpu[0-9][0-9]*'` -for CPU in $CPUs -do - if test ! "$CPU" = "cpu0" - then - echo "$CPU on = $status" - currVal=`cat ./$CPU/online` - # only change if different ... avoid error message - if test ! "$currVal" = "$status" - then - echo "$status" > "./$CPU/online" - fi - fi -done - -#****************************************************************************** diff --git a/P10_Sync/Sync/mutex/README_MAC_OSX b/P10_Sync/Sync/mutex/README_MAC_OSX deleted file mode 100644 index fdf5c02..0000000 --- a/P10_Sync/Sync/mutex/README_MAC_OSX +++ /dev/null @@ -1,7 +0,0 @@ -For Mac users - -"pthread_mutex_lock(&lock)" is slow on MACs compared to Linux: - -use instead: "while (pthread_mutex_trylock(&lock) > 0) {} ;" - -which implements a spin lock diff --git a/P10_Sync/Sync/mutex/coffeeTeller.c b/P10_Sync/Sync/mutex/coffeeTeller.c deleted file mode 100644 index de41d50..0000000 --- a/P10_Sync/Sync/mutex/coffeeTeller.c +++ /dev/null @@ -1,46 +0,0 @@ -/******************************************************************************* -* File: coffeTeller.c -* Purpose: coffe teller with pthreads -* Course: bsy -* Author: M. Thaler, 2011 -* Revision: 5/2012 -* Version: v.fs20 -*******************************************************************************/ - -#include -#include -#include -#include -#include - -#include - -#include "commonDefs.h" - -//****************************************************************************** - -void *coffeeTeller(void* data) { - - int i; - cData *cD = (cData *) data; - - // now start selling coffee - printf("\nCoffee teller machine starting\n\n"); - - i = 0; - while (i < ITERATIONS) { - if (cD->coinCount != cD->selCount1 + cD->selCount2) { - printf("error c = %5d s1 =%6d s2 =%6d diff: %4d\ti = %d\n", - cD->coinCount, cD->selCount1, cD->selCount2, - cD->coinCount - cD->selCount1 - cD->selCount2, - i); - cD->coinCount = 0; - cD->selCount1 = cD->selCount2 = 0; - } - if (i%1000000 == 0) printf("working\n"); - i++; - } - pthread_exit(0); -} - -//****************************************************************************** diff --git a/P10_Sync/Sync/mutex/coffeeTeller.h b/P10_Sync/Sync/mutex/coffeeTeller.h deleted file mode 100644 index 3cf9274..0000000 --- a/P10_Sync/Sync/mutex/coffeeTeller.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef MY_DEFINITIONS_TELLER_H -#define MY_DEFINITIONS_TELLER_H - -/******************************************************************************* -* File: coffeTeller.h -* Purpose: coffe teller with pthreads -* Course: bsy -* Author: M. Thaler, 2011 -* Revision: 5/2012 -* Version: v.fs20 -*******************************************************************************/ - -void *coffeeTeller(void* data); - -//****************************************************************************** - -#endif diff --git a/P10_Sync/Sync/mutex/commonDefs.h b/P10_Sync/Sync/mutex/commonDefs.h deleted file mode 100644 index 074dc86..0000000 --- a/P10_Sync/Sync/mutex/commonDefs.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef MY_DEFINITIONS_HEADER -#define MY_DEFINITIONS_HEADER - -/******************************************************************************* -* File: commonDefs.h -* Purpose: header file for common definitions -* Course: bsy -* Author: M. Thaler, 2011 -* Revision: 5/2012 -* Version: v.fs20 -*******************************************************************************/ - -#define ITERATIONS (100*1000*1000) - -//****************************************************************************** - -#define CUSTOMERS 4 // number of customers to be started - -//****************************************************************************** -// common data - -typedef struct { - int coinCount; // number of paid coffees - int selCount1; // number of chosen coffees of type 1 - int selCount2; // number of chosen coffees of type 2 - pthread_mutex_t lock; // common lock -} cData; - -//****************************************************************************** - -#endif - diff --git a/P10_Sync/Sync/mutex/customer.c b/P10_Sync/Sync/mutex/customer.c deleted file mode 100644 index f340905..0000000 --- a/P10_Sync/Sync/mutex/customer.c +++ /dev/null @@ -1,44 +0,0 @@ -/******************************************************************************* -* File: customer.c -* Purpose: customer thread -* Course: bsy -* Author: M. Thaler, 2011 -* Revision: 5/2012 -* Version: v.fs20 -*******************************************************************************/ - -#include -#include -#include -#include - -#include - -#include "commonDefs.h" -#include "mrand.h" - -//***************************************************************************** - -void *customer(void* data) { - float ranNum; - int i; - rand_t randnum; - - cData *cD = (cData *) data; - - rand_seed(&randnum, 0); - - // put coin and select coffee - for (i = 0; i < ITERATIONS; i++) { - ranNum = rand_float(&randnum); - cD->coinCount += 1; - if (ranNum < 0.5) - cD->selCount1 += 1; - else - cD->selCount2 += 1; - } - - pthread_exit(0); -} - -//***************************************************************************** diff --git a/P10_Sync/Sync/mutex/customer.h b/P10_Sync/Sync/mutex/customer.h deleted file mode 100644 index 58a2298..0000000 --- a/P10_Sync/Sync/mutex/customer.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef MY_DEFINITIONS_CUSTOMER_H -#define MY_DEFINITIONS_CUSTOMER_H - -/******************************************************************************* -* File: customer.h -* Purpose: customer thread -* Course: bsy -* Author: M. Thaler, 2011 -* Revision: 5/2012 -* Version: v.fs20 -*******************************************************************************/ - -void *customer(void* data); - -//****************************************************************************** -#endif diff --git a/P10_Sync/Sync/mutex/makefile b/P10_Sync/Sync/mutex/makefile deleted file mode 100644 index 744e460..0000000 --- a/P10_Sync/Sync/mutex/makefile +++ /dev/null @@ -1,33 +0,0 @@ -# Author M. Thaler -# Version v.fs20 - -CMP= gcc -std=gnu99 -CMPFLAGS= -Wall -g -LIB= -pthread -EXENAME1= startApp.e - -INCLS= commonDefs.h -FILES= startApp.o coffeeTeller.o customer.o - -doit: - @make --no-print-directory clean - @make --no-print-directory coffeeTeller - -coffeeTeller: $(FILES) - $(CMP) $(CMPFLAGS) $(FILES) $(LIB) -o $(EXENAME1) - -.c.o: $(INCLS) - $(CMP) -c $(CMPFLAGS) $< - -.cc.o: $(INCLS) - $(CMP) -c $(CMPFLAGS) $< - -all: - @make clean - make doit - -clean: - @rm -f *.e *.o - -purge: - @make clean diff --git a/P10_Sync/Sync/mutex/mrand.h b/P10_Sync/Sync/mutex/mrand.h deleted file mode 100644 index 0535f7f..0000000 --- a/P10_Sync/Sync/mutex/mrand.h +++ /dev/null @@ -1,102 +0,0 @@ -#ifndef MY_THREADSAVE_RANDOM_GENERATOR -#define MY_THREADSAVE_RANDOM_GENERATOR - -/****************************************************************************** -* File: mrand.h -* Purpose: simple portable thread save random generator -* ccording to Schrage -* Author: M. Thaler, 5/2012, BSy -* Version: v.fs20 -******************************************************************************/ - -#include - -//****************************************************************************** - -#define MMM_MR 2147483563 -#define AAA_MR 40014 -#define QQQ_MR 53668 -#define RRR_MR 12211 - -#define MRAND_MAX (MMM_MR-1) - -//****************************************************************************** - -typedef int rand_t; - -// seed ------------------------------------------------------------------------ - -void rand_seed(rand_t *srand, int seed) { - struct timeval prandom_seed; - gettimeofday(&prandom_seed, NULL); - if (seed > 0) - *srand = seed; - else - *srand = (prandom_seed.tv_usec); - -} - -// integer random value ------------------------------------------------------- -// range 0 .. MRAND_MAX (inclusive) - -int rand_int(rand_t *srand) { - int nx; - nx = AAA_MR * (*srand % QQQ_MR) - RRR_MR * (*srand / QQQ_MR); - *srand = (nx > 0) ? nx : nx + MMM_MR; - return (*srand); -} - -// float random value --------------------------------------------------------- -// range 0.0 .. 1.0 (inclusive) - -float rand_float(rand_t *srand) { - float fx; - *srand = rand_int(srand); - fx = (float)(*srand) / ((float)MRAND_MAX); - return (fx); -} - -// double random value -------------------------------------------------------- -// range 0.0 .. 1.0 (inclusive) - -double rand_double(rand_t *srand) { - double fx; - *srand = rand_int(srand); - fx = (double)(*srand) / ((double)MRAND_MAX); - return (fx); -} - -// unsigned int random range -------------------------------------------------- -// range low .. high - -unsigned int rand_range(unsigned int low, unsigned int high, rand_t *srand) { - unsigned int rv; - double prop; - prop = ((double)(high - low)) * rand_double(srand); - rv = low + ((unsigned int) prop); - return(rv); -} - -// unsigned int probability --------------------------------------------------- -// returns 0 or 1 - -unsigned int rand_prob(double prob, rand_t *srand) { - if (rand_double(srand) >= 1.0-prob) - return 1; - else - return 0; -} - -// unsigned int toss coin ----------------------------------------------------- -// returns 0 or 1 - -unsigned int toss_coin(rand_t *srand) { - if (rand_float(srand) >= 0.5) - return 1; - else - return 0; -} - -//****************************************************************************** - -#endif diff --git a/P10_Sync/Sync/mutex/startApp.c b/P10_Sync/Sync/mutex/startApp.c deleted file mode 100644 index 13ef658..0000000 --- a/P10_Sync/Sync/mutex/startApp.c +++ /dev/null @@ -1,59 +0,0 @@ -/******************************************************************************* -* File: startApp.c -* Purpose: mutex with locks -* Course: bsy -* Author: M. Thaler, 2011 -* Revision: 5/2012 -* Version: v.fs20 -*******************************************************************************/ - -#include -#include -#include -#include -#include -#include - -#include - -#include "commonDefs.h" -#include "coffeeTeller.h" -#include "customer.h" - -//****************************************************************************** -// common data - -cData cD; - -//****************************************************************************** - -int main(void) { - - unsigned int j; - int pthr; - - pthread_t tellerThread, customerThreads[CUSTOMERS]; - - cD.coinCount = 0; - cD.selCount1 = 0; - cD.selCount2 = 0; - pthread_mutex_init(&(cD.lock), NULL); - - // start teller and customers now that everything is set up - pthr = pthread_create(&tellerThread, NULL, coffeeTeller, &cD); - assert(pthr == 0); - for (j = 0; j < CUSTOMERS; j++) { - pthr = pthread_create(&(customerThreads[j]), NULL, customer, &cD); - assert(pthr == 0); - } - - // wait for all threads to terminate - pthread_join(tellerThread, NULL); - - for (j = 0; j < CUSTOMERS; j++) { - pthr = pthread_join(customerThreads[j], NULL); - assert(pthr == 0); - } -} - -//****************************************************************************** diff --git a/README.md b/README.md deleted file mode 100644 index ff73d86..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# snp-lab-code -Code für die Praktika zur Vorlesung Systemnahe Programmierung diff --git a/shared.mk b/shared.mk deleted file mode 100644 index 7bd6acb..0000000 --- a/shared.mk +++ /dev/null @@ -1,61 +0,0 @@ -# minimal required settings -SNP_TESTLIB := $(if $(SNP_TESTLIB),$(SNP_TESTLIB),"SNP_TESTLIB-is-not-set") -SNP_DOXYFILE := $(if $(SNP_DOXYFILE),$(SNP_DOXYFILE),"SNP_DOXYFILE-is-not-set") - -# directories to create (and remove upon cleanup) -CREATEDIRS := bin doc - -# list of derived file names from the source names -OBJECTS := $(SOURCES:%.c=%.o) # list of gcc -c ... produced *.o files -DEPS := $(SOURCES:%.c=%.d) # list of gcc -MD ... produced *.d files -TSTOBJECTS := $(TSTSOURCES:%.c=%.o) # list of gcc -c ... produced *.o files -TSTDEPS := $(TSTSOURCES:%.c=%.d) # list of gcc -MD ... produced *.d files -TSTTARGET := $(CURDIR)/tests/runtest - -# shared libs -TSTLIBDIR := $(SNP_TESTLIB)/lib -TSTINCDIR := $(SNP_TESTLIB)/include - -# full path to the target -FULLTARGET := $(CURDIR)/$(TARGET) - -# commands and flags -CC = gcc -CFLAGS = -std=c99 -Wall -pedantic -g -CPPFLAGS = -MD -Isrc -Itests -I$(TSTINCDIR) -DTARGET=$(FULLTARGET) -LDFLAGS = -static - -# targets which get always visited (without checking any up-to-date state) -.PHONY: default clean test doc install mkdir - -# targets -default: $(FULLTARGET) - @echo "#### $< built ####" - -$(FULLTARGET): mkdir $(OBJECTS) Makefile - $(LINK.c) -o $@ $(OBJECTS) $(LIBS) - -clean: - $(RM) $(TARGET) $(OBJECTS) $(DEPS) $(TSTTARGET) $(TSTOBJECTS) $(TSTDEPS) $(wildcard */*~ *~ tests/*.txt) - $(RM) -r $(CREATEDIRS) - @echo "#### $@ done ####" - -doc: - doxygen $(SNP_DOXYFILE) > /dev/null - @echo "#### $@ done ####" - -test: $(TSTTARGET) - (cd tests; $(TSTTARGET)) - @echo "#### $< executed ####" - -$(TSTTARGET): $(FULLTARGET) $(TSTOBJECTS) - $(LINK.c) -o $(TSTTARGET) $(TSTOBJECTS) $(LIBS) -lcunit -L$(TSTLIBDIR) -lsnptest - @echo "#### $@ built ####" - - -# create needed directories (ignoring any error) -mkdir: - -mkdir -p $(CREATEDIRS) - -# read in the gcc -MD ... produced dependencies (ignoring any error) --include $(DEPS) $(TSTDEPS) diff --git a/testlib/Makefile b/testlib/Makefile deleted file mode 100644 index 5706175..0000000 --- a/testlib/Makefile +++ /dev/null @@ -1,69 +0,0 @@ -# what to produce -TARGET := lib/libsnptest.a - -# public headers -HEADERS := include/test_utils.h - -# implementation files -SOURCES := src/test_utils.c - -# test implementations -TSTSOURCES := tests/tests.c - -# directories to create (and remove upon cleanup) -CREATEDIRS := lib doc - -# list of derived file names from the source names -OBJECTS := $(SOURCES:%.c=%.o) # list of gcc -c ... produced *.o files -DEPS := $(SOURCES:%.c=%.d) # list of gcc -MD ... produced *.d files -TSTOBJECTS := $(TSTSOURCES:%.c=%.o) # list of gcc -c ... produced *.o files -TSTDEPS := $(TSTSOURCES:%.c=%.d) # list of gcc -MD ... produced *.d files -TSTTARGET := $(CURDIR)/tests/runtest - -# full path to the target -FULLTARGET := $(CURDIR)/$(TARGET) - -# commands and flags -CC = gcc -CFLAGS = -std=c99 -Wall -pedantic -g -CPPFLAGS = -MD -Isrc -Itests -Iinclude -DTARGET=$(FULLTARGET) -LDFLAGS = -ARFLAGS = rc - -# targets which get always visited (without checking any up-to-date state) -.PHONY: default clean test doc install mkdir - -# targets -default: $(FULLTARGET) - @echo "#### $< built ####" - -$(FULLTARGET): mkdir $(OBJECTS) Makefile - $(AR) $(ARFLAGS) $@ $(OBJECTS) - -clean: - $(RM) $(TARGET) $(OBJECTS) $(DEPS) $(TSTTARGET) $(TSTOBJECTS) $(TSTDEPS) $(wildcard */*~ *~ tests/*.txt) - $(RM) -r $(CREATEDIRS) - @echo "#### $@ done ####" - -install: $(FULLTARGET) - @echo "#### $< installed ####" - -doc: - doxygen ../Doxyfile > /dev/null - @echo "#### $@ done ####" - -test: $(TSTTARGET) - (cd tests; $(TSTTARGET)) - @echo "#### $< executed ####" - -$(TSTTARGET): $(FULLTARGET) $(TSTOBJECTS) - $(LINK.c) -o $(TSTTARGET) $(TSTOBJECTS) $(FULLTARGET) -lcunit - @echo "#### $@ built ####" - - -# create needed directories (ignoring any error) -mkdir: - -mkdir -p $(CREATEDIRS) - -# read in the gcc -MD ... produced dependencies (ignoring any error) --include $(DEPS) $(TSTDEPS) diff --git a/testlib/include/test_utils.h b/testlib/include/test_utils.h deleted file mode 100644 index f076c1f..0000000 --- a/testlib/include/test_utils.h +++ /dev/null @@ -1,146 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Common test utilities for writing SNP tests. - */ -#ifndef _TEST_UTILS_H_ -#define _TEST_UTILS_H_ - -#include -#include -#include - -/// Stringize macro (evaluates the passed macro and makes a string out of it). Non-macros are stringized as-is. -#define XSTR(x) STR(x) -/// Stringize macro (does *not* evaluate the passed macro - makes the macro *name* a string). Non-macros are stringized as-is. -#define STR(x) #x - -/** - * @brief Gracefully checks by means of fopen(..., "r") if a file exists. - * @param[in] file_path The file to check for existence. - * @returns Returns 0 if the file does not exist, 1 otherwise. - * @remark If the file does not exist, errno was affected. This function takes care of this, i.e. it safes and restores the original errno state. - * @remark In case of an error situation, the function fails with a hard assert.h assertion violation. - * This allows to use the function outside of a tests, e.g. in setup and teardown functions. - */ -int file_exists(const char file_path[]); - -/** - * @brief Removes the given file if it exists. - * @param[in] file_path: The path to the file which gets removed if it exists. - * @remark Silently ignores any error. If you are interrested in the errors, set first *errno* to zero, and check it after the call. - * @remark In case of an error situation, the function fails with a hard assert.h assertion violation. - * This allows to use the function outside of a tests, e.g. in setup and teardown functions. - */ -void remove_file_if_exists(const char file_path[]); - -/** - * @brief Checks if the file content matches exactly the given lines. - * The lines must contain the new-line character. This is especially important if a file terminates without a new line character. - * @param[in] file: The path to the file to check against the lines. - * @param[in] lines: The array of lines which must match the file content exactly. Newlines must be part of the lines too. - * @param[in] n_lines: The number of lines in the line array. - */ -void assert_lines(const char file[], const char *lines[], size_t n_lines); - -/** - * @brief The test function's callback type. - */ -typedef void (*test_function_t)(void); - -/** - * @brief Boiler-plate code as a macro to define the complete body of the main function of the tests suite. - * @param[in] suite: The name of the test suite (const char *). - * @param[in] setup: The setup callback function which is executed before the first test is executed (*int setup(void)*). - * @param[in] cleanup: The teardown callback function which is executed after the last test is executed (*int teardown(void)*). - * @param[in] ...: The variable list of test_function callback (*void test(void)*) which constitue the test cases. They are executed in the given sequence. - * @remark Example code (see also CUnit Framework Documentation): - * @code - * // setup and teardown - * int setup(void) - * { - * // do some initialization if needed - * // ... - * return 0; // success - * } - * int teardown(void) - * { - * // do some cleanup if needed - * // ... - * return 0; // success - * } - * // tests - * void test_main_no_args(void) - * { - * // arrange - * // ... - - * // act - * // ... - - * // assert (use the CUnit CU_ASSERT_... macros) - * // ... - * } - * void test_main_one_arg(void) - * { - * //... - * } - * void test_main_two_args(void) - * { - * //... - * } - - * // execute the tests - * int main(void) - * { - * TestMainBasic("Hello World", setup, teardown - * , test_main_no_args - * , test_main_one_arg - * , test_main_two_args - * ); - * } - * @endcode - */ -#define TestMainBasic(suite, setup, cleanup, ...) \ - do { \ - CU_pSuite pSuite = NULL; \ - \ - /* initialize the CUnit test registry */ \ - if (CUE_SUCCESS != CU_initialize_registry()) \ - return CU_get_error(); \ - \ - /* functions and their names */ \ - test_function_t tests[] = { __VA_ARGS__ }; \ - char all_names[] = #__VA_ARGS__; \ - const size_t n = sizeof(tests)/sizeof(*tests); \ - const char *names[sizeof(tests)/sizeof(*tests)] = { strtok(all_names, ", ") } ; \ - for(size_t i = 1; i < n; i++) { \ - names[i] = strtok(NULL, ", "); \ - } \ - /* init suite and tests */ \ - pSuite = CU_add_suite(suite, setup, cleanup); \ - if (pSuite) { \ - size_t i; \ - for(i = 0; i < n; i++) { \ - if (!CU_add_test(pSuite, names[i], tests[i])) break; \ - } \ - /* Run all tests using the CUnit Basic interface */ \ - if (i == n) { \ - CU_basic_set_mode(CU_BRM_VERBOSE); \ - CU_basic_run_tests(); \ - } \ - } \ - CU_cleanup_registry(); \ - return CU_get_error(); \ - } while(0) \ - - -#endif diff --git a/testlib/mainpage.dox b/testlib/mainpage.dox deleted file mode 100644 index ab128a5..0000000 --- a/testlib/mainpage.dox +++ /dev/null @@ -1,10 +0,0 @@ -/** - * @mainpage SNP - Test Utilities - * - * @section Purpose - * - * This is a supporting test library for SNP tests. - * - * This project needs to be built before the labs. - * It provides the needed header files in the include folder and the libraries in the lib folder. - */ diff --git a/testlib/src/test_utils.c b/testlib/src/test_utils.c deleted file mode 100644 index 8d5c954..0000000 --- a/testlib/src/test_utils.c +++ /dev/null @@ -1,137 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Implementation of the test_utils. - */ -#include -#include -#include -#include -#include -#include -#include -#include "test_utils.h" - -int file_exists(const char file_path[]) -{ - // preconditions - assert(file_path); - int errno_safe = errno; - errno = 0; - // try and forgive... - FILE *file = fopen(file_path, "r"); - assert(file || (!file && (errno == ENOENT))); // either it exists or "No such file or directory" error code (see man errno). - errno = errno_safe; // fopen will set errno if the file does not exist - if (file) { - assert(0 == fclose(file)); - return 1; // existed - } - return 0; // did not exist -} - -void remove_file_if_exists(const char file_path[]) -{ - // we take the risk that between checking and removing, some undesired file access may happen and jeopardize the control logic... - if (file_exists(file_path)) { - assert(0 == unlink(file_path)); - } -} - -static void print_current_char(int c) -{ - if (c < 0) - { - printf(" "); - } - else if (isprint(c)) - { - printf(" 0x%02x = '%c'", c, c); - } - else - { - printf(" 0x%02x = %s", c, ""); - } -} - -void assert_lines(const char file[], const char *lines[], size_t n_lines) -{ - // preconditions - CU_ASSERT_PTR_NOT_NULL_FATAL(file); - CU_ASSERT_PTR_NOT_NULL_FATAL(lines); - - // file access may always fail - FILE *input = fopen(file, "r"); - if (!input) perror(file); - CU_ASSERT_PTR_NOT_NULL_FATAL(input); - - // process all lines and compare to the file content - size_t i = 0; - size_t n = 0; - for(i = 0; i < n_lines && n == 0; i++) { - const char *exp_line = lines[i]; - // allow NULL lines to allow for empty files: fix -pedantic warning "ISO C forbids zero-size array" - if (exp_line) { - size_t len = n = strlen(exp_line); - CU_ASSERT(n > 0); - while (n > 0) { - int exp_c = *exp_line; - int act_c = fgetc(input); - CU_ASSERT_FALSE(feof(input)); - CU_ASSERT_EQUAL(act_c, exp_c); - if (act_c != exp_c) { - printf("\nfile %s: line %zu, pos %zu: expected =", file, i+1, len-n+1); - print_current_char(exp_c); - printf(", actual ="); - print_current_char(act_c); - printf("\n"); - break; - } - exp_line++; - n--; - } - } - } - CU_ASSERT_FALSE(feof(input)); - (void)fgetc(input); - CU_ASSERT_TRUE(feof(input)); - CU_ASSERT_EQUAL(i, n_lines); - CU_ASSERT_EQUAL(n, 0); - - // successfully reached the end... - int fclose_result = fclose(input); - CU_ASSERT(0 == fclose_result); - - // print actual versus expected in case of error - if (n != 0 || i != n_lines) { - printf("---- EXPECTED ----\n"); - for(int i = 0; i < n_lines; i++) { - const char *p = lines[i]; - while(p && *p) { - putchar(*p); - p++; - } - } - printf("---- ACTUAL (%s) ----\n", file); - FILE* fd = fopen(file, "r"); - int last = 0; - while(fd && !feof(fd)) { - int c = fgetc(fd); - if (c != EOF) { - last = c; - putchar(c); - } - } - if (fd) fclose(fd); - if (last != '\n') putchar('\n'); - printf("---- END ----\n"); - } - -} diff --git a/testlib/tests/tests.c b/testlib/tests/tests.c deleted file mode 100644 index 0cb08eb..0000000 --- a/testlib/tests/tests.c +++ /dev/null @@ -1,171 +0,0 @@ -/* ---------------------------------------------------------------------------- - * -- _____ ______ _____ - - * -- |_ _| | ____|/ ____| - - * -- | | _ __ | |__ | (___ Institute of Embedded Systems - - * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - - * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - - * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - - * ---------------------------------------------------------------------------- - */ -/** - * @file - * @brief Test suite for the given package. - */ -#include -#include -#include -#include "test_utils.h" - -#ifndef TARGET // must be given by the make file --> see test target -#error missing TARGET define -#endif - -/// @brief The name of the STDOUT text file. -#define OUTFILE "stdout.txt" -/// @brief The name of the STDERR text file. -#define ERRFILE "stderr.txt" -/// @brief Some test output file -#define EMPTYFILE "EmptyFile.txt" -/// @brief Some test output file -#define NONEMPTYFILE "NonEmptyFile.txt" -/// @brief Some test output file -#define NONEMPTYFILE_NONLEND "NonEmptyFileNoNlEnd.txt" - -// setup & teardown -static int setup(void) -{ - remove_file_if_exists(OUTFILE); - remove_file_if_exists(ERRFILE); - remove_file_if_exists(EMPTYFILE); - remove_file_if_exists(NONEMPTYFILE); - remove_file_if_exists(NONEMPTYFILE_NONLEND); - // do nothing - return 0; // success -} -static int teardown(void) -{ - // do nothing - return 0; // success -} - - -// tests -static void test_remove_file_that_exists(void) -{ - // ** arrange ** - - // create a file - FILE *file = fopen(OUTFILE, "w"); - CU_ASSERT_PTR_NOT_NULL(file); - CU_ASSERT_EQUAL(fclose(file), 0); - errno = 0; - - // ** act ** - remove_file_if_exists(OUTFILE); - - // ** assert ** - - // make sure the file is removed - CU_ASSERT_EQUAL(errno, 0); - file = fopen(OUTFILE, "r"); - CU_ASSERT_TRUE(!file && errno == ENOENT); - // cleanup gracefully - if (file) CU_ASSERT_EQUAL(fclose(file), 0); - errno = 0; -} -static void test_remove_file_that_does_not_exist(void) -{ - // ** arrange ** - remove_file_if_exists(OUTFILE); - // the file is now supposed to not exist any more --> see test_remove_file_that_exists test result - - // ** act ** - - // call with a not existing file - remove_file_if_exists(OUTFILE); - // must not fail in any internal assertion - - // ** assert ** - - // make sure the file is removed - CU_ASSERT_EQUAL(errno, 0); - FILE *file = fopen(OUTFILE, "r"); - CU_ASSERT_TRUE(!file && errno == ENOENT); - // cleanup gracefully - if (file) CU_ASSERT_EQUAL(fclose(file), 0); - errno = 0; -} - -static void test_assert_lines_empty_file(void) -{ - // ** arrange ** - - // empty file - remove_file_if_exists(EMPTYFILE); - FILE *file = fopen(EMPTYFILE, "w"); - CU_ASSERT_PTR_NOT_NULL_FATAL(file); - CU_ASSERT_EQUAL(fclose(file), 0); - const char *lines[] = { NULL }; - - // ** act ** - assert_lines(EMPTYFILE, lines, sizeof(lines)/sizeof(*lines)); - - // ** assert ** - - // no assertions should have happened within assert_lines(...) -} -static void test_assert_lines_non_empty_file(void) -{ - // ** arrange ** - - // reference file - remove_file_if_exists(NONEMPTYFILE); - FILE *file = fopen(NONEMPTYFILE, "w"); - CU_ASSERT_PTR_NOT_NULL_FATAL(file); - CU_ASSERT_EQUAL(fprintf(file, "LINE1\n"), 6); - CU_ASSERT_EQUAL(fprintf(file, "LINE2\n"), 6); - CU_ASSERT_EQUAL(fclose(file), 0); - const char *lines[] = { "LINE1\n", "LINE2\n"}; - - // ** act ** - assert_lines(NONEMPTYFILE, lines, sizeof(lines)/sizeof(*lines)); - - // ** assert ** - - // no assertions should have happened within assert_lines(...) -} -static void test_assert_lines_no_newline_at_the_end(void) -{ - // ** arrange ** - - // reference file - remove_file_if_exists(NONEMPTYFILE_NONLEND); - FILE *file = fopen(NONEMPTYFILE_NONLEND, "w"); - CU_ASSERT_PTR_NOT_NULL_FATAL(file); - CU_ASSERT_EQUAL(fprintf(file, "LINE1\n"), 6); - CU_ASSERT_EQUAL(fprintf(file, "LINE2"), 5); - CU_ASSERT_EQUAL(fclose(file), 0); - const char *lines[] = { "LINE1\n", "LINE2"}; - - // ** act ** - assert_lines(NONEMPTYFILE_NONLEND, lines, sizeof(lines)/sizeof(*lines)); - - // ** assert ** - - // no assertions should have happened within assert_lines(...) -} - -/** - * @brief Registers and runs the tests. - * @returns success (==0) or error (!= 0) - */ -int main(void) -{ - TestMainBasic("SNP Test Lib", setup, teardown - , test_remove_file_that_exists - , test_remove_file_that_does_not_exist - , test_assert_lines_empty_file - , test_assert_lines_non_empty_file - , test_assert_lines_no_newline_at_the_end - ); -}