Skip to content

Commit 3811805

Browse files
committed
initial commit of the tests
1 parent 4d1efd2 commit 3811805

14 files changed

+3623
-0
lines changed

tests/AVLTree_test.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include "acutest/acutest.h"
4+
#include "../modules/AVLTree/AVLTree.h"
5+
6+
/* Helper function to create a sample integer value */
7+
int *create_int(int value) {
8+
int *ptr = (int *)malloc(sizeof(int));
9+
*ptr = value;
10+
return ptr;
11+
}
12+
13+
// void print_int(void *value) {
14+
// printf(" %d ", *(int *)value);
15+
// }
16+
17+
18+
/* Helper function to compare integers */
19+
int compare_ints(void *a, void *b) {
20+
return *(int *)a - *(int *)b;
21+
}
22+
23+
void test_avl_tree_insert_and_search() {
24+
AVLTree avl = NULL;
25+
26+
// Insert and search for elements in the AVL tree
27+
avl = AVLTree_insert(avl, create_int(5), compare_ints);
28+
AVLTree_insert(avl, create_int(3), compare_ints);
29+
AVLTree_insert(avl, create_int(7), compare_ints);
30+
31+
TEST_CHECK(AVLTree_search(avl, create_int(5), compare_ints));
32+
TEST_CHECK(AVLTree_search(avl, create_int(3), compare_ints));
33+
TEST_CHECK(AVLTree_search(avl, create_int(7), compare_ints));
34+
TEST_CHECK(!AVLTree_search(avl, create_int(2), compare_ints));
35+
36+
// Clean up
37+
AVLTree_destroy(avl, free);
38+
}
39+
40+
void test_avl_tree_delete() {
41+
AVLTree avl = NULL;
42+
43+
// Insert and delete elements from the AVL tree
44+
avl = AVLTree_insert(avl, create_int(5), compare_ints);
45+
TEST_CHECK(AVLTree_search(avl, create_int(5), compare_ints));
46+
avl = AVLTree_insert(avl, create_int(3), compare_ints);
47+
TEST_CHECK(AVLTree_search(avl, create_int(3), compare_ints));
48+
avl = AVLTree_insert(avl, create_int(7), compare_ints);
49+
TEST_CHECK(AVLTree_search(avl, create_int(7), compare_ints));
50+
51+
avl = AVLTree_delete(avl, create_int(5), compare_ints, free);
52+
TEST_CHECK(!AVLTree_search(avl, create_int(5), compare_ints));
53+
avl = AVLTree_delete(avl, create_int(3), compare_ints, free);
54+
TEST_CHECK(!AVLTree_search(avl, create_int(3), compare_ints));
55+
avl =AVLTree_delete(avl, create_int(7), compare_ints, free);
56+
TEST_CHECK(!AVLTree_search(avl, create_int(7), compare_ints));
57+
58+
TEST_CHECK(AVLTree_empty(avl));
59+
60+
// Clean up
61+
AVLTree_destroy(avl, free);
62+
}
63+
64+
void test_avl_tree_select_k_th_item() {
65+
AVLTree avl = NULL;
66+
67+
// Insert elements into the AVL tree
68+
avl = AVLTree_insert(avl, create_int(5), compare_ints);
69+
avl = AVLTree_insert(avl, create_int(3), compare_ints);
70+
avl = AVLTree_insert(avl, create_int(7), compare_ints);
71+
72+
// Test selecting k-th item
73+
TEST_CHECK(*(int *)AVLTree_select_k_th_item(avl, 1) == 3);
74+
TEST_CHECK(*(int *)AVLTree_select_k_th_item(avl, 2) == 5);
75+
TEST_CHECK(*(int *)AVLTree_select_k_th_item(avl, 3) == 7);
76+
77+
// Clean up
78+
AVLTree_destroy(avl, free);
79+
}
80+
81+
void test_avl_tree_count_items() {
82+
AVLTree avl = NULL;
83+
84+
// Insert and delete elements from the AVL tree
85+
avl = AVLTree_insert(avl, create_int(5), compare_ints);
86+
avl = AVLTree_insert(avl, create_int(3), compare_ints);
87+
avl = AVLTree_insert(avl, create_int(7), compare_ints);
88+
89+
// Test counting items in the AVL tree
90+
TEST_CHECK(AVL_count_items(avl) == 3); // Corrected the macro to TEST_CHECK
91+
92+
// Clean up
93+
AVLTree_destroy(avl, free);
94+
}
95+
96+
void test_avl_tree_min_max_values() {
97+
AVLTree avl = NULL;
98+
99+
// Insert elements into the AVL tree
100+
avl = AVLTree_insert(avl, create_int(5), compare_ints);
101+
avl = AVLTree_insert(avl, create_int(3), compare_ints);
102+
avl = AVLTree_insert(avl, create_int(7), compare_ints);
103+
104+
// Test finding min and max values
105+
TEST_CHECK(*(int *)AVLTree_min_value(avl) == 3);
106+
TEST_CHECK(*(int *)AVLTree_max_value(avl) == 7);
107+
108+
// Clean up
109+
AVLTree_destroy(avl, free);
110+
}
111+
112+
TEST_LIST = {
113+
{"test_avl_tree_insert_and_search", test_avl_tree_insert_and_search},
114+
{"test_avl_tree_delete", test_avl_tree_delete},
115+
{"test_avl_tree_select_k_th_item", test_avl_tree_select_k_th_item},
116+
{"test_avl_tree_count_items", test_avl_tree_count_items},
117+
{"test_avl_tree_min_max_values", test_avl_tree_min_max_values},
118+
{NULL, NULL} // Terminate the test list
119+
};

tests/BloomFilter_test.c

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

tests/ChainingHashTable_test.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "acutest/acutest.h"
5+
#include "../modules/SeparateChainingHashTable/ChainingHashTable.h"
6+
7+
8+
// Hash functions
9+
long unsigned int DJB2_hash(void *k) {
10+
char *key = (char *)k;
11+
unsigned int hash = 5381;
12+
int c;
13+
14+
while ((c = *key++))
15+
hash = ((hash << 5) + hash) + c; // hash * 33 + c
16+
17+
return hash;
18+
}
19+
20+
long unsigned int SDBM_hash(void *k) {
21+
char *key = (char *)k;
22+
unsigned int hash = 0;
23+
int c;
24+
25+
while ((c = *key++))
26+
hash = c + (hash << 6) + (hash << 16) - hash;
27+
28+
return hash;
29+
}
30+
31+
static int compare_strings(void *a, void *b) {
32+
return strcmp((char *)a, (char *)b);
33+
}
34+
35+
// static void *copy_int(void *data) {
36+
// int *new_data = (int *)malloc(sizeof(int));
37+
// if (new_data) {
38+
// *new_data = *(int *)data;
39+
// }
40+
// return new_data;
41+
// }
42+
43+
static void mock_print(void *data) {
44+
// Implement a mock print function to capture the printed output
45+
// In this example, we won't check the output, only if the function was called.
46+
}
47+
48+
static void test_separate_chaining_hash_table_insert_and_search() {
49+
SCHashtable *hash_table = SChashtable_create(compare_strings, mock_print, free, NULL, DJB2_hash);
50+
51+
char *keys[] = {"key1", "key2", "key3", "key4", "key5"};
52+
int values[] = {100, 200, 300, 400, 500};
53+
int num_elements = sizeof(keys) / sizeof(keys[0]);
54+
55+
// Insert key-value pairs
56+
for (int i = 0; i < num_elements; i++) {
57+
char *key_copy = (char *)malloc(strlen(keys[i]) + 1);
58+
strcpy(key_copy, keys[i]);
59+
SChashtable_insert(hash_table, key_copy, &values[i]);
60+
}
61+
62+
// Search for inserted key-value pairs
63+
for (int i = 0; i < num_elements; i++) {
64+
void *value = SChashtable_search(hash_table, keys[i]);
65+
TEST_CHECK(*(int *)value == values[i]);
66+
}
67+
68+
// Search for non-existing key
69+
char *non_existing_key = "key6";
70+
void *value = SChashtable_search(hash_table, non_existing_key);
71+
TEST_CHECK(value == NULL);
72+
73+
SChashtable_destroy(hash_table);
74+
}
75+
76+
77+
static void test_separate_chaining_hash_table_remove() {
78+
SCHashtable *hash_table = SChashtable_create(compare_strings, mock_print, free, NULL, SDBM_hash);
79+
80+
char *keys[] = {"key1", "key2", "key3", "key4", "key5"};
81+
int values[] = {100, 200, 300, 400, 500};
82+
int num_elements = sizeof(keys) / sizeof(keys[0]);
83+
84+
// Insert key-value pairs
85+
for (int i = 0; i < num_elements; i++) {
86+
char *key_copy = (char *)malloc(strlen(keys[i]) + 1);
87+
strcpy(key_copy, keys[i]);
88+
SChashtable_insert(hash_table, key_copy, &values[i]);
89+
}
90+
91+
// Remove key-value pairs
92+
SChashtable_remove(hash_table, "key1");
93+
SChashtable_remove(hash_table, "key3");
94+
SChashtable_remove(hash_table, "key5");
95+
96+
// Search for removed key-value pairs
97+
void *value = SChashtable_search(hash_table, "key1");
98+
TEST_CHECK(value == NULL);
99+
100+
value = SChashtable_search(hash_table, "key3");
101+
TEST_CHECK(value == NULL);
102+
103+
value = SChashtable_search(hash_table, "key5");
104+
TEST_CHECK(value == NULL);
105+
106+
// Search for existing key-value pairs
107+
value = SChashtable_search(hash_table, "key2");
108+
TEST_CHECK(*(int *)value == 200);
109+
110+
value = SChashtable_search(hash_table, "key4");
111+
TEST_CHECK(*(int *)value == 400);
112+
113+
SChashtable_destroy(hash_table);
114+
}
115+
116+
117+
TEST_LIST = {
118+
{"test_separate_chaining_hash_table_insert_and_search", test_separate_chaining_hash_table_insert_and_search},
119+
{"test_separate_chaining_hash_table_remove", test_separate_chaining_hash_table_remove},
120+
{NULL, NULL}
121+
};

0 commit comments

Comments
 (0)