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_res_i_j(igraph_vector_t * result,igraph_vector_int_t * i,igraph_vector_int_t * j)22 void print_res_i_j(igraph_vector_t *result, igraph_vector_int_t *i, igraph_vector_int_t *j) {
23 print_vector(result);
24 print_vector_int(i);
25 print_vector_int(j);
26 }
27
destroy_all(igraph_vector_t * result,igraph_vector_int_t * i,igraph_vector_int_t * j,igraph_sparsemat_t * spmat,igraph_sparsemat_t * spmat_comp)28 void destroy_all(igraph_vector_t *result, igraph_vector_int_t *i, igraph_vector_int_t *j, igraph_sparsemat_t *spmat, igraph_sparsemat_t *spmat_comp) {
29 igraph_vector_destroy(result);
30 igraph_vector_int_destroy(i);
31 igraph_vector_int_destroy(j);
32 igraph_sparsemat_destroy(spmat);
33 igraph_sparsemat_destroy(spmat_comp);
34 }
35
init_all(igraph_vector_t * result,igraph_vector_int_t * i,igraph_vector_int_t * j,igraph_sparsemat_t * spmat)36 void init_all(igraph_vector_t *result, igraph_vector_int_t *i, igraph_vector_int_t *j, igraph_sparsemat_t *spmat) {
37 igraph_vector_init(result, 0);
38 igraph_vector_int_init(i, 0);
39 igraph_vector_int_init(j, 0);
40 igraph_sparsemat_init(spmat, 0, 0, 0);
41 }
42
main()43 int main() {
44 igraph_sparsemat_t spmat;
45 igraph_sparsemat_t spmat_comp;
46 igraph_vector_t result;
47 igraph_vector_int_t i, j;
48 int k, l;
49 int size = 3;
50
51 printf("0x0 matrix\n");
52 init_all(&result, &i, &j, &spmat);
53 igraph_sparsemat_compress(&spmat, &spmat_comp);
54 IGRAPH_ASSERT(igraph_sparsemat_getelements_sorted(&spmat, &i, &j, &result) == IGRAPH_SUCCESS);
55 printf("triplet:\n");
56 print_res_i_j(&result, &i, &j);
57 IGRAPH_ASSERT(igraph_sparsemat_getelements_sorted(&spmat_comp, &i, &j, &result) == IGRAPH_SUCCESS);
58 printf("compressed:\n");
59 print_res_i_j(&result, &i, &j);
60 destroy_all(&result, &i, &j, &spmat, &spmat_comp);
61
62 printf("\n3x3 matrix\n");
63 init_all(&result, &i, &j, &spmat);
64 for (k = 0; k < size; k += 2) {
65 for (l = 0; l < size; l ++) {
66 igraph_sparsemat_entry(&spmat, k, l, 100);
67 igraph_sparsemat_entry(&spmat, k, l, k * size + l);
68 }
69 }
70 igraph_sparsemat_compress(&spmat, &spmat_comp);
71 IGRAPH_ASSERT(igraph_sparsemat_getelements_sorted(&spmat, &i, &j, &result) == IGRAPH_SUCCESS);
72 printf("triplet:\n");
73 print_res_i_j(&result, &i, &j);
74 IGRAPH_ASSERT(igraph_sparsemat_getelements_sorted(&spmat_comp, &i, &j, &result) == IGRAPH_SUCCESS);
75 printf("compressed:\n");
76 print_res_i_j(&result, &i, &j);
77
78 destroy_all(&result, &i, &j, &spmat, &spmat_comp);
79
80 VERIFY_FINALLY_STACK();
81 return 0;
82 }
83