]>
Commit | Line | Data |
---|---|---|
58f8a9ec QY |
1 | /* |
2 | * Test graph data structure. | |
3 | * Copyright (C) 2018 Cumulus Networks, Inc. | |
4 | * Quentin Young | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the Free | |
8 | * Software Foundation; either version 2 of the License, or (at your option) | |
9 | * any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
14 | * more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | #include <zebra.h> | |
21 | #include <graph.h> | |
22 | #include <memory.h> | |
23 | #include <buffer.h> | |
24 | ||
25 | #define NUMNODES 32 | |
26 | ||
27 | static void graph_custom_print_cb(struct graph_node *gn, struct buffer *buf) | |
28 | { | |
29 | char nbuf[64]; | |
30 | char *gname = gn->data; | |
31 | ||
32 | for (unsigned int i = 0; i < vector_active(gn->to); i++) { | |
33 | struct graph_node *adj = vector_slot(gn->to, i); | |
34 | char *name = adj->data; | |
35 | ||
36 | snprintf(nbuf, sizeof(nbuf), " n%s -> n%s;\n", gname, name); | |
37 | buffer_putstr(buf, nbuf); | |
38 | } | |
39 | } | |
40 | ||
41 | int main(int argc, char **argv) | |
42 | { | |
43 | struct graph *g = graph_new(); | |
44 | struct graph_node *gn[NUMNODES]; | |
45 | char names[NUMNODES][16]; | |
46 | ||
47 | /* create vertices */ | |
48 | for (unsigned int i = 0; i < NUMNODES; i++) { | |
2ec42b85 | 49 | snprintf(names[i], sizeof(names[i]), "%u", i); |
58f8a9ec QY |
50 | gn[i] = graph_new_node(g, names[i], NULL); |
51 | } | |
52 | ||
53 | /* create edges */ | |
54 | for (unsigned int i = 1; i < NUMNODES - 1; i++) { | |
55 | graph_add_edge(gn[0], gn[i]); | |
56 | graph_add_edge(gn[i], gn[i + 1]); | |
57 | } | |
58 | graph_add_edge(gn[0], gn[NUMNODES - 1]); | |
59 | graph_add_edge(gn[NUMNODES - 1], gn[1]); | |
60 | ||
61 | /* print DOT */ | |
62 | char *dumped = graph_dump_dot(g, gn[0], graph_custom_print_cb); | |
63 | ||
64 | fprintf(stdout, "%s", dumped); | |
65 | XFREE(MTYPE_TMP, dumped); | |
66 | ||
67 | /* remove some edges */ | |
68 | for (unsigned int i = NUMNODES - 1; i > NUMNODES / 2; --i) | |
69 | for (unsigned int j = 0; j < NUMNODES; j++) | |
70 | graph_remove_edge(gn[i], gn[j]); | |
71 | ||
72 | /* remove some nodes */ | |
73 | for (unsigned int i = 0; i < NUMNODES / 2; i++) | |
74 | graph_delete_node(g, gn[i]); | |
75 | ||
76 | graph_delete_graph(g); | |
77 | } |