Skip to content

Commit

Permalink
Common header-only include path for test modules
Browse files Browse the repository at this point in the history
  • Loading branch information
tkittel committed Oct 1, 2024
1 parent a0d9006 commit 70d1a54
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 105 deletions.
11 changes: 8 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,18 @@ else()
"${CMAKE_CURRENT_LIST_DIR}/scripts" "${nctest_pyenvmod}"
)
endif()
set( nctests_extra_inc_dirs PUBLIC "${CMAKE_CURRENT_LIST_DIR}/include" )
mctools_testutils_add_test_libs(
"${CMAKE_CURRENT_LIST_DIR}/libs" "${nctests_ncrystallib_target}"
"${CMAKE_CURRENT_LIST_DIR}/libs"
"${nctests_ncrystallib_target}"
"${nctests_extra_inc_dirs}"
)
mctools_testutils_add_test_modules(
"${CMAKE_CURRENT_LIST_DIR}/modules" "${nctests_ncrystallib_target}"
"${CMAKE_CURRENT_LIST_DIR}/modules"
"${nctests_ncrystallib_target}"
"${nctests_extra_inc_dirs}"
)


set( nctest_appenvmod "" )
if ( WIN32 )
if ( NCTESTS_STANDALONE )
Expand Down Expand Up @@ -134,6 +138,7 @@ list(
mctools_testutils_add_tests_apps(
"${CMAKE_CURRENT_LIST_DIR}/src"
"${nctests_ncrystallib_target}"
"${nctests_extra_inc_dirs}"
"${nctest_appenvmod}"
)
#FIXME: TODO Need python unit test which launches compiled app!
30 changes: 24 additions & 6 deletions tests/cmake/mctools_testutils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,28 @@ function( mctools_testutils_internal_missingpydeps resvar pydeps )
set( "${resvar}" "${missing}" PARENT_SCOPE )
endfunction()

function( mctools_testutils_add_test_modules librootdir extra_link_libs )
mctools_testutils_internal_add_test_libs( "${librootdir}" "${extra_link_libs}" "ON" )
function(
mctools_testutils_add_test_modules
librootdir extra_link_libs extra_inc_dirs
)
mctools_testutils_internal_add_test_libs(
"${librootdir}" "${extra_link_libs}" "${extra_inc_dirs}" "ON"
)
endfunction()

function( mctools_testutils_add_test_libs librootdir extra_link_libs )
mctools_testutils_internal_add_test_libs( "${librootdir}" "${extra_link_libs}" "OFF" )
function(
mctools_testutils_add_test_libs
librootdir extra_link_libs extra_inc_dirs
)
mctools_testutils_internal_add_test_libs(
"${librootdir}" "${extra_link_libs}" "${extra_inc_dirs}" "OFF"
)
endfunction()

function( mctools_testutils_internal_add_test_libs librootdir extra_link_libs is_module )
function(
mctools_testutils_internal_add_test_libs
librootdir extra_link_libs extra_inc_dirs is_module
)
file(
GLOB libdirs
LIST_DIRECTORIES true
Expand Down Expand Up @@ -195,6 +208,7 @@ function( mctools_testutils_internal_add_test_libs librootdir extra_link_libs is
endif()

target_link_libraries( ${name} PRIVATE ${extra_link_libs} )
target_include_directories( ${name} ${extra_inc_dirs} )

if ( EXISTS "${libdir}/include" )
if ( is_module )
Expand Down Expand Up @@ -222,7 +236,10 @@ function( mctools_testutils_internal_add_test_libs librootdir extra_link_libs is
endforeach()
endfunction()

function( mctools_testutils_add_tests_apps approotdir extra_link_libs envmod )
function(
mctools_testutils_add_tests_apps
approotdir extra_link_libs extra_inc_dirs envmod
)
set( testsbindir "${PROJECT_BINARY_DIR}/mctools_tests_bin" )
file( MAKE_DIRECTORY "${testsbindir}" )
file(
Expand All @@ -241,6 +258,7 @@ function( mctools_testutils_add_tests_apps approotdir extra_link_libs envmod )
target_link_libraries( ${bn} PRIVATE "TestLib_${dep}" )
endforeach()
target_link_libraries( ${bn} PRIVATE ${extra_link_libs} )
target_include_directories( ${bn} ${extra_inc_dirs} )

set( reflog "${appdir}/test.log" )
if ( EXISTS "${reflog}" )
Expand Down
63 changes: 63 additions & 0 deletions tests/include/NCTestUtils/NCTestModUtils.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef NCrystal_TestModUtils_hh
#define NCrystal_TestModUtils_hh

////////////////////////////////////////////////////////////////////////////////
// //
// This file is part of NCrystal (see https://mctools.github.io/ncrystal/) //
// //
// Copyright 2015-2024 NCrystal developers //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
// You may obtain a copy of the License at //
// //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
// Unless required by applicable law or agreed to in writing, software //
// distributed under the License is distributed on an "AS IS" BASIS, //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and //
// limitations under the License. //
// //
////////////////////////////////////////////////////////////////////////////////

// Macros and inline functions for implementing compiled test modules (to be
// loaded with the Lib class from loadlib.py.

#if defined (_WIN32) || defined (__CYGWIN__) || defined (WIN32)
# define NCTEST_API __declspec(dllexport)
#elif defined(__GNUC__) || defined(__clang__)
# define NCTEST_API __attribute__ ((visibility ("default")))
#else
# define NCTEST_API
#endif
#define NCTEST_CTYPES extern "C" NCTEST_API

#include <iostream>
#include <cstdlib>
#include <stdexcept>

namespace nctest {
namespace {
inline void printErrAndExit(const std::exception &e) noexcept(true) {
const std::runtime_error* stdrte = dynamic_cast<const std::runtime_error*>(&e);
if (stdrte)
std::cout<<"NCTest ERROR (std::runtime_error): "<<stdrte->what()<<std::endl;
else
std::cout<<"NCTest ERROR (unknown)"<<std::endl;
//std::exit(1); FIXME: What to do here? Perhaps revisit how we handle the
//exceptions in NCrystal's own chooks.
}
}
}
#define NCCATCH catch (std::exception& e) { nctest::printErrAndExit(e); }

#define NCTEST_CTYPE_DICTIONARY NCTEST_CTYPES const char * nctest_ctypes_dictionary()

#ifndef NCTESTMODUTILS_NO_NCRYSTAL_INCLUDE
# include "NCrystal/NCDefs.hh"
namespace NC = NCrystal;
#endif


#endif
31 changes: 2 additions & 29 deletions tests/modules/lib_fpe/fpe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,9 @@
// //
////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////
//FIXME: To common test header:
#if defined (_WIN32) || defined (__CYGWIN__) || defined (WIN32)
# define NCTEST_API __declspec(dllexport)
#elif defined(__GNUC__) || defined(__clang__)
# define NCTEST_API __attribute__ ((visibility ("default")))
#else
# define NCTEST_API
#endif
#define NCTEST_CTYPES extern "C" NCTEST_API

#include <iostream>
#include <cstdlib>
#include <stdexcept>

namespace nctest {
inline void printErrAndExit(const std::exception &e) noexcept(true) {
const std::runtime_error* stdrte = dynamic_cast<const std::runtime_error*>(&e);
if (stdrte)
std::cout<<"NCTest ERROR (std::runtime_error): "<<stdrte->what()<<std::endl;
else
std::cout<<"NCTest ERROR (unknown)"<<std::endl;
//std::exit(1);
}
}
#define NCCATCH catch (std::exception& e) { nctest::printErrAndExit(e); }
//////////////////////////////////////////////////////

#include "NCTestUtils/NCTestModUtils.hh"

NCTEST_CTYPES const char * nctest_ctypes_dictionary()
NCTEST_CTYPE_DICTIONARY
{
return "void nctest_catch_fpe()";
}
Expand Down
35 changes: 2 additions & 33 deletions tests/modules/lib_misc/lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,9 @@
// //
////////////////////////////////////////////////////////////////////////////////

#include "NCrystal/NCDefs.hh"
#include "NCTestUtils/NCTestModUtils.hh"

namespace NC = NCrystal;

//////////////////////////////////////////////////////
//FIXME: To common test header:
#if defined (_WIN32) || defined (__CYGWIN__) || defined (WIN32)
# define NCTEST_API __declspec(dllexport)
#elif defined(__GNUC__) || defined(__clang__)
# define NCTEST_API __attribute__ ((visibility ("default")))
#else
# define NCTEST_API
#endif
#define NCTEST_CTYPES extern "C" NCTEST_API

#include <iostream>
#include <cstdlib>
#include <stdexcept>

namespace nctest {
inline void printErrAndExit(const std::exception &e) noexcept(true) {
const std::runtime_error* stdrte = dynamic_cast<const std::runtime_error*>(&e);
if (stdrte)
std::cout<<"NCTest ERROR (std::runtime_error): "<<stdrte->what()<<std::endl;
else
std::cout<<"NCTest ERROR (unknown)"<<std::endl;
std::exit(1);
}
}
#define NCCATCH catch (std::exception& e) { nctest::printErrAndExit(e); }
//////////////////////////////////////////////////////


NCTEST_CTYPES const char * nctest_ctypes_dictionary()
NCTEST_CTYPE_DICTIONARY
{
return
"double nctest_divide_args( double, double );"
Expand Down
37 changes: 3 additions & 34 deletions tests/modules/lib_testfileutils/lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,11 @@
// //
////////////////////////////////////////////////////////////////////////////////

#include "NCrystal/NCDefs.hh"
#include "NCTestUtils/NCTestModUtils.hh"
#include "NCrystal/internal/NCFileUtils.hh"
#include <cstring>

namespace NC = NCrystal;

//////////////////////////////////////////////////////
//FIXME: To common test header:
#if defined (_WIN32) || defined (__CYGWIN__) || defined (WIN32)
# define NCTEST_API __declspec(dllexport)
#elif defined(__GNUC__) || defined(__clang__)
# define NCTEST_API __attribute__ ((visibility ("default")))
#else
# define NCTEST_API
#endif
#define NCTEST_CTYPES extern "C" NCTEST_API

#include <iostream>
#include <cstdlib>
#include <stdexcept>

namespace nctest {
inline void printErrAndExit(const std::exception &e) noexcept(true) {
const std::runtime_error* stdrte = dynamic_cast<const std::runtime_error*>(&e);
if (stdrte)
std::cout<<"NCTest ERROR (std::runtime_error): "<<stdrte->what()<<std::endl;
else
std::cout<<"NCTest ERROR (unknown)"<<std::endl;
std::exit(1);
}
}
#define NCCATCH catch (std::exception& e) { nctest::printErrAndExit(e); }
//////////////////////////////////////////////////////


NCTEST_CTYPES const char * nctest_ctypes_dictionary()
NCTEST_CTYPE_DICTIONARY
{
return
"int nctest_file_exists( const char * );"
Expand All @@ -64,7 +33,7 @@ NCTEST_CTYPES const char * nctest_ctypes_dictionary()

NCTEST_CTYPES int nctest_file_exists( const char * f )
{
int res;
int res = -1;
try {
nc_assert_always( f );
res = NC::file_exists( f ) ? 1 : 0;
Expand Down

0 comments on commit 70d1a54

Please sign in to comment.