123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include "core.h"
  14. #include "lol/unit.h"
  15. namespace lol
  16. {
  17. LOLUNIT_FIXTURE(RandTest)
  18. {
  19. void SetUp() {}
  20. void TearDown() {}
  21. LOLUNIT_TEST(Int32Bits)
  22. {
  23. int const rolls = 2000;
  24. int bits[32];
  25. memset(bits, 0, sizeof(bits));
  26. for (int i = 0; i < rolls; ++i)
  27. {
  28. int32_t r = rand<int32_t>();
  29. LOLUNIT_ASSERT_GEQUAL(r, 0);
  30. for (int k = 0; k < 31; k++)
  31. {
  32. bits[k] += r & 1;
  33. r >>= 1;
  34. }
  35. }
  36. for (int k = 0; k < 31; k++)
  37. {
  38. LOLUNIT_SET_CONTEXT(k);
  39. LOLUNIT_ASSERT_GEQUAL(bits[k], rolls / 3);
  40. LOLUNIT_ASSERT_LEQUAL(bits[k], rolls * 2 / 3);
  41. LOLUNIT_UNSET_CONTEXT(k);
  42. }
  43. }
  44. LOLUNIT_TEST(Int16Bits)
  45. {
  46. int const rolls = 2000;
  47. int bits[16];
  48. memset(bits, 0, sizeof(bits));
  49. for (int i = 0; i < rolls; ++i)
  50. {
  51. int16_t r = rand<int16_t>();
  52. LOLUNIT_ASSERT_GEQUAL(r, 0);
  53. for (int k = 0; k < 15; k++)
  54. {
  55. bits[k] += r & 1;
  56. r >>= 1;
  57. }
  58. }
  59. for (int k = 0; k < 15; k++)
  60. {
  61. LOLUNIT_SET_CONTEXT(k);
  62. LOLUNIT_ASSERT_GEQUAL(bits[k], rolls / 3);
  63. LOLUNIT_ASSERT_LEQUAL(bits[k], rolls * 2 / 3);
  64. LOLUNIT_UNSET_CONTEXT(k);
  65. }
  66. }
  67. LOLUNIT_TEST(Int8Bits)
  68. {
  69. int const rolls = 2000;
  70. int bits[8];
  71. memset(bits, 0, sizeof(bits));
  72. for (int i = 0; i < rolls; ++i)
  73. {
  74. int8_t r = rand<int8_t>();
  75. LOLUNIT_ASSERT_GEQUAL(r, 0);
  76. for (int k = 0; k < 7; k++)
  77. {
  78. bits[k] += r & 1;
  79. r >>= 1;
  80. }
  81. }
  82. for (int k = 0; k < 7; k++)
  83. {
  84. LOLUNIT_SET_CONTEXT(k);
  85. LOLUNIT_ASSERT_GEQUAL(bits[k], rolls / 3);
  86. LOLUNIT_ASSERT_LEQUAL(bits[k], rolls * 2 / 3);
  87. LOLUNIT_UNSET_CONTEXT(k);
  88. }
  89. }
  90. };
  91. } /* namespace lol */