Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qstring_generator.h
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#pragma once
5
6#include "../namespaces.h"
8#include "../utilities/semantics/generator_handler.h"
9
10#include <catch/catch.hpp>
11
12#include <random>
13
14#include <QString>
15
18
20 public:
21 QStringGenerator(Catch::Generators::GeneratorWrapper<QChar>&& character_generator, qsizetype minimum_length, qsizetype maximum_length)
23 random_engine{std::random_device{}()},
26 {
27 assert(minimum_length >= 0);
28 assert(maximum_length >= 0);
29 assert(minimum_length <= maximum_length);
30
31 if (!next())
32 Catch::throw_exception("Not enough values to initialize the first string");
33 }
34
35 QString const& get() const override { return current_string; }
36
37 bool next() override {
38 qsizetype length{length_distribution(random_engine)};
39
40 current_string = QString();
41 for (qsizetype length_index{0}; length_index < length; ++length_index) {
42 if (!character_generator.next()) return false;
43
44 current_string += character_generator.get();
45 }
46
47 return true;
48 }
49
50 private:
51 Catch::Generators::GeneratorWrapper<QChar> character_generator;
52
53 std::mt19937 random_engine;
54 std::uniform_int_distribution<qsizetype> length_distribution;
55
56 QString current_string;
57 };
58
59 } // end QDOC_CATCH_GENERATORS_PRIVATE_NAMESPACE
60
61 /*!
62 * Returns a generator that generates elements of QString from
63 * some amount of elements taken from \a character_generator.
64 *
65 * The generated strings will have a length in the range
66 * [\a minimum_length, \a maximum_length].
67 *
68 * For compatibility with the Qt API, it is possible to provide
69 * negative bounds for the length. This is, nonetheless,
70 * considered an error such that the bounds should always be
71 * greater or equal to zero.
72 *
73 * It is similarly considered an error to have minimum_length <=
74 * maximum_length.
75 *
76 * The provided generator will generate elements until \a
77 * character_generator is exhausted.
78 */
79 inline Catch::Generators::GeneratorWrapper<QString> string(Catch::Generators::GeneratorWrapper<QChar>&& character_generator, qsizetype minimum_length, qsizetype maximum_length) {
80 return Catch::Generators::GeneratorWrapper<QString>(std::unique_ptr<Catch::Generators::IGenerator<QString>>(new QDOC_CATCH_GENERATORS_PRIVATE_NAMESPACE::QStringGenerator(std::move(character_generator), minimum_length, maximum_length)));
81 }
82
83 /*!
84 * Returns an infinite generator whose elements are the empty
85 * QString.
86 */
88 return Catch::Generators::GeneratorWrapper<QString>(std::unique_ptr<Catch::Generators::IGenerator<QString>>(new QDOC_CATCH_GENERATORS_PRIVATE_NAMESPACE::QStringGenerator(character(), 0, 0)));
89 }
90
91
92} // end QDOC_CATCH_GENERATORS_ROOT_NAMESPACE
QStringGenerator(Catch::Generators::GeneratorWrapper< QChar > &&character_generator, qsizetype minimum_length, qsizetype maximum_length)
Catch::Generators::GeneratorWrapper< QString > empty_string()
Returns an infinite generator whose elements are the empty QString.
Catch::Generators::GeneratorWrapper< QString > string(Catch::Generators::GeneratorWrapper< QChar > &&character_generator, qsizetype minimum_length, qsizetype maximum_length)
Returns a generator that generates elements of QString from some amount of elements taken from charac...
#define QDOC_CATCH_GENERATORS_PRIVATE_NAMESPACE
Definition namespaces.h:8
#define QDOC_CATCH_GENERATORS_UTILITIES_ABSOLUTE_NAMESPACE
Definition namespaces.h:14
#define QDOC_CATCH_GENERATORS_ROOT_NAMESPACE
Definition namespaces.h:6
#define assert