]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_graph.c
2 * Test graph data structure.
3 * Copyright (C) 2018 Cumulus Networks, Inc.
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)
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
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
27 static void graph_custom_print_cb(struct graph_node
*gn
, struct buffer
*buf
)
30 char *gname
= gn
->data
;
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
;
36 snprintf(nbuf
, sizeof(nbuf
), " n%s -> n%s;\n", gname
, name
);
37 buffer_putstr(buf
, nbuf
);
41 int main(int argc
, char **argv
)
43 struct graph
*g
= graph_new();
44 struct graph_node
*gn
[NUMNODES
];
45 char names
[NUMNODES
][16];
48 for (unsigned int i
= 0; i
< NUMNODES
; i
++) {
49 snprintf(names
[i
], sizeof(names
[i
]), "%d", i
);
50 gn
[i
] = graph_new_node(g
, names
[i
], NULL
);
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]);
58 graph_add_edge(gn
[0], gn
[NUMNODES
- 1]);
59 graph_add_edge(gn
[NUMNODES
- 1], gn
[1]);
62 char *dumped
= graph_dump_dot(g
, gn
[0], graph_custom_print_cb
);
64 fprintf(stdout
, "%s", dumped
);
65 XFREE(MTYPE_TMP
, dumped
);
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
]);
72 /* remove some nodes */
73 for (unsigned int i
= 0; i
< NUMNODES
/ 2; i
++)
74 graph_delete_node(g
, gn
[i
]);
76 graph_delete_graph(g
);