]> git.ozlabs.org Git - ccan/blobdiff - ccan/aga/test/shortcut1.c
aga,agar: New shortcut1 sample graph and testcases based on it
[ccan] / ccan / aga / test / shortcut1.c
diff --git a/ccan/aga/test/shortcut1.c b/ccan/aga/test/shortcut1.c
new file mode 100644 (file)
index 0000000..bea05a6
--- /dev/null
@@ -0,0 +1,93 @@
+#include "config.h"
+
+#include <assert.h>
+
+#include <ccan/container_of/container_of.h>
+#include <ccan/ptrint/ptrint.h>
+
+#include <ccan/aga/aga.h>
+
+#include "simple-graph.h"
+
+static ptrint_t *shortcut1_first_edge(const struct aga_graph *g,
+                                     const struct aga_node *n)
+{
+       struct shortcut1_graph *s1g = container_of(g, struct shortcut1_graph,
+                                                  sg.g);
+       int ni = n - s1g->sg.nodes;
+
+       switch (ni) {
+       case 1:
+       case 2:
+               return int2ptr(1);
+
+       case 3:
+               return NULL;
+
+       default:
+               assert(0);
+       }
+}
+
+static ptrint_t *shortcut1_next_edge(const struct aga_graph *g,
+                                    const struct aga_node *n,
+                                    ptrint_t *e)
+{
+       struct shortcut1_graph *s1g = container_of(g, struct shortcut1_graph,
+                                                  sg.g);
+       int ni = n - s1g->sg.nodes;
+       int index = ptr2int(e);
+
+       switch (ni) {
+       case 1:
+               if (index == 1)
+                       return int2ptr(2);
+               assert(index == 2);
+               return NULL;
+
+       case 2:
+               assert(index == 1);
+               return NULL;
+
+       default:
+               assert(0);
+       }
+}
+
+static int shortcut1_edge_info(const struct aga_graph *g,
+                              const struct aga_node *n,
+                              ptrint_t *e, struct aga_edge_info *ei)
+{
+       struct shortcut1_graph *s1g = container_of(g, struct shortcut1_graph,
+                                                  sg.g);
+       int ni = n - s1g->sg.nodes;
+       int index = ptr2int(e);
+
+       switch (ni) {
+       case 1:
+               if (index == 1) {
+                       ei->to = &s1g->sg.nodes[3];
+                       ei->icost = 3;
+               } else {
+                       assert(index == 2);
+                       ei->to = &s1g->sg.nodes[2];
+               }
+               break;
+
+       case 2:
+               assert(index == 1);
+               ei->to = &s1g->sg.nodes[3];
+               break;
+
+       default:
+               assert(0);
+       }
+       return 0;
+}
+
+void shortcut1_graph_init(struct shortcut1_graph *s1g)
+{
+       simple_graph_init(&s1g->sg, shortcut1_first_edge,
+                         shortcut1_next_edge,
+                         shortcut1_edge_info);
+}