Skip to content

Commit

Permalink
A macro to check that some constructs are ill-formed.
Browse files Browse the repository at this point in the history
  • Loading branch information
eggrobin committed Apr 1, 2024
1 parent 6f9dea6 commit f83d132
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
33 changes: 33 additions & 0 deletions testing_utilities/check_well_formedness.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include <type_traits>

#define PRINCIPIA_CONCATENATE(s1, s2) s1##s2
#define PRINCIPIA_CONCATENATE_EXPANSION(s1, s2) PRINCIPIA_CONCATENATE(s1, s2)

#define PRINCIPIA_CHECK_WELL_FORMEDNESS(unique_concept_name, \
expected_well_formedness, \
expected_well_formedness_description, \
expression, \
...) \
template<template<typename> typename WITH = std::type_identity_t> \
concept unique_concept_name = \
requires(__VA_ARGS__) { \
(expression); \
}; \
static_assert(expected_well_formedness unique_concept_name<>, \
"Expected\n " #expression \
"\n" expected_well_formedness_description)
#define PRINCIPIA_CHECK_ILL_FORMED(expression, ...) \
PRINCIPIA_CHECK_WELL_FORMEDNESS( \
PRINCIPIA_CONCATENATE_EXPANSION(test_concept_, __COUNTER__), \
!, \
"not to compile", \
expression, \
__VA_ARGS__)
#define PRINCIPIA_CHECK_WELL_FORMED(expression, ...) \
PRINCIPIA_CHECK_WELL_FORMEDNESS( \
PRINCIPIA_CONCATENATE_EXPANSION(test_concept_, __COUNTER__), \
, \
"to compile", \
expression, \
__VA_ARGS__)
8 changes: 8 additions & 0 deletions testing_utilities/check_well_formedness_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "testing_utilities/check_well_formedness.hpp"

#include <string>

PRINCIPIA_CHECK_ILL_FORMED(s + 3, WITH<std::string> s);
PRINCIPIA_CHECK_ILL_FORMED(s + "3", WITH<std::pair<double, double>> s);
PRINCIPIA_CHECK_WELL_FORMED(s + t, WITH<std::string> s, WITH<std::string> t);
PRINCIPIA_CHECK_ILL_FORMED(s - t, WITH<std::string> s, WITH<std::string> t);
2 changes: 2 additions & 0 deletions testing_utilities/testing_utilities.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ClInclude Include="almost_equals_body.hpp" />
<ClInclude Include="approximate_quantity.hpp" />
<ClInclude Include="approximate_quantity_body.hpp" />
<ClInclude Include="check_well_formedness.hpp" />
<ClInclude Include="componentwise.hpp" />
<ClInclude Include="componentwise_body.hpp" />
<ClInclude Include="discrete_trajectory_factories.hpp" />
Expand Down Expand Up @@ -46,6 +47,7 @@
<ClCompile Include="algebra_test.cpp" />
<ClCompile Include="almost_equals_test.cpp" />
<ClCompile Include="approximate_quantity_test.cpp" />
<ClCompile Include="check_well_formedness_test.cpp" />
<ClCompile Include="componentwise_test.cpp" />
<ClCompile Include="discrete_trajectory_factories_test.cpp" />
<ClCompile Include="is_near_test.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions testing_utilities/testing_utilities.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
<ClInclude Include="optimization_test_functions.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="check_well_formedness.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="almost_equals_test.cpp">
Expand Down Expand Up @@ -148,5 +151,8 @@
<ClCompile Include="optimization_test_functions_test.cpp">
<Filter>Test Files</Filter>
</ClCompile>
<ClCompile Include="check_well_formedness_test.cpp">
<Filter>Test Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

0 comments on commit f83d132

Please sign in to comment.