api-dfs.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "config.h"
  2. #include <stddef.h>
  3. #include <assert.h>
  4. #include <ccan/tal/tal.h>
  5. #include <ccan/tap/tap.h>
  6. #include <ccan/array_size/array_size.h>
  7. #include <ccan/ptrint/ptrint.h>
  8. #include <ccan/agar/agar.h>
  9. #include "simple-graphr.h"
  10. #define test_dfs_partial(sr, first, ...) \
  11. do { \
  12. int cmp[] = { __VA_ARGS__ }; \
  13. bool stillok = true; \
  14. const void *node; \
  15. int i = 0; \
  16. agar_dfs(node, (sr), int2ptr(first)) { \
  17. int index = ptr2int(node); \
  18. diag("Visited %d\n", index); \
  19. if (i >= ARRAY_SIZE(cmp) || (index != cmp[i])) \
  20. stillok = false; \
  21. i++; \
  22. } \
  23. ok1(stillok); \
  24. } while (0)
  25. #define test_dfs(gr, first, ...) \
  26. do { \
  27. struct agar_state *sr; \
  28. ok1((sr = agar_dfs_new(NULL, (gr)))); \
  29. test_dfs_partial(sr, first, __VA_ARGS__); \
  30. tal_free(sr); \
  31. } while (0)
  32. int main(void)
  33. {
  34. struct trivial_graphr tgr;
  35. struct parallel_graphr pgr;
  36. struct full_graphr fgr;
  37. struct chain_graphr cgr;
  38. struct grid_graphr ggr1, ggr2;
  39. struct error_graphr egr;
  40. struct traversal1_graphr t1gr;
  41. struct agar_state *sr;
  42. const void *nr;
  43. plan_tests(2 * 13 + 12 + 10);
  44. trivial_graphr_init(&tgr);
  45. test_dfs(&tgr.gr, 1, 1);
  46. parallel_graphr_init(&pgr, 3);
  47. test_dfs(&pgr.gr, 1, 1, 2);
  48. full_graphr_init(&fgr, 5);
  49. test_dfs(&fgr.gr, 1, 1, 2, 3, 4, 5);
  50. test_dfs(&fgr.gr, 3, 3, 1, 2, 4, 5);
  51. chain_graphr_init(&cgr, 8);
  52. test_dfs(&cgr.fgr.gr, 1, 1, 2, 3, 4, 5, 6, 7, 8);
  53. test_dfs(&cgr.fgr.gr, 8, 8, 7, 6, 5, 4, 3, 2, 1);
  54. test_dfs(&cgr.fgr.gr, 5, 5, 4, 3, 2, 1, 6, 7, 8);
  55. grid_graphr_init(&ggr1, 3, 3, true, true, false, false);
  56. test_dfs(&ggr1.gr, 1, 1, 2, 3, 6, 9, 5, 8, 4, 7);
  57. test_dfs(&ggr1.gr, 5, 5, 6, 9, 8);
  58. test_dfs(&ggr1.gr, 9, 9);
  59. grid_graphr_init(&ggr2, 3, 3, true, true, true, true);
  60. test_dfs(&ggr2.gr, 1, 1, 2, 3, 6, 9, 8, 7, 4, 5);
  61. test_dfs(&ggr2.gr, 5, 5, 6, 9, 8, 7, 4, 1, 2, 3);
  62. test_dfs(&ggr2.gr, 9, 9, 8, 7, 4, 5, 6, 3, 2, 1);
  63. error_graphr_init(&egr);
  64. test_dfs(&egr.gr, 1, 1, 2);
  65. ok((sr = agar_dfs_new(NULL, &egr.gr)), "started error traversal");
  66. ok1(agar_dfs_explore(sr, int2ptr(3), &nr));
  67. ok(ptr2int(nr) == 3, "Expected node #3, actually #%ld", ptr2int(nr));
  68. ok1(agar_dfs_explore(sr, nr, &nr));
  69. ok(ptr2int(nr) == 4, "Expected node #4, actually #%ld", ptr2int(nr));
  70. ok1(!agar_dfs_explore(sr, nr, &nr));
  71. ok(agar_error(sr) == -1, "Error is %d (expected -1)", agar_error(sr));
  72. ok1(!agar_dfs_explore(sr, nr, &nr));
  73. tal_free(sr);
  74. test_dfs(&egr.gr, 1, 1, 2);
  75. traversal1_graphr_init(&t1gr);
  76. test_dfs(&t1gr.gr, 1, 1, 2, 4, 5, 3, 6);
  77. test_dfs(&t1gr.gr, 9, 9, 8, 6, 5, 7, 4);
  78. ok1((sr = agar_dfs_new(NULL, &t1gr.gr)));
  79. test_dfs_partial(sr, 1, 1, 2, 4, 5, 3, 6);
  80. test_dfs_partial(sr, 9, 9, 8, 7);
  81. tal_free(sr);
  82. ok1((sr = agar_dfs_new(NULL, &t1gr.gr)));
  83. test_dfs_partial(sr, 9, 9, 8, 6, 5, 7, 4);
  84. test_dfs_partial(sr, 1, 1, 2, 3);
  85. tal_free(sr);
  86. return exit_status();
  87. }