TestRandomGenerator.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (C) 2013 Felix Geyer <debfx@fobos.de>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 or (at your option)
  7. * version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "TestRandomGenerator.h"
  18. #include "core/Global.h"
  19. #include "crypto/Random.h"
  20. #include <QTest>
  21. QTEST_GUILESS_MAIN(TestRandomGenerator)
  22. void TestRandomGenerator::testArray()
  23. {
  24. auto ba = randomGen()->randomArray(10);
  25. QCOMPARE(ba.size(), 10);
  26. QVERIFY(ba != QByteArray(10, '\0'));
  27. auto ba2 = ba;
  28. randomGen()->randomize(ba2);
  29. QVERIFY(ba2 != ba);
  30. }
  31. void TestRandomGenerator::testUInt()
  32. {
  33. QVERIFY(randomGen()->randomUInt(0) == 0);
  34. QVERIFY(randomGen()->randomUInt(1) == 0);
  35. // Run a bunch of trials creating random numbers to ensure we meet the standard
  36. for (int i = 0; i < 100; ++i) {
  37. QVERIFY(randomGen()->randomUInt(5) < 5);
  38. QVERIFY(randomGen()->randomUInt(100) < 100);
  39. QVERIFY(randomGen()->randomUInt(100000U) < 100000U);
  40. QVERIFY(randomGen()->randomUInt((QUINT32_MAX / 2U) + 1U) < QUINT32_MAX / 2U + 1U);
  41. }
  42. }
  43. void TestRandomGenerator::testUIntRange()
  44. {
  45. // Run a bunch of trials to ensure we stay within the range
  46. for (int i = 0; i < 100; ++i) {
  47. auto rand = randomGen()->randomUIntRange(100, 200);
  48. QVERIFY(rand >= 100);
  49. QVERIFY(rand < 200);
  50. }
  51. }