catchorg / catchorg/Catch2

UnorderedRangeEquals calls Comparator function with different argument orders

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

Description

**Describe the bug**
When using UnorderedRangeEquals to compare two ranges of different types, the Comparator function is called assuming the order of arguments does not matter.

Let's assume that I want to compare a range of elements of type A against another range of elements of type B. I have implemented a Comparator function:
bool compareAB(const A& a, const B& b);

However, when providing that Comparator function to UnorderedRangeEquals, compilation will fail because it calls the Comparator function as compareAB(a, b) as well as compareAB(b, a), and it expects the Comparator function to work either way.

**Expected behavior**
UnorderedRangeEquals should call the Comparator function providing always the arguments in the same order: the first argument should correspond to an element in the first range, and the second element to an element in the second range.

**Reproduction steps**
This simple program triggers the error:
```
#include
#include
#include

struct A {
int i;
};

struct B {
int j;
};

TEST_CASE("Test") {
std::vector va = {{1},{2},{3},{4}};
std::vector vb = {{4},{2},{3},{1}};

CHECK_THAT(va, Catch::Matchers::UnorderedRangeEquals(vb,
[](auto v1, auto v2) {return v1.i == v2.j;}));
}
```
See in Compiler Explorer: https://godbolt.org/z/ajnb9468s

**Platform information:**

- OS: **Ubuntu 24.04**
- Compiler+version: **Clang 20.1.2**
- Catch version: **v3.11.0**

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.