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 
check(igraph_t * graph,igraph_vs_t * vs)22 void check(igraph_t *graph, igraph_vs_t *vs) {
23     igraph_vit_t vit;
24 
25     IGRAPH_ASSERT(igraph_vit_create(graph, *vs, &vit) == IGRAPH_SUCCESS);
26     for (; !IGRAPH_VIT_END(vit); IGRAPH_VIT_NEXT(vit)) {
27         printf("%" IGRAPH_PRId "\n", IGRAPH_VIT_GET(vit));
28     }
29 }
30 
main()31 int main() {
32     igraph_t g, g_no_vertices, g_no_edges;
33     igraph_vs_t vs;
34     igraph_vector_t v;
35     igraph_vit_t vit;
36 
37     igraph_small(&g, 5, IGRAPH_DIRECTED, 0,1, 0,2, 1,1, 1,3, 2,0, 2,3, 3,4, -1);
38     igraph_small(&g_no_vertices, 0, IGRAPH_UNDIRECTED, -1);
39     igraph_small(&g_no_edges, 5, IGRAPH_UNDIRECTED, -1);
40 
41     printf("Checking vs_none vertex selector:\n");
42     IGRAPH_ASSERT(igraph_vs_none(&vs) == IGRAPH_SUCCESS);
43     check(&g, &vs);
44     check(&g_no_edges, &vs);
45     check(&g_no_vertices, &vs);
46 
47     igraph_set_error_handler(igraph_error_handler_ignore);
48 
49     printf("Checking vector selector:\n");
50     igraph_vector_init_int(&v, 3, 2, 3, 4);
51     IGRAPH_ASSERT(igraph_vs_vector(&vs, &v) == IGRAPH_SUCCESS);
52     printf("Some graph:\n");
53     check(&g, &vs);
54     printf("Edgeless graph:\n");
55     check(&g_no_edges, &vs);
56     printf("Graph without vertices should fail.\n");
57     IGRAPH_ASSERT(igraph_vit_create(&g_no_vertices, vs, &vit) == IGRAPH_EINVVID);
58     igraph_vector_destroy(&v);
59 
60     printf("Vertex selector with negative index should fail\n");
61     igraph_vector_init_int(&v, 3, -2, 3, 4);
62     IGRAPH_ASSERT(igraph_vs_vector(&vs, &v) == IGRAPH_SUCCESS);
63     IGRAPH_ASSERT(igraph_vit_create(&g, vs, &vit) == IGRAPH_EINVVID);
64     igraph_vector_destroy(&v);
65 
66     printf("Checking copy vector selector:\n");
67     igraph_vector_init_int(&v, 3, 2, 3, 4);
68     IGRAPH_ASSERT(igraph_vs_vector_copy(&vs, &v) == IGRAPH_SUCCESS);
69     printf("Some graph:\n");
70     check(&g, &vs);
71     printf("Edgeless graph:\n");
72     check(&g_no_edges, &vs);
73     printf("Graph without vertices should fail.\n");
74     IGRAPH_ASSERT(igraph_vit_create(&g_no_vertices, vs, &vit) == IGRAPH_EINVVID);
75     IGRAPH_ASSERT(igraph_vs_type(&vs) == IGRAPH_VS_VECTOR);
76     igraph_vector_destroy(&v);
77 
78     igraph_destroy(&g);
79     igraph_destroy(&g_no_vertices);
80     igraph_destroy(&g_no_edges);
81 
82     VERIFY_FINALLY_STACK();
83     return 0;
84 }
85