diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2017-10-23 19:16:34 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2017-11-11 08:11:07 +0000 |
commit | 08bf28de03f16e5b014b23e228dfc3cfc2ac7feb (patch) | |
tree | 0919b98368e7f6ed8faf7bb2fa71b1d711d09a27 /tests/auto/corelib/global | |
parent | cfad4e298f4d65fe26c3a4109d19839bdfcd30c2 (diff) |
QRandomGenerator: add more of the std Random Engine API
This brings us to almost parity with the C++11 Random Engine API
requirements (see chapter 26.5.1.4 [rand.req.eng]). We don't implement
the templated Sseq requirements because it would require moving the
implementation details to the public API. And we don't implement the
<iostreams> code because we don't want to.
Change-Id: Icaa86fc7b54d4b368c0efffd14f05ff813ebd759
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests/auto/corelib/global')
-rw-r--r-- | tests/auto/corelib/global/qrandomgenerator/tst_qrandomgenerator.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/auto/corelib/global/qrandomgenerator/tst_qrandomgenerator.cpp b/tests/auto/corelib/global/qrandomgenerator/tst_qrandomgenerator.cpp index 9c1828d1dd..220ec9a2f8 100644 --- a/tests/auto/corelib/global/qrandomgenerator/tst_qrandomgenerator.cpp +++ b/tests/auto/corelib/global/qrandomgenerator/tst_qrandomgenerator.cpp @@ -73,6 +73,7 @@ public slots: private slots: void basics(); void knownSequence(); + void discard(); void copying(); void copyingGlobal(); void copyingSystem(); @@ -191,32 +192,63 @@ void tst_QRandomGenerator::knownSequence() QRandomGenerator rng; for (quint32 x : defaultRngResults) QCOMPARE(rng(), x); + + // should work again if we reseed it + rng.seed(); + for (quint32 x : defaultRngResults) + QCOMPARE(rng(), x); +} + +void tst_QRandomGenerator::discard() +{ + QRandomGenerator rng; + rng.discard(1); + QCOMPARE(rng(), defaultRngResults[1]); + + rng.discard(9); + QCOMPARE(rng(), defaultRngResults[11]); } void tst_QRandomGenerator::copying() { QRandomGenerator rng1; QRandomGenerator rng2 = rng1; + QCOMPARE(rng1, rng2); quint32 samples[20]; rng1.fillRange(samples); + // not equal anymore + QVERIFY(rng1 != rng2); + // should produce the same sequence, whichever it was for (quint32 x : samples) QCOMPARE(rng2(), x); + + // now they should compare equal again + QCOMPARE(rng1, rng2); } void tst_QRandomGenerator::copyingGlobal() { QRandomGenerator &global = *QRandomGenerator::global(); QRandomGenerator copy = global; + QCOMPARE(copy, global); + QCOMPARE(global, copy); quint32 samples[20]; global.fillRange(samples); + // not equal anymore + QVERIFY(copy != global); + // should produce the same sequence, whichever it was for (quint32 x : samples) QCOMPARE(copy(), x); + + // equal again + QCOMPARE(copy, global); + QCOMPARE(global, copy); } void tst_QRandomGenerator::copyingSystem() @@ -225,15 +257,24 @@ void tst_QRandomGenerator::copyingSystem() QRandomGenerator copy = system; QRandomGenerator copy2 = copy; copy2 = copy; + QCOMPARE(system, copy); + QCOMPARE(copy, copy2); quint32 samples[20]; copy2.fillRange(samples); + // they still compre equally + QCOMPARE(system, copy); + QCOMPARE(copy, copy2); + // should NOT produce the same sequence, whichever it was int sameCount = 0; for (quint32 x : samples) sameCount += (copy() == x); QVERIFY(sameCount < 20); + + QCOMPARE(system, copy); + QCOMPARE(copy, copy2); } void tst_QRandomGenerator::systemRng() |