test_rdxtree.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2022 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * This test module tests the radix tree API.
  18. */
  19. #include <kern/rcu.h>
  20. #include <kern/rdxtree.h>
  21. #include <test/test.h>
  22. static int
  23. rdxtree_count (struct rdxtree *tree)
  24. {
  25. struct rdxtree_iter iter;
  26. void *ptr;
  27. int count = 0;
  28. rdxtree_for_each (tree, &iter, ptr)
  29. ++count;
  30. return (count);
  31. }
  32. #define RDXTREE_SIZE 1024
  33. TEST_DEFERRED (rdxtree)
  34. {
  35. struct rdxtree tree;
  36. int error;
  37. void *ptr;
  38. int *val = ((int *)0) + 1;
  39. rdxtree_init (&tree, RDXTREE_KEY_ALLOC);
  40. for (int i = 0; i < RDXTREE_SIZE; ++i)
  41. {
  42. error = rdxtree_insert (&tree, (rdxtree_key_t)i, val + i);
  43. assert (! error);
  44. }
  45. assert (rdxtree_count (&tree) == RDXTREE_SIZE);
  46. for (int i = RDXTREE_SIZE - 1; i >= 0; --i)
  47. {
  48. ptr = rdxtree_lookup (&tree, (rdxtree_key_t)i);
  49. assert (ptr == val + i);
  50. }
  51. assert (val + 33 == rdxtree_remove (&tree, 33));
  52. val += RDXTREE_SIZE;
  53. rdxtree_key_t key;
  54. error = rdxtree_insert_alloc (&tree, val, &key);
  55. assert (key == 33);
  56. assert (! error);
  57. ++val;
  58. void **slot;
  59. error = rdxtree_insert_alloc_slot (&tree, val, &key, &slot);
  60. assert (! error);
  61. assert (key >= RDXTREE_SIZE);
  62. assert (rdxtree_load_slot (slot) == val);
  63. ptr = rdxtree_replace_slot (slot, val + 1);
  64. assert (ptr == val);
  65. assert (rdxtree_load_slot (slot) == val + 1);
  66. assert (!rdxtree_lookup (&tree, key + 2));
  67. assert (!rdxtree_lookup_slot (&tree, key + 2));
  68. assert (!rdxtree_remove (&tree, key + 2));
  69. rdxtree_remove_all (&tree);
  70. rcu_wait ();
  71. assert (rdxtree_count (&tree) == 0);
  72. return (TEST_OK);
  73. }