]> git.ozlabs.org Git - ccan/blob - ccan/aga/test/shortcut2.c
aga,agar: New shortcut2 sample graph and testcases based on it
[ccan] / ccan / aga / test / shortcut2.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 *shortcut2_first_edge(const struct aga_graph *g,
13                                       const struct aga_node *n)
14 {
15         struct shortcut2_graph *s1g = container_of(g, struct shortcut2_graph,
16                                                    sg.g);
17         int ni = n - s1g->sg.nodes;
18
19         switch (ni) {
20         case 1:
21         case 2:
22                 return int2ptr(1);
23
24         case 3:
25                 return NULL;
26
27         default:
28                 assert(0);
29         }
30 }
31
32 static ptrint_t *shortcut2_next_edge(const struct aga_graph *g,
33                                      const struct aga_node *n,
34                                      ptrint_t *e)
35 {
36         struct shortcut2_graph *s1g = container_of(g, struct shortcut2_graph,
37                                                    sg.g);
38         int ni = n - s1g->sg.nodes;
39         int index = ptr2int(e);
40
41         switch (ni) {
42         case 1:
43                 if (index == 1)
44                         return int2ptr(2);
45                 assert(index == 2);
46                 return NULL;
47
48         case 2:
49                 assert(index == 1);
50                 return NULL;
51
52         default:
53                 assert(0);
54         }
55 }
56
57 static int shortcut2_edge_info(const struct aga_graph *g,
58                                const struct aga_node *n,
59                                ptrint_t *e, struct aga_edge_info *ei)
60 {
61         struct shortcut2_graph *s1g = container_of(g, struct shortcut2_graph,
62                                                    sg.g);
63         int ni = n - s1g->sg.nodes;
64         int index = ptr2int(e);
65
66         switch (ni) {
67         case 1:
68                 if (index == 1) {
69                         ei->to = &s1g->sg.nodes[3];
70                 } else {
71                         assert(index == 2);
72                         ei->to = &s1g->sg.nodes[2];
73                 }
74                 ei->icost = 2;
75                 break;
76
77         case 2:
78                 assert(index == 1);
79                 ei->to = &s1g->sg.nodes[3];
80                 ei->icost = -1;
81                 break;
82
83         default:
84                 assert(0);
85         }
86         return 0;
87 }
88
89 void shortcut2_graph_init(struct shortcut2_graph *s1g)
90 {
91         simple_graph_init(&s1g->sg, shortcut2_first_edge,
92                           shortcut2_next_edge,
93                           shortcut2_edge_info);
94 }