1 /*
2 IGraph library.
3 Copyright (C) 2021 The igraph development team <igraph@igraph.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19 #include <igraph.h>
20 #include "test_utilities.inc"
21
chop_print_destroy(igraph_matrix_t * result)22 void chop_print_destroy(igraph_matrix_t *result) {
23 matrix_chop(result, 1e-10);
24 print_matrix(result);
25 igraph_matrix_destroy(result);
26 }
27
main()28 int main() {
29 igraph_t g;
30 igraph_matrix_t result;
31 igraph_vector_t roots, rootlevel;
32
33 printf("Empty graph check:\n");
34 igraph_small(&g, 0, 0, -1);
35 igraph_matrix_init(&result, 0, 0);
36 IGRAPH_ASSERT(igraph_layout_reingold_tilford_circular(&g, &result, IGRAPH_ALL, /*roots*/ NULL, /*rootlevel*/ NULL) == IGRAPH_SUCCESS);
37 chop_print_destroy(&result);
38 igraph_destroy(&g);
39
40 printf("Singleton graph check:\n");
41 igraph_small(&g, 1, 0, -1);
42 igraph_matrix_init(&result, 0, 0);
43 IGRAPH_ASSERT(igraph_layout_reingold_tilford_circular(&g, &result, IGRAPH_ALL, /*roots*/ NULL, /*rootlevel*/ NULL) == IGRAPH_SUCCESS);
44 chop_print_destroy(&result);
45 igraph_destroy(&g);
46
47 printf("Star graph check with given root:\n");
48 igraph_small(&g, 5, 1, 0,1, 0,2, 0,3, 0,4, -1);
49 igraph_matrix_init(&result, 0, 0);
50 igraph_vector_init_int(&roots, 1, 1);
51 IGRAPH_ASSERT(igraph_layout_reingold_tilford_circular(&g, &result, IGRAPH_OUT, &roots, /*rootlevel*/ NULL) == IGRAPH_SUCCESS);
52 chop_print_destroy(&result);
53 igraph_destroy(&g);
54 igraph_vector_destroy(&roots);
55
56 printf("Star graph check with root found by topological sort:\n");
57 igraph_small(&g, 5, 1, 1,0, 2,0, 3,0, 4,0, -1);
58 igraph_matrix_init(&result, 0, 0);
59 IGRAPH_ASSERT(igraph_layout_reingold_tilford_circular(&g, &result, IGRAPH_IN, NULL, /*rootlevel*/ NULL) == IGRAPH_SUCCESS);
60 chop_print_destroy(&result);
61 igraph_destroy(&g);
62
63 printf("Two minitrees without rootlevel:\n");
64 igraph_small(&g, 6, 1, 0,1, 0,2, 3,4, 3,5, -1);
65 igraph_matrix_init(&result, 0, 0);
66 igraph_vector_init_int(&roots, 2, 0, 3);
67 IGRAPH_ASSERT(igraph_layout_reingold_tilford_circular(&g, &result, IGRAPH_OUT, &roots, NULL) == IGRAPH_SUCCESS);
68 chop_print_destroy(&result);
69 igraph_destroy(&g);
70 igraph_vector_destroy(&roots);
71
72 printf("Two minitrees with rootlevel 10 and 20:\n");
73 igraph_small(&g, 6, 1, 0,1, 0,2, 3,4, 3,5, -1);
74 igraph_matrix_init(&result, 0, 0);
75 igraph_vector_init_int(&roots, 2, 0, 3);
76 igraph_vector_init_int(&rootlevel, 2, 10, 20);
77 IGRAPH_ASSERT(igraph_layout_reingold_tilford_circular(&g, &result, IGRAPH_OUT, &roots, &rootlevel) == IGRAPH_SUCCESS);
78 chop_print_destroy(&result);
79 igraph_destroy(&g);
80 igraph_vector_destroy(&roots);
81 igraph_vector_destroy(&rootlevel);
82
83 printf("Graph with just loops, triple edges and disconnected vertices:\n");
84 igraph_small(&g, 5, 1, 0,0, 0,0, 0,0, 1,2, 1,2, 1,2, -1);
85 igraph_matrix_init(&result, 0, 0);
86 IGRAPH_ASSERT(igraph_layout_reingold_tilford_circular(&g, &result, IGRAPH_ALL, NULL, /*rootlevel*/ NULL) == IGRAPH_SUCCESS);
87 chop_print_destroy(&result);
88 igraph_destroy(&g);
89
90 VERIFY_FINALLY_STACK();
91 igraph_set_error_handler(igraph_error_handler_ignore);
92
93 printf("Checking proper error handling:\n");
94 printf("Giving negative root.\n");
95 igraph_small(&g, 5, 1, 0,1, 0,2, 0,3, 0,4, -1);
96 igraph_matrix_init(&result, 0, 0);
97 igraph_vector_init_int(&roots, 1, -1);
98 IGRAPH_ASSERT(igraph_layout_reingold_tilford_circular(&g, &result, IGRAPH_OUT, &roots, /*rootlevel*/ NULL) == IGRAPH_EINVVID);
99 igraph_matrix_destroy(&result);
100 igraph_destroy(&g);
101 igraph_vector_destroy(&roots);
102
103 printf("Giving negative rootlevel.\n");
104 igraph_small(&g, 6, 1, 0,1, 0,2, 3,4, 3,5, -1);
105 igraph_matrix_init(&result, 0, 0);
106 igraph_vector_init_int(&roots, 2, 0, 3);
107 igraph_vector_init_int(&rootlevel, 2, -10, -20);
108 IGRAPH_ASSERT(igraph_layout_reingold_tilford_circular(&g, &result, IGRAPH_OUT, &roots, &rootlevel) == IGRAPH_EINVAL);
109 igraph_matrix_destroy(&result);
110 igraph_destroy(&g);
111 igraph_vector_destroy(&roots);
112 igraph_vector_destroy(&rootlevel);
113
114 VERIFY_FINALLY_STACK();
115 return 0;
116 }
117