File indexing completed on 2024-04-14 03:51:32

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2016 Michael Pyne <mpyne@kde.org>
0004     SPDX-FileCopyrightText: 2016 Arne Spiegelhauer <gm2.asp@gmail.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #include <krandom.h>
0010 #include <stdlib.h>
0011 
0012 #include <QTest>
0013 #include <QThread>
0014 
0015 #include <QObject>
0016 #include <QProcess>
0017 #include <QRegularExpression>
0018 #include <QString>
0019 #include <QTextStream>
0020 #include <QVarLengthArray>
0021 
0022 #include <algorithm>
0023 #include <iostream>
0024 
0025 typedef QVarLengthArray<int> intSequenceType;
0026 
0027 static const char *binpath;
0028 
0029 class KRandomTest : public QObject
0030 {
0031     Q_OBJECT
0032 
0033 private Q_SLOTS:
0034     void test_randomString();
0035     void test_randomStringThreaded();
0036     void test_shuffle();
0037 };
0038 
0039 void KRandomTest::test_randomString()
0040 {
0041     const int desiredLength = 12;
0042     const QString testString = KRandom::randomString(desiredLength);
0043     const QRegularExpression outputFormat(QRegularExpression::anchoredPattern(QStringLiteral("[A-Za-z0-9]+")));
0044     const QRegularExpressionMatch match = outputFormat.match(testString);
0045 
0046     QCOMPARE(testString.length(), desiredLength);
0047     QVERIFY(match.hasMatch());
0048 }
0049 
0050 void KRandomTest::test_shuffle()
0051 {
0052     {
0053         QRandomGenerator rg(1);
0054         QList<int> list = {1, 2, 3, 4, 5};
0055         const QList<int> shuffled = {5, 2, 4, 3, 1};
0056         KRandom::shuffle(list, &rg);
0057         QCOMPARE(list, shuffled);
0058     }
0059 
0060     {
0061         QRandomGenerator rg(1);
0062         QList<int> vector = {1, 2, 3, 4, 5};
0063         const QList<int> shuffled = {5, 2, 4, 3, 1};
0064         KRandom::shuffle(vector, &rg);
0065         QCOMPARE(vector, shuffled);
0066     }
0067 
0068     {
0069         QRandomGenerator rg(1);
0070         std::vector<int> std_vector = {1, 2, 3, 4, 5};
0071         const std::vector<int> shuffled = {5, 2, 4, 3, 1};
0072         KRandom::shuffle(std_vector, &rg);
0073         QCOMPARE(std_vector, shuffled);
0074     }
0075 }
0076 
0077 class KRandomTestThread : public QThread
0078 {
0079 protected:
0080     void run() override
0081     {
0082         result = KRandom::randomString(32);
0083     };
0084 
0085 public:
0086     QString result;
0087 };
0088 
0089 void KRandomTest::test_randomStringThreaded()
0090 {
0091     static const int size = 5;
0092     KRandomTestThread *threads[size];
0093     for (int i = 0; i < size; ++i) {
0094         threads[i] = new KRandomTestThread();
0095         threads[i]->start();
0096     }
0097     QSet<QString> results;
0098     for (int i = 0; i < size; ++i) {
0099         threads[i]->wait(2000);
0100         results.insert(threads[i]->result);
0101     }
0102     // each thread should have returned a unique result
0103     QCOMPARE(results.size(), size);
0104     for (int i = 0; i < size; ++i) {
0105         delete threads[i];
0106     }
0107 }
0108 
0109 // Manually implemented to dispatch to child process if needed to support
0110 // subtests
0111 int main([[maybe_unused]] int argc, char *argv[])
0112 {
0113     binpath = argv[0];
0114     KRandomTest randomTest;
0115     return QTest::qExec(&randomTest);
0116 }
0117 
0118 #include "krandomtest.moc"