publish testlib and doxygen shared configuration

This commit is contained in:
Andreas Gieriet
2020-02-10 00:11:44 +01:00
parent c3f83f6315
commit 6921ec60be
7 changed files with 568 additions and 224 deletions
Regular → Executable
+11 -18
View File
@@ -1,8 +1,8 @@
# what to produce
TARGET := bin/libprogctest.a
TARGET := lib/libprogctest.a
# public headers
HEADERS := src/test_utils.h
HEADERS := include/test_utils.h
# implementation files
SOURCES := src/test_utils.c
@@ -11,7 +11,7 @@ SOURCES := src/test_utils.c
TSTSOURCES := tests/tests.c
# directories to create (and remove upon cleanup)
CREATEDIRS := bin doc
CREATEDIRS := lib doc
# list of derived file names from the source names
OBJECTS := $(SOURCES:%.c=%.o) # list of gcc -c ... produced *.o files
@@ -20,22 +20,14 @@ 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
# libraries
CUNITINCDIR := $(CURDIR)/../CUnit/include
CUNITLIBDIR := $(CURDIR)/../CUnit/lib
# where to install the static library and the associated headers
INSTALLLIBDIR := $(CURDIR)/../lib
INSTALLINCDIR := $(CURDIR)/../include
# full path to the target
FULLTARGET := $(CURDIR)/$(TARGET)
# commands and flags
CC = gcc
CFLAGS = -std=c99 -Wall -g
CPPFLAGS = -MD -Isrc -Itests -I$(INSTALLINCDIR) -I$(CUNITINCDIR) -DTARGET=$(FULLTARGET)
LDFLAGS = -static -z muldefs
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)
@@ -54,17 +46,18 @@ clean:
@echo "#### $@ done ####"
install: $(FULLTARGET)
mkdir -p $(INSTALLLIBDIR) $(INSTALLINCDIR)
cp -f $(FULLTARGET) $(INSTALLLIBDIR)/
cp -f $(HEADERS) $(INSTALLINCDIR)/
@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) -L$(CUNITLIBDIR) -lcunit
$(LINK.c) -o $(TSTTARGET) $(TSTOBJECTS) $(FULLTARGET) -lcunit
@echo "#### $@ built ####"
@@ -16,7 +16,7 @@
#include <stdlib.h>
#include <stdio.h>
#include "CUnit/Basic.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)
@@ -109,38 +109,38 @@ typedef void (*test_function_t)(void);
* }
* @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) \
#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
+10
View File
@@ -0,0 +1,10 @@
/**
* @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.
*/
+101 -81
View File
@@ -17,101 +17,121 @@
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include "CUnit/Basic.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
// 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));
}
// 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);
// 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);
// 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 *line = lines[i];
CU_ASSERT_PTR_NOT_NULL(line);
if (line) {
size_t len = n = strlen(line);
CU_ASSERT(n > 0);
while (n > 0) {
int c = fgetc(input);
CU_ASSERT_FALSE(feof(input));
CU_ASSERT_EQUAL(c, *line);
if (c != *line) {
printf("\nfile %s: line %zu, pos %zu = %d = '%c', expected = %d = '%c'\n",
file, i+1, len-n+1, c, isprint(c) ? c : '.', *line, isprint(*line) ? *line : '.');
break;
}
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);
// 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");
}
// 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");
}
}
+90 -89
View File
@@ -13,7 +13,7 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include "CUnit/Basic.h"
#include <CUnit/Basic.h>
#include "test_utils.h"
#ifndef TARGET // must be given by the make file --> see test target
@@ -34,137 +34,138 @@
// 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
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
// do nothing
return 0; // success
}
// tests
static void test_remove_file_that_exists(void)
{
// ** arrange **
// ** arrange **
// create a file
FILE *file = fopen(OUTFILE, "w");
CU_ASSERT_PTR_NOT_NULL(file);
CU_ASSERT_EQUAL(fclose(file), 0);
errno = 0;
// 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);
// ** act **
remove_file_if_exists(OUTFILE);
// ** assert **
// ** 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;
// 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
// ** 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 **
// ** act **
// call with a not existing file
remove_file_if_exists(OUTFILE);
// must not fail in any internal assertion
// call with a not existing file
remove_file_if_exists(OUTFILE);
// must not fail in any internal assertion
// ** assert **
// ** 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;
// 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 **
// ** 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[] = {};
// 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));
// ** act **
assert_lines(EMPTYFILE, lines, sizeof(lines)/sizeof(*lines));
// ** assert **
// ** assert **
// no assertions should have happened within assert_lines(...)
// no assertions should have happened within assert_lines(...)
}
static void test_assert_lines_non_empty_file(void)
{
// ** arrange **
// ** 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"};
// 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));
// ** act **
assert_lines(NONEMPTYFILE, lines, sizeof(lines)/sizeof(*lines));
// ** assert **
// ** assert **
// no assertions should have happened within assert_lines(...)
// no assertions should have happened within assert_lines(...)
}
static void test_assert_lines_no_newline_at_the_end(void)
{
// ** arrange **
// ** 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"};
// 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));
// ** act **
assert_lines(NONEMPTYFILE_NONLEND, lines, sizeof(lines)/sizeof(*lines));
// ** assert **
// ** assert **
// no assertions should have happened within assert_lines(...)
// no assertions should have happened within assert_lines(...)
}
/**
* @brief Registers and runs the tests.
* @returns success (==0) or error (!= 0)
*/
int main(void)
{
TestMainBasic("PROGC 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
);
TestMainBasic("PROGC 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
);
}