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 
22 
main()23 int main() {
24     igraph_sparsemat_t spmat;
25     igraph_sparsemat_t spmat_comp;
26     int i, j;
27     int size = 3;
28 
29     printf("0x0 matrix\n");
30     igraph_sparsemat_init(&spmat, 0, 0, 0);
31     igraph_sparsemat_compress(&spmat, &spmat_comp);
32     IGRAPH_ASSERT(igraph_sparsemat_nonzero_storage(&spmat) == 0);
33     IGRAPH_ASSERT(igraph_sparsemat_nonzero_storage(&spmat_comp) == 0);
34     igraph_sparsemat_destroy(&spmat);
35     igraph_sparsemat_destroy(&spmat_comp);
36 
37     printf("3x3 compressed matrix with duplicate values that add up to zero.\n");
38     igraph_sparsemat_init(&spmat, size, size, 7);
39     for (i = 0; i < size; i++) {
40         for (j = 0; j < size; j++) {
41             igraph_sparsemat_entry(&spmat, i, j, 5);
42             igraph_sparsemat_entry(&spmat, i, j, -5);
43 
44             /* This checks if there's two entries for every loop. */
45             IGRAPH_ASSERT(igraph_sparsemat_nonzero_storage(&spmat) == (i * size + j + 1) * 2);
46             igraph_sparsemat_compress(&spmat, &spmat_comp);
47             IGRAPH_ASSERT(igraph_sparsemat_nonzero_storage(&spmat_comp) == (i * size + j + 1) * 2);
48 
49             igraph_sparsemat_destroy(&spmat_comp);
50         }
51     }
52     printf("Adding one entry to work around some broken error handling.\n");
53     igraph_sparsemat_entry(&spmat, 0, 0, 5);
54     igraph_sparsemat_compress(&spmat, &spmat_comp);
55 
56     printf("Removing duplicates should leave us with one entry in each position.\n");
57     igraph_sparsemat_dupl(&spmat_comp);
58     IGRAPH_ASSERT(igraph_sparsemat_nonzero_storage(&spmat_comp) == (size * size));
59 
60     printf("Removing all zeros should leave us with only one entry.\n");
61     igraph_sparsemat_dropzeros(&spmat_comp);
62     IGRAPH_ASSERT(igraph_sparsemat_nonzero_storage(&spmat_comp) == 1);
63 
64     igraph_sparsemat_destroy(&spmat);
65     igraph_sparsemat_destroy(&spmat_comp);
66 
67     VERIFY_FINALLY_STACK();
68     return 0;
69 }
70