You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

112 lines
2.5 KiB

  1. //
  2. // Lol Engine — Unit tests
  3. //
  4. // Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #include <lol/engine-internal.h>
  13. #include <lolunit.h>
  14. namespace lol
  15. {
  16. lolunit_declare_fixture(rand_test)
  17. {
  18. lolunit_declare_test(int32_bits)
  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(int16_bits)
  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(int8_bits)
  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 */