rtree.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "test/jemalloc_test.h"
  2. static rtree_node_elm_t *
  3. node_alloc(size_t nelms)
  4. {
  5. return ((rtree_node_elm_t *)calloc(nelms, sizeof(rtree_node_elm_t)));
  6. }
  7. static void
  8. node_dalloc(rtree_node_elm_t *node)
  9. {
  10. free(node);
  11. }
  12. TEST_BEGIN(test_rtree_get_empty)
  13. {
  14. unsigned i;
  15. for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
  16. rtree_t rtree;
  17. assert_false(rtree_new(&rtree, i, node_alloc, node_dalloc),
  18. "Unexpected rtree_new() failure");
  19. assert_ptr_null(rtree_get(&rtree, 0, false),
  20. "rtree_get() should return NULL for empty tree");
  21. rtree_delete(&rtree);
  22. }
  23. }
  24. TEST_END
  25. TEST_BEGIN(test_rtree_extrema)
  26. {
  27. unsigned i;
  28. extent_node_t node_a, node_b;
  29. for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
  30. rtree_t rtree;
  31. assert_false(rtree_new(&rtree, i, node_alloc, node_dalloc),
  32. "Unexpected rtree_new() failure");
  33. assert_false(rtree_set(&rtree, 0, &node_a),
  34. "Unexpected rtree_set() failure");
  35. assert_ptr_eq(rtree_get(&rtree, 0, true), &node_a,
  36. "rtree_get() should return previously set value");
  37. assert_false(rtree_set(&rtree, ~((uintptr_t)0), &node_b),
  38. "Unexpected rtree_set() failure");
  39. assert_ptr_eq(rtree_get(&rtree, ~((uintptr_t)0), true), &node_b,
  40. "rtree_get() should return previously set value");
  41. rtree_delete(&rtree);
  42. }
  43. }
  44. TEST_END
  45. TEST_BEGIN(test_rtree_bits)
  46. {
  47. unsigned i, j, k;
  48. for (i = 1; i < (sizeof(uintptr_t) << 3); i++) {
  49. uintptr_t keys[] = {0, 1,
  50. (((uintptr_t)1) << (sizeof(uintptr_t)*8-i)) - 1};
  51. extent_node_t node;
  52. rtree_t rtree;
  53. assert_false(rtree_new(&rtree, i, node_alloc, node_dalloc),
  54. "Unexpected rtree_new() failure");
  55. for (j = 0; j < sizeof(keys)/sizeof(uintptr_t); j++) {
  56. assert_false(rtree_set(&rtree, keys[j], &node),
  57. "Unexpected rtree_set() failure");
  58. for (k = 0; k < sizeof(keys)/sizeof(uintptr_t); k++) {
  59. assert_ptr_eq(rtree_get(&rtree, keys[k], true),
  60. &node, "rtree_get() should return "
  61. "previously set value and ignore "
  62. "insignificant key bits; i=%u, j=%u, k=%u, "
  63. "set key=%#"FMTxPTR", get key=%#"FMTxPTR, i,
  64. j, k, keys[j], keys[k]);
  65. }
  66. assert_ptr_null(rtree_get(&rtree,
  67. (((uintptr_t)1) << (sizeof(uintptr_t)*8-i)), false),
  68. "Only leftmost rtree leaf should be set; "
  69. "i=%u, j=%u", i, j);
  70. assert_false(rtree_set(&rtree, keys[j], NULL),
  71. "Unexpected rtree_set() failure");
  72. }
  73. rtree_delete(&rtree);
  74. }
  75. }
  76. TEST_END
  77. TEST_BEGIN(test_rtree_random)
  78. {
  79. unsigned i;
  80. sfmt_t *sfmt;
  81. #define NSET 16
  82. #define SEED 42
  83. sfmt = init_gen_rand(SEED);
  84. for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
  85. uintptr_t keys[NSET];
  86. extent_node_t node;
  87. unsigned j;
  88. rtree_t rtree;
  89. assert_false(rtree_new(&rtree, i, node_alloc, node_dalloc),
  90. "Unexpected rtree_new() failure");
  91. for (j = 0; j < NSET; j++) {
  92. keys[j] = (uintptr_t)gen_rand64(sfmt);
  93. assert_false(rtree_set(&rtree, keys[j], &node),
  94. "Unexpected rtree_set() failure");
  95. assert_ptr_eq(rtree_get(&rtree, keys[j], true), &node,
  96. "rtree_get() should return previously set value");
  97. }
  98. for (j = 0; j < NSET; j++) {
  99. assert_ptr_eq(rtree_get(&rtree, keys[j], true), &node,
  100. "rtree_get() should return previously set value");
  101. }
  102. for (j = 0; j < NSET; j++) {
  103. assert_false(rtree_set(&rtree, keys[j], NULL),
  104. "Unexpected rtree_set() failure");
  105. assert_ptr_null(rtree_get(&rtree, keys[j], true),
  106. "rtree_get() should return previously set value");
  107. }
  108. for (j = 0; j < NSET; j++) {
  109. assert_ptr_null(rtree_get(&rtree, keys[j], true),
  110. "rtree_get() should return previously set value");
  111. }
  112. rtree_delete(&rtree);
  113. }
  114. fini_gen_rand(sfmt);
  115. #undef NSET
  116. #undef SEED
  117. }
  118. TEST_END
  119. int
  120. main(void)
  121. {
  122. return (test(
  123. test_rtree_get_empty,
  124. test_rtree_extrema,
  125. test_rtree_bits,
  126. test_rtree_random));
  127. }