From 81780410aae4cd96d83e7ef5037f56a98286ff43 Mon Sep 17 00:00:00 2001 From: Philipp Schafft Date: Wed, 7 Nov 2018 14:44:00 +0000 Subject: [PATCH] Feature: Added basic test framework --- Makefile.am | 2 + configure.ac | 2 + src/tests/Makefile.am | 28 ++++++++++++++ src/tests/ctest_lib.c | 84 +++++++++++++++++++++++++++++++++++++++++ src/tests/ctest_lib.h | 21 +++++++++++ src/tests/ctest_suite.c | 20 ++++++++++ 6 files changed, 157 insertions(+) create mode 100644 src/tests/Makefile.am create mode 100644 src/tests/ctest_lib.c create mode 100644 src/tests/ctest_lib.h create mode 100644 src/tests/ctest_suite.c diff --git a/Makefile.am b/Makefile.am index fddc367..f1b5ce1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -40,3 +40,5 @@ AM_CFLAGS = -I$(top_srcdir)/include static: $(MAKE) all LDFLAGS="${LDFLAGS} -all-static" + +include $(srcdir)/src/tests/Makefile.am diff --git a/configure.ac b/configure.ac index eb7df1e..61080e8 100644 --- a/configure.ac +++ b/configure.ac @@ -147,6 +147,8 @@ CFLAGS="${CFLAGS} ${PTHREAD_CFLAGS}" CPPFLAGS="${CPPFLAGS} ${PTHREAD_CPPFLAGS}" LIBS="${LIBS} ${PTHREAD_LIBS}" +AC_REQUIRE_AUX_FILE([tap-driver.sh]) + AC_CONFIG_HEADERS([config.h]) AX_PREFIX_CONFIG_H([include/igloo/config.h], [igloo_CTC]) diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am new file mode 100644 index 0000000..7b8381a --- /dev/null +++ b/src/tests/Makefile.am @@ -0,0 +1,28 @@ +## Process this file with automake to produce Makefile.in + +####################### +# libigloo unit tests # +####################### + +TEST_LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) \ + $(top_srcdir)/build-aux/tap-driver.sh + +check_PROGRAMS = + +# +# Helper library for TAP tests +# + +libice_ctest_la_SOURCES = %reldir%/ctest_lib.c %reldir%/ctest_lib.h +noinst_LTLIBRARIES = libice_ctest.la + +# +# Test programs +# + +ctest_suite_test_SOURCES = %reldir%/ctest_suite.c +ctest_suite_test_LDADD = libice_ctest.la +check_PROGRAMS += ctest_suite.test + +# Add all programs to TESTS +TESTS = $(check_PROGRAMS) diff --git a/src/tests/ctest_lib.c b/src/tests/ctest_lib.c new file mode 100644 index 0000000..176a7df --- /dev/null +++ b/src/tests/ctest_lib.c @@ -0,0 +1,84 @@ +/* Icecast + * + * This program is distributed under the GNU General Public License, version 2. + * A copy of this license is included with this source. + * + * Copyright 2018, Philipp "ph3-der-loewe" Schafft , + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +#include "ctest_lib.h" + +static size_t ctest_g_test_num; +static int ctest_g_bailed_out; + +void ctest_init(void) +{ + ctest_g_test_num = 0; + ctest_g_bailed_out = 0; +} + +void ctest_fin(void) +{ + printf("1..%zu\n", ctest_g_test_num); +} + +void ctest_test(const char *desc, int res) +{ + const char *prefix = NULL; + + if (ctest_bailed_out()) + return; + + ctest_g_test_num++; + + if (res) { + prefix = "ok"; + } else { + prefix = "not ok"; + } + + if (desc) { + printf("%s %zu %s\n", prefix, ctest_g_test_num, desc); + } else { + printf("%s %zu\n", prefix, ctest_g_test_num); + } +} + +void ctest_diagnostic(const char *line) +{ + printf("# %s\n", line); +} + +void ctest_diagnostic_printf(const char *format, ...) +{ + va_list ap; + va_start(ap, format); + + printf("# "); + vprintf(format, ap); + printf("\n"); + + va_end(ap); +} + +void ctest_bail_out(const char *reason) +{ + ctest_g_bailed_out = 1; + if (reason) { + printf("Bail out! %s\n", reason); + } else { + printf("Bail out!\n"); + } +} + +int ctest_bailed_out(void) +{ + return ctest_g_bailed_out; +} diff --git a/src/tests/ctest_lib.h b/src/tests/ctest_lib.h new file mode 100644 index 0000000..69eb22a --- /dev/null +++ b/src/tests/ctest_lib.h @@ -0,0 +1,21 @@ +/* Icecast + * + * This program is distributed under the GNU General Public License, version 2. + * A copy of this license is included with this source. + * + * Copyright 2018, Philipp "ph3-der-loewe" Schafft , + */ + +#ifndef __CTEST_LIB_H__ +#define __CTEST_LIB_H__ + +void ctest_init(void); +void ctest_fin(void); + +void ctest_test(const char *desc, int res); +void ctest_diagnostic(const char *line); +void ctest_diagnostic_printf(const char *format, ...); +void ctest_bail_out(const char *reason); +int ctest_bailed_out(void); + +#endif diff --git a/src/tests/ctest_suite.c b/src/tests/ctest_suite.c new file mode 100644 index 0000000..91f7587 --- /dev/null +++ b/src/tests/ctest_suite.c @@ -0,0 +1,20 @@ +/* Icecast + * + * This program is distributed under the GNU General Public License, version 2. + * A copy of this license is included with this source. + * + * Copyright 2018, Philipp "ph3-der-loewe" Schafft , + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "ctest_lib.h" + +int main (void) { + ctest_init(); + ctest_test("suite working", 1); + ctest_fin(); + return 0; +}