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
29 igraph_vector_ptr_t custom_destructor_stack;
30
custom_destructor(void * ptr)31 void custom_destructor(void* ptr) {
32 igraph_vector_ptr_push_back(&custom_destructor_stack, ptr);
33 }
34
main()35 int main() {
36
37 igraph_vector_ptr_t v1, v2;
38 igraph_vector_ptr_t v3 = IGRAPH_VECTOR_PTR_NULL;
39 int i;
40 void ** ptr;
41 int d1 = 1, d2 = 2, d3 = 3, d4 = 4, d5 = 5;
42 char *block1 = 0, *block2 = 0;
43
44 /* igraph_vector_ptr_init, igraph_vector_ptr_destroy */
45 igraph_vector_ptr_init(&v1, 10);
46 igraph_vector_ptr_destroy(&v1);
47 igraph_vector_ptr_init(&v1, 0);
48 igraph_vector_ptr_destroy(&v1);
49
50 /* igraph_vector_ptr_free_all, igraph_vector_ptr_destroy_all */
51 igraph_vector_ptr_init(&v1, 5);
52 for (i = 0; i < igraph_vector_ptr_size(&v1); i++) {
53 VECTOR(v1)[i] = (void*)malloc(i * 10);
54 }
55 igraph_vector_ptr_free_all(&v1);
56 for (i = 0; i < igraph_vector_ptr_size(&v1); i++) {
57 VECTOR(v1)[i] = (void*)malloc(i * 10);
58 }
59 igraph_vector_ptr_destroy_all(&v1);
60
61 /* igraph_vector_ptr_reserve */
62 igraph_vector_ptr_init(&v1, 0);
63 igraph_vector_ptr_reserve(&v1, 5);
64 igraph_vector_ptr_reserve(&v1, 15);
65 igraph_vector_ptr_reserve(&v1, 1);
66 igraph_vector_ptr_reserve(&v1, 0);
67 igraph_vector_ptr_destroy(&v1);
68
69 /* igraph_vector_ptr_empty, igraph_vector_ptr_clear */
70 igraph_vector_ptr_init(&v1, 10);
71 if (igraph_vector_ptr_empty(&v1)) {
72 return 1;
73 }
74 igraph_vector_ptr_clear(&v1);
75 if (!igraph_vector_ptr_empty(&v1)) {
76 return 2;
77 }
78
79 /* igraph_vector_ptr_size */
80 if (igraph_vector_ptr_size(&v1) != 0) {
81 return 3;
82 }
83 igraph_vector_ptr_resize(&v1, 10);
84 if (igraph_vector_ptr_size(&v1) != 10) {
85 return 4;
86 }
87 igraph_vector_ptr_destroy(&v1);
88
89 /* igraph_vector_ptr_push_back */
90 igraph_vector_ptr_init(&v1, 0);
91 for (i = 0; i < 10; i++) {
92 igraph_vector_ptr_push_back(&v1, (void*)malloc(i * 10));
93 }
94 igraph_vector_ptr_destroy_all(&v1);
95
96 /* igraph_vector_ptr_e */
97 igraph_vector_ptr_init(&v1, 5);
98 VECTOR(v1)[0] = &d1;
99 VECTOR(v1)[1] = &d2;
100 VECTOR(v1)[2] = &d3;
101 VECTOR(v1)[3] = &d4;
102 VECTOR(v1)[4] = &d5;
103 if (igraph_vector_ptr_e(&v1, 0) != &d1) {
104 return 5;
105 }
106 if (igraph_vector_ptr_e(&v1, 1) != &d2) {
107 return 6;
108 }
109 if (igraph_vector_ptr_e(&v1, 2) != &d3) {
110 return 7;
111 }
112 if (igraph_vector_ptr_e(&v1, 3) != &d4) {
113 return 8;
114 }
115 if (igraph_vector_ptr_e(&v1, 4) != &d5) {
116 return 9;
117 }
118 igraph_vector_ptr_destroy(&v1);
119
120 /* igraph_vector_ptr_set */
121 igraph_vector_ptr_init(&v1, 5);
122 igraph_vector_ptr_set(&v1, 0, &d1);
123 igraph_vector_ptr_set(&v1, 1, &d2);
124 igraph_vector_ptr_set(&v1, 2, &d3);
125 igraph_vector_ptr_set(&v1, 3, &d4);
126 igraph_vector_ptr_set(&v1, 4, &d5);
127 if (igraph_vector_ptr_e(&v1, 0) != &d1) {
128 return 5;
129 }
130 if (igraph_vector_ptr_e(&v1, 1) != &d2) {
131 return 6;
132 }
133 if (igraph_vector_ptr_e(&v1, 2) != &d3) {
134 return 7;
135 }
136 if (igraph_vector_ptr_e(&v1, 3) != &d4) {
137 return 8;
138 }
139 if (igraph_vector_ptr_e(&v1, 4) != &d5) {
140 return 9;
141 }
142 igraph_vector_ptr_destroy(&v1);
143
144 /* igraph_vector_ptr_null */
145 igraph_vector_ptr_init(&v1, 5);
146 igraph_vector_ptr_set(&v1, 0, &d1);
147 igraph_vector_ptr_set(&v1, 1, &d2);
148 igraph_vector_ptr_set(&v1, 2, &d3);
149 igraph_vector_ptr_set(&v1, 3, &d4);
150 igraph_vector_ptr_set(&v1, 4, &d5);
151 igraph_vector_ptr_null(&v1);
152 for (i = 0; i < igraph_vector_ptr_size(&v1); i++) {
153 if (VECTOR(v1)[i] != 0) {
154 return 10;
155 }
156 }
157 igraph_vector_ptr_destroy(&v1);
158
159 /* igraph_vector_ptr_resize */
160 igraph_vector_ptr_init(&v1, 10);
161 igraph_vector_ptr_set(&v1, 0, &d1);
162 igraph_vector_ptr_set(&v1, 1, &d2);
163 igraph_vector_ptr_set(&v1, 2, &d3);
164 igraph_vector_ptr_set(&v1, 3, &d4);
165 igraph_vector_ptr_set(&v1, 4, &d5);
166 igraph_vector_ptr_resize(&v1, 10);
167 igraph_vector_ptr_resize(&v1, 15);
168 igraph_vector_ptr_resize(&v1, 5);
169 if (igraph_vector_ptr_size(&v1) != 5) {
170 return 11;
171 }
172 if (igraph_vector_ptr_e(&v1, 0) != &d1) {
173 return 12;
174 }
175 if (igraph_vector_ptr_e(&v1, 1) != &d2) {
176 return 13;
177 }
178 if (igraph_vector_ptr_e(&v1, 2) != &d3) {
179 return 14;
180 }
181 if (igraph_vector_ptr_e(&v1, 3) != &d4) {
182 return 15;
183 }
184 if (igraph_vector_ptr_e(&v1, 4) != &d5) {
185 return 16;
186 }
187 igraph_vector_ptr_destroy(&v1);
188
189 /* igraph_vector_ptr_view */
190 ptr = (void**) malloc(5 * sizeof(void*));
191 igraph_vector_ptr_view(&v3, ptr, 5);
192 ptr[0] = &d1;
193 ptr[1] = &d2;
194 ptr[2] = &d3;
195 ptr[3] = &d4;
196 ptr[4] = &d5;
197 for (i = 0; i < igraph_vector_ptr_size(&v3); i++) {
198 if ( *((int*)VECTOR(v3)[i]) != i + 1) {
199 return 17;
200 }
201 }
202
203 /* igraph_vector_ptr_init_copy */
204 igraph_vector_ptr_init_copy(&v1, ptr, 5);
205 for (i = 0; i < igraph_vector_ptr_size(&v1); i++) {
206 if ( *((int*)VECTOR(v1)[i]) != i + 1) {
207 return 18;
208 }
209 }
210
211 /* igraph_vector_ptr_copy_to */
212 igraph_vector_ptr_copy_to(&v1, ptr);
213 for (i = 0; i < igraph_vector_ptr_size(&v1); i++) {
214 if ( *((int*)ptr[i]) != i + 1) {
215 return 19;
216 }
217 }
218 free(ptr);
219 igraph_vector_ptr_destroy(&v1);
220
221 /* igraph_vector_ptr_copy */
222 igraph_vector_ptr_init(&v1, 5);
223 igraph_vector_ptr_set(&v1, 0, &d1);
224 igraph_vector_ptr_set(&v1, 1, &d2);
225 igraph_vector_ptr_set(&v1, 2, &d3);
226 igraph_vector_ptr_set(&v1, 3, &d4);
227 igraph_vector_ptr_set(&v1, 4, &d5);
228 igraph_vector_ptr_copy(&v2, &v1);
229 igraph_vector_ptr_destroy(&v1);
230 for (i = 0; i < igraph_vector_ptr_size(&v2); i++) {
231 if ( *((int*)VECTOR(v2)[i]) != i + 1) {
232 return 20;
233 }
234 }
235
236 /* igraph_vector_ptr_remove */
237 igraph_vector_ptr_remove(&v2, 0);
238 igraph_vector_ptr_remove(&v2, 3);
239 if ( *((int*)VECTOR(v2)[0]) != 2) {
240 return 21;
241 }
242 if ( *((int*)VECTOR(v2)[1]) != 3) {
243 return 22;
244 }
245 if ( *((int*)VECTOR(v2)[2]) != 4) {
246 return 23;
247 }
248
249 igraph_vector_ptr_destroy(&v2);
250
251 /* Testing destructor */
252 igraph_vector_ptr_init(&custom_destructor_stack, 0);
253 igraph_vector_ptr_init(&v1, 2);
254 block1 = IGRAPH_CALLOC(32, char);
255 block2 = IGRAPH_CALLOC(64, char);
256 VECTOR(v1)[0] = block1;
257 VECTOR(v1)[1] = block2;
258 if (igraph_vector_ptr_get_item_destructor(&v1) != 0) {
259 return 24;
260 }
261 if (igraph_vector_ptr_set_item_destructor(&v1, &custom_destructor) != 0) {
262 return 25;
263 }
264 /* Okay, let's clear the vector. This should push the blocks in the
265 * custom destructor stack */
266 igraph_vector_ptr_clear(&v1);
267 /* Put the blocks back and destroy the vector */
268 igraph_vector_ptr_push_back(&v1, block1);
269 igraph_vector_ptr_push_back(&v1, block2);
270 igraph_vector_ptr_destroy_all(&v1);
271
272 if (VECTOR(custom_destructor_stack)[0] != block1 ||
273 VECTOR(custom_destructor_stack)[1] != block2 ||
274 VECTOR(custom_destructor_stack)[2] != block1 ||
275 VECTOR(custom_destructor_stack)[3] != block2
276 ) {
277 return 26;
278 }
279
280 igraph_vector_ptr_destroy(&custom_destructor_stack);
281
282 VERIFY_FINALLY_STACK();
283
284 return 0;
285 }
286