您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

117 行
2.4 KiB

  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 */