~ubuntu-branches/ubuntu/precise/igraph/precise

« back to all changes in this revision

Viewing changes to examples/simple/vector_ptr.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathieu Malaterre
  • Date: 2009-11-16 18:12:42 UTC
  • Revision ID: james.westby@ubuntu.com-20091116181242-mzv9p5fz9uj57xd1
Tags: upstream-0.5.3
ImportĀ upstreamĀ versionĀ 0.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C -*-  */
 
2
/* 
 
3
   IGraph library.
 
4
   Copyright (C) 2006  Gabor Csardi <csardi@rmki.kfki.hu>
 
5
   MTA RMKI, Konkoly-Thege Miklos st. 29-33, Budapest 1121, Hungary
 
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
int main() {
 
28
  
 
29
  igraph_vector_ptr_t v1, v2;
 
30
  const igraph_vector_ptr_t v3=IGRAPH_VECTOR_PTR_NULL;
 
31
  int i;
 
32
  void ** ptr;
 
33
  int d1=1, d2=2, d3=3, d4=4, d5=5;
 
34
 
 
35
  /* igraph_vector_ptr_init, igraph_vector_ptr_destroy */
 
36
  igraph_vector_ptr_init(&v1, 10);
 
37
  igraph_vector_ptr_destroy(&v1);
 
38
  igraph_vector_ptr_init(&v1, 0);
 
39
  igraph_vector_ptr_destroy(&v1);
 
40
 
 
41
  /* igraph_vector_ptr_free_all, igraph_vector_ptr_destroy_all */
 
42
  igraph_vector_ptr_init(&v1, 5);
 
43
  for (i=0; i<igraph_vector_ptr_size(&v1); i++) {
 
44
    VECTOR(v1)[i]=(void*)malloc(i*10);
 
45
  }
 
46
  igraph_vector_ptr_free_all(&v1);
 
47
  for (i=0; i<igraph_vector_ptr_size(&v1); i++) {
 
48
    VECTOR(v1)[i]=(void*)malloc(i*10);
 
49
  }
 
50
  igraph_vector_ptr_destroy_all(&v1);     
 
51
  
 
52
  /* igraph_vector_ptr_reserve */
 
53
  igraph_vector_ptr_init(&v1, 0);
 
54
  igraph_vector_ptr_reserve(&v1, 5);
 
55
  igraph_vector_ptr_reserve(&v1, 15);
 
56
  igraph_vector_ptr_reserve(&v1, 1);
 
57
  igraph_vector_ptr_reserve(&v1, 0);
 
58
  igraph_vector_ptr_destroy(&v1);
 
59
 
 
60
  /* igraph_vector_ptr_empty, igraph_vector_ptr_clear */
 
61
  igraph_vector_ptr_init(&v1, 10);
 
62
  if (igraph_vector_ptr_empty(&v1)) {
 
63
    return 1;
 
64
  }
 
65
  igraph_vector_ptr_clear(&v1);
 
66
  if (!igraph_vector_ptr_empty(&v1)) {
 
67
    return 2;
 
68
  }
 
69
 
 
70
  /* igraph_vector_ptr_size */
 
71
  if (igraph_vector_ptr_size(&v1) != 0) {
 
72
    return 3;
 
73
  }
 
74
  igraph_vector_ptr_resize(&v1, 10);
 
75
  if (igraph_vector_ptr_size(&v1) != 10) {
 
76
    return 4;
 
77
  }
 
78
  igraph_vector_ptr_destroy(&v1);
 
79
 
 
80
  /* igraph_vector_ptr_push_back */
 
81
  igraph_vector_ptr_init(&v1, 0);
 
82
  for (i=0; i<10; i++) {
 
83
    igraph_vector_ptr_push_back(&v1, (void*)malloc(i*10));
 
84
  }
 
85
  igraph_vector_ptr_destroy_all(&v1);
 
86
  
 
87
  /* igraph_vector_ptr_e */
 
88
  igraph_vector_ptr_init(&v1, 5);
 
89
  VECTOR(v1)[0]=&d1;
 
90
  VECTOR(v1)[1]=&d2;
 
91
  VECTOR(v1)[2]=&d3;
 
92
  VECTOR(v1)[3]=&d4;
 
93
  VECTOR(v1)[4]=&d5;
 
94
  if (igraph_vector_ptr_e(&v1, 0) != &d1) {
 
95
    return 5;
 
96
  }
 
97
  if (igraph_vector_ptr_e(&v1, 1) != &d2) {
 
98
    return 6;
 
99
  }
 
100
  if (igraph_vector_ptr_e(&v1, 2) != &d3) {
 
101
    return 7;
 
102
  }
 
103
  if (igraph_vector_ptr_e(&v1, 3) != &d4) {
 
104
    return 8;
 
105
  }
 
106
  if (igraph_vector_ptr_e(&v1, 4) != &d5) {
 
107
    return 9;
 
108
  }
 
109
  igraph_vector_ptr_destroy(&v1);
 
110
 
 
111
  /* igraph_vector_ptr_set */
 
112
  igraph_vector_ptr_init(&v1, 5);
 
113
  igraph_vector_ptr_set(&v1, 0, &d1);
 
114
  igraph_vector_ptr_set(&v1, 1, &d2);
 
115
  igraph_vector_ptr_set(&v1, 2, &d3);
 
116
  igraph_vector_ptr_set(&v1, 3, &d4);
 
117
  igraph_vector_ptr_set(&v1, 4, &d5);
 
118
  if (igraph_vector_ptr_e(&v1, 0) != &d1) {
 
119
    return 5;
 
120
  }
 
121
  if (igraph_vector_ptr_e(&v1, 1) != &d2) {
 
122
    return 6;
 
123
  }
 
124
  if (igraph_vector_ptr_e(&v1, 2) != &d3) {
 
125
    return 7;
 
126
  }
 
127
  if (igraph_vector_ptr_e(&v1, 3) != &d4) {
 
128
    return 8;
 
129
  }
 
130
  if (igraph_vector_ptr_e(&v1, 4) != &d5) {
 
131
    return 9;
 
132
  }
 
133
  igraph_vector_ptr_destroy(&v1);
 
134
 
 
135
  /* igraph_vector_ptr_null */
 
136
  igraph_vector_ptr_init(&v1, 5);
 
137
  igraph_vector_ptr_set(&v1, 0, &d1);
 
138
  igraph_vector_ptr_set(&v1, 1, &d2);
 
139
  igraph_vector_ptr_set(&v1, 2, &d3);
 
140
  igraph_vector_ptr_set(&v1, 3, &d4);
 
141
  igraph_vector_ptr_set(&v1, 4, &d5);
 
142
  igraph_vector_ptr_null(&v1);
 
143
  for (i=0; i<igraph_vector_ptr_size(&v1); i++) {
 
144
    if (VECTOR(v1)[i] != 0) {
 
145
      return 10;
 
146
    }
 
147
  }
 
148
  igraph_vector_ptr_destroy(&v1);
 
149
 
 
150
  /* igraph_vector_ptr_resize */
 
151
  igraph_vector_ptr_init(&v1, 10);
 
152
  igraph_vector_ptr_set(&v1, 0, &d1);
 
153
  igraph_vector_ptr_set(&v1, 1, &d2);
 
154
  igraph_vector_ptr_set(&v1, 2, &d3);
 
155
  igraph_vector_ptr_set(&v1, 3, &d4);
 
156
  igraph_vector_ptr_set(&v1, 4, &d5);
 
157
  igraph_vector_ptr_resize(&v1, 10);
 
158
  igraph_vector_ptr_resize(&v1, 15);
 
159
  igraph_vector_ptr_resize(&v1, 5);
 
160
  if (igraph_vector_ptr_size(&v1) != 5) {
 
161
    return 11;
 
162
  }
 
163
  if (igraph_vector_ptr_e(&v1, 0) != &d1) {
 
164
    return 12;
 
165
  }
 
166
  if (igraph_vector_ptr_e(&v1, 1) != &d2) {
 
167
    return 13;
 
168
  }
 
169
  if (igraph_vector_ptr_e(&v1, 2) != &d3) {
 
170
    return 14;
 
171
  }
 
172
  if (igraph_vector_ptr_e(&v1, 3) != &d4) {
 
173
    return 15;
 
174
  }
 
175
  if (igraph_vector_ptr_e(&v1, 4) != &d5) {
 
176
    return 16;
 
177
  }
 
178
  igraph_vector_ptr_destroy(&v1);
 
179
 
 
180
  /* igraph_vector_ptr_view */
 
181
  ptr=(void**) malloc(5 * sizeof(void*));
 
182
  igraph_vector_ptr_view(&v3, ptr, 5);
 
183
  ptr[0]=&d1; ptr[1]=&d2; ptr[2]=&d3; ptr[3]=&d4; ptr[4]=&d5;
 
184
  for (i=0; i<igraph_vector_ptr_size(&v3); i++) {
 
185
    if ( *((int*)VECTOR(v3)[i]) != i+1) {
 
186
      return 17;
 
187
    }
 
188
  }
 
189
  
 
190
  /* igraph_vector_ptr_init_copy */
 
191
  igraph_vector_ptr_init_copy(&v1, ptr, 5);
 
192
  for (i=0; i<igraph_vector_ptr_size(&v1); i++) {
 
193
    if ( *((int*)VECTOR(v1)[i]) != i+1) {
 
194
      return 18;
 
195
    }
 
196
  }
 
197
 
 
198
  /* igraph_vector_ptr_copy_to */
 
199
  igraph_vector_ptr_copy_to(&v1, ptr);
 
200
  for (i=0; i<igraph_vector_ptr_size(&v1); i++) {
 
201
    if ( *((int*)ptr[i]) != i+1) {
 
202
      return 19;
 
203
    }
 
204
  }
 
205
  free(ptr);
 
206
  igraph_vector_ptr_destroy(&v1);
 
207
 
 
208
  /* igraph_vector_ptr_copy */
 
209
  igraph_vector_ptr_init(&v1, 5);
 
210
  igraph_vector_ptr_set(&v1, 0, &d1);
 
211
  igraph_vector_ptr_set(&v1, 1, &d2);
 
212
  igraph_vector_ptr_set(&v1, 2, &d3);
 
213
  igraph_vector_ptr_set(&v1, 3, &d4);
 
214
  igraph_vector_ptr_set(&v1, 4, &d5);
 
215
  igraph_vector_ptr_copy(&v2, &v1);
 
216
  igraph_vector_ptr_destroy(&v1);
 
217
  for (i=0; i<igraph_vector_ptr_size(&v2); i++) {
 
218
    if ( *((int*)VECTOR(v2)[i]) != i+1) {
 
219
      return 20;
 
220
    }
 
221
  }
 
222
 
 
223
  /* igraph_vector_ptr_remove */
 
224
  igraph_vector_ptr_remove(&v2, 0);
 
225
  igraph_vector_ptr_remove(&v2, 3);
 
226
  if ( *((int*)VECTOR(v2)[0]) != 2) {
 
227
      return 21;
 
228
  }
 
229
  if ( *((int*)VECTOR(v2)[1]) != 3) {
 
230
      return 22;
 
231
  }
 
232
  if ( *((int*)VECTOR(v2)[2]) != 4) {
 
233
      return 23;
 
234
  }
 
235
 
 
236
  igraph_vector_ptr_destroy(&v2);
 
237
   
 
238
  if (IGRAPH_FINALLY_STACK_SIZE() != 0) return 24;
 
239
 
 
240
  return 0;
 
241
}