multiorder.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * multiorder.c: Multi-order radix tree entry testing
  3. * Copyright (c) 2016 Intel Corporation
  4. * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
  5. * Author: Matthew Wilcox <matthew.r.wilcox@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. */
  16. #include <linux/radix-tree.h>
  17. #include <linux/slab.h>
  18. #include <linux/errno.h>
  19. #include "test.h"
  20. static void multiorder_check(unsigned long index, int order)
  21. {
  22. unsigned long i;
  23. unsigned long min = index & ~((1UL << order) - 1);
  24. unsigned long max = min + (1UL << order);
  25. RADIX_TREE(tree, GFP_KERNEL);
  26. printf("Multiorder index %ld, order %d\n", index, order);
  27. assert(item_insert_order(&tree, index, order) == 0);
  28. for (i = min; i < max; i++) {
  29. struct item *item = item_lookup(&tree, i);
  30. assert(item != 0);
  31. assert(item->index == index);
  32. }
  33. for (i = 0; i < min; i++)
  34. item_check_absent(&tree, i);
  35. for (i = max; i < 2*max; i++)
  36. item_check_absent(&tree, i);
  37. assert(item_delete(&tree, index) != 0);
  38. for (i = 0; i < 2*max; i++)
  39. item_check_absent(&tree, i);
  40. }
  41. static void multiorder_shrink(unsigned long index, int order)
  42. {
  43. unsigned long i;
  44. unsigned long max = 1 << order;
  45. RADIX_TREE(tree, GFP_KERNEL);
  46. struct radix_tree_node *node;
  47. printf("Multiorder shrink index %ld, order %d\n", index, order);
  48. assert(item_insert_order(&tree, 0, order) == 0);
  49. node = tree.rnode;
  50. assert(item_insert(&tree, index) == 0);
  51. assert(node != tree.rnode);
  52. assert(item_delete(&tree, index) != 0);
  53. assert(node == tree.rnode);
  54. for (i = 0; i < max; i++) {
  55. struct item *item = item_lookup(&tree, i);
  56. assert(item != 0);
  57. assert(item->index == 0);
  58. }
  59. for (i = max; i < 2*max; i++)
  60. item_check_absent(&tree, i);
  61. if (!item_delete(&tree, 0)) {
  62. printf("failed to delete index %ld (order %d)\n", index, order); abort();
  63. }
  64. for (i = 0; i < 2*max; i++)
  65. item_check_absent(&tree, i);
  66. }
  67. void multiorder_checks(void)
  68. {
  69. int i;
  70. for (i = 0; i < 20; i++) {
  71. multiorder_check(200, i);
  72. multiorder_check(0, i);
  73. multiorder_check((1UL << i) + 1, i);
  74. }
  75. for (i = 0; i < 15; i++)
  76. multiorder_shrink((1UL << (i + RADIX_TREE_MAP_SHIFT)), i);
  77. }