doctest instead catch

This commit is contained in:
BlackMATov
2021-01-12 03:14:41 +07:00
parent 08f5e9ad9f
commit 47a67e08dc
8 changed files with 6320 additions and 82 deletions

View File

@@ -1,6 +1,3 @@
# 3.11 version is required for `FetchContent`
cmake_minimum_required(VERSION 3.11 FATAL_ERROR)
project(promise.hpp.untests) project(promise.hpp.untests)
# #
@@ -19,7 +16,7 @@ endif()
# executable # executable
# #
file(GLOB UNTESTS_SOURCES "*.cpp" "*.hpp") file(GLOB_RECURSE UNTESTS_SOURCES "*.cpp" "*.hpp")
add_executable(${PROJECT_NAME} ${UNTESTS_SOURCES}) add_executable(${PROJECT_NAME} ${UNTESTS_SOURCES})
target_link_libraries(${PROJECT_NAME} promise.hpp) target_link_libraries(${PROJECT_NAME} promise.hpp)
@@ -32,23 +29,3 @@ target_compile_options(${PROJECT_NAME}
-Wall -Wextra -Wpedantic>) -Wall -Wextra -Wpedantic>)
add_test(${PROJECT_NAME} ${PROJECT_NAME}) add_test(${PROJECT_NAME} ${PROJECT_NAME})
#
# dependencies
#
find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME} Threads::Threads)
include(FetchContent)
FetchContent_Declare(
catchorg_catch2
GIT_REPOSITORY https://github.com/catchorg/catch2
GIT_TAG v2.13.2)
FetchContent_GetProperties(catchorg_catch2)
if(NOT catchorg_catch2_POPULATED)
FetchContent_Populate(catchorg_catch2)
target_include_directories(${PROJECT_NAME}
PRIVATE ${catchorg_catch2_SOURCE_DIR}/single_include)
endif()

View File

@@ -1,9 +0,0 @@
/*******************************************************************************
* 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-2021, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#define CATCH_CONFIG_MAIN
#define CATCH_CONFIG_FAST_COMPILE
#include <catch2/catch.hpp>

View File

@@ -0,0 +1,2 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

6260
untests/doctest/doctest.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,11 @@
#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__));

View File

@@ -4,14 +4,13 @@
* Copyright (C) 2018-2021, by Matvey Cherevko (blackmatov@gmail.com) * Copyright (C) 2018-2021, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/ ******************************************************************************/
#define CATCH_CONFIG_FAST_COMPILE #include <promise.hpp/jobber.hpp>
#include <catch2/catch.hpp> #include "doctest/doctest.h"
#include <thread> #include <thread>
#include <numeric> #include <numeric>
#include <cstring> #include <cstring>
#include <promise.hpp/jobber.hpp>
namespace jb = jobber_hpp; namespace jb = jobber_hpp;
TEST_CASE("jobber") { TEST_CASE("jobber") {
@@ -66,8 +65,8 @@ TEST_CASE("jobber") {
auto p1 = j.async([](float angle){ auto p1 = j.async([](float angle){
return std::cos(angle); return std::cos(angle);
}, pi * 2); }, pi * 2);
REQUIRE(p0.get() == Approx(0.f).margin(0.01f)); REQUIRE(p0.get() == doctest::Approx(0.f).epsilon(0.01f));
REQUIRE(p1.get() == Approx(1.f).margin(0.01f)); REQUIRE(p1.get() == doctest::Approx(1.f).epsilon(0.01f));
} }
{ {
jb::jobber j(1); jb::jobber j(1);
@@ -324,6 +323,6 @@ TEST_CASE("jobber") {
for ( std::size_t i = 0; i < 50; ++i ) { for ( std::size_t i = 0; i < 50; ++i ) {
r1 += std::sin(static_cast<float>(i)); r1 += std::sin(static_cast<float>(i));
} }
REQUIRE(r0 == Approx(r1 * 50.f).margin(0.01f)); REQUIRE(r0 == doctest::Approx(r1 * 50.f).epsilon(0.01f));
} }
} }

View File

@@ -4,15 +4,14 @@
* Copyright (C) 2018-2021, by Matvey Cherevko (blackmatov@gmail.com) * Copyright (C) 2018-2021, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/ ******************************************************************************/
#define CATCH_CONFIG_FAST_COMPILE #include <promise.hpp/promise.hpp>
#include <catch2/catch.hpp> #include "doctest/doctest.h"
#include <array> #include <array>
#include <thread> #include <thread>
#include <numeric> #include <numeric>
#include <cstring> #include <cstring>
#include <promise.hpp/promise.hpp>
namespace pr = promise_hpp; namespace pr = promise_hpp;
namespace namespace
@@ -75,7 +74,7 @@ namespace
} }
TEST_CASE("is_promise") { TEST_CASE("is_promise") {
SECTION("positive") { SUBCASE("positive") {
static_assert( static_assert(
pr::is_promise<pr::promise<void>>::value, pr::is_promise<pr::promise<void>>::value,
"unit test fail"); "unit test fail");
@@ -96,7 +95,7 @@ TEST_CASE("is_promise") {
pr::is_promise<const volatile pr::promise<int>>::value, pr::is_promise<const volatile pr::promise<int>>::value,
"unit test fail"); "unit test fail");
} }
SECTION("negative") { SUBCASE("negative") {
static_assert( static_assert(
!pr::is_promise<pr::promise<void>&>::value, !pr::is_promise<pr::promise<void>&>::value,
"unit test fail"); "unit test fail");
@@ -120,7 +119,7 @@ TEST_CASE("is_promise") {
} }
TEST_CASE("is_promise_r") { TEST_CASE("is_promise_r") {
SECTION("positive") { SUBCASE("positive") {
static_assert( static_assert(
pr::is_promise_r<void, pr::promise<void>>::value, pr::is_promise_r<void, pr::promise<void>>::value,
"unit test fail"); "unit test fail");
@@ -131,7 +130,7 @@ TEST_CASE("is_promise_r") {
pr::is_promise_r<double, const pr::promise<int>>::value, pr::is_promise_r<double, const pr::promise<int>>::value,
"unit test fail"); "unit test fail");
} }
SECTION("negative") { SUBCASE("negative") {
static_assert( static_assert(
!pr::is_promise_r<void, pr::promise<int>>::value, !pr::is_promise_r<void, pr::promise<int>>::value,
"unit test fail"); "unit test fail");
@@ -148,7 +147,7 @@ TEST_CASE("is_promise_r") {
} }
TEST_CASE("promise") { TEST_CASE("promise") {
SECTION("basic") { SUBCASE("basic") {
{ {
auto p1 = pr::promise<int>(); auto p1 = pr::promise<int>();
auto p2 = pr::promise<int>(); auto p2 = pr::promise<int>();
@@ -194,7 +193,7 @@ TEST_CASE("promise") {
REQUIRE_FALSE(p1 == p3); REQUIRE_FALSE(p1 == p3);
} }
} }
SECTION("resolved") { SUBCASE("resolved") {
{ {
int check_42_int = 0; int check_42_int = 0;
auto p = pr::promise<int>(); auto p = pr::promise<int>();
@@ -226,7 +225,7 @@ TEST_CASE("promise") {
REQUIRE(check_100500_transform == 100500); REQUIRE(check_100500_transform == 100500);
} }
} }
SECTION("resolved_ref") { SUBCASE("resolved_ref") {
{ {
int* check_42_int = nullptr; int* check_42_int = nullptr;
auto p = pr::promise<std::reference_wrapper<int>>(); auto p = pr::promise<std::reference_wrapper<int>>();
@@ -250,7 +249,7 @@ TEST_CASE("promise") {
REQUIRE(*check_42_int == 42); REQUIRE(*check_42_int == 42);
} }
} }
SECTION("rejected") { SUBCASE("rejected") {
{ {
bool call_fail_with_logic_error = false; bool call_fail_with_logic_error = false;
bool not_call_then_on_reject = true; bool not_call_then_on_reject = true;
@@ -300,7 +299,7 @@ TEST_CASE("promise") {
REQUIRE(check_multi_fail == 2); REQUIRE(check_multi_fail == 2);
} }
} }
SECTION("unresolved") { SUBCASE("unresolved") {
{ {
int check_42_int = 0; int check_42_int = 0;
bool not_call_before_resolve = true; bool not_call_before_resolve = true;
@@ -334,7 +333,7 @@ TEST_CASE("promise") {
REQUIRE(call_fail_with_logic_error); REQUIRE(call_fail_with_logic_error);
} }
} }
SECTION("finally") { SUBCASE("finally") {
{ {
bool all_is_ok = false; bool all_is_ok = false;
auto p = pr::promise<int>(); auto p = pr::promise<int>();
@@ -372,7 +371,7 @@ TEST_CASE("promise") {
REQUIRE(all_is_ok); REQUIRE(all_is_ok);
} }
} }
SECTION("after_finally") { SUBCASE("after_finally") {
{ {
int check_84_int = 0; int check_84_int = 0;
auto p = pr::promise<>(); auto p = pr::promise<>();
@@ -400,7 +399,7 @@ TEST_CASE("promise") {
REQUIRE(check_84_int == 84); REQUIRE(check_84_int == 84);
} }
} }
SECTION("failed_finally") { SUBCASE("failed_finally") {
{ {
int check_84_int = 0; int check_84_int = 0;
auto p = pr::promise<>(); auto p = pr::promise<>();
@@ -460,7 +459,7 @@ TEST_CASE("promise") {
REQUIRE(check_84_int == 84); REQUIRE(check_84_int == 84);
} }
} }
SECTION("make_promise") { SUBCASE("make_promise") {
{ {
int check_84_int = 0; int check_84_int = 0;
auto p = pr::make_promise<int>([](auto resolve, auto reject){ auto p = pr::make_promise<int>([](auto resolve, auto reject){
@@ -500,7 +499,7 @@ TEST_CASE("promise") {
REQUIRE(call_fail_with_logic_error); REQUIRE(call_fail_with_logic_error);
} }
} }
SECTION("make_resolved_promise") { SUBCASE("make_resolved_promise") {
{ {
bool call_check = false; bool call_check = false;
pr::make_resolved_promise() pr::make_resolved_promise()
@@ -518,7 +517,7 @@ TEST_CASE("promise") {
REQUIRE(check_42_int == 42); REQUIRE(check_42_int == 42);
} }
} }
SECTION("make_rejected_promise") { SUBCASE("make_rejected_promise") {
{ {
bool call_fail_with_logic_error = false; bool call_fail_with_logic_error = false;
pr::make_rejected_promise<int>(std::logic_error("hello fail")) pr::make_rejected_promise<int>(std::logic_error("hello fail"))
@@ -537,7 +536,7 @@ TEST_CASE("promise") {
REQUIRE(call_fail_with_logic_error); REQUIRE(call_fail_with_logic_error);
} }
} }
SECTION("exceptions") { SUBCASE("exceptions") {
{ {
bool not_call_then_on_reject = true; bool not_call_then_on_reject = true;
bool call_fail_with_logic_error = false; bool call_fail_with_logic_error = false;
@@ -569,7 +568,7 @@ TEST_CASE("promise") {
REQUIRE(call_fail_with_logic_error); REQUIRE(call_fail_with_logic_error);
} }
} }
SECTION("multi_then") { SUBCASE("multi_then") {
{ {
auto p = pr::promise<int>(); auto p = pr::promise<int>();
@@ -631,7 +630,7 @@ TEST_CASE("promise") {
REQUIRE(pb_value == 21); REQUIRE(pb_value == 21);
} }
} }
SECTION("chaining") { SUBCASE("chaining") {
{ {
int check_84_int = 0; int check_84_int = 0;
auto p1 = pr::make_resolved_promise(42); auto p1 = pr::make_resolved_promise(42);
@@ -687,7 +686,7 @@ TEST_CASE("promise") {
REQUIRE(check_84_int == 84); REQUIRE(check_84_int == 84);
} }
} }
SECTION("lazy_chaining") { SUBCASE("lazy_chaining") {
{ {
int check_84_int = 0; int check_84_int = 0;
auto p1 = pr::make_promise<int>(); auto p1 = pr::make_promise<int>();
@@ -707,7 +706,7 @@ TEST_CASE("promise") {
REQUIRE(check_84_int == 84); REQUIRE(check_84_int == 84);
} }
} }
SECTION("typed_chaining_fails") { SUBCASE("typed_chaining_fails") {
{ {
bool call_fail_with_logic_error = false; bool call_fail_with_logic_error = false;
auto p1 = pr::make_resolved_promise(42); auto p1 = pr::make_resolved_promise(42);
@@ -773,7 +772,7 @@ TEST_CASE("promise") {
REQUIRE(call_fail_with_logic_error); REQUIRE(call_fail_with_logic_error);
} }
} }
SECTION("void_chaining_fails") { SUBCASE("void_chaining_fails") {
{ {
bool call_fail_with_logic_error = false; bool call_fail_with_logic_error = false;
auto p1 = pr::make_resolved_promise(); auto p1 = pr::make_resolved_promise();
@@ -831,7 +830,7 @@ TEST_CASE("promise") {
REQUIRE(call_fail_with_logic_error); REQUIRE(call_fail_with_logic_error);
} }
} }
SECTION("make_all_promise") { SUBCASE("make_all_promise") {
{ {
bool all_is_ok = false; bool all_is_ok = false;
pr::make_all_promise(std::vector<pr::promise<int>>()) pr::make_all_promise(std::vector<pr::promise<int>>())
@@ -913,7 +912,7 @@ TEST_CASE("promise") {
}); });
} }
} }
SECTION("make_any_promise") { SUBCASE("make_any_promise") {
{ {
bool all_is_ok = false; bool all_is_ok = false;
auto p = pr::make_any_promise(std::vector<pr::promise<int>>{}); auto p = pr::make_any_promise(std::vector<pr::promise<int>>{});
@@ -973,7 +972,7 @@ TEST_CASE("promise") {
REQUIRE(all_is_ok); REQUIRE(all_is_ok);
} }
} }
SECTION("make_race_promise") { SUBCASE("make_race_promise") {
{ {
auto p1 = pr::promise<int>(); auto p1 = pr::promise<int>();
auto p2 = pr::promise<int>(); auto p2 = pr::promise<int>();
@@ -1049,7 +1048,7 @@ TEST_CASE("promise") {
}); });
} }
} }
SECTION("make_tuple_promise") { SUBCASE("make_tuple_promise") {
{ {
static_assert( static_assert(
std::is_same< std::is_same<
@@ -1131,7 +1130,7 @@ TEST_CASE("promise") {
}); });
} }
} }
SECTION("make_all_promise_fail") { SUBCASE("make_all_promise_fail") {
{ {
bool call_fail_with_logic_error = false; bool call_fail_with_logic_error = false;
bool not_call_then_on_reject = true; bool not_call_then_on_reject = true;
@@ -1169,7 +1168,7 @@ TEST_CASE("promise") {
REQUIRE(call_except_count == 1); REQUIRE(call_except_count == 1);
} }
} }
SECTION("make_race_promise_fail") { SUBCASE("make_race_promise_fail") {
{ {
bool call_fail_with_logic_error = false; bool call_fail_with_logic_error = false;
bool not_call_then_on_reject = true; bool not_call_then_on_reject = true;
@@ -1186,7 +1185,7 @@ TEST_CASE("promise") {
REQUIRE(call_fail_with_logic_error); REQUIRE(call_fail_with_logic_error);
} }
} }
SECTION("make_tuple_promise_fail") { SUBCASE("make_tuple_promise_fail") {
{ {
auto p1 = pr::promise<int>(); auto p1 = pr::promise<int>();
auto p2 = pr::make_tuple_promise(std::make_tuple(p1)); auto p2 = pr::make_tuple_promise(std::make_tuple(p1));
@@ -1224,7 +1223,7 @@ TEST_CASE("promise") {
REQUIRE_THROWS_AS(p3.get(), std::logic_error); REQUIRE_THROWS_AS(p3.get(), std::logic_error);
} }
} }
SECTION("then_all") { SUBCASE("then_all") {
{ {
int check_42_int = 0; int check_42_int = 0;
pr::make_resolved_promise() pr::make_resolved_promise()
@@ -1257,7 +1256,7 @@ TEST_CASE("promise") {
REQUIRE(check_42_int2 == 42); REQUIRE(check_42_int2 == 42);
} }
} }
SECTION("then_race") { SUBCASE("then_race") {
{ {
int check_42_int = 0; int check_42_int = 0;
pr::make_resolved_promise() pr::make_resolved_promise()
@@ -1302,7 +1301,7 @@ TEST_CASE("promise") {
REQUIRE(call_then_only_once == 1); REQUIRE(call_then_only_once == 1);
} }
} }
SECTION("then_tuple") { SUBCASE("then_tuple") {
{ {
float check_42_float = 0.f; float check_42_float = 0.f;
pr::make_resolved_promise() pr::make_resolved_promise()
@@ -1313,7 +1312,7 @@ TEST_CASE("promise") {
}).then([&check_42_float](const std::tuple<int, float>& t){ }).then([&check_42_float](const std::tuple<int, float>& t){
check_42_float = std::get<0>(t) + std::get<1>(t); check_42_float = std::get<0>(t) + std::get<1>(t);
}); });
REQUIRE(check_42_float == Approx(42.f).margin(0.01f)); REQUIRE(check_42_float == doctest::Approx(42.f).epsilon(0.01f));
} }
{ {
float check_42_float = 0.f; float check_42_float = 0.f;
@@ -1325,7 +1324,7 @@ TEST_CASE("promise") {
}).then([&check_42_float](const std::tuple<int, float>& t){ }).then([&check_42_float](const std::tuple<int, float>& t){
check_42_float = std::get<0>(t) + std::get<1>(t); check_42_float = std::get<0>(t) + std::get<1>(t);
}); });
REQUIRE(check_42_float == Approx(42.f).margin(0.01f)); REQUIRE(check_42_float == doctest::Approx(42.f).epsilon(0.01f));
} }
{ {
bool call_fail_with_logic_error = false; bool call_fail_with_logic_error = false;
@@ -1344,7 +1343,7 @@ TEST_CASE("promise") {
} }
TEST_CASE("get_and_wait") { TEST_CASE("get_and_wait") {
SECTION("get_void_promises") { SUBCASE("get_void_promises") {
{ {
auto p = pr::make_resolved_promise(); auto p = pr::make_resolved_promise();
REQUIRE_NOTHROW(p.get()); REQUIRE_NOTHROW(p.get());
@@ -1416,7 +1415,7 @@ TEST_CASE("get_and_wait") {
== pr::promise_wait_status::no_timeout); == pr::promise_wait_status::no_timeout);
} }
} }
SECTION("get_typed_promises") { SUBCASE("get_typed_promises") {
{ {
auto p = pr::make_resolved_promise(42); auto p = pr::make_resolved_promise(42);
REQUIRE(p.get() == 42); REQUIRE(p.get() == 42);
@@ -1471,7 +1470,7 @@ TEST_CASE("get_and_wait") {
REQUIRE(p.get() == 42); REQUIRE(p.get() == 42);
} }
} }
SECTION("get_or_default") { SUBCASE("get_or_default") {
{ {
auto p = pr::make_resolved_promise(42); auto p = pr::make_resolved_promise(42);
REQUIRE(p.get_or_default(84) == 42); REQUIRE(p.get_or_default(84) == 42);
@@ -1548,7 +1547,7 @@ TEST_CASE("promise_transformations") {
std::is_same<decltype(p_d)::value_type, double>::value, std::is_same<decltype(p_d)::value_type, double>::value,
"unit test fail"); "unit test fail");
} }
SECTION("after_except") { SUBCASE("after_except") {
{ {
auto p_v = pr::promise<int>() auto p_v = pr::promise<int>()
.then([](int)->int{ .then([](int)->int{

View File

@@ -4,14 +4,13 @@
* Copyright (C) 2018-2021, by Matvey Cherevko (blackmatov@gmail.com) * Copyright (C) 2018-2021, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/ ******************************************************************************/
#define CATCH_CONFIG_FAST_COMPILE #include <promise.hpp/scheduler.hpp>
#include <catch2/catch.hpp> #include "doctest/doctest.h"
#include <thread> #include <thread>
#include <numeric> #include <numeric>
#include <cstring> #include <cstring>
#include <promise.hpp/scheduler.hpp>
namespace sd = scheduler_hpp; namespace sd = scheduler_hpp;
TEST_CASE("scheduler") { TEST_CASE("scheduler") {