random_test.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. int 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. int 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. for(int i = 0; i < 1000; ++i)
  68. {
  69. run1.push_back(random.rand());
  70. }
  71. random.seed(0);
  72. std::vector<int> run2;
  73. for(int i = 0; i < 1000; ++i)
  74. {
  75. run2.push_back(random.rand());
  76. }
  77. ASSERT_EQ(run1, run2);
  78. }
  79. TEST(RandomTest, randf_determinism)
  80. {
  81. Random random;
  82. random.seed(0);
  83. std::vector<float> run1;
  84. for(int i = 0; i < 1000; ++i)
  85. {
  86. run1.push_back(random.randf(1.0f));
  87. }
  88. random.seed(0);
  89. std::vector<float> run2;
  90. for(int i = 0; i < 1000; ++i)
  91. {
  92. run2.push_back(random.randf(1.0f));
  93. }
  94. ASSERT_EQ(run1, run2);
  95. }
  96. /* EOF */