]> git.ozlabs.org Git - ccan/blob - ccan/aga/test/negacycle.c
aga,agar: Negative weight cycle testcase
[ccan] / ccan / aga / test / negacycle.c
1 #include "config.h"
2
3 #include <assert.h>
4
5 #include <ccan/container_of/container_of.h>
6 #include <ccan/ptrint/ptrint.h>
7
8 #include <ccan/aga/aga.h>
9
10 #include "simple-graph.h"
11
12 static ptrint_t *negacycle_first_edge(const struct aga_graph *g,
13                                       const struct aga_node *n)
14 {
15         return int2ptr(1);
16 }
17
18 static ptrint_t *negacycle_next_edge(const struct aga_graph *g,
19                                      const struct aga_node *n,
20                                      ptrint_t *e)
21 {
22         assert(ptr2int(e) == 1);
23         return NULL;
24 }
25
26 static int negacycle_edge_info(const struct aga_graph *g,
27                                const struct aga_node *n,
28                                ptrint_t *e, struct aga_edge_info *ei)
29 {
30         struct negacycle_graph *ng = container_of(g, struct negacycle_graph,
31                                                    sg.g);
32         int ni = n - ng->sg.nodes;
33
34         assert(ptr2int(e) == 1);
35         ei->to = &ng->sg.nodes[(ni % 3) + 1];
36         if (ni == 3)
37                 ei->icost = -3;
38         return 0;
39 }
40
41 void negacycle_graph_init(struct negacycle_graph *ng)
42 {
43         simple_graph_init(&ng->sg, negacycle_first_edge,
44                           negacycle_next_edge,
45                           negacycle_edge_info);
46 }