-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBloomFilter_test.c
88 lines (68 loc) · 2.18 KB
/
BloomFilter_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include "acutest/acutest.h"
#include "../modules/BloomFilter/BloomFilter.h"
// Define the hash functions
long unsigned int DJB2_hash(void *k){
char *key = (char *)k;
unsigned int hash = 5381;
int c;
while ((c = *key++))
hash = ((hash << 5) + hash) + c; // hash * 33 + c
return hash;
}
long unsigned int SDBM_hash(void *k) {
char *key = (char *)k;
unsigned int hash = 0;
int c;
while ((c = *key++))
hash = c + (hash << 6) + (hash << 16) - hash;
return hash;
}
long unsigned int h1(void *k) {
char *key = (char *)k;
unsigned int hash = 0;
int c;
while ((c = *key++))
hash += c;
return hash;
}
void test_bloom_filter_create() {
// Define the hash functions
HashFunc hash_functions[] = {DJB2_hash, SDBM_hash, h1};
// Test creating a bloom filter
BloomFilter bf = bloom_filter_create(64, 3, hash_functions);
TEST_CHECK(bf != NULL);
bloom_filter_destroy(bf);
}
void test_bloom_filter_insert_and_check() {
// Define the hash functions
HashFunc hash_functions[] = {DJB2_hash, SDBM_hash, h1};
BloomFilter bf = bloom_filter_create(64, 3, hash_functions);
// Test inserting and checking a key
int key = 42;
bloom_filter_insert(bf, &key);
TEST_CHECK(bloom_filter_check(bf, &key));
// Test checking a non-inserted key
int non_inserted_key = 123;
TEST_CHECK(!bloom_filter_check(bf, &non_inserted_key));
bloom_filter_destroy(bf);
}
void test_bloom_filter_reset() {
// Define the hash functions
HashFunc hash_functions[] = {DJB2_hash, SDBM_hash, h1};
BloomFilter bf = bloom_filter_create(64, 3, hash_functions);
// Insert a key and reset the bloom filter
int key = 42;
bloom_filter_insert(bf, &key);
TEST_CHECK(bloom_filter_check(bf, &key));
bloom_filter_reset(bf);
TEST_CHECK(!bloom_filter_check(bf, &key));
bloom_filter_destroy(bf);
}
TEST_LIST = {
{"test_bloom_filter_create", test_bloom_filter_create},
{"test_bloom_filter_insert_and_check", test_bloom_filter_insert_and_check},
{"test_bloom_filter_reset", test_bloom_filter_reset},
{NULL, NULL} // End of the test list
};