]> git.ozlabs.org Git - ccan/blobdiff - ccan/aga/test/negacycle.c
aga,agar: Negative weight cycle testcase
[ccan] / ccan / aga / test / negacycle.c
diff --git a/ccan/aga/test/negacycle.c b/ccan/aga/test/negacycle.c
new file mode 100644 (file)
index 0000000..8eae4bf
--- /dev/null
@@ -0,0 +1,46 @@
+#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 *negacycle_first_edge(const struct aga_graph *g,
+                                     const struct aga_node *n)
+{
+       return int2ptr(1);
+}
+
+static ptrint_t *negacycle_next_edge(const struct aga_graph *g,
+                                    const struct aga_node *n,
+                                    ptrint_t *e)
+{
+       assert(ptr2int(e) == 1);
+       return NULL;
+}
+
+static int negacycle_edge_info(const struct aga_graph *g,
+                              const struct aga_node *n,
+                              ptrint_t *e, struct aga_edge_info *ei)
+{
+       struct negacycle_graph *ng = container_of(g, struct negacycle_graph,
+                                                  sg.g);
+       int ni = n - ng->sg.nodes;
+
+       assert(ptr2int(e) == 1);
+       ei->to = &ng->sg.nodes[(ni % 3) + 1];
+       if (ni == 3)
+               ei->icost = -3;
+       return 0;
+}
+
+void negacycle_graph_init(struct negacycle_graph *ng)
+{
+       simple_graph_init(&ng->sg, negacycle_first_edge,
+                         negacycle_next_edge,
+                         negacycle_edge_info);
+}