Project

General

Profile

Feature #2840

Test that functionality does not compile that is not supposed to.

Added by Christian Blau 27 days ago. Updated 27 days ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
-
Target version:
Difficulty:
uncategorized
Close

Description

Classes/templates yield essential compiler errors to prevent misuse.

However testing that code changes do not unintentially allow code to compile that was explicitly forbidden from it requires testing the compilation for which we don't have functionality at the moment.

The following patch has a solution for this from Roland Schulz that eneded up not being used, [[https://gerrit.gromacs.org/#/c/8192/14/src/gromacs/gpu_utils/tests/CMakeLists.txt]]

function(gmx_compile_test)
   if(NOT GMX_DEVELOPER_BUILD)
      return() #Users might run tests on a different machine
   endif()
   set(NAME ${ARGV0})
   set(SOURCE ${ARGV1})
   set(ERRORMSG ${ARGV2})
   add_library(${NAME} OBJECT EXCLUDE_FROM_ALL ${SOURCE})
   add_test(NAME ${NAME} COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target ${NAME})
   set_tests_properties(${NAME} PROPERTIES PASS_REGULAR_EXPRESSION ${ERRORMSG})
endfunction()

gmx_compile_test(HostAllocatorCompileTest "hostallocator_compile.cpp" 
   "This allocator policy doesn't support copy construction")

History

#1 Updated by Mark Abraham 27 days ago

There are a few other approaches out there.

Those at https://stackoverflow.com/questions/30155619/expected-build-failure-tests-in-cmake leverage that ctest can be taught to understand that a test would fail, but it seems to require making subdirectories with source files, which would get messy for using in multiple places.

That of https://petriconi.net/?p=118 looks good. We can have have the helper types in a common header in src/testutils, and hopefully have test code that looks like

#include "foo.h" 

#include "testutils header.h" 

// The specialization for int shall be forbidden
template <>
struct Foo<int> : public disable_usage<Foo>
{
};

EXPECT_TRUE(is_disabled_from_usage<Foo<int>>::value);

#2 Updated by Mark Abraham 27 days ago

  • Description updated (diff)

Also available in: Atom PDF