From c9122ff84bc55cfdb8719520a9ecc08e68804dde Mon Sep 17 00:00:00 2001 From: Edward d'Auvergne <edward@nmr-relax.com> Date: Thu, 18 Feb 2016 16:08:20 +0100 Subject: [PATCH] TestSuite: Converted the old tests/test-mktime.cxx program into a unit test. This required many time related include files, functions, and variables to be tested for via CMake, and converted to macros via #cmakedefine, so that they would be automatically set for the test-mktime.cxx file. --- CMakeLists.txt | 12 +++- src/Include/config_cmake.h.in | 7 ++ test_suite/unit_tests/CMakeLists.txt | 1 + test_suite/unit_tests/general/CMakeLists.txt | 12 ++++ test_suite/unit_tests/general/TestSuite.cxx | 24 +++++++ .../unit_tests/general}/test-mktime.cxx | 66 ++++++++++++------- test_suite/unit_tests/general/test-mktime.hxx | 48 ++++++++++++++ 7 files changed, 142 insertions(+), 28 deletions(-) create mode 100644 test_suite/unit_tests/general/CMakeLists.txt create mode 100644 test_suite/unit_tests/general/TestSuite.cxx rename {tests => test_suite/unit_tests/general}/test-mktime.cxx (63%) create mode 100644 test_suite/unit_tests/general/test-mktime.hxx diff --git a/CMakeLists.txt b/CMakeLists.txt index de5cd25bd..372fbc59c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required (VERSION 3.0) include (CheckFunctionExists) +include (CheckVariableExists) include (CheckCSourceCompiles) include (CheckCXXSourceCompiles) include (CheckIncludeFile) @@ -396,9 +397,14 @@ find_package(SimGear ${MIN_SIMGEAR_VERSION} CONFIG REQUIRED) ############################################################################## -check_include_file(unistd.h HAVE_UNISTD_H) -check_include_file(sys/time.h HAVE_SYS_TIME_H) -check_include_file(windows.h HAVE_WINDOWS_H) +check_include_file(unistd.h HAVE_UNISTD_H) +check_include_file(sys/time.h HAVE_SYS_TIME_H) +check_include_file(sys/timeb.h HAVE_SYS_TIMEB_H) +check_include_file(windows.h HAVE_WINDOWS_H) +check_function_exists(timegm HAVE_TIMEGM) +check_variable_exists(daylight HAVE_DAYLIGHT) +check_function_exists(ftime HAVE_FTIME) +check_function_exists(gettimeofday HAVE_GETTIMEOFDAY) if(ENABLE_PROFILE) find_package(GooglePerfTools REQUIRED) diff --git a/src/Include/config_cmake.h.in b/src/Include/config_cmake.h.in index 514aa6f3c..1a2e172ea 100644 --- a/src/Include/config_cmake.h.in +++ b/src/Include/config_cmake.h.in @@ -54,6 +54,13 @@ #cmakedefine HAVE_QT +#cmakedefine HAVE_SYS_TIME_H +#cmakedefine HAVE_SYS_TIMEB_H +#cmakedefine HAVE_TIMEGM +#cmakedefine HAVE_DAYLIGHT +#cmakedefine HAVE_FTIME +#cmakedefine HAVE_GETTIMEOFDAY + #define FG_BUILD_TYPE "@FG_BUILD_TYPE@" #define HAVE_PUI diff --git a/test_suite/unit_tests/CMakeLists.txt b/test_suite/unit_tests/CMakeLists.txt index 3cdcb6598..8bd7e8e6b 100644 --- a/test_suite/unit_tests/CMakeLists.txt +++ b/test_suite/unit_tests/CMakeLists.txt @@ -1,5 +1,6 @@ # Add each unit test category. foreach( unit_test_category + general Scripting ) diff --git a/test_suite/unit_tests/general/CMakeLists.txt b/test_suite/unit_tests/general/CMakeLists.txt new file mode 100644 index 000000000..ed4e8f9ed --- /dev/null +++ b/test_suite/unit_tests/general/CMakeLists.txt @@ -0,0 +1,12 @@ +set(TESTSUITE_SOURCES + ${TESTSUITE_SOURCES} + ${CMAKE_CURRENT_SOURCE_DIR}/TestSuite.cxx + ${CMAKE_CURRENT_SOURCE_DIR}/test-mktime.cxx + PARENT_SCOPE +) + +set(TESTSUITE_HEADERS + ${TESTSUITE_HEADERS} + ${CMAKE_CURRENT_SOURCE_DIR}/test-mktime.hxx + PARENT_SCOPE +) diff --git a/test_suite/unit_tests/general/TestSuite.cxx b/test_suite/unit_tests/general/TestSuite.cxx new file mode 100644 index 000000000..ef06d3aa4 --- /dev/null +++ b/test_suite/unit_tests/general/TestSuite.cxx @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2016 Edward d'Auvergne + * + * This file is part of the program FlightGear. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "test-mktime.hxx" + + +// Set up the unit tests. +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(MktimeTests, "Unit tests"); diff --git a/tests/test-mktime.cxx b/test_suite/unit_tests/general/test-mktime.cxx similarity index 63% rename from tests/test-mktime.cxx rename to test_suite/unit_tests/general/test-mktime.cxx index 037e94e5e..5a630cd2a 100644 --- a/tests/test-mktime.cxx +++ b/test_suite/unit_tests/general/test-mktime.cxx @@ -1,10 +1,13 @@ // test the systems mktime() function - #ifdef HAVE_CONFIG_H # include <config.h> #endif +#ifdef HAVE_WINDOWS_H +# include <windows.h> +#endif + #include <math.h> #include <stdio.h> #include <time.h> @@ -20,6 +23,8 @@ # include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval #endif +#include "test-mktime.hxx" + #define LST_MAGIC_TIME_1998 890481600 // For now we assume that if daylight is not defined in @@ -43,6 +48,14 @@ long int fix_up_timezone( long int timezone_orig ) { } +// A printout function suitable for the test suite to replace printf(). +int printout(const char *text, ...) +{ + std::cout << text; + return 0; +} + + // Return time_t for Sat Mar 21 12:00:00 GMT // // I believe the mktime() has a SYSV vs. BSD behavior difference. @@ -87,14 +100,14 @@ time_t get_start_gmt(int year) { time_t start = mktime(&mt); - printf("start1 = %ld\n", start); - printf("start2 = %s", ctime(&start)); - printf("(tm_isdst = %d)\n", mt.tm_isdst); + printout("start1 = %ld\n", start); + printout("start2 = %s", ctime(&start)); + printout("(tm_isdst = %d)\n", mt.tm_isdst); TIMEZONE = fix_up_timezone( TIMEZONE ); # if defined( TIMEZONE_OFFSET_WORKS ) - printf("start = %ld, timezone = %ld\n", start, TIMEZONE); + printout("start = %ld, timezone = %ld\n", start, TIMEZONE); return( start - TIMEZONE ); # else // ! defined( TIMEZONE_OFFSET_WORKS ) @@ -102,18 +115,18 @@ time_t get_start_gmt(int year) { if ( daylight > 0 ) { daylight = 1; } else if ( daylight < 0 ) { - printf("OOOPS, problem in fg_time.cxx, no daylight savings info.\n"); + printout("OOOPS, problem in fg_time.cxx, no daylight savings info.\n"); } long int offset = -(TIMEZONE / 3600 - daylight); - printf(" Raw time zone offset = %ld\n", TIMEZONE); - printf(" Daylight Savings = %d\n", daylight); - printf(" Local hours from GMT = %ld\n", offset); + printout(" Raw time zone offset = %ld\n", TIMEZONE); + printout(" Daylight Savings = %d\n", daylight); + printout(" Local hours from GMT = %ld\n", offset); long int start_gmt = start - TIMEZONE + (daylight * 3600); - printf(" March 21 noon (CST) = %ld\n", start); + printout(" March 21 noon (CST) = %ld\n", start); return ( start_gmt ); # endif // ! defined( TIMEZONE_OFFSET_WORKS ) @@ -121,31 +134,34 @@ time_t get_start_gmt(int year) { } -int main() { +// The original test-mktime test. +void MktimeTests::testMktime() +{ time_t start_gmt; start_gmt = get_start_gmt(98); if ( start_gmt == LST_MAGIC_TIME_1998 ) { - printf("Time test = PASSED\n\n"); + printout("Time test = PASSED\n\n"); #ifdef HAVE_TIMEGM - printf("You have timegm() which is just like mktime() except that\n"); - printf("it explicitely expects input in GMT ... lucky you!\n"); + printout("You have timegm() which is just like mktime() except that\n"); + printout("it explicitely expects input in GMT ... lucky you!\n"); #elif MK_TIME_IS_GMT - printf("You don't seem to have timegm(), but mktime() seems to\n"); - printf("assume input is GMT on your system ... I guess that works\n"); + printout("You don't seem to have timegm(), but mktime() seems to\n"); + printout("assume input is GMT on your system ... I guess that works\n"); #else - printf("mktime() assumes local time zone on your system, but we can\n"); - printf("compensate just fine.\n"); + printout("mktime() assumes local time zone on your system, but we can\n"); + printout("compensate just fine.\n"); #endif } else { - printf("Time test = FAILED\n\n"); - printf("There is likely a problem with mktime() on your system.\n"); - printf("This will cause the sun/moon/stars/planets to be in the\n"); - printf("wrong place in the sky and the rendered time of day will be\n"); - printf("incorrect.\n\n"); - printf("Please report this to http://www.flightgear.org/~curt so we can work to fix\n"); - printf("the problem on your platform.\n"); + printout("Time test = FAILED\n\n"); + printout("There is likely a problem with mktime() on your system.\n"); + printout("This will cause the sun/moon/stars/planets to be in the\n"); + printout("wrong place in the sky and the rendered time of day will be\n"); + printout("incorrect.\n\n"); + printout("Please report this to flightgear-devel@lists.sourceforge.net so we can work to fix\n"); + printout("the problem on your platform.\n"); } + CPPUNIT_ASSERT_EQUAL(LST_MAGIC_TIME_1998, (int)start_gmt); } diff --git a/test_suite/unit_tests/general/test-mktime.hxx b/test_suite/unit_tests/general/test-mktime.hxx new file mode 100644 index 000000000..d6181764e --- /dev/null +++ b/test_suite/unit_tests/general/test-mktime.hxx @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2016 Edward d'Auvergne + * + * This file is part of the program FlightGear. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +#ifndef _FG_MKTIME_UNIT_TESTS_HXX +#define _FG_MKTIME_UNIT_TESTS_HXX + + +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/TestFixture.h> + + +// The single mktime unit test. +class MktimeTests : public CppUnit::TestFixture +{ + // Set up the test suite. + CPPUNIT_TEST_SUITE(MktimeTests); + CPPUNIT_TEST(testMktime); + CPPUNIT_TEST_SUITE_END(); + +public: + // Set up function for each test. + void setUp() {} + + // Clean up after each test. + void tearDown() {} + + // The original test-mktime test. + void testMktime(); +}; + +#endif // _FG_MKTIME_UNIT_TESTS_HXX