2 This file is part of GNUnet.
3 Copyright (C) 2004, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file util/test_container_bloomfilter.c
22 * @brief Testcase for the bloomfilter.
23 * @author Christian Grothoff
24 * @author Igor Wronsky
28 #include "gnunet_util_lib.h"
32 #define TESTFILE "/tmp/bloomtest.dat"
35 * Generate a random hashcode.
38 nextHC (struct GNUNET_HashCode * hc)
40 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, hc);
44 add_iterator (void *cls, struct GNUNET_HashCode * next)
47 struct GNUNET_HashCode pos;
57 main (int argc, char *argv[])
59 struct GNUNET_CONTAINER_BloomFilter *bf;
60 struct GNUNET_CONTAINER_BloomFilter *bfi;
61 struct GNUNET_HashCode tmp;
69 GNUNET_log_setup ("test-container-bloomfilter", "WARNING", NULL);
70 GNUNET_CRYPTO_seed_weak_random (1);
71 if (0 == STAT (TESTFILE, &sbuf))
72 if (0 != UNLINK (TESTFILE))
73 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink", TESTFILE);
74 bf = GNUNET_CONTAINER_bloomfilter_load (TESTFILE, SIZE, K);
76 for (i = 0; i < 200; i++)
79 GNUNET_CONTAINER_bloomfilter_add (bf, &tmp);
81 GNUNET_CRYPTO_seed_weak_random (1);
83 for (i = 0; i < 200; i++)
86 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
91 printf ("Got %d elements out of" "200 expected after insertion.\n", ok1);
92 GNUNET_CONTAINER_bloomfilter_free (bf);
95 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_get_raw_data (bf, buf, SIZE))
97 GNUNET_CONTAINER_bloomfilter_free (bf);
101 GNUNET_CONTAINER_bloomfilter_free (bf);
103 bf = GNUNET_CONTAINER_bloomfilter_load (TESTFILE, SIZE, K);
104 GNUNET_assert (bf != NULL);
105 bfi = GNUNET_CONTAINER_bloomfilter_init (buf, SIZE, K);
106 GNUNET_assert (bfi != NULL);
108 GNUNET_CRYPTO_seed_weak_random (1);
111 for (i = 0; i < 200; i++)
114 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
116 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES)
121 printf ("Got %d elements out of 200 " "expected after reloading.\n", ok1);
122 GNUNET_CONTAINER_bloomfilter_free (bf);
123 GNUNET_CONTAINER_bloomfilter_free (bfi);
129 printf ("Got %d elements out of 200 " "expected after initialization.\n",
131 GNUNET_CONTAINER_bloomfilter_free (bf);
132 GNUNET_CONTAINER_bloomfilter_free (bfi);
136 GNUNET_CRYPTO_seed_weak_random (1);
137 for (i = 0; i < 100; i++)
140 GNUNET_CONTAINER_bloomfilter_remove (bf, &tmp);
141 GNUNET_CONTAINER_bloomfilter_remove (bfi, &tmp);
144 GNUNET_CRYPTO_seed_weak_random (1);
148 for (i = 0; i < 200; i++)
151 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
153 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES)
159 printf ("Expected 100 elements in loaded filter"
160 " after adding 200 and deleting 100, got %d\n", ok1);
161 GNUNET_CONTAINER_bloomfilter_free (bf);
162 GNUNET_CONTAINER_bloomfilter_free (bfi);
167 printf ("Expected 200 elements in initialized filter"
168 " after adding 200 and deleting 100 "
169 "(which should do nothing for a filter not backed by a file), got %d\n",
171 GNUNET_CONTAINER_bloomfilter_free (bf);
172 GNUNET_CONTAINER_bloomfilter_free (bfi);
176 GNUNET_CRYPTO_seed_weak_random (3);
178 GNUNET_CONTAINER_bloomfilter_clear (bf);
180 for (i = 0; i < 1000; i++)
183 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
188 GNUNET_CONTAINER_bloomfilter_free (bf);
189 GNUNET_CONTAINER_bloomfilter_free (bfi);
193 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_or (bf, buf, SIZE))
195 GNUNET_CONTAINER_bloomfilter_free (bf);
196 GNUNET_CONTAINER_bloomfilter_free (bfi);
200 GNUNET_CRYPTO_seed_weak_random (2);
202 GNUNET_CONTAINER_bloomfilter_resize (bfi, &add_iterator, &i, SIZE * 2, K);
204 GNUNET_CRYPTO_seed_weak_random (2);
206 GNUNET_CONTAINER_bloomfilter_resize (bf, &add_iterator, &i, SIZE * 2, K);
207 GNUNET_CRYPTO_seed_weak_random (2);
211 for (i = 0; i < 20; i++)
214 if (GNUNET_CONTAINER_bloomfilter_test (bf, &tmp) == GNUNET_YES)
216 if (GNUNET_CONTAINER_bloomfilter_test (bfi, &tmp) == GNUNET_YES)
222 printf ("Expected 20 elements in resized file-backed filter"
223 " after adding 20, got %d\n", ok1);
224 GNUNET_CONTAINER_bloomfilter_free (bf);
225 GNUNET_CONTAINER_bloomfilter_free (bfi);
230 printf ("Expected 20 elements in resized filter"
231 " after adding 20, got %d\n", ok2);
232 GNUNET_CONTAINER_bloomfilter_free (bf);
233 GNUNET_CONTAINER_bloomfilter_free (bfi);
238 GNUNET_CONTAINER_bloomfilter_free (bf);
239 GNUNET_CONTAINER_bloomfilter_free (bfi);
241 GNUNET_break (0 == UNLINK (TESTFILE));