summaryrefslogtreecommitdiff
path: root/tests/auto/qdoc/catch_generators/tests/generators/catch_k_partition_of_r_generator.cpp
blob: 6b3bbd85a8a284397e3e6f7bbec8e2f3ef469fc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "namespaces.h"
#include "generators/k_partition_of_r_generator.h"

#include <catch.hpp>

#include <numeric>

using namespace QDOC_CATCH_GENERATORS_ROOT_NAMESPACE;

SCENARIO("Generating a k-partition of a real number", "[Partition][Reals]") {
    GIVEN("A real number r greater or equal to zero") {
        double r = GENERATE(take(10, random(0.0, 1000000.0)));

        AND_GIVEN("An amount of desired elements k greater than zero") {
            std::size_t k = GENERATE(take(10, random(1, 100)));

            WHEN("A k-partition of r is generated") {
                auto k_partition = GENERATE_COPY(take(10, k_partition_of_r(r, k)));

                THEN("The partition contains k elements") {
                    REQUIRE(k_partition.size() == k);

                    AND_THEN("The sum of those elements is r") {
                        REQUIRE(std::accumulate(k_partition.begin(), k_partition.end(), 0.0) == Approx(r));
                    }
                }
            }
        }
    }
}

TEST_CASE("All 1-partition of r are singleton collection with r as their element", "[Partition][Reals][SpecialCase]") {
    double r = GENERATE(take(10, random(0.0, 1000000.0)));
    auto k_partition = GENERATE_COPY(take(10, k_partition_of_r(r, 1)));

    REQUIRE(k_partition.size() == 1);
    REQUIRE(k_partition.front() == r);
}