1 /* -*- mode: C -*-  */
2 /*
3    IGraph library.
4    Copyright (C) 2006-2012  Gabor Csardi <csardi.gabor@gmail.com>
5    334 Harvard st, Cambridge MA, 02139 USA
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301 USA
21 
22 */
23 
24 #include <igraph.h>
25 
26 #include "test_utilities.inc"
27 
28 typedef struct {
29     int dim;
30     int m;
31     int nei;
32     igraph_bool_t directed, mutual, circular;
33     igraph_real_t *dimedges;
34 } lat_test_t;
35 
36 #define LAT_TEST(id, d, m, ne, di, mu, ci, ...) \
37     igraph_real_t lat_ ## id ## _edges[] = { __VA_ARGS__ } ; \
38     lat_test_t lat_ ## id = { d, m, ne, di, mu, ci, lat_ ## id ## _edges }
39 
40 /*----------------d--m--ne-di-mu-ci-dimedges------------------------*/
41 LAT_TEST(u_0,     0, 0, 1, 0, 0, 0,  -1 );
42 LAT_TEST(u_01,    1, 0, 1, 0, 0, 0,  0 );
43 LAT_TEST(u_02,    2, 0, 1, 0, 0, 0,  0, 1 );
44 LAT_TEST(u_03,    2, 0, 1, 0, 0, 0,  1, 0 );
45 
46 LAT_TEST(d_0,     0, 0, 1, 1, 0, 0,  -1 );
47 LAT_TEST(d_01,    1, 0, 1, 1, 0, 0,  0 );
48 LAT_TEST(d_02,    2, 0, 1, 1, 0, 0,  0, 1 );
49 LAT_TEST(d_03,    2, 0, 1, 1, 0, 0,  1, 0 );
50 
51 LAT_TEST(u_1,     1, 0, 1, 0, 0, 0,  1 );
52 LAT_TEST(u_1x1,   2, 0, 1, 0, 0, 0,  1, 1 );
53 LAT_TEST(u_2,     1, 1, 1, 0, 0, 0,  2, 0, 1 );
54 LAT_TEST(u_2x1,   2, 1, 1, 0, 0, 0,  2, 1, 0, 1 );
55 LAT_TEST(u_2x2,   2, 4, 1, 0, 0, 0,  2, 2, 0, 1, 0, 2, 1, 3, 2, 3 );
56 
57 LAT_TEST(uc_1,    1, 0, 1, 0, 0, 1,  1 );
58 LAT_TEST(uc_1x1,  2, 0, 1, 0, 0, 1,  1, 1 );
59 LAT_TEST(uc_2,    1, 1, 1, 0, 0, 1,  2, 0, 1 );
60 LAT_TEST(uc_2x1,  2, 1, 1, 0, 0, 1,  2, 1, 0, 1 );
61 LAT_TEST(uc_2x2,  2, 4, 1, 0, 0, 1,  2, 2, 0, 1, 0, 2, 1, 3, 2, 3 );
62 
63 LAT_TEST(dc_1,    1, 0, 1, 1, 0, 1,  1 );
64 LAT_TEST(dc_1x1,  2, 0, 1, 1, 0, 1,  1, 1 );
65 LAT_TEST(dc_2,    1, 2, 1, 1, 0, 1,  2, 0, 1, 1, 0 );
66 LAT_TEST(dc_2x1,  2, 2, 1, 1, 0, 1,  2, 1, 0, 1, 1, 0 );
67 LAT_TEST(dc_2x2,  2, 8, 1, 1, 0, 1,  2, 2, 0, 1, 0, 2, 1, 3, 2, 3,
68          1, 0, 2, 0, 3, 1, 3, 2, );
69 
70 LAT_TEST(d_1,     1, 0, 1, 1, 0, 0,  1 );
71 LAT_TEST(d_1x1,   2, 0, 1, 1, 0, 0,  1, 1 );
72 LAT_TEST(d_2,     1, 1, 1, 1, 0, 0,  2, 0, 1 );
73 LAT_TEST(d_2x1,   2, 1, 1, 1, 0, 0,  2, 1, 0, 1 );
74 LAT_TEST(d_2x2,   2, 4, 1, 1, 0, 0,  2, 2, 0, 1, 0, 2, 1, 3, 2, 3 );
75 
76 LAT_TEST(dmc_1,   1, 0, 1, 1, 0, 1,  1 );
77 LAT_TEST(dmc_1x1, 2, 0, 1, 1, 0, 1,  1, 1 );
78 LAT_TEST(dmc_2,   1, 2, 1, 1, 0, 1,  2, 0, 1, 1, 0 );
79 LAT_TEST(dmc_2x1, 2, 2, 1, 1, 0, 1,  2, 1, 0, 1, 1, 0 );
80 LAT_TEST(dmc_2x2, 2, 4, 1, 1, 0, 1,  2, 2, 0, 1, 0, 2, 1, 3, 2, 3,
81          1, 0, 3, 2, );
82 /*----------------d--m--ne-di-mu-ci-dimedges------------------------*/
83 
84 /* TODO: add more */
85 
86 lat_test_t *all_checks[] = { /*  1 */ &lat_u_0,   /*  2 */ &lat_u_01,
87                                       /*  3 */ &lat_u_02,  /*  4 */ &lat_u_03,
88                                       /*  5 */ &lat_d_0,   /*  6 */ &lat_d_01,
89                                       /*  7 */ &lat_d_02,  /*  8 */ &lat_d_03,
90                                       /*  9 */ &lat_u_1,   /* 10 */ &lat_u_1x1,
91                                       /* 11 */ &lat_u_2,   /* 12 */ &lat_u_2x1,
92                                       /* 13 */ &lat_u_2x2, /* 14 */ &lat_u_1,
93                                       /* 15 */ &lat_u_1x1, /* 16 */ &lat_u_2,
94                                       /* 17 */ &lat_u_2x1, /* 18 */ &lat_uc_2x2,
95                                       /* 19 */ &lat_dc_1,  /* 20 */ &lat_dc_1x1,
96                                       /* 21 */ &lat_dc_2,  /* 22 */ &lat_dc_2x1,
97                                       /* 23 */ &lat_dc_2x2,/* 24 */ &lat_d_1,
98                                       /* 25 */ &lat_d_1x1, /* 26 */ &lat_d_2,
99                                       /* 27 */ &lat_d_2x1, /* 28 */ &lat_d_2x2,
100                                       /* 29 */ &lat_dc_2x2,/* 30 */ &lat_d_1,
101                                       /* 31 */ &lat_d_1x1, /* 32 */ &lat_d_2,
102                                       /* 33 */ &lat_d_2x1, /* 34 */ &lat_d_2x2,
103                                       0
104                            };
105 
check_lattice_properties(const igraph_t * lattice,const igraph_vector_t * dim,igraph_bool_t directed,igraph_bool_t mutual,igraph_bool_t circular)106 int check_lattice_properties(const igraph_t *lattice,
107                              const igraph_vector_t *dim,
108                              igraph_bool_t directed,
109                              igraph_bool_t mutual,
110                              igraph_bool_t circular) {
111     igraph_bool_t res;
112 
113     /* Connected */
114     igraph_is_connected(lattice, &res, IGRAPH_WEAK);
115     if (!res && igraph_vcount(lattice) > 0) {
116         printf("Not connected\n");
117         return 1;
118     }
119 
120     /* Simple */
121     igraph_is_simple(lattice, &res);
122     if (!res) {
123         printf("Not simple\n");
124         return 2;
125     }
126 
127     return 0;
128 }
129 
check_lattice(const lat_test_t * test)130 int check_lattice(const lat_test_t *test) {
131     igraph_t graph, othergraph;
132     igraph_vector_t otheredges;
133     igraph_vector_t dimvector;
134     igraph_bool_t iso;
135     int ret;
136 
137     /* Create lattice */
138     igraph_vector_view(&dimvector, test->dimedges, test->dim);
139     igraph_lattice(&graph, &dimvector, test->nei, test->directed,
140                    test->mutual, test->circular);
141 
142     /* Check its properties */
143     if ((ret = check_lattice_properties(&graph, &dimvector, test->directed,
144                                         test->mutual, test->circular))) {
145         igraph_destroy(&graph);
146         printf("Lattice properties are not satisfied\n");
147         return ret;
148     }
149 
150     /* Check that it is isomorphic to the stored graph */
151     igraph_vector_view(&otheredges, test->dimedges + test->dim, test->m * 2);
152     igraph_create(&othergraph, &otheredges, igraph_vector_prod(&dimvector),
153                   test->directed);
154     igraph_isomorphic(&graph, &othergraph, &iso);
155     if (!iso) {
156         printf("--\n");
157         igraph_write_graph_edgelist(&graph, stdout);
158         printf("--\n");
159         igraph_write_graph_edgelist(&othergraph, stdout);
160         igraph_destroy(&graph);
161         igraph_destroy(&othergraph);
162         return 50;
163     }
164 
165     igraph_destroy(&graph);
166     igraph_destroy(&othergraph);
167     return 0;
168 }
169 
main()170 int main() {
171     int i, ret;
172 
173     i = 0;
174     while (all_checks[i]) {
175         if ((ret = check_lattice(all_checks[i]))) {
176             printf("Check no #%d failed.\n", (int) (i + 1));
177             return ret;
178         }
179         i++;
180     }
181 
182     VERIFY_FINALLY_STACK();
183 
184     return 0;
185 }
186