feat: add tests
This commit is contained in:
90
test/hashmap/hm1.c
Normal file
90
test/hashmap/hm1.c
Normal file
@@ -0,0 +1,90 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../../hashmap/myhashmap.h"
|
||||
|
||||
#define MAX_STR_LEN 64
|
||||
|
||||
/* My custom data type stored as value */
|
||||
struct my_custom_type {
|
||||
int age;
|
||||
char favourite_brand[MAX_STR_LEN];
|
||||
};
|
||||
|
||||
/* Let's hash our name */
|
||||
static unsigned int my_hash_func(const void *key) {
|
||||
char *name = (char *)key;
|
||||
size_t len = strlen(name);
|
||||
|
||||
unsigned int hash = 0;
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
hash += (int)name[i];
|
||||
}
|
||||
|
||||
return hash % 2069;
|
||||
}
|
||||
|
||||
/* Let's write our compare function */
|
||||
bool my_equal_fun(const void *key_a, const void *key_b) {
|
||||
char *name_a = (char *)key_a;
|
||||
char *name_b = (char *)key_b;
|
||||
|
||||
if (strcmp(name_a, name_b) == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* And our last two functions, the free key and value called inside mcl_hm_remove() */
|
||||
void my_free_key(void *key) { free(key); }
|
||||
|
||||
void my_free_value(void *value) {
|
||||
struct my_custom_type *mct = (struct my_custom_type *)value;
|
||||
|
||||
free(mct);
|
||||
}
|
||||
|
||||
void test_hm1(void) {
|
||||
/* Allocate a new hashmap */
|
||||
/* Pass your custom hash, equal and free functions */
|
||||
/* This hashmap will contain names as keys and a custom type as value */
|
||||
size_t key_size = sizeof(char) * MAX_STR_LEN;
|
||||
size_t value_size = sizeof(int) + sizeof(char) * MAX_STR_LEN;
|
||||
mcl_hashmap_s *map = mcl_hm_new(my_hash_func, my_equal_fun, my_free_key, my_free_value, key_size, value_size);
|
||||
assert(map != NULL);
|
||||
|
||||
/* Make a new value */
|
||||
struct my_custom_type p1 = {
|
||||
.age = 21,
|
||||
};
|
||||
strncpy(p1.favourite_brand, "Ferrari", sizeof(p1.favourite_brand));
|
||||
|
||||
/* Insert a new pair */
|
||||
assert(mcl_hm_set(map, "John", &p1));
|
||||
|
||||
/* Retrieve the data */
|
||||
/* Remember to free the value from the get function */
|
||||
mcl_bucket_s *john = mcl_hm_get(map, "John");
|
||||
assert(john != NULL);
|
||||
|
||||
char *name = (char *)john->key;
|
||||
struct my_custom_type *john_v = (struct my_custom_type *)john->value;
|
||||
int age = john_v->age;
|
||||
char *fav_brand = john_v->favourite_brand;
|
||||
|
||||
assert(strcmp(name, "John") == 0);
|
||||
assert(age == 21);
|
||||
assert(strcmp(fav_brand, "Ferrari") == 0);
|
||||
|
||||
/* Free the bucket */
|
||||
mcl_hm_free_bucket(john);
|
||||
|
||||
/* Remove a key from hash map */
|
||||
assert(mcl_hm_remove(map, "John"));
|
||||
|
||||
/* Deallocate */
|
||||
mcl_hm_free(map);
|
||||
}
|
||||
48
test/queue/q1.c
Normal file
48
test/queue/q1.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <assert.h>
|
||||
|
||||
#include "../../queue/myqueue.h"
|
||||
|
||||
void test_q1(void) {
|
||||
/* Allocate a new queue */
|
||||
mcl_queue_s *queue = mcl_queue_new(3, sizeof(int));
|
||||
assert(queue != NULL);
|
||||
|
||||
int val, out;
|
||||
|
||||
/* Push value to the ring buffer */
|
||||
val = 1;
|
||||
assert(mcl_queue_push(queue, &val) == 0);
|
||||
|
||||
val = 2;
|
||||
assert(mcl_queue_push(queue, &val) == 0);
|
||||
|
||||
/* Retrieve values */
|
||||
int front, rear;
|
||||
assert(mcl_queue_get_front(queue, &front) == 0);
|
||||
assert(mcl_queue_get_rear(queue, &rear) == 0);
|
||||
assert(front == 1);
|
||||
assert(rear == 2);
|
||||
|
||||
/* Remove an element from the buffer */
|
||||
assert(mcl_queue_pop(queue, &out) == 0);
|
||||
assert(out == 1);
|
||||
|
||||
assert(mcl_queue_get_front(queue, &front) == 0);
|
||||
assert(front == 2);
|
||||
|
||||
val = 3;
|
||||
assert(mcl_queue_push(queue, &val) == 0);
|
||||
|
||||
assert(mcl_queue_get_rear(queue, &rear) == 0);
|
||||
assert(rear == 3);
|
||||
|
||||
val = 4;
|
||||
assert(mcl_queue_push(queue, &val) == 0);
|
||||
|
||||
/* Clear queue */
|
||||
while (mcl_queue_pop(queue, &out) == 0) {}
|
||||
assert(queue->size == 0);
|
||||
|
||||
/* Deallocate memory */
|
||||
mcl_queue_free(queue);
|
||||
}
|
||||
37
test/string/str1.c
Normal file
37
test/string/str1.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../../string/mystring.h"
|
||||
|
||||
void test_str1(void) {
|
||||
size_t length;
|
||||
size_t capacity;
|
||||
char *c_str;
|
||||
|
||||
/* Allocate a new dynamic string with an initial capacity */
|
||||
mcl_string_s *str = mcl_string_new("Hello, world!", 512);
|
||||
assert(str != NULL);
|
||||
|
||||
/* Retrieve a C str from string with mcl_string_cstr() */
|
||||
c_str = mcl_string_cstr(str);
|
||||
length = mcl_string_length(str);
|
||||
capacity = mcl_string_capacity(str);
|
||||
assert(strcmp(c_str, "Hello, world!") == 0);
|
||||
assert(length == 13);
|
||||
assert(capacity == 512);
|
||||
|
||||
/* Append text to a mcl_string */
|
||||
assert(mcl_string_append(str, " How are you?") == 0);
|
||||
|
||||
c_str = mcl_string_cstr(str);
|
||||
length = mcl_string_length(str);
|
||||
capacity = mcl_string_capacity(str);
|
||||
assert(strcmp(c_str, "Hello, world! How are you?") == 0);
|
||||
assert(length == 26);
|
||||
assert(capacity == 512);
|
||||
|
||||
/* Always deallocate memory */
|
||||
mcl_string_free(str);
|
||||
}
|
||||
27
test/string/str2.c
Normal file
27
test/string/str2.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../../string/mystring.h"
|
||||
|
||||
void test_str2(void) {
|
||||
mcl_string_s *s1 = mcl_string_new("Hello, world!", 0);
|
||||
assert(s1 != NULL);
|
||||
mcl_string_s *s2 = mcl_string_new("Hello, world!", 0);
|
||||
assert(s2 != NULL);
|
||||
/* Don't call mcl_string_cstr() more than once in the same printf function */
|
||||
|
||||
int ret = mcl_string_compare(s1, s2);
|
||||
assert(ret == 0);
|
||||
|
||||
mcl_string_clear(s1);
|
||||
ret = mcl_string_compare(s1, s2);
|
||||
assert(ret != 0);
|
||||
|
||||
mcl_string_tolower(s1);
|
||||
mcl_string_toupper(s2);
|
||||
assert(strcmp(mcl_string_cstr(s1), "") == 0);
|
||||
assert(strcmp(mcl_string_cstr(s2), "HELLO, WORLD!") == 0);
|
||||
|
||||
mcl_string_free(s1);
|
||||
mcl_string_free(s2);
|
||||
}
|
||||
26
test/test.c
Normal file
26
test/test.c
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Ignore this file
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
puts("==== [Running Hashmap tests] ====");
|
||||
test_hm1();
|
||||
puts("OK!");
|
||||
puts("");
|
||||
|
||||
puts("==== [Running Queue tests] ====");
|
||||
test_q1();
|
||||
puts("OK!");
|
||||
puts("");
|
||||
|
||||
puts("==== [Running String tests] ====");
|
||||
test_str1();
|
||||
test_str2();
|
||||
puts("OK!");
|
||||
puts("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user