5
Copyright (C) 2006 Gabor Csardi <csardi@rmki.kfki.hu>
6
MTA RMKI, Konkoly-Thege Miklos st. 29-33, Budapest 1121, Hungary
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28
int print_vector(igraph_vector_t *v) {
29
long int i, n=igraph_vector_size(v);
31
printf("%li ", (long int) VECTOR(*v)[i]);
38
igraph_psumtree_t tree;
43
/* Uniform random numbers */
44
igraph_vector_init(&vec, 16);
45
igraph_psumtree_init(&tree, 16);
46
sum=igraph_psumtree_sum(&tree);
48
printf("Sum: %f instead of 0.\n", sum);
52
for (i=0; i<16; i++) {
53
igraph_psumtree_update(&tree, i, 1);
55
if ((sum=igraph_psumtree_sum(&tree)) != 16) {
56
printf("Sum: %f instead of 16.\n", sum);
60
for (i=0; i<16000; i++) {
61
igraph_real_t r=((double)rand())/RAND_MAX * sum;
63
igraph_psumtree_search(&tree, &idx, r);
64
VECTOR(vec)[idx] += 1;
66
for (i=0; i<16; i++) {
67
if (VECTOR(vec)[i] < 800 || VECTOR(vec)[i] > 1200) {
72
/* Nonuniform, even indices have twice as much chance */
73
for (i=0; i<16; i+=2) {
74
igraph_psumtree_update(&tree, i, 2);
76
if ((sum=igraph_psumtree_sum(&tree)) != 24) {
77
printf("Sum: %f instead of 24.\n", sum);
81
igraph_vector_null(&vec);
82
for (i=0; i<24000; i++) {
83
igraph_real_t r=((double)rand())/RAND_MAX * sum;
85
igraph_psumtree_search(&tree, &idx, r);
86
VECTOR(vec)[idx] += 1;
88
for (i=0; i<16; i++) {
89
if (i%2 == 0 && (VECTOR(vec)[i] < 1800 || VECTOR(vec)[i] > 2200)) {
92
if (i%2 != 0 && (VECTOR(vec)[i] < 800 || VECTOR(vec)[i] > 1200)) {
97
/* Test zero probabilities */
98
igraph_psumtree_update(&tree, 0, 0);
99
igraph_psumtree_update(&tree, 5, 0);
100
igraph_psumtree_update(&tree, 15, 0);
101
sum=igraph_psumtree_sum(&tree);
103
igraph_vector_null(&vec);
104
for (i=0; i<20000; i++) {
105
igraph_real_t r=((double)rand())/RAND_MAX * sum;
107
igraph_psumtree_search(&tree, &idx, r);
108
VECTOR(vec)[idx] += 1;
110
if (VECTOR(vec)[0] != 0 || VECTOR(vec)[5] != 0 || VECTOR(vec)[15] != 0) {
114
igraph_vector_destroy(&vec);
115
igraph_psumtree_destroy(&tree);
117
/****************************************************/
118
/* Non power-of-two vector size */
119
/****************************************************/
121
igraph_vector_init(&vec, 9);
122
igraph_psumtree_init(&tree, 9);
124
for (i=0; i<9; i++) {
125
igraph_psumtree_update(&tree, i, 1);
127
sum=igraph_psumtree_sum(&tree);
129
for (i=0; i<9000; i++) {
130
igraph_real_t r=((double)rand())/RAND_MAX * sum;
132
igraph_psumtree_search(&tree, &idx, r);
133
VECTOR(vec)[idx] += 1;
135
for (i=0; i<9; i++) {
136
if (VECTOR(vec)[i] < 800 || VECTOR(vec)[i] > 1200) {
141
/* Nonuniform, even indices have twice as much chance */
142
for (i=0; i<9; i+=2) {
143
igraph_psumtree_update(&tree, i, 2);
145
sum=igraph_psumtree_sum(&tree);
147
igraph_vector_null(&vec);
148
for (i=0; i<14000; i++) {
149
igraph_real_t r=((double)rand())/RAND_MAX * sum;
151
igraph_psumtree_search(&tree, &idx, r);
152
VECTOR(vec)[idx] += 1;
154
for (i=0; i<9; i++) {
155
if (i%2 == 0 && (VECTOR(vec)[i] < 1800 || VECTOR(vec)[i] > 2200)) {
158
if (i%2 != 0 && (VECTOR(vec)[i] < 800 || VECTOR(vec)[i] > 1200)) {
164
for (i=0; i<igraph_psumtree_size(&tree); i++) {
165
if (i%2==0 && igraph_psumtree_get(&tree, i) != 2) {
168
if (i%2!=0 && igraph_psumtree_get(&tree, i) != 1) {
173
/* Test zero probabilities */
174
igraph_psumtree_update(&tree, 0, 0);
175
igraph_psumtree_update(&tree, 5, 0);
176
igraph_psumtree_update(&tree, 8, 0);
177
sum=igraph_psumtree_sum(&tree);
179
igraph_vector_null(&vec);
180
for (i=0; i<9000; i++) {
181
igraph_real_t r=((double)rand())/RAND_MAX * sum;
183
igraph_psumtree_search(&tree, &idx, r);
184
VECTOR(vec)[idx] += 1;
186
if (VECTOR(vec)[0] != 0 || VECTOR(vec)[5] != 0 || VECTOR(vec)[8] != 0) {
190
igraph_vector_destroy(&vec);
191
igraph_psumtree_destroy(&tree);
193
if (!IGRAPH_FINALLY_STACK_EMPTY) return 13;