run.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <ccan/idtree/idtree.c>
  2. #include <ccan/tap/tap.h>
  3. #include <limits.h>
  4. #define ALLOC_MAX (2 * IDTREE_SIZE)
  5. static bool check_tal_parent(const tal_t *parent, const tal_t *ctx)
  6. {
  7. while (ctx) {
  8. if (ctx == parent)
  9. return true;
  10. ctx = tal_parent(ctx);
  11. }
  12. return false;
  13. }
  14. int main(int argc, char *argv[])
  15. {
  16. unsigned int i;
  17. const char allocated[ALLOC_MAX] = { 0 };
  18. struct idtree *idtree;
  19. void *ctx;
  20. plan_tests(ALLOC_MAX * 5 + 2);
  21. ctx = tal(NULL, char);
  22. idtree = idtree_new(ctx);
  23. ok1(check_tal_parent(ctx, idtree));
  24. for (i = 0; i < ALLOC_MAX; i++) {
  25. int id = idtree_add(idtree, &allocated[i], ALLOC_MAX-1);
  26. ok1(id == i);
  27. ok1(idtree_lookup(idtree, i) == &allocated[i]);
  28. }
  29. ok1(idtree_add(idtree, &allocated[i], ALLOC_MAX-1) == -1);
  30. /* Remove every second one. */
  31. for (i = 0; i < ALLOC_MAX; i += 2)
  32. ok1(idtree_remove(idtree, i));
  33. for (i = 0; i < ALLOC_MAX; i++) {
  34. if (i % 2 == 0)
  35. ok1(!idtree_lookup(idtree, i));
  36. else
  37. ok1(idtree_lookup(idtree, i) == &allocated[i]);
  38. }
  39. /* Now, finally, reallocate. */
  40. for (i = 0; i < ALLOC_MAX/2; i++) {
  41. ok1(idtree_add(idtree, &allocated[i*2], INT_MAX) == i * 2);
  42. }
  43. for (i = 0; i < ALLOC_MAX; i++) {
  44. ok1(idtree_lookup(idtree, i) == &allocated[i]);
  45. }
  46. tal_free(ctx);
  47. exit(exit_status());
  48. }