File: graph_tests.c

package info (click to toggle)
cmph 0.7-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,888 kB
  • ctags: 497
  • sloc: sh: 9,125; ansic: 4,887; makefile: 52
file content (67 lines) | stat: -rw-r--r-- 1,483 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "../src/graph.h"

#define DEBUG
#include "../src/debug.h"

int main(int argc, char **argv)
{
	graph_iterator_t it;
	cmph_uint32 i, neighbor;
	graph_t *g = graph_new(5, 10);

	fprintf(stderr, "Building random graph\n");
	for (i = 0; i < 10; ++i)
	{
		cmph_uint32 v1 = i % 5;
		cmph_uint32 v2 = (i*2) % 5;
		if (v1 == v2) continue;
		graph_add_edge(g, v1, v2);
		DEBUGP("Added edge %u %u\n", v1, v2);
	}
	graph_print(g);
	graph_del_edge(g, 4, 3);
	graph_print(g);
	graph_clear_edges(g);
	graph_print(g);
	graph_destroy(g);

	fprintf(stderr, "Building cyclic graph\n");
	g = graph_new(4, 5);
	graph_add_edge(g, 0, 3);
	graph_add_edge(g, 0, 1);
	graph_add_edge(g, 1, 2);
	graph_add_edge(g, 2, 0);
	if (!graph_is_cyclic(g))
	{
		return 1;
	}
	graph_destroy(g);

	fprintf(stderr, "Building non-cyclic graph\n");
	g = graph_new(5, 4);
	graph_add_edge(g, 0, 1);
	graph_add_edge(g, 1, 2);
	graph_add_edge(g, 2, 3);
	graph_add_edge(g, 3, 4);

	if (graph_is_cyclic(g))
	{
		return 1;
	}

	fprintf(stderr, "Checking neighbors iterator\n");
	it = graph_neighbors_it(g, 1);
	neighbor = graph_next_neighbor(g, &it);
	DEBUGP("Neighbor is %u\n", neighbor);
	if (neighbor != 0 && neighbor != 2) return 1;
	neighbor = graph_next_neighbor(g, &it);
	DEBUGP("Neighbor is %u\n", neighbor);
	if (neighbor != 0 && neighbor != 2) return 1;
	neighbor = graph_next_neighbor(g, &it);
	DEBUGP("Neighbor is %u\n", neighbor);
	if (neighbor != GRAPH_NO_NEIGHBOR) return 1;


	graph_destroy(g);
	return 0;
}