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 
print_and_destroy(igraph_vector_ptr_t * result)22 void print_and_destroy(igraph_vector_ptr_t *result) {
23     int i;
24     igraph_vector_t *v;
25     for (i = 0; i < igraph_vector_ptr_size(result); i++) {
26         v = VECTOR(*result)[i];
27         print_vector(v);
28         igraph_vector_destroy(v);
29         igraph_free(v);
30     }
31 }
32 
main()33 int main() {
34     igraph_t g_empty, g_lm;
35     igraph_vector_ptr_t result;
36     igraph_vs_t vids;
37 
38     igraph_vector_ptr_init(&result, 0);
39     igraph_vs_all(&vids);
40 
41     igraph_small(&g_empty, 0, 0, -1);
42     igraph_small(&g_lm, 6, 1, 0,1, 0,2, 1,1, 1,3, 2,0, 2,3, 3,4, 3,4, -1);
43 
44     printf("No vertices:\n");
45     IGRAPH_ASSERT(igraph_neighborhood(&g_empty, &result, vids, /*order*/ 1,
46                   /*mode*/ IGRAPH_ALL, /*mindist*/ 0) == IGRAPH_SUCCESS);
47     print_and_destroy(&result);
48 
49     printf("Directed graph with loops and multi-edges, order 0:\n");
50     IGRAPH_ASSERT(igraph_neighborhood(&g_lm, &result, vids, /*order*/ 0,
51                   /*mode*/ IGRAPH_ALL, /*mindist*/ 0) == IGRAPH_SUCCESS);
52     print_and_destroy(&result);
53 
54     printf("Directed graph with loops and multi-edges, order 1, ignoring direction:\n");
55     IGRAPH_ASSERT(igraph_neighborhood(&g_lm, &result, vids, /*order*/ 1,
56                   /*mode*/ IGRAPH_ALL, /*mindist*/ 0) == IGRAPH_SUCCESS);
57     print_and_destroy(&result);
58 
59     printf("Directed graph with loops and multi-edges, order 1, only checking IGRAPH_IN:\n");
60     IGRAPH_ASSERT(igraph_neighborhood(&g_lm, &result, vids, /*order*/ 1,
61                   /*mode*/ IGRAPH_IN, /*mindist*/ 0) == IGRAPH_SUCCESS);
62     print_and_destroy(&result);
63 
64     printf("Directed graph with loops and multi-edges, order 10, ignoring direction:\n");
65     IGRAPH_ASSERT(igraph_neighborhood(&g_lm, &result, vids, /*order*/ 10,
66                   /*mode*/ IGRAPH_ALL, /*mindist*/ 0) == IGRAPH_SUCCESS);
67     print_and_destroy(&result);
68 
69     printf("Directed graph with loops and multi-edges, order 2, mindist 2, IGRAPH_OUT:\n");
70     IGRAPH_ASSERT(igraph_neighborhood(&g_lm, &result, vids, /*order*/ 2,
71                   /*mode*/ IGRAPH_OUT, /*mindist*/ 2) == IGRAPH_SUCCESS);
72     print_and_destroy(&result);
73 
74     printf("Directed graph with loops and multi-edges, order 4, mindist 4, IGRAPH_ALL:\n");
75     IGRAPH_ASSERT(igraph_neighborhood(&g_lm, &result, vids, /*order*/ 4,
76                   /*mode*/ IGRAPH_ALL, /*mindist*/ 4) == IGRAPH_SUCCESS);
77     print_and_destroy(&result);
78 
79     VERIFY_FINALLY_STACK();
80     igraph_set_error_handler(igraph_error_handler_ignore);
81 
82     printf("Negative order.\n");
83     IGRAPH_ASSERT(igraph_neighborhood(&g_lm, &result, vids, /*order*/ -4,
84                   /*mode*/ IGRAPH_ALL, /*mindist*/ 4) == IGRAPH_EINVAL);
85 
86     printf("Negative mindist.\n");
87     IGRAPH_ASSERT(igraph_neighborhood(&g_lm, &result, vids, /*order*/ 4,
88                   /*mode*/ IGRAPH_ALL, /*mindist*/ -4) == IGRAPH_EINVAL);
89 
90     igraph_vector_ptr_destroy(&result);
91     igraph_destroy(&g_empty);
92     igraph_destroy(&g_lm);
93 
94     VERIFY_FINALLY_STACK();
95     return 0;
96 }
97