Skip to content

Commit 0308e0e

Browse files
committed
Simplify testing and feature testing infrastructure
1 parent 45da672 commit 0308e0e

14 files changed

Lines changed: 119 additions & 203 deletions

cmake/HPX_AddConfigTest.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,14 @@ function(hpx_check_for_cxx26_experimental_scope)
687687
)
688688
endfunction()
689689

690+
function(hpx_check_for_cxx26_contracts)
691+
add_hpx_config_test(
692+
HPX_WITH_CXX26_CONTRACTS
693+
SOURCE cmake/tests/cxx26_contracts.cpp
694+
FILE ${ARGN}
695+
)
696+
endfunction()
697+
690698
# ##############################################################################
691699
function(hpx_check_for_cxx_lambda_capture_decltype)
692700
add_hpx_config_test(

cmake/HPX_PerformCxxFeatureTests.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ function(hpx_perform_cxx_feature_tests)
173173
DEFINITIONS HPX_HAVE_CXX26_EXPERIMENTAL_SCOPE
174174
)
175175

176+
hpx_check_for_cxx26_contracts(DEFINITIONS HPX_HAVE_CXX26_CONTRACTS)
177+
176178
hpx_check_for_cxx_lambda_capture_decltype(
177179
DEFINITIONS HPX_HAVE_CXX_LAMBDA_CAPTURE_DECLTYPE
178180
)

cmake/tests/cxx26_contracts.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2025 Hartmut Kaiser
2+
//
3+
// SPDX-License-Identifier: BSL-1.0
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
7+
// test for availability of C++26 contracts
8+
9+
#if !defined(__cpp_contracts)
10+
#error "__cpp_contracts not defined, assume contracts are not supported"
11+
#endif
12+
13+
#include <contracts>
14+
15+
// Test actual contract syntax support (for experimental implementations)
16+
int main() pre(true) post(r : r == 0)
17+
{
18+
contract_assert(true);
19+
return 0;
20+
}

libs/core/contracts/CMakeLists.txt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,27 @@
77

88
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
99

10+
if(HPX_WITH_CXX26_CONTRACTS)
11+
# Map existing HPX_ASSERT to contract_assert
12+
hpx_option(
13+
HPX_CONTRACTS_WITH_ASSERTS_AS_CONTRACT_ASSERTS BOOL
14+
"Map existing HPX_ASERT to contract_assert. (default: ON)" ON ADVANCED
15+
CATEGORY "Modules"
16+
MODULE CONTRACTS
17+
)
18+
19+
if(HPX_CONTRACTS_WITH_ASSERTS_AS_CONTRACT_ASSERTS)
20+
hpx_add_config_define_namespace(
21+
DEFINE HPX_CONTRACTS_HAVE_ASSERTS_AS_CONTRACT_ASSERTS NAMESPACE CONTRACTS
22+
)
23+
endif()
24+
endif()
25+
1026
# Default location is $HPX_ROOT/libs/contracts/include
1127
set(contracts_headers hpx/contracts.hpp hpx/modules/contracts.hpp)
1228

13-
# Default location is $HPX_ROOT/libs/contracts/include_compatibility
14-
# cmake-format: off
15-
set(contracts_compat_headers
16-
# Add any legacy header mappings here if needed in the future
17-
# hpx/old_contracts.hpp => hpx/contracts.hpp
18-
)
19-
# cmake-format: on
20-
2129
# Default location is $HPX_ROOT/libs/contracts/src
22-
set(contracts_sources # Add source files here when implementation is added
23-
# contracts.cpp
24-
)
30+
set(contracts_sources)
2531

2632
include(HPX_AddModule)
2733
add_hpx_module(

libs/core/contracts/docs/index.rst

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,31 @@ automatically adapt their behavior based on compiler capabilities:
2323

2424
* When C++26 native contracts are available (``__cpp_contracts`` defined),
2525
they map to standard contract syntax
26-
* When contracts are not available but ``HPX_WITH_CONTRACTS=ON``, preconditions
27-
and postconditions become no-ops while contract assertions fall back to
28-
:c:macro:`HPX_ASSERT` for compatibility
29-
* When ``HPX_WITH_CONTRACTS=OFF``, preconditions and postconditions become
26+
* When ``HPX_WITH_CX26_CONTRACTS=OFF``, preconditions and postconditions become
3027
no-ops while contract assertions remain available as enhanced assertions
3128

3229
Configuration
3330
=============
3431

3532
Enable contracts in CMake::
3633

37-
cmake -DHPX_WITH_CONTRACTS=ON -DCMAKE_CXX_STANDARD=26
34+
cmake -DHPX_WITH_CXX26_CONTRACTS=ON -DCMAKE_CXX_STANDARD=26
3835

3936
Enable contract-enhanced assertions (optional)::
4037

41-
cmake -DHPX_WITH_CONTRACTS=ON -DHPX_WITH_ASSERTS_AS_CONTRACT_ASSERTS=ON
38+
cmake -DHPX_WITH_CXX26_CONTRACTS=ON -DHPX_CONTRACTS_WITH_ASSERTS_AS_CONTRACT_ASSERTS=ON
4239

4340
Contract assertions work even when contracts are disabled::
4441

45-
cmake -DHPX_WITH_CONTRACTS=OFF # HPX_CONTRACT_ASSERT still maps to HPX_ASSERT
42+
cmake -DHPX_WITH_CXX26_CONTRACTS=OFF # HPX_CONTRACT_ASSERT still maps to HPX_ASSERT
4643

4744
Advanced Features
4845
=================
4946

5047
Contract-Enhanced Assertions
5148
----------------------------
5249

53-
When ``HPX_WITH_ASSERTS_AS_CONTRACT_ASSERTS=ON`` is enabled, regular
50+
When ``HPX_CONTRACTS_WITH_ASSERTS_AS_CONTRACT_ASSERTS=ON`` is enabled, regular
5451
:c:macro:`HPX_ASSERT` calls are automatically upgraded to use contract
5552
assertions in C++26 mode::
5653

@@ -63,17 +60,16 @@ assertions in C++26 mode::
6360
.. warning::
6461
Enhanced assertions only provide benefits when native C++26 contracts are
6562
supported by the compiler. Without native contract support,
66-
``HPX_ASSERT`` ``HPX_CONTRACT_ASSERT`` ``HPX_ASSERT`` (no enhancement).
63+
``HPX_ASSERT`` -> ``HPX_CONTRACT_ASSERT`` -> ``HPX_ASSERT`` (no enhancement).
6764
CMake will issue a warning if you enable this option without native contract support.
6865

6966
This provides enhanced contract semantics throughout your existing codebase
7067
without requiring changes to assertion code. The transformation occurs in the
7168
contracts module (``contracts.hpp``) where the ``HPX_ASSERT`` macro is
7269
overridden to use ``HPX_CONTRACT_ASSERT`` when ``HPX_WITH_ASSERTS_AS_CONTRACT_ASSERTS=ON``:
7370

74-
* ``HPX_WITH_CONTRACTS=ON`` - Contracts module is enabled
75-
* ``HPX_WITH_ASSERTS_AS_CONTRACT_ASSERTS=ON`` - Assertion enhancement is enabled
76-
* ``HPX_HAVE_NATIVE_CONTRACTS=ON`` - C++26 native contracts are available (from config test)
71+
* ``HPX_WITH_CXX26_CONTRACTS=ON`` - Contracts module is enabled
72+
* ``HPX_CONTRACTS_WITH_ASSERTS_AS_CONTRACT_ASSERTS=ON`` - Assertion enhancement is enabled
7773

7874
The implementation works by redefining ``HPX_ASSERT`` in ``contracts.hpp`` to
7975
use ``HPX_CONTRACT_ASSERT``, which automatically adapts to the current contract
@@ -155,4 +151,4 @@ detection. Tests are organized into three categories:
155151
The test suite automatically detects compiler capabilities at configure time
156152
and builds only the appropriate tests for the current configuration.
157153

158-
See the :ref:`API reference <modules_contracts_api>` of the module for more details.
154+
See the :ref:`API reference <modules_contracts_api>` of the module for more details.

libs/core/contracts/include/hpx/modules/contracts.hpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,24 @@
2929

3030
// Contract implementation: automatically selects native C++26 contracts
3131
// or provides appropriate fallback behavior based on compiler capabilities
32-
#ifdef HPX_HAVE_CONTRACTS
33-
#if HPX_HAVE_NATIVE_CONTRACTS
32+
#if defined(HPX_HAVE_CXX26_CONTRACTS)
33+
3434
// Native C++26 contracts mode
3535
#define HPX_PRE(x) pre(x)
3636
#define HPX_CONTRACT_ASSERT(x) contract_assert(x)
3737
#define HPX_POST(x) post(x)
38+
39+
#if defined(HPX_CONTRACTS_HAVE_ASSERTS_AS_CONTRACT_ASSERTS)
40+
// Override HPX_ASSERT to use contract assertions
41+
#define HPX_ASSERT(x) contract_assert(x)
42+
#endif
43+
3844
#else
45+
3946
// Fallback mode: PRE/POST become no-ops for forward compatibility,
4047
// CONTRACT_ASSERT maps to HPX_ASSERT for runtime validation
41-
#pragma message( \
42-
"HPX Contracts: Using assertion fallback mode. " \
43-
"HPX_PRE/HPX_POST are no-ops, HPX_CONTRACT_ASSERT maps to HPX_ASSERT.")
4448
#define HPX_PRE(x)
4549
#define HPX_CONTRACT_ASSERT(x) HPX_ASSERT((x))
4650
#define HPX_POST(x)
47-
#endif
48-
#else
49-
// Contracts disabled: PRE/POST are no-ops, CONTRACT_ASSERT remains available
50-
#define HPX_PRE(x)
51-
#define HPX_CONTRACT_ASSERT(x) HPX_ASSERT((x))
52-
#define HPX_POST(x)
53-
#endif
5451

55-
#ifdef HPX_HAVE_ASSERTS_AS_CONTRACT_ASSERTS
56-
// Override HPX_ASSERT to use contract assertions
57-
#define HPX_ASSERT(x) HPX_CONTRACT_ASSERT(x)
5852
#endif

libs/core/contracts/tests/performance/CMakeLists.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,3 @@
55
# SPDX-License-Identifier: BSL-1.0
66
# Distributed under the Boost Software License, Version 1.0. (See accompanying
77
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8-
9-
# Currently no performance tests for contracts module Performance tests will be
10-
# added to measure contract overhead
11-
12-
if(HPX_WITH_TESTS)
13-
# Future: Add contract performance benchmarks here These would measure the
14-
# overhead of contract checking add_hpx_unit_test("contracts"
15-
# contract_performance_test SOURCES contract_performance_test.cpp )
16-
endif()

libs/core/contracts/tests/regressions/CMakeLists.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,3 @@
55
# SPDX-License-Identifier: BSL-1.0
66
# Distributed under the Boost Software License, Version 1.0. (See accompanying
77
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8-
9-
# Currently no regression tests for contracts module Regression tests will be
10-
# added as the contract system matures
11-
12-
if(HPX_WITH_TESTS)
13-
# Future: Add contract regression tests here These would test specific bug
14-
# fixes and edge cases add_hpx_unit_test("contracts" contract_regression_test1
15-
# SOURCES contract_regression_test1.cpp )
16-
endif()

0 commit comments

Comments
 (0)