From 3c3f1115f6e9a9b914e2dcbd796501ca7ce85342 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 19 Feb 2020 02:51:44 +0100 Subject: Tested ht* --- README.md | 11 +- include/libft_ht.h | 5 +- src/ht/ft_htdelone.c | 6 +- src/ht/ft_htdelone_key.c | 19 ---- src/ht/ft_htdestroy_all.c | 26 ----- src/ht/ft_htdestroy_key.c | 26 ----- src/ht/ft_htget.c | 9 +- src/ht/ft_htset.c | 16 +-- src/ht/ft_inter_htkey_cmp.c | 2 +- src/lst/ft_lstlfind.c | 2 +- src/lst/ft_lstremove_if.c | 4 +- test/include/libft_test.h | 7 ++ test/src/ht/test_ft_htdelone.c | 68 ++++++++++++ test/src/ht/test_ft_htdelone_key.c | 0 test/src/ht/test_ft_htdestroy.c | 38 +++++++ test/src/ht/test_ft_htdestroy_all.c | 0 test/src/ht/test_ft_htdestroy_key.c | 0 test/src/ht/test_ft_htget.c | 81 ++++++++++++++- test/src/ht/test_ft_htset.c | 23 ++-- test/src/lst/test_ft_lstdelone.c | 12 +++ test/src/main.c | 2 + test/src/runner/test_runner_ht.c | 13 ++- vendor/_unity/src/unity_memory.c | 202 ------------------------------------ vendor/_unity/unity_memory.c | 202 ++++++++++++++++++++++++++++++++++++ 24 files changed, 463 insertions(+), 311 deletions(-) delete mode 100644 src/ht/ft_htdelone_key.c delete mode 100644 src/ht/ft_htdestroy_all.c delete mode 100644 src/ht/ft_htdestroy_key.c delete mode 100644 test/src/ht/test_ft_htdelone_key.c delete mode 100644 test/src/ht/test_ft_htdestroy_all.c delete mode 100644 test/src/ht/test_ft_htdestroy_key.c delete mode 100644 vendor/_unity/src/unity_memory.c create mode 100644 vendor/_unity/unity_memory.c diff --git a/README.md b/README.md index aff6c03..6984a24 100644 --- a/README.md +++ b/README.md @@ -85,14 +85,11 @@ Much like the `.gitignore` file, you can put the files/directory to ignore when | Name | Prototype | Description | Tested | |------------------|-------------------------------------------------------------------------|-----------------------------------------------------------|--------| | ft_htcontent_new | `t_ftht_content *ft_htcontent_new(char *key, void *value)` | create a new key/value pair | [x] | -| ft_htdelone | `void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_content*))` | delete element at `key` | [ ] | -| ft_htdelone_key | `void ft_htdelone_key(t_ftht *ht, char *key)` | delete element at `key` and free `key` | [ ] | -| ft_htdestroy | `void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_content*))` | destroy `ht` with the `del` functions applied on values | [ ] | -| ft_htdestroy_all | `void ft_htdestroy_all(t_ftht *ht)` | destroy `ht`, free keys and values | [ ] | -| ft_htdestroy_key | `void ft_htdestroy_key(t_ftht *ht)` | destroy `ht`, free keys | [ ] | -| ft_htget | `void *ft_htget(t_ftht *ht, char *key)` | return value at `key` | [ ] | +| ft_htdelone | `void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_content*))` | delete element at `key` | [x] | +| ft_htdestroy | `void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_content*))` | destroy `ht` with the `del` functions applied on values | [x] | +| ft_htget | `void *ft_htget(t_ftht *ht, char *key)` | return value at `key` | [x] | | ft_htnew | `t_ftht *ft_htnew(t_ftsize size)` | create a new hash table with a underlying array of `size` | [x] | -| ft_htset | `t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value)` | set `key` to value, if element doesn't exist, create it | [ ] | +| ft_htset | `t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value)` | set `key` to value, if element doesn't exist, create it | [x] | ### io diff --git a/include/libft_ht.h b/include/libft_ht.h index 3c5ef6f..62f2ee1 100644 --- a/include/libft_ht.h +++ b/include/libft_ht.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:36:09 by cacharle #+# #+# */ -/* Updated: 2020/02/17 04:54:05 by cacharle ### ########.fr */ +/* Updated: 2020/02/19 02:38:28 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,8 @@ void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_content*)); void ft_htdestroy_all(t_ftht *ht); void ft_htdestroy_key(t_ftht *ht); void *ft_htget(t_ftht *ht, char *key); -t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value, void (*del)(void*)); +t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value, + void (*del)(t_ftht_content*)); void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_content*)); void ft_htdelone_key(t_ftht *ht, char *key); diff --git a/src/ht/ft_htdelone.c b/src/ht/ft_htdelone.c index 00a0b11..2c54721 100644 --- a/src/ht/ft_htdelone.c +++ b/src/ht/ft_htdelone.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:27:18 by cacharle #+# #+# */ -/* Updated: 2020/02/17 03:06:58 by cacharle ### ########.fr */ +/* Updated: 2020/02/19 02:35:06 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,6 @@ void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_content*)) { ft_lstremove_if(ht->entries + ft_hthash(ht, key), - ft_inter_htkey_cmp, key, - (void (*)(void*))del); + ft_inter_htkey_cmp, key, + (void (*)(void*))del); } diff --git a/src/ht/ft_htdelone_key.c b/src/ht/ft_htdelone_key.c deleted file mode 100644 index 96d55ec..0000000 --- a/src/ht/ft_htdelone_key.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_htdelone_key.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/01/30 09:45:11 by cacharle #+# #+# */ -/* Updated: 2020/01/30 09:46:42 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include "libft_ht.h" - -void ft_htdelone_key(t_ftht *ht, char *key) -{ - ft_htdelone(ht, key, ft_inter_htdelcontent_key); -} diff --git a/src/ht/ft_htdestroy_all.c b/src/ht/ft_htdestroy_all.c deleted file mode 100644 index 6f98a43..0000000 --- a/src/ht/ft_htdestroy_all.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_htdestroy_all.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/01/30 08:29:58 by cacharle #+# #+# */ -/* Updated: 2020/01/31 10:43:13 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft_ht.h" - -static void st_htdelcontent_all(t_ftht_content *content) -{ - if (content == NULL) - return ; - free(content->key); - free(content->value); -} - -void ft_htdestroy_all(t_ftht *ht) -{ - ft_htdestroy(ht, *st_htdelcontent_all); -} diff --git a/src/ht/ft_htdestroy_key.c b/src/ht/ft_htdestroy_key.c deleted file mode 100644 index a704314..0000000 --- a/src/ht/ft_htdestroy_key.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_htdestroy_key.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/01/30 08:31:02 by cacharle #+# #+# */ -/* Updated: 2020/01/31 10:43:45 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include "libft_ht.h" - -void ft_inter_htdelcontent_key(t_ftht_content *content) -{ - if (content == NULL) - return ; - free(content->key); -} - -void ft_htdestroy_key(t_ftht *ht) -{ - ft_htdestroy(ht, *ft_inter_htdelcontent_key); -} diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c index c5e7234..76e4fb2 100644 --- a/src/ht/ft_htget.c +++ b/src/ht/ft_htget.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:33:21 by cacharle #+# #+# */ -/* Updated: 2020/02/17 03:06:07 by cacharle ### ########.fr */ +/* Updated: 2020/02/19 01:44:41 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,10 +17,13 @@ void *ft_htget(t_ftht *ht, char *key) { t_ftht_digest digest; + t_ftlst *found; if (ht == NULL || key == NULL) return (NULL); digest = ft_hthash(ht, key); - return (((t_ftht_content*)ft_lstlfind(ht->entries[digest], - ft_inter_htkey_cmp, key)->content)->value); + found = ft_lstlfind(ht->entries[digest], ft_inter_htkey_cmp, key); + if (found == NULL) + return (NULL); + return (((t_ftht_content*)found->content)->value); } diff --git a/src/ht/ft_htset.c b/src/ht/ft_htset.c index 4157134..c7068d5 100644 --- a/src/ht/ft_htset.c +++ b/src/ht/ft_htset.c @@ -6,14 +6,15 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:41:52 by cacharle #+# #+# */ -/* Updated: 2020/02/17 05:02:15 by cacharle ### ########.fr */ +/* Updated: 2020/02/19 02:44:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include "libft_ht.h" -t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value, void (*del)(void*)) +t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value, + void (*del)(t_ftht_content*)) { t_ftht_digest digest; t_ftht_content *content; @@ -22,17 +23,18 @@ t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value, void (*del)(void*)) if (ht == NULL || key == NULL) return (NULL); + if ((content = ft_htcontent_new(key, value)) == NULL) + return (NULL); digest = ft_hthash(ht, key); - tmp = ft_lstlfind(ht->entries[digest], ft_inter_htkey_cmp, entry); + tmp = ft_lstlfind(ht->entries[digest], ft_inter_htkey_cmp, key); if (tmp != NULL) { if (del != NULL) - del(((t_ftht_content*)tmp->content)->value); - ((t_ftht_content*)tmp->content)->value = value; + (*del)(tmp->content); + tmp->content = content; return ((t_ftht_content*)tmp->content); } - if ((content = ft_htcontent_new(key, value)) == NULL) - return (NULL); + if ((entry = ft_lstnew(content)) == NULL) { free(content); diff --git a/src/ht/ft_inter_htkey_cmp.c b/src/ht/ft_inter_htkey_cmp.c index c91663d..6f04ecc 100644 --- a/src/ht/ft_inter_htkey_cmp.c +++ b/src/ht/ft_inter_htkey_cmp.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:24:39 by cacharle #+# #+# */ -/* Updated: 2020/02/16 02:25:43 by cacharle ### ########.fr */ +/* Updated: 2020/02/19 02:03:14 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/lst/ft_lstlfind.c b/src/lst/ft_lstlfind.c index ddb890b..92d37d8 100644 --- a/src/lst/ft_lstlfind.c +++ b/src/lst/ft_lstlfind.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/17 03:04:52 by cacharle #+# #+# */ -/* Updated: 2020/02/17 03:34:19 by cacharle ### ########.fr */ +/* Updated: 2020/02/19 02:03:11 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/lst/ft_lstremove_if.c b/src/lst/ft_lstremove_if.c index 488539e..5221ae4 100644 --- a/src/lst/ft_lstremove_if.c +++ b/src/lst/ft_lstremove_if.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:36:49 by cacharle #+# #+# */ -/* Updated: 2020/02/16 03:57:07 by cacharle ### ########.fr */ +/* Updated: 2020/02/19 02:06:22 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ void ft_lstremove_if(t_ftlst **lst, t_ftcompar_func cmp, if (lst == NULL || *lst == NULL) return ; - if (cmp((*lst)->content, ref) == 0) + if (cmp(ref, (*lst)->content) == 0) { saved_next = (*lst)->next; ft_lstdelone(*lst, del); diff --git a/test/include/libft_test.h b/test/include/libft_test.h index 85aa7d9..23c39c3 100644 --- a/test/include/libft_test.h +++ b/test/include/libft_test.h @@ -10,10 +10,17 @@ # include "unity.h" # include "unity_fixture.h" + +# undef free +# undef malloc + +# include + # include "libft.h" # include "libft_algo.h" # include "libft_ht.h" # include "helper/helper_segfault.h" + #endif diff --git a/test/src/ht/test_ft_htdelone.c b/test/src/ht/test_ft_htdelone.c index e69de29..3bc0922 100644 --- a/test/src/ht/test_ft_htdelone.c +++ b/test/src/ht/test_ft_htdelone.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_ft_htdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/19 02:08:00 by cacharle #+# #+# */ +/* Updated: 2020/02/19 02:46:10 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_test.h" + +TEST_GROUP(ft_htdelone); + +TEST_SETUP(ft_htdelone) +{} + +TEST_TEAR_DOWN(ft_htdelone) +{} + +static void st_del(t_ftht_content *v) +{ + free(v->key); + free(v); +} + +TEST(ft_htdelone, basic) +{ + t_ftht *ht = ft_htnew(3); + + ft_htset(ht, "bonjour", "je", st_del); + ft_htset(ht, "a", "yay", st_del); + ft_htset(ht, "b", "aasdf", st_del); + ft_htset(ht, "c", "a", st_del); + ft_htset(ht, "d", "dd", st_del); + + ft_htdelone(ht, "bonjour", st_del); + void *ptr = ft_htget(ht, "bonjour"); + TEST_ASSERT_NULL(ptr); + TEST_ASSERT_NOT_NULL(ft_htget(ht, "a")); + TEST_ASSERT_NOT_NULL(ft_htget(ht, "b")); + TEST_ASSERT_NOT_NULL(ft_htget(ht, "c")); + TEST_ASSERT_NOT_NULL(ft_htget(ht, "d")); + + ft_htdelone(ht, "a", st_del); + ptr = ft_htget(ht, "a"); + TEST_ASSERT_NULL(ptr); + TEST_ASSERT_NOT_NULL(ft_htget(ht, "b")); + TEST_ASSERT_NOT_NULL(ft_htget(ht, "c")); + TEST_ASSERT_NOT_NULL(ft_htget(ht, "d")); + + ft_htdelone(ht, "b", st_del); + ptr = ft_htget(ht, "b"); + TEST_ASSERT_NULL(ptr); + TEST_ASSERT_NOT_NULL(ft_htget(ht, "c")); + TEST_ASSERT_NOT_NULL(ft_htget(ht, "d")); + + ft_htdelone(ht, "c", st_del); + ptr = ft_htget(ht, "c"); + TEST_ASSERT_NULL(ptr); + TEST_ASSERT_NOT_NULL(ft_htget(ht, "d")); + + ft_htdelone(ht, "d", st_del); + ptr = ft_htget(ht, "d"); + TEST_ASSERT_NULL(ptr); +} diff --git a/test/src/ht/test_ft_htdelone_key.c b/test/src/ht/test_ft_htdelone_key.c deleted file mode 100644 index e69de29..0000000 diff --git a/test/src/ht/test_ft_htdestroy.c b/test/src/ht/test_ft_htdestroy.c index e69de29..360a7a0 100644 --- a/test/src/ht/test_ft_htdestroy.c +++ b/test/src/ht/test_ft_htdestroy.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_ft_htdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/19 02:45:27 by cacharle #+# #+# */ +/* Updated: 2020/02/19 02:49:36 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_test.h" + +TEST_GROUP(ft_htdestroy); + +TEST_SETUP(ft_htdestroy) +{} + +TEST_TEAR_DOWN(ft_htdestroy) +{} + +static void st_del(t_ftht_content *v) +{ + free(v->key); + free(v); +} + +TEST(ft_htdestroy, basic) +{ + t_ftht *ht = ft_htnew(2); + + ft_htset(ht, "a", "1", st_del); + ft_htset(ht, "b", "2", st_del); + ft_htset(ht, "c", "3", st_del); + ft_htset(ht, "d", "4", st_del); + ft_htdestroy(ht, st_del); +} diff --git a/test/src/ht/test_ft_htdestroy_all.c b/test/src/ht/test_ft_htdestroy_all.c deleted file mode 100644 index e69de29..0000000 diff --git a/test/src/ht/test_ft_htdestroy_key.c b/test/src/ht/test_ft_htdestroy_key.c deleted file mode 100644 index e69de29..0000000 diff --git a/test/src/ht/test_ft_htget.c b/test/src/ht/test_ft_htget.c index 591d492..5db06b7 100644 --- a/test/src/ht/test_ft_htget.c +++ b/test/src/ht/test_ft_htget.c @@ -16,9 +16,14 @@ TEST_TEAR_DOWN(ft_htget) int helper_segfault_pid; +static void st_del(t_ftht_content *c) +{ + free(c->key); + free(c); +} + TEST(ft_htget, segfault) { - TEST_IGNORE(); TEST_ASSERT_SEGFAULT(ft_htget((t_ftht*)NULL, "")); TEST_ASSERT_SEGFAULT(ft_htget(ft_htnew(1), (char*)NULL)); TEST_ASSERT_SEGFAULT(ft_htget(ft_htnew(1), "")); @@ -27,11 +32,83 @@ TEST(ft_htget, segfault) TEST(ft_htget, error_null) { - TEST_IGNORE(); TEST_ASSERT_NULL(ft_htget(NULL, NULL)); TEST_ASSERT_NULL(ft_htget(ht, NULL)); TEST_ASSERT_NULL(ft_htget(NULL, "")); TEST_ASSERT_NULL(ft_htget(ht, "")); TEST_ASSERT_NULL(ft_htget(ht, "hi")); TEST_ASSERT_NULL(ft_htget(ht, "asdfkasdflk")); + TEST_ASSERT_NULL(ft_htget(ht, "asdhfalsdhflahsdfhjasklehjfklwhjekrlanklshasdfkasdflk")); +} + +TEST(ft_htget, basic) +{ + ft_htset(ht, strdup("a"), strdup("data1"), st_del); + ft_htset(ht, strdup("b"), strdup("data2"), st_del); + ft_htset(ht, strdup("c"), strdup("data3"), st_del); + + char *s = ft_htget(ht, "a"); + TEST_ASSERT_NOT_NULL(s); + TEST_ASSERT_EQUAL_STRING(s, "data1"); + + s = ft_htget(ht, "b"); + TEST_ASSERT_NOT_NULL(s); + TEST_ASSERT_EQUAL_STRING(s, "data2"); + + s = ft_htget(ht, "c"); + TEST_ASSERT_NOT_NULL(s); + TEST_ASSERT_EQUAL_STRING(s, "data3"); + + ft_htset(ht, strdup("a"), strdup("bonjour1"), st_del); + ft_htset(ht, strdup("b"), strdup("bonjour2"), st_del); + ft_htset(ht, strdup("c"), strdup("bonjour3"), st_del); + + s = ft_htget(ht, "a"); + TEST_ASSERT_NOT_NULL(s); + TEST_ASSERT_EQUAL_STRING(s, "bonjour1"); + + s = ft_htget(ht, "b"); + TEST_ASSERT_NOT_NULL(s); + TEST_ASSERT_EQUAL_STRING(s, "bonjour2"); + + s = ft_htget(ht, "c"); + TEST_ASSERT_NOT_NULL(s); + TEST_ASSERT_EQUAL_STRING(s, "bonjour3"); +} + +TEST(ft_htget, collision) +{ + t_ftht *small = ft_htnew(1); + + ft_htset(small, strdup("a"), strdup("data1"), st_del); + ft_htset(small, strdup("b"), strdup("data2"), st_del); + ft_htset(small, strdup("c"), strdup("data3"), st_del); + + char *s = ft_htget(small, "a"); + TEST_ASSERT_NOT_NULL(s); + TEST_ASSERT_EQUAL_STRING(s, "data1"); + + s = ft_htget(small, "b"); + TEST_ASSERT_NOT_NULL(s); + TEST_ASSERT_EQUAL_STRING(s, "data2"); + + s = ft_htget(small, "c"); + TEST_ASSERT_NOT_NULL(s); + TEST_ASSERT_EQUAL_STRING(s, "data3"); + + ft_htset(small, strdup("a"), strdup("bonjour1"), st_del); + ft_htset(small, strdup("b"), strdup("bonjour2"), st_del); + ft_htset(small, strdup("c"), strdup("bonjour3"), st_del); + + s = ft_htget(small, "a"); + TEST_ASSERT_NOT_NULL(s); + TEST_ASSERT_EQUAL_STRING(s, "bonjour1"); + + s = ft_htget(small, "b"); + TEST_ASSERT_NOT_NULL(s); + TEST_ASSERT_EQUAL_STRING(s, "bonjour2"); + + s = ft_htget(small, "c"); + TEST_ASSERT_NOT_NULL(s); + TEST_ASSERT_EQUAL_STRING(s, "bonjour3"); } diff --git a/test/src/ht/test_ft_htset.c b/test/src/ht/test_ft_htset.c index c4b53b9..f161206 100644 --- a/test/src/ht/test_ft_htset.c +++ b/test/src/ht/test_ft_htset.c @@ -11,7 +11,13 @@ TEST_SETUP(ft_htset) TEST_TEAR_DOWN(ft_htset) { - ft_htdestroy_key(ht); + /* ft_htdestroy_key(ht); */ +} + +static void st_del(t_ftht_content *c) +{ + free(c->key); + free(c); } TEST(ft_htset, segfault) @@ -51,20 +57,19 @@ TEST(ft_htset, reset) small = ft_htnew(3); - t_ftht_content *content = ft_htset(ht, "bonjour", strdup("content"), free); + t_ftht_content *content = ft_htset(small, "bonjour", ft_strdup("content"), st_del); TEST_ASSERT_NOT_NULL(content); TEST_ASSERT_NOT_NULL(content->key); TEST_ASSERT_NOT_NULL(content->value); TEST_ASSERT_EQUAL_STRING(content->key, "bonjour"); TEST_ASSERT_EQUAL_STRING(content->value, "content"); - t_ftht_content *content_re = ft_htset(ht, "bonjour", strdup("yo"), free); + t_ftht_content *content_re = ft_htset(small, "bonjour", ft_strdup("yo"), st_del); TEST_ASSERT_NOT_NULL(content_re); - TEST_ASSERT_EQUAL_PTR(content, content_re); TEST_ASSERT_NOT_NULL(content_re->key); TEST_ASSERT_NOT_NULL(content_re->value); - TEST_ASSERT_EQUAL_STRING(content_re->key, "bonjour"); - TEST_ASSERT_EQUAL_STRING(content_re->value, "yo"); + TEST_ASSERT_EQUAL_STRING("bonjour", content_re->key); + TEST_ASSERT_EQUAL_STRING("yo", content_re->value); } TEST(ft_htset, collision) @@ -72,21 +77,21 @@ TEST(ft_htset, collision) t_ftht *small = NULL; small = ft_htnew(1); - t_ftht_content *content1 = ft_htset(ht, "bonjour", strdup("content1"), free); + t_ftht_content *content1 = ft_htset(ht, "bonjour", strdup("content1"), st_del); TEST_ASSERT_NOT_NULL(content1); TEST_ASSERT_NOT_NULL(content1->key); TEST_ASSERT_NOT_NULL(content1->value); TEST_ASSERT_EQUAL_STRING(content1->key, "bonjour"); TEST_ASSERT_EQUAL_STRING(content1->value, "content1"); - t_ftht_content *content2 = ft_htset(ht, "aurevoir", strdup("content2"), free); + t_ftht_content *content2 = ft_htset(ht, "aurevoir", strdup("content2"), st_del); TEST_ASSERT_NOT_NULL(content2); TEST_ASSERT_NOT_NULL(content2->key); TEST_ASSERT_NOT_NULL(content2->value); TEST_ASSERT_EQUAL_STRING(content2->key, "aurevoir"); TEST_ASSERT_EQUAL_STRING(content2->value, "content2"); - t_ftht_content *content3 = ft_htset(ht, "aloa", strdup("content3"), free); + t_ftht_content *content3 = ft_htset(ht, "aloa", strdup("content3"), st_del); TEST_ASSERT_NOT_NULL(content3); TEST_ASSERT_NOT_NULL(content3->key); TEST_ASSERT_NOT_NULL(content3->value); diff --git a/test/src/lst/test_ft_lstdelone.c b/test/src/lst/test_ft_lstdelone.c index 03c87d5..881a93b 100644 --- a/test/src/lst/test_ft_lstdelone.c +++ b/test/src/lst/test_ft_lstdelone.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/19 02:01:33 by cacharle #+# #+# */ +/* Updated: 2020/02/19 02:07:27 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "libft_test.h" TEST_GROUP(ft_lstdelone); diff --git a/test/src/main.c b/test/src/main.c index c015945..e93c623 100644 --- a/test/src/main.c +++ b/test/src/main.c @@ -35,6 +35,8 @@ static void run_all_test(void) RUN_TEST_GROUP(ft_htnew); RUN_TEST_GROUP(ft_htget); RUN_TEST_GROUP(ft_htset); + RUN_TEST_GROUP(ft_htdelone); + RUN_TEST_GROUP(ft_htdestroy); // algo RUN_TEST_GROUP(ft_bsearch); diff --git a/test/src/runner/test_runner_ht.c b/test/src/runner/test_runner_ht.c index f25a61a..6f83006 100644 --- a/test/src/runner/test_runner_ht.c +++ b/test/src/runner/test_runner_ht.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/12 22:44:39 by cacharle #+# #+# */ -/* Updated: 2020/02/17 05:02:25 by cacharle ### ########.fr */ +/* Updated: 2020/02/19 02:46:34 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,7 @@ TEST_GROUP_RUNNER(ft_htget) { RUN_TEST_CASE(ft_htget, segfault); RUN_TEST_CASE(ft_htget, error_null); + RUN_TEST_CASE(ft_htget, basic); } TEST_GROUP_RUNNER(ft_htset) @@ -38,3 +39,13 @@ TEST_GROUP_RUNNER(ft_htset) RUN_TEST_CASE(ft_htset, reset); RUN_TEST_CASE(ft_htset, collision); } + +TEST_GROUP_RUNNER(ft_htdelone) +{ + RUN_TEST_CASE(ft_htdelone, basic); +} + +TEST_GROUP_RUNNER(ft_htdestroy) +{ + RUN_TEST_CASE(ft_htdestroy, basic); +} diff --git a/vendor/_unity/src/unity_memory.c b/vendor/_unity/src/unity_memory.c deleted file mode 100644 index e4dc665..0000000 --- a/vendor/_unity/src/unity_memory.c +++ /dev/null @@ -1,202 +0,0 @@ -/* ========================================== - * Unity Project - A Test Framework for C - * Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams - * [Released under MIT License. Please refer to license.txt for details] - * ========================================== */ - -#include "unity.h" -#include "unity_memory.h" -#include - -#define MALLOC_DONT_FAIL -1 -static int malloc_count; -static int malloc_fail_countdown = MALLOC_DONT_FAIL; - -void UnityMalloc_StartTest(void) -{ - malloc_count = 0; - malloc_fail_countdown = MALLOC_DONT_FAIL; -} - -void UnityMalloc_EndTest(void) -{ - malloc_fail_countdown = MALLOC_DONT_FAIL; - if (malloc_count != 0) - { - UNITY_TEST_FAIL(Unity.CurrentTestLineNumber, "This test leaks!"); - } -} - -void UnityMalloc_MakeMallocFailAfterCount(int countdown) -{ - malloc_fail_countdown = countdown; -} - -/* These definitions are always included from unity_fixture_malloc_overrides.h */ -/* We undef to use them or avoid conflict with per the C standard */ -#undef malloc -#undef free -#undef calloc -#undef realloc - -#ifdef UNITY_EXCLUDE_STDLIB_MALLOC -static unsigned char unity_heap[UNITY_INTERNAL_HEAP_SIZE_BYTES]; -static size_t heap_index; -#else -#include -#endif - -typedef struct GuardBytes -{ - size_t size; - size_t guard_space; -} Guard; - -#define UNITY_MALLOC_ALIGNMENT (UNITY_POINTER_WIDTH / 8) -static const char end[] = "END"; - -static size_t unity_size_round_up(size_t size) -{ - size_t rounded_size; - - rounded_size = ((size + UNITY_MALLOC_ALIGNMENT - 1) / UNITY_MALLOC_ALIGNMENT) * UNITY_MALLOC_ALIGNMENT; - - return rounded_size; -} - -void* unity_malloc(size_t size) -{ - char* mem; - Guard* guard; - size_t total_size; - - total_size = sizeof(Guard) + unity_size_round_up(size + sizeof(end)); - - if (malloc_fail_countdown != MALLOC_DONT_FAIL) - { - if (malloc_fail_countdown == 0) - return NULL; - malloc_fail_countdown--; - } - - if (size == 0) return NULL; -#ifdef UNITY_EXCLUDE_STDLIB_MALLOC - if (heap_index + total_size > UNITY_INTERNAL_HEAP_SIZE_BYTES) - { - guard = NULL; - } - else - { - /* We know we can get away with this cast because we aligned memory already */ - guard = (Guard*)(void*)(&unity_heap[heap_index]); - heap_index += total_size; - } -#else - guard = (Guard*)UNITY_MALLOC(total_size); -#endif - if (guard == NULL) return NULL; - malloc_count++; - guard->size = size; - guard->guard_space = 0; - mem = (char*)&(guard[1]); - memcpy(&mem[size], end, sizeof(end)); - - return (void*)mem; -} - -static int isOverrun(void* mem) -{ - Guard* guard = (Guard*)mem; - char* memAsChar = (char*)mem; - guard--; - - return guard->guard_space != 0 || strcmp(&memAsChar[guard->size], end) != 0; -} - -static void release_memory(void* mem) -{ - Guard* guard = (Guard*)mem; - guard--; - - malloc_count--; -#ifdef UNITY_EXCLUDE_STDLIB_MALLOC - { - size_t block_size; - - block_size = unity_size_round_up(guard->size + sizeof(end)); - - if (mem == unity_heap + heap_index - block_size) - { - heap_index -= (sizeof(Guard) + block_size); - } - } -#else - UNITY_FREE(guard); -#endif -} - -void unity_free(void* mem) -{ - int overrun; - - if (mem == NULL) - { - return; - } - - overrun = isOverrun(mem); - release_memory(mem); - if (overrun) - { - UNITY_TEST_FAIL(Unity.CurrentTestLineNumber, "Buffer overrun detected during free()"); - } -} - -void* unity_calloc(size_t num, size_t size) -{ - void* mem = unity_malloc(num * size); - if (mem == NULL) return NULL; - memset(mem, 0, num * size); - return mem; -} - -void* unity_realloc(void* oldMem, size_t size) -{ - Guard* guard = (Guard*)oldMem; - void* newMem; - - if (oldMem == NULL) return unity_malloc(size); - - guard--; - if (isOverrun(oldMem)) - { - release_memory(oldMem); - UNITY_TEST_FAIL(Unity.CurrentTestLineNumber, "Buffer overrun detected during realloc()"); - } - - if (size == 0) - { - release_memory(oldMem); - return NULL; - } - - if (guard->size >= size) return oldMem; - -#ifdef UNITY_EXCLUDE_STDLIB_MALLOC /* Optimization if memory is expandable */ - { - size_t old_total_size = unity_size_round_up(guard->size + sizeof(end)); - - if ((oldMem == unity_heap + heap_index - old_total_size) && - ((heap_index - old_total_size + unity_size_round_up(size + sizeof(end))) <= UNITY_INTERNAL_HEAP_SIZE_BYTES)) - { - release_memory(oldMem); /* Not thread-safe, like unity_heap generally */ - return unity_malloc(size); /* No memcpy since data is in place */ - } - } -#endif - newMem = unity_malloc(size); - if (newMem == NULL) return NULL; /* Do not release old memory */ - memcpy(newMem, oldMem, guard->size); - release_memory(oldMem); - return newMem; -} diff --git a/vendor/_unity/unity_memory.c b/vendor/_unity/unity_memory.c new file mode 100644 index 0000000..e4dc665 --- /dev/null +++ b/vendor/_unity/unity_memory.c @@ -0,0 +1,202 @@ +/* ========================================== + * Unity Project - A Test Framework for C + * Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams + * [Released under MIT License. Please refer to license.txt for details] + * ========================================== */ + +#include "unity.h" +#include "unity_memory.h" +#include + +#define MALLOC_DONT_FAIL -1 +static int malloc_count; +static int malloc_fail_countdown = MALLOC_DONT_FAIL; + +void UnityMalloc_StartTest(void) +{ + malloc_count = 0; + malloc_fail_countdown = MALLOC_DONT_FAIL; +} + +void UnityMalloc_EndTest(void) +{ + malloc_fail_countdown = MALLOC_DONT_FAIL; + if (malloc_count != 0) + { + UNITY_TEST_FAIL(Unity.CurrentTestLineNumber, "This test leaks!"); + } +} + +void UnityMalloc_MakeMallocFailAfterCount(int countdown) +{ + malloc_fail_countdown = countdown; +} + +/* These definitions are always included from unity_fixture_malloc_overrides.h */ +/* We undef to use them or avoid conflict with per the C standard */ +#undef malloc +#undef free +#undef calloc +#undef realloc + +#ifdef UNITY_EXCLUDE_STDLIB_MALLOC +static unsigned char unity_heap[UNITY_INTERNAL_HEAP_SIZE_BYTES]; +static size_t heap_index; +#else +#include +#endif + +typedef struct GuardBytes +{ + size_t size; + size_t guard_space; +} Guard; + +#define UNITY_MALLOC_ALIGNMENT (UNITY_POINTER_WIDTH / 8) +static const char end[] = "END"; + +static size_t unity_size_round_up(size_t size) +{ + size_t rounded_size; + + rounded_size = ((size + UNITY_MALLOC_ALIGNMENT - 1) / UNITY_MALLOC_ALIGNMENT) * UNITY_MALLOC_ALIGNMENT; + + return rounded_size; +} + +void* unity_malloc(size_t size) +{ + char* mem; + Guard* guard; + size_t total_size; + + total_size = sizeof(Guard) + unity_size_round_up(size + sizeof(end)); + + if (malloc_fail_countdown != MALLOC_DONT_FAIL) + { + if (malloc_fail_countdown == 0) + return NULL; + malloc_fail_countdown--; + } + + if (size == 0) return NULL; +#ifdef UNITY_EXCLUDE_STDLIB_MALLOC + if (heap_index + total_size > UNITY_INTERNAL_HEAP_SIZE_BYTES) + { + guard = NULL; + } + else + { + /* We know we can get away with this cast because we aligned memory already */ + guard = (Guard*)(void*)(&unity_heap[heap_index]); + heap_index += total_size; + } +#else + guard = (Guard*)UNITY_MALLOC(total_size); +#endif + if (guard == NULL) return NULL; + malloc_count++; + guard->size = size; + guard->guard_space = 0; + mem = (char*)&(guard[1]); + memcpy(&mem[size], end, sizeof(end)); + + return (void*)mem; +} + +static int isOverrun(void* mem) +{ + Guard* guard = (Guard*)mem; + char* memAsChar = (char*)mem; + guard--; + + return guard->guard_space != 0 || strcmp(&memAsChar[guard->size], end) != 0; +} + +static void release_memory(void* mem) +{ + Guard* guard = (Guard*)mem; + guard--; + + malloc_count--; +#ifdef UNITY_EXCLUDE_STDLIB_MALLOC + { + size_t block_size; + + block_size = unity_size_round_up(guard->size + sizeof(end)); + + if (mem == unity_heap + heap_index - block_size) + { + heap_index -= (sizeof(Guard) + block_size); + } + } +#else + UNITY_FREE(guard); +#endif +} + +void unity_free(void* mem) +{ + int overrun; + + if (mem == NULL) + { + return; + } + + overrun = isOverrun(mem); + release_memory(mem); + if (overrun) + { + UNITY_TEST_FAIL(Unity.CurrentTestLineNumber, "Buffer overrun detected during free()"); + } +} + +void* unity_calloc(size_t num, size_t size) +{ + void* mem = unity_malloc(num * size); + if (mem == NULL) return NULL; + memset(mem, 0, num * size); + return mem; +} + +void* unity_realloc(void* oldMem, size_t size) +{ + Guard* guard = (Guard*)oldMem; + void* newMem; + + if (oldMem == NULL) return unity_malloc(size); + + guard--; + if (isOverrun(oldMem)) + { + release_memory(oldMem); + UNITY_TEST_FAIL(Unity.CurrentTestLineNumber, "Buffer overrun detected during realloc()"); + } + + if (size == 0) + { + release_memory(oldMem); + return NULL; + } + + if (guard->size >= size) return oldMem; + +#ifdef UNITY_EXCLUDE_STDLIB_MALLOC /* Optimization if memory is expandable */ + { + size_t old_total_size = unity_size_round_up(guard->size + sizeof(end)); + + if ((oldMem == unity_heap + heap_index - old_total_size) && + ((heap_index - old_total_size + unity_size_round_up(size + sizeof(end))) <= UNITY_INTERNAL_HEAP_SIZE_BYTES)) + { + release_memory(oldMem); /* Not thread-safe, like unity_heap generally */ + return unity_malloc(size); /* No memcpy since data is in place */ + } + } +#endif + newMem = unity_malloc(size); + if (newMem == NULL) return NULL; /* Do not release old memory */ + memcpy(newMem, oldMem, guard->size); + release_memory(oldMem); + return newMem; +} -- cgit