/src/igraph/fuzzing/misc_algos.cpp
Line | Count | Source |
1 | | /* |
2 | | igraph library. |
3 | | Copyright (C) 2024 The igraph development team |
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, write to the Free Software |
17 | | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 | | 02110-1301 USA |
19 | | */ |
20 | | |
21 | | #include <igraph.h> |
22 | | #include <cstdlib> |
23 | | |
24 | 2.06k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
25 | 2.06k | igraph_t graph; |
26 | 2.06k | igraph_vector_int_t edges; |
27 | | |
28 | 2.06k | igraph_set_warning_handler(igraph_warning_handler_ignore); |
29 | | |
30 | 2.06k | if (Size % 2 == 0 || Size > 512+1 || Size < 1) { |
31 | 14 | return 0; |
32 | 14 | } |
33 | | |
34 | 2.04k | igraph_vector_int_init(&edges, Size-1); |
35 | 142k | for (size_t i=0; i < Size-1; ++i) { |
36 | 140k | VECTOR(edges)[i] = Data[i+1]; |
37 | 140k | } |
38 | | |
39 | 2.04k | igraph_rng_seed(igraph_rng_default(), 137); |
40 | | |
41 | | /* Directed */ |
42 | 2.04k | if (igraph_create(&graph, &edges, Data[0], IGRAPH_DIRECTED) == IGRAPH_SUCCESS) { |
43 | 2.04k | igraph_vector_int_list_t ivl1; |
44 | 2.04k | igraph_vector_t v1, v2; |
45 | 2.04k | igraph_vector_int_t iv1, iv2; |
46 | 2.04k | igraph_real_t r1; |
47 | 2.04k | igraph_bool_t b1; |
48 | 2.04k | igraph_int_t i1, i2; |
49 | 2.04k | igraph_t g; |
50 | | |
51 | 2.04k | igraph_vector_int_list_init(&ivl1, 0); |
52 | 2.04k | igraph_vector_init(&v1, 0); |
53 | 2.04k | igraph_vector_init(&v2, 0); |
54 | 2.04k | igraph_vector_int_init(&iv1, 0); |
55 | 2.04k | igraph_vector_int_init(&iv2, 0); |
56 | | |
57 | 2.04k | igraph_minimum_cycle_basis(&graph, NULL, &ivl1, -1, true, true); |
58 | | |
59 | | /* Count triangles in the minimum cycle basis. */ |
60 | 2.04k | i1 = igraph_vector_int_list_size(&ivl1); |
61 | 2.04k | i2 = 0; |
62 | 35.5k | for (igraph_int_t i=0; i < i1; i++) { |
63 | 33.5k | if (igraph_vector_int_size(igraph_vector_int_list_get_ptr(&ivl1, i)) == 3) { |
64 | 3.85k | i2++; |
65 | 3.85k | } |
66 | 33.5k | } |
67 | | |
68 | 2.04k | igraph_fundamental_cycles(&graph, NULL, &ivl1, -1, -1); |
69 | | |
70 | 2.04k | igraph_motifs_randesu(&graph, &v1, 3, NULL); |
71 | | |
72 | 2.04k | igraph_list_triangles(&graph, &iv1); |
73 | | |
74 | 2.04k | igraph_count_triangles(&graph, &r1); |
75 | | |
76 | | /* Cross-check direct triangle count to list of triangles. */ |
77 | 2.04k | IGRAPH_ASSERT(igraph_vector_int_size(&iv1) == 3*r1); |
78 | | |
79 | | /* Cross-check direct triangle count with 3-motifs that include a triangle. */ |
80 | 2.04k | IGRAPH_ASSERT( |
81 | 2.04k | VECTOR(v1)[7] + VECTOR(v1)[8] + |
82 | 2.04k | VECTOR(v1)[11] + VECTOR(v1)[12] + |
83 | 2.04k | VECTOR(v1)[13] + VECTOR(v1)[14] + VECTOR(v1)[15] == r1); |
84 | | |
85 | | /* The number of triangles in the minimum cycle basis is no greater than the total. |
86 | | * Example when not all triangles are part of the basis: K_4 graph. |
87 | | */ |
88 | 2.04k | IGRAPH_ASSERT(i2 <= r1); |
89 | | |
90 | 2.04k | igraph_is_triangle_free(&graph, &b1); |
91 | 2.04k | IGRAPH_ASSERT((!b1) == (!!r1)); |
92 | | |
93 | 2.04k | igraph_ecc(&graph, &v1, igraph_ess_all(IGRAPH_EDGEORDER_ID), 3, false, true); |
94 | | |
95 | 2.04k | igraph_count_reachable(&graph, &iv1, IGRAPH_OUT); |
96 | 2.04k | igraph_transitive_closure(&graph, &g); |
97 | 2.04k | igraph_destroy(&g); |
98 | | |
99 | 2.04k | if (igraph_vcount(&graph) >= 2) { |
100 | 2.02k | igraph_get_all_simple_paths(&graph, &ivl1, 0, igraph_vss_1(1), IGRAPH_ALL, -1, 5, IGRAPH_UNLIMITED); |
101 | 2.02k | } |
102 | | |
103 | 2.04k | if (igraph_vcount(&graph) >= 1) { |
104 | 2.04k | igraph_t subg; |
105 | | |
106 | 2.04k | igraph_random_walk(&graph, NULL, &iv1, &iv2, 0, IGRAPH_ALL, igraph_ecount(&graph), IGRAPH_RANDOM_WALK_STUCK_RETURN); |
107 | | |
108 | 2.04k | igraph_induced_subgraph(&graph, &subg, igraph_vss_vector(&iv1), IGRAPH_SUBGRAPH_AUTO); |
109 | 2.04k | igraph_destroy(&subg); |
110 | | |
111 | 2.04k | igraph_subgraph_from_edges(&graph, &subg, igraph_ess_vector(&iv2), true); |
112 | 2.04k | igraph_destroy(&subg); |
113 | | |
114 | 2.04k | igraph_reverse_edges(&graph, igraph_ess_vector(&iv2)); |
115 | 2.04k | } |
116 | | |
117 | 2.04k | i1 = igraph_vcount(&graph); |
118 | 2.04k | i2 = igraph_ecount(&graph); |
119 | 2.04k | igraph_to_undirected(&graph, IGRAPH_TO_UNDIRECTED_COLLAPSE, NULL); |
120 | 2.04k | IGRAPH_ASSERT(igraph_vcount(&graph) == i1); |
121 | 2.04k | IGRAPH_ASSERT(igraph_ecount(&graph) <= i2); |
122 | 2.04k | IGRAPH_ASSERT(! igraph_is_directed(&graph)); |
123 | | |
124 | 2.04k | igraph_vector_resize(&v2, 4); |
125 | 2.04k | igraph_vector_null(&v2); |
126 | 2.04k | igraph_motifs_randesu(&graph, &v1, 4, &v2); |
127 | | |
128 | 2.04k | if (igraph_vcount(&graph) >= 1) { |
129 | 2.04k | igraph_random_walk(&graph, NULL, &iv1, &iv2, 0, IGRAPH_ALL, igraph_ecount(&graph), IGRAPH_RANDOM_WALK_STUCK_RETURN); |
130 | 2.04k | } |
131 | | |
132 | 2.04k | igraph_vector_int_destroy(&iv2); |
133 | 2.04k | igraph_vector_int_destroy(&iv1); |
134 | 2.04k | igraph_vector_destroy(&v2); |
135 | 2.04k | igraph_vector_destroy(&v1); |
136 | 2.04k | igraph_vector_int_list_destroy(&ivl1); |
137 | | |
138 | 2.04k | igraph_destroy(&graph); |
139 | 2.04k | } |
140 | | |
141 | 2.04k | igraph_vector_int_destroy(&edges); |
142 | | |
143 | 2.04k | IGRAPH_ASSERT(IGRAPH_FINALLY_STACK_EMPTY); |
144 | | |
145 | 2.04k | return 0; // Non-zero return values are reserved for future use. |
146 | 2.04k | } |