summaryrefslogtreecommitdiff
path: root/src/qdoc/catch_generators/src/catch_generators/generators/k_partition_of_r_generator.h
diff options
context:
space:
mode:
authorLuca Di Sera <luca.disera@qt.io>2023-05-12 15:12:11 +0200
committerLuca Di Sera <luca.disera@qt.io>2023-05-15 13:02:08 +0200
commitac01635f461bd9f211d89f6eea833d9d928fd113 (patch)
tree4efbfd1fc2f551435605b1c72d93e372f2dbb90b /src/qdoc/catch_generators/src/catch_generators/generators/k_partition_of_r_generator.h
parent7057d01fbb9f8f37c707b33e3b92c10a78919ddc (diff)
downloadqttools-ac01635f461bd9f211d89f6eea833d9d928fd113.tar.gz
QDoc: Move catch_generators under src
QDoc employs a support library, "catch_generators", to provide custom support for the data-generation that certain tests use or might want to use when using the Catch2 testing framework, which QDoc uses for some of its tests. "catch_generators" was currently kept under "tests/auto/qdoc" and its headers were included and used directly by related tests by relative paths. Due to a certain restructuring that is happening in QDoc, with one of the goals, among others, being to increase the locality of QDoc-related code under "src/qdoc", the "catch_generators" support library is now moved under "src/qdoc". To allow code that depended on it to keep their usages without requiring the addition of some relative paths from the "tests" directory to the "src" directory, a library target, "Qt::QDocCatchGeneratorsPrivate" was created for it. The target can be linked-to to gain access to the previously-directly-used headers, with include path "catch_generators/.*". To allow for this specific include path to work, the internal directory structure for "catch_generators" was slightly modified with the addition of some intermediate directories in between its root and the library headers. The root "CMakeLists.txt" file for the QDoc project was modified to add the moved "catch_generators" as a subdirectory, to include it in the build process. "catch_generators" contains its own test code under the subdirectory "tests". Previously the test target in "tests" included the relevant headers from the library by use of relative paths to its sources. Now that the library is exported as a target, the "CMakeLists.txt" file under "tests" was modified to rely on linking to the library itself to obtain access to the required headers. Similarly, targets under "tests/auto/qdoc" that used the dependency now link to it instead. Hence, their "CMakeLists.txt" files were modified to avoid using the dependency as an include directory and instead use "Qt::QDocCatcGeneratorsPrivate" as a library. The inclusions of the "catch_generators" headers in relevant sources was modified to respect the new "catch_generators/.*" path. Additionally, "catch_generators" exposed a top level header, "qdoc_catch_generators.h", including certain headers in the library that were expected to be commonly used by tests, so that the consumers did not have to include multiple headers when using various common generators. The header was removed in favor of including the library headers themselves now that they have a well-defined root provided by the exposed target. Sources that included the header were modified to include only their relevant headers from "catch_generators", respecting the additional granularity. Due to the Qt Project CI performing discovery of tests by configuring only the "tests" directory of the module; the moved "catch_generators", which contained its own testing code, will now escape the automated testing in CI for its own tests, as the relevant target does not reside under the "tests" directory of the repository. To avoid this issue, the "CMakeLists.txt" file under "tests/auto/qdoc", was modified to `include` the "CMakeLists.txt" file that describes "catch_generators"' test target, so as to "mirror" the test under the "tests" directory. To support working when included, the "CMakeLists.txt" file that describes "catch_generators"' test target was modified to refer to any relevant relative path by prefixing the value of `CMAKE_CURRENT_LIST_DIR`, so that the relative paths would not be incorrectly resolved when using `include`. Due to this "mirroring", the relevant test target would be configured twice when the whole repository was configured, resulting in an error. To ensure that this is not the case, the `include` directive was hidden behind `QT_BUILD_STANADALONE_TESTS`, which is not set during a general configuration of the repository but is set when CI performs test-discovery on the "tests" directory. Similarly, the "tests" directory of a repository is configured only when `QT_BUILD_TESTS` is `ON`, which further ensures that the required dependencies, such as `Qt::Test`, will be found before the directory is configured. By having moved the test declaration outside of the `tests` directory, the test will always be configured, failing to find its dependencies when this happens. To avoid the issue, the `add_subdirectory(tests)` directive in `src/qdoc/catch_generators/CMakeLists.txt` was conditioned over `QT_BUILD_TESTS`. Certain files that were missing an license header were enhanced with it, as the lack of an header will now be an issue outside the "tests" directory. Change-Id: I0bca477b7da75c121c24b8528fc9defa26a6123e Reviewed-by: Topi Reiniƶ <topi.reinio@qt.io>
Diffstat (limited to 'src/qdoc/catch_generators/src/catch_generators/generators/k_partition_of_r_generator.h')
-rw-r--r--src/qdoc/catch_generators/src/catch_generators/generators/k_partition_of_r_generator.h113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/qdoc/catch_generators/src/catch_generators/generators/k_partition_of_r_generator.h b/src/qdoc/catch_generators/src/catch_generators/generators/k_partition_of_r_generator.h
new file mode 100644
index 000000000..832ee2838
--- /dev/null
+++ b/src/qdoc/catch_generators/src/catch_generators/generators/k_partition_of_r_generator.h
@@ -0,0 +1,113 @@
+// Copyright (C) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+
+#pragma once
+
+#include "../namespaces.h"
+
+#include <catch/catch.hpp>
+
+#include <random>
+#include <numeric>
+#include <algorithm>
+
+namespace QDOC_CATCH_GENERATORS_ROOT_NAMESPACE {
+ namespace QDOC_CATCH_GENERATORS_PRIVATE_NAMESPACE {
+
+ class KPartitionOfRGenerator : public Catch::Generators::IGenerator<std::vector<double>> {
+ public:
+ KPartitionOfRGenerator(double r, std::size_t k)
+ : random_engine{std::random_device{}()},
+ interval_distribution{0.0, r},
+ k{k},
+ r{r},
+ current_partition(k)
+ {
+ assert(r >= 0.0);
+ assert(k >= 1);
+
+ static_cast<void>(next());
+ }
+
+ std::vector<double> const& get() const override { return current_partition; }
+
+ bool next() override {
+ if (k == 1) current_partition[0] = r;
+ else {
+ // REMARK: The following wasn't formally proved
+ // but is based on intuition.
+ // It is probably erroneous but is expected to be
+ // good enough for our case.
+
+ // REMARK: We aim to provide a non skewed
+ // distribution for the elements of the partition.
+ //
+ // The reasoning for this is to ensure that our
+ // testing surface has a good chance of hitting
+ // many of the available elements between the many
+ // runs.
+ //
+ // To approximate this, a specific algorithm was chosen.
+ // The following code can be intuitively seen as doing the following:
+ //
+ // Consider an interval [0.0, r] on the real line, where r > 0.0.
+ //
+ // k - 1 > 0 elements of the interval are chosen,
+ // partitioning the interval into disjoint
+ // sub-intervals.
+ //
+ // ---------------------------------------------------------------------------------------------------------------------
+ // | | | | |
+ // 0 k_1 k_2 k_3 r
+ // | | | | |
+ // _______--------------------_______________________________________________________-----------------------------------
+ // k_1 - 0 k_2 - k_1 k_3 - k_2 r - k_3
+ // p1 p2 p3 p4
+ //
+ // The length of each sub interval is chosen as one of the elements of the partition.
+ //
+ // Trivially, the sum of the chosen elements is r.
+ //
+ // Furthermore, as long as the distribution used
+ // to choose the elements of the original interval
+ // is uniform, the probability of each partition
+ // being produced should tend to being uniform
+ // itself.
+ std::generate(current_partition.begin(), current_partition.end() - 1, [this](){ return interval_distribution(random_engine); });
+
+ current_partition.back() = r;
+
+ std::sort(current_partition.begin(), current_partition.end());
+ std::adjacent_difference(current_partition.begin(), current_partition.end(), current_partition.begin());
+ }
+
+ return true;
+ }
+
+ private:
+ std::mt19937 random_engine;
+ std::uniform_real_distribution<double> interval_distribution;
+
+ std::size_t k;
+ double r;
+
+ std::vector<double> current_partition;
+ };
+
+ } // end QDOC_CATCH_GENERATORS_PRIVATE_NAMESPACE
+
+ /*!
+ * Returns a generator that generates collections of \a k elements
+ * whose sum is \a r.
+ *
+ * \a r must be a real number greater or euqal to zero and \a k
+ * must be a natural number greater than zero.
+ *
+ * The generated partitions tends to be uniformely distributed
+ * over the set of partitions of r.
+ */
+ inline Catch::Generators::GeneratorWrapper<std::vector<double>> k_partition_of_r(double r, std::size_t k) {
+ return Catch::Generators::GeneratorWrapper<std::vector<double>>(std::unique_ptr<Catch::Generators::IGenerator<std::vector<double>>>(new QDOC_CATCH_GENERATORS_PRIVATE_NAMESPACE::KPartitionOfRGenerator(r, k)));
+ }
+
+} // end QDOC_CATCH_GENERATORS_ROOT_NAMESPACE