mirror of
https://github.com/BlackMATov/promise.hpp.git
synced 2025-12-13 19:57:58 +07:00
new build scripts
This commit is contained in:
@@ -1,19 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
set -e
|
|
||||||
DIR="$( cd "$( dirname "${BASH_SOURCE}" )" && pwd )"
|
|
||||||
|
|
||||||
ROOT_DIR="${DIR}/.."
|
|
||||||
BUILD_DIR="${ROOT_DIR}/build/coverage"
|
|
||||||
|
|
||||||
mkdir -p "${BUILD_DIR}"
|
|
||||||
(cd "${BUILD_DIR}" && cmake "${ROOT_DIR}" -DCMAKE_BUILD_TYPE=Debug -DBUILD_WITH_COVERAGE=ON)
|
|
||||||
(cd "${BUILD_DIR}" && cmake --build .)
|
|
||||||
|
|
||||||
(cd "${BUILD_DIR}" && lcov -d . -z)
|
|
||||||
(cd "${BUILD_DIR}" && ctest --verbose)
|
|
||||||
|
|
||||||
(cd "${BUILD_DIR}" && lcov -d . -c -o "coverage.info")
|
|
||||||
(cd "${BUILD_DIR}" && lcov -r "coverage.info" "*/usr/*" "*/untests/*" -o "coverage.info")
|
|
||||||
(cd "${BUILD_DIR}" && lcov -l "coverage.info")
|
|
||||||
|
|
||||||
bash <(curl -s https://codecov.io/bash) -f "${BUILD_DIR}/coverage.info" || echo "Codecov did not collect coverage reports"
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
set -e
|
|
||||||
DIR="$( cd "$( dirname "${BASH_SOURCE}" )" && pwd )"
|
|
||||||
|
|
||||||
ROOT_DIR="${DIR}/.."
|
|
||||||
BUILD_DIR="${ROOT_DIR}/build/darwin_release"
|
|
||||||
|
|
||||||
mkdir -p "${BUILD_DIR}"
|
|
||||||
(cd "${BUILD_DIR}" && cmake "${ROOT_DIR}" -DCMAKE_BUILD_TYPE=Release)
|
|
||||||
(cd "${BUILD_DIR}" && cmake --build .)
|
|
||||||
(cd "${BUILD_DIR}" && ctest --verbose)
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
set -e
|
|
||||||
DIR="$( cd "$( dirname "${BASH_SOURCE}" )" && pwd )"
|
|
||||||
|
|
||||||
ROOT_DIR="${DIR}/.."
|
|
||||||
BUILD_DIR="${ROOT_DIR}/build/linux_release"
|
|
||||||
|
|
||||||
mkdir -p "${BUILD_DIR}"
|
|
||||||
(cd "${BUILD_DIR}" && cmake "${ROOT_DIR}" -DCMAKE_BUILD_TYPE=Release)
|
|
||||||
(cd "${BUILD_DIR}" && cmake --build .)
|
|
||||||
(cd "${BUILD_DIR}" && ctest --verbose)
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
@echo off
|
|
||||||
set DIR=%~dp0
|
|
||||||
|
|
||||||
set ROOT_DIR=%DIR%..\
|
|
||||||
set BUILD_DIR=%ROOT_DIR%build\windows_release_x64\
|
|
||||||
|
|
||||||
if not exist %BUILD_DIR% mkdir %BUILD_DIR% || goto :error
|
|
||||||
|
|
||||||
pushd %BUILD_DIR% || goto :error
|
|
||||||
cmake %ROOT_DIR% -A x64 || goto :error
|
|
||||||
cmake --build . --config Release || goto :error
|
|
||||||
ctest --verbose || goto :error
|
|
||||||
popd || goto :error
|
|
||||||
|
|
||||||
goto :EOF
|
|
||||||
|
|
||||||
:error
|
|
||||||
echo Failed with error #%errorlevel%.
|
|
||||||
exit /b %errorlevel%
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
@echo off
|
|
||||||
set DIR=%~dp0
|
|
||||||
|
|
||||||
set ROOT_DIR=%DIR%..\
|
|
||||||
set BUILD_DIR=%ROOT_DIR%build\windows_release_x86\
|
|
||||||
|
|
||||||
if not exist %BUILD_DIR% mkdir %BUILD_DIR% || goto :error
|
|
||||||
|
|
||||||
pushd %BUILD_DIR% || goto :error
|
|
||||||
cmake %ROOT_DIR% -A Win32 || goto :error
|
|
||||||
cmake --build . --config Release || goto :error
|
|
||||||
ctest --verbose || goto :error
|
|
||||||
popd || goto :error
|
|
||||||
|
|
||||||
goto :EOF
|
|
||||||
|
|
||||||
:error
|
|
||||||
echo Failed with error #%errorlevel%.
|
|
||||||
exit /b %errorlevel%
|
|
||||||
16
.github/workflows/coverage.yml
vendored
16
.github/workflows/coverage.yml
vendored
@@ -1,16 +0,0 @@
|
|||||||
name: coverage
|
|
||||||
|
|
||||||
on: [push, pull_request]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: macos-10.15
|
|
||||||
name: "coverage"
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
submodules: recursive
|
|
||||||
- name: Install lcov by Homebrew
|
|
||||||
run: brew install lcov
|
|
||||||
- name: Build && Test && Upload
|
|
||||||
run: .ci/build_coverage.sh
|
|
||||||
21
.github/workflows/darwin.yml
vendored
21
.github/workflows/darwin.yml
vendored
@@ -10,15 +10,22 @@ jobs:
|
|||||||
matrix:
|
matrix:
|
||||||
config:
|
config:
|
||||||
# https://github.com/actions/virtual-environments/tree/main/images/macos
|
# https://github.com/actions/virtual-environments/tree/main/images/macos
|
||||||
- { os: "macos-10.15", xcode: "10.3" }
|
- { os: "macos-10.15", xcode: "10.3", arch: "x64" }
|
||||||
- { os: "macos-10.15", xcode: "11.7" }
|
- { os: "macos-12", xcode: "14.2", arch: "x64" }
|
||||||
- { os: "macos-10.15", xcode: "12.4" }
|
|
||||||
name: "xcode-${{matrix.config.xcode}}"
|
name: "xcode-${{matrix.config.xcode}}"
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- name: Setup
|
||||||
|
run: brew install cmake ninja
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: true
|
||||||
- name: Select Xcode
|
- name: Select Xcode
|
||||||
run: sudo xcode-select --switch "/Applications/Xcode_${{matrix.config.xcode}}.app"
|
run: sudo xcode-select --switch "/Applications/Xcode_${{matrix.config.xcode}}.app"
|
||||||
- name: Build && Test
|
- name: Build
|
||||||
run: .ci/build_darwin.sh
|
run: |
|
||||||
|
cmake --preset macos-${{matrix.config.arch}}
|
||||||
|
cmake --build --preset macos-${{matrix.config.arch}}-release
|
||||||
|
- name: Test
|
||||||
|
run: |
|
||||||
|
ctest --preset macos-${{matrix.config.arch}}-release
|
||||||
24
.github/workflows/linux.yml
vendored
24
.github/workflows/linux.yml
vendored
@@ -11,21 +11,21 @@ jobs:
|
|||||||
config:
|
config:
|
||||||
# https://github.com/actions/virtual-environments/tree/main/images/linux
|
# https://github.com/actions/virtual-environments/tree/main/images/linux
|
||||||
- { os: "ubuntu-20.04", cc: "gcc-7", cxx: "g++-7" }
|
- { os: "ubuntu-20.04", cc: "gcc-7", cxx: "g++-7" }
|
||||||
- { os: "ubuntu-20.04", cc: "gcc-8", cxx: "g++-8" }
|
|
||||||
- { os: "ubuntu-20.04", cc: "gcc-9", cxx: "g++-9" }
|
|
||||||
- { os: "ubuntu-20.04", cc: "gcc-10", cxx: "g++-10" }
|
|
||||||
- { os: "ubuntu-20.04", cc: "clang-7", cxx: "clang++-7" }
|
- { os: "ubuntu-20.04", cc: "clang-7", cxx: "clang++-7" }
|
||||||
- { os: "ubuntu-20.04", cc: "clang-8", cxx: "clang++-8" }
|
- { os: "ubuntu-22.04", cc: "gcc-12", cxx: "g++-12" }
|
||||||
- { os: "ubuntu-20.04", cc: "clang-9", cxx: "clang++-9" }
|
- { os: "ubuntu-22.04", cc: "clang-14", cxx: "clang++-14" }
|
||||||
- { os: "ubuntu-20.04", cc: "clang-10", cxx: "clang++-10" }
|
|
||||||
name: "${{matrix.config.cxx}}"
|
name: "${{matrix.config.cxx}}"
|
||||||
steps:
|
steps:
|
||||||
- name: Setup
|
- name: Setup
|
||||||
run: sudo apt-get -y install "${{matrix.config.cc}}" "${{matrix.config.cxx}}"
|
run: sudo apt-get -y install cmake ninja-build ${{matrix.config.cc}} ${{matrix.config.cxx}}
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: true
|
||||||
- name: Build && Test
|
- name: Build
|
||||||
run: .ci/build_linux.sh
|
run: |
|
||||||
env: { CC: "${{matrix.config.cc}}", CXX: "${{matrix.config.cxx}}" }
|
cmake --preset linux-${{matrix.config.cc}}
|
||||||
|
cmake --build --preset linux-${{matrix.config.cc}}-release
|
||||||
|
- name: Test
|
||||||
|
run: |
|
||||||
|
ctest --preset linux-${{matrix.config.cc}}-release
|
||||||
|
|||||||
28
.github/workflows/windows.yml
vendored
28
.github/workflows/windows.yml
vendored
@@ -10,14 +10,24 @@ jobs:
|
|||||||
matrix:
|
matrix:
|
||||||
config:
|
config:
|
||||||
# https://github.com/actions/virtual-environments/tree/main/images/win
|
# https://github.com/actions/virtual-environments/tree/main/images/win
|
||||||
- { os: "windows-2016", vs: "Visual Studio 2017", arch: "x86" }
|
- { os: "windows-2019", vc: "msvc2019", arch: "x86" }
|
||||||
- { os: "windows-2016", vs: "Visual Studio 2017", arch: "x64" }
|
- { os: "windows-2019", vc: "msvc2019", arch: "x64" }
|
||||||
- { os: "windows-2019", vs: "Visual Studio 2019", arch: "x86" }
|
- { os: "windows-2022", vc: "msvc2022", arch: "x86" }
|
||||||
- { os: "windows-2019", vs: "Visual Studio 2019", arch: "x64" }
|
- { os: "windows-2022", vc: "msvc2022", arch: "x64" }
|
||||||
name: "${{matrix.config.vs}} ${{matrix.config.arch}}"
|
name: "${{matrix.config.vc}} ${{matrix.config.arch}}"
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- name: Setup
|
||||||
|
run: choco install cmake ninja
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: true
|
||||||
- name: Build && Test
|
- name: Select MSVC
|
||||||
run: .ci\build_windows_${{matrix.config.arch}}.bat
|
uses: ilammy/msvc-dev-cmd@v1
|
||||||
|
- name: Build
|
||||||
|
run: |
|
||||||
|
cmake --preset windows-${{matrix.config.arch}}-${{matrix.config.vc}}
|
||||||
|
cmake --build --preset windows-${{matrix.config.arch}}-${{matrix.config.vc}}-release
|
||||||
|
- name: Test
|
||||||
|
run: |
|
||||||
|
ctest --preset windows-${{matrix.config.arch}}-${{matrix.config.vc}}-release
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
build/*
|
build/*
|
||||||
.vscode/*
|
install/*
|
||||||
|
.clangd/*
|
||||||
CMakeLists.txt.user
|
CMakeLists.txt.user
|
||||||
|
|||||||
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "vendors/doctest"]
|
||||||
|
path = vendors/doctest
|
||||||
|
url = https://github.com/doctest/doctest/
|
||||||
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [{
|
||||||
|
"name": "LLDB Debug",
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${command:cmake.launchTargetPath}",
|
||||||
|
"args": [],
|
||||||
|
"cwd": "${workspaceFolder}"
|
||||||
|
}]
|
||||||
|
}
|
||||||
29
.vscode/settings.json
vendored
Normal file
29
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"[cpp]": {
|
||||||
|
"files.encoding": "utf8",
|
||||||
|
"files.insertFinalNewline": true,
|
||||||
|
"files.trimFinalNewlines": true,
|
||||||
|
"files.trimTrailingWhitespace": true
|
||||||
|
},
|
||||||
|
"[cmake]": {
|
||||||
|
"files.encoding": "utf8",
|
||||||
|
"files.insertFinalNewline": true,
|
||||||
|
"files.trimFinalNewlines": true,
|
||||||
|
"files.trimTrailingWhitespace": true
|
||||||
|
},
|
||||||
|
"[python]": {
|
||||||
|
"files.encoding": "utf8",
|
||||||
|
"files.insertFinalNewline": true,
|
||||||
|
"files.trimFinalNewlines": true,
|
||||||
|
"files.trimTrailingWhitespace": true
|
||||||
|
},
|
||||||
|
"clangd.arguments": [
|
||||||
|
"--all-scopes-completion",
|
||||||
|
"--background-index",
|
||||||
|
"--clang-tidy",
|
||||||
|
"--compile-commands-dir=${workspaceFolder}/.clangd",
|
||||||
|
"--completion-style=detailed",
|
||||||
|
"--header-insertion=never"
|
||||||
|
],
|
||||||
|
"cmake.copyCompileCommands": "${workspaceFolder}/.clangd/compile_commands.json"
|
||||||
|
}
|
||||||
112
CMakeLists.txt
112
CMakeLists.txt
@@ -1,34 +1,98 @@
|
|||||||
# 3.8 version is required for `cxx_std_17`
|
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
|
||||||
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
|
|
||||||
|
|
||||||
if(NOT DEFINED PROJECT_NAME)
|
project(promise.hpp
|
||||||
set(BUILD_AS_STANDALONE ON)
|
VERSION "0.0.1"
|
||||||
else()
|
DESCRIPTION "C++ asynchronous promises like a Promises/A+"
|
||||||
set(BUILD_AS_STANDALONE OFF)
|
HOMEPAGE_URL "https://github.com/blackmatov/promise.hpp")
|
||||||
endif()
|
|
||||||
|
|
||||||
project(promise.hpp)
|
#
|
||||||
|
# LIBRARY
|
||||||
|
#
|
||||||
|
|
||||||
add_library(${PROJECT_NAME} INTERFACE)
|
add_library(${PROJECT_NAME} INTERFACE)
|
||||||
target_include_directories(${PROJECT_NAME} INTERFACE headers)
|
add_library(promise.hpp::promise.hpp ALIAS ${PROJECT_NAME})
|
||||||
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)
|
|
||||||
|
target_compile_features(${PROJECT_NAME} INTERFACE
|
||||||
|
cxx_std_17)
|
||||||
|
|
||||||
|
target_include_directories(${PROJECT_NAME} INTERFACE
|
||||||
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/headers>
|
||||||
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
|
||||||
|
|
||||||
find_package(Threads REQUIRED)
|
find_package(Threads REQUIRED)
|
||||||
target_link_libraries(${PROJECT_NAME} INTERFACE Threads::Threads)
|
target_link_libraries(${PROJECT_NAME} INTERFACE Threads::Threads)
|
||||||
|
|
||||||
target_compile_options(${PROJECT_NAME}
|
#
|
||||||
INTERFACE
|
# INSTALL
|
||||||
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
|
#
|
||||||
-Wno-c++98-compat-pedantic
|
|
||||||
-Wno-ctad-maybe-unsupported
|
|
||||||
-Wno-padded
|
|
||||||
-Wno-unknown-warning-option
|
|
||||||
-Wno-weak-vtables>)
|
|
||||||
|
|
||||||
if(BUILD_AS_STANDALONE)
|
if(PROJECT_IS_TOP_LEVEL)
|
||||||
option(BUILD_WITH_UNTESTS "Build with unit tests" ON)
|
include(CMakePackageConfigHelpers)
|
||||||
if(BUILD_WITH_UNTESTS)
|
include(GNUInstallDirs)
|
||||||
enable_testing()
|
|
||||||
add_subdirectory(untests)
|
set(PROMISE_HPP_INSTALL_CONFIG_DIR
|
||||||
endif()
|
"${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
|
||||||
|
|
||||||
|
set(PROMISE_HPP_INSTALL_CONFIG_INPUT
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in")
|
||||||
|
|
||||||
|
set(PROMISE_HPP_INSTALL_GENERATED_CONFIG_CMAKE
|
||||||
|
"${CMAKE_CURRENT_BINARY_DIR}/generated/${PROJECT_NAME}-config.cmake")
|
||||||
|
|
||||||
|
set(PROMISE_HPP_INSTALL_GENERATED_CONFIG_VERSION_CMAKE
|
||||||
|
"${CMAKE_CURRENT_BINARY_DIR}/generated/${PROJECT_NAME}-config-version.cmake")
|
||||||
|
|
||||||
|
configure_package_config_file(
|
||||||
|
"${PROMISE_HPP_INSTALL_CONFIG_INPUT}"
|
||||||
|
"${PROMISE_HPP_INSTALL_GENERATED_CONFIG_CMAKE}"
|
||||||
|
INSTALL_DESTINATION "${PROMISE_HPP_INSTALL_CONFIG_DIR}"
|
||||||
|
NO_SET_AND_CHECK_MACRO
|
||||||
|
NO_CHECK_REQUIRED_COMPONENTS_MACRO)
|
||||||
|
|
||||||
|
write_basic_package_version_file(
|
||||||
|
"${PROMISE_HPP_INSTALL_GENERATED_CONFIG_VERSION_CMAKE}"
|
||||||
|
VERSION ${PROJECT_VERSION}
|
||||||
|
COMPATIBILITY AnyNewerVersion
|
||||||
|
ARCH_INDEPENDENT)
|
||||||
|
|
||||||
|
install(
|
||||||
|
TARGETS ${PROJECT_NAME}
|
||||||
|
EXPORT ${PROJECT_NAME}-targets
|
||||||
|
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
|
||||||
|
|
||||||
|
install(
|
||||||
|
DIRECTORY headers/${PROJECT_NAME}
|
||||||
|
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
|
||||||
|
|
||||||
|
install(
|
||||||
|
EXPORT ${PROJECT_NAME}-targets
|
||||||
|
FILE ${PROJECT_NAME}-targets.cmake
|
||||||
|
NAMESPACE ${PROJECT_NAME}::
|
||||||
|
DESTINATION "${PROMISE_HPP_INSTALL_CONFIG_DIR}")
|
||||||
|
|
||||||
|
install(
|
||||||
|
FILES "${PROMISE_HPP_INSTALL_GENERATED_CONFIG_CMAKE}"
|
||||||
|
"${PROMISE_HPP_INSTALL_GENERATED_CONFIG_VERSION_CMAKE}"
|
||||||
|
DESTINATION "${PROMISE_HPP_INSTALL_CONFIG_DIR}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
#
|
||||||
|
# DEVELOPER
|
||||||
|
#
|
||||||
|
|
||||||
|
if(PROJECT_IS_TOP_LEVEL)
|
||||||
|
option(BUILD_WITH_COVERAGE "Build with coverage" OFF)
|
||||||
|
option(BUILD_WITH_SANITIZERS "Build with sanitizers" OFF)
|
||||||
|
|
||||||
|
enable_testing()
|
||||||
|
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
||||||
|
set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "CMake")
|
||||||
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
||||||
|
|
||||||
|
include(EnableASan)
|
||||||
|
include(EnableGCov)
|
||||||
|
include(EnableUBSan)
|
||||||
|
|
||||||
|
add_subdirectory(vendors)
|
||||||
|
add_subdirectory(untests)
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
342
CMakePresets.json
Normal file
342
CMakePresets.json
Normal file
@@ -0,0 +1,342 @@
|
|||||||
|
{
|
||||||
|
"version": 3,
|
||||||
|
|
||||||
|
"cmakeMinimumRequired": {
|
||||||
|
"major": 3,
|
||||||
|
"minor": 21,
|
||||||
|
"patch": 0
|
||||||
|
},
|
||||||
|
|
||||||
|
"configurePresets": [
|
||||||
|
{
|
||||||
|
"name": "ninja-base",
|
||||||
|
"hidden": true,
|
||||||
|
"generator": "Ninja Multi-Config",
|
||||||
|
"binaryDir": "${sourceDir}/build/${presetName}",
|
||||||
|
"installDir": "${sourceDir}/install/${presetName}",
|
||||||
|
"cacheVariables": {
|
||||||
|
"CMAKE_EXPORT_COMPILE_COMMANDS": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-base",
|
||||||
|
"hidden": true,
|
||||||
|
"inherits": "ninja-base",
|
||||||
|
"condition": {
|
||||||
|
"type": "equals",
|
||||||
|
"lhs": "${hostSystemName}",
|
||||||
|
"rhs": "Linux"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-clang-7",
|
||||||
|
"inherits": "linux-base",
|
||||||
|
"cacheVariables": {
|
||||||
|
"CMAKE_C_COMPILER": "clang-7",
|
||||||
|
"CMAKE_CXX_COMPILER": "clang++-7"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-clang-14",
|
||||||
|
"inherits": "linux-base",
|
||||||
|
"cacheVariables": {
|
||||||
|
"CMAKE_C_COMPILER": "clang-14",
|
||||||
|
"CMAKE_CXX_COMPILER": "clang++-14"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-gcc-7",
|
||||||
|
"inherits": "linux-base",
|
||||||
|
"cacheVariables": {
|
||||||
|
"CMAKE_C_COMPILER": "gcc-7",
|
||||||
|
"CMAKE_CXX_COMPILER": "g++-7"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-gcc-12",
|
||||||
|
"inherits": "linux-base",
|
||||||
|
"cacheVariables": {
|
||||||
|
"CMAKE_C_COMPILER": "gcc-12",
|
||||||
|
"CMAKE_CXX_COMPILER": "g++-12"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "macos-base",
|
||||||
|
"hidden": true,
|
||||||
|
"inherits": "ninja-base",
|
||||||
|
"condition": {
|
||||||
|
"type": "equals",
|
||||||
|
"lhs": "${hostSystemName}",
|
||||||
|
"rhs": "Darwin"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "macos-arm64",
|
||||||
|
"inherits": "macos-base",
|
||||||
|
"architecture": {
|
||||||
|
"value": "arm64",
|
||||||
|
"strategy": "external"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "macos-arm64-san",
|
||||||
|
"inherits": "macos-arm64",
|
||||||
|
"cacheVariables": {
|
||||||
|
"BUILD_WITH_SANITIZERS": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "macos-x64",
|
||||||
|
"inherits": "macos-base",
|
||||||
|
"architecture": {
|
||||||
|
"value": "x64",
|
||||||
|
"strategy": "external"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "macos-x64-san",
|
||||||
|
"inherits": "macos-x64",
|
||||||
|
"cacheVariables": {
|
||||||
|
"BUILD_WITH_SANITIZERS": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-base",
|
||||||
|
"hidden": true,
|
||||||
|
"inherits": "ninja-base",
|
||||||
|
"condition": {
|
||||||
|
"type": "equals",
|
||||||
|
"lhs": "${hostSystemName}",
|
||||||
|
"rhs": "Windows"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x86-msvc2019",
|
||||||
|
"inherits": "windows-base",
|
||||||
|
"architecture": {
|
||||||
|
"value": "x86",
|
||||||
|
"strategy": "external"
|
||||||
|
},
|
||||||
|
"toolset": {
|
||||||
|
"value": "v142,host=x64",
|
||||||
|
"strategy": "external"
|
||||||
|
},
|
||||||
|
"cacheVariables": {
|
||||||
|
"CMAKE_C_COMPILER": "cl",
|
||||||
|
"CMAKE_CXX_COMPILER": "cl"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x64-msvc2019",
|
||||||
|
"inherits": "windows-base",
|
||||||
|
"architecture": {
|
||||||
|
"value": "x64",
|
||||||
|
"strategy": "external"
|
||||||
|
},
|
||||||
|
"toolset": {
|
||||||
|
"value": "v142,host=x64",
|
||||||
|
"strategy": "external"
|
||||||
|
},
|
||||||
|
"cacheVariables": {
|
||||||
|
"CMAKE_C_COMPILER": "cl",
|
||||||
|
"CMAKE_CXX_COMPILER": "cl"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x86-msvc2022",
|
||||||
|
"inherits": "windows-base",
|
||||||
|
"architecture": {
|
||||||
|
"value": "x86",
|
||||||
|
"strategy": "external"
|
||||||
|
},
|
||||||
|
"toolset": {
|
||||||
|
"value": "v143,host=x64",
|
||||||
|
"strategy": "external"
|
||||||
|
},
|
||||||
|
"cacheVariables": {
|
||||||
|
"CMAKE_C_COMPILER": "cl",
|
||||||
|
"CMAKE_CXX_COMPILER": "cl"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x64-msvc2022",
|
||||||
|
"inherits": "windows-base",
|
||||||
|
"architecture": {
|
||||||
|
"value": "x64",
|
||||||
|
"strategy": "external"
|
||||||
|
},
|
||||||
|
"toolset": {
|
||||||
|
"value": "v143,host=x64",
|
||||||
|
"strategy": "external"
|
||||||
|
},
|
||||||
|
"cacheVariables": {
|
||||||
|
"CMAKE_C_COMPILER": "cl",
|
||||||
|
"CMAKE_CXX_COMPILER": "cl"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
|
||||||
|
"buildPresets": [
|
||||||
|
{
|
||||||
|
"name": "linux-clang-7-debug",
|
||||||
|
"configuration": "Debug",
|
||||||
|
"configurePreset": "linux-clang-7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-clang-7-release",
|
||||||
|
"configuration": "Release",
|
||||||
|
"configurePreset": "linux-clang-7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-clang-14-debug",
|
||||||
|
"configuration": "Debug",
|
||||||
|
"configurePreset": "linux-clang-14"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-clang-14-release",
|
||||||
|
"configuration": "Release",
|
||||||
|
"configurePreset": "linux-clang-14"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-gcc-7-debug",
|
||||||
|
"configuration": "Debug",
|
||||||
|
"configurePreset": "linux-gcc-7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-gcc-7-release",
|
||||||
|
"configuration": "Release",
|
||||||
|
"configurePreset": "linux-gcc-7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-gcc-12-debug",
|
||||||
|
"configuration": "Debug",
|
||||||
|
"configurePreset": "linux-gcc-12"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-gcc-12-release",
|
||||||
|
"configuration": "Release",
|
||||||
|
"configurePreset": "linux-gcc-12"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "macos-arm64-debug",
|
||||||
|
"configuration": "Debug",
|
||||||
|
"configurePreset": "macos-arm64-san"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "macos-arm64-release",
|
||||||
|
"configuration": "Release",
|
||||||
|
"configurePreset": "macos-arm64"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "macos-x64-debug",
|
||||||
|
"configuration": "Debug",
|
||||||
|
"configurePreset": "macos-x64-san"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "macos-x64-release",
|
||||||
|
"configuration": "Release",
|
||||||
|
"configurePreset": "macos-x64"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x86-msvc2019-debug",
|
||||||
|
"configuration": "Debug",
|
||||||
|
"configurePreset": "windows-x86-msvc2019"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x86-msvc2019-release",
|
||||||
|
"configuration": "Release",
|
||||||
|
"configurePreset": "windows-x86-msvc2019"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x64-msvc2019-debug",
|
||||||
|
"configuration": "Debug",
|
||||||
|
"configurePreset": "windows-x64-msvc2019"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x64-msvc2019-release",
|
||||||
|
"configuration": "Release",
|
||||||
|
"configurePreset": "windows-x64-msvc2019"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x86-msvc2022-debug",
|
||||||
|
"configuration": "Debug",
|
||||||
|
"configurePreset": "windows-x86-msvc2022"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x86-msvc2022-release",
|
||||||
|
"configuration": "Release",
|
||||||
|
"configurePreset": "windows-x86-msvc2022"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x64-msvc2022-debug",
|
||||||
|
"configuration": "Debug",
|
||||||
|
"configurePreset": "windows-x64-msvc2022"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x64-msvc2022-release",
|
||||||
|
"configuration": "Release",
|
||||||
|
"configurePreset": "windows-x64-msvc2022"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
|
||||||
|
"testPresets": [
|
||||||
|
{
|
||||||
|
"name": "test-base",
|
||||||
|
"hidden": true,
|
||||||
|
"output": {
|
||||||
|
"verbosity": "verbose"
|
||||||
|
},
|
||||||
|
"configuration": "Release"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-clang-7-release",
|
||||||
|
"inherits": "test-base",
|
||||||
|
"configurePreset": "linux-clang-7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-clang-14-release",
|
||||||
|
"inherits": "test-base",
|
||||||
|
"configurePreset": "linux-clang-14"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-gcc-7-release",
|
||||||
|
"inherits": "test-base",
|
||||||
|
"configurePreset": "linux-gcc-7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "linux-gcc-12-release",
|
||||||
|
"inherits": "test-base",
|
||||||
|
"configurePreset": "linux-gcc-12"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "macos-arm64-release",
|
||||||
|
"inherits": "test-base",
|
||||||
|
"configurePreset": "macos-arm64"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "macos-x64-release",
|
||||||
|
"inherits": "test-base",
|
||||||
|
"configurePreset": "macos-x64"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x86-msvc2019-release",
|
||||||
|
"inherits": "test-base",
|
||||||
|
"configurePreset": "windows-x86-msvc2019"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x64-msvc2019-release",
|
||||||
|
"inherits": "test-base",
|
||||||
|
"configurePreset": "windows-x64-msvc2019"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x86-msvc2022-release",
|
||||||
|
"inherits": "test-base",
|
||||||
|
"configurePreset": "windows-x86-msvc2022"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "windows-x64-msvc2022-release",
|
||||||
|
"inherits": "test-base",
|
||||||
|
"configurePreset": "windows-x64-msvc2022"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
18
README.md
18
README.md
@@ -5,21 +5,18 @@
|
|||||||
[![linux][badge.linux]][linux]
|
[![linux][badge.linux]][linux]
|
||||||
[![darwin][badge.darwin]][darwin]
|
[![darwin][badge.darwin]][darwin]
|
||||||
[![windows][badge.windows]][windows]
|
[![windows][badge.windows]][windows]
|
||||||
[![codecov][badge.codecov]][codecov]
|
|
||||||
[![language][badge.language]][language]
|
[![language][badge.language]][language]
|
||||||
[![license][badge.license]][license]
|
[![license][badge.license]][license]
|
||||||
|
|
||||||
[badge.darwin]: https://img.shields.io/github/workflow/status/BlackMATov/promise.hpp/darwin/main?label=Xcode&logo=xcode
|
[badge.darwin]: https://img.shields.io/github/actions/workflow/status/BlackMATov/promise.hpp/.github/workflows/darwin.yml?label=Xcode&logo=xcode
|
||||||
[badge.linux]: https://img.shields.io/github/workflow/status/BlackMATov/promise.hpp/linux/main?label=GCC%2FClang&logo=linux
|
[badge.linux]: https://img.shields.io/github/actions/workflow/status/BlackMATov/promise.hpp/.github/workflows/linux.yml?label=GCC%2FClang&logo=linux
|
||||||
[badge.windows]: https://img.shields.io/github/workflow/status/BlackMATov/promise.hpp/windows/main?label=Visual%20Studio&logo=visual-studio
|
[badge.windows]: https://img.shields.io/github/actions/workflow/status/BlackMATov/promise.hpp/.github/workflows/windows.yml?label=Visual%20Studio&logo=visual-studio
|
||||||
[badge.codecov]: https://img.shields.io/codecov/c/github/BlackMATov/promise.hpp/main?logo=codecov
|
|
||||||
[badge.language]: https://img.shields.io/badge/language-C%2B%2B17-yellow
|
[badge.language]: https://img.shields.io/badge/language-C%2B%2B17-yellow
|
||||||
[badge.license]: https://img.shields.io/badge/license-MIT-blue
|
[badge.license]: https://img.shields.io/badge/license-MIT-blue
|
||||||
|
|
||||||
[darwin]: https://github.com/BlackMATov/promise.hpp/actions?query=workflow%3Adarwin
|
[darwin]: https://github.com/BlackMATov/promise.hpp/actions?query=workflow%3Adarwin
|
||||||
[linux]: https://github.com/BlackMATov/promise.hpp/actions?query=workflow%3Alinux
|
[linux]: https://github.com/BlackMATov/promise.hpp/actions?query=workflow%3Alinux
|
||||||
[windows]: https://github.com/BlackMATov/promise.hpp/actions?query=workflow%3Awindows
|
[windows]: https://github.com/BlackMATov/promise.hpp/actions?query=workflow%3Awindows
|
||||||
[codecov]: https://codecov.io/gh/BlackMATov/promise.hpp
|
|
||||||
[language]: https://en.wikipedia.org/wiki/C%2B%2B17
|
[language]: https://en.wikipedia.org/wiki/C%2B%2B17
|
||||||
[license]: https://en.wikipedia.org/wiki/MIT_License
|
[license]: https://en.wikipedia.org/wiki/MIT_License
|
||||||
|
|
||||||
@@ -27,9 +24,10 @@
|
|||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- [gcc](https://www.gnu.org/software/gcc/) **>= 7**
|
|
||||||
- [clang](https://clang.llvm.org/) **>= 7**
|
- [clang](https://clang.llvm.org/) **>= 7**
|
||||||
- [msvc](https://visualstudio.microsoft.com/) **>= 2017**
|
- [gcc](https://www.gnu.org/software/gcc/) **>= 7**
|
||||||
|
- [msvc](https://visualstudio.microsoft.com/) **>= 2019**
|
||||||
|
- [xcode](https://developer.apple.com/xcode/) **>= 10.3**
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -43,7 +41,7 @@ Also, you can add the root repository directory to your [cmake](https://cmake.or
|
|||||||
|
|
||||||
```cmake
|
```cmake
|
||||||
add_subdirectory(external/promise.hpp)
|
add_subdirectory(external/promise.hpp)
|
||||||
target_link_libraries(your_project_target promise.hpp)
|
target_link_libraries(your_project_target PUBLIC promise.hpp::promise.hpp)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
@@ -51,7 +49,7 @@ target_link_libraries(your_project_target promise.hpp)
|
|||||||
### Creating a promise
|
### Creating a promise
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include "promise.hpp"
|
#include "promise.hpp/promise.hpp"
|
||||||
using promise_hpp;
|
using promise_hpp;
|
||||||
|
|
||||||
promise<std::string> download(const std::string& url)
|
promise<std::string> download(const std::string& url)
|
||||||
|
|||||||
6
cmake/Config.cmake.in
Normal file
6
cmake/Config.cmake.in
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
@PACKAGE_INIT@
|
||||||
|
|
||||||
|
include(CMakeFindDependencyMacro)
|
||||||
|
find_dependency(Threads REQUIRED)
|
||||||
|
|
||||||
|
include("${CMAKE_CURRENT_LIST_DIR}/promise.hpp-targets.cmake")
|
||||||
16
cmake/EnableASan.cmake
Normal file
16
cmake/EnableASan.cmake
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# https://clang.llvm.org/docs/AddressSanitizer.html
|
||||||
|
|
||||||
|
add_library(${PROJECT_NAME}.enable_asan INTERFACE)
|
||||||
|
add_library(${PROJECT_NAME}::enable_asan ALIAS ${PROJECT_NAME}.enable_asan)
|
||||||
|
|
||||||
|
target_compile_options(${PROJECT_NAME}.enable_asan INTERFACE
|
||||||
|
-fsanitize=address
|
||||||
|
-fno-omit-frame-pointer
|
||||||
|
-fsanitize-address-use-after-scope
|
||||||
|
-fsanitize-address-use-after-return=always)
|
||||||
|
|
||||||
|
target_link_options(${PROJECT_NAME}.enable_asan INTERFACE
|
||||||
|
-fsanitize=address
|
||||||
|
-fno-omit-frame-pointer
|
||||||
|
-fsanitize-address-use-after-scope
|
||||||
|
-fsanitize-address-use-after-return=always)
|
||||||
10
cmake/EnableGCov.cmake
Normal file
10
cmake/EnableGCov.cmake
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
|
||||||
|
|
||||||
|
add_library(${PROJECT_NAME}.enable_gcov INTERFACE)
|
||||||
|
add_library(${PROJECT_NAME}::enable_gcov ALIAS ${PROJECT_NAME}.enable_gcov)
|
||||||
|
|
||||||
|
target_compile_options(${PROJECT_NAME}.enable_gcov INTERFACE
|
||||||
|
--coverage)
|
||||||
|
|
||||||
|
target_link_options(${PROJECT_NAME}.enable_gcov INTERFACE
|
||||||
|
--coverage)
|
||||||
12
cmake/EnableUBSan.cmake
Normal file
12
cmake/EnableUBSan.cmake
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
|
||||||
|
|
||||||
|
add_library(${PROJECT_NAME}.enable_ubsan INTERFACE)
|
||||||
|
add_library(${PROJECT_NAME}::enable_ubsan ALIAS ${PROJECT_NAME}.enable_ubsan)
|
||||||
|
|
||||||
|
target_compile_options(${PROJECT_NAME}.enable_ubsan INTERFACE
|
||||||
|
-fsanitize=undefined
|
||||||
|
-fno-omit-frame-pointer)
|
||||||
|
|
||||||
|
target_link_options(${PROJECT_NAME}.enable_ubsan INTERFACE
|
||||||
|
-fsanitize=undefined
|
||||||
|
-fno-omit-frame-pointer)
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "promise.hpp"
|
#include "../promise.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "promise.hpp"
|
#include "../promise.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
@@ -1,38 +1,69 @@
|
|||||||
project(promise.hpp.untests)
|
project(promise.hpp.untests)
|
||||||
|
|
||||||
#
|
|
||||||
# coverage
|
|
||||||
#
|
|
||||||
|
|
||||||
option(BUILD_WITH_COVERAGE "Build with coverage" OFF)
|
|
||||||
if(BUILD_WITH_COVERAGE AND (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang"))
|
|
||||||
set(COVERAGE_FLAGS "--coverage")
|
|
||||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${COVERAGE_FLAGS}")
|
|
||||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${COVERAGE_FLAGS}")
|
|
||||||
set(CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} ${COVERAGE_FLAGS}")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
#
|
|
||||||
# executable
|
|
||||||
#
|
|
||||||
|
|
||||||
file(GLOB_RECURSE UNTESTS_SOURCES "*.cpp" "*.hpp")
|
file(GLOB_RECURSE UNTESTS_SOURCES "*.cpp" "*.hpp")
|
||||||
|
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${UNTESTS_SOURCES})
|
||||||
|
|
||||||
add_executable(${PROJECT_NAME} ${UNTESTS_SOURCES})
|
add_executable(${PROJECT_NAME} ${UNTESTS_SOURCES})
|
||||||
target_link_libraries(${PROJECT_NAME} promise.hpp)
|
target_link_libraries(${PROJECT_NAME} PRIVATE promise.hpp::promise.hpp)
|
||||||
|
|
||||||
target_compile_options(${PROJECT_NAME}
|
#
|
||||||
PRIVATE
|
# setup defines
|
||||||
$<$<CXX_COMPILER_ID:MSVC>: /bigobj /wd4702>)
|
#
|
||||||
|
|
||||||
target_compile_options(${PROJECT_NAME}
|
function(setup_defines_for_target TARGET)
|
||||||
PRIVATE
|
target_compile_definitions(${TARGET} PRIVATE
|
||||||
$<$<CXX_COMPILER_ID:MSVC>:
|
DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS
|
||||||
/WX /W4>
|
DOCTEST_CONFIG_USE_STD_HEADERS)
|
||||||
PRIVATE
|
endfunction()
|
||||||
$<$<CXX_COMPILER_ID:GNU>:
|
|
||||||
-Werror -Wall -Wextra -Wpedantic>
|
setup_defines_for_target(${PROJECT_NAME})
|
||||||
PRIVATE
|
|
||||||
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
|
#
|
||||||
-Werror -Weverything -Wconversion>)
|
# setup libraries
|
||||||
|
#
|
||||||
|
|
||||||
|
function(setup_libraries_for_target TARGET)
|
||||||
|
target_link_libraries(${TARGET} PRIVATE doctest::doctest_with_main)
|
||||||
|
|
||||||
|
if(${BUILD_WITH_COVERAGE})
|
||||||
|
target_link_libraries(${TARGET} PRIVATE promise.hpp::enable_gcov)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(${BUILD_WITH_SANITIZERS})
|
||||||
|
target_link_libraries(${TARGET} PRIVATE promise.hpp::enable_asan promise.hpp::enable_ubsan)
|
||||||
|
endif()
|
||||||
|
endfunction()
|
||||||
|
|
||||||
|
setup_libraries_for_target(${PROJECT_NAME})
|
||||||
|
|
||||||
|
#
|
||||||
|
# setup warnings
|
||||||
|
#
|
||||||
|
|
||||||
|
function(setup_warnings_for_target TARGET)
|
||||||
|
target_compile_options(${TARGET}
|
||||||
|
PRIVATE
|
||||||
|
$<$<CXX_COMPILER_ID:MSVC>:
|
||||||
|
/WX /W4 /wd4702>
|
||||||
|
PRIVATE
|
||||||
|
$<$<CXX_COMPILER_ID:GNU>:
|
||||||
|
-Werror -Wall -Wextra -Wpedantic>
|
||||||
|
PRIVATE
|
||||||
|
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
|
||||||
|
-Werror -Weverything -Wconversion
|
||||||
|
-Wno-c++98-compat
|
||||||
|
-Wno-c++98-compat-pedantic
|
||||||
|
-Wno-ctad-maybe-unsupported
|
||||||
|
-Wno-padded
|
||||||
|
-Wno-unknown-warning-option
|
||||||
|
-Wno-weak-vtables
|
||||||
|
>)
|
||||||
|
endfunction()
|
||||||
|
|
||||||
|
setup_warnings_for_target(${PROJECT_NAME})
|
||||||
|
|
||||||
|
#
|
||||||
|
# add tests
|
||||||
|
#
|
||||||
|
|
||||||
add_test(${PROJECT_NAME} ${PROJECT_NAME})
|
add_test(${PROJECT_NAME} ${PROJECT_NAME})
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
|
||||||
#include "doctest.h"
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,11 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "doctest.h"
|
|
||||||
|
|
||||||
#define STATIC_REQUIRE(...)\
|
|
||||||
static_assert(__VA_ARGS__, #__VA_ARGS__);\
|
|
||||||
REQUIRE(__VA_ARGS__)
|
|
||||||
|
|
||||||
#define STATIC_REQUIRE_FALSE(...)\
|
|
||||||
static_assert(!(__VA_ARGS__), "!(" #__VA_ARGS__ ")");\
|
|
||||||
REQUIRE(!(__VA_ARGS__))
|
|
||||||
@@ -4,8 +4,8 @@
|
|||||||
* Copyright (C) 2018-2023, by Matvey Cherevko (blackmatov@gmail.com)
|
* Copyright (C) 2018-2023, by Matvey Cherevko (blackmatov@gmail.com)
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
#include <promise.hpp/jobber.hpp>
|
#include <promise.hpp/bonus/jobber.hpp>
|
||||||
#include "doctest/doctest.h"
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
|
|||||||
419
untests/promise1_tests.cpp
Normal file
419
untests/promise1_tests.cpp
Normal file
@@ -0,0 +1,419 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* This file is part of the "https://github.com/blackmatov/promise.hpp"
|
||||||
|
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||||
|
* Copyright (C) 2018-2023, by Matvey Cherevko (blackmatov@gmail.com)
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include <promise.hpp/promise.hpp>
|
||||||
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <thread>
|
||||||
|
#include <numeric>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
namespace pr = promise_hpp;
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct obj_t {
|
||||||
|
};
|
||||||
|
|
||||||
|
bool check_hello_fail_exception(std::exception_ptr e) {
|
||||||
|
try {
|
||||||
|
std::rethrow_exception(e);
|
||||||
|
} catch (std::logic_error& ee) {
|
||||||
|
return 0 == std::strcmp(ee.what(), "hello fail");
|
||||||
|
} catch (...) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("is_promise") {
|
||||||
|
SUBCASE("positive") {
|
||||||
|
static_assert(
|
||||||
|
pr::is_promise<pr::promise<void>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
pr::is_promise<const pr::promise<void>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
pr::is_promise<const volatile pr::promise<void>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
|
||||||
|
static_assert(
|
||||||
|
pr::is_promise<pr::promise<int>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
pr::is_promise<const pr::promise<int>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
pr::is_promise<const volatile pr::promise<int>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
}
|
||||||
|
SUBCASE("negative") {
|
||||||
|
static_assert(
|
||||||
|
!pr::is_promise<pr::promise<void>&>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
!pr::is_promise<const pr::promise<void>*>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
!pr::is_promise<const volatile pr::promise<int>&>::value,
|
||||||
|
"unit test fail");
|
||||||
|
|
||||||
|
static_assert(
|
||||||
|
!pr::is_promise<int>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
!pr::is_promise<void>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
!pr::is_promise<const volatile int>::value,
|
||||||
|
"unit test fail");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("is_promise_r") {
|
||||||
|
SUBCASE("positive") {
|
||||||
|
static_assert(
|
||||||
|
pr::is_promise_r<void, pr::promise<void>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
pr::is_promise_r<int, const pr::promise<int>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
pr::is_promise_r<double, const pr::promise<int>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
}
|
||||||
|
SUBCASE("negative") {
|
||||||
|
static_assert(
|
||||||
|
!pr::is_promise_r<void, pr::promise<int>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
!pr::is_promise_r<void, const pr::promise<int>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
!pr::is_promise_r<int, pr::promise<obj_t>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
!pr::is_promise_r<double, int>::value,
|
||||||
|
"unit test fail");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("promise1") {
|
||||||
|
SUBCASE("basic") {
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<int>();
|
||||||
|
REQUIRE_FALSE(p1 == p2);
|
||||||
|
REQUIRE(p1 != p2);
|
||||||
|
|
||||||
|
REQUIRE((p1 < p2 || p2 < p1));
|
||||||
|
REQUIRE(p1.hash() != p2.hash());
|
||||||
|
REQUIRE(p1.hash() == std::hash<pr::promise<int>>()(p1));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<void>();
|
||||||
|
auto p2 = pr::promise<void>();
|
||||||
|
REQUIRE_FALSE(p1 == p2);
|
||||||
|
REQUIRE(p1 != p2);
|
||||||
|
|
||||||
|
REQUIRE((p1 < p2 || p2 < p1));
|
||||||
|
REQUIRE(p1.hash() != p2.hash());
|
||||||
|
REQUIRE(p1.hash() == std::hash<pr::promise<void>>()(p1));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<int>();
|
||||||
|
auto p3 = p1;
|
||||||
|
REQUIRE(p1 == p3);
|
||||||
|
p3 = p2;
|
||||||
|
REQUIRE(p2 == p3);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<int>();
|
||||||
|
auto p3 = p1;
|
||||||
|
p1.swap(p2);
|
||||||
|
REQUIRE(p2 == p3);
|
||||||
|
REQUIRE_FALSE(p1 == p3);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<void>();
|
||||||
|
auto p2 = pr::promise<void>();
|
||||||
|
auto p3 = p1;
|
||||||
|
p1.swap(p2);
|
||||||
|
REQUIRE(p2 == p3);
|
||||||
|
REQUIRE_FALSE(p1 == p3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("resolved") {
|
||||||
|
{
|
||||||
|
int check_42_int = 0;
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
p.resolve(42);
|
||||||
|
p.then([&check_42_int](int value){
|
||||||
|
check_42_int = value;
|
||||||
|
});
|
||||||
|
REQUIRE(check_42_int == 42);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int check_84_int = 0;
|
||||||
|
bool check_void_call = false;
|
||||||
|
int check_100500_transform = 0;
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
p.resolve(42);
|
||||||
|
p.then([](int value){
|
||||||
|
return value * 2;
|
||||||
|
}).then([&check_84_int](int value){
|
||||||
|
check_84_int = value;
|
||||||
|
}).then([&check_void_call](){
|
||||||
|
check_void_call = true;
|
||||||
|
}).then([](){
|
||||||
|
return 100500;
|
||||||
|
}).then([&check_100500_transform](int value){
|
||||||
|
check_100500_transform = value;
|
||||||
|
});
|
||||||
|
REQUIRE(check_84_int == 84);
|
||||||
|
REQUIRE(check_void_call);
|
||||||
|
REQUIRE(check_100500_transform == 100500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("resolved_ref") {
|
||||||
|
{
|
||||||
|
int* check_42_int = nullptr;
|
||||||
|
auto p = pr::promise<std::reference_wrapper<int>>();
|
||||||
|
int i = 42;
|
||||||
|
p.resolve(i);
|
||||||
|
p.then([&check_42_int](int& value) mutable {
|
||||||
|
check_42_int = &value;
|
||||||
|
});
|
||||||
|
REQUIRE(check_42_int);
|
||||||
|
REQUIRE(*check_42_int == 42);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const int* check_42_int = nullptr;
|
||||||
|
auto p = pr::promise<std::reference_wrapper<const int>>();
|
||||||
|
const int i = 42;
|
||||||
|
p.resolve(i);
|
||||||
|
p.then([&check_42_int](const int& value) mutable {
|
||||||
|
check_42_int = &value;
|
||||||
|
});
|
||||||
|
REQUIRE(check_42_int);
|
||||||
|
REQUIRE(*check_42_int == 42);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("rejected") {
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
bool not_call_then_on_reject = true;
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
p.reject(std::logic_error("hello fail"));
|
||||||
|
p.then([¬_call_then_on_reject](int value) {
|
||||||
|
(void)value;
|
||||||
|
not_call_then_on_reject = false;
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
REQUIRE(not_call_then_on_reject);
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::logic_error ee("hello fail");
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
p.reject(ee);
|
||||||
|
p.then([](int){
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::logic_error ee("hello fail");
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
p.reject(std::make_exception_ptr(ee));
|
||||||
|
p.then([](int){
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int check_multi_fail = 0;
|
||||||
|
auto p = pr::promise<>();
|
||||||
|
p.reject(std::logic_error("hello fail"));
|
||||||
|
p.except([&check_multi_fail](std::exception_ptr e){
|
||||||
|
++check_multi_fail;
|
||||||
|
std::rethrow_exception(e);
|
||||||
|
}).except([&check_multi_fail](std::exception_ptr){
|
||||||
|
++check_multi_fail;
|
||||||
|
});
|
||||||
|
REQUIRE(check_multi_fail == 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("unresolved") {
|
||||||
|
{
|
||||||
|
int check_42_int = 0;
|
||||||
|
bool not_call_before_resolve = true;
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
p.then([¬_call_before_resolve](int value){
|
||||||
|
not_call_before_resolve = false;
|
||||||
|
return value * 2;
|
||||||
|
}).then([&check_42_int, ¬_call_before_resolve](int value){
|
||||||
|
not_call_before_resolve = false;
|
||||||
|
check_42_int = value;
|
||||||
|
});
|
||||||
|
REQUIRE(check_42_int == 0);
|
||||||
|
REQUIRE(not_call_before_resolve);
|
||||||
|
p.resolve(42);
|
||||||
|
REQUIRE(check_42_int == 84);
|
||||||
|
REQUIRE_FALSE(not_call_before_resolve);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool not_call_then_on_reject = true;
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
p.then([¬_call_then_on_reject](int){
|
||||||
|
not_call_then_on_reject = false;
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
REQUIRE(not_call_then_on_reject);
|
||||||
|
REQUIRE_FALSE(call_fail_with_logic_error);
|
||||||
|
p.reject(std::make_exception_ptr(std::logic_error("hello fail")));
|
||||||
|
REQUIRE(not_call_then_on_reject);
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("finally") {
|
||||||
|
{
|
||||||
|
bool all_is_ok = false;
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
p.finally([&all_is_ok](){
|
||||||
|
all_is_ok = true;
|
||||||
|
});
|
||||||
|
REQUIRE_FALSE(all_is_ok);
|
||||||
|
p.resolve(1);
|
||||||
|
REQUIRE(all_is_ok);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool all_is_ok = false;
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
p.finally([&all_is_ok](){
|
||||||
|
all_is_ok = true;
|
||||||
|
});
|
||||||
|
REQUIRE_FALSE(all_is_ok);
|
||||||
|
p.reject(std::make_exception_ptr(std::logic_error("hello fail")));
|
||||||
|
REQUIRE(all_is_ok);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool all_is_ok = false;
|
||||||
|
pr::make_resolved_promise(1)
|
||||||
|
.finally([&all_is_ok](){
|
||||||
|
all_is_ok = true;
|
||||||
|
});
|
||||||
|
REQUIRE(all_is_ok);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool all_is_ok = false;
|
||||||
|
pr::make_rejected_promise<int>(std::logic_error("hello fail"))
|
||||||
|
.finally([&all_is_ok](){
|
||||||
|
all_is_ok = true;
|
||||||
|
});
|
||||||
|
REQUIRE(all_is_ok);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("after_finally") {
|
||||||
|
{
|
||||||
|
int check_84_int = 0;
|
||||||
|
auto p = pr::promise<>();
|
||||||
|
p.finally([&check_84_int](){
|
||||||
|
check_84_int = 42;
|
||||||
|
return 100500;
|
||||||
|
}).then([&check_84_int](){
|
||||||
|
check_84_int *= 2;
|
||||||
|
});
|
||||||
|
REQUIRE(check_84_int == 0);
|
||||||
|
p.resolve();
|
||||||
|
REQUIRE(check_84_int == 84);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int check_84_int = 0;
|
||||||
|
auto p = pr::promise<>();
|
||||||
|
p.finally([&check_84_int](){
|
||||||
|
check_84_int = 42;
|
||||||
|
return 100500;
|
||||||
|
}).except([&check_84_int](std::exception_ptr){
|
||||||
|
check_84_int *= 2;
|
||||||
|
});
|
||||||
|
REQUIRE(check_84_int == 0);
|
||||||
|
p.reject(std::make_exception_ptr(std::logic_error("hello fail")));
|
||||||
|
REQUIRE(check_84_int == 84);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("failed_finally") {
|
||||||
|
{
|
||||||
|
int check_84_int = 0;
|
||||||
|
auto p = pr::promise<>();
|
||||||
|
p.finally([&check_84_int](){
|
||||||
|
check_84_int += 42;
|
||||||
|
throw std::logic_error("hello fail");
|
||||||
|
}).except([&check_84_int](std::exception_ptr e){
|
||||||
|
if ( check_hello_fail_exception(e) ) {
|
||||||
|
check_84_int += 42;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
p.resolve();
|
||||||
|
REQUIRE(check_84_int == 84);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int check_84_int = 0;
|
||||||
|
auto p = pr::promise<>();
|
||||||
|
p.finally([&check_84_int](){
|
||||||
|
check_84_int += 42;
|
||||||
|
throw std::logic_error("hello fail");
|
||||||
|
}).except([&check_84_int](std::exception_ptr e){
|
||||||
|
if ( check_hello_fail_exception(e) ) {
|
||||||
|
check_84_int += 42;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
p.reject(std::make_exception_ptr(std::logic_error("hello")));
|
||||||
|
REQUIRE(check_84_int == 84);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int check_84_int = 0;
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
p.finally([&check_84_int](){
|
||||||
|
check_84_int += 42;
|
||||||
|
throw std::logic_error("hello fail");
|
||||||
|
}).except([&check_84_int](std::exception_ptr e) -> int {
|
||||||
|
if ( check_hello_fail_exception(e) ) {
|
||||||
|
check_84_int += 42;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
p.resolve(1);
|
||||||
|
REQUIRE(check_84_int == 84);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int check_84_int = 0;
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
p.finally([&check_84_int](){
|
||||||
|
check_84_int += 42;
|
||||||
|
throw std::logic_error("hello fail");
|
||||||
|
}).except([&check_84_int](std::exception_ptr e) -> int {
|
||||||
|
if ( check_hello_fail_exception(e) ) {
|
||||||
|
check_84_int += 42;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
p.reject(std::make_exception_ptr(std::logic_error("hello")));
|
||||||
|
REQUIRE(check_84_int == 84);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
405
untests/promise2_tests.cpp
Normal file
405
untests/promise2_tests.cpp
Normal file
@@ -0,0 +1,405 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* This file is part of the "https://github.com/blackmatov/promise.hpp"
|
||||||
|
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||||
|
* Copyright (C) 2018-2023, by Matvey Cherevko (blackmatov@gmail.com)
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include <promise.hpp/promise.hpp>
|
||||||
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <thread>
|
||||||
|
#include <numeric>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
namespace pr = promise_hpp;
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct obj_t {
|
||||||
|
};
|
||||||
|
|
||||||
|
bool check_hello_fail_exception(std::exception_ptr e) {
|
||||||
|
try {
|
||||||
|
std::rethrow_exception(e);
|
||||||
|
} catch (std::logic_error& ee) {
|
||||||
|
return 0 == std::strcmp(ee.what(), "hello fail");
|
||||||
|
} catch (...) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("promise2") {
|
||||||
|
SUBCASE("make_promise") {
|
||||||
|
{
|
||||||
|
int check_84_int = 0;
|
||||||
|
auto p = pr::make_promise<int>([](auto resolve, auto reject){
|
||||||
|
(void)reject;
|
||||||
|
resolve(42);
|
||||||
|
});
|
||||||
|
p.then([](int value){
|
||||||
|
return value * 2;
|
||||||
|
}).then([&check_84_int](int value){
|
||||||
|
check_84_int = value;
|
||||||
|
});
|
||||||
|
REQUIRE(check_84_int == 84);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p = pr::make_promise<int>([](auto resolve, auto reject){
|
||||||
|
(void)resolve;
|
||||||
|
reject(std::logic_error("hello fail"));
|
||||||
|
});
|
||||||
|
p.except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p = pr::make_promise<int>([](auto resolve, auto reject){
|
||||||
|
(void)resolve;
|
||||||
|
(void)reject;
|
||||||
|
throw std::logic_error("hello fail");
|
||||||
|
});
|
||||||
|
p.except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("make_resolved_promise") {
|
||||||
|
{
|
||||||
|
bool call_check = false;
|
||||||
|
pr::make_resolved_promise()
|
||||||
|
.then([&call_check]{
|
||||||
|
call_check = true;
|
||||||
|
});
|
||||||
|
REQUIRE(call_check);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int check_42_int = 0;
|
||||||
|
pr::make_resolved_promise(42)
|
||||||
|
.then([&check_42_int](int value){
|
||||||
|
check_42_int = value;
|
||||||
|
});
|
||||||
|
REQUIRE(check_42_int == 42);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("make_rejected_promise") {
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
pr::make_rejected_promise<int>(std::logic_error("hello fail"))
|
||||||
|
.except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
pr::make_rejected_promise(std::logic_error("hello fail"))
|
||||||
|
.except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("exceptions") {
|
||||||
|
{
|
||||||
|
bool not_call_then_on_reject = true;
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
p.resolve(42);
|
||||||
|
p.then([](int){
|
||||||
|
throw std::logic_error("hello fail");
|
||||||
|
}).then([¬_call_then_on_reject](){
|
||||||
|
not_call_then_on_reject = false;
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
REQUIRE(not_call_then_on_reject);
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool not_call_then_on_reject = true;
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
p.resolve(42);
|
||||||
|
p.then([](int){
|
||||||
|
throw std::logic_error("hello fail");
|
||||||
|
}, [](std::exception_ptr){
|
||||||
|
throw std::logic_error("hello fail2");
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
REQUIRE(not_call_then_on_reject);
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("multi_then") {
|
||||||
|
{
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
|
||||||
|
int pa_value = 0;
|
||||||
|
{
|
||||||
|
auto pa = p.then([](int value){
|
||||||
|
return value * 2;
|
||||||
|
}).then([&pa_value](int value){
|
||||||
|
pa_value = value;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
int pb_value = 0;
|
||||||
|
{
|
||||||
|
auto pb = p.then([](int value){
|
||||||
|
return value / 2;
|
||||||
|
}).then([&pb_value](int value){
|
||||||
|
pb_value = value;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
REQUIRE(pa_value == 0);
|
||||||
|
REQUIRE(pb_value == 0);
|
||||||
|
|
||||||
|
p.resolve(42);
|
||||||
|
|
||||||
|
REQUIRE(pa_value == 84);
|
||||||
|
REQUIRE(pb_value == 21);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
|
||||||
|
int pa_value = 0;
|
||||||
|
{
|
||||||
|
auto pa = p.then([](int){
|
||||||
|
throw std::logic_error("hello fail");
|
||||||
|
}).except([&pa_value](std::exception_ptr e){
|
||||||
|
if ( check_hello_fail_exception(e) ) {
|
||||||
|
pa_value = 84;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
int pb_value = 0;
|
||||||
|
{
|
||||||
|
auto pb = p.then([](int value){
|
||||||
|
return value / 2;
|
||||||
|
}).then([&pb_value](int value){
|
||||||
|
pb_value = value;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
REQUIRE(pa_value == 0);
|
||||||
|
REQUIRE(pb_value == 0);
|
||||||
|
|
||||||
|
p.resolve(42);
|
||||||
|
|
||||||
|
REQUIRE(pa_value == 84);
|
||||||
|
REQUIRE(pb_value == 21);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("chaining") {
|
||||||
|
{
|
||||||
|
int check_84_int = 0;
|
||||||
|
auto p1 = pr::make_resolved_promise(42);
|
||||||
|
auto p2 = pr::make_resolved_promise(84);
|
||||||
|
|
||||||
|
p1.then([&p2](int v){
|
||||||
|
(void)v;
|
||||||
|
return p2;
|
||||||
|
}).then([&check_84_int](int v2){
|
||||||
|
check_84_int = v2;
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE(check_84_int == 84);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int check_84_int = 0;
|
||||||
|
auto p1 = pr::make_resolved_promise();
|
||||||
|
auto p2 = pr::make_resolved_promise(84);
|
||||||
|
|
||||||
|
p1.then([&p2](){
|
||||||
|
return p2;
|
||||||
|
}).then([&check_84_int](int v){
|
||||||
|
check_84_int = v;
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE(check_84_int == 84);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int check_84_int = 0;
|
||||||
|
auto p1 = pr::make_resolved_promise(42);
|
||||||
|
auto p2 = pr::make_resolved_promise();
|
||||||
|
|
||||||
|
p1.then([&p2](int v){
|
||||||
|
(void)v;
|
||||||
|
return p2;
|
||||||
|
}).then([&check_84_int](){
|
||||||
|
check_84_int = 84;
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE(check_84_int == 84);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int check_84_int = 0;
|
||||||
|
auto p1 = pr::make_resolved_promise();
|
||||||
|
auto p2 = pr::make_resolved_promise();
|
||||||
|
|
||||||
|
p1.then([&p2](){
|
||||||
|
return p2;
|
||||||
|
}).then([&check_84_int](){
|
||||||
|
check_84_int = 84;
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE(check_84_int == 84);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("lazy_chaining") {
|
||||||
|
{
|
||||||
|
int check_84_int = 0;
|
||||||
|
auto p1 = pr::make_promise<int>();
|
||||||
|
auto p2 = pr::make_promise<int>();
|
||||||
|
|
||||||
|
p1.then([&p2](int v){
|
||||||
|
(void)v;
|
||||||
|
return p2;
|
||||||
|
}).then([&check_84_int](int v2){
|
||||||
|
check_84_int = v2;
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE(check_84_int == 0);
|
||||||
|
p1.resolve(42);
|
||||||
|
REQUIRE(check_84_int == 0);
|
||||||
|
p2.resolve(84);
|
||||||
|
REQUIRE(check_84_int == 84);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("typed_chaining_fails") {
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p1 = pr::make_resolved_promise(42);
|
||||||
|
|
||||||
|
p1.then([](int v) -> pr::promise<int> {
|
||||||
|
(void)v;
|
||||||
|
throw std::logic_error("hello fail");
|
||||||
|
}).then([](int v2){
|
||||||
|
(void)v2;
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p1 = pr::make_resolved_promise(42);
|
||||||
|
auto p2 = pr::make_resolved_promise(84);
|
||||||
|
|
||||||
|
p1.then([&p2](int v){
|
||||||
|
(void)v;
|
||||||
|
return p2;
|
||||||
|
}).then([](int v2){
|
||||||
|
(void)v2;
|
||||||
|
throw std::logic_error("hello fail");
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p1 = pr::make_rejected_promise<int>(std::logic_error("hello fail"));
|
||||||
|
auto p2 = pr::make_resolved_promise(84);
|
||||||
|
|
||||||
|
p1.then([&p2](int v){
|
||||||
|
(void)v;
|
||||||
|
return p2;
|
||||||
|
}).then([](int v2){
|
||||||
|
(void)v2;
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p1 = pr::make_resolved_promise(42);
|
||||||
|
auto p2 = pr::make_rejected_promise<int>(std::logic_error("hello fail"));
|
||||||
|
|
||||||
|
p1.then([&p2](int v){
|
||||||
|
(void)v;
|
||||||
|
return p2;
|
||||||
|
}).then([](int v2){
|
||||||
|
(void)v2;
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("void_chaining_fails") {
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p1 = pr::make_resolved_promise();
|
||||||
|
|
||||||
|
p1.then([]() -> pr::promise<void> {
|
||||||
|
throw std::logic_error("hello fail");
|
||||||
|
}).then([](){
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p1 = pr::make_resolved_promise();
|
||||||
|
auto p2 = pr::make_resolved_promise();
|
||||||
|
|
||||||
|
p1.then([&p2](){
|
||||||
|
return p2;
|
||||||
|
}).then([](){
|
||||||
|
throw std::logic_error("hello fail");
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p1 = pr::make_rejected_promise<void>(std::logic_error("hello fail"));
|
||||||
|
auto p2 = pr::make_resolved_promise();
|
||||||
|
|
||||||
|
p1.then([&p2](){
|
||||||
|
return p2;
|
||||||
|
}).then([](){
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p1 = pr::make_resolved_promise();
|
||||||
|
auto p2 = pr::make_rejected_promise<void>(std::logic_error("hello fail"));
|
||||||
|
|
||||||
|
p1.then([&p2](){
|
||||||
|
return p2;
|
||||||
|
}).then([](){
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
568
untests/promise3_tests.cpp
Normal file
568
untests/promise3_tests.cpp
Normal file
@@ -0,0 +1,568 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* This file is part of the "https://github.com/blackmatov/promise.hpp"
|
||||||
|
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||||
|
* Copyright (C) 2018-2023, by Matvey Cherevko (blackmatov@gmail.com)
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include <promise.hpp/promise.hpp>
|
||||||
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <thread>
|
||||||
|
#include <numeric>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
namespace pr = promise_hpp;
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct obj_t {
|
||||||
|
};
|
||||||
|
|
||||||
|
bool check_hello_fail_exception(std::exception_ptr e) {
|
||||||
|
try {
|
||||||
|
std::rethrow_exception(e);
|
||||||
|
} catch (std::logic_error& ee) {
|
||||||
|
return 0 == std::strcmp(ee.what(), "hello fail");
|
||||||
|
} catch (...) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool check_empty_aggregate_exception(std::exception_ptr e) {
|
||||||
|
try {
|
||||||
|
std::rethrow_exception(e);
|
||||||
|
} catch (pr::aggregate_exception& ee) {
|
||||||
|
return ee.empty();
|
||||||
|
} catch (...) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool check_two_aggregate_exception(std::exception_ptr e) {
|
||||||
|
try {
|
||||||
|
std::rethrow_exception(e);
|
||||||
|
} catch (pr::aggregate_exception& ee) {
|
||||||
|
if ( ee.size() != 2 ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return check_hello_fail_exception(ee[0])
|
||||||
|
&& check_hello_fail_exception(ee[1]);
|
||||||
|
} catch (...) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("promise3") {
|
||||||
|
SUBCASE("make_all_promise") {
|
||||||
|
{
|
||||||
|
bool all_is_ok = false;
|
||||||
|
pr::make_all_promise(std::vector<pr::promise<int>>())
|
||||||
|
.then([&all_is_ok](const std::vector<int>& c){
|
||||||
|
all_is_ok = c.empty();
|
||||||
|
});
|
||||||
|
REQUIRE(all_is_ok);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool all_is_ok = false;
|
||||||
|
auto p = pr::make_resolved_promise().then_all([](){
|
||||||
|
return std::vector<pr::promise<int>>{
|
||||||
|
pr::make_resolved_promise(32),
|
||||||
|
pr::make_resolved_promise(10)};
|
||||||
|
}).then([&all_is_ok](const std::vector<int>& c){
|
||||||
|
all_is_ok = (2 == c.size())
|
||||||
|
&& c[0] == 32
|
||||||
|
&& c[1] == 10;
|
||||||
|
});
|
||||||
|
REQUIRE(all_is_ok);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool all_is_ok = false;
|
||||||
|
auto p = pr::make_resolved_promise(1).then_all([](int){
|
||||||
|
return std::vector<pr::promise<int>>{
|
||||||
|
pr::make_resolved_promise(32),
|
||||||
|
pr::make_resolved_promise(10)};
|
||||||
|
}).then([&all_is_ok](const std::vector<int>& c){
|
||||||
|
all_is_ok = (2 == c.size())
|
||||||
|
&& c[0] == 32
|
||||||
|
&& c[1] == 10;
|
||||||
|
});
|
||||||
|
REQUIRE(all_is_ok);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<int>();
|
||||||
|
|
||||||
|
int call_then_only_once = 0;
|
||||||
|
pr::make_all_promise(std::vector<pr::promise<int>>{p1, p2})
|
||||||
|
.then([&call_then_only_once](const std::vector<int>& c){
|
||||||
|
(void)c;
|
||||||
|
++call_then_only_once;
|
||||||
|
});
|
||||||
|
|
||||||
|
p1.resolve(1);
|
||||||
|
p2.resolve(2);
|
||||||
|
|
||||||
|
REQUIRE(call_then_only_once == 1);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<int>();
|
||||||
|
|
||||||
|
int call_then_only_once = 0;
|
||||||
|
pr::make_all_promise(std::array<pr::promise<int>, 2>{p1, p2})
|
||||||
|
.then([&call_then_only_once](const std::vector<int>& c){
|
||||||
|
(void)c;
|
||||||
|
++call_then_only_once;
|
||||||
|
});
|
||||||
|
|
||||||
|
p1.resolve(1);
|
||||||
|
p2.resolve(2);
|
||||||
|
|
||||||
|
REQUIRE(call_then_only_once == 1);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
class o_t {
|
||||||
|
public:
|
||||||
|
o_t() = delete;
|
||||||
|
o_t(int i) { (void)i; }
|
||||||
|
};
|
||||||
|
|
||||||
|
pr::promise<>()
|
||||||
|
.then_all([](){
|
||||||
|
return std::vector<pr::promise<o_t>>{
|
||||||
|
pr::make_resolved_promise<o_t>(40),
|
||||||
|
pr::make_resolved_promise<o_t>(2)};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("make_any_promise") {
|
||||||
|
{
|
||||||
|
bool all_is_ok = false;
|
||||||
|
auto p = pr::make_any_promise(std::vector<pr::promise<int>>{});
|
||||||
|
p.except([&all_is_ok](std::exception_ptr e){
|
||||||
|
all_is_ok = check_empty_aggregate_exception(e);
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
REQUIRE(all_is_ok);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::make_resolved_promise().then_any([](){
|
||||||
|
return std::vector<pr::promise<int>>{
|
||||||
|
pr::make_resolved_promise(32),
|
||||||
|
pr::make_resolved_promise(10)};
|
||||||
|
}).then([](int i){
|
||||||
|
return i;
|
||||||
|
});
|
||||||
|
REQUIRE(p.get() == 32);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::make_resolved_promise(1).then_any([](int){
|
||||||
|
return std::vector<pr::promise<int>>{
|
||||||
|
pr::make_resolved_promise(32),
|
||||||
|
pr::make_resolved_promise(10)};
|
||||||
|
}).then([](int i){
|
||||||
|
return i;
|
||||||
|
});
|
||||||
|
REQUIRE(p.get() == 32);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::make_any_promise(std::vector<pr::promise<int>>{
|
||||||
|
pr::make_resolved_promise(32),
|
||||||
|
pr::make_rejected_promise<int>(std::logic_error("hello fail"))
|
||||||
|
}).then([](int i){
|
||||||
|
return i;
|
||||||
|
});
|
||||||
|
REQUIRE(p.get() == 32);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::make_any_promise(std::vector<pr::promise<int>>{
|
||||||
|
pr::make_rejected_promise<int>(std::logic_error("hello fail")),
|
||||||
|
pr::make_resolved_promise(32)
|
||||||
|
}).then([](int i){
|
||||||
|
return i;
|
||||||
|
});
|
||||||
|
REQUIRE(p.get() == 32);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool all_is_ok = false;
|
||||||
|
auto p = pr::make_any_promise(std::vector<pr::promise<int>>{
|
||||||
|
pr::make_rejected_promise<int>(std::logic_error("hello fail")),
|
||||||
|
pr::make_rejected_promise<int>(std::logic_error("hello fail"))
|
||||||
|
}).except([&all_is_ok](std::exception_ptr e){
|
||||||
|
all_is_ok = check_two_aggregate_exception(e);
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
REQUIRE(all_is_ok);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("make_race_promise") {
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<int>();
|
||||||
|
|
||||||
|
int check_42_int = 0;
|
||||||
|
int call_then_only_once = 0;
|
||||||
|
pr::make_race_promise(std::vector<pr::promise<int>>{p1, p2})
|
||||||
|
.then([&check_42_int, &call_then_only_once](const int& v){
|
||||||
|
check_42_int = v;
|
||||||
|
++call_then_only_once;
|
||||||
|
});
|
||||||
|
|
||||||
|
p1.resolve(42);
|
||||||
|
REQUIRE(check_42_int == 42);
|
||||||
|
REQUIRE(call_then_only_once == 1);
|
||||||
|
|
||||||
|
p2.resolve(84);
|
||||||
|
REQUIRE(check_42_int == 42);
|
||||||
|
REQUIRE(call_then_only_once == 1);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<int>();
|
||||||
|
|
||||||
|
int check_42_int = 0;
|
||||||
|
int call_then_only_once = 0;
|
||||||
|
pr::make_race_promise(std::vector<pr::promise<int>>{p1, p2})
|
||||||
|
.then([&check_42_int, &call_then_only_once](const int& v){
|
||||||
|
check_42_int = v;
|
||||||
|
++call_then_only_once;
|
||||||
|
});
|
||||||
|
|
||||||
|
p2.resolve(42);
|
||||||
|
REQUIRE(check_42_int == 42);
|
||||||
|
REQUIRE(call_then_only_once == 1);
|
||||||
|
|
||||||
|
p1.resolve(84);
|
||||||
|
REQUIRE(check_42_int == 42);
|
||||||
|
REQUIRE(call_then_only_once == 1);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<int>();
|
||||||
|
|
||||||
|
int check_42_int = 0;
|
||||||
|
int call_then_only_once = 0;
|
||||||
|
pr::make_race_promise(std::array<pr::promise<int>,2>{p1, p2})
|
||||||
|
.then([&check_42_int, &call_then_only_once](const int& v){
|
||||||
|
check_42_int = v;
|
||||||
|
++call_then_only_once;
|
||||||
|
});
|
||||||
|
|
||||||
|
p2.resolve(42);
|
||||||
|
REQUIRE(check_42_int == 42);
|
||||||
|
REQUIRE(call_then_only_once == 1);
|
||||||
|
|
||||||
|
p1.resolve(84);
|
||||||
|
REQUIRE(check_42_int == 42);
|
||||||
|
REQUIRE(call_then_only_once == 1);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
class o_t {
|
||||||
|
public:
|
||||||
|
o_t() = delete;
|
||||||
|
o_t(int i) { (void)i; }
|
||||||
|
};
|
||||||
|
|
||||||
|
pr::promise<>()
|
||||||
|
.then_race([](){
|
||||||
|
return std::vector<pr::promise<o_t>>{
|
||||||
|
pr::make_resolved_promise<o_t>(40),
|
||||||
|
pr::make_resolved_promise<o_t>(2)};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("make_tuple_promise") {
|
||||||
|
{
|
||||||
|
static_assert(
|
||||||
|
std::is_same<
|
||||||
|
pr::impl::tuple_promise_result_t<std::tuple<>>,
|
||||||
|
std::tuple<>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
std::is_same<
|
||||||
|
pr::impl::tuple_promise_result_t<std::tuple<pr::promise<int>>>,
|
||||||
|
std::tuple<int>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
static_assert(
|
||||||
|
std::is_same<
|
||||||
|
pr::impl::tuple_promise_result_t<std::tuple<pr::promise<int>, pr::promise<float>>>,
|
||||||
|
std::tuple<int, float>>::value,
|
||||||
|
"unit test fail");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::make_tuple_promise(std::make_tuple());
|
||||||
|
REQUIRE(p.get() == std::make_tuple());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::make_tuple_promise(std::make_tuple(p1));
|
||||||
|
p1.resolve(42);
|
||||||
|
REQUIRE(p2.get_or_default(std::make_tuple(0)) == std::make_tuple(42));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto t0 = std::make_tuple(p1);
|
||||||
|
auto p2 = pr::make_tuple_promise(t0);
|
||||||
|
p1.resolve(42);
|
||||||
|
REQUIRE(p2.get_or_default(std::make_tuple(0)) == std::make_tuple(42));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<float>();
|
||||||
|
auto p3 = pr::make_tuple_promise(std::make_tuple(p1, p2));
|
||||||
|
p1.resolve(42);
|
||||||
|
p2.resolve(4.2f);
|
||||||
|
REQUIRE(p3.get_or_default(std::make_tuple(0, 0.f)) == std::make_tuple(42, 4.2f));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<float>();
|
||||||
|
auto p3 = pr::promise<int>();
|
||||||
|
auto p4 = pr::make_tuple_promise(std::make_tuple(p1, p2, p3));
|
||||||
|
p1.resolve(42);
|
||||||
|
p2.resolve(4.2f);
|
||||||
|
p3.resolve(84);
|
||||||
|
REQUIRE(p4.get_or_default(std::make_tuple(0, 0.f, 0)) == std::make_tuple(42, 4.2f, 84));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
class o_t {
|
||||||
|
public:
|
||||||
|
o_t() = delete;
|
||||||
|
};
|
||||||
|
|
||||||
|
pr::promise<>()
|
||||||
|
.then_tuple([](){
|
||||||
|
auto p1 = pr::promise<o_t>();
|
||||||
|
auto p2 = pr::promise<o_t>();
|
||||||
|
return std::make_tuple(std::move(p1), std::move(p2));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<std::reference_wrapper<int>>();
|
||||||
|
auto p2 = pr::promise<std::reference_wrapper<float>>();
|
||||||
|
auto p3 = pr::make_tuple_promise(std::make_tuple(p1, p2));
|
||||||
|
|
||||||
|
int i = 10;
|
||||||
|
float f = 0.f;
|
||||||
|
p1.resolve(i);
|
||||||
|
p2.resolve(f);
|
||||||
|
|
||||||
|
p3.then([&i,&f](const std::tuple<int&, float&>& t){
|
||||||
|
REQUIRE(&std::get<0>(t) == &i);
|
||||||
|
REQUIRE(&std::get<1>(t) == &f);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("make_all_promise_fail") {
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
bool not_call_then_on_reject = true;
|
||||||
|
auto p = pr::make_all_promise(std::vector<pr::promise<int>>{
|
||||||
|
pr::make_rejected_promise<int>(std::logic_error("hello fail")),
|
||||||
|
pr::make_resolved_promise(10)
|
||||||
|
}).then([¬_call_then_on_reject](const std::vector<int>& c){
|
||||||
|
(void)c;
|
||||||
|
not_call_then_on_reject = false;
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
REQUIRE(not_call_then_on_reject);
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<int>();
|
||||||
|
|
||||||
|
int call_then_count = 0;
|
||||||
|
int call_except_count = 0;
|
||||||
|
pr::make_all_promise(std::vector<pr::promise<int>>{p1, p2})
|
||||||
|
.then([&call_then_count](const std::vector<int>& c){
|
||||||
|
(void)c;
|
||||||
|
++call_then_count;
|
||||||
|
}, [&call_except_count](std::exception_ptr){
|
||||||
|
++call_except_count;
|
||||||
|
});
|
||||||
|
|
||||||
|
p1.resolve(1);
|
||||||
|
REQUIRE(call_then_count == 0);
|
||||||
|
REQUIRE(call_except_count == 0);
|
||||||
|
p2.reject(std::logic_error("hello fail"));
|
||||||
|
REQUIRE(call_then_count == 0);
|
||||||
|
REQUIRE(call_except_count == 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("make_race_promise_fail") {
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
bool not_call_then_on_reject = true;
|
||||||
|
auto p = pr::make_race_promise(std::vector<pr::promise<int>>{
|
||||||
|
pr::make_rejected_promise<int>(std::logic_error("hello fail")),
|
||||||
|
pr::make_resolved_promise(10)
|
||||||
|
}).then([¬_call_then_on_reject](const int& c){
|
||||||
|
(void)c;
|
||||||
|
not_call_then_on_reject = false;
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
});
|
||||||
|
REQUIRE(not_call_then_on_reject);
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("make_tuple_promise_fail") {
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::make_tuple_promise(std::make_tuple(p1));
|
||||||
|
p1.reject(std::logic_error("hello failt"));
|
||||||
|
REQUIRE_THROWS_AS(p2.get(), std::logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<float>();
|
||||||
|
auto p3 = pr::make_tuple_promise(std::make_tuple(p1, p2));
|
||||||
|
p1.resolve(42);
|
||||||
|
p2.reject(std::logic_error("hello failt"));
|
||||||
|
REQUIRE_THROWS_AS(p3.get(), std::logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<float>();
|
||||||
|
auto p3 = pr::make_tuple_promise(std::make_tuple(p1, p2));
|
||||||
|
p1.reject(std::logic_error("hello failt"));
|
||||||
|
p2.resolve(4.2f);
|
||||||
|
REQUIRE_THROWS_AS(p3.get(), std::logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<float>();
|
||||||
|
auto p3 = pr::make_tuple_promise(std::make_tuple(p1, p2));
|
||||||
|
p1.reject(std::logic_error("hello failt"));
|
||||||
|
REQUIRE_THROWS_AS(p3.get(), std::logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p1 = pr::promise<int>();
|
||||||
|
auto p2 = pr::promise<float>();
|
||||||
|
auto p3 = pr::make_tuple_promise(std::make_tuple(p1, p2));
|
||||||
|
p2.reject(std::logic_error("hello failt"));
|
||||||
|
REQUIRE_THROWS_AS(p3.get(), std::logic_error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("then_all") {
|
||||||
|
{
|
||||||
|
int check_42_int = 0;
|
||||||
|
pr::make_resolved_promise()
|
||||||
|
.then_all([](){
|
||||||
|
return std::vector<pr::promise<int>>{
|
||||||
|
pr::make_resolved_promise(32),
|
||||||
|
pr::make_resolved_promise(10)};
|
||||||
|
}).then([&check_42_int](const std::vector<int>& v){
|
||||||
|
if ( v.size() == 2) {
|
||||||
|
check_42_int = v[0] + v[1];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
REQUIRE(check_42_int == 42);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int check_42_int = 0;
|
||||||
|
int check_42_int2 = 0;
|
||||||
|
pr::make_resolved_promise(42)
|
||||||
|
.then_all([&check_42_int](int v){
|
||||||
|
check_42_int = v;
|
||||||
|
return std::vector<pr::promise<int>>{
|
||||||
|
pr::make_resolved_promise(32),
|
||||||
|
pr::make_resolved_promise(10)};
|
||||||
|
}).then([&check_42_int2](const std::vector<int>& v){
|
||||||
|
if ( v.size() == 2) {
|
||||||
|
check_42_int2 = v[0] + v[1];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
REQUIRE(check_42_int == 42);
|
||||||
|
REQUIRE(check_42_int2 == 42);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("then_race") {
|
||||||
|
{
|
||||||
|
int check_42_int = 0;
|
||||||
|
pr::make_resolved_promise()
|
||||||
|
.then_race([](){
|
||||||
|
return std::vector<pr::promise<int>>{
|
||||||
|
pr::make_resolved_promise(42),
|
||||||
|
pr::make_rejected_promise<int>(std::logic_error("hello fail"))};
|
||||||
|
}).then([&check_42_int](const int& v){
|
||||||
|
check_42_int = v;
|
||||||
|
});
|
||||||
|
REQUIRE(check_42_int == 42);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
pr::make_resolved_promise()
|
||||||
|
.then_race([](){
|
||||||
|
return std::vector<pr::promise<int>>{
|
||||||
|
pr::make_rejected_promise<int>(std::logic_error("hello fail")),
|
||||||
|
pr::make_resolved_promise(42)};
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int check_42_int = 0;
|
||||||
|
int check_42_int2 = 0;
|
||||||
|
int call_then_only_once = 0;
|
||||||
|
pr::make_resolved_promise(42)
|
||||||
|
.then_race([&check_42_int](int v){
|
||||||
|
check_42_int = v;
|
||||||
|
return std::vector<pr::promise<int>>{
|
||||||
|
pr::make_resolved_promise(42),
|
||||||
|
pr::make_resolved_promise(10)};
|
||||||
|
}).then([&call_then_only_once, &check_42_int2](const int& v){
|
||||||
|
++call_then_only_once;
|
||||||
|
check_42_int2 = v;
|
||||||
|
});
|
||||||
|
REQUIRE(check_42_int == 42);
|
||||||
|
REQUIRE(check_42_int2 == 42);
|
||||||
|
REQUIRE(call_then_only_once == 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("then_tuple") {
|
||||||
|
{
|
||||||
|
double check_42_double = 0.0;
|
||||||
|
pr::make_resolved_promise()
|
||||||
|
.then_tuple([](){
|
||||||
|
return std::make_tuple(
|
||||||
|
pr::make_resolved_promise(32),
|
||||||
|
pr::make_resolved_promise(10.0));
|
||||||
|
}).then([&check_42_double](const std::tuple<int, double>& t){
|
||||||
|
check_42_double = std::get<0>(t) + std::get<1>(t);
|
||||||
|
});
|
||||||
|
REQUIRE(check_42_double == doctest::Approx(42.0).epsilon(0.01));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
double check_42_double = 0.0;
|
||||||
|
pr::make_resolved_promise(42)
|
||||||
|
.then_tuple([](int){
|
||||||
|
return std::make_tuple(
|
||||||
|
pr::make_resolved_promise(32),
|
||||||
|
pr::make_resolved_promise(10.0));
|
||||||
|
}).then([&check_42_double](const std::tuple<int, double>& t){
|
||||||
|
check_42_double = static_cast<double>(std::get<0>(t)) + std::get<1>(t);
|
||||||
|
});
|
||||||
|
REQUIRE(check_42_double == doctest::Approx(42.0).epsilon(0.01));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
pr::make_resolved_promise(42)
|
||||||
|
.then_tuple([](int){
|
||||||
|
return std::make_tuple(
|
||||||
|
pr::make_resolved_promise(32),
|
||||||
|
pr::make_rejected_promise<float>(std::logic_error("hello fail")));
|
||||||
|
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
return std::make_tuple(0, 0.f);
|
||||||
|
});
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
356
untests/promise4_tests.cpp
Normal file
356
untests/promise4_tests.cpp
Normal file
@@ -0,0 +1,356 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* This file is part of the "https://github.com/blackmatov/promise.hpp"
|
||||||
|
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||||
|
* Copyright (C) 2018-2023, by Matvey Cherevko (blackmatov@gmail.com)
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include <promise.hpp/promise.hpp>
|
||||||
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <thread>
|
||||||
|
#include <numeric>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
namespace pr = promise_hpp;
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct obj_t {
|
||||||
|
};
|
||||||
|
|
||||||
|
bool check_hello_fail_exception(std::exception_ptr e) {
|
||||||
|
try {
|
||||||
|
std::rethrow_exception(e);
|
||||||
|
} catch (std::logic_error& ee) {
|
||||||
|
return 0 == std::strcmp(ee.what(), "hello fail");
|
||||||
|
} catch (...) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class auto_thread final {
|
||||||
|
public:
|
||||||
|
template < typename F, typename... Args >
|
||||||
|
auto_thread(F&& f, Args&&... args)
|
||||||
|
: thread_(std::forward<F>(f), std::forward<Args>(args)...) {}
|
||||||
|
|
||||||
|
~auto_thread() noexcept {
|
||||||
|
if ( thread_.joinable() ) {
|
||||||
|
thread_.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void join() {
|
||||||
|
thread_.join();
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
std::thread thread_;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("get_and_wait") {
|
||||||
|
SUBCASE("get_void_promises") {
|
||||||
|
{
|
||||||
|
auto p = pr::make_resolved_promise();
|
||||||
|
REQUIRE_NOTHROW(p.get());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::make_rejected_promise<void>(std::logic_error("hello fail"));
|
||||||
|
REQUIRE_THROWS_AS(p.get(), std::logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::promise<void>();
|
||||||
|
auto_thread t{[p]() mutable {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||||
|
p.resolve();
|
||||||
|
}};
|
||||||
|
t.join();
|
||||||
|
REQUIRE_NOTHROW(p.get());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::promise<void>();
|
||||||
|
auto_thread t{[p]() mutable {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||||
|
p.resolve();
|
||||||
|
}};
|
||||||
|
REQUIRE_NOTHROW(p.get());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const auto time_now = [](){
|
||||||
|
return std::chrono::high_resolution_clock::now();
|
||||||
|
};
|
||||||
|
|
||||||
|
auto p1 = pr::make_resolved_promise();
|
||||||
|
REQUIRE_NOTHROW(p1.wait());
|
||||||
|
REQUIRE(p1.wait_for(std::chrono::milliseconds(-1)) == pr::promise_wait_status::no_timeout);
|
||||||
|
REQUIRE(p1.wait_for(std::chrono::milliseconds(0)) == pr::promise_wait_status::no_timeout);
|
||||||
|
REQUIRE(p1.wait_until(time_now() + std::chrono::milliseconds(-1)) == pr::promise_wait_status::no_timeout);
|
||||||
|
REQUIRE(p1.wait_until(time_now() + std::chrono::milliseconds(0)) == pr::promise_wait_status::no_timeout);
|
||||||
|
|
||||||
|
auto p2 = pr::make_resolved_promise<int>(5);
|
||||||
|
REQUIRE_NOTHROW(p2.wait());
|
||||||
|
REQUIRE(p2.wait_for(std::chrono::milliseconds(-1)) == pr::promise_wait_status::no_timeout);
|
||||||
|
REQUIRE(p2.wait_for(std::chrono::milliseconds(0)) == pr::promise_wait_status::no_timeout);
|
||||||
|
REQUIRE(p2.wait_until(time_now() + std::chrono::milliseconds(-1)) == pr::promise_wait_status::no_timeout);
|
||||||
|
REQUIRE(p2.wait_until(time_now() + std::chrono::milliseconds(0)) == pr::promise_wait_status::no_timeout);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::promise<void>();
|
||||||
|
auto_thread t{[p]() mutable {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||||
|
p.resolve();
|
||||||
|
}};
|
||||||
|
REQUIRE(p.wait_for(
|
||||||
|
std::chrono::milliseconds(5))
|
||||||
|
== pr::promise_wait_status::timeout);
|
||||||
|
REQUIRE(p.wait_for(
|
||||||
|
std::chrono::milliseconds(200))
|
||||||
|
== pr::promise_wait_status::no_timeout);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::promise<void>();
|
||||||
|
auto_thread t{[p]() mutable {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||||
|
p.resolve();
|
||||||
|
}};
|
||||||
|
REQUIRE(p.wait_until(
|
||||||
|
std::chrono::high_resolution_clock::now() + std::chrono::milliseconds(5))
|
||||||
|
== pr::promise_wait_status::timeout);
|
||||||
|
REQUIRE(p.wait_until(
|
||||||
|
std::chrono::high_resolution_clock::now() + std::chrono::milliseconds(200))
|
||||||
|
== pr::promise_wait_status::no_timeout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("get_typed_promises") {
|
||||||
|
{
|
||||||
|
auto p = pr::make_resolved_promise(42);
|
||||||
|
REQUIRE(p.get() == 42);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::make_rejected_promise<int>(std::logic_error("hello fail"));
|
||||||
|
REQUIRE_THROWS_AS(p.get(), std::logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
auto_thread t{[p]() mutable {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||||
|
p.resolve(42);
|
||||||
|
}};
|
||||||
|
t.join();
|
||||||
|
REQUIRE(p.get() == 42);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
auto_thread t{[p]() mutable {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||||
|
p.resolve(42);
|
||||||
|
}};
|
||||||
|
REQUIRE(p.get() == 42);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
auto_thread t{[p]() mutable {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||||
|
p.resolve(42);
|
||||||
|
}};
|
||||||
|
REQUIRE(p.wait_for(
|
||||||
|
std::chrono::milliseconds(5))
|
||||||
|
== pr::promise_wait_status::timeout);
|
||||||
|
REQUIRE(p.wait_for(
|
||||||
|
std::chrono::milliseconds(200))
|
||||||
|
== pr::promise_wait_status::no_timeout);
|
||||||
|
REQUIRE(p.get() == 42);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
auto_thread t{[p]() mutable {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||||
|
p.resolve(42);
|
||||||
|
}};
|
||||||
|
REQUIRE(p.wait_until(
|
||||||
|
std::chrono::high_resolution_clock::now() + std::chrono::milliseconds(5))
|
||||||
|
== pr::promise_wait_status::timeout);
|
||||||
|
REQUIRE(p.wait_until(
|
||||||
|
std::chrono::high_resolution_clock::now() + std::chrono::milliseconds(200))
|
||||||
|
== pr::promise_wait_status::no_timeout);
|
||||||
|
REQUIRE(p.get() == 42);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("get_or_default") {
|
||||||
|
{
|
||||||
|
auto p = pr::make_resolved_promise(42);
|
||||||
|
REQUIRE(p.get_or_default(84) == 42);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::make_rejected_promise<int>(std::logic_error("hello fail"));
|
||||||
|
REQUIRE(p.get_or_default(84) == 84);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
auto_thread t{[p]() mutable {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||||
|
p.resolve(42);
|
||||||
|
}};
|
||||||
|
REQUIRE(p.get_or_default(84) == 42);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::promise<int>();
|
||||||
|
auto_thread t{[p]() mutable {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||||
|
p.reject(std::logic_error("hello fail"));
|
||||||
|
}};
|
||||||
|
REQUIRE(p.get_or_default(84) == 84);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::make_resolved_promise();
|
||||||
|
REQUIRE_NOTHROW(p.get_or_default());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p = pr::make_rejected_promise<void>(std::logic_error("hello fail"));
|
||||||
|
REQUIRE_NOTHROW(p.get_or_default());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("promise_transformations") {
|
||||||
|
{
|
||||||
|
auto p_v = pr::promise<int>()
|
||||||
|
.then([](int){});
|
||||||
|
static_assert(
|
||||||
|
std::is_same<decltype(p_v)::value_type, void>::value,
|
||||||
|
"unit test fail");
|
||||||
|
|
||||||
|
auto p_f = pr::promise<int>()
|
||||||
|
.then([](int){return 1.f;});
|
||||||
|
static_assert(
|
||||||
|
std::is_same<decltype(p_f)::value_type, float>::value,
|
||||||
|
"unit test fail");
|
||||||
|
|
||||||
|
auto p_d = pr::promise<int>()
|
||||||
|
.then([](int){return 1.f;})
|
||||||
|
.then([](float){return 1.0;});
|
||||||
|
static_assert(
|
||||||
|
std::is_same<decltype(p_d)::value_type, double>::value,
|
||||||
|
"unit test fail");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p_v = pr::promise<void>()
|
||||||
|
.then([](){});
|
||||||
|
static_assert(
|
||||||
|
std::is_same<decltype(p_v)::value_type, void>::value,
|
||||||
|
"unit test fail");
|
||||||
|
|
||||||
|
auto p_f = pr::promise<void>()
|
||||||
|
.then([](){return 1.f;});
|
||||||
|
static_assert(
|
||||||
|
std::is_same<decltype(p_f)::value_type, float>::value,
|
||||||
|
"unit test fail");
|
||||||
|
|
||||||
|
auto p_d = pr::promise<void>()
|
||||||
|
.then([](){return 1.f;})
|
||||||
|
.then([](float){return 1.0;});
|
||||||
|
static_assert(
|
||||||
|
std::is_same<decltype(p_d)::value_type, double>::value,
|
||||||
|
"unit test fail");
|
||||||
|
}
|
||||||
|
SUBCASE("after_except") {
|
||||||
|
{
|
||||||
|
auto p_v = pr::promise<int>()
|
||||||
|
.then([](int)->int{
|
||||||
|
throw std::logic_error("hello fail");
|
||||||
|
})
|
||||||
|
.except([](std::exception_ptr){
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
static_assert(
|
||||||
|
std::is_same<decltype(p_v)::value_type, int>::value,
|
||||||
|
"unit test fail");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto p_v = pr::promise<int>()
|
||||||
|
.then([](int)->int{
|
||||||
|
throw std::logic_error("hello fail");
|
||||||
|
})
|
||||||
|
.except([](std::exception_ptr){
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
static_assert(
|
||||||
|
std::is_same<decltype(p_v)::value_type, int>::value,
|
||||||
|
"unit test fail");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("life_after_except") {
|
||||||
|
{
|
||||||
|
int check_42_int = 0;
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p = pr::make_rejected_promise<int>(std::logic_error("hello fail"));
|
||||||
|
p.then([](int v){
|
||||||
|
return v;
|
||||||
|
})
|
||||||
|
.except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
return 42;
|
||||||
|
})
|
||||||
|
.then([&check_42_int](int value){
|
||||||
|
check_42_int = value;
|
||||||
|
});
|
||||||
|
REQUIRE(check_42_int == 42);
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int check_42_int = 0;
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p = pr::make_rejected_promise<int>(std::logic_error("hello fail"));
|
||||||
|
p.then([](int v){
|
||||||
|
return v;
|
||||||
|
})
|
||||||
|
.except([&call_fail_with_logic_error](std::exception_ptr e) -> int {
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
std::rethrow_exception(e);
|
||||||
|
})
|
||||||
|
.except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = call_fail_with_logic_error && check_hello_fail_exception(e);
|
||||||
|
return 42;
|
||||||
|
})
|
||||||
|
.then([&check_42_int](int value){
|
||||||
|
check_42_int = value;
|
||||||
|
});
|
||||||
|
REQUIRE(check_42_int == 42);
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool call_then_after_except = false;
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
auto p = pr::make_rejected_promise(std::logic_error("hello fail"));
|
||||||
|
p.then([](){})
|
||||||
|
.except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
})
|
||||||
|
.then([&call_then_after_except](){
|
||||||
|
call_then_after_except = true;
|
||||||
|
});
|
||||||
|
REQUIRE(call_then_after_except);
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bool call_fail_with_logic_error = false;
|
||||||
|
bool call_then_after_multi_except = false;
|
||||||
|
auto p = pr::make_rejected_promise(std::logic_error("hello fail"));
|
||||||
|
p.then([](){})
|
||||||
|
.except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||||
|
std::rethrow_exception(e);
|
||||||
|
})
|
||||||
|
.except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||||
|
call_fail_with_logic_error = call_fail_with_logic_error && check_hello_fail_exception(e);
|
||||||
|
})
|
||||||
|
.then([&call_then_after_multi_except](){
|
||||||
|
call_then_after_multi_except = true;
|
||||||
|
});
|
||||||
|
REQUIRE(call_fail_with_logic_error);
|
||||||
|
REQUIRE(call_then_after_multi_except);
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -4,8 +4,8 @@
|
|||||||
* Copyright (C) 2018-2023, by Matvey Cherevko (blackmatov@gmail.com)
|
* Copyright (C) 2018-2023, by Matvey Cherevko (blackmatov@gmail.com)
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
#include <promise.hpp/scheduler.hpp>
|
#include <promise.hpp/bonus/scheduler.hpp>
|
||||||
#include "doctest/doctest.h"
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
|
|||||||
5
vendors/CMakeLists.txt
vendored
Normal file
5
vendors/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
project(promise.hpp.vendors)
|
||||||
|
|
||||||
|
set(DOCTEST_NO_INSTALL ON CACHE INTERNAL "")
|
||||||
|
add_subdirectory(doctest)
|
||||||
|
set_target_properties(doctest_with_main PROPERTIES FOLDER promise.hpp.vendors)
|
||||||
1
vendors/doctest
vendored
Submodule
1
vendors/doctest
vendored
Submodule
Submodule vendors/doctest added at b7c21ec5ce
Reference in New Issue
Block a user