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 
call_and_print(igraph_integer_t n,igraph_matrix_t * pref_matrix,igraph_vector_int_t * block_sizes,igraph_bool_t directed,igraph_bool_t loops)22 void call_and_print(igraph_integer_t n, igraph_matrix_t *pref_matrix, igraph_vector_int_t *block_sizes, igraph_bool_t directed, igraph_bool_t loops) {
23     igraph_t result;
24     IGRAPH_ASSERT(igraph_sbm_game(&result, n, pref_matrix, block_sizes, directed, loops) == IGRAPH_SUCCESS);
25     print_graph_canon(&result);
26     printf("\n");
27     igraph_destroy(&result);
28 }
29 
30 
main()31 int main() {
32     igraph_t result;
33     igraph_matrix_t pref_matrix_0, pref_matrix_1, pref_matrix_3, pref_matrix_3u, pref_matrix_nonsq, pref_matrix_oor, pref_matrix_nsym;
34     igraph_vector_int_t block_sizes_0, block_sizes_1, block_sizes_3, block_sizes_neg;
35 
36     igraph_matrix_init(&pref_matrix_0, 0, 0);
37 
38     igraph_matrix_init(&pref_matrix_1, 1, 1);
39     MATRIX(pref_matrix_1, 0, 0) = 1;
40 
41     igraph_matrix_init(&pref_matrix_3, 3, 3);
42     igraph_matrix_null(&pref_matrix_3);
43     MATRIX(pref_matrix_3, 0, 1) = 1;
44     MATRIX(pref_matrix_3, 2, 2) = 1;
45 
46     igraph_matrix_init(&pref_matrix_3u, 3, 3);
47     igraph_matrix_null(&pref_matrix_3u);
48     MATRIX(pref_matrix_3u, 0, 1) = 1;
49     MATRIX(pref_matrix_3u, 1, 0) = 1;
50     MATRIX(pref_matrix_3u, 2, 2) = 1;
51 
52     igraph_matrix_init(&pref_matrix_nonsq, 3, 2);
53 
54     igraph_matrix_init(&pref_matrix_oor, 3, 3);
55     igraph_matrix_null(&pref_matrix_oor);
56     MATRIX(pref_matrix_oor, 0, 1) = 10;
57 
58     igraph_matrix_init(&pref_matrix_nsym, 3, 3);
59     igraph_matrix_null(&pref_matrix_nsym);
60     MATRIX(pref_matrix_nsym, 0, 1) = 1;
61 
62     igraph_vector_int_init_int(&block_sizes_0, 0);
63     igraph_vector_int_init_int(&block_sizes_1, 1, 1);
64     igraph_vector_int_init_int(&block_sizes_3, 3, 2, 2, 2);
65     igraph_vector_int_init_int(&block_sizes_neg, 3, 2, 2, -2);
66 
67     printf("No vertices.\n");
68     call_and_print(0, &pref_matrix_0, &block_sizes_0, 0, 0);
69 
70     printf("One vertex, directed, with loops.\n");
71     call_and_print(1, &pref_matrix_1, &block_sizes_1, 1, 1);
72 
73     printf("Six vertices, directed, only edges from block 0 to 1 and 2 to 2.\n");
74     call_and_print(6, &pref_matrix_3, &block_sizes_3, 1, 1);
75 
76     printf("Six vertices, directed, only edges from block 0 to 1 and 2 to 2, no loops.\n");
77     call_and_print(6, &pref_matrix_3, &block_sizes_3, 1, 0);
78 
79     printf("Six vertices, undirected, only edges between block 0 and 1, and inside block 2.\n");
80     call_and_print(6, &pref_matrix_3u, &block_sizes_3, 0, 1);
81 
82     printf("Six vertices, undirected, only edges between block 0 and 1, and inside block 2, no loops.\n");
83     call_and_print(6, &pref_matrix_3u, &block_sizes_3, 0, 0);
84 
85     VERIFY_FINALLY_STACK();
86 
87     printf("Check for nonsquare matrix error handling.\n");
88     CHECK_ERROR(igraph_sbm_game(&result, 6, &pref_matrix_nonsq, &block_sizes_3, 0, 0), IGRAPH_NONSQUARE);
89 
90     printf("Check for preference matrix probability out of range error handling.\n");
91     CHECK_ERROR(igraph_sbm_game(&result, 6, &pref_matrix_oor, &block_sizes_3, 0, 0), IGRAPH_EINVAL);
92 
93     printf("Check for nonsymmetric preference matrix for undirected graph error handling.\n");
94     CHECK_ERROR(igraph_sbm_game(&result, 6, &pref_matrix_nsym, &block_sizes_3, 0, 0), IGRAPH_EINVAL);
95 
96     printf("Check for incorrect block size vector error handling.\n");
97     CHECK_ERROR(igraph_sbm_game(&result, 6, &pref_matrix_3, &block_sizes_1, 1, 0), IGRAPH_EINVAL);
98 
99     printf("Check for negative block size error handling.\n");
100     CHECK_ERROR(igraph_sbm_game(&result, 6, &pref_matrix_3, &block_sizes_neg, 1, 0), IGRAPH_EINVAL);
101 
102     printf("Check for sum of block sizes not equal to number of vertices error handling.\n");
103     CHECK_ERROR(igraph_sbm_game(&result, 3, &pref_matrix_3, &block_sizes_3, 1, 0), IGRAPH_EINVAL);
104 
105     igraph_matrix_destroy(&pref_matrix_0);
106     igraph_matrix_destroy(&pref_matrix_1);
107     igraph_matrix_destroy(&pref_matrix_3);
108     igraph_matrix_destroy(&pref_matrix_3u);
109     igraph_matrix_destroy(&pref_matrix_oor);
110     igraph_matrix_destroy(&pref_matrix_nsym);
111     igraph_matrix_destroy(&pref_matrix_nonsq);
112     igraph_vector_int_destroy(&block_sizes_0);
113     igraph_vector_int_destroy(&block_sizes_1);
114     igraph_vector_int_destroy(&block_sizes_3);
115     igraph_vector_int_destroy(&block_sizes_neg);
116 
117     VERIFY_FINALLY_STACK();
118     return 0;
119 }
120