From c2a0b6bada97cb497225a08206084116ebc77f33 Mon Sep 17 00:00:00 2001 From: "Gergely Polonkai (W00d5t0ck)" Date: Fri, 4 Oct 2013 23:37:36 +0200 Subject: [PATCH] Add test case for GsweTimestamp This can be run with make check. Other tests are to follow --- Makefile.am | 2 +- configure.ac | 18 ++++ tests/Makefile.am | 192 ++++++++++++++++++++++++++++++++++++ tests/gswe-timestamp-test.c | 41 ++++++++ 4 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 tests/Makefile.am create mode 100644 tests/gswe-timestamp-test.c diff --git a/Makefile.am b/Makefile.am index b59d2f9..b657228 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,5 @@ ACLOCAL_AMFLAGS = -I m4 -SUBDIRS = swe swe/src swe/doc src po docs/reference/swe-glib data +SUBDIRS = swe swe/src swe/doc src po docs/reference/swe-glib data tests DISTCHECK_CONFIGURE_FLAGS = --enable-gtk-doc diff --git a/configure.ac b/configure.ac index 6e9edec..1fedcae 100644 --- a/configure.ac +++ b/configure.ac @@ -12,6 +12,12 @@ AC_ARG_ENABLE(debug, [AC_HELP_STRING([--enable-debug], [compile with debugging s if test "x$enable_debug" = "xyes" ; then AC_DEFINE([DEBUG], [1], [Define if debugging is enabled]) + if test x$cflags_set != xset ; then + case " $CFLAGS " in + *[[\ \ ]]-g[[\ \ ]]*) ;; + *) CFLAGS="$CFLAGS -g" ;; + esac + fi fi AC_SUBST([SWE_GLIB_MAJOR_VERSION], [swe_glib_major_version]) @@ -37,6 +43,17 @@ GTK_DOC_CHECK([1.19], [--flavour no-tmpl]) GOBJECT_INTROSPECTION_CHECK([1.0]) PKG_PROG_PKG_CONFIG AC_PATH_PROG([GLIB_MKENUMS], [glib-mkenums]) +AC_CANONICAL_HOST +native_win32=no +case "$host" in + *-*-mingw*) + native_win32=yes + ;; +esac +AM_CONDITIONAL(OS_WIN32, [test "$native_win32" = "yes"]) +AM_CONDITIONAL(OS_UNIX, [test "$native_win32" != "yes"]) +AC_PATH_PROG([GTESTER], [gtester]) +AC_PATH_PROG([GTESTER_REPORT], [gtester-report]) PKG_CHECK_MODULES([GLIB], [glib-2.0]) PKG_CHECK_MODULES([GOBJECT], [gobject-2.0]) AC_CONFIG_MACRO_DIR([m4]) @@ -55,6 +72,7 @@ AC_CONFIG_FILES([ src/Makefile data/Makefile po/Makefile.in + tests/Makefile docs/reference/swe-glib/version.xml docs/reference/swe-glib/Makefile data/swe-glib.pc diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 0000000..bdba2af --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,192 @@ +# glib.mk - Borrowed from GLib +# initialize variables for unconditional += appending +BUILT_SOURCES = +BUILT_EXTRA_DIST = +CLEANFILES = *.log *.trs +DISTCLEANFILES = +MAINTAINERCLEANFILES = +EXTRA_DIST = +TEST_PROGS = + +noinst_LTLIBRARIES = +noinst_PROGRAMS = +noinst_SCRIPTS = +noinst_DATA = + +check_LTLIBRARIES = +check_PROGRAMS = +check_SCRIPTS = +check_DATA = + +TESTS = + +### testing rules + +# test: run all tests in cwd and subdirs +test: test-nonrecursive +if OS_UNIX + @ for subdir in $(SUBDIRS) . ; do \ + test "$$subdir" = "." -o "$$subdir" = "po" || \ + ( cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $@ ) || exit $? ; \ + done + +# test-nonrecursive: run tests only in cwd +test-nonrecursive: ${TEST_PROGS} + @test -z "${TEST_PROGS}" || G_TEST_SRCDIR="$(abs_srcdir)" G_TEST_BUILDDIR="$(abs_builddir)" G_DEBUG=gc-friendly MALLOC_CHECK_=2 MALLOC_PERTURB_=$$(($${RANDOM:-256} % 256)) ${GTESTER} --verbose ${TEST_PROGS} +else +test-nonrecursive: +endif + +# test-report: run tests in subdirs and generate report +# perf-report: run tests in subdirs with -m perf and generate report +# full-report: like test-report: with -m perf and -m slow +test-report perf-report full-report: ${TEST_PROGS} + @test -z "${TEST_PROGS}" || { \ + case $@ in \ + test-report) test_options="-k";; \ + perf-report) test_options="-k -m=perf";; \ + full-report) test_options="-k -m=perf -m=slow";; \ + esac ; \ + if test -z "$$GTESTER_LOGDIR" ; then \ + G_TEST_SRCDIR="$(abs_srcdir)" G_TEST_BUILDDIR="$(abs_builddir)" ${GTESTER} --verbose $$test_options -o test-report.xml ${TEST_PROGS} ; \ + elif test -n "${TEST_PROGS}" ; then \ + G_TEST_SRCDIR="$(abs_srcdir)" G_TEST_BUILDDIR="$(abs_builddir)" ${GTESTER} --verbose $$test_options -o `mktemp "$$GTESTER_LOGDIR/log-XXXXXX"` ${TEST_PROGS} ; \ + fi ; \ + } + @ ignore_logdir=true ; \ + if test -z "$$GTESTER_LOGDIR" ; then \ + GTESTER_LOGDIR=`mktemp -d "\`pwd\`/.testlogs-XXXXXX"`; export GTESTER_LOGDIR ; \ + ignore_logdir=false ; \ + fi ; \ + if test -d "$(top_srcdir)/.git" ; then \ + REVISION=`git describe` ; \ + else \ + REVISION=$(VERSION) ; \ + fi ; \ + for subdir in $(SUBDIRS) . ; do \ + test "$$subdir" = "." -o "$$subdir" = "po" || \ + ( cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $@ ) || exit $? ; \ + done ; \ + $$ignore_logdir || { \ + echo '' > $@.xml ; \ + echo '' >> $@.xml ; \ + echo '' >> $@.xml ; \ + echo ' $(PACKAGE)' >> $@.xml ; \ + echo ' $(VERSION)' >> $@.xml ; \ + echo " $$REVISION" >> $@.xml ; \ + echo '' >> $@.xml ; \ + for lf in `ls -L "$$GTESTER_LOGDIR"/.` ; do \ + sed '1,1s/^?]*?>//' <"$$GTESTER_LOGDIR"/"$$lf" >> $@.xml ; \ + done ; \ + echo >> $@.xml ; \ + echo '' >> $@.xml ; \ + rm -rf "$$GTESTER_LOGDIR"/ ; \ + ${GTESTER_REPORT} --version 2>/dev/null 1>&2 ; test "$$?" != 0 || ${GTESTER_REPORT} $@.xml >$@.html ; \ + } +.PHONY: test test-report perf-report full-report test-nonrecursive + +.PHONY: lcov genlcov lcov-clean +# use recursive makes in order to ignore errors during check +lcov: + -$(MAKE) $(AM_MAKEFLAGS) -k check + $(MAKE) $(AM_MAKEFLAGS) genlcov + +# we have to massage the lcov.info file slightly to hide the effect of libtool +# placing the objects files in the .libs/ directory separate from the *.c +# we also have to delete tests/.libs/libmoduletestplugin_*.gcda +genlcov: + rm -f $(top_builddir)/tests/.libs/libmoduletestplugin_*.gcda + $(LTP) --directory $(top_builddir) --capture --output-file glib-lcov.info --test-name GLIB_PERF --no-checksum --compat-libtool + LANG=C $(LTP_GENHTML) --prefix $(top_builddir) --output-directory glib-lcov --title "GLib Code Coverage" --legend --show-details glib-lcov.info + @echo "file://$(abs_top_builddir)/glib-lcov/index.html" + +lcov-clean: + -$(LTP) --directory $(top_builddir) -z + -rm -rf glib-lcov.info glib-lcov + -find -name '*.gcda' -print | xargs rm + +# run tests in cwd as part of make check +check-local: test-nonrecursive + +# We support a fairly large range of possible variables. It is expected that all types of files in a test suite +# will belong in exactly one of the following variables. +# +# First, we support the usual automake suffixes, but in lowercase, with the customary meaning: +# +# test_programs, test_scripts, test_data, test_ltlibraries +# +# The above are used to list files that are involved in both uninstalled and installed testing. The +# test_programs and test_scripts are taken to be actual testcases and will be run as part of the test suite. +# Note that _data is always used with the nobase_ automake variable name to ensure that installed test data is +# installed in the same way as it appears in the package layout. +# +# In order to mark a particular file as being only for one type of testing, use 'installed' or 'uninstalled', +# like so: +# +# installed_test_programs, uninstalled_test_programs +# installed_test_scripts, uninstalled_test_scripts +# installed_test_data, uninstalled_test_data +# installed_test_ltlibraries, uninstalled_test_ltlibraries +# +# Additionally, we support 'extra' infixes for programs and scripts. This is used for support programs/scripts +# that should not themselves be run as testcases (but exist to be used from other testcases): +# +# test_extra_programs, installed_test_extra_programs, uninstalled_test_extra_programs +# test_extra_scripts, installed_test_extra_scripts, uninstalled_test_extra_scripts +# +# Additionally, for _scripts and _data, we support the customary dist_ prefix so that the named script or data +# file automatically end up in the tarball. +# +# dist_test_scripts, dist_test_data, dist_test_extra_scripts +# dist_installed_test_scripts, dist_installed_test_data, dist_installed_test_extra_scripts +# dist_uninstalled_test_scripts, dist_uninstalled_test_data, dist_uninstalled_test_extra_scripts +# +# Note that no file is automatically disted unless it appears in one of the dist_ variables. This follows the +# standard automake convention of not disting programs scripts or data by default. +# +# test_programs, test_scripts, uninstalled_test_programs and uninstalled_test_scripts (as well as their disted +# variants) will be run as part of the in-tree 'make check'. These are all assumed to be runnable under +# gtester. That's a bit strange for scripts, but it's possible. + +# we use test -z "$(TEST_PROGS)" above, so make sure we have no extra whitespace... +TEST_PROGS += $(strip $(test_programs) $(test_scripts) $(uninstalled_test_programs) $(uninstalled_test_scripts) \ + $(dist_test_scripts) $(dist_uninstalled_test_scripts)) + +if OS_WIN32 +TESTS += $(test_programs) $(test_scripts) $(uninstalled_test_programs) $(uninstalled_test_scripts) \ + $(dist_test_scripts) $(dist_uninstalled_test_scripts) +endif + +# Note: build even the installed-only targets during 'make check' to ensure that they still work. +# We need to do a bit of trickery here and manage disting via EXTRA_DIST instead of using dist_ prefixes to +# prevent automake from mistreating gmake functions like $(wildcard ...) and $(addprefix ...) as if they were +# filenames, including removing duplicate instances of the opening part before the space, eg. '$(addprefix'. +all_test_programs = $(test_programs) $(uninstalled_test_programs) $(installed_test_programs) \ + $(test_extra_programs) $(uninstalled_test_extra_programs) $(installed_test_extra_programs) +all_test_scripts = $(test_scripts) $(uninstalled_test_scripts) $(installed_test_scripts) \ + $(test_extra_scripts) $(uninstalled_test_extra_scripts) $(installed_test_extra_scripts) +all_dist_test_scripts = $(dist_test_scripts) $(dist_uninstalled_test_scripts) $(dist_installed_test_scripts) \ + $(dist_test_extra_scripts) $(dist_uninstalled_test_extra_scripts) $(dist_installed_test_extra_scripts) +all_test_scripts += $(all_dist_test_scripts) +EXTRA_DIST += $(all_dist_test_scripts) +all_test_data = $(test_data) $(uninstalled_test_data) $(installed_test_data) +all_dist_test_data = $(dist_test_data) $(dist_uninstalled_test_data) $(dist_installed_test_data) +all_test_data += $(all_dist_test_data) +EXTRA_DIST += $(all_dist_test_data) +all_test_ltlibs = $(test_ltlibraries) $(uninstalled_test_ltlibraries) $(installed_test_ltlibraries) + +check_LTLIBRARIES += $(all_test_ltlibs) +check_PROGRAMS += $(all_test_programs) +check_SCRIPTS += $(all_test_scripts) +check_DATA += $(all_test_data) + +# End of glib.mk + +LDADD = $(top_builddir)/src/libswe-glib-2.0.la +DEFS = -DG_LOG_DOMAIN=\"SWE-GLib\" +AM_CPPFLAGS = $(GLIB_CFLAGS) $(GOBJECT_CFLAGS) $(LIBSWE_CFLAGS) -I$(top_srcdir)/src +AM_CFLAGS = -g + +test_programs = gswe-timestamp-test +TESTS += $(test_programs) + diff --git a/tests/gswe-timestamp-test.c b/tests/gswe-timestamp-test.c new file mode 100644 index 0000000..e84b171 --- /dev/null +++ b/tests/gswe-timestamp-test.c @@ -0,0 +1,41 @@ +#undef G_DISABLE_ASSERT + +#include +#include +#include + +static void +test_timestamp_jd(void) +{ + GsweTimestamp *timestamp; + gdouble jd; + + timestamp = gswe_timestamp_new_from_gregorian_full(1983, 3, 7, 11, 54, 45, 0, 1); + g_assert(timestamp); + + jd = gswe_timestamp_get_julian_day(timestamp, NULL); + g_assert(jd == 2445400.9553030556999146938323974609375); + + g_object_unref(timestamp); + + timestamp = gswe_timestamp_new_from_julian_day(jd); + g_assert(timestamp); + g_assert(gswe_timestamp_get_gregorian_year(timestamp, NULL) == 1983); + g_assert(gswe_timestamp_get_gregorian_month(timestamp, NULL) == 3); + g_assert(gswe_timestamp_get_gregorian_day(timestamp, NULL) == 7); + g_assert(gswe_timestamp_get_gregorian_hour(timestamp, NULL) == 11); + g_assert(gswe_timestamp_get_gregorian_minute(timestamp, NULL) == 54); + g_assert(gswe_timestamp_get_gregorian_second(timestamp, NULL) == 45); + g_assert(gswe_timestamp_get_gregorian_microsecond(timestamp, NULL) == 0); +} + +int +main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/gswe-timestamp/jd", test_timestamp_jd); + + return g_test_run(); +} +