random_test.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SuperTux
  2. // Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
  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 3 of the License, or
  7. // (at your option) any later version.
  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. #include <gtest/gtest.h>
  17. #include "math/random.hpp"
  18. TEST(RandomTest, rand1i)
  19. {
  20. Random random;
  21. random.seed(0);
  22. for(int i = 0; i < 1000; ++i)
  23. {
  24. int v = random.rand(10);
  25. ASSERT_LE(0, v);
  26. ASSERT_LT(v, 10);
  27. }
  28. }
  29. TEST(RandomTest, rand2i)
  30. {
  31. Random random;
  32. random.seed(0);
  33. for(int i = 0; i < 1000; ++i)
  34. {
  35. int v = random.rand(10, 20);
  36. ASSERT_LE(10, v);
  37. ASSERT_LT(v, 20);
  38. }
  39. }
  40. TEST(RandomTest, rand1f)
  41. {
  42. Random random;
  43. random.seed(0);
  44. for(int i = 0; i < 1000; ++i)
  45. {
  46. float v = random.randf(10);
  47. ASSERT_LE(0.0f, v);
  48. ASSERT_LT(v, 10.0f);
  49. }
  50. }
  51. TEST(RandomTest, rand2f)
  52. {
  53. Random random;
  54. random.seed(0);
  55. for(int i = 0; i < 1000; ++i)
  56. {
  57. float v = random.randf(10.0f, 20.0f);
  58. ASSERT_LE(10.0f, v);
  59. ASSERT_LT(v, 20.0f);
  60. }
  61. }
  62. TEST(RandomTest, rand_determinism)
  63. {
  64. Random random;
  65. random.seed(0);
  66. std::vector<int> run1;
  67. run1.reserve(1000);
  68. for(int i = 0; i < 1000; ++i)
  69. {
  70. run1.push_back(random.rand());
  71. }
  72. random.seed(0);
  73. std::vector<int> run2;
  74. run2.reserve(1000);
  75. for(int i = 0; i < 1000; ++i)
  76. {
  77. run2.push_back(random.rand());
  78. }
  79. ASSERT_EQ(run1, run2);
  80. }
  81. TEST(RandomTest, randf_determinism)
  82. {
  83. Random random;
  84. random.seed(0);
  85. std::vector<float> run1;
  86. run1.reserve(1000);
  87. for(int i = 0; i < 1000; ++i)
  88. {
  89. run1.push_back(random.randf(1.0f));
  90. }
  91. random.seed(0);
  92. std::vector<float> run2;
  93. run2.reserve(1000);
  94. for(int i = 0; i < 1000; ++i)
  95. {
  96. run2.push_back(random.randf(1.0f));
  97. }
  98. ASSERT_EQ(run1, run2);
  99. }
  100. /* EOF */