run.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <ccan/idtree/idtree.c>
  2. #include <ccan/tap/tap.h>
  3. #include <limits.h>
  4. #define ALLOC_MAX (2 * IDTREE_SIZE)
  5. int main(int argc, char *argv[])
  6. {
  7. unsigned int i;
  8. const char allocated[ALLOC_MAX] = { 0 };
  9. struct idtree *idtree;
  10. void *ctx;
  11. plan_tests(ALLOC_MAX * 5 + 2);
  12. ctx = talloc_named_const(NULL, 1, "test root");
  13. idtree = idtree_new(ctx);
  14. ok1(talloc_find_parent_byname(idtree, "test root") == ctx);
  15. for (i = 0; i < ALLOC_MAX; i++) {
  16. int id = idtree_add(idtree, &allocated[i], ALLOC_MAX-1);
  17. ok1(id == i);
  18. ok1(idtree_lookup(idtree, i) == &allocated[i]);
  19. }
  20. ok1(idtree_add(idtree, &allocated[i], ALLOC_MAX-1) == -1);
  21. /* Remove every second one. */
  22. for (i = 0; i < ALLOC_MAX; i += 2)
  23. ok1(idtree_remove(idtree, i));
  24. for (i = 0; i < ALLOC_MAX; i++) {
  25. if (i % 2 == 0)
  26. ok1(!idtree_lookup(idtree, i));
  27. else
  28. ok1(idtree_lookup(idtree, i) == &allocated[i]);
  29. }
  30. /* Now, finally, reallocate. */
  31. for (i = 0; i < ALLOC_MAX/2; i++) {
  32. ok1(idtree_add(idtree, &allocated[i*2], INT_MAX) == i * 2);
  33. }
  34. for (i = 0; i < ALLOC_MAX; i++) {
  35. ok1(idtree_lookup(idtree, i) == &allocated[i]);
  36. }
  37. talloc_free(ctx);
  38. exit(exit_status());
  39. }