1 /* -*- mode: C -*-  */
2 /*
3    IGraph library.
4    Copyright (C) 2006-2012  Gabor Csardi <csardi.gabor@gmail.com>
5    334 Harvard street, 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 #include <stdlib.h>
26 
27 #include "test_utilities.inc"
28 
main()29 int main() {
30 
31     igraph_vector_t v, v2, v3;
32     int i;
33     igraph_real_t *ptr;
34     long int pos;
35     igraph_real_t min, max, min2, max2;
36     long int which_min, which_max, which_min2, which_max2;
37 
38     printf("Initialise empty vector\n");
39     igraph_vector_init(&v, 0);
40     igraph_vector_destroy(&v);
41 
42     printf("Initialise vector of length 10\n");
43     igraph_vector_init(&v, 10);
44     print_vector_format(&v, stdout, "%g");
45     igraph_vector_destroy(&v);
46 
47     printf("Test VECTOR() and igraph_vector_size\n");
48     igraph_vector_init(&v, 10);
49     for (i = 0; i < igraph_vector_size(&v); i++) {
50         VECTOR(v)[i] = 10 - i;
51     }
52     print_vector_format(&v, stdout, "%g");
53     igraph_vector_destroy(&v);
54 
55     printf("Test igraph_vector_reserve and igraph_vector_push_back\n");
56     igraph_vector_init(&v, 0);
57     igraph_vector_reserve(&v, 10);
58     for (i = 0; i < 10; i++) {
59         igraph_vector_push_back(&v, i);
60     }
61 
62     printf("Test igraph_vector_empty and igraph_vector_clear\n");
63     IGRAPH_ASSERT(!igraph_vector_empty(&v));
64     igraph_vector_clear(&v);
65     IGRAPH_ASSERT(igraph_vector_empty(&v));
66     igraph_vector_destroy(&v);
67 
68     printf("Test igraph_vector_e and igraph_vector_e_ptr\n");
69     igraph_vector_init(&v, 5);
70     for (i = 0; i < igraph_vector_size(&v); i++) {
71         *igraph_vector_e_ptr(&v, i) = 100 * i;
72     }
73     for (i = 0; i < igraph_vector_size(&v); i++) {
74         printf(" %li", (long int)igraph_vector_e(&v, i));
75     }
76     printf("\n");
77     igraph_vector_destroy(&v);
78 
79     printf("Test igraph_vector_set\n");
80     igraph_vector_init(&v, 5);
81     for (i = 0; i < igraph_vector_size(&v); i++) {
82         igraph_vector_set(&v, i, 20 * i);
83     }
84     print_vector_format(&v, stdout, "%g");
85     igraph_vector_destroy(&v);
86 
87     printf("Test igraph_vector_null\n");
88     igraph_vector_init(&v, 0);
89     igraph_vector_null(&v);
90     igraph_vector_destroy(&v);
91     igraph_vector_init(&v, 10);
92     for (i = 0; i < igraph_vector_size(&v); i++) {
93         VECTOR(v)[i] = i + 1;
94     }
95     igraph_vector_null(&v);
96     print_vector_format(&v, stdout, "%g");
97     igraph_vector_destroy(&v);
98 
99     printf("Test igraph_vector_tail, igraph_vector_pop_back\n");
100     igraph_vector_init(&v, 10);
101     for (i = 0; i < igraph_vector_size(&v); i++) {
102         VECTOR(v)[i] = i + 1;
103     }
104     while (!igraph_vector_empty(&v)) {
105         printf(" %li", (long int)igraph_vector_tail(&v));
106         printf(" %li", (long int)igraph_vector_pop_back(&v));
107     }
108     printf("\n");
109     igraph_vector_destroy(&v);
110 
111     printf("Test igraph_vector_init_seq, igraph_vector_order\n");
112     igraph_vector_init_seq(&v, 1, 10);
113     igraph_vector_init(&v2, 0);
114     igraph_vector_order1(&v, &v2, 10);
115     print_vector_format(&v2, stdout, "%g");
116     igraph_vector_destroy(&v2);
117     igraph_vector_destroy(&v);
118 
119     printf("Test igraph_vector_resize, igraph_vector_sort\n");
120     igraph_vector_init(&v, 20);
121     for (i = 0; i < 10; i++) {
122         VECTOR(v)[i] = 10 - i;
123     }
124     igraph_vector_resize(&v, 10);
125     igraph_vector_sort(&v);
126     print_vector_format(&v, stdout, "%g");
127     igraph_vector_destroy(&v);
128 
129     printf("Test igraph_vector_{which}_{min, max}\n");
130     igraph_vector_init(&v, 10);
131     for (i = 0; i < igraph_vector_size(&v); i++) {
132         VECTOR(v)[i] = 100 - i;
133     }
134     for (i = 0; i < 10; i++) {
135         printf(" %li", (long int)VECTOR(v)[i]);
136     }
137     printf("\n");
138 
139     min = igraph_vector_min(&v);
140     which_min = igraph_vector_which_min(&v);
141 
142     IGRAPH_ASSERT(min == 91);
143     IGRAPH_ASSERT(which_min == 9);
144     IGRAPH_ASSERT(min == VECTOR(v)[which_min]);
145 
146     max = igraph_vector_max(&v);
147     which_max = igraph_vector_which_max(&v);
148 
149     IGRAPH_ASSERT(max == 100);
150     IGRAPH_ASSERT(which_max == 0);
151     IGRAPH_ASSERT(max == VECTOR(v)[which_max]);
152 
153     igraph_vector_minmax(&v, &min2, &max2);
154     igraph_vector_which_minmax(&v, &which_min2, &which_max2);
155 
156     IGRAPH_ASSERT(min == min2);
157     IGRAPH_ASSERT(max == max2);
158     IGRAPH_ASSERT(which_min == which_min2);
159     IGRAPH_ASSERT(which_max == which_max2);
160     IGRAPH_ASSERT(min2 == VECTOR(v)[which_min2]);
161     IGRAPH_ASSERT(max2 == VECTOR(v)[which_max2]);
162 
163     printf("Test NaN values\n");
164     igraph_vector_push_back(&v, IGRAPH_NAN);
165     igraph_vector_push_back(&v, IGRAPH_NAN);
166     igraph_vector_push_back(&v, 1);
167 
168     IGRAPH_ASSERT(igraph_vector_is_any_nan(&v));
169 
170     min = igraph_vector_min(&v);
171     which_min = igraph_vector_which_min(&v);
172 
173     IGRAPH_ASSERT(igraph_is_nan(min));
174     /* Index should be to first NaN value */
175     IGRAPH_ASSERT(which_min == 10);
176     IGRAPH_ASSERT(igraph_is_nan(VECTOR(v)[which_min]));
177 
178     max = igraph_vector_max(&v);
179     which_max = igraph_vector_which_max(&v);
180 
181     IGRAPH_ASSERT(igraph_is_nan(max));
182     /* Index should be to first NaN value */
183     IGRAPH_ASSERT(which_max == 10);
184     /* In case of NaN it should hold that which_max == which_min */
185     IGRAPH_ASSERT(which_max == which_min);
186 
187     igraph_vector_minmax(&v, &min2, &max2);
188     igraph_vector_which_minmax(&v, &which_min2, &which_max2);
189 
190     IGRAPH_ASSERT(igraph_is_nan(min2));
191     IGRAPH_ASSERT(igraph_is_nan(max2));
192     IGRAPH_ASSERT(which_min == which_min2);
193     IGRAPH_ASSERT(which_max == which_max2);
194     /* In case of NaN it should hold that which_max == which_min */
195     IGRAPH_ASSERT(which_min2 == which_max2);
196     IGRAPH_ASSERT(igraph_is_nan(VECTOR(v)[which_min2]));
197     IGRAPH_ASSERT(igraph_is_nan(VECTOR(v)[which_max2]));
198 
199     printf("Test igraph_vector_init_copy\n");
200     igraph_vector_destroy(&v);
201     ptr = (igraph_real_t*) malloc(10 * sizeof(igraph_real_t));
202     igraph_vector_init_copy(&v, ptr, 10);
203     free(ptr);
204     for (i = 0; i < 10; i++) {
205         VECTOR(v)[i] = 100 - i;
206     }
207     print_vector_format(&v, stdout, "%g");
208     igraph_vector_destroy(&v);
209 
210     printf("Test igraph_vector_copy_to\n");
211     ptr = (igraph_real_t*) malloc(10 * sizeof(igraph_real_t));
212     igraph_vector_init_seq(&v, 11, 20);
213     igraph_vector_copy_to(&v, ptr);
214     for (i = 0; i < 10; i++) {
215         printf(" %li", (long int)ptr[i]);
216     }
217     printf("\n");
218     free(ptr);
219     igraph_vector_destroy(&v);
220 
221     printf("Test igraph_vector_init_seq, igraph_vector_sum, igraph_vector_prod\n");
222     igraph_vector_init_seq(&v, 1, 5);
223     printf(" %li", (long int)igraph_vector_sum(&v));
224     printf(" %li\n", (long int)igraph_vector_prod(&v));
225 
226     printf("Test igraph_vector_remove_section\n");
227     igraph_vector_remove_section(&v, 2, 4);
228     printf(" %li", (long int)igraph_vector_sum(&v));
229     printf(" %li\n", (long int)igraph_vector_prod(&v));
230     igraph_vector_destroy(&v);
231 
232     printf("Test igraph_vector_remove\n");
233     igraph_vector_init_seq(&v, 1, 10);
234     igraph_vector_remove(&v, 9);
235     igraph_vector_remove(&v, 0);
236     igraph_vector_remove(&v, 4);
237     printf(" %li\n", (long int)igraph_vector_sum(&v));
238     igraph_vector_destroy(&v);
239 
240     printf("Test igraph_vector_move_interval\n");
241     igraph_vector_init_seq(&v, 0, 9);
242     igraph_vector_move_interval(&v, 5, 10, 0);
243     IGRAPH_ASSERT(igraph_vector_sum(&v) == 70);
244     igraph_vector_destroy(&v);
245 
246     printf("Test igraph_vector_isininterval\n");
247     igraph_vector_init_seq(&v, 1, 10);
248     IGRAPH_ASSERT(igraph_vector_isininterval(&v, 1, 10));
249     IGRAPH_ASSERT(!igraph_vector_isininterval(&v, 2, 10));
250     IGRAPH_ASSERT(!igraph_vector_isininterval(&v, 1, 9));
251 
252     printf("Test igraph_vector_any_smaller\n");
253     IGRAPH_ASSERT(!igraph_vector_any_smaller(&v, 1));
254     IGRAPH_ASSERT(igraph_vector_any_smaller(&v, 2));
255     igraph_vector_destroy(&v);
256 
257     printf("Test igraph_vector_all_e\n");
258 
259     printf("Test igraph_vector_binsearch\n");
260     igraph_vector_init_seq(&v, 0, 9);
261     for (i = 0; i < igraph_vector_size(&v); i++) {
262         IGRAPH_ASSERT(igraph_vector_binsearch(&v, 0, 0));
263     }
264     IGRAPH_ASSERT(!igraph_vector_binsearch(&v, 10, 0));
265     IGRAPH_ASSERT(!igraph_vector_binsearch(&v, -1, 0));
266 
267     for (i = 0; i < igraph_vector_size(&v); i++) {
268         VECTOR(v)[i] = 2 * i;
269     }
270     for (i = 0; i < igraph_vector_size(&v); i++) {
271         long int pos;
272         IGRAPH_ASSERT(igraph_vector_binsearch(&v, VECTOR(v)[i], &pos));
273         IGRAPH_ASSERT(pos == i);
274         IGRAPH_ASSERT(!igraph_vector_binsearch(&v, VECTOR(v)[i] + 1, &pos));
275     }
276     igraph_vector_destroy(&v);
277 
278     printf("Test Binsearch in empty vector\n");
279     igraph_vector_init(&v, 0);
280     IGRAPH_ASSERT(!igraph_vector_binsearch2(&v, 0));
281     IGRAPH_ASSERT(!igraph_vector_binsearch(&v, 1, &pos));
282     IGRAPH_ASSERT(pos == 0);
283     igraph_vector_destroy(&v);
284 
285     printf("Test igraph_vector_init_real\n");
286     igraph_vector_init_real(&v, 10, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0);
287     print_vector_format(&v, stdout, "%g");
288     igraph_vector_destroy(&v);
289 
290     printf("Test igraph_vector_init_int\n");
291     igraph_vector_init_int(&v, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
292     print_vector_format(&v, stdout, "%g");
293     igraph_vector_destroy(&v);
294 
295     printf("Test igraph_vector_init_real\n");
296     igraph_vector_init_real_end(&v, -1, 1.0, 2.0, 3.0, 4.0, 5.0,
297                                 6.0, 7.0, 8.0, 9.0, 10.0, -1.0);
298     print_vector_format(&v, stdout, "%g");
299     igraph_vector_destroy(&v);
300 
301     printf("Test igraph_vector_init_int\n");
302     igraph_vector_init_int_end(&v, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1);
303     print_vector_format(&v, stdout, "%g");
304     igraph_vector_destroy(&v);
305 
306     printf("Test igraph_vector_permdelete\n");
307     printf("Test igraph_vector_remove_negidx\n");
308 
309     printf("Test order2\n");
310     igraph_vector_init_int_end(&v, -1, 10, 9, 8, 7, 6, 7, 8, 9, 10, -1);
311     igraph_vector_order2(&v);
312     print_vector_format(&v, stdout, "%g");
313     igraph_vector_destroy(&v);
314 
315     printf("Test filter_smaller, quite special....\n");
316     igraph_vector_init_int_end(&v, -1, 0, 1, 2, 3, 4, 4, 4, 4, 5, 6, 7, 8, -1);
317     igraph_vector_filter_smaller(&v, 4);
318     print_vector_format(&v, stdout, "%g");
319     igraph_vector_destroy(&v);
320     igraph_vector_init_int_end(&v, -1, 1, 2, 3, 4, 4, 4, 4, 5, 6, 7, 8, -1);
321     igraph_vector_filter_smaller(&v, 0);
322     print_vector_format(&v, stdout, "%g");
323     igraph_vector_destroy(&v);
324     igraph_vector_init_int_end(&v, -1, 0, 0, 1, 2, 3, 4, 4, 4, 4, 5, 6, 7, 8, -1);
325     igraph_vector_filter_smaller(&v, 0);
326     print_vector_format(&v, stdout, "%g");
327     igraph_vector_destroy(&v);
328 
329     printf("Test rank\n");
330     igraph_vector_init_int_end(&v, -1, 0, 1, 2, 6, 5, 2, 1, 0, -1);
331     igraph_vector_init(&v2, 0);
332     igraph_vector_rank(&v, &v2, 7);
333     print_vector_format(&v, stdout, "%g");
334     print_vector_format(&v2, stdout, "%g");
335     igraph_vector_destroy(&v);
336     igraph_vector_destroy(&v2);
337 
338     printf("Test order\n");
339     igraph_vector_init_int_end(&v,  -1, 1, 1, 2, 2, -1);
340     igraph_vector_init_int_end(&v2, -1, 2, 3, 1, 3, -1);
341     igraph_vector_init(&v3, 0);
342     igraph_vector_order(&v, &v2, &v3, 3);
343     print_vector_format(&v3, stdout, "%g");
344     igraph_vector_destroy(&v);
345     igraph_vector_destroy(&v2);
346     igraph_vector_destroy(&v3);
347 
348     printf("Test fill\n");
349 
350     igraph_vector_init(&v, 100);
351     igraph_vector_fill(&v, 1.234567);
352     for (i = 0; i < igraph_vector_size(&v); i++) {
353         IGRAPH_ASSERT(VECTOR(v)[i] == 1.234567);
354     }
355     igraph_vector_destroy(&v);
356 
357     VERIFY_FINALLY_STACK();
358 
359     return 0;
360 }
361