TestRandomGenerator.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 "TestGlobal.h"
  19. #include "core/Endian.h"
  20. #include "core/Global.h"
  21. #include "crypto/Random.h"
  22. #include <QTest>
  23. QTEST_GUILESS_MAIN(TestRandomGenerator)
  24. void TestRandomGenerator::testArray()
  25. {
  26. auto ba = randomGen()->randomArray(10);
  27. QCOMPARE(ba.size(), 10);
  28. QVERIFY(ba != QByteArray(10, '\0'));
  29. auto ba2 = ba;
  30. randomGen()->randomize(ba2);
  31. QVERIFY(ba2 != ba);
  32. }
  33. void TestRandomGenerator::testUInt()
  34. {
  35. QVERIFY(randomGen()->randomUInt(0) == 0);
  36. QVERIFY(randomGen()->randomUInt(1) == 0);
  37. // Run a bunch of trials creating random numbers to ensure we meet the standard
  38. for (int i = 0; i < 100; ++i) {
  39. QVERIFY(randomGen()->randomUInt(5) < 5);
  40. QVERIFY(randomGen()->randomUInt(100) < 100);
  41. QVERIFY(randomGen()->randomUInt(100000U) < 100000U);
  42. QVERIFY(randomGen()->randomUInt((QUINT32_MAX / 2U) + 1U) < QUINT32_MAX / 2U + 1U);
  43. }
  44. }
  45. void TestRandomGenerator::testUIntRange()
  46. {
  47. // Run a bunch of trials to ensure we stay within the range
  48. for (int i = 0; i < 100; ++i) {
  49. auto rand = randomGen()->randomUIntRange(100, 200);
  50. QVERIFY(rand >= 100);
  51. QVERIFY(rand < 200);
  52. }
  53. }