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

114 行
2.4 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2014 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. #include <lol/engine-internal.h>
  11. #include <lolunit.h>
  12. namespace lol
  13. {
  14. lolunit_declare_fixture(RandTest)
  15. {
  16. void SetUp() {}
  17. void TearDown() {}
  18. lolunit_declare_test(Int32Bits)
  19. {
  20. int const rolls = 2000;
  21. int bits[32];
  22. memset(bits, 0, sizeof(bits));
  23. for (int i = 0; i < rolls; ++i)
  24. {
  25. int32_t r = rand<int32_t>();
  26. lolunit_assert_gequal(r, 0);
  27. for (int k = 0; k < 31; k++)
  28. {
  29. bits[k] += r & 1;
  30. r >>= 1;
  31. }
  32. }
  33. for (int k = 0; k < 31; k++)
  34. {
  35. lolunit_set_context(k);
  36. lolunit_assert_gequal(bits[k], rolls / 3);
  37. lolunit_assert_lequal(bits[k], rolls * 2 / 3);
  38. lolunit_unset_context(k);
  39. }
  40. }
  41. lolunit_declare_test(Int16Bits)
  42. {
  43. int const rolls = 2000;
  44. int bits[16];
  45. memset(bits, 0, sizeof(bits));
  46. for (int i = 0; i < rolls; ++i)
  47. {
  48. int16_t r = rand<int16_t>();
  49. lolunit_assert_gequal(r, 0);
  50. for (int k = 0; k < 15; k++)
  51. {
  52. bits[k] += r & 1;
  53. r >>= 1;
  54. }
  55. }
  56. for (int k = 0; k < 15; k++)
  57. {
  58. lolunit_set_context(k);
  59. lolunit_assert_gequal(bits[k], rolls / 3);
  60. lolunit_assert_lequal(bits[k], rolls * 2 / 3);
  61. lolunit_unset_context(k);
  62. }
  63. }
  64. lolunit_declare_test(Int8Bits)
  65. {
  66. int const rolls = 2000;
  67. int bits[8];
  68. memset(bits, 0, sizeof(bits));
  69. for (int i = 0; i < rolls; ++i)
  70. {
  71. int8_t r = rand<int8_t>();
  72. lolunit_assert_gequal(r, 0);
  73. for (int k = 0; k < 7; k++)
  74. {
  75. bits[k] += r & 1;
  76. r >>= 1;
  77. }
  78. }
  79. for (int k = 0; k < 7; k++)
  80. {
  81. lolunit_set_context(k);
  82. lolunit_assert_gequal(bits[k], rolls / 3);
  83. lolunit_assert_lequal(bits[k], rolls * 2 / 3);
  84. lolunit_unset_context(k);
  85. }
  86. }
  87. };
  88. } /* namespace lol */