aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-04-03 07:19:25 +0200
committerCharles <sircharlesaze@gmail.com>2020-04-03 07:19:25 +0200
commitd2feec1f97e9f8f201e56ad33662bb663c328a0a (patch)
treef97690082352bb1752d867d66f21a7bee1ef126c
parent948c0953527fe3bef28904b38a16a9e4342e7e98 (diff)
downloadlibft-d2feec1f97e9f8f201e56ad33662bb663c328a0a.tar.gz
libft-d2feec1f97e9f8f201e56ad33662bb663c328a0a.tar.bz2
libft-d2feec1f97e9f8f201e56ad33662bb663c328a0a.zip
Changing hash table del function to regular one with a first order internal function, removing a few typedef to instead use standard types
-rw-r--r--include/libft.h14
-rw-r--r--include/libft_algo.h11
-rw-r--r--include/libft_def.h (renamed from include/libft_types.h)14
-rw-r--r--include/libft_ht.h50
-rw-r--r--include/libft_lst.h2
-rw-r--r--include/libft_mem.h2
-rw-r--r--src/algo/ft_is_set.c5
-rw-r--r--src/ht/ft_htdelone.c11
-rw-r--r--src/ht/ft_htdestroy.c10
-rw-r--r--src/ht/ft_htget.c6
-rw-r--r--src/ht/ft_hthash.c4
-rw-r--r--src/ht/ft_htnew.c8
-rw-r--r--src/ht/ft_htset.c37
-rw-r--r--src/ht/ft_inter_htdel_first_order.c33
-rw-r--r--src/str/ft_strcasecmp.c2
-rw-r--r--src/str/ft_strncasecmp.c2
-rw-r--r--src/str/ft_strncmp.c2
-rw-r--r--test/src/ht/test_ft_htdelone.c28
-rw-r--r--test/src/ht/test_ft_htdestroy.c18
-rw-r--r--test/src/ht/test_ft_htget.c54
-rw-r--r--test/src/ht/test_ft_htset.c16
-rw-r--r--test/src/runner/test_runner_ht.c2
22 files changed, 173 insertions, 158 deletions
diff --git a/include/libft.h b/include/libft.h
index 74f074b..e982608 100644
--- a/include/libft.h
+++ b/include/libft.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 09:45:02 by cacharle #+# #+# */
-/* Updated: 2020/02/10 02:19:47 by cacharle ### ########.fr */
+/* Updated: 2020/04/03 06:32:37 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,18 +17,18 @@
# include <stdarg.h>
# include <stdlib.h>
# include <stddef.h>
+# include <stdbool.h>
# include <limits.h>
# include <errno.h>
-# include "libft_types.h"
-# include "libft_ctype.h"
-# include "libft_io.h"
-# include "libft_mem.h"
-# include "libft_str.h"
-
# ifdef __linux__
# include <stdio.h>
# define OPEN_MAX FOPEN_MAX
# endif
+# include "libft_io.h"
+# include "libft_mem.h"
+# include "libft_str.h"
+# include "libft_ctype.h"
+
#endif
diff --git a/include/libft_algo.h b/include/libft_algo.h
index 7223e7b..04191e6 100644
--- a/include/libft_algo.h
+++ b/include/libft_algo.h
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* ft_algo.h :+: :+: :+: */
+/* libft_algo.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/19 07:22:57 by cacharle #+# #+# */
-/* Updated: 2020/02/10 05:58:26 by cacharle ### ########.fr */
+/* Updated: 2020/04/03 07:05:04 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,11 +14,8 @@
# define LIBFT_ALGO_H
# include <stdlib.h>
-# include <stddef.h>
# include "libft_mem.h"
-# include "libft_types.h"
-
-typedef int (*t_ftcompar_func)(const void*, const void*);
+# include "libft_def.h"
typedef struct
{
@@ -39,7 +36,7 @@ typedef struct s_ft_search_const
t_ftcompar_func compar;
} t_ftsearch_const;
-t_ftbool ft_is_set(void *base, size_t nel, size_t width,
+bool ft_is_set(void *base, size_t nel, size_t width,
t_ftcompar_func compar);
int ft_compar_int(const void *a, const void *b);
void ft_qsort(void *base, size_t nel, size_t width,
diff --git a/include/libft_types.h b/include/libft_def.h
index 3987a80..69a7655 100644
--- a/include/libft_types.h
+++ b/include/libft_def.h
@@ -1,17 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* libft_types.h :+: :+: :+: */
+/* libft_def.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/31 10:36:56 by cacharle #+# #+# */
-/* Updated: 2020/02/28 12:10:02 by cacharle ### ########.fr */
+/* Updated: 2020/04/03 07:04:57 by charles ### ########.fr */
/* */
/* ************************************************************************** */
-#ifndef LIBFT_TYPES_H
-# define LIBFT_TYPES_H
+#ifndef LIBFT_DEF_H
+# define LIBFT_DEF_H
+
+# include <stddef.h>
+# include <stdbool.h>
# define TRUE 1
# define FALSE 0
@@ -27,4 +30,7 @@ typedef unsigned int t_ftuint;
typedef long int t_ftlong;
typedef unsigned long int t_ftulong;
+typedef void (*t_ftdel_func)(void*);
+typedef int (*t_ftcompar_func)(const void*, const void*);
+
#endif
diff --git a/include/libft_ht.h b/include/libft_ht.h
index 10c6fc7..b3c1d2d 100644
--- a/include/libft_ht.h
+++ b/include/libft_ht.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/31 10:36:09 by cacharle #+# #+# */
-/* Updated: 2020/04/01 17:59:35 by charles ### ########.fr */
+/* Updated: 2020/04/03 07:12:12 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,6 +19,7 @@
*/
# include "libft.h"
+# include "libft_def.h"
# include "libft_lst.h"
/*
@@ -27,45 +28,42 @@
** \param value Pointer to data
*/
-typedef struct s_ftht_entry
+typedef struct s_ftht_entry
{
- char *key;
- void *value;
-} t_ftht_entry;
-
-typedef t_ftlst* t_ftht_bucket;
+ char *key;
+ void *value;
+} t_ftht_entry;
/*
** \brief Hash table struct
** \param size Number of buckets
-** \param buckets Bucket array
+** \param buckets Bucket array, each bucket is a linked list
*/
-typedef struct s_ftht
+typedef struct s_ftht
{
- t_ftsize size;
- t_ftht_bucket *buckets;
-} t_ftht;
-
-typedef t_ftuint t_ftht_digest;
+ size_t size;
+ t_ftlst **buckets;
+} t_ftht;
-t_ftht_digest ft_hthash(t_ftht *ht, char *key);
+size_t ft_hthash(t_ftht *ht, char *key);
-t_ftht *ft_htnew(t_ftsize size);
-void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_entry*));
-void *ft_htget(t_ftht *ht, char *key);
-t_ftht_entry *ft_htset(t_ftht *ht, char *key, void *value,
- void (*del)(t_ftht_entry*));
-void ft_htdelone(t_ftht *ht, char *key,
- void (*del)(t_ftht_entry*));
-t_ftht_entry *ft_htentry_new(char *key, void *value);
-void ft_htiter(t_ftht *ht, void (*f)(t_ftht_entry*));
+t_ftht *ft_htnew(size_t size);
+void ft_htdestroy(t_ftht *ht, t_ftdel_func del);
+void *ft_htget(t_ftht *ht, char *key);
+t_ftht_entry *ft_htset(t_ftht *ht, char *key, void *value,
+ t_ftdel_func del);
+void ft_htdelone(t_ftht *ht, char *key, t_ftdel_func del);
+t_ftht_entry *ft_htentry_new(char *key, void *value);
+void ft_htiter(t_ftht *ht, void (*f)(t_ftht_entry*));
/*
** internals
*/
-int ft_inter_htkey_cmp(const void *ref_key,
- const void *content);
+int ft_inter_htkey_cmp(const void *ref_key, const void *content);
+void ft_inter_htdel_first_order(t_ftht_entry *entry);
+void ft_inter_htdel_first_order_setup(t_ftdel_func del);
+void ft_inter_htdel_first_order_teardown(void);
#endif
diff --git a/include/libft_lst.h b/include/libft_lst.h
index d7157a9..2938bd2 100644
--- a/include/libft_lst.h
+++ b/include/libft_lst.h
@@ -19,7 +19,7 @@
*/
# include <stdlib.h>
-# include "libft_types.h"
+# include "libft_def.h"
# include "libft_algo.h"
/*
diff --git a/include/libft_mem.h b/include/libft_mem.h
index 37c072e..26b4ccd 100644
--- a/include/libft_mem.h
+++ b/include/libft_mem.h
@@ -14,7 +14,7 @@
# define LIBFT_MEM_H
# include <stddef.h>
-# include "libft_types.h"
+# include "libft_def.h"
void ft_bzero(void *s, size_t n);
void *ft_memset(void *s, int c, size_t n);
diff --git a/src/algo/ft_is_set.c b/src/algo/ft_is_set.c
index 3e7ae31..79a5e7c 100644
--- a/src/algo/ft_is_set.c
+++ b/src/algo/ft_is_set.c
@@ -6,14 +6,13 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/19 07:17:15 by cacharle #+# #+# */
-/* Updated: 2020/02/10 02:51:41 by cacharle ### ########.fr */
+/* Updated: 2020/04/03 07:05:19 by charles ### ########.fr */
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_algo.h"
-t_ftbool ft_is_set(void *base, size_t nel, size_t width,
+bool ft_is_set(void *base, size_t nel, size_t width,
t_ftcompar_func compar)
{
size_t i;
diff --git a/src/ht/ft_htdelone.c b/src/ht/ft_htdelone.c
index 7374a44..bc2e047 100644
--- a/src/ht/ft_htdelone.c
+++ b/src/ht/ft_htdelone.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/30 09:27:18 by cacharle #+# #+# */
-/* Updated: 2020/02/28 12:10:16 by cacharle ### ########.fr */
+/* Updated: 2020/04/03 07:14:28 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,14 +16,15 @@
/*
** \brief Delete one hash table entry
** \param key Key of entry to delete
-** \param del Function to destroy the entry
-** \warning The del function HAS to free the key
+** \param del Function to destroy the entry value
** \note Do nothing if their is to entry which correspond to key
*/
-void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_entry*))
+void ft_htdelone(t_ftht *ht, char *key, void (*del)(void*))
{
+ ft_inter_htdel_first_order_setup(del);
ft_lstremove_if(ht->buckets + ft_hthash(ht, key),
ft_inter_htkey_cmp, key,
- (void (*)(void*))del);
+ (void (*)(void*))ft_inter_htdel_first_order);
+ ft_inter_htdel_first_order_teardown();
}
diff --git a/src/ht/ft_htdestroy.c b/src/ht/ft_htdestroy.c
index ff362d2..c43754e 100644
--- a/src/ht/ft_htdestroy.c
+++ b/src/ht/ft_htdestroy.c
@@ -6,11 +6,10 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/30 08:31:02 by cacharle #+# #+# */
-/* Updated: 2020/02/28 12:10:31 by cacharle ### ########.fr */
+/* Updated: 2020/04/03 07:01:30 by charles ### ########.fr */
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_ht.h"
/*
@@ -19,12 +18,15 @@
** \warning The del function HAS to free the key
*/
-void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_entry*))
+void ft_htdestroy(t_ftht *ht, t_ftdel_func del)
{
if (ht == NULL)
return ;
+ ft_inter_htdel_first_order_setup(del);
while (ht->size-- > 0)
- ft_lstdestroy(ht->buckets + ht->size, (void (*)(void*))del);
+ ft_lstdestroy(ht->buckets + ht->size,
+ (void (*)(void*))ft_inter_htdel_first_order);
+ ft_inter_htdel_first_order_teardown();
free(ht->buckets);
free(ht);
}
diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c
index a6383fe..75da785 100644
--- a/src/ht/ft_htget.c
+++ b/src/ht/ft_htget.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/30 08:33:21 by cacharle #+# #+# */
-/* Updated: 2020/04/01 18:02:57 by charles ### ########.fr */
+/* Updated: 2020/04/03 07:12:58 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,8 +22,8 @@
void *ft_htget(t_ftht *ht, char *key)
{
- t_ftht_digest digest;
- t_ftlst *found;
+ size_t digest;
+ t_ftlst *found;
if (ht == NULL || key == NULL)
return (NULL);
diff --git a/src/ht/ft_hthash.c b/src/ht/ft_hthash.c
index 3369d24..c5d42ea 100644
--- a/src/ht/ft_hthash.c
+++ b/src/ht/ft_hthash.c
@@ -20,9 +20,9 @@
*/
// maybe use a less efficient but understandable function
-t_ftht_digest ft_hthash(t_ftht *ht, char *key)
+size_t ft_hthash(t_ftht *ht, char *key)
{
- t_ftht_digest digest;
+ size_t digest;
if (*key == '\0')
return (0);
diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c
index e5335d2..b6d1cc3 100644
--- a/src/ht/ft_htnew.c
+++ b/src/ht/ft_htnew.c
@@ -6,12 +6,12 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */
-/* Updated: 2020/02/28 12:23:43 by cacharle ### ########.fr */
+/* Updated: 2020/04/03 06:53:51 by charles ### ########.fr */
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_ht.h"
+#include "libft_mem.h"
/*
** \brief Create a new hash table.
@@ -19,7 +19,7 @@
** \return Created hash table or NULL is an allocation failed
*/
-t_ftht *ft_htnew(t_ftsize size)
+t_ftht *ft_htnew(size_t size)
{
t_ftht *ht;
@@ -27,7 +27,7 @@ t_ftht *ft_htnew(t_ftsize size)
return (NULL);
if ((ht = (t_ftht*)malloc(sizeof(t_ftht))) == NULL)
return (NULL);
- ht->buckets = (t_ftht_bucket*)ft_calloc(size, sizeof(t_ftht_entry));
+ ht->buckets = (t_ftlst**)ft_calloc(size, sizeof(t_ftlst*));
if (ht->buckets == NULL)
{
free(ht);
diff --git a/src/ht/ft_htset.c b/src/ht/ft_htset.c
index 68d3752..b3a44ee 100644
--- a/src/ht/ft_htset.c
+++ b/src/ht/ft_htset.c
@@ -6,18 +6,17 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/30 08:41:52 by cacharle #+# #+# */
-/* Updated: 2020/04/01 18:02:12 by charles ### ########.fr */
+/* Updated: 2020/04/03 07:13:17 by charles ### ########.fr */
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_ht.h"
/*
** \brief Create/Update a entry in hash table.
** \note If `key` already exist in `ht`
-** only updates the list node content.
-** Else create a new list node in addition the list content.
+** only updates the list node entry.
+** Else create a new list node in addition the list entry.
** \param ht Hash table where the entry is modified
** \param key Key of the new entry
** \param value Value of the new entry
@@ -25,32 +24,36 @@
** \return Pointer to the created entry, NULL if an allocation failed.
*/
-t_ftht_entry *ft_htset(t_ftht *ht, char *key, void *value,
- void (*del)(t_ftht_entry*))
+t_ftht_entry *ft_htset(
+ t_ftht *ht,
+ char *key,
+ void *value,
+ void (*del)(void*)
+)
{
- t_ftht_digest digest;
- t_ftht_entry *content;
- t_ftht_bucket bucket;
+ size_t digest;
+ t_ftht_entry *entry;
t_ftlst *tmp;
if (ht == NULL || key == NULL)
return (NULL);
- if ((content = ft_htentry_new(key, value)) == NULL)
- return (NULL);
digest = ft_hthash(ht, key);
tmp = ft_lstlfind(ht->buckets[digest], ft_inter_htkey_cmp, key);
if (tmp != NULL)
{
if (del != NULL)
- del(tmp->data);
- tmp->data = content;
+ del(((t_ftht_entry*)tmp->data)->value);
+ ((t_ftht_entry*)tmp->data)->value = value;
return ((t_ftht_entry*)tmp->data);
}
- if ((bucket = ft_lstnew(content)) == NULL)
+ if ((entry = ft_htentry_new(key, value)) == NULL)
+ return (NULL);
+ if ((tmp = ft_lstnew(entry)) == NULL)
{
- del(content);
+ free(entry->key);
+ free(entry);
return (NULL);
}
- ft_lstpush_front(ht->buckets + digest, bucket);
- return (content);
+ ft_lstpush_front(ht->buckets + digest, tmp);
+ return (entry);
}
diff --git a/src/ht/ft_inter_htdel_first_order.c b/src/ht/ft_inter_htdel_first_order.c
new file mode 100644
index 0000000..b6fd770
--- /dev/null
+++ b/src/ht/ft_inter_htdel_first_order.c
@@ -0,0 +1,33 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_inter_htdel_first_order.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/03 06:56:54 by charles #+# #+# */
+/* Updated: 2020/04/03 06:58:36 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_ht.h"
+
+static t_ftdel_func g_htdelone_value_del_func = NULL;
+
+void ft_inter_htdel_first_order(t_ftht_entry *entry)
+{
+ if (g_htdelone_value_del_func != NULL)
+ g_htdelone_value_del_func(entry->value);
+ free(entry->key);
+ free(entry);
+}
+
+void ft_inter_htdel_first_order_setup(t_ftdel_func del)
+{
+ g_htdelone_value_del_func = del;
+}
+
+void ft_inter_htdel_first_order_teardown(void)
+{
+ g_htdelone_value_del_func = NULL;
+}
diff --git a/src/str/ft_strcasecmp.c b/src/str/ft_strcasecmp.c
index 044e6de..6dd86eb 100644
--- a/src/str/ft_strcasecmp.c
+++ b/src/str/ft_strcasecmp.c
@@ -11,7 +11,7 @@
/* ************************************************************************** */
#include "libft_str.h"
-#include "libft_types.h"
+#include "libft_def.h"
int ft_strcasecmp(const char *s1, const char *s2)
{
diff --git a/src/str/ft_strncasecmp.c b/src/str/ft_strncasecmp.c
index aafdc8c..7153237 100644
--- a/src/str/ft_strncasecmp.c
+++ b/src/str/ft_strncasecmp.c
@@ -11,7 +11,7 @@
/* ************************************************************************** */
#include "libft.h"
-#include "libft_types.h"
+#include "libft_def.h"
int ft_strncasecmp(const char *s1, const char *s2, size_t n)
{
diff --git a/src/str/ft_strncmp.c b/src/str/ft_strncmp.c
index caa052b..d810e8c 100644
--- a/src/str/ft_strncmp.c
+++ b/src/str/ft_strncmp.c
@@ -11,7 +11,7 @@
/* ************************************************************************** */
#include "libft.h"
-#include "libft_types.h"
+#include "libft_def.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
diff --git a/test/src/ht/test_ft_htdelone.c b/test/src/ht/test_ft_htdelone.c
index 5ba4822..a64b34e 100644
--- a/test/src/ht/test_ft_htdelone.c
+++ b/test/src/ht/test_ft_htdelone.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/19 02:08:00 by cacharle #+# #+# */
-/* Updated: 2020/02/19 02:46:10 by cacharle ### ########.fr */
+/* Updated: 2020/04/03 07:08:38 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,23 +20,17 @@ TEST_SETUP(ft_htdelone)
TEST_TEAR_DOWN(ft_htdelone)
{}
-static void st_del(t_ftht_entry *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_htset(ht, "bonjour", strdup("je"), free);
+ ft_htset(ht, "a", strdup("yay"), free);
+ ft_htset(ht, "b", strdup("aasdf"), free);
+ ft_htset(ht, "c", "a", NULL);
+ ft_htset(ht, "d", "dd", NULL);
- ft_htdelone(ht, "bonjour", st_del);
+ ft_htdelone(ht, "bonjour", free);
void *ptr = ft_htget(ht, "bonjour");
TEST_ASSERT_NULL(ptr);
TEST_ASSERT_NOT_NULL(ft_htget(ht, "a"));
@@ -44,25 +38,25 @@ TEST(ft_htdelone, basic)
TEST_ASSERT_NOT_NULL(ft_htget(ht, "c"));
TEST_ASSERT_NOT_NULL(ft_htget(ht, "d"));
- ft_htdelone(ht, "a", st_del);
+ ft_htdelone(ht, "a", free);
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);
+ ft_htdelone(ht, "b", free);
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);
+ ft_htdelone(ht, "c", NULL);
ptr = ft_htget(ht, "c");
TEST_ASSERT_NULL(ptr);
TEST_ASSERT_NOT_NULL(ft_htget(ht, "d"));
- ft_htdelone(ht, "d", st_del);
+ ft_htdelone(ht, "d", NULL);
ptr = ft_htget(ht, "d");
TEST_ASSERT_NULL(ptr);
}
diff --git a/test/src/ht/test_ft_htdestroy.c b/test/src/ht/test_ft_htdestroy.c
index 1512a48..4d212e1 100644
--- a/test/src/ht/test_ft_htdestroy.c
+++ b/test/src/ht/test_ft_htdestroy.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/19 02:45:27 by cacharle #+# #+# */
-/* Updated: 2020/02/19 02:49:36 by cacharle ### ########.fr */
+/* Updated: 2020/04/03 07:07:07 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,19 +20,13 @@ TEST_SETUP(ft_htdestroy)
TEST_TEAR_DOWN(ft_htdestroy)
{}
-static void st_del(t_ftht_entry *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);
+ ft_htset(ht, "a", strdup("1"), free);
+ ft_htset(ht, "b", strdup("2"), free);
+ ft_htset(ht, "c", "3", NULL);
+ ft_htset(ht, "d", "4", NULL);
+ ft_htdestroy(ht, NULL);
}
diff --git a/test/src/ht/test_ft_htget.c b/test/src/ht/test_ft_htget.c
index 386c3dc..4d4b4eb 100644
--- a/test/src/ht/test_ft_htget.c
+++ b/test/src/ht/test_ft_htget.c
@@ -16,12 +16,6 @@ TEST_TEAR_DOWN(ft_htget)
int helper_segfault_pid;
-static void st_del(t_ftht_entry *c)
-{
- free(c->key);
- free(c);
-}
-
TEST(ft_htget, segfault)
{
TEST_ASSERT_SEGFAULT(ft_htget((t_ftht*)NULL, ""));
@@ -43,72 +37,72 @@ TEST(ft_htget, error_null)
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);
+ ft_htset(ht, strdup("a"), strdup("data1"), free);
+ ft_htset(ht, strdup("b"), strdup("data2"), free);
+ ft_htset(ht, strdup("c"), strdup("data3"), free);
char *s = ft_htget(ht, "a");
TEST_ASSERT_NOT_NULL(s);
- TEST_ASSERT_EQUAL_STRING(s, "data1");
+ TEST_ASSERT_EQUAL_STRING(s, "data1");
s = ft_htget(ht, "b");
TEST_ASSERT_NOT_NULL(s);
- TEST_ASSERT_EQUAL_STRING(s, "data2");
+ TEST_ASSERT_EQUAL_STRING(s, "data2");
s = ft_htget(ht, "c");
TEST_ASSERT_NOT_NULL(s);
- TEST_ASSERT_EQUAL_STRING(s, "data3");
+ 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);
+ ft_htset(ht, strdup("a"), strdup("bonjour1"), free);
+ ft_htset(ht, strdup("b"), strdup("bonjour2"), free);
+ ft_htset(ht, strdup("c"), strdup("bonjour3"), free);
s = ft_htget(ht, "a");
TEST_ASSERT_NOT_NULL(s);
- TEST_ASSERT_EQUAL_STRING(s, "bonjour1");
+ TEST_ASSERT_EQUAL_STRING(s, "bonjour1");
s = ft_htget(ht, "b");
TEST_ASSERT_NOT_NULL(s);
- TEST_ASSERT_EQUAL_STRING(s, "bonjour2");
+ TEST_ASSERT_EQUAL_STRING(s, "bonjour2");
s = ft_htget(ht, "c");
TEST_ASSERT_NOT_NULL(s);
- TEST_ASSERT_EQUAL_STRING(s, "bonjour3");
+ 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);
+ ft_htset(small, strdup("a"), strdup("data1"), free);
+ ft_htset(small, strdup("b"), strdup("data2"), free);
+ ft_htset(small, strdup("c"), strdup("data3"), free);
char *s = ft_htget(small, "a");
TEST_ASSERT_NOT_NULL(s);
- TEST_ASSERT_EQUAL_STRING(s, "data1");
+ TEST_ASSERT_EQUAL_STRING(s, "data1");
s = ft_htget(small, "b");
TEST_ASSERT_NOT_NULL(s);
- TEST_ASSERT_EQUAL_STRING(s, "data2");
+ TEST_ASSERT_EQUAL_STRING(s, "data2");
s = ft_htget(small, "c");
TEST_ASSERT_NOT_NULL(s);
- TEST_ASSERT_EQUAL_STRING(s, "data3");
+ 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);
+ ft_htset(small, strdup("a"), strdup("bonjour1"), free);
+ ft_htset(small, strdup("b"), strdup("bonjour2"), free);
+ ft_htset(small, strdup("c"), strdup("bonjour3"), free);
s = ft_htget(small, "a");
TEST_ASSERT_NOT_NULL(s);
- TEST_ASSERT_EQUAL_STRING(s, "bonjour1");
+ TEST_ASSERT_EQUAL_STRING(s, "bonjour1");
s = ft_htget(small, "b");
TEST_ASSERT_NOT_NULL(s);
- TEST_ASSERT_EQUAL_STRING(s, "bonjour2");
+ TEST_ASSERT_EQUAL_STRING(s, "bonjour2");
s = ft_htget(small, "c");
TEST_ASSERT_NOT_NULL(s);
- TEST_ASSERT_EQUAL_STRING(s, "bonjour3");
+ 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 96d848e..70232fd 100644
--- a/test/src/ht/test_ft_htset.c
+++ b/test/src/ht/test_ft_htset.c
@@ -14,12 +14,6 @@ TEST_TEAR_DOWN(ft_htset)
/* ft_htdestroy_key(ht); */
}
-static void st_del(t_ftht_entry *c)
-{
- free(c->key);
- free(c);
-}
-
TEST(ft_htset, segfault)
{
TEST_ASSERT_SEGFAULT(ft_htset(NULL, "", strdup(""), NULL));
@@ -57,14 +51,14 @@ TEST(ft_htset, reset)
small = ft_htnew(3);
- t_ftht_entry *content = ft_htset(small, "bonjour", ft_strdup("content"), st_del);
+ t_ftht_entry *content = ft_htset(small, "bonjour", ft_strdup("content"), free);
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_entry *content_re = ft_htset(small, "bonjour", ft_strdup("yo"), st_del);
+ t_ftht_entry *content_re = ft_htset(small, "bonjour", ft_strdup("yo"), free);
TEST_ASSERT_NOT_NULL(content_re);
TEST_ASSERT_NOT_NULL(content_re->key);
TEST_ASSERT_NOT_NULL(content_re->value);
@@ -77,21 +71,21 @@ TEST(ft_htset, collision)
t_ftht *small = NULL;