rtree.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "test/jemalloc_test.h"
  2. TEST_BEGIN(test_rtree_get_empty)
  3. {
  4. unsigned i;
  5. for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
  6. rtree_t *rtree = rtree_new(i, imalloc, idalloc);
  7. assert_u_eq(rtree_get(rtree, 0), 0,
  8. "rtree_get() should return NULL for empty tree");
  9. rtree_delete(rtree);
  10. }
  11. }
  12. TEST_END
  13. TEST_BEGIN(test_rtree_extrema)
  14. {
  15. unsigned i;
  16. for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
  17. rtree_t *rtree = rtree_new(i, imalloc, idalloc);
  18. rtree_set(rtree, 0, 1);
  19. assert_u_eq(rtree_get(rtree, 0), 1,
  20. "rtree_get() should return previously set value");
  21. rtree_set(rtree, ~((uintptr_t)0), 1);
  22. assert_u_eq(rtree_get(rtree, ~((uintptr_t)0)), 1,
  23. "rtree_get() should return previously set value");
  24. rtree_delete(rtree);
  25. }
  26. }
  27. TEST_END
  28. TEST_BEGIN(test_rtree_bits)
  29. {
  30. unsigned i, j, k;
  31. for (i = 1; i < (sizeof(uintptr_t) << 3); i++) {
  32. uintptr_t keys[] = {0, 1,
  33. (((uintptr_t)1) << (sizeof(uintptr_t)*8-i)) - 1};
  34. rtree_t *rtree = rtree_new(i, imalloc, idalloc);
  35. for (j = 0; j < sizeof(keys)/sizeof(uintptr_t); j++) {
  36. rtree_set(rtree, keys[j], 1);
  37. for (k = 0; k < sizeof(keys)/sizeof(uintptr_t); k++) {
  38. assert_u_eq(rtree_get(rtree, keys[k]), 1,
  39. "rtree_get() should return previously set "
  40. "value and ignore insignificant key bits; "
  41. "i=%u, j=%u, k=%u, set key=%#"PRIxPTR", "
  42. "get key=%#"PRIxPTR, i, j, k, keys[j],
  43. keys[k]);
  44. }
  45. assert_u_eq(rtree_get(rtree,
  46. (((uintptr_t)1) << (sizeof(uintptr_t)*8-i))), 0,
  47. "Only leftmost rtree leaf should be set; "
  48. "i=%u, j=%u", i, j);
  49. rtree_set(rtree, keys[j], 0);
  50. }
  51. rtree_delete(rtree);
  52. }
  53. }
  54. TEST_END
  55. TEST_BEGIN(test_rtree_random)
  56. {
  57. unsigned i;
  58. sfmt_t *sfmt;
  59. #define NSET 100
  60. #define SEED 42
  61. sfmt = init_gen_rand(SEED);
  62. for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
  63. rtree_t *rtree = rtree_new(i, imalloc, idalloc);
  64. uintptr_t keys[NSET];
  65. unsigned j;
  66. for (j = 0; j < NSET; j++) {
  67. keys[j] = (uintptr_t)gen_rand64(sfmt);
  68. rtree_set(rtree, keys[j], 1);
  69. assert_u_eq(rtree_get(rtree, keys[j]), 1,
  70. "rtree_get() should return previously set value");
  71. }
  72. for (j = 0; j < NSET; j++) {
  73. assert_u_eq(rtree_get(rtree, keys[j]), 1,
  74. "rtree_get() should return previously set value");
  75. }
  76. for (j = 0; j < NSET; j++) {
  77. rtree_set(rtree, keys[j], 0);
  78. assert_u_eq(rtree_get(rtree, keys[j]), 0,
  79. "rtree_get() should return previously set value");
  80. }
  81. for (j = 0; j < NSET; j++) {
  82. assert_u_eq(rtree_get(rtree, keys[j]), 0,
  83. "rtree_get() should return previously set value");
  84. }
  85. rtree_delete(rtree);
  86. }
  87. fini_gen_rand(sfmt);
  88. #undef NSET
  89. #undef SEED
  90. }
  91. TEST_END
  92. int
  93. main(void)
  94. {
  95. return (test(
  96. test_rtree_get_empty,
  97. test_rtree_extrema,
  98. test_rtree_bits,
  99. test_rtree_random));
  100. }