aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/moslib/ds/hashmap.h31
-rw-r--r--include/moslib/tester.h21
2 files changed, 38 insertions, 14 deletions
diff --git a/include/moslib/ds/hashmap.h b/include/moslib/ds/hashmap.h
index 3c50287..4136ac2 100644
--- a/include/moslib/ds/hashmap.h
+++ b/include/moslib/ds/hashmap.h
@@ -83,6 +83,14 @@
//
// ----------------------------------------------------------------------------------------------------
//
+// To delete an an entry from a hashmap use:
+//
+// int deleted = hm_del(hm, key);
+//
+// It will return 1 if entry of such key existed and was deleted, 0 otherwise.
+//
+// ----------------------------------------------------------------------------------------------------
+//
// You can loop over the hashmap using these functions:
//
// struct Entry *first = hm_first(hm);
@@ -130,6 +138,7 @@
#define hm_put_e mos_hm_put_e
#define hm_get mos_hm_get
#define hm_get_e mos_hm_get_e
+#define hm_del mos_hm_del
#define hm_first mos_hm_first
#define hm_next mos_hm_next
#define hm_size mos_hm_size
@@ -221,6 +230,19 @@ typedef struct {
// pointer to the entry or NULL if no entry with such key exists
#define mos_hm_get_e(hm, k) (mos_hm_ini, (hm)->key = k, mos_hm_get_e_fn(hm, mos_hm_fn_args))
+// Delete an entry with specific key from the hashmap
+//
+// Arguments:
+// hm
+// hashmap to delete from
+//
+// k
+// key of the entry to delete
+//
+// Returns:
+// 1 if an entry of given key was deleted, 0 otherwise
+#define mos_hm_del(hm, k) (mos_hm_ini, (hm)->key = k, mos_hm_del_fn(hm, mos_hm_fn_args))
+
// Get a pointer to the first entry in the hashmap
//
// Arguments:
@@ -250,7 +272,7 @@ typedef struct {
// Arguments:
// hm
// hashmap to get the count of entries from
-extern size_t mos_hm_size(void *hm);
+extern size_t mos_hm_size(const void *hm);
// Free the hashmap
//
@@ -268,8 +290,9 @@ extern void mos_hm_init(void *p_hm, size_t entry_size);
extern void *mos_hm_new_fn(MosHmInitArgs args);
extern void mos_hm_put_fn(void *p_hm, void *key, size_t entry_size, size_t key_size, uint8_t load_factor);
extern size_t mos_hm_get_fn(void *hm, void *key, size_t entry_size, size_t key_size);
-extern void *mos_hm_get_e_fn(void *hm, void *key, size_t entry_size, size_t key_size);
-extern void *mos_hm_first_fn(void *hm, size_t entry_size);
-extern void *mos_hm_next_fn(void *hm, void *curr, size_t entry_size);
+extern void *mos_hm_get_e_fn(const void *hm, const void *key, size_t entry_size, size_t key_size);
+extern int mos_hm_del_fn(const void *hm, const void *key, size_t entry_size, size_t key_size);
+extern void *mos_hm_first_fn(const void *hm, size_t entry_size);
+extern void *mos_hm_next_fn(const void *hm, const void *curr, size_t entry_size);
#endif // MOSLIB_DS_HASHMAP_H
diff --git a/include/moslib/tester.h b/include/moslib/tester.h
index a522b9d..f8cc8b1 100644
--- a/include/moslib/tester.h
+++ b/include/moslib/tester.h
@@ -23,10 +23,9 @@
// ----------------------------------------------------------------------------------------------------
//
// Create tests.
-// Tests are of type TestFn (Test (*)()).
// They should use test_assert functions to check correctness.
// Example:
-// Test test_fn() {
+// TEST(test_fn) {
// int result1 = 1 + 2;
// int result2 = 3 + 5;
//
@@ -38,11 +37,11 @@
// You need to create a helper test and pass it into test_assert_exit in main test.
// Pass the helper test and expected exit code to it.
// Example:
-// Test helper_test() {
+// TEST_HELPER(helper_test) {
// int param1, param2;
// int result = exit_failure_function(&param1, &param2);
// }
-// Test main_test() {
+// TEST(main_test) {
// test_assert_exit(helper_test, EXIT_FAILURE);
// }
//
@@ -50,17 +49,17 @@
// You need to create a helper test and pass it into test_assert_out in main test.
// Pass the helper test, output stream, and expected output to it.
// Example:
-// Test helper_test() {
+// TEST_HELPER(helper_test) {
// printf("output");
// }
-// Test main_test() {
+// TEST(main_test) {
// test_assert_out(helper_test, stdout, "output");
// }
//
// If your functions have output and you want to suppress them for the sake of tests,
// you can do so using suppress_output and unsupress_outputs.
// Example:
-// Test error_test() {
+// TEST(error_test) {
// suppress_output(stdout);
// suppress_output(stderr);
// int code = fun_that_fails_with_output();
@@ -96,7 +95,8 @@
#ifndef MOS_FORCE_PREFIXES
-#define Test MosTest
+#define TEST MOS_TEST
+#define TEST_HELPER MOS_TEST_HELPER
#define Tester MosTester
#define TestGroup MosTestGroup
#define TestFn MosTestFn
@@ -113,10 +113,11 @@
#endif // MOS_FORCE_PREFIXES
-typedef void MosTest; // this is just for nice indication that a function is a test
+#define MOS_TEST(name) static void name(void)
+#define MOS_TEST_HELPER MOS_TEST
typedef struct MosTester MosTester;
typedef struct MosTestGroup MosTestGroup;
-typedef MosTest (*MosTestFn)(void);
+typedef void (*MosTestFn)(void);
// Initalize new tester
// You can skip this and just define it as NULL - it will initalize automatically