From 1925805cc760061d5742f9d215998561fcd45211 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 1 Apr 2020 23:02:59 +0200 Subject: Added ft_vecremove, Updated README.md --- Makefile | 5 +- README.md | 163 +------------------------------------- include/libft_vec.h | 3 +- src/vec/ft_vecremove.c | 31 ++++++++ test/src/main.c | 1 + test/src/runner/test_runner_vec.c | 7 +- test/src/vec/test_ft_vecremove.c | 64 +++++++++++++++ 7 files changed, 111 insertions(+), 163 deletions(-) create mode 100644 src/vec/ft_vecremove.c create mode 100644 test/src/vec/test_ft_vecremove.c diff --git a/Makefile b/Makefile index da7db5a..7886427 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/10/08 15:45:53 by cacharle #+# #+# # -# Updated: 2020/04/01 18:09:04 by charles ### ########.fr # +# Updated: 2020/04/01 22:00:44 by charles ### ########.fr # # # # **************************************************************************** # @@ -31,6 +31,9 @@ INCLUDE_DIR = include CC = gcc OFLAG ?= -O1 CCFLAGS = $(OFLAG) -I$(INCLUDE_DIR) -Wall -Wextra -Werror +ifeq ($(TRAVIS_COMPILER),gcc) +CCFLAGS += -Wno-unused-result +endif IGNORE_FILE = .libftignore IGNORE_DEFAULT = ft_printf diff --git a/README.md b/README.md index 6984a24..a278471 100644 --- a/README.md +++ b/README.md @@ -28,166 +28,9 @@ Much like the `.gitignore` file, you can put the files/directory to ignore when ## Documentation -### mem - -| Name | Prototype | Description | Tested | -|--------------------|-------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------|--------| -| ft_bzero | `void ft_bzero(void *s, size_t n)` | fill `n` bytes of `s` with 0 | [x] | -| ft_calloc | `void *ft_calloc(size_t count, size_t size)` | allocate `count` element of `size` and initialize them to 0 | [x] | -| ft_memccpy | `void *ft_memccpy(void *dest, const void *src, int c, size_t n)` | copy `n` byte from `src` to `dest` until `c` is reached | [x] | -| ft_memchr | `void *ft_memchr(const void *s, int c, size_t n)` | return the address of the first occurance of `c` in `s` or NULL if `c` isn't found | [x] | -| ft_memcmp | `int ft_memcmp(const void *s1, const void *s2, size_t n)` | compare `n` byte of `s1` and `s2` | [x] | -| ft_memcpy | `void *ft_memcpy(void *dest, const void *src, size_t n)` | copy `n` byte from `src` to `dest` | [x] | -| ft_memmem | `void *ft_memmem(const void *big, size_t big_len, const void *little, size_t little_len)` | search `little` in `big` | [x] | -| ft_memmove | `void *ft_memmove(void *dst, const void *src, size_t len)` | copy `len` byte from `src` to `dst` and the copied memory can overlap | [x] | -| ft_memset | `void *ft_memset(void *s, int c, size_t n)` | set `n` byte of `s` to `c` | [x] | -| ft_memset_pattern4 | `void ft_memset_pattern4(void *b, const void *pattern4, size_t len)` | set `len` byte of `b` to a reapeated pattern of 4 bytes | [x] | -| ft_memswap | `void ft_memswap(void *a, void *b, size_t size)` | swap `a` and `b` memory | [x] | - -### algo - -| Name | Prototype | Description | Tested | -|---------------|-----------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------|--------| -| ft_bsearch | `void *ft_bsearch(const void *base, size_t nel, size_t width, t_ftsearch_const *consts)` | search `consts.key` in `base` using binary search (NULL if not found) | [x] | -| ft_compar_int | `int ft_compar_int(const void *a, const void *b)` | comparison function of `int` | [x] | -| ft_heapsort | `int ft_heapsort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *))` | sort `base` using heapsort | [?] | -| ft_is_set | `t_ftbool ft_is_set(void *base, size_t nel, size_t width, t_ftcompar_func compar)` | check is `base` has unique elements | [x] | -| ft_lfind | `void *ft_lfind(const void *base, size_t *nelp, size_t width, t_ftsearch_const *consts)` | search `consts.key` in `base` using linear search (NULL if not found) | [x] | -| ft_lsearch | `void *ft_lsearch(const void *base, size_t *nelp, size_t width, t_ftsearch_const *consts)` | search `consts.key` in `base` using linear search (insert at the end if not found) | [x] | -| ft_mergesort | `int ft_mergesort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *))` | sort `base` using mergesort | [x] | -| ft_qsort | `void ft_qsort(void *base, size_t nel, size_t width, t_ftcompar_func compar)` | sort `base` using quicksort | [x] | -| ft_reverse | `void ft_reverse(void *base, size_t nel, size_t width)` | reverse `base` | [x] | - -### bt - -| Name | Prototype | Description | Tested | -|--------------|---------------------------------------------------------------|----------------------------------------------|--------| -| ft_btdestroy | `void ft_btdestroy(t_ftbtree *tree, void (*del)(void *data))` | destroy `tree` and apply `del` on nodes data | [ ] | -| ft_btnew | `t_ftbtree *ft_btnew(void *data)` | create a new node with `data` | [ ] | - -### ctype - -| Name | Prototype | Description | Tested | -|------------|-------------------------|----------------------------------------------------|--------| -| ft_isalnum | `int ft_isalnum(int c)` | c is alphanumeric | [x] | -| ft_isalpha | `int ft_isalpha(int c)` | c is alphabetic | [x] | -| ft_isascii | `int ft_isascii(int c)` | c is ascii | [x] | -| ft_isblank | `int ft_isblank(int c)` | c is `\t` ` ` | [x] | -| ft_isdigit | `int ft_isdigit(int c)` | c is a digit | [x] | -| ft_isprint | `int ft_isprint(int c)` | c is a printable character | [x] | -| ft_isspace | `int ft_isspace(int c)` | c is `\t` `\n` `\v` `\f` `\r` ` ` | [x] | -| ft_todigit | `int ft_todigit(int c)` | convert lower case to upper case | [x] | -| ft_tolower | `int ft_tolower(int c)` | convert upper case to lower case | [x] | -| ft_toupper | `int ft_toupper(int c)` | convert digit character to int (-1 if not a digit) | [x] | - -### ht - -| 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` | [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 | [x] | - -### io - -| Name | Prototype | Description | Tested | -|---------------|-----------------------------------------|------------------------------------------------|--------| -| ft_getchar | `char ft_getchar(void)` | read one character from stdin and return it | [ ] | -| ft_next_line | `int ft_next_line(int fd, char **line)` | read a line of `fd` and put it in `line` | [ ] | -| ft_putchar | `void ft_putchar(char c)` | write a character on stdout | [ ] | -| ft_putchar_fd | `void ft_putchar_fd(char c, int fd)` | write a character on `fd` | [ ] | -| ft_putendl | `void ft_putendl(char *s)` | write a string followed by a newline on stdout | [ ] | -| ft_putendl_fd | `void ft_putendl_fd(char *s, int fd)` | write a string followed by a newline on `fd` | [ ] | -| ft_putnbr | `void ft_putnbr(int n)` | write a number on stdout | [ ] | -| ft_putnbr_fd | `void ft_putnbr_fd(int n, int fd)` | write a number on `fd` | [ ] | -| ft_putstr | `void ft_putstr(char const *s)` | write a string on stdout | [ ] | -| ft_putstr_fd | `void ft_putstr_fd(char *s, int fd)` | write a string on `fd` | [ ] | - -### printf - -| Name | Prototype | Description | Tested | -|---------------|----------------------------------------------------------------------------|---------------------------------------------------------------------|--------| -| ft_asprintf | `int ft_asprintf(char **ret, const char *format, ...)` | allocate and return the formated string | [ ] | -| ft_dprintf | `int ft_dprintf(int fd, const char *format, ...)` | write formated string to `fd` | [ ] | -| ft_printf | `int ft_printf(const char *format, ...)` | write formated string to stdout | [ ] | -| ft_snprintf | `int ft_snprintf(char *str, size_t size, const char *format, ...)` | write formated string in `str` with max `size` character | [ ] | -| ft_sprintf | `int ft_sprintf(char *str, const char *format, ...)` | write formated string in `str` | [ ] | -| ft_vasprintf | `int ft_vasprintf(char **ret, const char *format, va_list ap)` | allocate and return the formated string, with `ap` | [ ] | -| ft_vdprintf | `int ft_vdprintf(int fd, const char *format, va_list ap)` | write formated string to `fd` , with `ap` | [ ] | -| ft_vprintf | `int ft_vprintf(const char *format, va_list ap)` | write formated string to stdout, with `ap` | [ ] | -| ft_vsnprintf | `int ft_vsnprintf(char *str, size_t size, const char *format, va_list ap)` | write formated string in `str` with max `size` character, with `ap` | [ ] | -| ft_vsprintf | `int ft_vsprintf(char *str, const char *format, va_list ap)` | write formated string in `str`, with `ap` | [ ] | - -### lst - -| Name | Prototype | Description | Tested | -|--------------------|---------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------|--------| -| ft_lstadd_back | `void ft_lstadd_back(t_ftlst **alst, t_ftlst *new)` | add `new` at the start of `alst` | [x] | -| ft_lstadd_front | `void ft_lstadd_front(t_ftlst **alst, t_ftlst *new)` | add `new` at the end of `alst` | [x] | -| ft_lstbsearch | `t_ftlst *ft_lstbsearch(t_ftlst *lst, t_ftbool (*equal)(void *ref, void *content), void *ref)` | search `ref` in `lst` using binary search | [x] | -| ft_lstlsearch | `t_ftlst *ft_lstlsearch(t_ftlst *lst, t_ftbool (*equal)(void *ref, void *content), void *ref)` | search `ref` in `lst` using linear search (push back if not found)| [x] | -| ft_lstlfind | `t_ftlst *ft_lstlsearch(t_ftlst *lst, t_ftbool (*equal)(void *ref, void *content), void *ref)` | search `ref` in `lst` using linear search | [x] | -| ft_lstclear | `void ft_lstclear(t_ftlst **lst, void (*del)(void *))` | free all data with `del`, free all nodes and set `*lst` to NULL | [x] | -| ft_lstdelone | `void ft_lstdelone(t_ftlst *lst, void (*del)(void *))` | free `lst` node | [x] | -| ft_lstiter | `void ft_lstiter(t_ftlst *lst, void (*f)(void *))` | apply `f` on each node of `lst` | [x] | -| ft_lstlast | `t_ftlst *ft_lstlast(t_ftlst *lst)` | return the last node of `lst` | [x] | -| ft_lstmap | `t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *))` | create a new list which is `lst` with all node passed through `f` | [x] | -| ft_lstnew | `t_ftlst *ft_lstnew(void const *content)` | create a new node with `content` | [x] | -| ft_lstpop_front | `void ft_lstpop_front(t_ftlst **lst, void (*del)(void *))` | remove the front of `lst` | [x] | -| ft_lstremove_if | `void ft_lstremove_if(t_ftlst **lst, int (*cmp)(const void *ref, const void *content), void *ref, void (*del)(void *content))` | remove all element which verify `equal` | [x] | -| ft_lstreverse | `void ft_lstreverse(t_ftlst **lst)` | reverse `lst` | [x] | -| ft_lstreverse_ret | `t_ftlst *ft_lstreverse_ret(t_ftlst *lst)` | reverse `lst` and return it | [x] | -| ft_lstsize | `int ft_lstsize(t_ftlst *lst)` | return the number of node in `lst` | [x] | -| ft_lstsort | `void ft_lstsort(t_ftlst **begin_list, int (*cmp)(void *, void*))` | sort `begin_list` using mergesort | [x] | -| ft_lstsorted_merge | `t_ftlst *ft_lstsorted_merge(t_ftlst *l1, t_ftlst *l2, int (*cmp)(void *, void *))` | merge 2 sorted list and return the merged and still sorted one | [x] | - -### str - -| Name | Prototype | Description | Tested | -|----------------|--------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------|--------| -| ft_atoi | `int ft_atoi(const char *nptr)` | convert `nptr` to an int | [ ] | -| ft_atoi_strict | `int ft_atoi_strict(const char *s )` | convert `nptr` to an int without spaces or other characters | [ ] | -| ft_itoa | `char *ft_itoa(int n)` | return a string which represent `n` | [ ] | -| ft_split | `char **ft_split(char const *s, char c)` | return the strings in between `c` in `s` | [ ] | -| ft_strcasecmp | `int ft_strcasecmp(const char *s1, const char *s2)` | compare `s1` and `s2` ignoring case | [ ] | -| ft_strcat | `char *ft_strcat(char *dest, const char *src)` | concatenate `dest` and `src` in `dest` | [ ] | -| ft_strchr | `char *ft_strchr(const char *s, int c)` | search `c` in `s`, return address if found, NULL otherwise | [ ] | -| ft_strcmp | `int ft_strcmp(const char *s1, const char *s2)` | compare `s1` and `s2` | [ ] | -| ft_strcount | `int ft_strcount(char *str, char c)` | count the number of occurence of `c` in `str` | [ ] | -| ft_strcpy | `char *ft_strcpy(char *dest, const char *src)` | copy `src` in `dest` | [ ] | -| ft_strcspn | `size_t ft_strcspn(const char *s, const char *charset)` | return the index of the first character in `charset` | [ ] | -| ft_strdup | `char *ft_strdup(const char *s)` | allocate and copy `s` in a new string then return it | [ ] | -| ft_strequ | `int ft_strequ(char const *s1, char const *s2)` | test if `s1` and `s2` are equal | [ ] | -| ft_striter | `void ft_striter(char *s, void (*f)(char *))` | apply `f` over every character of `s` | [ ] | -| ft_striteri | `void ft_striteri(char *s, void (*f)(unsigned int, char *))` | apply `f` over every character of `s` with index | [ ] | -| ft_strjoin | `char *ft_strjoin(char const *s1, char const *s2)` | allocate a concatenation of `s1` and `s2` | [ ] | -| ft_strjoinf | `char *ft_strjoinf(char const *s1, char const *s2, t_ftstrjoin_tag tag)` | allocate a concatenation of `s1` and `s2` then free `s1`, `s2` or both according to `tag` | [ ] | -| ft_strlcat | `size_t ft_strlcat(char *dst, const char *src, size_t size)` | concatenate `dest` and `src` in dest with maximum `size - 1` character | [ ] | -| ft_strlcpy | `size_t ft_strlcpy(char *dst, const char *src, size_t size)` | copy `src` in `dest` with maximum `size - 1` character | [ ] | -| ft_strlen | `size_t ft_strlen(const char *s)` | return the length of `s` | [x] | -| ft_strmap | `char *ft_strmap(char const *s, char (*f)(char))` | allocate a copy of `s` with every character passed through `f` | [ ] | -| ft_strmapi | `char *ft_strmapi(char *s, char (*f)(unsigned int, char))` | allocate a copy of `s` with every character passed through `f` with index | [ ] | -| ft_strncasecmp | `int ft_strncasecmp(const char *s1, const char *s2, size_t n)` | compare `s1` and `s2` ignoring case until `n` | [ ] | -| ft_strncat | `char *ft_strncat(char *dest, const char *src, size_t n)` | concatenate `dest` and `src` in dest with maximum `n` character | [ ] | -| ft_strncmp | `int ft_strncmp(const char *s1, const char *s2, size_t n)` | compare `s1` and `s2` until `n` | [ ] | -| ft_strncpy | `char *ft_strncpy(char *dest, const char *src, size_t n)` | copy `src` in `dest` with maximum `n` character | [ ] | -| ft_strndup | `char *ft_strndup(const char *s1, size_t n)` | allocate and copy `s` until `n` in a new string then return it | [ ] | -| ft_strnequ | `int ft_strnequ(char const *s1, char const *s2, size_t n)` | test if `s1` and `s2` are not equal | [ ] | -| ft_strnlen | `size_t ft_strnlen(const char *s, size_t maxlen)` | return the length of `s` until `maxlen` | [ ] | -| ft_strnstr | `char *ft_strnstr(const char *haystack, const char *needle, size_t len)` | search `len` character of `haystack` for `needle` | [ ] | -| ft_strpbrk | `char *ft_strpbrk(const char *s, const char *charset)` | return the address of the first character in `charset` in `s` (NULL if not any) | [ ] | -| ft_strrchr | `char *ft_strrchr(const char *s, int c)` | search `c` in `s` from the end, return the address of first match (NULL if not found) | [ ] | -| ft_strsep | `char *ft_strsep(char **stringp, const char *delim)` | find first occurance of `delim` in `*stringp` replace it with `'\0'` return pointer to the match and store char after in `*stringp` | [ ] | -| ft_strspn | `size_t ft_strspn(const char *s, const char *charset)` | return the index of the first character not in `charset` | [ ] | -| ft_strstr | `char *ft_strstr(const char *haystack, const char *needle)` | search for `needle` in `haystack` (NULL if not found) | [ ] | -| ft_strtol | `long ft_strtol(const char *s, char **endptr, int base)` | convert `s` to a long int and store address of the next to last digit in `endptr`, according to `base` (2..26) | [ ] | -| ft_strtolower | `char *ft_strtolower(char *s)` | convert all characters of `s` to lower case | [ ] | -| ft_strtoupper | `char *ft_strtoupper(char *s)` | convert all characters of `s` to upper case | [ ] | -| ft_strtrim | `char *ft_strtrim(char const *s1, char const *set)` | remove the characters in `set` at the start and end of `s1` | [ ] | -| ft_substr | `char *ft_substr(char const *s, unsigned int start, size_t len)` | allocate and fill substring in `s` at `start` with length `len` | [ ] | +1. Install [Doxygen](http://doxygen.nl/manual/install.html) +2. Run `make doc` +3. Open doc/html/index.html in your browser or read the man in doc/man/man3 ## School correction state diff --git a/include/libft_vec.h b/include/libft_vec.h index 62ec257..f9199ba 100644 --- a/include/libft_vec.h +++ b/include/libft_vec.h @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 18:57:16 by charles #+# #+# */ -/* Updated: 2020/04/01 19:40:53 by charles ### ########.fr */ +/* Updated: 2020/04/01 22:53:33 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,5 +42,6 @@ t_ftvec *ft_vecgrow(t_ftvec *vec); t_ftvec *ft_vecpush(t_ftvec *vec, void *pushed); void ft_vecpop(t_ftvec *vec, void (*del)(void *elem)); void ft_veciter(t_ftvec *vec, void (*f)(void *elem)); +void ft_vecremove(t_ftvec *vec, size_t i, void (*del)(void *elem)); #endif diff --git a/src/vec/ft_vecremove.c b/src/vec/ft_vecremove.c new file mode 100644 index 0000000..d24ba29 --- /dev/null +++ b/src/vec/ft_vecremove.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecremove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 22:45:07 by charles #+# #+# */ +/* Updated: 2020/04/01 22:58:21 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Remove element from vector +** \param vec Vector to remove from +** \param i Index of the element to remove +** \param del Delete function applied to ith element +*/ + +void ft_vecremove(t_ftvec *vec, size_t i, void (*del)(void *elem)) +{ + if (vec->size == 0 || i > vec->size - 1) + return ; + if (del != NULL) + del(vec->data[i]); + ft_memmove(vec->data + i, vec->data + i + 1, + (vec->size - i - 1) * sizeof(void*)); + vec->size--; +} diff --git a/test/src/main.c b/test/src/main.c index 83e8d5f..8884f81 100644 --- a/test/src/main.c +++ b/test/src/main.c @@ -76,6 +76,7 @@ static void run_all_test(void) RUN_TEST_GROUP(ft_vecnew); RUN_TEST_GROUP(ft_vecpop); RUN_TEST_GROUP(ft_vecpush); + RUN_TEST_GROUP(ft_vecremove); } int main(int argc, const char **argv) diff --git a/test/src/runner/test_runner_vec.c b/test/src/runner/test_runner_vec.c index af91360..e0d3008 100644 --- a/test/src/runner/test_runner_vec.c +++ b/test/src/runner/test_runner_vec.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 19:55:53 by charles #+# #+# */ -/* Updated: 2020/04/01 19:58:25 by charles ### ########.fr */ +/* Updated: 2020/04/01 22:49:28 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,3 +43,8 @@ TEST_GROUP_RUNNER(ft_vecpush) RUN_TEST_CASE(ft_vecpush, basic); } +TEST_GROUP_RUNNER(ft_vecremove) +{ + RUN_TEST_CASE(ft_vecremove, basic); +} + diff --git a/test/src/vec/test_ft_vecremove.c b/test/src/vec/test_ft_vecremove.c new file mode 100644 index 0000000..8cecc19 --- /dev/null +++ b/test/src/vec/test_ft_vecremove.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_ft_vecremove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 22:48:58 by charles #+# #+# */ +/* Updated: 2020/04/01 22:56:37 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_test.h" + +TEST_GROUP(ft_vecremove); + +TEST_SETUP(ft_vecremove) +{} + +TEST_TEAR_DOWN(ft_vecremove) +{} + +TEST(ft_vecremove, basic) +{ + t_ftvec *vec; + + vec = ft_vecnew(4); + for (size_t i = 0; i < 4; i++) + vec->data[i] = (void*)(i + 1); + vec->size = 4; + + ft_vecremove(vec, 0, NULL); + TEST_ASSERT_EQUAL(3, vec->size); + TEST_ASSERT_EQUAL(4, vec->capacity); + TEST_ASSERT_EQUAL_UINT64(2, (size_t)vec->data[0]); + TEST_ASSERT_EQUAL_UINT64(3, (size_t)vec->data[1]); + TEST_ASSERT_EQUAL_UINT64(4, (size_t)vec->data[2]); + + ft_vecremove(vec, 1, NULL); + TEST_ASSERT_EQUAL(2, vec->size); + TEST_ASSERT_EQUAL(4, vec->capacity); + TEST_ASSERT_EQUAL_UINT64(2, (size_t)vec->data[0]); + TEST_ASSERT_EQUAL_UINT64(4, (size_t)vec->data[1]); + + ft_vecremove(vec, 1, NULL); + TEST_ASSERT_EQUAL(1, vec->size); + TEST_ASSERT_EQUAL(4, vec->capacity); + TEST_ASSERT_EQUAL_UINT64(2, (size_t)vec->data[0]); + + ft_vecremove(vec, 1, NULL); + TEST_ASSERT_EQUAL(1, vec->size); + TEST_ASSERT_EQUAL(4, vec->capacity); + TEST_ASSERT_EQUAL_UINT64(2, (size_t)vec->data[0]); + + ft_vecremove(vec, 0, NULL); + TEST_ASSERT_EQUAL(0, vec->size); + TEST_ASSERT_EQUAL(4, vec->capacity); + + ft_vecremove(vec, 0, NULL); + TEST_ASSERT_EQUAL(0, vec->size); + TEST_ASSERT_EQUAL(4, vec->capacity); + + ft_vecdestroy(vec, NULL); +} -- cgit From a2452f88fffffe4e2a278578de5ac961858a5b72 Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 2 Apr 2020 11:20:44 +0200 Subject: Fixed ft_vecgrow, Added ft_vecinsert --- include/libft_vec.h | 3 +- src/vec/ft_vecgrow.c | 4 +-- src/vec/ft_vecinsert.c | 36 +++++++++++++++++++++ src/vec/ft_vecpush.c | 2 +- test/src/main.c | 1 + test/src/runner/test_runner_vec.c | 6 +++- test/src/vec/test_ft_vecgrow.c | 41 +++++++++++++++++++----- test/src/vec/test_ft_vecinsert.c | 66 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 147 insertions(+), 12 deletions(-) create mode 100644 src/vec/ft_vecinsert.c create mode 100644 test/src/vec/test_ft_vecinsert.c diff --git a/include/libft_vec.h b/include/libft_vec.h index f9199ba..c4e3e22 100644 --- a/include/libft_vec.h +++ b/include/libft_vec.h @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 18:57:16 by charles #+# #+# */ -/* Updated: 2020/04/01 22:53:33 by charles ### ########.fr */ +/* Updated: 2020/04/02 11:00:57 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,5 +43,6 @@ t_ftvec *ft_vecpush(t_ftvec *vec, void *pushed); void ft_vecpop(t_ftvec *vec, void (*del)(void *elem)); void ft_veciter(t_ftvec *vec, void (*f)(void *elem)); void ft_vecremove(t_ftvec *vec, size_t i, void (*del)(void *elem)); +t_ftvec *ft_vecinsert(t_ftvec *vec, size_t i, void *elem); #endif diff --git a/src/vec/ft_vecgrow.c b/src/vec/ft_vecgrow.c index bb8b8c7..2213c88 100644 --- a/src/vec/ft_vecgrow.c +++ b/src/vec/ft_vecgrow.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 19:13:07 by charles #+# #+# */ -/* Updated: 2020/04/01 21:15:20 by charles ### ########.fr */ +/* Updated: 2020/04/02 10:43:01 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ t_ftvec *ft_vecgrow(t_ftvec *vec) new_capacity = vec->capacity * FT_VEC_GROWTH_FACTOR; if ((new_data = (void**)malloc(sizeof(void*) * new_capacity)) == NULL) return (NULL); - ft_memcpy(new_data, vec->data, vec->size); + ft_memcpy(new_data, vec->data, vec->size * sizeof(void*)); free(vec->data); vec->data = new_data; vec->capacity = new_capacity; diff --git a/src/vec/ft_vecinsert.c b/src/vec/ft_vecinsert.c new file mode 100644 index 0000000..1682daa --- /dev/null +++ b/src/vec/ft_vecinsert.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecinsert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/02 10:46:59 by charles #+# #+# */ +/* Updated: 2020/04/02 11:04:19 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Insert element in vector +** \param vec Vector where element is inserted +** \param i Index where element should be inserted, +** bound checking is performed +** \param elem Element to insert +** \return Passed vector or NULL on error +*/ + +t_ftvec *ft_vecinsert(t_ftvec *vec, size_t i, void *elem) +{ + if (i > vec->size) + return (NULL); + if (vec->capacity <= vec->size) + if (ft_vecgrow(vec) == NULL) + return (NULL); + ft_memmove(vec->data + i + 1, vec->data + i, + (vec->size - i) * sizeof(void*)); + vec->data[i] = elem; + vec->size++; + return (vec); +} diff --git a/src/vec/ft_vecpush.c b/src/vec/ft_vecpush.c index fc903ef..026ae3d 100644 --- a/src/vec/ft_vecpush.c +++ b/src/vec/ft_vecpush.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 19:22:20 by charles #+# #+# */ -/* Updated: 2020/04/01 20:20:06 by charles ### ########.fr */ +/* Updated: 2020/04/02 10:51:38 by charles ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/test/src/main.c b/test/src/main.c index 8884f81..ad5c9e8 100644 --- a/test/src/main.c +++ b/test/src/main.c @@ -77,6 +77,7 @@ static void run_all_test(void) RUN_TEST_GROUP(ft_vecpop); RUN_TEST_GROUP(ft_vecpush); RUN_TEST_GROUP(ft_vecremove); + RUN_TEST_GROUP(ft_vecinsert); } int main(int argc, const char **argv) diff --git a/test/src/runner/test_runner_vec.c b/test/src/runner/test_runner_vec.c index e0d3008..e084742 100644 --- a/test/src/runner/test_runner_vec.c +++ b/test/src/runner/test_runner_vec.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 19:55:53 by charles #+# #+# */ -/* Updated: 2020/04/01 22:49:28 by charles ### ########.fr */ +/* Updated: 2020/04/02 11:01:36 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,3 +48,7 @@ TEST_GROUP_RUNNER(ft_vecremove) RUN_TEST_CASE(ft_vecremove, basic); } +TEST_GROUP_RUNNER(ft_vecinsert) +{ + RUN_TEST_CASE(ft_vecinsert, basic); +} diff --git a/test/src/vec/test_ft_vecgrow.c b/test/src/vec/test_ft_vecgrow.c index 0b40390..8a80c7e 100644 --- a/test/src/vec/test_ft_vecgrow.c +++ b/test/src/vec/test_ft_vecgrow.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 19:41:59 by charles #+# #+# */ -/* Updated: 2020/04/01 21:17:13 by charles ### ########.fr */ +/* Updated: 2020/04/02 10:41:28 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,24 +24,51 @@ TEST(ft_vecgrow, basic) { t_ftvec *vec = ft_vecnew(1); TEST_ASSERT_NOT_NULL(vec); + vec->size = 1; + vec->data[0] = (void*)(0xfefefefeUL << 32); + TEST_ASSERT_NOT_NULL(ft_vecgrow(vec)); TEST_ASSERT_EQUAL(2, vec->capacity); - TEST_ASSERT_EQUAL(0, vec->size); + TEST_ASSERT_EQUAL(1, vec->size); + TEST_ASSERT_EQUAL_HEX64(0xfefefefeUL << 32, vec->data[0]); + vec->size = 2; + vec->data[1] = (void*)(0xfafafafaUL << 32); + TEST_ASSERT_NOT_NULL(ft_vecgrow(vec)); TEST_ASSERT_EQUAL(3, vec->capacity); - TEST_ASSERT_EQUAL(0, vec->size); + TEST_ASSERT_EQUAL(2, vec->size); + TEST_ASSERT_EQUAL_HEX64(0xfefefefeUL << 32, vec->data[0]); + TEST_ASSERT_EQUAL_HEX64(0xfafafafaUL << 32, vec->data[1]); + vec->size = 3; + vec->data[2] = (void*)(0xfbfbfbfbUL << 32); + TEST_ASSERT_NOT_NULL(ft_vecgrow(vec)); TEST_ASSERT_EQUAL(4, vec->capacity); - TEST_ASSERT_EQUAL(0, vec->size); + TEST_ASSERT_EQUAL(3, vec->size); + TEST_ASSERT_EQUAL_HEX64(0xfefefefeUL << 32, vec->data[0]); + TEST_ASSERT_EQUAL_HEX64(0xfafafafaUL << 32, vec->data[1]); + TEST_ASSERT_EQUAL_HEX64(0xfbfbfbfbUL << 32, vec->data[2]); + TEST_ASSERT_NOT_NULL(ft_vecgrow(vec)); TEST_ASSERT_EQUAL(6, vec->capacity); - TEST_ASSERT_EQUAL(0, vec->size); + TEST_ASSERT_EQUAL(3, vec->size); + TEST_ASSERT_EQUAL_HEX64(0xfefefefeUL << 32, vec->data[0]); + TEST_ASSERT_EQUAL_HEX64(0xfafafafaUL << 32, vec->data[1]); + TEST_ASSERT_EQUAL_HEX64(0xfbfbfbfbUL << 32, vec->data[2]); + TEST_ASSERT_NOT_NULL(ft_vecgrow(vec)); TEST_ASSERT_EQUAL(9, vec->capacity); - TEST_ASSERT_EQUAL(0, vec->size); + TEST_ASSERT_EQUAL(3, vec->size); + TEST_ASSERT_EQUAL_HEX64(0xfefefefeUL << 32, vec->data[0]); + TEST_ASSERT_EQUAL_HEX64(0xfafafafaUL << 32, vec->data[1]); + TEST_ASSERT_EQUAL_HEX64(0xfbfbfbfbUL << 32, vec->data[2]); + TEST_ASSERT_NOT_NULL(ft_vecgrow(vec)); TEST_ASSERT_EQUAL(13, vec->capacity); - TEST_ASSERT_EQUAL(0, vec->size); + TEST_ASSERT_EQUAL(3, vec->size); + TEST_ASSERT_EQUAL_HEX64(0xfefefefeUL << 32, vec->data[0]); + TEST_ASSERT_EQUAL_HEX64(0xfafafafaUL << 32, vec->data[1]); + TEST_ASSERT_EQUAL_HEX64(0xfbfbfbfbUL << 32, vec->data[2]); ft_vecdestroy(vec, NULL); } diff --git a/test/src/vec/test_ft_vecinsert.c b/test/src/vec/test_ft_vecinsert.c new file mode 100644 index 0000000..9b89813 --- /dev/null +++ b/test/src/vec/test_ft_vecinsert.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_ft_vecinsert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 22:48:58 by charles #+# #+# */ +/* Updated: 2020/04/02 11:05:14 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_test.h" + +TEST_GROUP(ft_vecinsert); + +TEST_SETUP(ft_vecinsert) +{} + +TEST_TEAR_DOWN(ft_vecinsert) +{} + +TEST(ft_vecinsert, basic) +{ + t_ftvec *vec; + + vec = ft_vecnew(2); + vec->data[0] = (void*)(1UL << 32); + vec->data[1] = (void*)(2UL << 32); + vec->size = 2; + + TEST_ASSERT_NOT_NULL(ft_vecinsert(vec, 0, (void*)(0xfefefefeUL << 32))); + TEST_ASSERT_EQUAL(3, vec->capacity); + TEST_ASSERT_EQUAL(3, vec->size); + TEST_ASSERT_EQUAL_HEX64((void*)(0xfefefefeUL << 32), vec->data[0]); + TEST_ASSERT_EQUAL_HEX64((void*)(1UL << 32), vec->data[1]); + TEST_ASSERT_EQUAL_HEX64((void*)(2UL << 32), vec->data[2]); + + TEST_ASSERT_NOT_NULL(ft_vecinsert(vec, 1, (void*)(0xfafafafaUL << 32))); + TEST_ASSERT_EQUAL(4, vec->capacity); + TEST_ASSERT_EQUAL(4, vec->size); + TEST_ASSERT_EQUAL_HEX64((void*)(0xfefefefeUL << 32), vec->data[0]); + TEST_ASSERT_EQUAL_HEX64((void*)(0xfafafafaUL << 32), vec->data[1]); + TEST_ASSERT_EQUAL_HEX64((void*)(1UL << 32), vec->data[2]); + TEST_ASSERT_EQUAL_HEX64((void*)(2UL << 32), vec->data[3]); + + TEST_ASSERT_NOT_NULL(ft_vecinsert(vec, 2, (void*)(0xfbfbfbfbUL << 32))); + TEST_ASSERT_EQUAL(6, vec->capacity); + TEST_ASSERT_EQUAL(5, vec->size); + TEST_ASSERT_EQUAL_HEX64((void*)(0xfefefefeUL << 32), vec->data[0]); + TEST_ASSERT_EQUAL_HEX64((void*)(0xfafafafaUL << 32), vec->data[1]); + TEST_ASSERT_EQUAL_HEX64((void*)(0xfbfbfbfbUL << 32), vec->data[2]); + TEST_ASSERT_EQUAL_HEX64((void*)(1UL << 32), vec->data[3]); + + TEST_ASSERT_NOT_NULL(ft_vecinsert(vec, vec->size, (void*)(0xfcUL << 31))); + TEST_ASSERT_EQUAL(6, vec->capacity); + TEST_ASSERT_EQUAL(6, vec->size); + TEST_ASSERT_EQUAL_HEX64((void*)(0xfefefefeUL << 32), vec->data[0]); + TEST_ASSERT_EQUAL_HEX64((void*)(0xfafafafaUL << 32), vec->data[1]); + TEST_ASSERT_EQUAL_HEX64((void*)(0xfbfbfbfbUL << 32), vec->data[2]); + TEST_ASSERT_EQUAL_HEX64((void*)(1UL << 32), vec->data[3]); + TEST_ASSERT_EQUAL_HEX64((void*)(2UL << 32), vec->data[4]); + TEST_ASSERT_EQUAL_HEX64((void*)(0xfcUL << 31), vec->data[5]); + + ft_vecdestroy(vec, NULL); +} -- cgit From 948c0953527fe3bef28904b38a16a9e4342e7e98 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 3 Apr 2020 00:29:26 +0200 Subject: Added ft_fnmatch function --- include/libft_str.h | 9 ++- src/str/ft_fnmatch.c | 37 ++++++++++++ test/src/main.c | 1 + test/src/runner/test_runner_str.c | 5 ++ test/src/str/test_ft_fnmatch.c | 117 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 src/str/ft_fnmatch.c create mode 100644 test/src/str/test_ft_fnmatch.c diff --git a/include/libft_str.h b/include/libft_str.h index ea6115d..1b0703f 100644 --- a/include/libft_str.h +++ b/include/libft_str.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:39:22 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:09:56 by cacharle ### ########.fr */ +/* Updated: 2020/04/02 23:45:42 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ # define LIBFT_STR_H # include +# include # include "libft_ctype.h" typedef enum @@ -74,6 +75,12 @@ int ft_strnequ(char const *s1, char const *s2, size_t n); char *ft_strtolower(char *s); char *ft_strtoupper(char *s); +/* +** glob +*/ + +bool ft_fnmatch(const char *pattern, const char *string); + /* ** bloat ? */ diff --git a/src/str/ft_fnmatch.c b/src/str/ft_fnmatch.c new file mode 100644 index 0000000..5fc35d8 --- /dev/null +++ b/src/str/ft_fnmatch.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_fnmatch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/02 23:24:16 by charles #+# #+# */ +/* Updated: 2020/04/03 00:28:46 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Search a glob pattern in a string +** \param pattern Glob pattern '*' are interpreted as zero or more character +** \param string String to search in +** \return True if pattern was found, false otherwise +*/ + +bool ft_fnmatch(const char *pattern, const char *string) +{ + if (*pattern == '\0') + return (*string == '\0'); + if (*string == '\0') + return (*pattern == '\0' || (*pattern == '*' && pattern[1] == '\0')); + if (*pattern == '*') + { + if (ft_fnmatch(pattern + 1, string)) + return (true); + return (ft_fnmatch(pattern, string + 1)); + } + if (*pattern != *string) + return (false); + return (ft_fnmatch(pattern + 1, string + 1)); +} diff --git a/test/src/main.c b/test/src/main.c index ad5c9e8..b9e8bf7 100644 --- a/test/src/main.c +++ b/test/src/main.c @@ -29,6 +29,7 @@ static void run_all_test(void) // str RUN_TEST_GROUP(ft_strlen); + RUN_TEST_GROUP(ft_fnmatch); // ht RUN_TEST_GROUP(ft_htentry_new); diff --git a/test/src/runner/test_runner_str.c b/test/src/runner/test_runner_str.c index 3d829ad..a299786 100644 --- a/test/src/runner/test_runner_str.c +++ b/test/src/runner/test_runner_str.c @@ -4,3 +4,8 @@ TEST_GROUP_RUNNER(ft_strlen) { RUN_TEST_CASE(ft_strlen, basic); } + +TEST_GROUP_RUNNER(ft_fnmatch) +{ + RUN_TEST_CASE(ft_fnmatch, basic); +} diff --git a/test/src/str/test_ft_fnmatch.c b/test/src/str/test_ft_fnmatch.c new file mode 100644 index 0000000..f7dc892 --- /dev/null +++ b/test/src/str/test_ft_fnmatch.c @@ -0,0 +1,117 @@ +#include "libft_test.h" + +TEST_GROUP(ft_fnmatch); + +TEST_SETUP(ft_fnmatch) +{} + +TEST_TEAR_DOWN(ft_fnmatch) +{} + +TEST(ft_fnmatch, basic) +{ + TEST_ASSERT_TRUE(ft_fnmatch("", "")); + TEST_ASSERT_TRUE(ft_fnmatch("l", "l")); + TEST_ASSERT_TRUE(ft_fnmatch("kl", "kl")); + TEST_ASSERT_TRUE(ft_fnmatch("jkl", "jkl")); + TEST_ASSERT_TRUE(ft_fnmatch("ijkl", "ijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("hijkl", "hijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("ghijkl", "ghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("fghijkl", "fghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("efghijkl", "efghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("defghijkl", "defghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("cdefghijkl", "cdefghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("bcdefghijkl", "bcdefghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("abcdefghijkl", "abcdefghijkl")); + + TEST_ASSERT_FALSE(ft_fnmatch("", "a")); + TEST_ASSERT_FALSE(ft_fnmatch("", "ab")); + TEST_ASSERT_FALSE(ft_fnmatch("", "abc")); + TEST_ASSERT_FALSE(ft_fnmatch("a", "ab")); + TEST_ASSERT_FALSE(ft_fnmatch("ab", "abc")); + TEST_ASSERT_FALSE(ft_fnmatch("a", "")); + TEST_ASSERT_FALSE(ft_fnmatch("ab", "")); + TEST_ASSERT_FALSE(ft_fnmatch("abc", "")); + TEST_ASSERT_FALSE(ft_fnmatch("ab", "a")); + TEST_ASSERT_FALSE(ft_fnmatch("abc", "ab")); + + TEST_ASSERT_TRUE(ft_fnmatch("*", "")); + TEST_ASSERT_TRUE(ft_fnmatch("*", "l")); + TEST_ASSERT_TRUE(ft_fnmatch("*", "kl")); + TEST_ASSERT_TRUE(ft_fnmatch("*", "jkl")); + TEST_ASSERT_TRUE(ft_fnmatch("*", "ijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("*", "hijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("*", "ghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("*", "fghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("*", "efghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("*", "defghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("*", "cdefghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("*", "bcdefghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("*", "abcdefghijkl")); + + TEST_ASSERT_TRUE(ft_fnmatch("*.c", ".c")); + TEST_ASSERT_TRUE(ft_fnmatch("*.c", "l.c")); + TEST_ASSERT_TRUE(ft_fnmatch("*.c", "kl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("*.c", "jkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("*.c", "ijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("*.c", "hijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("*.c", "ghijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("*.c", "fghijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("*.c", "efghijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("*.c", "defghijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("*.c", "cdefghijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("*.c", "bcdefghijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("*.c", "abcdefghijkl.c")); + + TEST_ASSERT_TRUE(ft_fnmatch("../*", "../")); + TEST_ASSERT_TRUE(ft_fnmatch("../*", "../l")); + TEST_ASSERT_TRUE(ft_fnmatch("../*", "../kl")); + TEST_ASSERT_TRUE(ft_fnmatch("../*", "../jkl")); + TEST_ASSERT_TRUE(ft_fnmatch("../*", "../ijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("../*", "../hijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("../*", "../ghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("../*", "../fghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("../*", "../efghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("../*", "../defghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("../*", "../cdefghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("../*", "../bcdefghijkl")); + TEST_ASSERT_TRUE(ft_fnmatch("../*", "../abcdefghijkl")); + + TEST_ASSERT_TRUE(ft_fnmatch("../*.c", "../.c")); + TEST_ASSERT_TRUE(ft_fnmatch("../*.c", "../l.c")); + TEST_ASSERT_TRUE(ft_fnmatch("../*.c", "../kl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("../*.c", "../jkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("../*.c", "../ijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("../*.c", "../hijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("../*.c", "../ghijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("../*.c", "../fghijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("../*.c", "../efghijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("../*.c", "../defghijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("../*.c", "../cdefghijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("../*.c", "../bcdefghijkl.c")); + TEST_ASSERT_TRUE(ft_fnmatch("../*.c", "../abcdefghijkl.c")); + + TEST_ASSERT_TRUE(ft_fnmatch("*.*", ".")); + TEST_ASSERT_TRUE(ft_fnmatch("*.*", "a.a")); + TEST_ASSERT_TRUE(ft_fnmatch("*.*", "ab.ab")); + TEST_ASSERT_TRUE(ft_fnmatch("*.*", "abc.abc")); + TEST_ASSERT_TRUE(ft_fnmatch("*.*", "abcd.abcd")); + TEST_ASSERT_TRUE(ft_fnmatch("*.*", "abcde.abcde")); + TEST_ASSERT_TRUE(ft_fnmatch("*.*", "abcdef.abcdef")); + TEST_ASSERT_TRUE(ft_fnmatch("*.*", "abcdefg.abcdefg")); + TEST_ASSERT_TRUE(ft_fnmatch("*.*", "abcdefgh.abcdefgh")); + + TEST_ASSERT_FALSE(ft_fnmatch("*.c", ".h")); + TEST_ASSERT_FALSE(ft_fnmatch("*.c", "a.h")); + TEST_ASSERT_FALSE(ft_fnmatch("*.c", "ab.h")); + TEST_ASSERT_FALSE(ft_fnmatch("*.c", "abc.h")); + TEST_ASSERT_FALSE(ft_fnmatch("*.c", "abcd.h")); + + TEST_ASSERT_FALSE(ft_fnmatch("*.c", ".h")); + TEST_ASSERT_FALSE(ft_fnmatch("*.c", "a.h")); + TEST_ASSERT_FALSE(ft_fnmatch("*.c", "ab.h")); + TEST_ASSERT_FALSE(ft_fnmatch("*.c", "abc.h")); + TEST_ASSERT_FALSE(ft_fnmatch("*.c", "abcd.h")); + + TEST_ASSERT_TRUE(ft_fnmatch("da*da*da*", "daaadabadmanda")); +} -- cgit From d2feec1f97e9f8f201e56ad33662bb663c328a0a Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 3 Apr 2020 07:19:25 +0200 Subject: Changing hash table del function to regular one with a first order internal function, removing a few typedef to instead use standard types --- include/libft.h | 14 +++++----- include/libft_algo.h | 11 +++----- include/libft_def.h | 36 +++++++++++++++++++++++++ include/libft_ht.h | 50 +++++++++++++++++----------------- include/libft_lst.h | 2 +- include/libft_mem.h | 2 +- include/libft_types.h | 30 --------------------- src/algo/ft_is_set.c | 5 ++-- src/ht/ft_htdelone.c | 11 ++++---- src/ht/ft_htdestroy.c | 10 ++++--- src/ht/ft_htget.c | 6 ++--- src/ht/ft_hthash.c | 4 +-- src/ht/ft_htnew.c | 8 +++--- src/ht/ft_htset.c | 37 +++++++++++++------------ src/ht/ft_inter_htdel_first_order.c | 33 +++++++++++++++++++++++ src/str/ft_strcasecmp.c | 2 +- src/str/ft_strncasecmp.c | 2 +- src/str/ft_strncmp.c | 2 +- test/src/ht/test_ft_htdelone.c | 28 ++++++++----------- test/src/ht/test_ft_htdestroy.c | 18 +++++-------- test/src/ht/test_ft_htget.c | 54 +++++++++++++++++-------------------- test/src/ht/test_ft_htset.c | 16 ++++------- test/src/runner/test_runner_ht.c | 2 +- 23 files changed, 199 insertions(+), 184 deletions(-) create mode 100644 include/libft_def.h delete mode 100644 include/libft_types.h create mode 100644 src/ht/ft_inter_htdel_first_order.c 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 # include # include +# include # include # include -# include "libft_types.h" -# include "libft_ctype.h" -# include "libft_io.h" -# include "libft_mem.h" -# include "libft_str.h" - # ifdef __linux__ # include # 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 -# include # 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_def.h b/include/libft_def.h new file mode 100644 index 0000000..69a7655 --- /dev/null +++ b/include/libft_def.h @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_def.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/31 10:36:56 by cacharle #+# #+# */ +/* Updated: 2020/04/03 07:04:57 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_DEF_H +# define LIBFT_DEF_H + +# include +# include + +# define TRUE 1 +# define FALSE 0 + +typedef unsigned char t_ftbyte; +typedef int t_ftbool; +typedef unsigned int t_ftsize; + +typedef char t_ftchar; +typedef unsigned char t_ftuchar; +typedef int t_ftint; +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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 f