prng.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "test/jemalloc_test.h"
  2. TEST_BEGIN(test_prng_lg_range_u32) {
  3. uint32_t sa, sb;
  4. uint32_t ra, rb;
  5. unsigned lg_range;
  6. sa = 42;
  7. ra = prng_lg_range_u32(&sa, 32);
  8. sa = 42;
  9. rb = prng_lg_range_u32(&sa, 32);
  10. expect_u32_eq(ra, rb,
  11. "Repeated generation should produce repeated results");
  12. sb = 42;
  13. rb = prng_lg_range_u32(&sb, 32);
  14. expect_u32_eq(ra, rb,
  15. "Equivalent generation should produce equivalent results");
  16. sa = 42;
  17. ra = prng_lg_range_u32(&sa, 32);
  18. rb = prng_lg_range_u32(&sa, 32);
  19. expect_u32_ne(ra, rb,
  20. "Full-width results must not immediately repeat");
  21. sa = 42;
  22. ra = prng_lg_range_u32(&sa, 32);
  23. for (lg_range = 31; lg_range > 0; lg_range--) {
  24. sb = 42;
  25. rb = prng_lg_range_u32(&sb, lg_range);
  26. expect_u32_eq((rb & (UINT32_C(0xffffffff) << lg_range)),
  27. 0, "High order bits should be 0, lg_range=%u", lg_range);
  28. expect_u32_eq(rb, (ra >> (32 - lg_range)),
  29. "Expected high order bits of full-width result, "
  30. "lg_range=%u", lg_range);
  31. }
  32. }
  33. TEST_END
  34. TEST_BEGIN(test_prng_lg_range_u64) {
  35. uint64_t sa, sb, ra, rb;
  36. unsigned lg_range;
  37. sa = 42;
  38. ra = prng_lg_range_u64(&sa, 64);
  39. sa = 42;
  40. rb = prng_lg_range_u64(&sa, 64);
  41. expect_u64_eq(ra, rb,
  42. "Repeated generation should produce repeated results");
  43. sb = 42;
  44. rb = prng_lg_range_u64(&sb, 64);
  45. expect_u64_eq(ra, rb,
  46. "Equivalent generation should produce equivalent results");
  47. sa = 42;
  48. ra = prng_lg_range_u64(&sa, 64);
  49. rb = prng_lg_range_u64(&sa, 64);
  50. expect_u64_ne(ra, rb,
  51. "Full-width results must not immediately repeat");
  52. sa = 42;
  53. ra = prng_lg_range_u64(&sa, 64);
  54. for (lg_range = 63; lg_range > 0; lg_range--) {
  55. sb = 42;
  56. rb = prng_lg_range_u64(&sb, lg_range);
  57. expect_u64_eq((rb & (UINT64_C(0xffffffffffffffff) << lg_range)),
  58. 0, "High order bits should be 0, lg_range=%u", lg_range);
  59. expect_u64_eq(rb, (ra >> (64 - lg_range)),
  60. "Expected high order bits of full-width result, "
  61. "lg_range=%u", lg_range);
  62. }
  63. }
  64. TEST_END
  65. TEST_BEGIN(test_prng_lg_range_zu) {
  66. size_t sa, sb;
  67. size_t ra, rb;
  68. unsigned lg_range;
  69. sa = 42;
  70. ra = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR));
  71. sa = 42;
  72. rb = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR));
  73. expect_zu_eq(ra, rb,
  74. "Repeated generation should produce repeated results");
  75. sb = 42;
  76. rb = prng_lg_range_zu(&sb, ZU(1) << (3 + LG_SIZEOF_PTR));
  77. expect_zu_eq(ra, rb,
  78. "Equivalent generation should produce equivalent results");
  79. sa = 42;
  80. ra = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR));
  81. rb = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR));
  82. expect_zu_ne(ra, rb,
  83. "Full-width results must not immediately repeat");
  84. sa = 42;
  85. ra = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR));
  86. for (lg_range = (ZU(1) << (3 + LG_SIZEOF_PTR)) - 1; lg_range > 0;
  87. lg_range--) {
  88. sb = 42;
  89. rb = prng_lg_range_zu(&sb, lg_range);
  90. expect_zu_eq((rb & (SIZE_T_MAX << lg_range)),
  91. 0, "High order bits should be 0, lg_range=%u", lg_range);
  92. expect_zu_eq(rb, (ra >> ((ZU(1) << (3 + LG_SIZEOF_PTR)) -
  93. lg_range)), "Expected high order bits of full-width "
  94. "result, lg_range=%u", lg_range);
  95. }
  96. }
  97. TEST_END
  98. TEST_BEGIN(test_prng_range_u32) {
  99. uint32_t range;
  100. const uint32_t max_range = 10000000;
  101. const uint32_t range_step = 97;
  102. const unsigned nreps = 10;
  103. for (range = 2; range < max_range; range += range_step) {
  104. uint32_t s;
  105. unsigned rep;
  106. s = range;
  107. for (rep = 0; rep < nreps; rep++) {
  108. uint32_t r = prng_range_u32(&s, range);
  109. expect_u32_lt(r, range, "Out of range");
  110. }
  111. }
  112. }
  113. TEST_END
  114. TEST_BEGIN(test_prng_range_u64) {
  115. uint64_t range;
  116. const uint64_t max_range = 10000000;
  117. const uint64_t range_step = 97;
  118. const unsigned nreps = 10;
  119. for (range = 2; range < max_range; range += range_step) {
  120. uint64_t s;
  121. unsigned rep;
  122. s = range;
  123. for (rep = 0; rep < nreps; rep++) {
  124. uint64_t r = prng_range_u64(&s, range);
  125. expect_u64_lt(r, range, "Out of range");
  126. }
  127. }
  128. }
  129. TEST_END
  130. TEST_BEGIN(test_prng_range_zu) {
  131. size_t range;
  132. const size_t max_range = 10000000;
  133. const size_t range_step = 97;
  134. const unsigned nreps = 10;
  135. for (range = 2; range < max_range; range += range_step) {
  136. size_t s;
  137. unsigned rep;
  138. s = range;
  139. for (rep = 0; rep < nreps; rep++) {
  140. size_t r = prng_range_zu(&s, range);
  141. expect_zu_lt(r, range, "Out of range");
  142. }
  143. }
  144. }
  145. TEST_END
  146. int
  147. main(void) {
  148. return test_no_reentrancy(
  149. test_prng_lg_range_u32,
  150. test_prng_lg_range_u64,
  151. test_prng_lg_range_zu,
  152. test_prng_range_u32,
  153. test_prng_range_u64,
  154. test_prng_range_zu);
  155. }