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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/*
This file is part of the KDE libraries
SPDX-FileCopyrightText: 2016 Michael Pyne <mpyne@kde.org>
SPDX-FileCopyrightText: 2016 Arne Spiegelhauer <gm2.asp@gmail.com>
SPDX-License-Identifier: LGPL-2.0-only
*/
#include <krandom.h>
#include <krandomsequence.h>
#include <stdlib.h>
#include <QTest>
#include <QThread>
#include <QObject>
#include <QProcess>
#include <QRegularExpression>
#include <QString>
#include <QTextStream>
#include <QVarLengthArray>
#include <algorithm>
#include <iostream>
typedef QVarLengthArray<int> intSequenceType;
static const char *binpath;
#if KCOREADDONS_BUILD_DEPRECATED_SINCE(5, 75)
static bool seqsAreEqual(const intSequenceType &l, const intSequenceType &r)
{
if (l.size() != r.size()) {
return false;
}
const intSequenceType::const_iterator last(l.end());
intSequenceType::const_iterator l_first(l.begin());
intSequenceType::const_iterator r_first(r.begin());
while (l_first != last && *l_first == *r_first) {
l_first++;
r_first++;
}
return l_first == last;
}
#endif
#if KCOREADDONS_BUILD_DEPRECATED_SINCE(5, 72)
// Fills seq with random bytes produced by a new process. Seq should already
// be sized to the needed amount of random numbers.
static bool getChildRandSeq(intSequenceType &seq)
{
QProcess subtestProcess;
// Launch a separate process to generate random numbers to test first-time
// seeding.
subtestProcess.start(QLatin1String(binpath), QStringList() << QString::number(seq.count()));
subtestProcess.waitForFinished();
QTextStream childStream(subtestProcess.readAllStandardOutput());
std::generate(seq.begin(), seq.end(), [&]() {
int temp;
childStream >> temp;
return temp;
});
char c;
childStream >> c;
return c == '@' && childStream.status() == QTextStream::Ok;
}
#endif
class KRandomTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
#if KCOREADDONS_BUILD_DEPRECATED_SINCE(5, 72)
void test_random();
#endif
void test_randomString();
void test_randomStringThreaded();
#if KCOREADDONS_BUILD_DEPRECATED_SINCE(5, 75)
void test_KRS();
#endif
void test_shuffle();
};
#if KCOREADDONS_BUILD_DEPRECATED_SINCE(5, 72)
void KRandomTest::test_random()
{
int testValue = KRandom::random();
QVERIFY(testValue >= 0);
QVERIFY(testValue < RAND_MAX);
// Verify seeding results in different numbers across different procs
// See bug 362161
intSequenceType out1(10);
intSequenceType out2(10);
QVERIFY(getChildRandSeq(out1));
QVERIFY(getChildRandSeq(out2));
QVERIFY(!seqsAreEqual(out1, out2));
}
#endif
void KRandomTest::test_randomString()
{
const int desiredLength = 12;
const QString testString = KRandom::randomString(desiredLength);
const QRegularExpression outputFormat(QRegularExpression::anchoredPattern(QStringLiteral("[A-Za-z0-9]+")));
const QRegularExpressionMatch match = outputFormat.match(testString);
QCOMPARE(testString.length(), desiredLength);
QVERIFY(match.hasMatch());
}
#if KCOREADDONS_BUILD_DEPRECATED_SINCE(5, 75)
void KRandomTest::test_KRS()
{
using std::all_of;
using std::generate;
const int maxInt = 50000;
KRandomSequence krs1;
KRandomSequence krs2;
intSequenceType out1(10);
intSequenceType out2(10);
generate(out1.begin(), out1.end(), [&]() {
return krs1.getInt(maxInt);
});
generate(out2.begin(), out2.end(), [&]() {
return krs2.getInt(maxInt);
});
QVERIFY(!seqsAreEqual(out1, out2));
QVERIFY(all_of(out1.begin(), out1.end(), [&](int x) {
return x < maxInt;
}));
QVERIFY(all_of(out2.begin(), out2.end(), [&](int x) {
return x < maxInt;
}));
// Compare same-seed
krs1.setSeed(5000);
krs2.setSeed(5000);
generate(out1.begin(), out1.end(), [&]() {
return krs1.getInt(maxInt);
});
generate(out2.begin(), out2.end(), [&]() {
return krs2.getInt(maxInt);
});
QVERIFY(seqsAreEqual(out1, out2));
QVERIFY(all_of(out1.begin(), out1.end(), [&](int x) {
return x < maxInt;
}));
QVERIFY(all_of(out2.begin(), out2.end(), [&](int x) {
return x < maxInt;
}));
// Compare same-seed and assignment ctor
krs1 = KRandomSequence(8000);
krs2 = KRandomSequence(8000);
generate(out1.begin(), out1.end(), [&]() {
return krs1.getInt(maxInt);
});
generate(out2.begin(), out2.end(), [&]() {
return krs2.getInt(maxInt);
});
QVERIFY(seqsAreEqual(out1, out2));
QVERIFY(all_of(out1.begin(), out1.end(), [&](int x) {
return x < maxInt;
}));
QVERIFY(all_of(out2.begin(), out2.end(), [&](int x) {
return x < maxInt;
}));
}
#endif
void KRandomTest::test_shuffle()
{
{
QRandomGenerator rg(1);
QList<int> list = {1, 2, 3, 4, 5};
const QList<int> shuffled = {5, 2, 4, 3, 1};
KRandom::shuffle(list, &rg);
QCOMPARE(list, shuffled);
}
{
QRandomGenerator rg(1);
QVector<int> vector = {1, 2, 3, 4, 5};
const QVector<int> shuffled = {5, 2, 4, 3, 1};
KRandom::shuffle(vector, &rg);
QCOMPARE(vector, shuffled);
}
{
QRandomGenerator rg(1);
std::vector<int> std_vector = {1, 2, 3, 4, 5};
const std::vector<int> shuffled = {5, 2, 4, 3, 1};
KRandom::shuffle(std_vector, &rg);
QCOMPARE(std_vector, shuffled);
}
}
class KRandomTestThread : public QThread
{
protected:
void run() override
{
result = KRandom::randomString(32);
};
public:
QString result;
};
void KRandomTest::test_randomStringThreaded()
{
static const int size = 5;
KRandomTestThread *threads[size];
for (int i = 0; i < size; ++i) {
threads[i] = new KRandomTestThread();
threads[i]->start();
}
QSet<QString> results;
for (int i = 0; i < size; ++i) {
threads[i]->wait(2000);
results.insert(threads[i]->result);
}
// each thread should have returned a unique result
QCOMPARE(results.size(), size);
for (int i = 0; i < size; ++i) {
delete threads[i];
}
}
#if KCOREADDONS_BUILD_DEPRECATED_SINCE(5, 72)
// Used by getChildRandSeq... outputs random numbers to stdout and then
// exits the process.
static void childGenRandom(int count)
{
// No logic to 300, just wanted to avoid it accidentally being 2.4B...
if (count <= 0 || count > 300) {
exit(-1);
}
while (--count > 0) {
std::cout << KRandom::random() << ' ';
}
std::cout << KRandom::random() << '@';
exit(0);
}
#endif
// Manually implemented to dispatch to child process if needed to support
// subtests
int main([[maybe_unused]] int argc, char *argv[])
{
#if KCOREADDONS_BUILD_DEPRECATED_SINCE(5, 72)
if (argc > 1) {
childGenRandom(std::atoi(argv[1]));
Q_UNREACHABLE();
}
#endif
binpath = argv[0];
KRandomTest randomTest;
return QTest::qExec(&randomTest);
}
#include "krandomtest.moc"
|