]> git.proxmox.com Git - mirror_frr.git/blame - tests/lib/test_graph.c
*: auto-convert to SPDX License IDs
[mirror_frr.git] / tests / lib / test_graph.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
58f8a9ec
QY
2/*
3 * Test graph data structure.
4 * Copyright (C) 2018 Cumulus Networks, Inc.
5 * Quentin Young
58f8a9ec
QY
6 */
7#include <zebra.h>
8#include <graph.h>
9#include <memory.h>
10#include <buffer.h>
11
12#define NUMNODES 32
13
14static void graph_custom_print_cb(struct graph_node *gn, struct buffer *buf)
15{
16 char nbuf[64];
17 char *gname = gn->data;
18
19 for (unsigned int i = 0; i < vector_active(gn->to); i++) {
20 struct graph_node *adj = vector_slot(gn->to, i);
21 char *name = adj->data;
22
23 snprintf(nbuf, sizeof(nbuf), " n%s -> n%s;\n", gname, name);
24 buffer_putstr(buf, nbuf);
25 }
26}
27
28int main(int argc, char **argv)
29{
30 struct graph *g = graph_new();
31 struct graph_node *gn[NUMNODES];
32 char names[NUMNODES][16];
33
34 /* create vertices */
35 for (unsigned int i = 0; i < NUMNODES; i++) {
2ec42b85 36 snprintf(names[i], sizeof(names[i]), "%u", i);
58f8a9ec
QY
37 gn[i] = graph_new_node(g, names[i], NULL);
38 }
39
40 /* create edges */
41 for (unsigned int i = 1; i < NUMNODES - 1; i++) {
42 graph_add_edge(gn[0], gn[i]);
43 graph_add_edge(gn[i], gn[i + 1]);
44 }
45 graph_add_edge(gn[0], gn[NUMNODES - 1]);
46 graph_add_edge(gn[NUMNODES - 1], gn[1]);
47
48 /* print DOT */
49 char *dumped = graph_dump_dot(g, gn[0], graph_custom_print_cb);
50
51 fprintf(stdout, "%s", dumped);
52 XFREE(MTYPE_TMP, dumped);
53
54 /* remove some edges */
55 for (unsigned int i = NUMNODES - 1; i > NUMNODES / 2; --i)
56 for (unsigned int j = 0; j < NUMNODES; j++)
57 graph_remove_edge(gn[i], gn[j]);
58
59 /* remove some nodes */
60 for (unsigned int i = 0; i < NUMNODES / 2; i++)
61 graph_delete_node(g, gn[i]);
62
63 graph_delete_graph(g);
64}