catchorg / catchorg/Catch2

GENERATE_COPY does not mix variables and provided generators correctly

Open
#2,144 4 comments 0 reactions 0 assignees View on GitHub
Serious bug
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

**Describe the bug**
When mixing variables with a provided generator (e.g. `RandomIntegerGenerator`) I get values outside of the given range
i.e. the following test often fails (not always)
```c++
// I want to test for 'low', 'high' and 100 random values between the them ...
TestType v = GENERATE_COPY(low, high, take(100, random(low, high)));

CHECK(v >= low);
CHECK(v <= high);
```

**Expected behavior**
I never expect `v` to be outside of the range low - high.

**Reproduction steps**
Steps to reproduce the bug.
```c++
// Catch v2.13.4
// msvc 19.28.29335

#define CATCH_CONFIG_MAIN

#include "catch.hpp"
#include

#define LOG_V(arg) \
INFO("v is " << arg); \
std::cout << "v is " << arg << std::endl;

TEMPLATE_TEST_CASE("GENERATE_COPY", "", uint16_t, uint32_t) {
const TestType low = std::numeric_limits::max() / 4;
const TestType high = std::numeric_limits::max() / 2;

SECTION("using low and high values passes") {
TestType v = GENERATE_COPY(low, high);
LOG_V(v);
CHECK(v >= low);
CHECK(v <= high);
}

SECTION("taking random values between low and high passes") {
TestType v = GENERATE_COPY(take(100, random(low, high)));
LOG_V(v);
CHECK(v >= low);
CHECK(v <= high);
}

SECTION("mixing low and high with random values between low and high (sometimes) fails") {
TestType v = GENERATE_COPY(low, high, take(100, random(low, high)));
LOG_V(v);
CHECK(v >= low);
CHECK(v <= high);
}
}
```
**Platform information:**

- OS: **Windows 10**
- Compiler+version: **msvc 19.28.29335**
- Catch version: **v2.13.4**

**Additional context**
I have seen all sorts of odd behaviour while investigating this, including
1. `take` only ever returning one result
2. `as{}, ` causing compiler issues
3. rvalues causing compiler issues e.g. `GENERATE_COPY(low - 1, high, take(...`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.