]> git.ozlabs.org Git - ccan/blob - ccan/aga/test/shortcut1.c
aga,agar: New shortcut1 sample graph and testcases based on it
[ccan] / ccan / aga / test / shortcut1.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 *shortcut1_first_edge(const struct aga_graph *g,
13                                       const struct aga_node *n)
14 {
15         struct shortcut1_graph *s1g = container_of(g, struct shortcut1_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 *shortcut1_next_edge(const struct aga_graph *g,
33                                      const struct aga_node *n,
34                                      ptrint_t *e)
35 {
36         struct shortcut1_graph *s1g = container_of(g, struct shortcut1_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 shortcut1_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 shortcut1_graph *s1g = container_of(g, struct shortcut1_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                         ei->icost = 3;
71                 } else {
72                         assert(index == 2);
73                         ei->to = &s1g->sg.nodes[2];
74                 }
75                 break;
76
77         case 2:
78                 assert(index == 1);
79                 ei->to = &s1g->sg.nodes[3];
80                 break;
81
82         default:
83                 assert(0);
84         }
85         return 0;
86 }
87
88 void shortcut1_graph_init(struct shortcut1_graph *s1g)
89 {
90         simple_graph_init(&s1g->sg, shortcut1_first_edge,
91                           shortcut1_next_edge,
92                           shortcut1_edge_info);
93 }