diff options
author | Vincent Sanders <vince@kyllikki.org> | 2015-07-10 10:48:26 +0100 |
---|---|---|
committer | Vincent Sanders <vince@kyllikki.org> | 2015-07-10 10:48:26 +0100 |
commit | 94b5c956766a3f1ee916f2bb946c3d9273c90ae1 (patch) | |
tree | 6144918d9f0f4f5aafb06b6913502c5ae522b199 /utils | |
parent | 7b2d15a036bf4ef67eb88cfee23272d564c4a766 (diff) | |
download | netsurf-94b5c956766a3f1ee916f2bb946c3d9273c90ae1.tar.gz netsurf-94b5c956766a3f1ee916f2bb946c3d9273c90ae1.tar.bz2 |
Add unit test for bloom filter
Adds check based unit test for teh bloom filter implementation. This
is based on Roberts original test code in utils/bloom.c and uses
/usr/share/dict as a source of strings to check bloom creation, no
false negatives and the false positive rate is below 15%.
Diffstat (limited to 'utils')
-rw-r--r-- | utils/bloom.c | 61 |
1 files changed, 4 insertions, 57 deletions
diff --git a/utils/bloom.c b/utils/bloom.c index df9e76e1c..e51ee63fe 100644 --- a/utils/bloom.c +++ b/utils/bloom.c @@ -16,8 +16,10 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -/** \file - * Trivial bloom filter */ +/** + * \file + * Trivial bloom filter + */ #include <stdlib.h> #include "utils/bloom.h" @@ -107,58 +109,3 @@ uint32_t bloom_items(struct bloom_filter *b) return b->items; } -#ifdef TEST_RIG - -#include <stdio.h> -#include <string.h> -#include <assert.h> - -int main(int argc, char *arg[]) -{ - struct bloom_filter *b = bloom_create(8192); - FILE *dict = fopen("/usr/share/dict/words", "r"); - char buf[BUFSIZ]; - int false_positives = 0, total = 0; - - for (int i = 0; i < 8192; i++) { - fscanf(dict, "%s", buf); - printf("adding %s\n", buf); - bloom_insert_str(b, buf, strlen(buf)); - } - - printf("adding NetSurf\n"); - - bloom_insert_str(b, "NetSurf", 7); - printf("checking NetSurf (should be true)\n"); - assert(bloom_search_str(b, "NetSurf", 7)); - - fseek(dict, 0, SEEK_SET); - - for (int i = 0; i < 8192; i++) { - fscanf(dict, "%s", buf); - printf("checking %s (should be true)\n", buf); - assert(bloom_search_str(b, buf, strlen(buf))); - - total++; - } - - for (int i = 0; i < 8192; i++) { - fscanf(dict, "%s", buf); - printf("checking %s (should be false)\n", buf); - if (bloom_search_str(b, buf, strlen(buf)) == true) - false_positives++; - total++; - } - - printf("false positives: %d of %d, %f%%\n", - false_positives, total, - ((float)false_positives / total) * 100); - - fclose(dict); - bloom_destroy(b); - - return 0; -} - -#endif /* TEST_RIG */ - |