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
make_box(int vertices,float half_size,igraph_vector_t * bounds)22 void make_box(int vertices, float half_size, igraph_vector_t *bounds) {
23 for (int i = 0; i < 4; i++) {
24 igraph_vector_init(&bounds[i], vertices);
25 }
26 igraph_vector_fill(&bounds[0], -half_size);
27 igraph_vector_fill(&bounds[1], half_size);
28 igraph_vector_fill(&bounds[2], -half_size);
29 igraph_vector_fill(&bounds[3], half_size);
30 }
31
destroy_bounds(igraph_vector_t * bounds)32 void destroy_bounds(igraph_vector_t *bounds) {
33 for (int i = 0; i < 4; i++) {
34 igraph_vector_destroy(&bounds[i]);
35 }
36 }
37
check_and_destroy(igraph_matrix_t * result,igraph_real_t half_size)38 void check_and_destroy(igraph_matrix_t *result, igraph_real_t half_size) {
39 igraph_real_t min, max;
40 igraph_matrix_minmax(result, &min, &max);
41 IGRAPH_ASSERT(min >= -half_size);
42 IGRAPH_ASSERT(max <= half_size);
43 igraph_matrix_destroy(result);
44 }
45
main()46 int main() {
47 igraph_t g;
48 igraph_matrix_t result;
49 igraph_vector_t bounds[4];
50 igraph_vector_t weights;
51 igraph_real_t seed[20] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, -0.1, -0.2, -0.3, -0.4, -0.5, -0.6, -0.7, -0.8, -0.9, -1.0};
52
53 igraph_rng_seed(igraph_rng_default(), 42);
54
55 printf("Empty graph.\n");
56 igraph_small(&g, 0, 0, -1);
57 igraph_matrix_init(&result, 0, 0);
58 IGRAPH_ASSERT(igraph_layout_fruchterman_reingold(&g, &result, /*use_seed*/ 0,
59 /*niter*/ 100, /*start_temp*/ 1.0, IGRAPH_LAYOUT_NOGRID,
60 /*weight*/ NULL, /*minx*/ NULL, /*maxx*/ NULL, /*miny*/ NULL,
61 /*maxy*/ NULL) == IGRAPH_SUCCESS);
62 print_matrix(&result);
63 igraph_matrix_destroy(&result);
64 igraph_destroy(&g);
65
66 printf("Singleton graph in a box.\n");
67 igraph_small(&g, 1, 0, -1);
68 igraph_matrix_init(&result, 0, 0);
69 make_box(1, 1.0, bounds);
70 IGRAPH_ASSERT(igraph_layout_fruchterman_reingold(&g, &result, /*use_seed*/ 0,
71 /*niter*/ 100, /*start_temp*/ 1.0, IGRAPH_LAYOUT_NOGRID,
72 /*weights*/ NULL, &bounds[0], &bounds[1], &bounds[2], &bounds[3]) == IGRAPH_SUCCESS);
73 check_and_destroy(&result, 1.0);
74 igraph_destroy(&g);
75 destroy_bounds(bounds);
76
77 printf("A few tests with a disconnected graph of 10 vertices with loops in a box from -1 to 1.\n");
78 igraph_small(&g, 10, 0, 0,1, 1,2, 2,0, 5,6, 6,7, 7,6, 7,7, 8,8, -1);
79 igraph_vector_init(&weights, 8);
80 igraph_vector_fill(&weights, 100);
81 make_box(10, 1.0, bounds);
82 printf("Without weights, grid or bounds.\n");
83 igraph_matrix_init(&result, 0, 0);
84 IGRAPH_ASSERT(igraph_layout_fruchterman_reingold(&g, &result, /*use_seed*/ 0,
85 /*niter*/ 100, /*start_temp*/ 10.0, IGRAPH_LAYOUT_NOGRID,
86 /*weight*/ NULL, /*minx*/ NULL, /*maxx*/ NULL, /*miny*/ NULL,
87 /*maxy*/ NULL) == IGRAPH_SUCCESS);
88 check_and_destroy(&result, 50.0);
89
90 printf("With weights and no grid.\n");
91 igraph_matrix_init(&result, 0, 0);
92 IGRAPH_ASSERT(igraph_layout_fruchterman_reingold(&g, &result, /*use_seed*/ 0,
93 /*niter*/ 100, /*start_temp*/ 1.0, IGRAPH_LAYOUT_NOGRID,
94 /*weight*/ NULL, /*minx*/ NULL, /*maxx*/ NULL, /*miny*/ NULL,
95 /*maxy*/ NULL) == IGRAPH_SUCCESS);
96 check_and_destroy(&result, 50.0);
97
98 printf("With weights and grid and high temperature.\n");
99 igraph_matrix_init(&result, 0, 0);
100 IGRAPH_ASSERT(igraph_layout_fruchterman_reingold(&g, &result, /*use_seed*/ 0,
101 /*niter*/ 10, /*start_temp*/ 1e10, IGRAPH_LAYOUT_GRID,
102 &weights, &bounds[0], &bounds[1], &bounds[2], &bounds[3]) == IGRAPH_SUCCESS);
103 check_and_destroy(&result, 1.0);
104
105 printf("With weights and grid and high temperature and seed.\n");
106 matrix_init_real_row_major(&result, 10, 2, seed);
107 IGRAPH_ASSERT(igraph_layout_fruchterman_reingold(&g, &result, /*use_seed*/ 1,
108 /*niter*/ 10, /*start_temp*/ 1e10, IGRAPH_LAYOUT_GRID,
109 &weights, &bounds[0], &bounds[1], &bounds[2], &bounds[3]) == IGRAPH_SUCCESS);
110 check_and_destroy(&result, 1.0);
111 igraph_destroy(&g);
112
113 printf("Full graph of 5 vertices, seed and no iterations:\n");
114 igraph_full(&g, 5, 0, 0);
115 matrix_init_real_row_major(&result, 5, 2, seed);
116 IGRAPH_ASSERT(igraph_layout_fruchterman_reingold(&g, &result, /*use_seed*/ 1,
117 /*niter*/ 0, /*start_temp*/ 100, IGRAPH_LAYOUT_GRID,
118 /*weight*/ NULL, /*minx*/ NULL, /*maxx*/ NULL, /*miny*/ NULL,
119 /*maxy*/ NULL) == IGRAPH_SUCCESS);
120 print_matrix(&result);
121 igraph_matrix_destroy(&result);
122 igraph_destroy(&g);
123 destroy_bounds(bounds);
124 igraph_vector_destroy(&weights);
125
126 VERIFY_FINALLY_STACK();
127 return 0;
128 }
129