Move tests from projects/ to tests/

This commit is contained in:
Martin Hořeňovský
2019-12-05 16:00:20 +01:00
parent 90e2549cec
commit 0fea081ad1
84 changed files with 2429 additions and 2429 deletions

View File

@@ -0,0 +1,158 @@
#
# Build extra tests.
#
# Requires CATCH_BUILD_EXTRA_TESTS to be defined 'true', see ../CMakeLists.txt.
#
cmake_minimum_required( VERSION 3.5 )
project( Catch2ExtraTests LANGUAGES CXX )
message( STATUS "Extra tests included" )
# define folders used:
set( TESTS_DIR ${CATCH_DIR}/projects/ExtraTests )
set( SINGLE_INCLUDE_PATH ${CATCH_DIR}/single_include )
add_executable(PrefixedMacros ${TESTS_DIR}/X01-PrefixedMacros.cpp)
target_compile_definitions( PrefixedMacros PRIVATE CATCH_CONFIG_PREFIX_ALL )
add_test(NAME CATCH_CONFIG_PREFIX_ALL COMMAND PrefixedMacros -s)
set_tests_properties(
CATCH_CONFIG_PREFIX_ALL
PROPERTIES
PASS_REGULAR_EXPRESSION "CATCH_"
FAIL_REGULAR_EXPRESSION
# The spaces are important -> They disambiguate between CATCH_REQUIRE
# and REQUIRE without prefix.
" REQUIRE; REQUIRE_FALSE; REQUIRE_THROWS; REQUIRE_THROWS_AS; REQUIRE_THROWS_WITH; REQUIRE_THROWS_MATCHES; REQUIRE_NOTHROW; CHECK; CHECK_FALSE; CHECKED_IF; CHECKED_ELSE; CHECK_NOFAIL; CHECK_THROWS; CHECK_THROWS_AS; CHECK_THROWS_WITH; CHECK_THROWS_MATCHES; CHECK_NOTHROW; REQUIRE_THAT; CHECK_THAT"
)
add_executable(DisabledMacros ${TESTS_DIR}/X02-DisabledMacros.cpp)
target_compile_definitions( DisabledMacros PRIVATE CATCH_CONFIG_DISABLE )
add_test(NAME CATCH_CONFIG_DISABLE-1 COMMAND DisabledMacros -s)
set_tests_properties(
CATCH_CONFIG_DISABLE-1
PROPERTIES
PASS_REGULAR_EXPRESSION "No tests ran"
FAIL_REGULAR_EXPRESSION "This should not happen"
)
add_test(NAME CATCH_CONFIG_DISABLE-2 COMMAND DisabledMacros --list-tests)
set_tests_properties(
CATCH_CONFIG_DISABLE-2
PROPERTIES
PASS_REGULAR_EXPRESSION "0 test cases"
)
add_executable( DisabledExceptions-DefaultHandler ${TESTS_DIR}/X03-DisabledExceptions-DefaultHandler.cpp )
add_executable( DisabledExceptions-CustomHandler ${TESTS_DIR}/X04-DisabledExceptions-CustomHandler.cpp )
foreach(target DisabledExceptions-DefaultHandler DisabledExceptions-CustomHandler)
target_compile_options( ${target}
PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/EHs-c-;/D_HAS_EXCEPTIONS=0>
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:AppleClang>>:-fno-exceptions>
# $<$<CXX_COMPILER_ID:Clang>:-fno-exceptions>
# $<$<CXX_COMPILER_ID:GNU>:-fno-exceptions>
)
endforeach()
add_test(NAME CATCH_CONFIG_DISABLE_EXCEPTIONS-1 COMMAND DisabledExceptions-DefaultHandler "Tests that run")
set_tests_properties(
CATCH_CONFIG_DISABLE_EXCEPTIONS-1
PROPERTIES
PASS_REGULAR_EXPRESSION "assertions: 4 \| 2 passed \| 2 failed"
FAIL_REGULAR_EXPRESSION "abort;terminate;fatal"
)
add_test(NAME CATCH_CONFIG_DISABLE_EXCEPTIONS-2 COMMAND DisabledExceptions-DefaultHandler "Tests that abort")
set_tests_properties(
CATCH_CONFIG_DISABLE_EXCEPTIONS-2
PROPERTIES
PASS_REGULAR_EXPRESSION "Catch will terminate"
)
add_test(NAME CATCH_CONFIG_DISABLE_EXCEPTIONS-3 COMMAND DisabledExceptions-CustomHandler "Tests that run")
set_tests_properties(
CATCH_CONFIG_DISABLE_EXCEPTIONS-3
PROPERTIES
PASS_REGULAR_EXPRESSION "assertions: 4 \| 2 passed \| 2 failed"
FAIL_REGULAR_EXPRESSION "====== CUSTOM HANDLER ======"
)
add_test(NAME CATCH_CONFIG_DISABLE_EXCEPTIONS-4 COMMAND DisabledExceptions-CustomHandler "Tests that abort")
set_tests_properties(
CATCH_CONFIG_DISABLE_EXCEPTIONS-4
PROPERTIES
PASS_REGULAR_EXPRESSION "====== CUSTOM HANDLER ======"
)
add_executable(FallbackStringifier ${TESTS_DIR}/X10-FallbackStringifier.cpp)
target_compile_definitions( FallbackStringifier PRIVATE CATCH_CONFIG_FALLBACK_STRINGIFIER=fallbackStringifier )
add_test(NAME FallbackStringifier COMMAND FallbackStringifier -r compact -s)
set_tests_properties(
FallbackStringifier
PROPERTIES
PASS_REGULAR_EXPRESSION "foo{} for: { !!! }"
)
add_executable(DisableStringification ${TESTS_DIR}/X11-DisableStringification.cpp)
target_compile_definitions( DisableStringification PRIVATE CATCH_CONFIG_DISABLE_STRINGIFICATION )
add_test(NAME CATCH_CONFIG_DISABLE_STRINGIFICATION COMMAND DisableStringification -r compact -s)
set_tests_properties(
CATCH_CONFIG_DISABLE_STRINGIFICATION
PROPERTIES
PASS_REGULAR_EXPRESSION "Disabled by CATCH_CONFIG_DISABLE_STRINGIFICATION"
FAIL_REGULAR_EXPRESSION "Hidden{} == Hidden{}"
)
add_executable(BenchmarkingMacros ${TESTS_DIR}/X20-BenchmarkingMacros.cpp)
target_compile_definitions( BenchmarkingMacros PRIVATE CATCH_CONFIG_ENABLE_BENCHMARKING )
add_test(NAME BenchmarkingMacros COMMAND BenchmarkingMacros -r console -s)
set_tests_properties(
BenchmarkingMacros
PROPERTIES
PASS_REGULAR_EXPRESSION "benchmark name samples iterations estimated"
)
# This test touches windows.h, so it should only be compiled under msvc
if (MSVC)
# This test fails if it does not compile and succeeds otherwise
add_executable(WindowsHeader ${TESTS_DIR}/X90-WindowsHeaderInclusion.cpp)
set_property( TARGET WindowsHeader PROPERTY CXX_STANDARD 14 )
set_property( TARGET WindowsHeader PROPERTY CXX_STANDARD_REQUIRED ON )
set_property( TARGET WindowsHeader PROPERTY CXX_EXTENSIONS OFF )
target_include_directories( WindowsHeader PRIVATE ${SINGLE_INCLUDE_PATH} )
add_test(NAME WindowsHeader COMMAND WindowsHeader -r compact)
endif()
set( EXTRA_TEST_BINARIES
PrefixedMacros
DisabledMacros
DisabledExceptions-DefaultHandler
DisabledExceptions-CustomHandler
FallbackStringifier
DisableStringification
BenchmarkingMacros
)
# Shared config
foreach( test ${EXTRA_TEST_BINARIES} )
set_property( TARGET ${test} PROPERTY CXX_STANDARD 14 )
set_property( TARGET ${test} PROPERTY CXX_STANDARD_REQUIRED ON )
set_property( TARGET ${test} PROPERTY CXX_EXTENSIONS OFF )
target_include_directories( ${test} PRIVATE ${SINGLE_INCLUDE_PATH} )
endforeach()

11
tests/ExtraTests/ToDo.txt Normal file
View File

@@ -0,0 +1,11 @@
Configuration options that are left default and thus are not properly tested
yet:
CATCH_CONFIG_COUNTER // Use __COUNTER__ to generate unique names for test cases
CATCH_CONFIG_WINDOWS_SEH // Enable SEH handling on Windows
CATCH_CONFIG_FAST_COMPILE // Sacrifices some (rather minor) features for compilation speed
CATCH_CONFIG_DISABLE_MATCHERS // Do not compile Matchers in this compilation unit
CATCH_CONFIG_POSIX_SIGNALS // Enable handling POSIX signals
CATCH_CONFIG_WINDOWS_CRTDBG // Enable leak checking using Windows's CRT Debug Heap
CATCH_CONFIG_DEFAULT_REPORTER
CATCH_CONFIG_ENABLE_ALL_STRINGMAKERS

View File

@@ -0,0 +1,81 @@
// X11-DisableStringification.cpp
// Test that Catch's prefixed macros compile and run properly.
#define CATCH_CONFIG_MAIN
// This won't provide full coverage, but it might be worth checking
// the other branch as well
#define CATCH_CONFIG_RUNTIME_STATIC_REQUIRE
#include <catch2/catch.hpp>
#include <type_traits>
#include <stdexcept>
[[noreturn]]
void this_throws() {
throw std::runtime_error("Some msg");
}
void this_doesnt_throw() {}
CATCH_TEST_CASE("PrefixedMacros") {
using namespace Catch::Matchers;
CATCH_REQUIRE( 1 == 1 );
CATCH_REQUIRE_FALSE( 1 != 1 );
CATCH_REQUIRE_THROWS(this_throws());
CATCH_REQUIRE_THROWS_AS(this_throws(), std::runtime_error);
CATCH_REQUIRE_THROWS_WITH(this_throws(), "Some msg");
CATCH_REQUIRE_THROWS_MATCHES(this_throws(), std::runtime_error, Predicate<std::runtime_error>([](std::runtime_error const&) { return true; }));
CATCH_REQUIRE_NOTHROW(this_doesnt_throw());
CATCH_CHECK( 1 == 1 );
CATCH_CHECK_FALSE( 1 != 1 );
CATCH_CHECKED_IF( 1 == 1 ) {
CATCH_SUCCEED("don't care");
} CATCH_CHECKED_ELSE ( 1 == 1 ) {
CATCH_SUCCEED("don't care");
}
CATCH_CHECK_NOFAIL(1 == 2);
CATCH_CHECK_THROWS(this_throws());
CATCH_CHECK_THROWS_AS(this_throws(), std::runtime_error);
CATCH_CHECK_THROWS_WITH(this_throws(), "Some msg");
CATCH_CHECK_THROWS_MATCHES(this_throws(), std::runtime_error, Predicate<std::runtime_error>([](std::runtime_error const&) { return true; }));
CATCH_CHECK_NOTHROW(this_doesnt_throw());
CATCH_REQUIRE_THAT("abcd", Equals("abcd"));
CATCH_CHECK_THAT("bdef", Equals("bdef"));
CATCH_INFO( "some info" );
CATCH_UNSCOPED_INFO( "some info" );
CATCH_WARN( "some warn" );
CATCH_SECTION("some section") {
int i = 1;
CATCH_CAPTURE( i );
CATCH_DYNAMIC_SECTION("Dynamic section: " << i) {
CATCH_FAIL_CHECK( "failure" );
}
}
CATCH_STATIC_REQUIRE( std::is_void<void>::value );
CATCH_STATIC_REQUIRE_FALSE( std::is_void<int>::value );
CATCH_FAIL("");
}
// Missing:
//
// #define CATCH_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ )
// #define CATCH_METHOD_AS_TEST_CASE( method, ... ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, __VA_ARGS__ )
// #define CATCH_REGISTER_TEST_CASE( Function, ... ) INTERNAL_CATCH_REGISTER_TESTCASE( Function, __VA_ARGS__ )
//
// // "BDD-style" convenience wrappers
// #define CATCH_SCENARIO( ... ) CATCH_TEST_CASE( "Scenario: " __VA_ARGS__ )
// #define CATCH_SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, "Scenario: " __VA_ARGS__ )
// #define CATCH_GIVEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Given: " << desc )
// #define CATCH_WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " When: " << desc )
// #define CATCH_AND_WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( "And when: " << desc )
// #define CATCH_THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Then: " << desc )
// #define CATCH_AND_THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " And: " << desc )
//

View File

@@ -0,0 +1,31 @@
// X02-DisabledMacros.cpp
// Test that CATCH_CONFIG_DISABLE turns off TEST_CASE autoregistration
// and expressions in assertion macros are not run.
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
// CATCH_CONFIG_DISABLE also prevents reporter registration.
// We need to manually register at least one reporter for our tests
static Catch::ReporterRegistrar<Catch::ConsoleReporter> temporary( "console" );
#include <iostream>
struct foo {
foo(){
REQUIRE_NOTHROW( print() );
}
void print() const {
std::cout << "This should not happen\n";
}
};
// Construct foo, but `foo::print` should not be run
foo f;
// This test should not be run, because it won't be registered
TEST_CASE( "Disabled Macros" ) {
std::cout << "This should not happen\n";
FAIL();
}

View File

@@ -0,0 +1,23 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
TEST_CASE("Tests that run") {
// All of these should be run and be reported
CHECK(1 == 2);
CHECK(1 == 1);
CHECK(1 != 3);
CHECK(1 == 4);
}
TEST_CASE("Tests that abort") {
// Avoid abort and other exceptional exits -- there is no way
// to tell CMake that abort is the desired outcome of a test.
std::set_terminate([](){exit(1);});
REQUIRE(1 == 1);
REQUIRE(1 != 2);
REQUIRE(1 == 3);
// We should not get here, because the test above aborts
REQUIRE(1 != 4);
}

View File

@@ -0,0 +1,33 @@
#define CATCH_CONFIG_DISABLE_EXCEPTIONS_CUSTOM_HANDLER
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
namespace Catch {
[[noreturn]]
void throw_exception(std::exception const& e) {
Catch::cerr() << "====== CUSTOM HANDLER ====== run terminates because an exception was thrown.\n"
<< "The message was: " << e.what() << '\n';
// Avoid abort and other exceptional exits -- there is no way
// to tell CMake that abort is the desired outcome of a test.
exit(1);
}
}
TEST_CASE("Tests that run") {
// All of these should be run and be reported
CHECK(1 == 2);
CHECK(1 == 1);
CHECK(1 != 3);
CHECK(1 == 4);
}
TEST_CASE("Tests that abort") {
REQUIRE(1 == 1);
REQUIRE(1 != 2);
REQUIRE(1 == 3);
// We should not get here, because the test above aborts
REQUIRE(1 != 4);
}

View File

@@ -0,0 +1,23 @@
// X10-FallbackStringifier.cpp
// Test that defining fallbackStringifier compiles
#include <string>
// A catch-all stringifier
template <typename T>
std::string fallbackStringifier(T const&) {
return "{ !!! }";
}
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
struct foo {
explicit operator bool() const {
return true;
}
};
TEST_CASE("aa") {
REQUIRE(foo{});
}

View File

@@ -0,0 +1,16 @@
// X11-DisableStringification.cpp
// Test that stringification of original expression can be disabled
// this is a workaround for VS 2017 issue with Raw String literal
// and preprocessor token pasting. In other words, hopefully this test
// will be deleted soon :-)
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
struct Hidden {};
bool operator==(Hidden, Hidden) { return true; }
TEST_CASE("DisableStringification") {
REQUIRE( Hidden{} == Hidden{} );
}

View File

@@ -0,0 +1,133 @@
// X20-BenchmarkingMacros.cpp
// Test that the benchmarking support macros compile properly with the single header
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
namespace {
std::uint64_t factorial(std::uint64_t number) {
if (number < 2) {
return 1;
}
return number * factorial(number - 1);
}
}
TEST_CASE("Benchmark factorial", "[benchmark]") {
CHECK(factorial(0) == 1);
// some more asserts..
CHECK(factorial(10) == 3628800);
BENCHMARK("factorial 10") {
return factorial(10);
};
CHECK(factorial(14) == 87178291200ull);
BENCHMARK("factorial 14") {
return factorial(14);
};
//
// BENCHMARK("factorial 20") {
// return factorial(20);
// };
//
// BENCHMARK("factorial 35") {
// return factorial(35);
// };
}
TEST_CASE("Benchmark containers", "[.][benchmark]") {
static const int size = 100;
std::vector<int> v;
std::map<int, int> m;
SECTION("without generator") {
BENCHMARK("Load up a vector") {
v = std::vector<int>();
for (int i = 0; i < size; ++i)
v.push_back(i);
};
REQUIRE(v.size() == size);
// test optimizer control
BENCHMARK("Add up a vector's content") {
uint64_t add = 0;
for (int i = 0; i < size; ++i)
add += v[i];
return add;
};
BENCHMARK("Load up a map") {
m = std::map<int, int>();
for (int i = 0; i < size; ++i)
m.insert({ i, i + 1 });
};
REQUIRE(m.size() == size);
BENCHMARK("Reserved vector") {
v = std::vector<int>();
v.reserve(size);
for (int i = 0; i < size; ++i)
v.push_back(i);
};
REQUIRE(v.size() == size);
BENCHMARK("Resized vector") {
v = std::vector<int>();
v.resize(size);
for (int i = 0; i < size; ++i)
v[i] = i;
};
REQUIRE(v.size() == size);
int array[size];
BENCHMARK("A fixed size array that should require no allocations") {
for (int i = 0; i < size; ++i)
array[i] = i;
};
int sum = 0;
for (int i = 0; i < size; ++i)
sum += array[i];
REQUIRE(sum > size);
SECTION("XYZ") {
BENCHMARK_ADVANCED("Load up vector with chronometer")(Catch::Benchmark::Chronometer meter) {
std::vector<int> k;
meter.measure([&](int idx) {
k = std::vector<int>();
for (int i = 0; i < size; ++i)
k.push_back(idx);
});
REQUIRE(k.size() == size);
};
int runs = 0;
BENCHMARK("Fill vector indexed", benchmarkIndex) {
v = std::vector<int>();
v.resize(size);
for (int i = 0; i < size; ++i)
v[i] = benchmarkIndex;
runs = benchmarkIndex;
};
for (size_t i = 0; i < v.size(); ++i) {
REQUIRE(v[i] == runs);
}
}
}
SECTION("with generator") {
auto generated = GENERATE(range(0, 10));
BENCHMARK("Fill vector generated") {
v = std::vector<int>();
v.resize(size);
for (int i = 0; i < size; ++i)
v[i] = generated;
};
for (size_t i = 0; i < v.size(); ++i) {
REQUIRE(v[i] == generated);
}
}
}

View File

@@ -0,0 +1,12 @@
// X90-WindowsHeaderInclusion.cpp
// Test that the Catch2 header compiles even after including windows.h
// without defining NOMINMAX first. As an FYI, if you do that, you are
// wrong.
#include <windows.h>
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
TEST_CASE("Catch2 did survive compilation with windows.h", "[compile-test]") {
SUCCEED();
}