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 
main()22 int main() {
23     igraph_t g, g_copy;
24     igraph_bool_t same;
25     igraph_vector_t degrees;
26     igraph_vs_t vertices;
27     igraph_rng_seed(igraph_rng_default(), 42);
28 
29     /*No edges, should just return the same graph*/
30     igraph_small(&g, 5, IGRAPH_DIRECTED, -1);
31     IGRAPH_ASSERT(igraph_rewire_directed_edges(&g, /*probability*/ 0.1,
32                                                /*loops*/ 0, /*mode*/ IGRAPH_ALL)
33                   == IGRAPH_SUCCESS);
34     IGRAPH_ASSERT(igraph_ecount(&g) == 0);
35     IGRAPH_ASSERT(igraph_vcount(&g) == 5);
36     igraph_destroy(&g);
37 
38     /*No rewire*/
39     igraph_small(&g, 10, IGRAPH_DIRECTED, 0,1, 0,3, 5,4, 4,8, 9,2, 9,3, 9,7, 7,7, 7,8, -1);
40     igraph_copy(&g_copy, &g);
41     IGRAPH_ASSERT(igraph_rewire_directed_edges(&g, /*probability*/ 0.0,
42                                                /*loops*/ 0, /*mode*/ IGRAPH_ALL)
43                   == IGRAPH_SUCCESS);
44     IGRAPH_ASSERT(igraph_is_same_graph(&g, &g_copy, &same) == IGRAPH_SUCCESS);
45     IGRAPH_ASSERT(same);
46 
47     /*Rewire*/
48     IGRAPH_ASSERT(igraph_rewire_directed_edges(&g, /*probability*/ 0.5,
49                                                /*loops*/ 1, /*mode*/ IGRAPH_ALL)
50                   == IGRAPH_SUCCESS);
51     IGRAPH_ASSERT(igraph_is_same_graph(&g, &g_copy, &same) == IGRAPH_SUCCESS);
52     IGRAPH_ASSERT(!same); /*guaranteed for this seed*/
53     IGRAPH_ASSERT(igraph_ecount(&g) == 9);
54     IGRAPH_ASSERT(igraph_vcount(&g) == 10);
55     igraph_destroy(&g);
56     igraph_destroy(&g_copy);
57 
58     /*Out-star remains out-star if outs are moved*/
59     igraph_small(&g, 10, IGRAPH_DIRECTED, 0,1, 0,2, 0,3, 0,4, 0,5, 0,6, 0,7, 0,8, 0,9, -1);
60     IGRAPH_ASSERT(igraph_rewire_directed_edges(&g, /*probability*/ 1.0,
61                                                /*loops*/ 0, /*mode*/ IGRAPH_OUT)
62                   == IGRAPH_SUCCESS);
63     igraph_vector_init(&degrees, 0);
64     igraph_vs_1(&vertices, 0);
65     igraph_degree(&g, &degrees, vertices, IGRAPH_ALL, 0);
66     IGRAPH_ASSERT(VECTOR(degrees)[0] == 9);
67     igraph_vector_destroy(&degrees);
68     igraph_vs_destroy(&vertices);
69     igraph_destroy(&g);
70 
71     /*Check if multiple edges are created when using mode == IGRAPH_ALL*/
72     igraph_small(&g, 5, IGRAPH_DIRECTED, 0,1, 0,2, 0,3, 0,4, 1,2, 1,3, 1,4, 2,3, 2,4, 3,4, -1);
73     IGRAPH_ASSERT(igraph_rewire_directed_edges(&g, /*probability*/ 1.0,
74                                                /*loops*/ 0, /*mode*/ IGRAPH_ALL)
75                   == IGRAPH_SUCCESS);
76     print_graph_canon(&g);
77 
78     /*A few erroneous calls*/
79     VERIFY_FINALLY_STACK();
80     igraph_set_error_handler(igraph_error_handler_ignore);
81 
82     IGRAPH_ASSERT(igraph_rewire_directed_edges(&g, /*probability*/ -0.1, /*loops*/ 0, /*mode*/ IGRAPH_ALL) == IGRAPH_EINVAL);
83     IGRAPH_ASSERT(igraph_rewire_directed_edges(&g, /*probability*/ 1.1, /*loops*/ 0, /*mode*/ IGRAPH_ALL) == IGRAPH_EINVAL);
84     igraph_destroy(&g);
85 
86     VERIFY_FINALLY_STACK();
87     return 0;
88 }
89