aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/binary_search.c2
-rw-r--r--tests/dynarray_struct.c2
-rw-r--r--tests/hmap.c105
-rw-r--r--tests/linked_list.c11
-rw-r--r--tests/obscure.c2
-rw-r--r--tests/types.c3
6 files changed, 116 insertions, 9 deletions
diff --git a/tests/binary_search.c b/tests/binary_search.c
index e3a973b..ce35d14 100644
--- a/tests/binary_search.c
+++ b/tests/binary_search.c
@@ -4,7 +4,7 @@
#define prints(el) \
do {printf("search %i: %s\n", el, (array_binary_search(array, &el, __default_int_cmp)) ? "true" : "false");} while(0)
-int main() {
+int main(void) {
int data[] = {9, 5, 2, 7, 6, 10, 5, 9, 7}; // no 8 there
int* array = array_new(int, sizeof(data)/sizeof(data[0]));
memcpy(array, data, sizeof(data));
diff --git a/tests/dynarray_struct.c b/tests/dynarray_struct.c
index 6cce97c..7675ab4 100644
--- a/tests/dynarray_struct.c
+++ b/tests/dynarray_struct.c
@@ -27,7 +27,7 @@ int point_abs(const void* a, const void* b) {
return absa-absb;
}
-int main()
+int main(void)
{
struct point *points = array_new(struct point, 4);
for (size_t i = 0; i < array_size(points); ++i) points[i] = (struct point){.x = i, .y = 2 * i, .z = 4 * i, .w = 8 * i};
diff --git a/tests/hmap.c b/tests/hmap.c
new file mode 100644
index 0000000..920c854
--- /dev/null
+++ b/tests/hmap.c
@@ -0,0 +1,105 @@
+#define CONTAINER_IMPLEMENTATION
+#define HMAP_MAX_BUCKET_SIZE 1
+#define HMAP_INIT_SIZE 1
+#include "../include/container.h"
+
+
+// Necessary for hash map to operate
+int int_eqals(const struct hmap_pair* a, const struct hmap_pair* b) {
+ return *(int*)(a->key) - *(int*)(b->key);
+}
+
+// Just a regular hash algorithm
+size_t int_hash(const int* a) {
+ return *a ^ (*a << 1);
+}
+
+// A macro for fun
+#define free_pair(pair) do { free(pair.key); free(pair.val); } while(0)
+
+// Here is an example of how you might iterate elements in hash map
+void print_hmap(struct hash_map* map) {
+ printf("hash map at %p\n", (void*)map);
+ struct hash_map_iter iter;
+ for (hmapi_begin(map, &iter); !hmapi_end(&iter); hmapi_next(&iter)) {
+ printf("key: %i, value: %i\n", *(int*)hmapi_get_key(&iter), *(int*)hmapi_get_val(&iter));
+ }
+}
+struct hmap_pair mkpr(int key, int val) {
+ struct hmap_pair ret = {malloc(sizeof(int)), malloc(sizeof(int))};
+ *(int*)ret.key = key;
+ *(int*)ret.val = val;
+ return ret;
+}
+void freepr(struct hmap_pair* pr) {
+ free(pr->key);
+ free(pr->val);
+}
+
+int main(void) {
+ // Casting functions to the right type should not break it
+ struct hmap_pair pairs[] = {mkpr(1, 2), mkpr(2, 3), mkpr(3, 4), mkpr(3, 5), mkpr(3, 6), mkpr(6, 8)};
+ struct hash_map map = hmap_create_from_buffer(
+ sizeof(int), sizeof(int),
+ (hmap_equal_fn)int_eqals, (hmap_hash_fn)int_hash,
+ pairs, sizeof(pairs)/sizeof(*pairs));
+
+ struct hmap_pair w1 = {malloc(sizeof(int)), malloc(sizeof(int))};
+ *(int*)(w1.key) = 10;
+ *(int*)(w1.val) = 15;
+ struct hmap_pair w2 = {malloc(sizeof(int)), malloc(sizeof(int))};
+ *(int*)(w2.key) = 10;
+ *(int*)(w2.val) = 15;
+ struct hmap_pair w3 = {malloc(sizeof(int)), malloc(sizeof(int))};
+ *(int*)(w3.key) = -69;
+ *(int*)(w3.val) = 12;
+
+ // Ensure there are no memory leaks
+ hmap_insert_mallocated(&map, w1);
+ hmap_insert_mallocated(&map, w2);
+ hmap_insert_mallocated(&map, w3);
+
+ int key;
+ int val;
+ struct hmap_pair p1 = {.key = &key, .val = &val};
+ key = 274;
+ val = 349;
+ hmap_insert_copy(&map, p1);
+ key = 1234;
+ val = 5678;
+ hmap_insert_copy(&map, p1);
+ key = 42;
+ val = 727;
+ hmap_insert_copy(&map, p1);
+ hmap_insert_copy(&map, p1); // Second insert does not do anything
+
+ int zero = 0;
+ hmap_update(&map, &key, &zero);
+
+ int ten = 10;
+ printf("hmap at %i: %i\n", ten, *(int*)hmap_get(&map, &ten));
+ hmap_remove(&map, &ten);
+
+ struct hmap_pair pairs_2[] = {mkpr(100, 200), mkpr(200, 600), mkpr(600, 300), mkpr(300, 420)};
+ hmap_insert_range(&map, pairs_2, sizeof(pairs_2)/sizeof(*pairs_2));
+
+ hmap_iter iter;
+ void** rmkeys = array_new(void*, 0);
+ for (hmapi_begin(&map, &iter); !hmapi_end(&iter); hmapi_next(&iter)) {
+ if (*(int*)hmapi_get_key(&iter) % 100 != 0)
+ array_push(rmkeys, hmapi_get_key(&iter));
+ }
+ for (size_t i = 0; i < array_size(rmkeys); ++i)
+ hmap_remove(&map, rmkeys[i]);
+ array_free(rmkeys);
+
+ print_hmap(&map);
+ hmap_free(&map);
+ for (size_t i = 0; i < sizeof(pairs)/sizeof(*pairs); ++i) {
+ freepr(pairs + i);
+ }
+ for (size_t i = 0; i < sizeof(pairs_2)/sizeof(*pairs_2); ++i) {
+ freepr(pairs_2 + i);
+ }
+ return 0;
+}
diff --git a/tests/linked_list.c b/tests/linked_list.c
index 1682375..7068e83 100644
--- a/tests/linked_list.c
+++ b/tests/linked_list.c
@@ -4,15 +4,15 @@
void print_linked_list(struct linked_list* list) {
int i = 0;
- printf("linked list at %p, size = %zu\n", list, list->meta.assumed_size);
- printf("first=%p, last=%p\n", list->first, list->last);
+ printf("linked list at %p, size = %zu\n", (void*)list, list->meta.assumed_size);
+ printf("first=%p, last=%p\n", (void*)list->first, (void*)list->last);
for (struct linked_list_node* current = list->first; current != NULL; current = current->next) {
- printf("[%i] = {%i, %p}\n", i, *(int*)current->data, current->next);
+ printf("[%i] = {%i, %p}\n", i, *(int*)current->data, (void*)current->next);
++i;
}
}
-int main() {
+int main(void) {
int buf[] = {0, 1, 3, 2, 7, 8, 6, 0, 18, 1};
struct linked_list list = ll_create_from_buffer(sizeof(int), buf, sizeof(buf)/sizeof(*buf));
@@ -24,7 +24,8 @@ int main() {
ll_insert_front(&list, &one);
ll_append(&list, &one);
ll_append(&list, &one);
- ll_remove_all(&list, &one, __default_int_cmp);
+ // TODO: fix this function
+ ll_remove_all(&list, &one, __default_int_cmp);
print_linked_list(&list);
ll_remove_at(&list, 3);
ll_remove_back(&list);
diff --git a/tests/obscure.c b/tests/obscure.c
index 190ff09..7b6f1fd 100644
--- a/tests/obscure.c
+++ b/tests/obscure.c
@@ -5,7 +5,7 @@
typedef char* packet_t;
-int main() {
+int main(void) {
packet_t *data = array_new(packet_t, 0);
packet_t el_0 = malloc(128);
memset(el_0, 42, 128);
diff --git a/tests/types.c b/tests/types.c
index 4320e8d..94d6cbe 100644
--- a/tests/types.c
+++ b/tests/types.c
@@ -1,7 +1,8 @@
#include <stdio.h>
+#define RS_TYPES_USE_128
#include "../include/rstypes.h"
-i32 main() {
+i32 main(void) {
u8 a = 215;
i8 b = -127;
u16 c = 65535;