Skip to content

Commit

Permalink
Merge pull request #7 from emil-e/master
Browse files Browse the repository at this point in the history
Merge upstream master
  • Loading branch information
woodfell authored Jul 12, 2023
2 parents a79fd09 + a5724ea commit b8f11ac
Show file tree
Hide file tree
Showing 17 changed files with 108 additions and 27 deletions.
5 changes: 0 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ if(MINGW)
target_compile_definitions(rapidcheck PRIVATE RC_SEED_SYSTEM_TIME)
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "^MSVC$")
# To reflect the language level (cf. https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-160)
target_compile_options(rapidcheck PUBLIC /Zc:__cplusplus)
endif ()

if(NOT RC_ENABLE_RTTI)
target_compile_definitions(rapidcheck PUBLIC RC_DONT_USE_RTTI)
endif()
Expand Down
62 changes: 62 additions & 0 deletions doc/catch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Catch test integration

rapidcheck comes with some basic integrations for the catch test library.

## Usage

This support is available throught the `extras/catch` module. You can either
directly add the `extras/gtest/inclode` directory to your include path:

```cmake
add_subdirectory(rapidcheck)
set include_directories(rapidcheck/extras/gtest/include)
add_executable(MyTest main.cpp)
```

Or link against the `rapidcheck_catch` cmake target:

```cmake
add_subdirectory(rapidcheck)
add_executable(MyTest main.cpp)
target_link_libraries(MyTest rapidcheck_catch)
```

Once you've done one of these, you can simply:

```cpp
#include <catch2/catch.hpp>
#include "rapidcheck.h"
#include "rapidcheck/catch.h'
```
## Reference
### `rc::prop("My test description", []{return true;}, /*verbose=*/true)`
The `rc::prop` can be used in place of `SECTION` for convenient checking of
properties. The third parameter is optional and defaults to `false`.
```cpp
TEST_CASE("001: My first test case")
{
rc::prop("My property description",
[](int a, int b)
{
// rapidcheck will produce the `a` and `b`
return (a + b) == (b + a); // return true for passed test, false for
// failed test
});
// no problem mixing rapidcheck tests with regular catch assertions
SECTION("Normal catch stuff")
{
WHEN("Something happens")
{
THEN("assert a result")
{
REQUIRE(1 == 1);
}
}
}
}
```
2 changes: 1 addition & 1 deletion doc/generators_ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ Generates a value which is not less than `0`.

```C++
// Example:
const auto x = *gen::positive<int>();
const auto x = *gen::nonNegative<int>();
```

## Containers
Expand Down
2 changes: 1 addition & 1 deletion doc/gtest.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RapidCheck comes with support for integrating with Google Test and allows you to

## Usage

This support is available through the `extras/gtest` module. You can either directly add the `extras/gtest/include` directory to your include path or link against the `rapidcheck_gtest` target in your `CMakeLists.txt`. You can then simply `#include <rapidcheck/gtest.h>`. Note that `rapidcheck/gtest.h` needs to be included after `gtest/gtest.h`.
This support is available through the `extras/gtest` module. In order to enable it, pass `-DRC_ENABLE_GTEST=ON` to your `CMake` flags while building RapidCheck. Then you can either directly add the `extras/gtest/include` directory to your include path or link against the `rapidcheck_gtest` target in your `CMakeLists.txt`. You can then simply `#include <rapidcheck/gtest.h>`. Note that `rapidcheck/gtest.h` needs to be included after `gtest/gtest.h`.

## Reference

Expand Down
1 change: 1 addition & 0 deletions doc/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The following document is a work in progress and some parts are still missing. T

## Extras

- [Catch Test integratio](catch.md)
- [Google Test integration](gtest.md)
- [Google Mock integration](gmock.md)
- [Boost support](boost.md)
Expand Down
2 changes: 1 addition & 1 deletion ext/googletest
Submodule googletest updated 404 files
16 changes: 13 additions & 3 deletions extras/catch/include/rapidcheck/catch.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
#include <sstream>

#include <rapidcheck.h>
#include <catch2/catch.hpp>

// To support Catch2 v3 we check if the new header has already been included,
// otherwise we include the old header.
#ifndef CATCH_TEST_MACROS_HPP_INCLUDED
#include <catch2/catch.hpp>
#endif

namespace rc {

Expand All @@ -13,7 +18,7 @@ namespace rc {
/// @param description A description of the property.
/// @param testable The object that implements the property.
template <typename Testable>
void prop(const std::string &description, Testable &&testable) {
void prop(const std::string &description, Testable &&testable, bool verbose=false) {
using namespace detail;

#ifdef CATCH_CONFIG_PREFIX_ALL
Expand All @@ -26,11 +31,16 @@ void prop(const std::string &description, Testable &&testable) {

if (result.template is<SuccessResult>()) {
const auto success = result.template get<SuccessResult>();
if (!success.distribution.empty()) {
if (verbose || !success.distribution.empty()) {
std::cout << "- " << description << std::endl;
printResultMessage(result, std::cout);
std::cout << std::endl;
}
#ifdef CATCH_CONFIG_PREFIX_ALL
CATCH_SUCCEED();
#else
SUCCEED();
#endif
} else {
std::ostringstream ss;
printResultMessage(result, ss);
Expand Down
6 changes: 3 additions & 3 deletions include/rapidcheck/Compat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace rc {
namespace compat {

#if __cplusplus <= 201402L
#if __cpp_lib_is_invocable >= 201703
template <typename Fn, typename ...Args>
using return_type = typename std::result_of<Fn(Args...)>;
using return_type = typename std::invoke_result<Fn,Args...>;
#else
template <typename Fn, typename ...Args>
using return_type = typename std::invoke_result<Fn,Args...>;
using return_type = typename std::result_of<Fn(Args...)>;
#endif

}
Expand Down
1 change: 1 addition & 0 deletions include/rapidcheck/detail/BitStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class BitStream {
T next();

/// Returns the next random of the given type and number of bits.
/// nbits is capped at the number of bits in T
template <typename T>
T next(int nbits);

Expand Down
1 change: 1 addition & 0 deletions include/rapidcheck/detail/BitStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ T BitStream<Source>::next(int nbits, std::false_type)
#endif
{
using SourceType = decltype(m_source.next());
nbits = std::min(nbits, numBits<T>());

if (nbits == 0) {
return 0;
Expand Down
1 change: 1 addition & 0 deletions include/rapidcheck/detail/Serialization.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Serialization.h"

#include <cstdint>
#include <limits>

namespace rc {
Expand Down
1 change: 1 addition & 0 deletions include/rapidcheck/detail/Utility.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <cstdint>
#include <string>
#include <tuple>
#include <limits>
Expand Down
14 changes: 7 additions & 7 deletions include/rapidcheck/gen/detail/ShrinkValueIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ namespace gen {
namespace detail {

template <typename Iterator>
class ShrinkValueIterator
: public std::iterator<
std::input_iterator_tag,
typename std::iterator_traits<Iterator>::value_type::ValueType,
std::ptrdiff_t,
typename std::iterator_traits<Iterator>::value_type::ValueType *,
typename std::iterator_traits<Iterator>::value_type::ValueType &&> {
class ShrinkValueIterator {
public:
using T = typename std::iterator_traits<Iterator>::value_type::ValueType;

using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T *;
using reference = T &&;

ShrinkValueIterator(Iterator it)
: m_it(it) {}

Expand Down
8 changes: 7 additions & 1 deletion include/rapidcheck/seq/SeqIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ namespace seq {

/// STL iterator for `Seq`.
template <typename T>
class SeqIterator : public std::iterator<std::input_iterator_tag, T> {
class SeqIterator {
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T *;
using reference = T &;

/// Creates a new past-the-end `SeqIterator`.
SeqIterator() = default;

Expand Down
1 change: 1 addition & 0 deletions src/detail/Base64.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <cstdint>
#include <string>
#include <vector>

Expand Down
4 changes: 3 additions & 1 deletion test/detail/BitStreamTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ TEST_CASE("BitStream") {
const auto sizes = *bitSizes;
int totalSize = 0;
for (int size : sizes) {
totalSize += size;
// nbits is capped at 64 when reading a 64-bit integer,
// so we have to cap it as well.
totalSize += std::min(size, 64);
stream.next<uint64_t>(size);
}

Expand Down
8 changes: 4 additions & 4 deletions test/seq/TransformTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ TEST_CASE("seq::drop") {
REQUIRE(seq.next()->numberOf("copy") == 0);
}

SECTION("sanity check") {
SECTION("safety test") {
REQUIRE(seq::drop(2, seq::just(1, 2, 3)) == seq::just(3));
}
}
Expand Down Expand Up @@ -64,7 +64,7 @@ TEST_CASE("seq::take") {
REQUIRE(seq.next()->numberOf("copy") == 0);
}

SECTION("sanity check") {
SECTION("safety test") {
REQUIRE(seq::take(2, seq::just(1, 2, 3)) == seq::just(1, 2));
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ TEST_CASE("seq::dropWhile") {
REQUIRE(seq.next()->numberOf("copy") == 0);
}

SECTION("sanity check") {
SECTION("safety test") {
auto seq = seq::dropWhile(seq::just(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
[](int x) { return x < 5; });
auto expected = seq::just(5, 6, 7, 8, 9, 10);
Expand Down Expand Up @@ -125,7 +125,7 @@ TEST_CASE("seq::takeWhile") {
REQUIRE(seq.next()->numberOf("copy") == 0);
}

SECTION("sanity check") {
SECTION("safety test") {
auto seq = seq::takeWhile(seq::just(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
[](int x) { return x < 5; });
auto expected = seq::just(0, 1, 2, 3, 4);
Expand Down

0 comments on commit b8f11ac

Please sign in to comment.