aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/ds/array_tests.c12
-rw-r--r--tests/ds/hashmap_tests.c46
-rw-r--r--tests/safe_tests.c26
-rw-r--r--tests/string_tests.c20
4 files changed, 66 insertions, 38 deletions
diff --git a/tests/ds/array_tests.c b/tests/ds/array_tests.c
index b292407..c5d7c4d 100644
--- a/tests/ds/array_tests.c
+++ b/tests/ds/array_tests.c
@@ -4,7 +4,7 @@
#include <string.h>
-Test cap_test() {
+TEST(cap_test) {
float *arr = NULL;
test_assert(arr_cap(arr) == 0, "uninitialized array has capacity different than 0");
@@ -24,7 +24,7 @@ Test cap_test() {
arr_free(arr);
}
-Test len_test() {
+TEST(len_test) {
char *arr = NULL;
test_assert(arr_len(arr) == 0, "uninitialized array has length different than 0");
@@ -39,7 +39,7 @@ Test len_test() {
arr_free(arr);
}
-Test adding_items() {
+TEST(adding_items) {
int *arr = NULL;
// {3}
@@ -84,7 +84,7 @@ Test adding_items() {
arr_free(arr);
}
-Test getting_items() {
+TEST(getting_items) {
struct test {
int f1;
char f2;
@@ -121,7 +121,7 @@ Test getting_items() {
arr_free(arr);
}
-Test deleting_items() {
+TEST(deleting_items) {
int *arr = NULL;
int items[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
arr_append_n(arr, items, 9);
@@ -152,7 +152,7 @@ Test deleting_items() {
arr_free(arr);
}
-Test arr_concat_test() {
+TEST(arr_concat_test) {
unsigned long *arr1 = NULL;
unsigned long *arr2 = NULL;
diff --git a/tests/ds/hashmap_tests.c b/tests/ds/hashmap_tests.c
index 288bb4e..844ffb2 100644
--- a/tests/ds/hashmap_tests.c
+++ b/tests/ds/hashmap_tests.c
@@ -8,7 +8,9 @@
#include "moslib/ds/array.h"
#include "moslib/string.h"
-Test put_get() {
+#define INIT_CAP 16
+
+TEST(put_get) {
HM_DEF(E, float, int);
E *hm = NULL;
@@ -21,22 +23,21 @@ Test put_get() {
test_assert(hm_get(hm, 0.0f) == 2137, "hm_put didn't update value properly");
test_assert(hm_size(hm) == 2, "hm_size returned wrong entry count");
- // a lot of puts and gets to test expansion of hashmap
- for (int i = 2; i < 100; i++) {
+ for (int i = 2; i < 2 * INIT_CAP + 2; i++) {
hm_put(hm, (float)i, i);
}
int flag = 1;
- for (int i = 2; i < 100; i++) {
+ for (int i = 2; i < 2 * INIT_CAP + 2; i++) {
if (hm_get(hm, (float)i) != i)
flag = 0;
}
test_assert(flag, "hm_put / hm_get failed when expansion was needed");
- test_assert(hm_size(hm) == 100, "hm_size returned wrong entry count");
+ test_assert(hm_size(hm) == 2 * INIT_CAP + 2, "hm_size returned wrong entry count");
hm_free(hm);
}
-Test put_e_get_e() {
+TEST(put_e_get_e) {
HM_DEF(E, float, int);
E *hm = NULL;
@@ -54,7 +55,33 @@ Test put_e_get_e() {
hm_free(hm);
}
-Test str_key() {
+TEST(deleting) {
+ HM_DEF(E, long double, float);
+ E *hm = NULL;
+
+ hm_put(hm, 1.0, 1.0);
+ hm_put(hm, 2.0, 2.0);
+
+ int del = hm_del(hm, 1.0);
+ test_assert(hm_get_e(hm, 1.0) == NULL, "hm_del didn't delete entry it supposed to");
+ test_assert(del == 1, "hm_del didn't return 1 when it deleted");
+ test_assert(hm_size(hm) == 1, "hm_size after successful hm_del is incorrect");
+
+ del = hm_del(hm, 1.0);
+ test_assert(del == 0, "hm_del didn't return 0 when it didn't delete");
+ test_assert(hm_get(hm, 2.0) == 2.0, "hm_del deleted wrong entry");
+ test_assert(hm_size(hm) == 1, "hm_size after failed hm_del is incorrect");
+
+ for (int i = 3; i < 2 * INIT_CAP + 3; i++) {
+ hm_put(hm, (long double)i, (float)i);
+ }
+ test_assert(hm_get_e(hm, 1.0) == NULL, "expansion revived entry");
+ test_assert(hm_size(hm) == 2 * INIT_CAP + 1, "hm_size after expansion is incorrect");
+
+ hm_free(hm);
+}
+
+TEST(str_key) {
HM_DEF(E, char *, int);
E *hm = hm_new(.key = STR);
@@ -72,7 +99,7 @@ Test str_key() {
hm_free(hm);
}
-Test ss_key() {
+TEST(ss_key) {
HM_DEF(E, StringSlice, int);
E *hm = hm_new(.key = SS);
@@ -92,7 +119,7 @@ Test ss_key() {
hm_free(hm);
}
-Test looping() {
+TEST(looping) {
HM_DEF(E, int, int);
E *hm = NULL;
@@ -127,6 +154,7 @@ void ds_hashmap_tests(Tester *tester) {
TestGroup *group = add_test_group(tester, "moslib/ds/hashmap.h");
add_test(group, put_get);
add_test(group, put_e_get_e);
+ add_test(group, deleting);
add_test(group, str_key);
add_test(group, ss_key);
add_test(group, looping);
diff --git a/tests/safe_tests.c b/tests/safe_tests.c
index 3d46a37..b50b073 100644
--- a/tests/safe_tests.c
+++ b/tests/safe_tests.c
@@ -5,11 +5,11 @@
#include <stdlib.h>
#include <unistd.h>
-Test malloc_exit() {
+TEST_HELPER(malloc_exit) {
safe_malloc(ULLONG_MAX);
}
-Test test_safe_malloc() {
+TEST(test_safe_malloc) {
void *p = safe_malloc(1024);
test_assert(p != NULL, "Failed to malloc 1KiB");
@@ -18,11 +18,11 @@ Test test_safe_malloc() {
unsuppress_outputs();
}
-Test calloc_exit() {
+TEST_HELPER(calloc_exit) {
safe_calloc(ULLONG_MAX, ULLONG_MAX);
}
-Test test_safe_calloc() {
+TEST(test_safe_calloc) {
void *p = safe_calloc(16, 16);
test_assert(p != NULL, "Failed to calloc 16 items of 16 bytes");
@@ -31,12 +31,12 @@ Test test_safe_calloc() {
unsuppress_outputs();
}
-Test realloc_exit() {
+TEST_HELPER(realloc_exit) {
void *p = NULL;
safe_realloc(p, ULLONG_MAX);
}
-Test test_safe_realloc() {
+TEST(test_safe_realloc) {
void *p = NULL;
p = safe_realloc(p, 1024);
test_assert(p != NULL, "Failed to realloc NULL to 1KiB");
@@ -49,11 +49,11 @@ Test test_safe_realloc() {
unsuppress_outputs();
}
-Test fopen_exit() {
+TEST_HELPER(fopen_exit) {
safe_fopen("/non-existing-file-name", "r");
}
-Test test_safe_fopen() {
+TEST(test_safe_fopen) {
FILE *fp = safe_fopen(__FILE__, "r");
test_assert(fp != NULL, "Failed to fopen current file");
@@ -62,16 +62,16 @@ Test test_safe_fopen() {
unsuppress_outputs();
}
-Test test_safe_tmpfile() {
+TEST(test_safe_tmpfile) {
FILE *fp = safe_tmpfile();
test_assert(fp != NULL, "Failed to create a tmpfile");
}
-Test dup_exit() {
+TEST_HELPER(dup_exit) {
safe_dup(-1);
}
-Test test_safe_dup() {
+TEST(test_safe_dup) {
int fd = safe_dup(STDOUT_FILENO);
test_assert(fd != -1, "Failed to dup STDOUT_FILENO");
close(fd);
@@ -81,11 +81,11 @@ Test test_safe_dup() {
unsuppress_outputs();
}
-Test dup2_exit() {
+TEST_HELPER(dup2_exit) {
safe_dup2(-1, STDOUT_FILENO);
}
-Test test_safe_dup2() {
+TEST(test_safe_dup2) {
int res = dup2(STDOUT_FILENO, STDOUT_FILENO);
test_assert(res != -1, "Failed to dup STDOUT_FILENO 2 itself");
diff --git a/tests/string_tests.c b/tests/string_tests.c
index 4cb6372..0d0d7e2 100644
--- a/tests/string_tests.c
+++ b/tests/string_tests.c
@@ -9,7 +9,7 @@
#define HELLO_WORLD_LIT "Hello, World!"
-Test creating() {
+TEST(creating) {
char *str = HELLO_WORLD_LIT;
size_t str_len = strlen(str);
@@ -32,12 +32,12 @@ Test creating() {
test_assert(ss5.data == ss1.data && ss5.len == ss1.len, "ss_from_arr created wrong slice");
}
-Test ss_print_helper() {
+TEST(ss_print_helper) {
StringSlice ss = ss_from_str("test");
ss_print(ss);
}
-Test printing() {
+TEST(printing) {
test_assert_out(ss_print_helper, stdout, "test");
StringSlice ss = ss_from_str("slice");
@@ -46,7 +46,7 @@ Test printing() {
test_assert(strcmp(buf, "out: slice :)") == 0, "SS_FMT + SS_ARGS printed wrong output");
}
-Test comparing() {
+TEST(comparing) {
StringSlice ss = ss_from_str("hello world");
test_assert(ss_eq(ss, ss), "slice is not equal to itself");
test_assert(ss_eq_str(ss, ss.data), "slice is not equal to its C string");
@@ -66,7 +66,7 @@ Test comparing() {
test_assert(ss_eq_str(ss4, "world") == 0, "slice is equal to different string");
}
-Test trimming() {
+TEST(trimming) {
StringSlice ss = ss_from_str(" Hi :) ");
StringSlice t = ss_trim(ss);
test_assert(ss_eq_str(t, "Hi :)"), "trim didn't trim properly");
@@ -83,7 +83,7 @@ Test trimming() {
test_assert(ss_eq_str(t, ""), "trimming breaks for all whitespace slice");
}
-Test prefix_suffix() {
+TEST(prefix_suffix) {
StringSlice ss = ss_from_str("pre mid suf");
test_assert(ss_starts_with(ss, ss_from_str("pre")), "slice doesn't start with its slice prefix");
@@ -103,7 +103,7 @@ Test prefix_suffix() {
test_assert(ss_ends_with(ss2, ss_from_str("")), "ends_with breaks for empty slice and suffix");
}
-Test deleting() {
+TEST(deleting) {
StringSlice ss = ss_from_str("1234567890");
size_t deleted = ss_del_left(&ss, 2);
@@ -147,7 +147,7 @@ Test deleting() {
test_assert(!flag && ss.len == 7, "del_suffix_str didn't work properly for not found suffix");
}
-Test splitting() {
+TEST(splitting) {
StringSlice ss1 = ss_from_str("Hello, World!");
StringSlice ss2 = ss_split(&ss1, ',');
@@ -191,7 +191,7 @@ Test splitting() {
test_assert(ss_eq_str(ss_right, "") && ss_eq_str(ss_middle, ""), "ss_split_right didn't split properly (n > len)");
}
-Test finding() {
+TEST(finding) {
char *txt = "a slice of FINDME text";
StringSlice ss = ss_from_str(txt);
@@ -205,7 +205,7 @@ Test finding() {
test_assert(ss_eq_str(ss_find_str(ss, "abcd"), ""), "find_str didn't return empty slice for not found target");
}
-Test utf8_len_test() {
+TEST(utf8_len_test) {
StringSlice ss = ss_from_str("1 byte chars");
test_assert(ss_utf8_len(ss) == ss.len, "utf8_len differs from len for all 1 byte characters");