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 #include "../../src/graph/neighbors.h"
22 
call_and_print(igraph_t * graph,igraph_integer_t pnode,igraph_neimode_t mode,igraph_loops_t loops,igraph_multiple_t multiple)23 void call_and_print(igraph_t *graph, igraph_integer_t pnode, igraph_neimode_t mode, igraph_loops_t loops, igraph_multiple_t multiple) {
24     igraph_vector_t neis;
25     igraph_vector_init(&neis, 0);
26     IGRAPH_ASSERT(igraph_i_neighbors(graph, &neis, pnode, mode, loops, multiple) == IGRAPH_SUCCESS);
27     print_vector(&neis);
28     igraph_vector_destroy(&neis);
29 }
30 
31 
main()32 int main() {
33     igraph_t g_1, g_lm, g_lmu, g_s1, g_s2;
34 
35     igraph_small(&g_1, 1, 0, -1);
36     igraph_small(&g_lm, 6, 1, 0,1, 0,2, 1,1, 1,3, 2,0, 2,0, 2,3, 3,4, 3,4, -1);
37     igraph_small(&g_lmu, 6, 0, 0,1, 0,2, 1,1, 1,3, 2,0, 2,0, 2,3, 3,4, 3,4, -1);
38     igraph_small(&g_s1, 2, 1, 0,1, 0,1, 1,0, 1,0, -1);
39     igraph_small(&g_s2, 2, 1, 0,1, 1,0, 1,0, -1);
40 
41     igraph_vector_t neis;
42     igraph_vector_init(&neis, 0);
43 
44     printf("One vertex:\n");
45     call_and_print(&g_1, 0, IGRAPH_ALL, IGRAPH_LOOPS_ONCE, IGRAPH_MULTIPLE);
46 
47     printf("Vertex with multiple edges, IGRAPH_IN, IGRAPH_MULTIPLE:\n");
48     call_and_print(&g_lm, 0, IGRAPH_IN, IGRAPH_LOOPS_ONCE, IGRAPH_MULTIPLE);
49 
50     printf("Vertex with multiple edges, IGRAPH_OUT, IGRAPH_MULTIPLE:\n");
51     call_and_print(&g_lm, 0, IGRAPH_OUT, IGRAPH_LOOPS_ONCE, IGRAPH_MULTIPLE);
52 
53     printf("Vertex with multiple edges, IGRAPH_ALL, IGRAPH_MULTIPLE:\n");
54     call_and_print(&g_lm, 0, IGRAPH_ALL, IGRAPH_LOOPS_TWICE, IGRAPH_MULTIPLE);
55 
56     printf("Vertex with multiple edges, undirected, IGRAPH_MULTIPLE:\n");
57     call_and_print(&g_lmu, 0, IGRAPH_IN, IGRAPH_LOOPS_ONCE, IGRAPH_MULTIPLE);
58 
59     printf("Vertex with multiple edges, IGRAPH_IN, IGRAPH_NO_MULTIPLE:\n");
60     call_and_print(&g_lm, 0, IGRAPH_IN, IGRAPH_LOOPS_ONCE, IGRAPH_NO_MULTIPLE);
61 
62     printf("Vertex with multiple edges, IGRAPH_OUT, IGRAPH_NO_MULTIPLE:\n");
63     call_and_print(&g_lm, 0, IGRAPH_OUT, IGRAPH_LOOPS_ONCE, IGRAPH_NO_MULTIPLE);
64 
65     printf("Vertex with multiple edges, IGRAPH_ALL, IGRAPH_NO_MULTIPLE:\n");
66     call_and_print(&g_lm, 0, IGRAPH_ALL, IGRAPH_LOOPS_TWICE, IGRAPH_NO_MULTIPLE);
67 
68     printf("Vertex with multiple edges, undirected, IGRAPH_NO_MULTIPLE:\n");
69     call_and_print(&g_lmu, 0, IGRAPH_IN, IGRAPH_NO_LOOPS, IGRAPH_NO_MULTIPLE);
70 
71     printf("Vertex 1 with loop, IGRAPH_OUT, IGRAPH_NO_LOOPS:\n");
72     call_and_print(&g_lm, 1, IGRAPH_OUT, IGRAPH_NO_LOOPS, IGRAPH_MULTIPLE);
73 
74     printf("Vertex 1 with loop, IGRAPH_ALL, IGRAPH_NO_LOOPS:\n");
75     call_and_print(&g_lm, 1, IGRAPH_ALL, IGRAPH_NO_LOOPS, IGRAPH_MULTIPLE);
76 
77     printf("Vertex 1 with loop, undirected, IGRAPH_NO_LOOPS:\n");
78     call_and_print(&g_lmu, 1, IGRAPH_IN, IGRAPH_NO_LOOPS, IGRAPH_MULTIPLE);
79 
80     printf("Vertex 1 with loop, IGRAPH_OUT, IGRAPH_LOOPS_ONCE:\n");
81     call_and_print(&g_lm, 1, IGRAPH_OUT, IGRAPH_LOOPS_ONCE, IGRAPH_MULTIPLE);
82 
83     printf("Vertex 1 with loop, IGRAPH_ALL, IGRAPH_LOOPS_ONCE:\n");
84     call_and_print(&g_lm, 1, IGRAPH_ALL, IGRAPH_LOOPS_ONCE, IGRAPH_MULTIPLE);
85 
86     printf("Vertex 1 with loop, undirected, IGRAPH_LOOPS_ONCE:\n");
87     call_and_print(&g_lmu, 1, IGRAPH_IN, IGRAPH_LOOPS_ONCE, IGRAPH_MULTIPLE);
88 
89     printf("Vertex 1 with loop, IGRAPH_ALL, IGRAPH_LOOPS_TWICE:\n");
90     call_and_print(&g_lm, 1, IGRAPH_ALL, IGRAPH_LOOPS_TWICE, IGRAPH_MULTIPLE);
91 
92     printf("Vertex 1 with loop, undirected, IGRAPH_LOOPS_TWICE:\n");
93     call_and_print(&g_lmu, 1, IGRAPH_IN, IGRAPH_LOOPS_TWICE, IGRAPH_MULTIPLE);
94 
95     printf("Graph with 2 edges from 0 to 1, and 2 from 1 to 0, IGRAPH_ALL, IGRAPH_MULTIPLE:\n");
96     call_and_print(&g_s1, 0, IGRAPH_ALL, IGRAPH_LOOPS_ONCE, IGRAPH_MULTIPLE);
97 
98     printf("Graph with 1 edge from 0 to 1, and 2 from 1 to 0, IGRAPH_ALL, IGRAPH_MULTIPLE:\n");
99     call_and_print(&g_s2, 0, IGRAPH_ALL, IGRAPH_LOOPS_ONCE, IGRAPH_MULTIPLE);
100 
101     VERIFY_FINALLY_STACK();
102     igraph_set_error_handler(igraph_error_handler_ignore);
103 
104     printf("Trying IGRAPH_LOOPS_TWICE with IGRAPH_OUT:\n");
105     IGRAPH_ASSERT(igraph_i_neighbors(&g_lm, &neis, 0, IGRAPH_OUT, IGRAPH_LOOPS_TWICE, IGRAPH_NO_MULTIPLE) == IGRAPH_EINVAL);
106 
107     printf("Trying invalid vertex ID:\n");
108     IGRAPH_ASSERT(igraph_i_neighbors(&g_lm, &neis, 42, IGRAPH_ALL, IGRAPH_LOOPS_TWICE, IGRAPH_MULTIPLE) == IGRAPH_EINVVID);
109 
110     printf("Trying invalid mode:\n");
111     IGRAPH_ASSERT(igraph_i_neighbors(&g_lm, &neis, 0, (igraph_neimode_t) 42, IGRAPH_LOOPS_TWICE, IGRAPH_MULTIPLE) == IGRAPH_EINVMODE);
112 
113     igraph_destroy(&g_1);
114     igraph_destroy(&g_lm);
115     igraph_destroy(&g_lmu);
116     igraph_destroy(&g_s1);
117     igraph_destroy(&g_s2);
118     igraph_vector_destroy(&neis);
119 
120     VERIFY_FINALLY_STACK();
121     return 0;
122 }
123