1 /* -*- mode: C -*-  */
2 /*
3    IGraph library.
4    Copyright (C) 2006-2021  The igraph development team <igraph@igraph.org>
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <https://www.gnu.org/licenses/>.
18 */
19 
20 #include <igraph.h>
21 
22 #include "operators/rewire_internal.h"
23 
24 #include "test_utilities.inc"
25 
check_rewiring(igraph_tree_mode_t tree_mode,igraph_bool_t use_adjlist,igraph_bool_t allow_loops,const char * description)26 static void check_rewiring(igraph_tree_mode_t tree_mode, igraph_bool_t use_adjlist, igraph_bool_t allow_loops, const char* description) {
27 
28     igraph_t g;
29     igraph_vector_t indegree_before, outdegree_before, indegree_after, outdegree_after;
30 
31     igraph_tree(&g, 10, 3, tree_mode);
32 
33     igraph_vector_init(&indegree_before, 0);
34     igraph_vector_init(&outdegree_before, 0);
35     igraph_degree(&g, &indegree_before, igraph_vss_all(), IGRAPH_IN, 1);
36     igraph_degree(&g, &outdegree_before, igraph_vss_all(), IGRAPH_OUT, 1);
37 
38     igraph_i_rewire(&g, 1000, allow_loops ? IGRAPH_REWIRING_SIMPLE_LOOPS : IGRAPH_REWIRING_SIMPLE, use_adjlist);
39 
40     igraph_vector_init(&indegree_after, 0);
41     igraph_vector_init(&outdegree_after, 0);
42     igraph_degree(&g, &indegree_after, igraph_vss_all(), IGRAPH_IN, 1);
43     igraph_degree(&g, &outdegree_after, igraph_vss_all(), IGRAPH_OUT, 1);
44 
45     if ((!igraph_vector_all_e(&indegree_before, &indegree_after)) ||
46         (!igraph_vector_all_e(&outdegree_before, &outdegree_after))) {
47 
48         printf("%s: graph degrees changed. Rewired graph is below.\n", description);
49         print_graph(&g);
50 
51         abort();
52     }
53 
54     igraph_destroy(&g);
55     igraph_vector_destroy(&indegree_before);
56     igraph_vector_destroy(&outdegree_before);
57     igraph_vector_destroy(&indegree_after);
58     igraph_vector_destroy(&outdegree_after);
59 
60 }
61 
main()62 int main() {
63     igraph_rng_seed(igraph_rng_default(), 3925);
64 
65     /* Short test for the top-level igraph_rewire() functions (instead of igraph_i_rewire()). */
66     {
67         igraph_t graph;
68         igraph_ring(&graph, 12, IGRAPH_UNDIRECTED, /* mutual= */ 0, /* circular= */ 1);
69         igraph_rewire(&graph, 50, IGRAPH_REWIRING_SIMPLE);
70         igraph_destroy(&graph);
71     }
72 
73     check_rewiring(IGRAPH_TREE_OUT, 0, 0, "Directed, no loops, standard-method");
74     check_rewiring(IGRAPH_TREE_OUT, 1, 0, "Directed, no loops, adjlist-method");
75     check_rewiring(IGRAPH_TREE_OUT, 0, 1, "Directed, loops, standard-method");
76     check_rewiring(IGRAPH_TREE_OUT, 1, 1, "Directed, loops, adjlist-method");
77     check_rewiring(IGRAPH_TREE_UNDIRECTED, 0, 0, "Undirected, no loops, standard-method");
78     check_rewiring(IGRAPH_TREE_UNDIRECTED, 1, 0, "Undirected, no loops, adjlist-method");
79     check_rewiring(IGRAPH_TREE_UNDIRECTED, 0, 1, "Undirected, loops, standard-method");
80     check_rewiring(IGRAPH_TREE_UNDIRECTED, 1, 1, "Undirected, loops, adjlist-method");
81 
82     VERIFY_FINALLY_STACK();
83     return 0;
84 }
85