remove stuff till HS21, backup at other branch
This commit is contained in:
@@ -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)
|
||||
@@ -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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <CUnit/Basic.h>
|
||||
|
||||
/// 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 (<I>const char *</I>).
|
||||
* @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
|
||||
@@ -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.
|
||||
*/
|
||||
@@ -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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <CUnit/Basic.h>
|
||||
#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(" <End-Of-File>");
|
||||
}
|
||||
else if (isprint(c))
|
||||
{
|
||||
printf(" 0x%02x = '%c'", c, c);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(" 0x%02x = %s", c, "<Not-Printable-Char>");
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <CUnit/Basic.h>
|
||||
#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
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user